public function processGenerateTransferPDF()
 {
     // We get movments
     if (Tools::isSubmit('ids_mvt')) {
         $ids_mvt = Tools::getValue('ids_mvt');
         $movements = new ErpStockMvt();
         $this->generatePDF(array($movements->getMovementsByIds($ids_mvt)), 'Transfer', 'P');
     }
 }
 /* stock update */
 $stock = new ErpStock($initial_stock_mvt[0]['id_stock']);
 $stock->physical_quantity -= Tools::getValue('quantity');
 $stock->usable_quantity -= Tools::getValue('quantity');
 /* If stock  update ok .. */
 if ($stock->update()) {
     /*  Synchro of stocks */
     StockAvailable::synchronize($stock->id_product);
     /*  Add stock mvt reason if not exists (lang non installed at the module installation) */
     $query = 'SELECT * FROM ' . _DB_PREFIX_ . 'stock_mvt_reason_lang WHERE id_stock_mvt_reason = ' . (int) Configuration::get('ERP_RECEPTION_CANCELING_ID') . ' AND id_lang = ' . (int) Context::getContext()->language->id;
     $results = Db::getInstance()->ExecuteS($query);
     if ($results <= 0) {
         Db::getInstance()->insert(_DB_PREFIX_ . 'stock_mvt_reason_lang', array('id_stock_mvt_reason' => Configuration::get('ERP_RECEPTION_CANCELING_ID'), 'id_lang' => (int) Context::getContext()->language->id, 'name' => $erpip->l('Reception cancelling')));
     }
     /* Opposite stock Mvt creation */
     $stock_mvt = new ErpStockMvt();
     $stock_mvt->id_stock = $initial_stock_mvt[0]['id_stock'];
     $stock_mvt->id_order = 0;
     $stock_mvt->id_supply_order = $initial_stock_mvt[0]['id_supply_order'];
     $stock_mvt->id_stock_mvt_reason = Configuration::get('ERP_RECEPTION_CANCELING_ID');
     $stock_mvt->id_employee = Tools::getValue('id_employee');
     $stock_mvt->employee_lastname = Tools::getValue('employee_lastname');
     $stock_mvt->employee_firstname = Tools::getValue('employee_firstname');
     $stock_mvt->physical_quantity = Tools::getValue('quantity');
     $stock_mvt->date_add = Date('Y-m-d h:s:i');
     $stock_mvt->sign = -1;
     $stock_mvt->price_te = $initial_stock_mvt[0]['price_te'];
     $stock_mvt->last_wa = $initial_stock_mvt[0]['last_wa'];
     $stock_mvt->current_wa = $initial_stock_mvt[0]['current_wa'];
     $stock_mvt->referer = 0;
     if ($stock_mvt->add()) {
 public function processGenerateTransferPDF()
 {
     // if we have movments
     $ids_mvt = Tools::getValue('ids_mvt');
     if (!empty($ids_mvt)) {
         require_once _PS_MODULE_DIR_ . 'erpillicopresta/classes/pdf/HTMLTemplateTransfer.php';
         $movements = new ErpStockMvt();
         $pdf = new PDF(array($movements->getMovementsByIds($ids_mvt)), 'Transfer', Context::getContext()->smarty);
         $pdf->render(true);
     }
 }
 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'));
         }
     }
 }