/**
  * AdminController::postProcess() override
  * @see AdminController::postProcess()
  */
 public function postProcess()
 {
     require_once _PS_MODULE_DIR_ . 'erpillicopresta/models/ErpFeature.php';
     $this->context->smarty->assign(array('erp_feature' => ErpFeature::getFeaturesWithToken($this->context->language->iso_code), 'template_path' => $this->template_path));
     if (Tools::isSubmit('export_csv')) {
         $this->renderCSV();
     }
     // checks access
     if (Tools::isSubmit('submitAdd' . $this->table) && !($this->tabAccess['add'] === '1')) {
         $this->errors[] = Tools::displayError($this->l('You do not have permission to add suppliers.'));
         return parent::postProcess();
     }
     if (Tools::isSubmit('submitAdd' . $this->table)) {
         if (Tools::isSubmit('id_supplier') && !($obj = $this->loadObject(true))) {
             return;
         }
         // updates/creates address if it does not exist
         if (Tools::isSubmit('id_address') && (int) Tools::getValue('id_address') > 0) {
             $address = new Address((int) Tools::getValue('id_address'));
         } else {
             $address = new Address();
         }
         // creates address
         $address->alias = Tools::getValue('name', null);
         $address->lastname = 'supplier';
         // skip problem with numeric characters in supplier name
         $address->firstname = 'supplier';
         // skip problem with numeric characters in supplier name
         $address->address1 = Tools::getValue('address', null);
         $address->address2 = Tools::getValue('address2', null);
         $address->postcode = Tools::getValue('postcode', null);
         $address->phone = Tools::getValue('phone', null);
         $address->id_country = Tools::getValue('id_country', null);
         $address->id_state = Tools::getValue('id_state', null);
         $address->city = Tools::getValue('city', null);
         $validation = $address->validateController();
         // checks address validity
         if (count($validation) > 0) {
             foreach ($validation as $item) {
                 $this->errors[] = $item;
             }
             $this->errors[] = Tools::displayError($this->l('The address is not correct. Please make sure all of the required fields are completed.'));
         } else {
             if (Tools::isSubmit('id_address') && Tools::getValue('id_address') > 0) {
                 $address->update();
             } else {
                 $address->save();
                 $_POST['id_address'] = $address->id;
             }
         }
         //--ERP informations
         // updates/creates erp_supplier if it does not exist
         if (Tools::isSubmit('id_erpip_supplier') && (int) Tools::getValue('id_erpip_supplier') > 0) {
             $erp_supplier = new ErpSupplier((int) Tools::getValue('id_erpip_supplier'));
         } else {
             $erp_supplier = new ErpSupplier();
         }
         // creates erp_supplier
         $erp_supplier->email = Tools::getValue('email', null);
         $erp_supplier->fax = Tools::getValue('fax', null);
         $erp_supplier->franco_amount = Tools::getValue('franco_amount', null);
         $erp_supplier->discount_amount = Tools::getValue('discount_amount', null);
         $erp_supplier->shipping_amount = Tools::getValue('shipping_amount', null);
         $erp_supplier->escompte = Tools::getValue('escompte', null);
         $erp_supplier->delivery_time = Tools::getValue('delivery_time', null);
         $erp_supplier->account_number_accounting = Tools::getValue('account_number_accounting', null);
         $validation2 = $erp_supplier->validateController();
         //print_r($validation2);
         // checks erp_supplier validity
         if (count($validation2) > 0) {
             foreach ($validation2 as $item) {
                 $this->errors[] = $item;
             }
             $this->errors[] = Tools::displayError($this->l('The ErpIllicopresta Supplier is not correct. Please make sure all of the required fields are completed.'));
         } else {
             if (Tools::isSubmit('id_erpip_supplier') && Tools::getValue('id_erpip_supplier') > 0) {
                 $erp_supplier->update();
             } else {
                 $erp_supplier->save();
                 $_POST['id_erpip_supplier'] = $erp_supplier->id;
             }
         }
         return parent::postProcess();
     } else {
         if (Tools::isSubmit('delete' . $this->table)) {
             if (!($obj = $this->loadObject(true))) {
                 return;
             } else {
                 if (SupplyOrder::supplierHasPendingOrders($obj->id)) {
                     $this->errors[] = $this->l('It is not possible to delete a supplier if there are pending supplier orders.');
                 } else {
                     //delete all product_supplier linked to this supplier
                     Db::getInstance()->execute('DELETE FROM `' . _DB_PREFIX_ . 'product_supplier` WHERE `id_supplier`=' . (int) $obj->id);
                     $id_address = Address::getAddressIdBySupplierId($obj->id);
                     $address = new Address($id_address);
                     if (Validate::isLoadedObject($address)) {
                         $address->deleted = 1;
                         $address->save();
                     }
                     //delete erp supplier
                     $id_erpip_supplier = ErpSupplier::getErpSupplierIdBySupplierId($obj->id);
                     $erp_supplier = new ErpSupplier($id_erpip_supplier);
                     if (Validate::isLoadedObject($erp_supplier)) {
                         $erp_supplier->delete();
                     }
                     return parent::postProcess();
                 }
             }
         } else {
             return parent::postProcess();
         }
     }
 }