Ejemplo n.º 1
0
    protected function supplyOrdersDetailsImportOne($info, &$products, &$reset, $force_ids, $current_line, $validateOnly = false)
    {
        // sets default values if needed
        AdminImportController::setDefaultValues($info);
        // gets the supply order
        if (array_key_exists('supply_order_reference', $info) && pSQL($info['supply_order_reference']) && SupplyOrder::exists(pSQL($info['supply_order_reference']))) {
            $supply_order = SupplyOrder::getSupplyOrderByReference(pSQL($info['supply_order_reference']));
        } else {
            $this->errors[] = sprintf($this->l('Supply Order (%s) could not be loaded (at line %d).'), $info['supply_order_reference'], $current_line + 1);
        }
        if (empty($this->errors)) {
            // sets parameters
            $id_product = (int) $info['id_product'];
            if (!$info['id_product_attribute']) {
                $info['id_product_attribute'] = 0;
            }
            $id_product_attribute = (int) $info['id_product_attribute'];
            $unit_price_te = (double) $info['unit_price_te'];
            $quantity_expected = (int) $info['quantity_expected'];
            $discount_rate = (double) $info['discount_rate'];
            $tax_rate = (double) $info['tax_rate'];
            // checks if one product/attribute is there only once
            if (isset($products[$id_product][$id_product_attribute])) {
                $this->errors[] = sprintf($this->l('Product/Attribute (%d/%d) cannot be added twice (at line %d).'), $id_product, $id_product_attribute, $current_line + 1);
            } else {
                $products[$id_product][$id_product_attribute] = $quantity_expected;
            }
            // checks parameters
            if (false === ($supplier_reference = ProductSupplier::getProductSupplierReference($id_product, $id_product_attribute, $supply_order->id_supplier))) {
                $this->errors[] = sprintf($this->l('Product (%d/%d) is not available for this order (at line %d).'), $id_product, $id_product_attribute, $current_line + 1);
            }
            if ($unit_price_te < 0) {
                $this->errors[] = sprintf($this->l('Unit Price (tax excl.) (%d) is not valid (at line %d).'), $unit_price_te, $current_line + 1);
            }
            if ($quantity_expected < 0) {
                $this->errors[] = sprintf($this->l('Quantity Expected (%d) is not valid (at line %d).'), $quantity_expected, $current_line + 1);
            }
            if ($discount_rate < 0 || $discount_rate > 100) {
                $this->errors[] = sprintf($this->l('Discount rate (%d) is not valid (at line %d). %s.'), $discount_rate, $current_line + 1, $this->l('Format: Between 0 and 100'));
            }
            if ($tax_rate < 0 || $tax_rate > 100) {
                $this->errors[] = sprintf($this->l('Quantity Expected (%d) is not valid (at line %d).'), $tax_rate, $current_line + 1, $this->l('Format: Between 0 and 100'));
            }
            // if no errors, sets supply order details
            if (empty($this->errors)) {
                // resets order if needed
                if (!$validateOnly && $reset) {
                    $supply_order->resetProducts();
                    $reset = false;
                }
                // creates new product
                $supply_order_detail = new SupplyOrderDetail();
                AdminImportController::arrayWalk($info, array('AdminImportController', 'fillInfo'), $supply_order_detail);
                // sets parameters
                $supply_order_detail->id_supply_order = $supply_order->id;
                $currency = new Currency($supply_order->id_ref_currency);
                $supply_order_detail->id_currency = $currency->id;
                $supply_order_detail->exchange_rate = $currency->conversion_rate;
                $supply_order_detail->supplier_reference = $supplier_reference;
                $supply_order_detail->name = Product::getProductName($id_product, $id_product_attribute, $supply_order->id_lang);
                // gets ean13 / ref / upc
                $query = new DbQuery();
                $query->select('
					IFNULL(pa.reference, IFNULL(p.reference, \'\')) as reference,
					IFNULL(pa.ean13, IFNULL(p.ean13, \'\')) as ean13,
					IFNULL(pa.upc, IFNULL(p.upc, \'\')) as upc
				');
                $query->from('product', 'p');
                $query->leftJoin('product_attribute', 'pa', 'pa.id_product = p.id_product AND id_product_attribute = ' . (int) $id_product_attribute);
                $query->where('p.id_product = ' . (int) $id_product);
                $query->where('p.is_virtual = 0 AND p.cache_is_pack = 0');
                $res = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
                $product_infos = $res['0'];
                $supply_order_detail->reference = $product_infos['reference'];
                $supply_order_detail->ean13 = $product_infos['ean13'];
                $supply_order_detail->upc = $product_infos['upc'];
                $supply_order_detail->force_id = (bool) $force_ids;
                if (!$validateOnly) {
                    $supply_order_detail->add();
                    $supply_order->update();
                }
                unset($supply_order_detail);
            }
        }
    }
 /**
  * @see AdminController::processDelete();
  */
 public function processDelete()
 {
     if (Tools::isSubmit('delete' . $this->table)) {
         /** @var Warehouse $obj */
         // check if the warehouse exists and can be deleted
         if (!($obj = $this->loadObject(true))) {
             return;
         } elseif ($obj->getQuantitiesOfProducts() > 0) {
             // not possible : products
             $this->errors[] = $this->l('It is not possible to delete a warehouse when there are products in it.');
         } elseif (SupplyOrder::warehouseHasPendingOrders($obj->id)) {
             // not possible : supply orders
             $this->errors[] = $this->l('It is not possible to delete a Warehouse if it has pending supply orders.');
         } else {
             // else, it can be deleted
             // sets the address of the warehouse as deleted
             $address = new Address($obj->id_address);
             $address->deleted = 1;
             $address->save();
             // removes associations with carriers/shops/products location
             $obj->setCarriers(array());
             $obj->resetProductsLocations();
             return parent::processDelete();
         }
     }
 }
 /**
  * AdminController::postProcess() override
  * @see AdminController::postProcess()
  */
 public function postProcess()
 {
     // checks access
     if (Tools::isSubmit('submitAdd' . $this->table) && !($this->tabAccess['add'] === '1')) {
         $this->errors[] = Tools::displayError('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->phone_mobile = Tools::getValue('phone_mobile', 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('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;
             }
         }
         return parent::postProcess();
     } elseif (Tools::isSubmit('delete' . $this->table)) {
         if (!($obj = $this->loadObject(true))) {
             return;
         } elseif (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();
             }
             return parent::postProcess();
         }
     } else {
         return parent::postProcess();
     }
 }
 /**
  * AdminController::postProcess() override
  * @see AdminController::postProcess()
  */
 public function postProcess()
 {
     $this->is_editing_order = false;
     // Checks access
     if (Tools::isSubmit('submitAddsupply_order') && !($this->tabAccess['add'] === '1')) {
         $this->errors[] = Tools::displayError('You do not have permission to add a supply order.');
     }
     if (Tools::isSubmit('submitBulkUpdatesupply_order_detail') && !($this->tabAccess['edit'] === '1')) {
         $this->errors[] = Tools::displayError('You do not have permission to edit an order.');
     }
     // Trick to use both Supply Order as template and actual orders
     if (Tools::isSubmit('is_template')) {
         $_GET['mod'] = 'template';
     }
     // checks if supply order reference is unique
     if (Tools::isSubmit('reference')) {
         // gets the reference
         $ref = pSQL(Tools::getValue('reference'));
         if (Tools::getValue('id_supply_order') != 0 && SupplyOrder::getReferenceById((int) Tools::getValue('id_supply_order')) != $ref) {
             if ((int) SupplyOrder::exists($ref) != 0) {
                 $this->errors[] = Tools::displayError('The reference has to be unique.');
             }
         } elseif (Tools::getValue('id_supply_order') == 0 && (int) SupplyOrder::exists($ref) != 0) {
             $this->errors[] = Tools::displayError('The reference has to be unique.');
         }
     }
     if ($this->errors) {
         return;
     }
     // Global checks when add / update a supply order
     if (Tools::isSubmit('submitAddsupply_order') || Tools::isSubmit('submitAddsupply_orderAndStay')) {
         $this->action = 'save';
         $this->is_editing_order = true;
         // get supplier ID
         $id_supplier = (int) Tools::getValue('id_supplier', 0);
         if ($id_supplier <= 0 || !Supplier::supplierExists($id_supplier)) {
             $this->errors[] = Tools::displayError('The selected supplier is not valid.');
         }
         // get warehouse id
         $id_warehouse = (int) Tools::getValue('id_warehouse', 0);
         if ($id_warehouse <= 0 || !Warehouse::exists($id_warehouse)) {
             $this->errors[] = Tools::displayError('The selected warehouse is not valid.');
         }
         // get currency id
         $id_currency = (int) Tools::getValue('id_currency', 0);
         if ($id_currency <= 0 || (!($result = Currency::getCurrency($id_currency)) || empty($result))) {
             $this->errors[] = Tools::displayError('The selected currency is not valid.');
         }
         // get delivery date
         if (Tools::getValue('mod') != 'template' && strtotime(Tools::getValue('date_delivery_expected')) <= strtotime('-1 day')) {
             $this->errors[] = Tools::displayError('The specified date cannot be in the past.');
         }
         // gets threshold
         $quantity_threshold = Tools::getValue('load_products');
         if (is_numeric($quantity_threshold)) {
             $quantity_threshold = (int) $quantity_threshold;
         } else {
             $quantity_threshold = null;
         }
         if (!count($this->errors)) {
             // forces date for templates
             if (Tools::isSubmit('is_template') && !Tools::getValue('date_delivery_expected')) {
                 $_POST['date_delivery_expected'] = date('Y-m-d h:i:s');
             }
             // specify initial state
             $_POST['id_supply_order_state'] = 1;
             //defaut creation state
             // specify global reference currency
             $_POST['id_ref_currency'] = Currency::getDefaultCurrency()->id;
             // specify supplier name
             $_POST['supplier_name'] = Supplier::getNameById($id_supplier);
             //specific discount check
             $_POST['discount_rate'] = (double) str_replace(array(' ', ','), array('', '.'), Tools::getValue('discount_rate', 0));
         }
         // manage each associated product
         $this->manageOrderProducts();
         // if the threshold is defined and we are saving the order
         if (Tools::isSubmit('submitAddsupply_order') && Validate::isInt($quantity_threshold)) {
             $this->loadProducts((int) $quantity_threshold);
         }
     }
     // Manage state change
     if (Tools::isSubmit('submitChangestate') && Tools::isSubmit('id_supply_order') && Tools::isSubmit('id_supply_order_state')) {
         if ($this->tabAccess['edit'] != '1') {
             $this->errors[] = Tools::displayError('You do not have permission to change the order status.');
         }
         // get state ID
         $id_state = (int) Tools::getValue('id_supply_order_state', 0);
         if ($id_state <= 0) {
             $this->errors[] = Tools::displayError('The selected supply order status is not valid.');
         }
         // get supply order ID
         $id_supply_order = (int) Tools::getValue('id_supply_order', 0);
         if ($id_supply_order <= 0) {
             $this->errors[] = Tools::displayError('The supply order ID is not valid.');
         }
         if (!count($this->errors)) {
             // try to load supply order
             $supply_order = new SupplyOrder($id_supply_order);
             if (Validate::isLoadedObject($supply_order)) {
                 // get valid available possible states for this order
                 $states = SupplyOrderState::getSupplyOrderStates($supply_order->id_supply_order_state);
                 foreach ($states as $state) {
                     // if state is valid, change it in the order
                     if ($id_state == $state['id_supply_order_state']) {
                         $new_state = new SupplyOrderState($id_state);
                         $old_state = new SupplyOrderState($supply_order->id_supply_order_state);
                         // special case of validate state - check if there are products in the order and the required state is not an enclosed state
                         if ($supply_order->isEditable() && !$supply_order->hasEntries() && !$new_state->enclosed) {
                             $this->errors[] = Tools::displayError('It is not possible to change the status of this order because you did not order any products.');
                         }
                         if (!count($this->errors)) {
                             $supply_order->id_supply_order_state = $state['id_supply_order_state'];
                             if ($supply_order->save()) {
                                 if ($new_state->pending_receipt) {
                                     $supply_order_details = $supply_order->getEntries();
                                     foreach ($supply_order_details as $supply_order_detail) {
                                         $is_present = Stock::productIsPresentInStock($supply_order_detail['id_product'], $supply_order_detail['id_product_attribute'], $supply_order->id_warehouse);
                                         if (!$is_present) {
                                             $stock = new Stock();
                                             $stock_params = array('id_product_attribute' => $supply_order_detail['id_product_attribute'], 'id_product' => $supply_order_detail['id_product'], 'physical_quantity' => 0, 'price_te' => $supply_order_detail['price_te'], 'usable_quantity' => 0, 'id_warehouse' => $supply_order->id_warehouse);
                                             // saves stock in warehouse
                                             $stock->hydrate($stock_params);
                                             $stock->add();
                                         }
                                     }
                                 }
                                 // if pending_receipt,
                                 // or if the order is being canceled,
                                 // or if the order is received completely
                                 // synchronizes StockAvailable
                                 if ($new_state->pending_receipt && !$new_state->receipt_state || ($old_state->receipt_state || $old_state->pending_receipt) && $new_state->enclosed && !$new_state->receipt_state || $new_state->receipt_state && $new_state->enclosed) {
                                     $supply_order_details = $supply_order->getEntries();
                                     $products_done = array();
                                     foreach ($supply_order_details as $supply_order_detail) {
                                         if (!in_array($supply_order_detail['id_product'], $products_done)) {
                                             StockAvailable::synchronize($supply_order_detail['id_product']);
                                             $products_done[] = $supply_order_detail['id_product'];
                                         }
                                     }
                                 }
                                 $token = Tools::getValue('token') ? Tools::getValue('token') : $this->token;
                                 $redirect = self::$currentIndex . '&token=' . $token;
                                 $this->redirect_after = $redirect . '&conf=5';
                             }
                         }
                     }
                 }
             } else {
                 $this->errors[] = Tools::displayError('The selected supplier is not valid.');
             }
         }
     }
     // updates receipt
     if (Tools::isSubmit('submitBulkUpdatesupply_order_detail') && Tools::isSubmit('id_supply_order')) {
         $this->postProcessUpdateReceipt();
     }
     // use template to create a supply order
     if (Tools::isSubmit('create_supply_order') && Tools::isSubmit('id_supply_order')) {
         $this->postProcessCopyFromTemplate();
     }
     if (!count($this->errors) && $this->is_editing_order || !$this->is_editing_order) {
         parent::postProcess();
     }
 }
 /**
  * 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();
         }
     }
 }
Ejemplo n.º 6
0
    public function supplyOrdersDetailsImport()
    {
        // opens CSV & sets locale
        $this->receiveTab();
        $handle = $this->openCsvFile();
        AdminImportController::setLocale();
        $products = array();
        $reset = true;
        // main loop, for each supply orders details to import
        for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, $this->separator); ++$current_line) {
            // if convert requested
            if (Tools::getValue('convert')) {
                $line = $this->utf8EncodeArray($line);
            }
            $info = AdminImportController::getMaskedRow($line);
            // sets default values if needed
            AdminImportController::setDefaultValues($info);
            // gets the supply order
            if (array_key_exists('supply_order_reference', $info) && pSQL($info['supply_order_reference']) && SupplyOrder::exists(pSQL($info['supply_order_reference']))) {
                $supply_order = SupplyOrder::getSupplyOrderByReference(pSQL($info['supply_order_reference']));
            } else {
                $this->errors[] = sprintf($this->l('Supply Order (%s) could not be loaded (at line %d).'), (int) $info['supply_order_reference'], $current_line + 1);
            }
            if (empty($this->errors)) {
                // sets parameters
                $id_product = (int) $info['id_product'];
                if (!$info['id_product_attribute']) {
                    $info['id_product_attribute'] = 0;
                }
                $id_product_attribute = (int) $info['id_product_attribute'];
                $unit_price_te = (double) $info['unit_price_te'];
                $quantity_expected = (int) $info['quantity_expected'];
                $discount_rate = (double) $info['discount_rate'];
                $tax_rate = (double) $info['tax_rate'];
                // checks if one product is there only once
                if (isset($product['id_product'])) {
                    if ($product['id_product'] == $id_product_attribute) {
                        $this->errors[] = sprintf($this->l('Product (%d/%D) cannot be added twice (at line %d).'), $id_product, $id_product_attribute, $current_line + 1);
                    } else {
                        $product['id_product'] = $id_product_attribute;
                    }
                } else {
                    $product['id_product'] = 0;
                }
                // checks parameters
                if (false === ($supplier_reference = ProductSupplier::getProductSupplierReference($id_product, $id_product_attribute, $supply_order->id_supplier))) {
                    $this->errors[] = sprintf($this->l('Product (%d/%d) is not available for this order (at line %d).'), $id_product, $id_product_attribute, $current_line + 1);
                }
                if ($unit_price_te < 0) {
                    $this->errors[] = sprintf($this->l('Unit Price (tax excl.) (%d) is not valid (at line %d).'), $unit_price_te, $current_line + 1);
                }
                if ($quantity_expected < 0) {
                    $this->errors[] = sprintf($this->l('Quantity Expected (%d) is not valid (at line %d).'), $quantity_expected, $current_line + 1);
                }
                if ($discount_rate < 0 || $discount_rate > 100) {
                    $this->errors[] = sprintf($this->l('Discount rate (%d) is not valid (at line %d). %s.'), $discount_rate, $current_line + 1, $this->l('Format: Between 0 and 100'));
                }
                if ($tax_rate < 0 || $tax_rate > 100) {
                    $this->errors[] = sprintf($this->l('Quantity Expected (%d) is not valid (at line %d).'), $tax_rate, $current_line + 1, $this->l('Format: Between 0 and 100'));
                }
                // if no errors, sets supply order details
                if (empty($this->errors)) {
                    // resets order if needed
                    if ($reset) {
                        $supply_order->resetProducts();
                        $reset = false;
                    }
                    // creates new product
                    $supply_order_detail = new SupplyOrderDetail();
                    AdminImportController::arrayWalk($info, array('AdminImportController', 'fillInfo'), $supply_order_detail);
                    // sets parameters
                    $supply_order_detail->id_supply_order = $supply_order->id;
                    $currency = new Currency($supply_order->id_ref_currency);
                    $supply_order_detail->id_currency = $currency->id;
                    $supply_order_detail->exchange_rate = $currency->conversion_rate;
                    $supply_order_detail->supplier_reference = $supplier_reference;
                    $supply_order_detail->name = Product::getProductName($id_product, $id_product_attribute, $supply_order->id_lang);
                    // gets ean13 / ref / upc
                    $query = new DbQuery();
                    $query->select('
						IFNULL(pa.reference, IFNULL(p.reference, \'\')) as reference,
						IFNULL(pa.ean13, IFNULL(p.ean13, \'\')) as ean13,
						IFNULL(pa.upc, IFNULL(p.upc, \'\')) as upc
					');
                    $query->from('product', 'p');
                    $query->leftJoin('product_attribute', 'pa', 'pa.id_product = p.id_product AND id_product_attribute = ' . (int) $id_product_attribute);
                    $query->where('p.id_product = ' . (int) $id_product);
                    $query->where('p.is_virtual = 0 AND p.cache_is_pack = 0');
                    $res = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
                    $product_infos = $res['0'];
                    $supply_order_detail->reference = $product_infos['reference'];
                    $supply_order_detail->ean13 = $product_infos['ean13'];
                    $supply_order_detail->upc = $product_infos['upc'];
                    $supply_order_detail->add();
                    $supply_order->update();
                    unset($supply_order_detail);
                }
            }
        }
        // closes
        $this->closeCsvFile($handle);
    }
Ejemplo n.º 7
0
         }
     } else {
         $total = '0';
     }
     $total = number_format($total, 2, '.', ' ');
     $total = str_replace(',', '', $total);
     echo $total;
     break;
     /*  Group facturation */
 /*  Group facturation */
 case 'billing':
     $orders = Tools::getValue('orders');
     $invoice_number = Tools::getValue('invoice_number');
     $date_to_invoice = Tools::getValue('date_to_invoice');
     foreach ($orders as $id_supply_order) {
         $order = new SupplyOrder($id_supply_order);
         $order->invoice_number = $invoice_number;
         $order->date_to_invoice = $date_to_invoice;
         $order->id_supply_order_state = 5;
         $order->save();
     }
     break;
     /* change supply order state */
 /* change supply order state */
 case 'updateSupplyOrderStatus':
     // get state ID
     $id_state = (int) Tools::getValue('id_supply_order_state', 0);
     if ($id_state <= 0) {
         echo Tools::jsonEncode(array('error' => $erpip->l('The selected supply order status is not valid.')));
         exit;
     }
    public function ajaxGetSupplyOrderDetail()
    {
        if (Tools::isSubmit('id_supplier_order')) {
            $no_result = true;
            $id_supplier_order = (int) pSQL(Tools::getValue('id_supplier_order'));
            /*  try to load supply order */
            $supply_order = new SupplyOrder($id_supplier_order);
            $enteries = $supply_order->getEntriesCollection();
            if (!empty($enteries)) {
                $i = 0;
                $j = 0;
                $nb_enteries = count($enteries);
                foreach ($enteries as $enterie) {
                    if ($no_result == true) {
                        echo '<table class="table" style="float: left;width:' . ($nb_enteries > 10 ? '400px' : '100%') . ';">
												<tr>
														<th width="60%">' . $this->l('Name') . '</th>
														<th width="20%">' . $this->l('Quantity') . '</th>
														<th width="20%">' . $this->l('Price') . '</th>
												</tr>';
                        $j = $j + 1;
                    }
                    /*  fields name :name, quantity_expected,name_displayed, price_te,price_ti
                     * ,price_with_order_discount_te, tax_rate , quantity_received
                     * , reference ,supplier_reference */
                    ?>
								<tr height="40">
										<td ><?php 
                    echo $enterie->name;
                    ?>
</td>
										<td class="right"><?php 
                    echo (int) $enterie->quantity_expected;
                    ?>
</td>
										<td  class="right"><?php 
                    echo Tools::displayPrice($enterie->price_te, (int) $supply_order->id_currency);
                    ?>
</td>
								</tr>
								<?php 
                    if ($i % 9 == 0 && $i != 0) {
                        if ($j % 4 == 0) {
                            echo '<br />';
                        }
                        echo '</table>
												<table class="table" style="float: right;width: 400px;">
												<tr>
														<th>' . $this->l('Name') . '</th>
														<th>' . $this->l('Quantity') . '</th>
														<th>' . $this->l('Price') . '</th>
												</tr>';
                        $j = $j + 1;
                        $i = 0;
                    } else {
                        $i = $i + 1;
                    }
                    $no_result = false;
                }
                echo '</table>';
            }
        } else {
            echo $this->l('Error: no supply order found !');
        }
    }
 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';
 }