/** * Ths method manage associated products to the order when updating it */ public function manageOrderProducts() { // load supply order $id_supply_order = (int) Tools::getValue('id_supply_order', null); $products_already_in_order = array(); if ($id_supply_order != null) { $supply_order = new SupplyOrder($id_supply_order); if (Validate::isLoadedObject($supply_order)) { // tests if the supplier or currency have changed in the supply order $new_supplier_id = (int) Tools::getValue('id_supplier'); $new_currency_id = (int) Tools::getValue('id_currency'); if ($new_supplier_id != $supply_order->id_supplier || $new_currency_id != $supply_order->id_currency) { // resets all products in this order $supply_order->resetProducts(); } else { $products_already_in_order = $supply_order->getEntries(); $currency = new Currency($supply_order->id_ref_currency); // gets all product ids to manage $product_ids_str = Tools::getValue('product_ids', null); $product_ids = explode('|', $product_ids_str); $product_ids_to_delete_str = Tools::getValue('product_ids_to_delete', null); $product_ids_to_delete = array_unique(explode('|', $product_ids_to_delete_str)); //delete products that are not managed anymore foreach ($products_already_in_order as $paio) { $product_ok = false; foreach ($product_ids_to_delete as $id) { $id_check = $paio['id_product'] . '_' . $paio['id_product_attribute']; if ($id_check == $id) { $product_ok = true; } } if ($product_ok === true) { $entry = new SupplyOrderDetail($paio['id_supply_order_detail']); $entry->delete(); } } // manage each product foreach ($product_ids as $id) { $errors = array(); // check if a checksum is available for this product and test it $check = Tools::getValue('input_check_' . $id, ''); $check_valid = md5(_COOKIE_KEY_ . $id); if ($check_valid != $check) { continue; } $pos = strpos($id, '_'); if ($pos === false) { continue; } // Load / Create supply order detail $entry = new SupplyOrderDetail(); $id_supply_order_detail = (int) Tools::getValue('input_id_' . $id, 0); if ($id_supply_order_detail > 0) { $existing_entry = new SupplyOrderDetail($id_supply_order_detail); if (Validate::isLoadedObject($supply_order)) { $entry =& $existing_entry; } } // get product informations $entry->id_product = substr($id, 0, $pos); $entry->id_product_attribute = substr($id, $pos + 1); $entry->unit_price_te = (double) str_replace(array(' ', ','), array('', '.'), Tools::getValue('input_unit_price_te_' . $id, 0)); $entry->quantity_expected = (int) str_replace(array(' ', ','), array('', '.'), Tools::getValue('input_quantity_expected_' . $id, 0)); $entry->discount_rate = (double) str_replace(array(' ', ','), array('', '.'), Tools::getValue('input_discount_rate_' . $id, 0)); $entry->tax_rate = (double) str_replace(array(' ', ','), array('', '.'), Tools::getValue('input_tax_rate_' . $id, 0)); $entry->reference = Tools::getValue('input_reference_' . $id, ''); $entry->supplier_reference = Tools::getValue('input_supplier_reference_' . $id, ''); $entry->ean13 = Tools::getValue('input_ean13_' . $id, ''); $entry->upc = Tools::getValue('input_upc_' . $id, ''); //get the product name in the order language $entry->name = Product::getProductName($entry->id_product, $entry->id_product_attribute, $supply_order->id_lang); if (empty($entry->name)) { $entry->name = ''; } if ($entry->supplier_reference == null) { $entry->supplier_reference = ''; } $entry->exchange_rate = $currency->conversion_rate; $entry->id_currency = $currency->id; $entry->id_supply_order = $supply_order->id; $errors = $entry->validateController(); //get the product name displayed in the backoffice according to the employee language $entry->name_displayed = Tools::getValue('input_name_displayed_' . $id, ''); // if there is a problem, handle error for the current product if (count($errors) > 0) { // add the product to error array => display again product line $this->order_products_errors[] = array('id_product' => $entry->id_product, 'id_product_attribute' => $entry->id_product_attribute, 'unit_price_te' => $entry->unit_price_te, 'quantity_expected' => $entry->quantity_expected, 'discount_rate' => $entry->discount_rate, 'tax_rate' => $entry->tax_rate, 'name' => $entry->name, 'name_displayed' => $entry->name_displayed, 'reference' => $entry->reference, 'supplier_reference' => $entry->supplier_reference, 'ean13' => $entry->ean13, 'upc' => $entry->upc); $error_str = '<ul>'; foreach ($errors as $e) { $error_str .= '<li>' . sprintf($this->l('Field: %s'), $e) . '</li>'; } $error_str .= '</ul>'; $this->errors[] = sprintf(Tools::displayError('Please verify the product information for "%s":'), $entry->name) . ' ' . $error_str; } else { $entry->save(); } } } } } }
/** * Ths method manage associated products to the order when updating it */ public function manageOrderProducts() { // load supply order $id_supply_order = (int) Tools::getValue('id_supply_order', null); $products_already_in_order = array(); if ($id_supply_order != null) { $supply_order = new SupplyOrder($id_supply_order); if (Validate::isLoadedObject($supply_order)) { // tests if the supplier or currency have changed in the supply order $new_supplier_id = (int) Tools::getValue('id_supplier'); $new_currency_id = (int) Tools::getValue('id_currency'); if ($new_supplier_id != $supply_order->id_supplier || $new_currency_id != $supply_order->id_currency) { // resets all products in this order $supply_order->resetProducts(); } else { $products_already_in_order = $supply_order->getEntries(); $currency = new Currency($supply_order->id_ref_currency); // gets all product ids to manage $product_ids_str = Tools::getValue('product_ids', null); $product_ids = explode('|', $product_ids_str); $product_ids_to_delete_str = Tools::getValue('product_ids_to_delete', null); $product_ids_to_delete = array_unique(explode('|', $product_ids_to_delete_str)); //delete products that are not managed anymore foreach ($products_already_in_order as $paio) { $product_ok = false; foreach ($product_ids_to_delete as $id) { $id_check = $paio['id_product'] . '_' . $paio['id_product_attribute']; if ($id_check == $id) { $product_ok = true; } } if ($product_ok === true) { $entry = new SupplyOrderDetail($paio['id_supply_order_detail']); $entry->delete(); //--ERP information // delete bind in erp_supply_order_detail table $id_erpip_supply_order_detail = ErpSupplyOrderDetail::getErpSupplierOrderDetailIdBySupplierOrderDetailId((int) $paio['id_supply_order_detail']); if ((int) $id_erpip_supply_order_detail > 0) { $erp_supply_order_detail = new ErpSupplyOrderDetail((int) $id_erpip_supply_order_detail); $erp_supply_order_detail->delete(); } } } // manage each product foreach ($product_ids as $id) { // if quantity is null, we dont save product in supply order if (Tools::getValue('input_quantity_expected_' . $id, 0) == '0') { continue; } $errors = array(); // check if a checksum is available for this product and test it $check = Tools::getValue('input_check_' . $id, ''); $check_valid = md5(_COOKIE_KEY_ . $id); if ($check_valid != $check) { continue; } $pos = strpos($id, '_'); if ($pos === false) { continue; } // Load / Create supply order detail $entry = new SupplyOrderDetail(); $id_supply_order_detail = (int) Tools::getValue('input_id_' . $id, 0); if ($id_supply_order_detail > 0) { $existing_entry = new SupplyOrderDetail($id_supply_order_detail); if (Validate::isLoadedObject($supply_order)) { $entry =& $existing_entry; } } // get product informations $entry->id_product = Tools::substr($id, 0, $pos); $entry->id_product_attribute = Tools::substr($id, $pos + 1); $entry->unit_price_te = (double) str_replace(array(' ', ','), array('', '.'), Tools::getValue('input_unit_price_te_' . $id, 0)); $entry->quantity_expected = (int) str_replace(array(' ', ','), array('', '.'), Tools::getValue('input_quantity_expected_' . $id, 0)); $entry->discount_rate = (double) str_replace(array(' ', ','), array('', '.'), Tools::getValue('input_discount_rate_' . $id, 0)); $entry->tax_rate = (double) str_replace(array(' ', ','), array('', '.'), Tools::getValue('input_tax_rate_' . $id, 0)); $entry->reference = Tools::getValue('input_reference_' . $id, ''); $entry->supplier_reference = Tools::getValue('input_supplier_reference_' . $id, ''); $entry->ean13 = Tools::getValue('input_ean13_' . $id, ''); $entry->upc = Tools::getValue('input_upc_' . $id, ''); // fixed bug of discount_value_te field while % is null if ($entry->discount_rate == '0.000000') { $entry->discount_value_te = '0.000000'; } //get the product name in the order language $entry->name = Product::getProductName($entry->id_product, $entry->id_product_attribute, $supply_order->id_lang); if (empty($entry->name)) { $entry->name = ''; } if ($entry->supplier_reference == null) { $entry->supplier_reference = ''; } $entry->exchange_rate = $currency->conversion_rate; $entry->id_currency = $currency->id; $entry->id_supply_order = $supply_order->id; $errors = $entry->validateController(); //get the product name displayed in the backoffice according to the employee language $entry->name_displayed = Tools::getValue('input_name_displayed_' . $id, ''); // if there is a problem, handle error for the current product if (count($errors) > 0) { // add the product to error array => display again product line $this->order_products_errors[] = array('id_product' => $entry->id_product, 'id_product_attribute' => $entry->id_product_attribute, 'unit_price_te' => $entry->unit_price_te, 'quantity_expected' => $entry->quantity_expected, 'discount_rate' => $entry->discount_rate, 'tax_rate' => $entry->tax_rate, 'name' => $entry->name, 'name_displayed' => $entry->name_displayed, 'reference' => $entry->reference, 'supplier_reference' => $entry->supplier_reference, 'ean13' => $entry->ean13, 'upc' => $entry->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:') . $entry->name . ' ' . $error_str); } else { $entry->save(); } //-ERP information // updates/creates erp_supplier_order_detail if it does not exist if (Tools::isSubmit('id_erpip_supply_order_detail_' . $id) && (int) Tools::getValue('id_erpip_supply_order_detail_' . $id) > 0) { $erp_supply_order_detail = new ErpSupplyOrderDetail((int) Tools::getValue('id_erpip_supply_order_detail_' . $id)); } else { $erp_supply_order_detail = new ErpSupplyOrderDetail(); } // creates erp_supplier_order_detail $erp_supply_order_detail->comment = Tools::getValue('input_comment_' . $id, ''); $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 { if (Tools::isSubmit('id_erpip_supply_order_detail_' . $id) && Tools::getValue('id_erpip_supply_order_detail_' . $id) > 0) { $erp_supply_order_detail->update(); } else { $erp_supply_order_detail->save(); $_POST['id_erpip_supply_order_detail'][$erp_supply_order_detail->id] = $entry->id; } } } } } } }