public function postProcess() { // Warehouse $warehouses = Warehouse::getWarehouses(true); // if we already pick a warehouse source and location, we filter on twice to not display selections of the other if (Tools::isSubmit('warehouseA') && Tools::getValue('warehouseA') != -1 && Tools::isSubmit('warehouseB') && Tools::getValue('warehouseB') != -1) { $warehouseA = Tools::getValue('warehouseA'); $warehouseB = Tools::getValue('warehouseB'); $warehousesA = array(); $warehousesB = array(); foreach ($warehouses as $warehouse) { if ($warehouse['id_warehouse'] != $warehouseA) { array_push($warehousesB, $warehouse); } if ($warehouse['id_warehouse'] != $warehouseB) { array_push($warehousesA, $warehouse); } } } elseif (Tools::isSubmit('warehouseA') && Tools::getValue('warehouseA') != -1) { $warehouseA = Tools::getValue('warehouseA'); $warehousesB = array(); foreach ($warehouses as $warehouse) { if ($warehouse['id_warehouse'] != $warehouseA) { array_push($warehousesB, $warehouse); } } $warehousesA = $warehouses; // Restaure B list on no selection $this->context->smarty->assign(array('warehouseB' => -1)); } elseif (Tools::isSubmit('warehouseB') && Tools::getValue('warehouseB') != -1) { $warehouseB = Tools::getValue('warehouseB'); $warehousesA = array(); foreach ($warehouses as $warehouse) { if ($warehouse['id_warehouse'] != $warehouseB) { array_push($warehousesA, $warehouse); } } $warehousesB = $warehouses; } else { $warehousesA = $warehouses; $warehousesB = $warehouses; } // validate_transfer if (Tools::isSubmit('validate_transfer')) { $transfer_ok = false; if (Tools::isSubmit('id_stockA') && Tools::isSubmit('id_stockB') && Tools::isSubmit('id_employee') && Tools::isSubmit('firstname') && Tools::isSubmit('lastname') && Tools::isSubmit('values')) { // After transfert, delete cookies to get a blank display if (Tools::isSubmit('deleteCookie')) { $cookie = new Cookie('psAdmin'); $cookie->warehouseA = ''; $cookie->warehouseB = ''; } /* Initialisation */ $ids_mvt = array(); // Call the helper "traducteur" of the chain transfert require_once _PS_MODULE_DIR_ . 'erpillicopresta/classes/helpers/StockTransferHelper.php'; require_once _PS_MODULE_DIR_ . 'erpillicopresta/classes/stock/ErpStock.php'; $values = StockTransferHelper::getTransfertDataAsArray(Tools::getValue('values')); /* For each movement recorded */ foreach ($values as $value) { $id_product = $value['id_product']; $id_product_attribute = $value['id_product_attribute']; $transfer_quantity = $value['quantity']; $id_stock_s1 = $value['id_stock_s1']; $id_stock_s2 = $value['id_stock_s2']; if (empty($id_stock_s1)) { $this->errors[] = 'Error while updating the stock for a product : stock id missing !'; return true; } // update stock un warehouse A (source) $stock = new ErpStock((int) $id_stock_s1); $stock->physical_quantity -= $transfer_quantity; $stock->usable_quantity -= $transfer_quantity; if ($stock->physical_quantity < 0) { $stock->physical_quantity = 0; } if ($stock->usable_quantity < 0) { $stock->usable_quantity = 0; } /* Update Stock */ if ($stock->update()) { /* Movment A to B */ $stock_mvt = new ErpStockMvt(); $stock_mvt->id_stock = $stock->id; $stock_mvt->id_order = 0; $stock_mvt->id_supply_order = 0; $stock_mvt->id_stock_mvt_reason = 6; $stock_mvt->id_employee = Tools::getValue('id_employee'); $stock_mvt->employee_firstname = Tools::getValue('firstname'); $stock_mvt->employee_lastname = Tools::getValue('lastname'); $stock_mvt->price_te = $stock->getPriceTe(); $stock_mvt->current_wa = $stock->getPriceTe(); $stock_mvt->sign = -1; $stock_mvt->physical_quantity = $transfer_quantity; // if movment OK, update stock in B warehouse (location) if ($stock_mvt->add(true)) { // add id movment stock generated (useful to generate the warehouse delivery notice) array_push($ids_mvt, $stock_mvt->getLastId()); if ((int) $id_stock_s2 > 0) { $stock_s2 = new ErpStock((int) $id_stock_s2); $stock_s2->physical_quantity += $transfer_quantity; $stock_s2->usable_quantity += $transfer_quantity; } else { require_once _PS_MODULE_DIR_ . 'erpillicopresta/classes/ErpProduct.php'; //get product information $product_stock = new ProductCore((int) $id_product, (int) $id_product_attribute); // stock not existe we add row in stock table $stock_s2 = new ErpStock(); $stock_s2->physical_quantity += $transfer_quantity; $stock_s2->usable_quantity += $transfer_quantity; $stock_s2->id_product = (int) $id_product; $stock_s2->id_product_attribute = (int) $id_product_attribute; $stock_s2->id_warehouse = (int) Tools::getValue('id_stockB'); $stock_s2->price_te = $product_stock->wholesale_price; $stock_s2->ean13 = $product_stock->ean13; $stock_s2->upc = $product_stock->upc; } /* of OK, movment from B to A */ if ($stock_s2->save()) { $stock_mvt->id_stock = $stock_s2->id; $stock_mvt->id_stock_mvt_reason = 7; $stock_mvt->sign = 1; // add id movment stock generated (useful to generate warehouse inter delivery) if ($stock_mvt->add(true)) { array_push($ids_mvt, $stock_mvt->getLastId()); } else { $this->errors[] = 'Error while updating the stock for a product'; } } else { $this->errors[] = 'Error while updating the stock for a product'; } } else { $this->errors[] = 'Error while updating the stock for a product'; } } else { $this->errors[] = 'Error while updating the stock for a product'; } // synchronize availbale stock ::quantity StockAvailable::synchronize($id_product); } $ids_mvt = implode('|', $ids_mvt); // we reset values $transfer_ok = true; } } // transfer is ok, we redirect the user to home page of transfer if (Tools::isSubmit('validate_transfer') && isset($transfer_ok) && $transfer_ok) { $url_redirect = self::$currentIndex . '&token=' . Tools::getAdminTokenLite('AdminStockTransfer'); $url_redirect .= '&validate_transfer&ids_mvt=' . $ids_mvt . '&ids_mvt_csv=' . Tools::getValue('ids_mvt_csv'); $url_redirect .= '&id_warehouse_src=' . Tools::getValue('id_warehouse_src') . '&id_warehouse_dst=' . Tools::getValue('id_warehouse_dst'); Tools::redirectAdmin($url_redirect); } if (Tools::isSubmit('ids_mvt_csv') && Tools::isSubmit('get_csv_transfer')) { $this->renderCSV(); } // Generate PDF of tranfert if (Tools::isSubmit('ids_mvt') && Tools::isSubmit('action') && Tools::getValue('action') == 'generateTransferPDF') { $this->processGenerateTransferPDF(); } // to get erp feature list require_once _PS_MODULE_DIR_ . 'erpillicopresta/models/ErpFeature.php'; // send warehouse liste to template $this->context->smarty->assign(array('warehousesA' => $warehousesA, 'warehousesB' => $warehousesB, 'categories' => Category::getSimpleCategories((int) $this->context->language->id), 'suppliers' => Supplier::getSuppliers(), 'manufacturers' => Manufacturer::getManufacturers(), 'id_category' => -1, 'id_supplier' => -1, 'id_manufacturer' => -1, 'name_or_ean' => '', 'erp_feature' => ErpFeature::getFeaturesWithToken($this->context->language->iso_code), 'template_path' => $this->template_path)); }
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')); } } }