From 59ebf2b7d07592d43c17551f4b1c0e8052763b60 Mon Sep 17 00:00:00 2001 From: dSevere Date: Fri, 21 Oct 2011 15:06:50 +0000 Subject: [PATCH] // add forms for add / remove / transfer stock git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@9557 b9a71923-0436-4b27-9f14-aed3839534dd --- .../admin/AdminStockManagementController.php | 964 ++++++++++++++---- 1 file changed, 746 insertions(+), 218 deletions(-) diff --git a/controllers/admin/AdminStockManagementController.php b/controllers/admin/AdminStockManagementController.php index ace6e95ca..dcf11a94a 100644 --- a/controllers/admin/AdminStockManagementController.php +++ b/controllers/admin/AdminStockManagementController.php @@ -28,7 +28,7 @@ /** * @since 1.5.0 */ -class AdminStockManagementController extends AdminController +class AdminStockManagementControllerCore extends AdminController { public function __construct() { @@ -37,21 +37,18 @@ class AdminStockManagementController extends AdminController $this->className = 'Product'; $this->lang = true; - $this->addRowAction('details'); - $this->addRowAction('addstock'); - $this->addRowAction('removestock'); - $this->addRowAction('transferstock'); - $this->fieldsDisplay = array( 'reference' => array( 'title' => $this->l('Reference'), 'align' => 'center', + 'filter_key' => 'a!reference', 'width' => 100, 'widthColumn' => 150 ), 'ean13' => array( 'title' => $this->l('EAN13'), 'align' => 'center', + 'filter_key' => 'a!ean13', 'width' => 75, 'widthColumn' => 100 ), @@ -59,7 +56,6 @@ class AdminStockManagementController extends AdminController 'title' => $this->l('Name'), 'width' => 350, 'widthColumn' => 'auto', - 'filter_key' => 'b!name' ), 'stock' => array( 'title' => $this->l('Total quantities in stock'), @@ -71,40 +67,640 @@ class AdminStockManagementController extends AdminController ), ); - $this->_select = 'a.id_product as id, COUNT(pa.id_product_attribute) as variations'; - $this->_join = 'LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (pa.id_product = a.id_product)'; - - // Manage specific forms for stock management - $this->declareStockManagementForms(); - parent::__construct(); + + // Override confirmation messages specifically for this controller + $this->_conf = array( + 1 => $this->l('The product was successfully added to stock'), + 2 => $this->l('The product was properly removed from the stock'), + 3 => $this->l('The transfer was done properly'), + ); } /** - * init override + * AdminController::initList() override + * @see AdminController::initList() + */ + public function initList() + { + $this->addRowAction('details'); + $this->addRowAction('addstock'); + $this->addRowAction('removestock'); + $this->addRowAction('transferstock'); + + //no link on list rows + $this->list_no_link = true; + + $this->_select = 'a.id_product as id, COUNT(pa.id_product_attribute) as variations'; + $this->_join = 'LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (pa.id_product = a.id_product)'; + + $this->displayInformation( + $this->l('This interface allows you to manage the stocks of each of your products and their variations. + The quantities of each product are global, so includes all quantities of each warehouses. + You can add and delete products related to a given warehouse. + You can transfer products from a warehouse to another.' + ) + ); + + return parent::initList(); + } + + /** + * AdminController::initForm() override + * @see AdminController::initForm() + */ + public function initForm() + { + //get warehouses list + $warehouses = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' + SELECT `id_warehouse`, CONCAT(`reference`, " - ", `name`) as name + FROM `'._DB_PREFIX_.'warehouse` + ORDER BY `reference` ASC'); + + $currencies = Currency::getCurrencies(); + + switch ($this->display) + { + case 'addstock' : + $this->fields_form = array( + 'legend' => array( + 'title' => $this->l('Add product to stock'), + 'image' => '../img/admin/arrow_up.png' + ), + 'input' => array( + array( + 'type' => 'hidden', + 'name' => 'is_post', + ), + array( + 'type' => 'hidden', + 'name' => 'id_product', + ), + array( + 'type' => 'hidden', + 'name' => 'id_product_attribute', + ), + array( + 'type' => 'hidden', + 'name' => 'check', + ), + array( + 'type' => 'text', + 'label' => $this->l('Reference:'), + 'name' => 'reference', + 'size' => 30, + 'disabled' => true, + ), + array( + 'type' => 'text', + 'label' => $this->l('Manufacturer reference:'), + 'name' => 'manufacturer_reference', + 'size' => 30, + 'disabled' => true, + ), + array( + 'type' => 'text', + 'label' => $this->l('EAN13:'), + 'name' => 'ean13', + 'size' => 15, + 'disabled' => true, + ), + array( + 'type' => 'text', + 'label' => $this->l('Name :'), + 'name' => 'name', + 'size' => 75, + 'disabled' => true, + ), + array( + 'type' => 'text', + 'label' => $this->l('Quantity to add:'), + 'name' => 'quantity', + 'size' => 10, + 'maxlength' => 6, + 'required' => true, + 'p' => $this->l('Physical quantity to add to the stock for this product') + ), + array( + 'type' => 'radio', + 'label' => $this->l('Usable for sale?:'), + 'name' => 'usable', + 'required' => true, + 'class' => 't', + 'is_bool' => true, + 'values' => array( + array( + 'id' => 'active_on', + 'value' => 1, + 'label' => $this->l('Enabled') + ), + array( + 'id' => 'active_off', + 'value' => 0, + 'label' => $this->l('Disabled') + ) + ), + 'p' => $this->l('Is this quantity is usable for sale on shops, or reserved in the warehouse for other purpose ?:') + ), + array( + 'type' => 'select', + 'label' => $this->l('Warehouse:'), + 'name' => 'id_warehouse', + 'required' => true, + 'options' => array( + 'query' => $warehouses, + 'id' => 'id_warehouse', + 'name' => 'name' + ), + 'p' => $this->l('Select the warehouse where you want to add the product into') + ), + array( + 'type' => 'text', + 'label' => $this->l('Unit price (TE):'), + 'name' => 'price', + 'required' => true, + 'size' => 10, + 'maxlength' => 10, + 'p' => $this->l('Unit purchase price or unit manufacturing cost for this product, tax excluded') + ), + array( + 'type' => 'select', + 'label' => $this->l('Currency:'), + 'name' => 'id_currency', + 'required' => true, + 'options' => array( + 'query' => $currencies, + 'id' => 'id_currency', + 'name' => 'name' + ), + 'p' => $this->l('The currency associated to the product unit price.'), + ), + array( + 'type' => 'select', + 'label' => $this->l('Reason :'), + 'name' => 'id_stock_mvt_reason', + 'required' => true, + 'options' => array( + 'query' => StockMvtReason::getStockMvtReasons($this->context->language->id, 1), + 'id' => 'id_stock_mvt_reason', + 'name' => 'name' + ), + 'p' => $this->l('Reason to add in stock movements'), + ), + ), + 'submit' => array( + 'title' => $this->l(' Add to stock '), + 'class' => 'button' + ) + ); + break; + + case 'removestock' : + $this->fields_form = array( + 'legend' => array( + 'title' => $this->l('Remove product from stock'), + 'image' => '../img/admin/arrow_down.png' + ), + 'input' => array( + array( + 'type' => 'hidden', + 'name' => 'is_post', + ), + array( + 'type' => 'hidden', + 'name' => 'id_product', + ), + array( + 'type' => 'hidden', + 'name' => 'id_product_attribute', + ), + array( + 'type' => 'hidden', + 'name' => 'check', + ), + array( + 'type' => 'text', + 'label' => $this->l('Reference:'), + 'name' => 'reference', + 'size' => 30, + 'disabled' => true, + ), + array( + 'type' => 'text', + 'label' => $this->l('Manufacturer reference:'), + 'name' => 'manufacturer_reference', + 'size' => 30, + 'disabled' => true, + ), + array( + 'type' => 'text', + 'label' => $this->l('EAN13:'), + 'name' => 'ean13', + 'size' => 15, + 'disabled' => true, + ), + array( + 'type' => 'text', + 'label' => $this->l('Name :'), + 'name' => 'name', + 'size' => 75, + 'disabled' => true, + ), + array( + 'type' => 'text', + 'label' => $this->l('Quantity to remove:'), + 'name' => 'quantity', + 'size' => 10, + 'maxlength' => 6, + 'required' => true, + 'p' => $this->l('Physical quantity to remove from the stock for this product') + ), + array( + 'type' => 'radio', + 'label' => $this->l('Usable for sale?:'), + 'name' => 'usable', + 'required' => true, + 'class' => 't', + 'is_bool' => true, + 'values' => array( + array( + 'id' => 'active_on', + 'value' => 1, + 'label' => $this->l('Enabled') + ), + array( + 'id' => 'active_off', + 'value' => 0, + 'label' => $this->l('Disabled') + ) + ), + 'p' => $this->l('Do you want to remove this quantity from usable quantity for sale on shops ?:') + ), + array( + 'type' => 'select', + 'label' => $this->l('Warehouse:'), + 'name' => 'id_warehouse', + 'required' => true, + 'options' => array( + 'query' => $warehouses, + 'id' => 'id_warehouse', + 'name' => 'name' + ), + 'p' => $this->l('Select the warehouse from where you want to remove the product') + ), + array( + 'type' => 'select', + 'label' => $this->l('Reason :'), + 'name' => 'id_stock_mvt_reason', + 'required' => true, + 'options' => array( + 'query' => StockMvtReason::getStockMvtReasons($this->context->language->id, -1), + 'id' => 'id_stock_mvt_reason', + 'name' => 'name' + ), + 'p' => $this->l('Reason to add in stock movements'), + ), + ), + 'submit' => array( + 'title' => $this->l(' Remove from stock '), + 'class' => 'button' + ) + ); + break; + + case 'transferstock' : + $this->fields_form = array( + 'legend' => array( + 'title' => $this->l('Transfert product from warehouse to another'), + 'image' => '../img/admin/arrow-right.png' + ), + 'input' => array( + array( + 'type' => 'hidden', + 'name' => 'is_post', + ), + array( + 'type' => 'hidden', + 'name' => 'id_product', + ), + array( + 'type' => 'hidden', + 'name' => 'id_product_attribute', + ), + array( + 'type' => 'hidden', + 'name' => 'check', + ), + array( + 'type' => 'text', + 'label' => $this->l('Reference:'), + 'name' => 'reference', + 'size' => 30, + 'disabled' => true, + ), + array( + 'type' => 'text', + 'label' => $this->l('Manufacturer reference:'), + 'name' => 'manufacturer_reference', + 'size' => 30, + 'disabled' => true, + ), + array( + 'type' => 'text', + 'label' => $this->l('EAN13:'), + 'name' => 'ean13', + 'size' => 15, + 'disabled' => true, + ), + array( + 'type' => 'text', + 'label' => $this->l('Name :'), + 'name' => 'name', + 'size' => 75, + 'disabled' => true, + ), + array( + 'type' => 'text', + 'label' => $this->l('Quantity to transfer:'), + 'name' => 'quantity', + 'size' => 10, + 'maxlength' => 6, + 'required' => true, + 'p' => $this->l('Physical quantity to transfer for this product') + ), + array( + 'type' => 'select', + 'label' => $this->l('Source Warehouse:'), + 'name' => 'id_warehouse_from', + 'required' => true, + 'options' => array( + 'query' => $warehouses, + 'id' => 'id_warehouse', + 'name' => 'name' + ), + 'p' => $this->l('Select the warehouse from where you want to transfer the product') + ), + array( + 'type' => 'radio', + 'label' => $this->l('Usable for sale in source warehouse ?:'), + 'name' => 'usable_from', + 'required' => true, + 'class' => 't', + 'is_bool' => true, + 'values' => array( + array( + 'id' => 'active_on', + 'value' => 1, + 'label' => $this->l('Enabled') + ), + array( + 'id' => 'active_off', + 'value' => 0, + 'label' => $this->l('Disabled') + ) + ), + 'p' => $this->l('Do you want to transfer this quantity from usable quantity for sale on shops in the source warehouse ?:') + ), + array( + 'type' => 'select', + 'label' => $this->l('Destination Warehouse:'), + 'name' => 'id_warehouse_to', + 'required' => true, + 'options' => array( + 'query' => $warehouses, + 'id' => 'id_warehouse', + 'name' => 'name' + ), + 'p' => $this->l('Select the warehouse where you want to transfer the product') + ), + array( + 'type' => 'radio', + 'label' => $this->l('Usable for sale in destination warehosue ?:'), + 'name' => 'usable_to', + 'required' => true, + 'class' => 't', + 'is_bool' => true, + 'values' => array( + array( + 'id' => 'active_on', + 'value' => 1, + 'label' => $this->l('Enabled') + ), + array( + 'id' => 'active_off', + 'value' => 0, + 'label' => $this->l('Disabled') + ) + ), + 'p' => $this->l('Do you want to transfer this quantity as usable quantity for sale on shops in the destination warehouse ?:') + ), + ), + 'submit' => array( + 'title' => $this->l(' Transfer '), + 'class' => 'button' + ) + ); + break; + } + } + + /** + * AdminController::postProcess() override + * @see AdminController::postProcess() + */ + public function postProcess() + { + parent::postProcess(); + + // Global checks when add / remove / transfer product + if ((Tools::isSubmit('addstock') || Tools::isSubmit('removestock') || Tools::isSubmit('transferstock') ) && Tools::isSubmit('is_post')) + { + // get product ID + $id_product = (int)Tools::getValue('id_product', 0); + if ($id_product <= 0) + $this->_errors[] = Tools::displayError('The selected product is not valid.'); + + // get product_attribute ID + $id_product_attribute = (int)Tools::getValue('id_product_attribute', 0); + + // check the product hash + $check = Tools::getValue('check', ''); + $check_valid = md5(_COOKIE_KEY_.$id_product.$id_product_attribute); + if ($check != $check_valid) + $this->_errors[] = Tools::displayError('The selected product is not valid.'); + + // get quantity and check that the post value is really an integer + // If it's not, we have to do nothing. + $quantity = Tools::getValue('quantity', 0); + if (!is_numeric($quantity) || (int)$quantity <= 0) + $this->_errors[] = Tools::displayError('The quantity value is not valid.'); + $quantity = (int)$quantity; + + $token = Tools::getValue('token') ? Tools::getValue('token') : $this->token; + $redirect = self::$currentIndex.'&token='.$token; + } + + // Global checks when add / remove product + if ((Tools::isSubmit('addstock') || Tools::isSubmit('removestock') ) && Tools::isSubmit('is_post')) + { + // 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 stock movement reason id + $id_stock_mvt_reason = (int)Tools::getValue('id_stock_mvt_reason', 0); + if ($id_stock_mvt_reason <= 0 || !StockMvtReason::exists($id_stock_mvt_reason)) + $this->_errors[] = Tools::displayError('The reason is not valid.'); + + // get usable flag + $usable = Tools::getValue('usable', null); + if (is_null($usable)) + $this->_errors[] = Tools::displayError('You have to specify if the product quantity is usable for sale on shops.'); + $usable = (bool)$usable; + } + + if (Tools::isSubmit('addstock') && Tools::isSubmit('is_post')) + { + // get product unit price + $price = str_replace(',', '.', Tools::getValue('price', 0)); + if (!is_numeric($price)) + $this->_errors[] = Tools::displayError('The product price is not valid.'); + $price = round(floatval($price), 6); + + // get product unit price 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.'); + + // if all is ok, add stock + if (count($this->_errors) == 0) + { + $warehouse = new Warehouse($id_warehouse); + + // convert price to warehouse currency if needed + if ($id_currency != $warehouse->id_currency) + { + // First convert price to the default currency + $price_converted_to_default_currency = Tools::convertPrice($price, $id_currency, false); + + // Convert the new price from default currency to needed currency + $price = Tools::convertPrice($price_converted_to_default_currency, $warehouse->id_currency, true); + } + + // add stock + $stock_manager = StockManagerFactory::getManager(); + + if ($stock_manager->addProduct($id_product, $id_product_attribute, $warehouse, $quantity, $id_stock_mvt_reason, $price, $usable)) + Tools::redirectAdmin($redirect.'&conf=1'); + else + $this->_errors[] = Tools::displayError('An error occured. No stock was added.'); + } + } + + if (Tools::isSubmit('removestock') && Tools::isSubmit('is_post')) + { + // if all is ok, remove stock + if (count($this->_errors) == 0) + { + $warehouse = new Warehouse($id_warehouse); + + // remove stock + $stock_manager = StockManagerFactory::getManager(); + $removed_products = $stock_manager->removeProduct($id_product, $id_product_attribute, $warehouse, $quantity, $id_stock_mvt_reason, $usable); + + if (count($removed_products) > 0) + Tools::redirectAdmin($redirect.'&conf=2'); + else + $this->_errors[] = Tools::displayError('It is not possible to remove the specified quantity or an error occured. No stock was removed.'); + } + } + + if (Tools::isSubmit('transferstock') && Tools::isSubmit('is_post')) + { + // get source warehouse id + $id_warehouse_from = (int)Tools::getValue('id_warehouse_from', 0); + if ($id_warehouse_from <= 0 || !Warehouse::exists($id_warehouse_from)) + $this->_errors[] = Tools::displayError('The source warehouse is not valid.'); + + // get destination warehouse id + $id_warehouse_to = (int)Tools::getValue('id_warehouse_to', 0); + if ($id_warehouse_to <= 0 || !Warehouse::exists($id_warehouse_to)) + $this->_errors[] = Tools::displayError('The destination warehouse is not valid.'); + + // get usable flag for source warehouse + $usable_from = Tools::getValue('usable_from', null); + if (is_null($usable_from)) + $this->_errors[] = Tools::displayError('You have to specify if the product quantity is usable for sale on shops in source warehouse.'); + $usable_from = (bool)$usable_from; + + // get usable flag for destination warehouse + $usable_to = Tools::getValue('usable_to', null); + if (is_null($usable_to)) + $this->_errors[] = Tools::displayError('You have to specify if the product quantity is usable for sale on shops in destination warehouse.'); + $usable_to = (bool)$usable_to; + + // if all is ok, transfer stock + if (count($this->_errors) == 0) + { + // transfer stock + $stock_manager = StockManagerFactory::getManager(); + + $is_transfer = $stock_manager->transferBetweenWarehouses( + $id_product, + $id_product_attribute, + $quantity, + 1, //@TOTO specify new default movement for transfer + $id_warehouse_from, + $id_warehouse_to, + $usable_from, + $usable_to + ); + + if ($is_transfer) + Tools::redirectAdmin($redirect.'&conf=3'); + else + $this->_errors[] = Tools::displayError('It is not possible to transfer the specified quantity, or an error occured. No stock was transfered.'); + } + } + } + + /** + * AdminController::init() override + * @see AdminController::init() */ public function init() { parent::init(); - if (isset($_GET['addstock'])) - { - $this->action = 'addstock'; + if (Tools::isSubmit('addstock')) $this->display = 'addstock'; - } + + if (Tools::isSubmit('removestock')) + $this->display = 'removestock'; + + if (Tools::isSubmit('transferstock')) + $this->display = 'transferstock'; } /** * method call when ajax request is made with the details row action + * @see AdminController::postProcess() */ public function ajaxProcess() { // test if an id is submit if (Tools::isSubmit('id')) { - // desactivate native lang gestion of the controller + // override attributes + $this->identifier = 'id_product_attribute'; + $this->display = 'list'; $this->lang = false; + $this->addRowAction('addstock'); + $this->addRowAction('removestock'); + $this->addRowAction('transferstock'); + // get current lang id $lang_id = (int)$this->context->language->id; @@ -127,10 +723,6 @@ class AdminStockManagementController extends AdminController $this->_where = 'AND a.id_product = '.$product_id; $this->_group = 'GROUP BY a.id_product_attribute'; - // override this attributes - $this->identifier = 'id_product_attribute'; - $this->display = 'list'; - // get list and force no limit clause in the request $this->getList($this->context->language->id, null, null, 0, false); @@ -138,6 +730,7 @@ class AdminStockManagementController extends AdminController $helper = new HelperList(); $helper->actions = $this->actions; $helper->list_skip_actions = $this->list_skip_actions; + $helper->no_link = true; $helper->shopLinkType = ''; $helper->identifier = $this->identifier; // Force render - no filter, form, js, sorting ... @@ -151,11 +744,12 @@ class AdminStockManagementController extends AdminController } /** - * getList override + * AdminController::getList() override + * @see AdminController::getList() */ - public function getList($id_lang, $orderBy = null, $orderWay = null, $start = 0, $limit = null, $id_lang_shop = false) + public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = false) { - parent::getList($id_lang, $orderBy, $orderWay, $start, $limit, $id_lang_shop); + parent::getList($id_lang, $order_by, $order_way, $start, $limit, $id_lang_shop); if ($this->display == 'list') { @@ -173,7 +767,7 @@ class AdminStockManagementController extends AdminController $this->addRowActionSkipList('details', array($item['id'])); // specify actions in function of stock - $this->skipActionByStock($item, $i, true); + $this->skipActionByStock($item, true); } // If current product has variations else if ((int)$item['variations'] > 0) @@ -189,7 +783,7 @@ class AdminStockManagementController extends AdminController $this->addRowActionSkipList('details', array($item['id'])); // specify actions in function of stock - $this->skipActionByStock($item, $i, false); + $this->skipActionByStock($item, false); } } } @@ -208,7 +802,7 @@ class AdminStockManagementController extends AdminController //get stocks for this product if ($is_product_variation) - $stock = $stock_manager->getProductPhysicalQuantities($item['id'], $item['id_product']); + $stock = $stock_manager->getProductPhysicalQuantities($item['id_product'], $item['id']); else $stock = $stock_manager->getProductPhysicalQuantities($item['id'], 0); @@ -223,202 +817,133 @@ class AdminStockManagementController extends AdminController } } - private function declareStockManagementForms() { - - $this->stock_management_forms['addstock'] = array( - 'legend' => array( - 'title' => $this->l('Warehouse'), - 'image' => '../img/admin/tab.gif' - ), - 'input' => array( - array( - 'type' => 'hidden', - 'name' => 'id_address', - ), - array( - 'type' => 'text', - 'label' => $this->l('Reference:'), - 'name' => 'reference', - 'size' => 30, - 'maxlength' => 32, - 'required' => true, - 'p' => $this->l('Code / Reference of this warehouse'), - ), - array( - 'type' => 'text', - 'label' => $this->l('Name:'), - 'name' => 'name', - 'size' => 40, - 'maxlength' => 45, - 'required' => true, - 'p' => $this->l('Name of this warehouse') - ), - array( - 'type' => 'text', - 'label' => $this->l('Phone:'), - 'name' => 'phone', - 'size' => 15, - 'maxlength' => 16, - 'p' => $this->l('Phone number of this warehouse') - ), - array( - 'type' => 'text', - 'label' => $this->l('Adress:'), - 'name' => 'address', - 'size' => 100, - 'maxlength' => 128, - 'required' => true - ), - array( - 'type' => 'text', - 'label' => $this->l('Adress:').' (2)', - 'name' => 'address2', - 'size' => 100, - 'maxlength' => 128, - ), - array( - 'type' => 'text', - 'label' => $this->l('Postcode/ Zip Code:'), - 'name' => 'postcode', - 'size' => 10, - 'maxlength' => 12, - 'required' => true, - ), - array( - 'type' => 'text', - 'label' => $this->l('City:'), - 'name' => 'city', - 'size' => 10, - 'maxlength' => 12, - 'required' => true, - ), - array( - 'type' => 'select', - 'label' => $this->l('Country:'), - 'name' => 'id_country', - 'required' => true, - 'options' => array( - 'query' => Country::getCountries($this->context->language->id, false), - 'id' => 'id_country', - 'name' => 'name' - ), - 'p' => $this->l('Country where state, region or city is located') - ), - array( - 'type' => 'select', - 'label' => $this->l('State'), - 'name' => 'id_state', - 'required' => true, - 'options' => array( - 'id' => 'id_state', - 'name' => 'name' - ) - ), - array( - 'type' => 'select', - 'label' => $this->l('Management type:'), - 'name' => 'management_type', - 'required' => true, - 'options' => array( - 'query' => array( - array( - 'id' => 'WA', - 'name' => $this->l('Weight Average') - ), - array( - 'id' => 'FIFO', - 'name' => $this->l('First In, First Out') - ), - array( - 'id' => 'LIFO', - 'name' => $this->l('Last In, First Out') - ), - ), - 'id' => 'id', - 'name' => 'name' - ), - 'p' => $this->l('Onventory valuation method'), - 'hint' => $this->l('Do not change this value before the end of the accounting period for this Warehouse.'), - ), - array( - 'type' => 'select', - 'label' => $this->l('Associated shops:'), - 'name' => 'ids_shops[]', - 'required' => true, - 'multiple' => true, - 'options' => array( - 'query' => Shop::getShops(), - 'id' => 'id_shop', - 'name' => 'name' - ), - 'p' => $this->l('Associated shops'), - 'hint' => $this->l('By associating a shop to a warehouse, all products in this warehouse will be available for sale in the associated shop. Shipment of an order of this shop is also possible from this warehouse'), - ), - array( - 'type' => 'select', - 'label' => $this->l('Associated carriers:'), - 'name' => 'ids_carriers[]', - 'required' => true, - 'multiple' => true, - 'options' => array( - 'query' => Carrier::getCarriers($this->context->language->id, true), - 'id' => 'id_carrier', - 'name' => 'name' - ), - 'p' => $this->l('Associated carrier'), - 'hint' => $this->l('You can specifiy the carriers availables for shipping orders from this warehouse'), - ), - ), - 'submit' => array( - 'title' => $this->l(' Save '), - 'class' => 'button' - ) - ); - } - /** - * initContent override + * AdminController::initContent() override + * @see AdminController::initContent() */ public function initContent() { - if ($this->display != 'addstock') - $this->display = 'list'; - - parent::initContent(); - - if ($this->display == 'addstock') + // Manage the add stock form + if ($this->display == 'addstock' || $this->display == 'removestock' || $this->display == 'transferstock') { - $this->fields_form = $this->stock_management_forms['addstock']; - - $this->getlanguages(); - $helper = new HelperForm(); - // Check if form template has been overriden - if (file_exists($this->context->smarty->template_dir.'/'.$this->tpl_folder.'form.tpl')) - $helper->tpl = $this->tpl_folder.'form.tpl'; - $helper->currentIndex = self::$currentIndex; - $helper->token = $this->token; - $helper->table = $this->table; - $helper->id = $obj->id; - $helper->languages = $this->_languages; - $helper->default_form_language = $this->default_form_language; - $helper->allow_employee_form_lang = $this->allow_employee_form_lang; - $helper->fields_value = $this->getFieldsValue($obj); - $this->content .= $helper->generateForm($this->fields_form); - - if ($this->tabAccess['view']) + if (Tools::isSubmit('id_product') || Tools::isSubmit('id_product_attribute')) { - if (Tools::getValue('back')) - $this->context->smarty->assign('back', Tools::safeOutput(Tools::getValue('back'))); + // get id product and product attribute if possible + $id_product = (int)Tools::getValue('id_product', 0); + $id_product_attribute = (int)Tools::getValue('id_product_attribute', 0); + + $product_is_valid = false; + $lang_id = $this->context->language->id; + + // try to load product attribute first + if ($id_product_attribute > 0) + { + // try to load product attribute + $combination = new Combination($id_product_attribute); + if (is_int($combination->id)) + { + $product_is_valid = true; + $id_product = $combination->id_product; + $reference = $combination->reference; + $ean13 = $combination->ean13; + $manufacturer_reference = $combination->supplier_reference; + + // get the full name for this combination + $query = new DbQuery(); + + $query->select('IFNULL(CONCAT(pl.`name`, \' : \', GROUP_CONCAT(agl.`name`, \' - \', al.`name` SEPARATOR \', \')),pl.`name`) as name'); + $query->from('product_attribute a'); + $query->join('INNER JOIN '._DB_PREFIX_.'product_lang pl ON (pl.`id_product` = a.`id_product` AND pl.`id_lang` = '.$lang_id.') + LEFT JOIN '._DB_PREFIX_.'product_attribute_combination pac ON (pac.`id_product_attribute` = a.`id_product_attribute`) + LEFT JOIN '._DB_PREFIX_.'attribute atr ON (atr.`id_attribute` = pac.`id_attribute`) + LEFT JOIN '._DB_PREFIX_.'attribute_lang al ON (al.`id_attribute` = atr.`id_attribute` AND al.`id_lang` = '.$lang_id.') + LEFT JOIN '._DB_PREFIX_.'attribute_group_lang agl ON (agl.`id_attribute_group` = atr.`id_attribute_group` AND agl.`id_lang` = '.$lang_id.')' + ); + $query->where('a.`id_product_attribute` = '.$id_product_attribute); + $name = Db::getInstance()->getValue($query); + } + } + // try to load a simple product else - $this->context->smarty->assign('back', Tools::safeOutput(Tools::getValue(self::$currentIndex.'&token='.$this->token))); + { + $product = new Product($id_product, false, $lang_id); + if (is_int($product->id)) + { + $product_is_valid = true; + $reference = $product->reference; + $ean13 = $product->ean13; + $name = $product->name; + $manufacturer_reference = $product->supplier_reference; + } + } + + if ($product_is_valid === true) + { + // init form + $this->initForm(); + $this->getlanguages(); + + $helper = new HelperForm(); + + // Check if form template has been overriden + if (file_exists($this->context->smarty->template_dir.'/'.$this->tpl_folder.'form.tpl')) + $helper->tpl = $this->tpl_folder.'form.tpl'; + + $helper->submit_action = $this->display; + $helper->currentIndex = self::$currentIndex; + $helper->token = $this->token; + $helper->id = null; // no display standard hidden field in the form + $helper->languages = $this->_languages; + $helper->default_form_language = $this->default_form_language; + $helper->allow_employee_form_lang = $this->allow_employee_form_lang; + + $helper->fields_value = array( + 'id_product' => $id_product, + 'id_product_attribute' => $id_product_attribute, + 'reference' => $reference, + 'manufacturer_reference' => $manufacturer_reference, + 'name' => $name, + 'ean13' => $ean13, + 'check' => md5(_COOKIE_KEY_.$id_product.$id_product_attribute), + 'quantity' => Tools::getValue('quantity', ''), + 'id_warehouse' => Tools::getValue('id_warehouse', ''), + 'usable' => Tools::getValue('usable', ''), + 'price' => Tools::getValue('price', ''), + 'id_currency' => Tools::getValue('id_currency', ''), + 'id_stock_mvt_reason' => Tools::getValue('id_stock_mvt_reason', ''), + 'is_post' => 1, + ); + + if ($this->display == 'transferstock') + { + $helper->fields_value['id_warehouse_from'] = Tools::getValue('id_warehouse_from', ''); + $helper->fields_value['id_warehouse_to'] = Tools::getValue('id_warehouse_to', ''); + $helper->fields_value['usable_from'] = Tools::getValue('usable_from', ''); + $helper->fields_value['usable_to'] = Tools::getValue('usable_to', ''); + } + + $this->content .= $helper->generateForm($this->fields_form); + + $this->context->smarty->assign(array( + 'content' => $this->content + )); + } + else + $this->_errors[] = Tools::displayError('The specified product is not valid'); } - } - + else + { + $this->display = 'list'; + parent::initContent(); + } } /** * Display addstock action link + * @param string $token the token to add to the link + * @param int $id the identifier to add to the link + * @return string */ public function displayAddstockLink($token = null, $id) { @@ -428,8 +953,7 @@ class AdminStockManagementController extends AdminController $this->context->smarty->assign(array( 'href' => self::$currentIndex. '&'.$this->identifier.'='.$id. - '&addstock'. - '&token='.($token != null ? $token : $this->token), + '&addstock&token='.($token != null ? $token : $this->token), 'action' => self::$cache_lang['AddStock'], )); @@ -438,6 +962,9 @@ class AdminStockManagementController extends AdminController /** * Display removestock action link + * @param string $token the token to add to the link + * @param int $id the identifier to add to the link + * @return string */ public function displayRemovestockLink($token = null, $id) { @@ -447,8 +974,7 @@ class AdminStockManagementController extends AdminController $this->context->smarty->assign(array( 'href' => self::$currentIndex. '&'.$this->identifier.'='.$id. - '&removestock'. - '&token='.($token != null ? $token : $this->token), + '&removestock&token='.($token != null ? $token : $this->token), 'action' => self::$cache_lang['RemoveStock'], )); @@ -457,6 +983,9 @@ class AdminStockManagementController extends AdminController /** * Display transferstock action link + * @param string $token the token to add to the link + * @param int $id the identifier to add to the link + * @return string */ public function displayTransferstockLink($token = null, $id) { @@ -466,8 +995,7 @@ class AdminStockManagementController extends AdminController $this->context->smarty->assign(array( 'href' => self::$currentIndex. '&'.$this->identifier.'='.$id. - '&transferstock'. - '&token='.($token != null ? $token : $this->token), + '&transferstock&token='.($token != null ? $token : $this->token), 'action' => self::$cache_lang['TransferStock'], ));