From a70e6e4a7fd1d113cd3963c25efb673bdb493d4a Mon Sep 17 00:00:00 2001 From: bMancone Date: Mon, 24 Oct 2011 10:10:14 +0000 Subject: [PATCH] // StockCover : added warehouse selection & fixed period selection --- .../themes/template/stock_cover/content.tpl | 25 ++++-- classes/stock/StockManager.php | 11 ++- .../admin/AdminStockCoverController.php | 77 +++++++++++++++---- 3 files changed, 83 insertions(+), 30 deletions(-) diff --git a/admin-dev/themes/template/stock_cover/content.tpl b/admin-dev/themes/template/stock_cover/content.tpl index cec6c5186..2b6fe8559 100644 --- a/admin-dev/themes/template/stock_cover/content.tpl +++ b/admin-dev/themes/template/stock_cover/content.tpl @@ -23,18 +23,29 @@ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA *} - +
+ + {if count($stock_cover_periods) > 1} - - - - - {foreach from=$stock_cover_periods key=k item=i} {/foreach} -
+ {/if} +{if count($stock_cover_warehouses) > 1} +
+ + +
+{/if} + {$content} \ No newline at end of file diff --git a/classes/stock/StockManager.php b/classes/stock/StockManager.php index 35887364c..82ae4192d 100644 --- a/classes/stock/StockManager.php +++ b/classes/stock/StockManager.php @@ -519,14 +519,14 @@ class StockManagerCore implements StockManagerInterface SELECT SUM(sm.`physical_quantity`) as quantity_out FROM `'._DB_PREFIX_.'stock_mvt` sm LEFT JOIN `'._DB_PREFIX_.'stock` s ON (sm.`id_stock` = s.`id_stock`) - LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product` = s.`id_product` AND p.`id_product` = '.(int)$id_product.') - LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (p.`id_product` = pa.`id_product` AND pa.id_product_attribute = '.(int)$id_product_attribute.') + LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product` = s.`id_product`) + LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (p.`id_product` = pa.`id_product`) WHERE sm.`sign` = -1 - AND TO_DAYS(NOW()) - TO_DAYS(sm.`date_add`) <= '.(int)$coverage. + AND TO_DAYS(NOW()) - TO_DAYS(sm.`date_add`) <= '.(int)$coverage.' + AND s.`id_product` = '.(int)$id_product.' + AND s.`id_product_attribute` = '.(int)$id_product_attribute. ($id_warehouse ? ' AND s.`id_warehouse` = '.(int)$id_warehouse : '').' - GROUP BY sm.`id_stock_mvt` ORDER BY sm.`date_add` DESC'; - $quantity_out = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query); if (!$quantity_out) return 0; @@ -536,7 +536,6 @@ class StockManagerCore implements StockManagerInterface $id_product_attribute, ($id_warehouse ? array($id_warehouse) : null), true); - $time_left = ($quantity_per_day == 0) ? 365 : round($physical_quantity / $quantity_per_day); return $time_left; diff --git a/controllers/admin/AdminStockCoverController.php b/controllers/admin/AdminStockCoverController.php index 3a8227520..480b606fc 100644 --- a/controllers/admin/AdminStockCoverController.php +++ b/controllers/admin/AdminStockCoverController.php @@ -30,6 +30,9 @@ */ class AdminStockCoverControllerCore extends AdminController { + private $stock_cover_warehouses; + private $stock_cover_periods; + public function __construct() { $this->context = Context::getContext(); @@ -74,6 +77,18 @@ class AdminStockCoverControllerCore extends AdminController ), ); + $this->stock_cover_periods = array( + $this->l('One week') => 7, + $this->l('Two weeks') => 14, + $this->l('Three weeks') => 21, + $this->l('One month') => 31, + $this->l('Six months') => 186, + $this->l('One year') => 365 + ); + + $this->stock_cover_warehouses = Warehouse::getWarehouseList(true); + array_unshift($this->stock_cover_warehouses, array('id_warehouse' => -1, 'name' => $this->l('All Warehouses'))); + parent::__construct(); } @@ -100,12 +115,18 @@ class AdminStockCoverControllerCore extends AdminController 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.') LEFT JOIN '._DB_PREFIX_.'stock s ON (a.id_product_attribute = s.id_product_attribute) - WHERE a.id_product = '.$product_id.' + WHERE a.id_product = '.$product_id. + ($this->getCurrentCoverageWarehouse() != -1 ? ' AND s.id_warehouse = '.$this->getCurrentCoverageWarehouse() : '').' GROUP BY a.id_product_attribute'; $datas = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query); foreach ($datas as &$data) - $data['coverage'] = StockManagerFactory::getManager()->getProductCoverage($data['id_product'], $data['id'], $this->getCurrentCoveragePeriod()); + { + if ($this->getCurrentCoverageWarehouse() == -1) + $data['coverage'] = StockManagerFactory::getManager()->getProductCoverage($data['id_product'], $data['id'], $this->getCurrentCoveragePeriod()); + else + $data['coverage'] = StockManagerFactory::getManager()->getProductCoverage($data['id_product'], $data['id'], $this->getCurrentCoveragePeriod(), $this->getCurrentCoverageWarehouse()); + } echo Tools::jsonEncode(array('data'=> $datas, 'fields_display' => $this->fieldsDisplay)); } @@ -126,22 +147,17 @@ class AdminStockCoverControllerCore extends AdminController $this->_select = 'a.id_product as id, COUNT(pa.id_product_attribute) as variations, s.physical_quantity as stock'; $this->_join = 'LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (pa.id_product = a.id_product) LEFT JOIN `'._DB_PREFIX_.'stock` s ON (s.id_product = a.id_product AND s.id_product_attribute = 0)'; + if ($this->getCurrentCoverageWarehouse() != -1) + $this->_where = 'AND s.id_warehouse = '.$this->getCurrentCoverageWarehouse(); - $stock_cover_periods = array( - $this->l('One week') => 7, - $this->l('Two weeks') => 14, - $this->l('Three weeks') => 21, - $this->l('One month') => 31, - $this->l('Six months') => 186, - $this->l('One year') => 365 - ); - - $this->context->smarty->assign('stock_cover_periods', $stock_cover_periods); + $this->context->smarty->assign('stock_cover_periods', $this->stock_cover_periods); $this->context->smarty->assign('stock_cover_cur_period', $this->getCurrentCoveragePeriod()); + $this->context->smarty->assign('stock_cover_warehouses', $this->stock_cover_warehouses); + $this->context->smarty->assign('stock_cover_cur_warehouse', $this->getCurrentCoverageWarehouse()); $this->displayInformation( $this->l('Considering the coverage period choosen and the quantity of products/combinations that you sold, - this interface gives you an idea of when one product will run out of stock .' + this interface gives you an idea of when one product will run out of stock .' ) ); @@ -162,7 +178,10 @@ class AdminStockCoverControllerCore extends AdminController $item = &$this->_list[$i]; if ((int)$item['variations'] <= 0) { - $item['coverage'] = StockManagerFactory::getManager()->getProductCoverage($item['id'], 0, 4); + if ($this->getCurrentCoverageWarehouse() == -1) + $item['coverage'] = StockManagerFactory::getManager()->getProductCoverage($item['id'], 0, $this->getCurrentCoveragePeriod()); + else + $item['coverage'] = StockManagerFactory::getManager()->getProductCoverage($item['id'], 0, $this->getCurrentCoveragePeriod(), $this->getCurrentCoverageWarehouse()); $this->addRowActionSkipList('details', array($item['id'])); } } @@ -177,9 +196,33 @@ class AdminStockCoverControllerCore extends AdminController { static $coverage_period = 0; - // if coverage period == 0 then it is set to 7, otherwise, checks if we can get it via $_GET - $coverage_period = ($coverage_period == 0 ? 7 : ((int)Tools::getValue('coverage_period') ? (int)Tools::getValue('coverage_period') : 7)); - + if ($coverage_period == 0) + { + $coverage_period = 7; // Week by default + if ((int)Tools::getValue('coverage_period')) + $coverage_period = (int)Tools::getValue('coverage_period'); + } return $coverage_period; } + + /** + * Gets the current warehouse used + * + * @return int id_warehouse + */ + private function getCurrentCoverageWarehouse() + { + static $warehouse = 0; + + if ($warehouse == 0) + { + $warehouse = null; + if ((int)Tools::getValue('coverage_warehouse')) + $warehouse = (int)Tools::getValue('coverage_warehouse'); + else if ((int)$this->context->cookie->warehouse) + $warehouse = (int)$this->context->cookie->warehouse; + $this->context->cookie->warehouse = $warehouse; + } + return $warehouse; + } } \ No newline at end of file