public function beforeValidate($options = array())
 {
     parent::beforeValidate($options);
     //get type config
     if (isset($this->data['type_config'])) {
         $this->type_config = array_merge($this->type_config, $this->data['type_config']);
         unset($this->data['type_config']);
     }
     // check for price as required
     if ($this->type_config['price']['required']) {
         $this->validate['price']['It can not be empty'] = array('rule' => 'notEmpty');
     }
     // check for parent_lookup as required
     if ($this->type_config['parent_lookup']['required']) {
         $this->validate['parent_lookup']['It can not be empty'] = array('rule' => 'notEmpty');
     }
     // check for value as required
     if ($this->type_config['value']['required']) {
         $this->validate['value']['It can not be empty'] = array('rule' => 'notEmpty');
     }
     // check for value as unique
     if ($this->type_config['value']['unique']) {
         //debug($this->id);exit;
         $this->validate['value']['It is already exist'] = array('rule' => array('uniqueValue', 'lookup_type'));
     }
     $this->data['InventoryLookup']['department_id'] = $this->createSearchCache($this->data['InventoryLookup']['department_id']);
 }
 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;
     }
 }
 public function beforeFind($queryData)
 {
     parent::beforeFind($queryData);
     if (!isset($queryData['conditions'])) {
         $queryData['conditions'] = array();
     }
     $queryData['conditions']['ItemDepartment.delete'] = '0';
     return $queryData;
 }
 public function afterSave($created)
 {
     parent::afterSave($created);
     if (isset($this->data['CabinetsItem'])) {
         // delete CabinetsItem
         if ($this->data['CabinetsItem'][-1]['accessories'] == 1) {
             $this->CabinetsItem->deleteAll(array('cabinet_id' => $this->id, 'accessories' => '1'));
         } else {
             $this->CabinetsItem->deleteAll(array('cabinet_id' => $this->id, 'accessories' => '0'));
             $this->CabinetsItem->deleteAll(array('cabinet_id' => $this->id, 'accessories' => null));
         }
         //debug($this->data['CabinetsItem']);
         if (is_array($this->data['CabinetsItem'])) {
             // save CabinetsItem
             $cabinetItems = array();
             $index = 0;
             foreach ($this->data['CabinetsItem'] as $index => $cabinetItem) {
                 if ($index == '-1') {
                     continue;
                     // skip the first entry as it will be invalid
                 }
                 $cabinetItems[$index]['item_id'] = trim($cabinetItem['item_id']);
                 $cabinetItems[$index]['item_quantity'] = trim($cabinetItem['item_quantity']);
                 $cabinetItems[$index]['accessories'] = (int) trim($cabinetItem['accessories']);
                 $cabinetItems[$index]['cabinet_id'] = $this->id;
                 $index++;
             }
             $this->CabinetsItem->saveAll($cabinetItems);
         }
     }
     if (isset($this->data['Cabinet']['CabinetsInstallation'])) {
         // delete CabinetsItem
         $this->CabinetsInstallation->deleteAll(array('cabinet_id' => $this->id));
         //debug($this->data['CabinetsItem']);
         if (is_array($this->data['Cabinet']['CabinetsInstallation'])) {
             // save CabinetsItem
             $cabinetInstallations = array();
             foreach ($this->data['Cabinet']['CabinetsInstallation'] as $index => $cabinetInstallation) {
                 if ($index == -1) {
                     continue;
                 }
                 // skip invalid one
                 $cabinetInstallation['inventory_lookup_id'] = (int) trim($cabinetInstallation['inventory_lookup_id']);
                 $cabinetInstallations[$cabinetInstallation['inventory_lookup_id']]['inventory_lookup_id'] = $cabinetInstallation['inventory_lookup_id'];
                 $cabinetInstallations[$cabinetInstallation['inventory_lookup_id']]['cabinet_id'] = $this->id;
             }
             $this->CabinetsInstallation->saveAll($cabinetInstallations);
         }
     }
     //$this->insert_cabinet_to_drupal($this->id); // add to drupal
 }
 public function beforeSave($options = array())
 {
     parent::beforeSave($options);
     if (is_array($this->data['Supplier']['supplier_type']) && !empty($this->data['Supplier']['supplier_type'])) {
         $this->data['Supplier']['supplier_type'] = serialize($this->data['Supplier']['supplier_type']);
     } else {
         $this->data['Supplier']['supplier_type'] = '';
     }
     if (is_array($this->data['Supplier']['employee_rep']) && !empty($this->data['Supplier']['employee_rep'])) {
         $this->data['Supplier']['employee_rep'] = serialize($this->data['Supplier']['employee_rep']);
     } else {
         $this->data['Supplier']['employee_rep'] = '';
     }
     return true;
 }
 function afterSave($created)
 {
     parent::afterSave($created);
     if (isset($this->data['ColorMaterial'][0]['material_id']) && is_array($this->data['ColorMaterial'][0]['material_id'])) {
         foreach ($this->data['ColorMaterial'][0]['material_id'] as $index => $material_id) {
             $colorMaterials[$index]['color_id'] = $this->data['ColorSection']['color_id'];
             $colorMaterials[$index]['material_id'] = $material_id;
             if (!empty($colorSection['edgetape_id'])) {
                 $colorMaterials[$index]['edgetape_id'] = $this->data['ColorSection']['edgetape_id'];
             } else {
                 unset($colorMaterials[$index]['edgetape_id']);
             }
             $colorMaterials[$index]['color_section_id'] = $this->data['ColorSection']['id'];
         }
         $this->ColorMaterial->saveAll($colorMaterials);
     }
 }
 function afterSave($created)
 {
     parent::afterSave($created);
     //$this->insert_door_to_drupal($this->id); // add to drupal
 }
 public function beforeValidate($options = array())
 {
     parent::beforeValidate($options);
     if (!$this->id) {
         if (isset($this->data['Item']['base_item'])) {
             $item = $this->data;
             $this->read(null, $this->data['Item']['base_item']);
             array_merge($this->data['Item'], $item['Item']);
             $item['Item']['item_group'] = $this->data['Item']['item_group'];
             $this->data = $item;
         }
         App::import('Model', 'Inventory.InventoryLookup');
         $inventorylookup = new InventoryLookup();
         $number = $inventorylookup->find("first", array("conditions" => array("InventoryLookup.id" => $this->data['Item']['item_group'])));
         $items = $this->find('first', array('limit' => 1, 'order' => array('Item.id DESC')));
         $item_id = $items['Item']['id'] + 1;
         $number['InventoryLookup']['num'] = str_pad($item_id, 6, '0', STR_PAD_LEFT);
         if ($this->data['Item']['base_item'] != 0) {
             $this->data['Item']['number'] = $number['InventoryLookup']['value'] . $number['InventoryLookup']['num'];
         }
     }
 }