/**
  * @see AdminController::renderView()
  */
 public function renderView()
 {
     require_once _PS_MODULE_DIR_ . 'erpillicopresta/models/ErpSupplyOrderCustomer.php';
     $this->show_toolbar = true;
     $this->toolbar_scroll = false;
     $this->table = 'supply_order_detail';
     $this->identifier = 'id_supply_order_detail';
     $this->className = 'SupplyOrderDetail';
     $this->colorOnBackground = false;
     $this->lang = false;
     $this->list_simple_header = true;
     $this->list_no_link = true;
     // gets the id supplier to view
     $id_supply_order = (int) Tools::getValue('id_supply_order');
     // gets global order information
     $supply_order = new SupplyOrder((int) $id_supply_order);
     if (Validate::isLoadedObject($supply_order)) {
         if (!$supply_order->is_template) {
             $this->displayInformation($this->l('This interface allows you to display detailed information about your order.') . '<br />');
         } else {
             $this->displayInformation($this->l('This interface allows you to display detailed information about your order template.') . '<br />');
         }
         $lang_id = (int) $supply_order->id_lang;
         // just in case..
         unset($this->_select, $this->_join, $this->_where, $this->_orderBy, $this->_orderWay, $this->_group, $this->_filterHaving, $this->_filter);
         $this->_select = 'CONCAT("' . $this->l('Ref') . ' : ", reference, " ", "' . $this->l('Supplier') . ' : ", supplier_reference) as refs';
         $this->_select .= ',CONCAT("EAN13 :", ean13," UPC : ",upc) as bare_codes ';
         // gets all information on the products ordered
         $this->_where = 'AND a.`id_supply_order` = ' . (int) $id_supply_order;
         // gets the list ordered by price desc, without limit
         $this->getList($lang_id, 'price_te', 'DESC', 0, false, false);
         // gets the currency used in this order
         $currency = new Currency($supply_order->id_currency);
         // gets the warehouse where products will be received
         $warehouse = new Warehouse($supply_order->id_warehouse);
         // sets toolbar title with order reference
         if (!$supply_order->is_template) {
             $this->toolbar_title = sprintf($this->l('Details on supply order #%s'), $supply_order->reference);
         } else {
             $this->toolbar_title = sprintf($this->l('Details on supply order template #%s'), $supply_order->reference);
         }
         // re-defines fields_list
         $this->fields_list = array('refs' => array('title' => $this->l('Refs.'), 'align' => 'center', 'width' => 120, 'orderby' => false, 'filter' => false, 'search' => false), 'bare_codes' => array('title' => $this->l('Bare codes'), 'align' => 'center', 'width' => 100, 'orderby' => false, 'filter' => false, 'search' => false), 'name' => array('title' => $this->l('Name'), 'orderby' => false, 'filter' => false, 'search' => false), 'unit_price_te' => array('title' => $this->l('Unit price (tax excl.)'), 'align' => 'right', 'width' => 80, 'orderby' => false, 'filter' => false, 'search' => false, 'type' => 'price', 'currency' => true), 'quantity_expected' => array('title' => $this->l('Quantity'), 'align' => 'right', 'width' => 80, 'orderby' => false, 'filter' => false, 'search' => false), 'price_te' => array('title' => $this->l('Price (tax excl.)'), 'align' => 'right', 'width' => 80, 'orderby' => false, 'filter' => false, 'search' => false, 'type' => 'price', 'currency' => true), 'discount_rate' => array('title' => $this->l('Discount rate'), 'align' => 'right', 'width' => 80, 'orderby' => false, 'filter' => false, 'search' => false, 'suffix' => '%'), 'discount_value_te' => array('title' => $this->l('Discount value (tax excl.)'), 'align' => 'right', 'width' => 80, 'orderby' => false, 'filter' => false, 'search' => false, 'type' => 'price', 'currency' => true), 'price_with_discount_te' => array('title' => $this->l('Price with product discount (tax excl.)'), 'align' => 'right', 'width' => 80, 'orderby' => false, 'filter' => false, 'search' => false, 'type' => 'price', 'currency' => true), 'tax_rate' => array('title' => $this->l('Tax rate'), 'align' => 'right', 'width' => 80, 'orderby' => false, 'filter' => false, 'search' => false, 'suffix' => '%'), 'tax_value' => array('title' => $this->l('Tax value'), 'align' => 'right', 'width' => 80, 'orderby' => false, 'filter' => false, 'search' => false, 'type' => 'price', 'currency' => true), 'price_ti' => array('title' => $this->l('Price (tax incl.)'), 'align' => 'right', 'width' => 80, 'orderby' => false, 'filter' => false, 'search' => false, 'type' => 'price', 'currency' => true));
         //some staff before render list
         foreach ($this->_list as &$item) {
             $item['discount_rate'] = Tools::ps_round($item['discount_rate'], 4);
             $item['tax_rate'] = Tools::ps_round($item['tax_rate'], 4);
             $item['id_currency'] = $currency->id;
         }
         // unsets some buttons
         unset($this->toolbar_btn['export-csv-orders']);
         unset($this->toolbar_btn['export-csv-details']);
         unset($this->toolbar_btn['new']);
         // renders list
         $helper = new HelperList();
         $this->setHelperDisplay($helper);
         $helper->actions = array();
         $helper->show_toolbar = false;
         $helper->toolbar_btn = $this->toolbar_btn;
         $content = $helper->generateList($this->_list, $this->fields_list);
         //-ERP information
         //get information in erp_supply_order
         $erp_shipping_amount = 0;
         $erp_escompte = 0;
         $erp_date_to_invoice = null;
         $erp_global_discount_amount = 0;
         $erp_invoice_number = 0;
         $erp_supply_order_description = '';
         $d_erp_supply_order = ErpSupplyOrder::getErpSupplierOrderIdBySupplierOrderId((int) $id_supply_order);
         if ((int) $d_erp_supply_order > 0) {
             $erp_supply_order = new ErpSupplyOrder((int) $d_erp_supply_order);
             $erp_shipping_amount = $erp_supply_order->shipping_amount;
             $erp_escompte = $erp_supply_order->escompte;
             $erp_date_to_invoice = $erp_supply_order->date_to_invoice;
             $erp_global_discount_amount = $erp_supply_order->global_discount_amount;
             $erp_invoice_number = $erp_supply_order->invoice_number;
             $erp_supply_order_description = $erp_supply_order->description;
         }
         // calculated values
         $total_shipping = $supply_order->total_with_discount_te + $erp_shipping_amount;
         $escompte_amount = $total_shipping * $erp_escompte / 100;
         $total_escompte = $total_shipping - $escompte_amount;
         $total_to_pay = $total_escompte + $supply_order->total_tax;
         // remove id_lang attribute for version >= 1.5.5
         // id_lang parameter in displayDate() is deprecated
         if (version_compare(_PS_VERSION_, '1.5.5', '>=')) {
             $supply_order_creation_date = Tools::displayDate($supply_order->date_add, null, false);
             $supply_order_last_update = Tools::displayDate($supply_order->date_upd, null, false);
             $supply_order_expected = Tools::displayDate($supply_order->date_delivery_expected, null, false);
             //$supply_order_date_to_invoice = Tools::displayDate($erp_date_to_invoice, null , false);
         } else {
             $supply_order_creation_date = Tools::displayDate($supply_order->date_add, $lang_id, false);
             $supply_order_last_update = Tools::displayDate($supply_order->date_upd, $lang_id, false);
             $supply_order_expected = Tools::displayDate($supply_order->date_delivery_expected, $lang_id, false);
             //$supply_order_date_to_invoice = Tools::displayDate($erp_date_to_invoice, $lang_id , false);
         }
         // display these global order informations
         $this->tpl_view_vars = array('supply_order_detail_content' => $content, 'supply_order_warehouse' => Validate::isLoadedObject($warehouse) ? $warehouse->name : '', 'supply_order_reference' => $supply_order->reference, 'supply_order_supplier_name' => $supply_order->supplier_name, 'supply_order_creation_date' => $supply_order_creation_date, 'supply_order_last_update' => $supply_order_last_update, 'supply_order_expected' => $supply_order_expected, 'supply_order_discount_rate' => Tools::ps_round($supply_order->discount_rate, 2), 'supply_order_total_te' => Tools::displayPrice($supply_order->total_te, $currency), 'supply_order_discount_value_te' => Tools::displayPrice($supply_order->discount_value_te, $currency), 'supply_order_total_with_discount_te' => Tools::displayPrice($supply_order->total_with_discount_te, $currency), 'supply_order_total_tax' => Tools::displayPrice($supply_order->total_tax, $currency), 'supply_order_total_ti' => Tools::displayPrice($supply_order->total_ti, $currency), 'supply_order_currency' => $currency, 'is_template' => $supply_order->is_template, 'supply_order_discount_amount' => Tools::ps_round($erp_global_discount_amount, 2), 'supply_order_escompte' => Tools::ps_round($erp_escompte, 2), 'supply_order_shipping_amount' => Tools::ps_round($erp_shipping_amount, 2), 'supply_order_invoice_number' => $erp_invoice_number, 'supply_order_date_to_invoice' => $erp_date_to_invoice, 'total_shipping' => Tools::displayPrice($total_shipping, $currency), 'escompte_amount' => Tools::displayPrice($escompte_amount, $currency), 'total_escompte' => Tools::displayPrice($total_escompte, $currency), 'total_to_pay' => Tools::displayPrice($total_to_pay, $currency), 'supply_order_description' => $erp_supply_order_description, 'concerned_customer' => ErpSupplyOrderCustomer::getSupplyOrdersConcernedCustomer((int) $id_supply_order));
     }
     return parent::renderView();
 }
 /**
  * @see HTMLTemplate::getContent()
  */
 public function getContent()
 {
     $final_pdf = '';
     require_once _PS_MODULE_DIR_ . 'erpillicopresta/models/ErpSupplyOrderCustomer.php';
     require_once _PS_MODULE_DIR_ . 'erpillicopresta/models/ErpSupplier.php';
     require_once _PS_MODULE_DIR_ . 'erpillicopresta/models/ErpSupplyOrder.php';
     // Retrieval of the link between supplier order AND customer order if exists
     $erp_supply_order_customer = ErpSupplyOrderCustomer::getSupplyOrdersCustomer((int) $this->supply_order->id);
     $supply_order_details = $this->supply_order->getEntriesCollection((int) $this->supply_order->id_lang);
     $this->roundSupplyOrderDetails($supply_order_details);
     $supply_order_receipt_history = array();
     foreach ($supply_order_details as $supply_order_detail) {
         $supply_order_receipt_history[$supply_order_detail->id] = $this->getSupplyOrderReceiptHistoryCollection($supply_order_detail->id);
     }
     $this->roundSupplyOrder($this->supply_order);
     $tax_order_summary = $this->getTaxOrderSummary();
     $currency = new Currency((int) $this->supply_order->id_currency);
     //-ERP information
     // get additional supplier information
     $erp_supplier = null;
     $erp_supplier_fax = '';
     if (isset($this->supply_order->id_supplier) && (int) $this->supply_order->id_supplier > 0) {
         $id_erpip_supplier = ErpSupplier::getErpSupplierIdBySupplierId((int) $this->supply_order->id_supplier);
         if ((int) $id_erpip_supplier > 0) {
             $erp_supplier = new ErpSupplier((int) $id_erpip_supplier);
         }
     }
     if ($erp_supplier != null) {
         $erp_supplier_fax = $erp_supplier->fax;
     }
     //-ERP information
     // get additional supply order information
     $erp_supply_order = null;
     if (isset($this->supply_order->id) && (int) $this->supply_order->id > 0) {
         $id_erpip_supply_order = ErpSupplyOrder::getErpSupplierOrderIdBySupplierOrderId((int) $this->supply_order->id);
         if ((int) $id_erpip_supply_order > 0) {
             $erp_supply_order = new ErpSupplyOrder((int) $id_erpip_supply_order);
         }
     }
     // get shipping address
     $adresse_livraison = self::getStoreByName('Adresse livraison');
     $this->smarty->assign(array('warehouse' => $this->warehouse, 'address_warehouse' => $this->address_warehouse, 'address_supplier' => $this->address_supplier, 'supply_order' => $this->supply_order, 'erp_supply_order' => $erp_supply_order, 'supply_order_details' => $supply_order_details, 'supply_order_receipt_history' => $supply_order_receipt_history, 'tax_order_summary' => $tax_order_summary, 'currency' => $currency, 'fax' => $erp_supplier_fax, 'action' => Tools::getValue('submitAction'), 'shop_name' => Configuration::get('PS_SHOP_NAME'), 'shop_addr' => Configuration::get('PS_SHOP_ADDR1'), 'shop_addr2' => Configuration::get('PS_SHOP_ADDR2'), 'shop_code' => Configuration::get('PS_SHOP_CODE'), 'shop_city' => Configuration::get('PS_SHOP_CITY'), 'shop_country' => Configuration::get('PS_SHOP_COUNTRY'), 'adresse_livraison' => $adresse_livraison));
     // if there is an supply order generated : display a PDF page by customer
     if (!empty($erp_supply_order_customer)) {
         // distribution by customer : one page per customer
         $final_item = array();
         foreach ($erp_supply_order_customer as $item) {
             $final_item[$item['id_customer']][] = $item;
         }
         $pdf_customer = '';
         // Create page per customer
         foreach ($final_item as $id_customer => $datas) {
             $customer = new Customer((int) $id_customer);
             $order_detail = array();
             foreach ($datas as $data) {
                 $order_detail_line = new OrderDetailCore((int) $data['id_order_detail']);
                 $order_detail_line->tax_rate = Tax::getProductTaxRate($order_detail_line->product_id);
                 $order_detail[] = $order_detail_line;
             }
             $this->smarty->assign(array('is_customer_page' => 'true'));
             $this->smarty->assign(array('supply_order_details' => $order_detail));
             $this->smarty->assign(array('customer_name' => $customer->lastname . ' ' . $customer->firstname));
             $pdf_customer .= '<div style="page-break-before:always"><div>';
             $pdf_customer .= $this->smarty->fetch(_PS_MODULE_DIR_ . 'erpillicopresta/views/templates/admin/pdf/erp-supply-order-customer.tpl');
         }
         $this->smarty->assign(array('is_customer_page' => 'false'));
         $this->smarty->assign(array('supply_order_details' => $supply_order_details));
         $final_pdf = $this->smarty->fetch(_PS_MODULE_DIR_ . 'erpillicopresta/views/templates/admin/pdf/erp-supply-order.tpl') . $pdf_customer;
     } else {
         $final_pdf = $this->smarty->fetch(_PS_MODULE_DIR_ . 'erpillicopresta/views/templates/admin/pdf/erp-supply-order.tpl');
     }
     return $final_pdf;
 }
 public function initOrderingContent()
 {
     // list order id by created providers
     $supply_order_created = array();
     $this->show_toolbar = true;
     $this->display = 'ordering';
     if ($this->is_1_6) {
         $this->initPageHeaderToolbar();
     }
     $this->initToolbar();
     $datas = $this->getDataGeneration();
     if (!empty($datas['data_return'])) {
         //get default currencie
         $id_default_currency = Configuration::get('PS_CURRENCY_DEFAULT');
         // get default id lang
         $id_default_lang = Configuration::get('PS_LANG_DEFAULT');
         foreach ($datas['data_return'] as $id_supplier => $products_info) {
             // Get provider datas
             $supplier = new Supplier((int) $id_supplier);
             // get warehouse datas, delivery date and tax for the provider order
             $id_warehouse_data = Tools::getValue('id_warehouse');
             $date_delivery_expected_data = Tools::getValue('date_delivery_expected');
             $tax_rate_data = Tools::getValue('tax_rate');
             $tax_rate_data = $tax_rate_data[$id_supplier];
             // id warehouse
             $id_warehouse = $id_warehouse_data[$id_supplier];
             // delivery date
             $date_delivery_expected = $date_delivery_expected_data[$id_supplier];
             // create the provider order
             $supply_order = new SupplyOrder();
             $supply_order->reference = ErpSupplyOrderClasses::getNextSupplyOrderReference();
             $supply_order->id_supplier = $id_supplier;
             $supply_order->supplier_name = $supplier->name;
             $supply_order->id_warehouse = $id_warehouse;
             $supply_order->id_currency = $id_default_currency;
             $supply_order->id_lang = $id_default_lang;
             $supply_order->id_supply_order_state = 1;
             $supply_order->id_ref_currency = (int) Currency::getDefaultCurrency()->id;
             $supply_order->date_delivery_expected = $date_delivery_expected;
             // if recording is ok, create the order lines
             if ($supply_order->add()) {
                 // get the provider id order
                 $id_supply_order = $this->getLastIdSupplyOrder();
                 $supply_order_created[] = $id_supply_order;
                 // Ajout de son historique
                 // add historical
                 $history = new SupplyOrderHistory();
                 $history->id_supply_order = $id_supply_order;
                 $history->id_state = 3;
                 $history->id_employee = (int) $this->context->employee->id;
                 $history->employee_firstname = pSQL($this->context->employee->firstname);
                 $history->employee_lastname = pSQL($this->context->employee->lastname);
                 $history->save();
                 // Create entries for provider order
                 if (!empty($products_info)) {
                     $i = 0;
                     foreach ($products_info as $item) {
                         if (!isset($item['product_id'])) {
                             continue;
                         }
                         $supply_order_detail = new SupplyOrderDetail();
                         $supply_order_detail->id_supply_order = $id_supply_order;
                         $supply_order_detail->id_currency = (int) Currency::getDefaultCurrency()->id;
                         $supply_order_detail->id_product = $item['product_id'];
                         $supply_order_detail->id_product_attribute = $item['product_attribute_id'];
                         $supply_order_detail->reference = $item['product_reference'];
                         $supply_order_detail->supplier_reference = $item['product_supplier_reference'];
                         $supply_order_detail->name = $item['product_name'];
                         $supply_order_detail->ean13 = $item['product_ean13'];
                         $supply_order_detail->upc = $item['product_upc'];
                         $supply_order_detail->quantity_expected = $item['total_product_quantity'];
                         $supply_order_detail->exchange_rate = 1;
                         $supply_order_detail->unit_price_te = $item['unit_price_tax_excl'];
                         $supply_order_detail->tax_rate = $tax_rate_data[$i];
                         $supply_order_detail->save();
                         // Get the supply order created
                         $id_supply_order_detail = $this->getLastIdSupplyOrderDetail();
                         // Record the relation between provider order and customer order
                         if (!empty($item)) {
                             foreach ($item['concerned_id_order_detail'] as $customer_link) {
                                 $supply_order_customer = new ErpSupplyOrderCustomer();
                                 $supply_order_customer->id_customer = $customer_link['id_customer'];
                                 $supply_order_customer->id_order_detail = $customer_link['id_order_detail'];
                                 $supply_order_customer->id_supply_order_detail = $id_supply_order_detail;
                                 $supply_order_customer->id_supply_order = $id_supply_order;
                                 $supply_order_customer->save();
                             }
                         }
                         $i++;
                     }
                     // Rerecording provider order data to update totals
                     $supply_order->save();
                 }
             }
         }
         // update provider order status
         if (!empty($datas['order_to_change_state'])) {
             foreach ($datas['order_to_change_state'] as $id_order) {
                 $order_change_state = new Order((int) $id_order);
                 $order_change_state->setCurrentState($this->generate_order_state_to, (int) $this->context->employee->id);
             }
         }
         $this->confirmations[] = $this->l('Order saved successfully !');
         // remove treated order in cookies
         $this->context->cookie->__unset('unselected_orders');
     } else {
         $this->errors[] = Tools::displayError($this->l('No data available for ordering ! You must select at least one order.'));
     }
     $this->context->smarty->assign(array('content' => '', 'show_toolbar ' => 'true', 'show_toolbar' => true, 'toolbar_btn' => $this->toolbar_btn, 'title' => $this->l('Supply Order : order screen'), 'toolbar_scroll' => $this->toolbar_scroll, 'token' => $this->token, 'url_post' => self::$currentIndex . '&token=' . $this->token, 'supply_order_created' => !empty($supply_order_created) ? implode(',', $supply_order_created) : ''));
     $this->createTemplate('ordering.tpl');
     $this->template = 'ordering.tpl';
 }