/**
  * Helper function for AdminSupplyOrdersController::postProcess()
  *
  * @see AdminSupplyOrdersController::postProcess()
  */
 protected function postProcessUpdateReceipt()
 {
     // gets all box selected
     $rows = Tools::getValue('supply_order_detailBox');
     if (!$rows) {
         $this->errors[] = Tools::displayError('You did not select any products to update.');
         return;
     }
     // final array with id_supply_order_detail and value to update
     $to_update = array();
     // gets quantity for each id_order_detail
     foreach ($rows as $row) {
         if (Tools::getValue('quantity_received_today_' . $row)) {
             $to_update[$row] = (int) Tools::getValue('quantity_received_today_' . $row);
         }
     }
     // checks if there is something to update
     if (!count($to_update)) {
         $this->errors[] = Tools::displayError('You did not select any products to update.');
         return;
     }
     $supply_order = new SupplyOrder((int) Tools::getValue('id_supply_order'));
     foreach ($to_update as $id_supply_order_detail => $quantity) {
         $supply_order_detail = new SupplyOrderDetail($id_supply_order_detail);
         if (Validate::isLoadedObject($supply_order_detail) && Validate::isLoadedObject($supply_order)) {
             // checks if quantity is valid
             // It's possible to receive more quantity than expected in case of a shipping error from the supplier
             if (!Validate::isInt($quantity) || $quantity <= 0) {
                 $this->errors[] = sprintf(Tools::displayError('Quantity (%d) for product #%d is not valid'), (int) $quantity, (int) $id_supply_order_detail);
             } else {
                 // creates the history
                 $supplier_receipt_history = new SupplyOrderReceiptHistory();
                 $supplier_receipt_history->id_supply_order_detail = (int) $id_supply_order_detail;
                 $supplier_receipt_history->id_employee = (int) $this->context->employee->id;
                 $supplier_receipt_history->employee_firstname = pSQL($this->context->employee->firstname);
                 $supplier_receipt_history->employee_lastname = pSQL($this->context->employee->lastname);
                 $supplier_receipt_history->id_supply_order_state = (int) $supply_order->id_supply_order_state;
                 $supplier_receipt_history->quantity = (int) $quantity;
                 // updates quantity received
                 $supply_order_detail->quantity_received += (int) $quantity;
                 // if current state is "Pending receipt", then we sets it to "Order received in part"
                 if (3 == $supply_order->id_supply_order_state) {
                     $supply_order->id_supply_order_state = 4;
                 }
                 // Adds to stock
                 $warehouse = new Warehouse($supply_order->id_warehouse);
                 if (!Validate::isLoadedObject($warehouse)) {
                     $this->errors[] = Tools::displayError('The warehouse could not be loaded.');
                     return;
                 }
                 $price = $supply_order_detail->unit_price_te;
                 // converts the unit price to the warehouse currency if needed
                 if ($supply_order->id_currency != $warehouse->id_currency) {
                     // first, converts the price to the default currency
                     $price_converted_to_default_currency = Tools::convertPrice($supply_order_detail->unit_price_te, $supply_order->id_currency, false);
                     // then, converts the newly calculated pri-ce from the default currency to the needed currency
                     $price = Tools::ps_round(Tools::convertPrice($price_converted_to_default_currency, $warehouse->id_currency, true), 6);
                 }
                 $manager = StockManagerFactory::getManager();
                 $res = $manager->addProduct($supply_order_detail->id_product, $supply_order_detail->id_product_attribute, $warehouse, (int) $quantity, Configuration::get('PS_STOCK_MVT_SUPPLY_ORDER'), $price, true, $supply_order->id);
                 $location = Warehouse::getProductLocation($supply_order_detail->id_product, $supply_order_detail->id_product_attribute, $warehouse->id);
                 $res = Warehouse::setProductlocation($supply_order_detail->id_product, $supply_order_detail->id_product_attribute, $warehouse->id, $location ? $location : '');
                 if ($res) {
                     $supplier_receipt_history->add();
                     $supply_order_detail->save();
                     StockAvailable::synchronize($supply_order_detail->id_product);
                 } else {
                     $this->errors[] = Tools::displayError('Something went wrong when setting warehouse on product record');
                 }
             }
         }
     }
     $supply_order->id_supply_order_state = $supply_order->id_supply_order_state == 4 && $supply_order->getAllPendingQuantity() > 0 ? 4 : 5;
     $supply_order->save();
     if (!count($this->errors)) {
         // display confirm message
         $token = Tools::getValue('token') ? Tools::getValue('token') : $this->token;
         $redirect = self::$currentIndex . '&token=' . $token;
         $this->redirect_after = $redirect . '&conf=4';
     }
 }
 /**
  * Helper function for AdminAdvancedSupplyOrderController::postProcess()
  *
  * @see AdminAdvancedSupplyOrderController::postProcess()
  */
 protected function postProcessUpdateReceipt()
 {
     // gets all box selected
     $rows = Tools::getValue('supply_order_detailBox');
     if (!$rows) {
         $this->errors[] = Tools::displayError($this->l('You did not select any product to update.'));
         return;
     }
     // final array with id_supply_order_detail and value to update
     $to_update = array();
     $comment = array();
     // gets quantity for each id_order_detail
     //--ERP information add new product
     foreach ($rows as $row) {
         // If _new, it mean new product, then we explode to get the number
         if (strpos($row, "_") !== false) {
             $row = explode('_', $row);
             $row = $row[1];
             // Commande courante
             $supply_order = new SupplyOrder((int) Tools::getValue('id_supply_order'));
             // Currency courant
             $currency = new Currency($supply_order->id_ref_currency);
             // Objet supply order detail
             $supply_order_detail = new SupplyOrderDetail();
             $supply_order_detail->id = 0;
             $supply_order_detail->reference = Tools::getValue("input_reference_{$row}");
             $supply_order_detail->name = Tools::getValue("input_name_displayed_{$row}");
             $supply_order_detail->ean13 = Tools::getValue("input_ean13_{$row}");
             $supply_order_detail->upc = Tools::getValue("input_upc_{$row}");
             $supply_order_detail->unit_price_te = Tools::getValue("input_unit_price_te_{$row}");
             $supply_order_detail->discount_rate = Tools::getValue("input_discount_rate_{$row}");
             $supply_order_detail->tax_rate = Tools::getValue("input_tax_rate_{$row}");
             $supply_order_detail->quantity_expected = 0;
             $supply_order_detail->exchange_rate = $currency->conversion_rate;
             $supply_order_detail->id_currency = $currency->id;
             $supply_order_detail->id_supply_order = $supply_order->id;
             $supply_order_detail->supplier_reference = Tools::getValue("input_supplier_reference_{$row}") == null ? '' : Tools::getValue("input_supplier_reference_{$row}");
             $supply_order_detail->name_displayed = Tools::getValue('input_name_displayed_' . $row);
             $ids = Tools::getValue("input_id_product_{$row}");
             // If declension we explode
             if (strrpos($ids, "_")) {
                 $ids = explode('_', $ids);
                 $supply_order_detail->id_product = $ids[0];
                 $supply_order_detail->id_product_attribute = $ids[1];
             } else {
                 $supply_order_detail->id_product = $ids;
                 $supply_order_detail->id_product_attribute = 0;
             }
             // Name
             $supply_order_detail->name = Product::getProductName($supply_order_detail->id_product, $supply_order_detail->id_product_attribute, $supply_order->id_lang);
             $errors = $supply_order_detail->validateController();
             // if there is a problem, handle error for the current product
             // error > 1 only because quantity_expected is always in error
             // Then errors are displayed if there is more than 1 error
             if (count($errors) > 1) {
                 // add the product to error array => display again product line
                 $this->order_products_errors[] = array('id_product' => $supply_order_detail->id_product, 'id_product_attribute' => $supply_order_detail->id_product_attribute, 'unit_price_te' => $supply_order_detail->unit_price_te, 'quantity_expected' => $supply_order_detail->quantity_expected, 'discount_rate' => $supply_order_detail->discount_rate, 'tax_rate' => $supply_order_detail->tax_rate, 'name' => $supply_order_detail->name, 'name_displayed' => $supply_order_detail->name_displayed, 'reference' => $supply_order_detail->reference, 'supplier_reference' => $supply_order_detail->supplier_reference, 'ean13' => $supply_order_detail->ean13, 'upc' => $supply_order_detail->upc);
                 $error_str = '<ul>';
                 foreach ($errors as $e) {
                     $error_str .= '<li>' . $this->l('field') . ' ' . $e . '</li>';
                 }
                 $error_str .= '</ul>';
                 $this->errors[] = Tools::displayError($this->l('Please check the product information:') . $supply_order_detail->name . ' ' . $error_str);
             } else {
                 $supply_order_detail->save();
                 $to_update[Db::getInstance()->Insert_ID()] = (int) Tools::getValue('quantity_received_today_' . $row);
                 $comment[Db::getInstance()->Insert_ID()] = Tools::getValue('input_comment_' . $row);
                 //--ERP information
                 // creates erp_supplier_order_detail for new product
                 if ((int) $supply_order_detail->id > 0) {
                     $erp_supply_order_detail = new ErpSupplyOrderDetail();
                     $erp_supply_order_detail->id_supply_order_detail = (int) $supply_order_detail->id;
                     $erp_supply_order_detail->comment = Tools::getValue('input_comment_' . $row, '');
                     $validation_esod = $erp_supply_order_detail->validateController();
                     // checks erp_supplier_order_detail validity
                     if (count($validation_esod) > 0) {
                         foreach ($validation_esod as $item) {
                             $this->errors[] = $item;
                         }
                         $this->errors[] = Tools::displayError('The ErpIllicopresta Supplier Order Detail is not correct. Please make sure all of the required fields are completed.');
                     } else {
                         $erp_supply_order_detail->save();
                     }
                 }
             }
         } else {
             if (Tools::isSubmit('quantity_received_today_' . $row)) {
                 if (Tools::isSubmit('input_comment_' . $row)) {
                     $comment[Db::getInstance()->Insert_ID()] = Tools::getValue('input_comment_' . $row);
                 }
                 $to_update[$row] = (int) Tools::getValue('quantity_received_today_' . $row);
             }
         }
     }
     // checks if there is something to update
     if (!count($to_update)) {
         $this->errors[] = Tools::displayError($this->l('You did not select any product to update.'));
         return;
     }
     foreach ($to_update as $id_supply_order_detail => $quantity) {
         $supply_order_detail = new SupplyOrderDetail($id_supply_order_detail);
         $supply_order = new SupplyOrder((int) Tools::getValue('id_supply_order'));
         if (Validate::isLoadedObject($supply_order_detail) && Validate::isLoadedObject($supply_order)) {
             // checks if quantity is valid
             // It's possible to receive more quantity than expected in case of a shipping error from the supplier
             if (!Validate::isInt($quantity) || $quantity <= 0) {
                 $this->errors[] = sprintf(Tools::displayError($this->l('Quantity (%d) for product #%d is not valid')), (int) $quantity, (int) $id_supply_order_detail);
             } else {
                 // creates the history
                 $supplier_receipt_history = new SupplyOrderReceiptHistory();
                 $supplier_receipt_history->id_supply_order_detail = (int) $id_supply_order_detail;
                 $supplier_receipt_history->id_employee = (int) $this->context->employee->id;
                 $supplier_receipt_history->employee_firstname = pSQL($this->context->employee->firstname);
                 $supplier_receipt_history->employee_lastname = pSQL($this->context->employee->lastname);
                 $supplier_receipt_history->id_supply_order_state = (int) $supply_order->id_supply_order_state;
                 $supplier_receipt_history->quantity = (int) $quantity;
                 // updates quantity received
                 $supply_order_detail->quantity_received += (int) $quantity;
                 // if current state is "Pending receipt", then we sets it to "Order received in part"
                 if (3 == $supply_order->id_supply_order_state) {
                     $supply_order->id_supply_order_state = 4;
                 }
                 // Adds to stock
                 $warehouse = new Warehouse($supply_order->id_warehouse);
                 if (!Validate::isLoadedObject($warehouse)) {
                     $this->errors[] = Tools::displayError($this->l('The warehouse could not be loaded.'));
                     return;
                 }
                 $price = $supply_order_detail->unit_price_te;
                 // converts the unit price to the warehouse currency if needed
                 if ($supply_order->id_currency != $warehouse->id_currency) {
                     // first, converts the price to the default currency
                     $price_converted_to_default_currency = Tools::convertPrice($supply_order_detail->unit_price_te, $supply_order->id_currency, false);
                     // then, converts the newly calculated pri-ce from the default currency to the needed currency
                     $price = Tools::ps_round(Tools::convertPrice($price_converted_to_default_currency, $warehouse->id_currency, true), 6);
                 }
                 $manager = StockManagerFactory::getManager();
                 $res = $manager->addProduct($supply_order_detail->id_product, $supply_order_detail->id_product_attribute, $warehouse, (int) $quantity, Configuration::get('PS_STOCK_MVT_SUPPLY_ORDER'), $price, true, $supply_order->id);
                 if ($res) {
                     StockAvailable::synchronize($supply_order_detail->id_product);
                 } else {
                     $this->errors[] = Tools::displayError($this->l('Error while adding products to the warehouse.'));
                 }
                 $location = Warehouse::getProductLocation($supply_order_detail->id_product, $supply_order_detail->id_product_attribute, $warehouse->id);
                 $res = Warehouse::setProductlocation($supply_order_detail->id_product, $supply_order_detail->id_product_attribute, $warehouse->id, $location ? $location : '');
                 //-ERP information
                 //
                 if ($res) {
                     $supplier_receipt_history->add();
                     $supply_order_detail->save();
                     $supply_order->save();
                 } else {
                     $this->errors[] = Tools::displayError($this->l('Error while setting warehouse on product record'));
                 }
             }
         }
     }
     if (!count($this->errors)) {
         // display confirm message
         $token = Tools::getValue('token') ? Tools::getValue('token') : $this->token;
         $redirect = self::$currentIndex . '&token=' . $token;
         $this->redirect_after = $redirect . '&conf=4';
     }
 }