public function productHandler($product) { if (empty($product['idproduct'])) { $this->errors[] = Tools::displayError($this->l('Error : the id of the product is missing !')); return false; } $inventory_product = new InventoryProduct(); $inventory_product->id_erpip_inventory = self::$id_erpip_inventory_static; $inventory_product->id_product = $product['idproduct']; $inventory_product->id_product_attribute = $product['idproductattribute']; $inventory_product->qte_before = !isset($product['physicalquantity']) || $product['physicalquantity'] == '' ? 0 : $product['physicalquantity']; $inventory_product->qte_after = !isset($product['foundquantity']) || $product['foundquantity'] == '' ? 0 : (int) $product['foundquantity']; $inventory_product->id_warehouse = Tools::isSubmit('id_warehouse') ? (int) Tools::getValue('id_warehouse') : -1; // if not mvt reason chosen => depend of quanities, select increate or decrease if (!isset($product['idreason']) || $product['idreason'] == '') { if ($inventory_product->qte_before <= $inventory_product->qte_after) { $inventory_product->id_mvt_reason = 1; } else { $inventory_product->id_mvt_reason = 2; } } else { $inventory_product->id_mvt_reason = $product['idreason']; } // Gireg: // only treat products that match to stock manager selected : // Products in advanced stock manager if we re in advanced stock manager // Or product in non advanced stock manager, if we are in non advanced stock manager if ($this->advanced_stock_management == StockAvailable::dependsOnStock((int) $product['idproduct'])) { // If record inventory line --> update stock if ($inventory_product->add()) { // if advanced stock manager if ($this->advanced_stock_management == 1) { $stock = new ErpStock(); $stock->id_product = $product['idproduct']; $stock->id_product_attribute = $product['idproductattribute']; $stock->id_warehouse = $this->id_warehouse; $stock->physical_quantity = !isset($product['foundquantity']) || $product['foundquantity'] == '' ? 0 : (int) $product['foundquantity']; $stock->usable_quantity = !isset($product['foundquantity']) || $product['foundquantity'] == '' ? 0 : (int) $product['foundquantity']; // get reference price $price = $stock->getPriceTe(); // if $price is false, we dont have stock for this product. si quantity is lower than stock, set price to 0 if (isset($product['foundquantity']) && (int) $product['foundquantity'] < $product['physicalquantity'] || ($price = false)) { $price = 0; } $stock->price_te = $price; // if we already have a stock line for this production, we update else we insert if ($stock->id = $stock->getStockId()) { $maj_stock = $stock->update(); } else { $maj_stock = $stock->add(); } } else { $maj_stock = StockAvailable::setQuantity($product['idproduct'], $product['idproductattribute'], (int) $product['foundquantity']); if (is_null($maj_stock)) { $maj_stock = true; } } // if update stock ok --> update location if ($maj_stock) { // no stock change if advanced stock inactive if ($this->advanced_stock_management == 1) { require_once _PS_MODULE_DIR_ . 'erpillicopresta/classes/stock/ErpWarehouseProductLocation.php'; $wpl_id = ErpWarehouseProductLocationClass::getWarehouseProductLocationId($product['idproduct'], $product['idproductattribute']); $warehouse_location = new ErpWarehouseProductLocationClass($wpl_id); $warehouse_location->id_product = $product['idproduct']; $warehouse_location->id_product_attribute = $product['idproductattribute']; $warehouse_location->id_warehouse = $this->id_warehouse; /*if ($product['area'] != '--') $warehouse_location->zone = $product['area']; if ($product['subarea'] != '--') $warehouse_location->sous_zone = $product['subarea'];*/ $warehouse_location->location = $product['location']; $warehouse_location->id_warehouse_product_location = $wpl_id; // if update location ok & advanced stock active -> generate stock movement if ($warehouse_location->update()) { //echo 'Maj location OK'; // No stock movement if advanced stock inactive if ($this->advanced_stock_management == 1) { $stock_mvt = new ErpStockMvt(); $stock_mvt->id_stock = $stock->id; $stock_mvt->id_order = 0; $stock_mvt->id_supply_order = 0; // if not mvt reason selected => depend of quantity to increase or decrease if (!isset($product['idreason']) || $product['idreason'] == '') { if ($inventory_product->qte_before <= $inventory_product->qte_after) { $stock_mvt->id_stock_mvt_reason = 1; } else { $stock_mvt->id_stock_mvt_reason = 2; } } else { $stock_mvt->id_stock_mvt_reason = $product['idreason']; } $stock_mvt->id_employee = $this->id_employee; $stock_mvt->employee_firstname = $this->firstname; $stock_mvt->employee_lastname = $this->lastname; $stock_mvt->price_te = $price; $stock_mvt->current_wa = $price; // Récupération du sign (flèche up / down) // Get sign (arrow up/down) if (isset($product['foundquantity']) && ((int) $product['foundquantity'] >= (int) $product['physicalquantity'] || (int) $product['foundquantity'] == 0)) { $stock_mvt->sign = 1; } else { $stock_mvt->sign = -1; } // calculate the quantity movement of stock $foundquantity = !isset($product['foundquantity']) || $product['foundquantity'] == '' ? 0 : (int) $product['foundquantity']; $physicalquantity = !isset($product['physicalquantity']) || $product['physicalquantity'] == '' ? 0 : (int) $product['physicalquantity']; $mvt = abs($foundquantity - $physicalquantity); $stock_mvt->physical_quantity = $mvt; // Synchronise available stock if ($stock_mvt->add(true)) { StockAvailable::synchronize($product['idproduct']); } else { $this->errors[] = Tools::displayError($this->l('Error while inserting stock movement. Please try again.')); } } else { $this->errors[] = Tools::displayError($this->l('No stock movement. You need to activate the advanced stock management in Preference > Products')); } } else { $this->errors[] = Tools::displayError($this->l('Error while updating product location')); } } } else { $this->errors[] = Tools::displayError($this->l('Error while updating stock')); } } else { $this->errors[] = Tools::displayError($this->l('Error while inserting product inventory row')); } } }