public function testZeroPriceWithVariations()
 {
     Config::inst()->update('ProductCategory', 'must_have_price', true);
     $products = $this->electronics->ProductsShowable();
     $this->assertEquals(0, $products->count(), 'No product should be returned as there\'s no price set');
     // Create a variation for HDTV
     ProductVariation::create(array('InternalItemID' => '50-Inch', 'Price' => 1200, 'ProductID' => $this->hdtv->ID))->write();
     $products = $this->electronics->ProductsShowable();
     $this->assertDOSEquals(array(array('URLSegment' => 'hdtv')), $products, 'HDTV has a priced extension and should now show up in the list of products');
 }
 /**
  * Generates variations based on selected attributes.
  *
  * @param ProductAttributeType $attributetype
  * @param array                $values
  */
 public function generateVariationsFromAttributes(ProductAttributeType $attributetype, array $values)
 {
     //TODO: introduce transactions here, in case objects get half made etc
     //if product has variation attribute types
     if (!empty($values)) {
         //TODO: get values dataobject set
         $avalues = $attributetype->convertArrayToValues($values);
         $existingvariations = $this->owner->Variations();
         if ($existingvariations->exists()) {
             //delete old variation, and create new ones - to prevent modification of exising variations
             foreach ($existingvariations as $oldvariation) {
                 $oldvalues = $oldvariation->AttributeValues();
                 foreach ($avalues as $value) {
                     $newvariation = $oldvariation->duplicate();
                     $newvariation->InternalItemID = $this->owner->InternalItemID . '-' . $newvariation->ID;
                     $newvariation->AttributeValues()->addMany($oldvalues);
                     $newvariation->AttributeValues()->add($value);
                     $newvariation->write();
                     $existingvariations->add($newvariation);
                 }
                 $existingvariations->remove($oldvariation);
                 $oldvariation->AttributeValues()->removeAll();
                 $oldvariation->delete();
                 $oldvariation->destroy();
                 //TODO: check that old variations actually stick around, as they will be needed for past orders etc
             }
         } else {
             foreach ($avalues as $value) {
                 $variation = ProductVariation::create();
                 $variation->ProductID = $this->owner->ID;
                 $variation->Price = $this->owner->BasePrice;
                 $variation->write();
                 $variation->InternalItemID = $this->owner->InternalItemID . '-' . $variation->ID;
                 $variation->AttributeValues()->add($value);
                 $variation->write();
                 $existingvariations->add($variation);
             }
         }
     }
 }
 public function variationRow(&$obj, $val, $record)
 {
     $obj->write();
     //make sure product is in DB
     //TODO: or find existing variation
     $variation = ProductVariation::get()->filter("InternalItemID", '$val')->first();
     if (!$variation) {
         $variation = ProductVariation::create();
         $variation->InternalItemID = $val;
         $variation->ProductID = $obj->ID;
         //link to product
         $variation->write();
     }
     $varcols = array('->processVariation', '->processVariation1', '->processVariation2', '->processVariation3', '->processVariation4', '->processVariation5', '->processVariation6');
     foreach ($varcols as $col) {
         if (isset($record[$col])) {
             $parts = explode(":", $record[$col]);
             if (count($parts) == 2) {
                 $attributetype = trim($parts[0]);
                 $attributevalues = explode(",", $parts[1]);
                 //get rid of empty values
                 foreach ($attributevalues as $key => $value) {
                     if (!$value || trim($value) == "") {
                         unset($attributevalues[$key]);
                     }
                 }
                 if (count($attributevalues) == 1) {
                     $attributetype = ProductAttributeType::find_or_make($attributetype);
                     foreach ($attributevalues as $key => $value) {
                         $val = trim($value);
                         if ($val != "" && $val != null) {
                             $attributevalues[$key] = $val;
                             //remove outside spaces from values
                         }
                     }
                     $attributetype->addValues($attributevalues);
                     //create and add values to attribute type
                     $obj->VariationAttributeTypes()->add($attributetype);
                     //add variation attribute type to product
                     //TODO: if existing variation, then remove current values
                     //record vairation attribute values (variation1, 2 etc)
                     foreach ($attributetype->convertArrayToValues($attributevalues) as $value) {
                         $variation->AttributeValues()->add($value);
                         break;
                     }
                 }
             }
         }
     }
     //copy db values into variation (InternalItemID, Price, Stock, etc) ...there will be unknowns from extensions.
     $dbfields = $variation->db();
     foreach ($record as $field => $value) {
         if (isset($dbfields[$field])) {
             $variation->{$field} = $value;
         }
     }
     $variation->write();
 }