Example #1
0
 /**
  * Post treatment for warehouses
  */
 public function processWarehouses()
 {
     if ((int) Tools::getValue('warehouse_loaded') === 1 && Validate::isLoadedObject($product = new Product((int) ($id_product = Tools::getValue('id_product'))))) {
         // Get all id_product_attribute
         $attributes = $product->getAttributesResume($this->context->language->id);
         if (empty($attributes)) {
             $attributes[] = array('id_product_attribute' => 0, 'attribute_designation' => '');
         }
         // Get all available warehouses
         $warehouses = Warehouse::getWarehouses(true);
         // Get already associated warehouses
         $associated_warehouses_collection = WarehouseProductLocation::getCollection($product->id);
         $elements_to_manage = array();
         // get form inforamtion
         foreach ($attributes as $attribute) {
             foreach ($warehouses as $warehouse) {
                 $key = $warehouse['id_warehouse'] . '_' . $product->id . '_' . $attribute['id_product_attribute'];
                 // get elements to manage
                 if (Tools::isSubmit('check_warehouse_' . $key)) {
                     $location = Tools::getValue('location_warehouse_' . $key, '');
                     $elements_to_manage[$key] = $location;
                 }
             }
         }
         // Delete entry if necessary
         foreach ($associated_warehouses_collection as $awc) {
             if (!array_key_exists($awc->id_warehouse . '_' . $awc->id_product . '_' . $awc->id_product_attribute, $elements_to_manage)) {
                 $awc->delete();
             }
         }
         // Manage locations
         foreach ($elements_to_manage as $key => $location) {
             $params = explode('_', $key);
             $wpl_id = (int) WarehouseProductLocation::getIdByProductAndWarehouse((int) $params[1], (int) $params[2], (int) $params[0]);
             if (empty($wpl_id)) {
                 //create new record
                 $warehouse_location_entity = new WarehouseProductLocation();
                 $warehouse_location_entity->id_product = (int) $params[1];
                 $warehouse_location_entity->id_product_attribute = (int) $params[2];
                 $warehouse_location_entity->id_warehouse = (int) $params[0];
                 $warehouse_location_entity->location = pSQL($location);
                 $warehouse_location_entity->save();
             } else {
                 $warehouse_location_entity = new WarehouseProductLocation((int) $wpl_id);
                 $location = pSQL($location);
                 if ($location != $warehouse_location_entity->location) {
                     $warehouse_location_entity->location = pSQL($location);
                     $warehouse_location_entity->update();
                 }
             }
         }
         StockAvailable::synchronize((int) $id_product);
     }
 }
 /**
  * @param Product $obj
  * @throws Exception
  * @throws SmartyException
  */
 public function initFormWarehouses($obj)
 {
     $data = $this->createTemplate($this->tpl_form);
     if ($obj->id) {
         if ($this->product_exists_in_shop) {
             // Get all id_product_attribute
             $attributes = $obj->getAttributesResume($this->context->language->id);
             if (empty($attributes)) {
                 $attributes[] = array('id_product' => $obj->id, 'id_product_attribute' => 0, 'attribute_designation' => '');
             }
             $product_designation = array();
             foreach ($attributes as $attribute) {
                 $product_designation[$attribute['id_product_attribute']] = rtrim($obj->name[$this->context->language->id] . ' - ' . $attribute['attribute_designation'], ' - ');
             }
             // Get all available warehouses
             $warehouses = Warehouse::getWarehouses(true);
             // Get already associated warehouses
             $associated_warehouses_collection = WarehouseProductLocation::getCollection($obj->id);
             $data->assign(array('attributes' => $attributes, 'warehouses' => $warehouses, 'associated_warehouses' => $associated_warehouses_collection, 'product_designation' => $product_designation, 'product' => $obj, 'link' => $this->context->link, 'token' => $this->token));
         } else {
             $this->displayWarning($this->l('You must save the product in this shop before managing warehouses.'));
         }
     } else {
         $this->displayWarning($this->l('You must save this product before managing warehouses.'));
     }
     $this->tpl_form_vars['custom_form'] = $data->fetch();
 }