Ejemplo n.º 1
0
 function afterSave($created)
 {
     parent::afterSave($created);
     if (isset($this->data['ColorSection']) && is_array($this->data['ColorSection'])) {
         // delete previous items
         $this->ColorSection->deleteAll(array('color_id' => $this->id));
         $this->ColorMaterial->deleteAll(array('color_id' => $this->id));
         App::uses('ColorSection', 'Inventory.Model');
         foreach ($this->data['ColorSection'] as $index => $colorSection) {
             $colorSection['price'] = $colorSection['cost'] * $colorSection['markup'];
             $colorSection['type'] = $index == 0 ? 'cabinate_material' : 'door_material';
             if ($colorSection['price'] > 0) {
                 $colorSections[$index]['id'] = null;
                 $colorSections[$index]['color_id'] = $this->id;
                 $colorSections[$index]['cost'] = $colorSection['cost'];
                 $colorSections[$index]['markup'] = $colorSection['markup'];
                 $colorSections[$index]['price'] = $colorSection['cost'] * $colorSection['markup'];
                 if (!empty($colorSection['edgetape_id'])) {
                     $colorSections[$index]['edgetape_id'] = $colorSection['edgetape_id'];
                 } else {
                     unset($colorSections[$index]['edgetape_id']);
                 }
                 $colorSections[$index]['type'] = $index == 0 ? 'cabinate_material' : 'door_material';
                 $colorSections[$index]['ColorMaterial'] = $colorSection['ColorMaterial'];
             }
             $color = new ColorSection();
             $color->save($colorSections[$index]);
         }
         //pr($colorSections[$index]);exit;
     }
 }
 function getColor()
 {
     $this->autoRender = false;
     $sql = "select * from inventory where Department = 'Stain';";
     $datas = $this->Cabinet->query($sql);
     App::import("Model", "Inventory.Color");
     App::import("Model", "Inventory.ColorSection");
     $i = 1;
     foreach ($datas as $data) {
         $color_model = new Color();
         $color_data = array();
         $color_data['Color']['name'] = $data['inventory']['Description'];
         $color_data['Color']['code'] = $data['inventory']['Material'] . "C" . $i;
         if (!$color_model->save($color_data)) {
             pr($color_model->validationErrors);
         }
         //Cabinet Material
         $cabinet_material_data = array();
         $cabinet_material_data['ColorSection']['color_id'] = $color_model->id;
         $cabinet_material_data['ColorSection']['edgetape_id'] = 0;
         $cabinet_material_data['ColorSection']['cost'] = $data['inventory']['Cost'];
         $cabinet_material_data['ColorSection']['markup'] = 1;
         $cabinet_material_data['ColorSection']['price'] = $data['inventory']['Price'];
         $cabinet_material_data['ColorSection']['type'] = "cabinate_material";
         $color_section_model1 = new ColorSection();
         if (!$color_section_model1->save($cabinet_material_data)) {
             pr($color_section_model1->validationErrors);
         }
         //Door Material
         $door_material_data = array();
         $door_material_data['ColorSection']['color_id'] = $color_model->id;
         $door_material_data['ColorSection']['edgetape_id'] = 0;
         $door_material_data['ColorSection']['cost'] = $data['inventory']['Cost'];
         $door_material_data['ColorSection']['markup'] = 1;
         $door_material_data['ColorSection']['price'] = $data['inventory']['Price'];
         $door_material_data['ColorSection']['type'] = "door_material";
         $color_section_model2 = new ColorSection();
         if (!$color_section_model2->save($door_material_data)) {
             pr($color_section_model2->validationErrors);
         }
         $i++;
     }
 }