/**
  * AdminController::renderForm() override
  * @see AdminController::renderForm()
  */
 public function renderForm()
 {
     // loads current warehouse
     if (!($obj = $this->loadObject(true))) {
         return;
     }
     if (Tools::isSubmit('addsupply_order') || Tools::isSubmit('updatesupply_order') || Tools::isSubmit('submitAddsupply_order') || Tools::isSubmit('submitUpdatesupply_order')) {
         if (Tools::isSubmit('addsupply_order') || Tools::isSubmit('submitAddsupply_order')) {
             $this->toolbar_title = $this->l('Create a new supply order');
         }
         if (Tools::isSubmit('updatesupply_order') || Tools::isSubmit('submitUpdatesupply_order')) {
             $this->toolbar_title = $this->l('Manage supply orders');
         }
         if (Tools::isSubmit('mod') && Tools::getValue('mod') === 'template' || $this->object->is_template) {
             $this->toolbar_title .= ' (' . $this->l('template') . ')';
         }
         //get warehouses list
         $warehouses = Warehouse::getWarehouses(true);
         // displays warning if there are no warehouses
         if (!$warehouses) {
             $this->displayWarning($this->l('You must have at least one warehouse. See Stock/Warehouses'));
         }
         //get currencies list
         $currencies = Currency::getCurrencies();
         $id_default_currency = Configuration::get('PS_CURRENCY_DEFAULT');
         $default_currency = Currency::getCurrency($id_default_currency);
         if ($default_currency) {
             $currencies = array_merge(array($default_currency, '-'), $currencies);
         }
         //get suppliers list
         $suppliers = Supplier::getSuppliers();
         //get languages list
         $languages = Language::getLanguages(true);
         $id_default_lang = Configuration::get('PS_LANG_DEFAULT');
         $default_lang = Language::getLanguage($id_default_lang);
         if ($default_lang) {
             $languages = array_merge(array($default_lang, '-'), $languages);
         }
         $this->fields_form = array('legend' => array('title' => $this->l('Order information'), $this->is_1_6 ? 'icon' : 'image' => $this->is_1_6 ? 'icon-pencil' : '../img/admin/edit.gif'), 'input' => array(array('type' => 'hidden', 'name' => 'id_erpip_supply_order'), array('type' => 'text', 'label' => $this->l('Reference:'), 'name' => 'reference', 'size' => 50, 'required' => true, 'readonly' => true, 'desc' => $this->l('The reference number of your order.')), array('type' => 'textarea', 'label' => $this->l('Description:'), 'name' => 'description', 'cols' => 60, 'rows' => 10, 'lang' => false, 'hint' => $this->l('Invalid characters :') . ' <>;=#{}', 'desc' => $this->l('Will appear in the suppliers list'), 'autoload_rte' => 'rte'), array('type' => 'select', 'label' => $this->l('Supplier:'), 'name' => 'id_supplier', 'required' => true, 'options' => array('query' => $suppliers, 'id' => 'id_supplier', 'name' => 'name'), 'desc' => $this->l('Select the supplier you\'ll be purchasing from.'), 'hint' => $this->l('Warning: All products already added to the order will be removed.')), array('type' => 'select', 'label' => $this->l('Warehouse:'), 'name' => 'id_warehouse', 'required' => true, 'options' => array('query' => $warehouses, 'id' => 'id_warehouse', 'name' => 'name'), 'desc' => $this->l('Which warehouse will the order be sent to?')), array('type' => 'select', 'label' => $this->l('Currency:'), 'name' => 'id_currency', 'required' => true, 'options' => array('query' => $currencies, 'id' => 'id_currency', 'name' => 'name'), 'desc' => $this->l('The currency of the order.'), 'hint' => $this->l('Warning: All products already added to the order will be removed.')), array('type' => 'select', 'label' => $this->l('Order Language:'), 'name' => 'id_lang', 'required' => true, 'options' => array('query' => $languages, 'id' => 'id_lang', 'name' => 'name'), 'desc' => $this->l('The language of the order.')), array('type' => 'radio', 'label' => $this->l('Global discount type:'), 'name' => 'global_discount_type', 'required' => true, 'br' => true, 'class' => 't', 'default_value' => 'amount', 'values' => array(array('id' => 'global_discount_type' . _PS_SMARTY_NO_COMPILE_, 'value' => 'rate', 'label' => $this->l('Ratio')), array('id' => 'global_discount_type' . _PS_SMARTY_CHECK_COMPILE_, 'value' => 'amount', 'label' => $this->l('Amount'))), 'desc' => $this->l('Please select the discount type which will be applied to this order : ratio or amount')), array('type' => 'text', 'label' => $this->l('Global discount rate (%):'), 'name' => 'discount_rate', 'size' => 10, 'required' => true, 'desc' => $this->l('This is the global discount rate in percent for the order.')), array('type' => 'text', 'label' => $this->l('Discount total amount:'), 'name' => 'global_discount_amount', 'size' => 10, 'required' => false, 'desc' => $this->l('Please indicate the discount total amount')), array('type' => 'text', 'label' => $this->l('Discount (%)'), 'name' => 'escompte', 'size' => 10, 'required' => false, 'desc' => $this->l('Percentage discount impacting the order total.')), array('type' => 'text', 'label' => $this->l('Shipping amount'), 'name' => 'shipping_amount', 'size' => 10, 'required' => false, 'desc' => $this->l('Shipping amount that will be added to the total order (tax excl.).')), array('type' => 'text', 'label' => $this->l('Automatically load products:'), 'name' => 'load_products', 'size' => 10, 'required' => false, 'hint' => $this->l('This will reset the order'), 'desc' => $this->l('If specified, each product quantity inferior or equal to this value will be loaded.'))), 'submit' => array('title' => $this->l('Save order')));
         if (Tools::isSubmit('mod') && Tools::getValue('mod') === 'template' || $this->object->is_template) {
             $this->fields_form['input'][] = array('type' => 'hidden', 'name' => 'is_template');
             $this->fields_form['input'][] = array('type' => 'hidden', 'name' => 'date_delivery_expected');
         } else {
             $this->fields_form['input'][] = array('type' => 'date', 'label' => $this->l('Expected delivery date:'), 'name' => 'date_delivery_expected', 'size' => 10, 'required' => true, 'desc' => $this->l('The expected delivery date for this order is...'));
         }
         //specific discount display
         if (isset($this->object->discount_rate)) {
             $this->object->discount_rate = Tools::ps_round($this->object->discount_rate, 4);
         }
         //specific date display
         if (isset($this->object->date_delivery_expected)) {
             $date = explode(' ', $this->object->date_delivery_expected);
             if ($date) {
                 $this->object->date_delivery_expected = $date[0];
             }
         }
         $this->displayInformation($this->l('If you wish to order products, they have to be available for the specified supplier/warehouse.') . ' ' . $this->l('See Catalog/Products/Your Product/Suppliers & Warehouses') . '<br />' . $this->l('Changing the currency or the supplier will reset the order.') . '<br />' . '<br />' . $this->l('Please note that you can only order from one supplier at a time.'));
         //-- ERP informations
         // add additional information
         $erp_filed_value = array();
         $erp_filed_value['id_erpip_supply_order'] = 0;
         $franco_amount = 0;
         //if we are in add form
         if (Tools::isSubmit('addsupply_order') || Tools::isSubmit('submitAddsupply_order')) {
             $erp_filed_value['reference'] = ErpSupplyOrderClasses::getNextSupplyOrderReference();
             //get value of the first supplier : the default selected
             if (!empty($suppliers)) {
                 // get additional information for the first supplier
                 $id_erpip_supplier = ErpSupplier::getErpSupplierIdBySupplierId((int) $suppliers[0]['id_supplier']);
                 $erp_supplier = null;
                 if ($id_erpip_supplier > 0) {
                     $erp_supplier = new ErpSupplier((int) $id_erpip_supplier);
                 }
                 if ($erp_supplier != null) {
                     $franco_amount = $erp_supplier->franco_amount;
                     $erp_filed_value['escompte'] = $erp_supplier->escompte;
                     $erp_filed_value['shipping_amount'] = $erp_supplier->shipping_amount;
                     $erp_filed_value['global_discount_amount'] = $erp_supplier->discount_amount;
                     if ((int) $erp_supplier->delivery_time > 0) {
                         $erp_filed_value['date_delivery_expected'] = date('Y-m-d', strtotime('+ ' . (int) $erp_supplier->delivery_time . ' days'));
                     }
                     // the additional information not existe then the id_erpip_supply_order is 0
                     $erp_filed_value['id_erpip_supply_order'] = 0;
                 }
             }
         } else {
             // set franco amount value
             if (isset($this->object->id_supplier)) {
                 $supplier = new ErpSupplier($this->object->id_supplier);
                 $franco_amount = $supplier->franco_amount;
             }
             // loads current erp_supplier_order information for this supplier order - if possible
             $erp_supplier_order = null;
             if (isset($obj->id)) {
                 $id_erpip_supply_order = ErpSupplyOrder::getErpSupplierOrderIdBySupplierOrderId($obj->id);
                 if ($id_erpip_supply_order > 0) {
                     $erp_supplier_order = new ErpSupplyOrder((int) $id_erpip_supply_order);
                 }
             }
             // force specific fields values (erp_supplier_order)
             if ($erp_supplier_order != null) {
                 $erp_filed_value['id_erpip_supply_order'] = $erp_supplier_order->id_erpip_supply_order;
                 $erp_filed_value['escompte'] = $erp_supplier_order->escompte;
                 $erp_filed_value['shipping_amount'] = $erp_supplier_order->shipping_amount;
                 $erp_filed_value['global_discount_amount'] = $erp_supplier_order->global_discount_amount;
                 $erp_filed_value['description'] = $erp_supplier_order->description;
             }
         }
         //Set form value
         $this->fields_value = $erp_filed_value;
         $this->context->smarty->assign(array('franco_amount' => $franco_amount, 'shipping_amount' => $erp_filed_value['shipping_amount'], 'supply_order_total_te' => number_format($this->object->total_te, 2, ',', ' '), 'amount_to_franco_with_produc_discount' => number_format((double) $franco_amount - $this->object->total_te, 2, ',', ' '), 'template_path' => $this->template_path, 'controller_status' => $this->controller_status, 'stock_management_active' => $this->advanced_stock_management));
         $this->getFilters();
         if (Tools::isSubmit('export_csv')) {
             $this->renderCSV();
         }
         return parent::renderForm();
     }
 }
 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';
 }