// StockCover : added warehouse selection & fixed period selection

This commit is contained in:
bMancone committed 2011-10-24 10:10:14 +00:00
1 parent ae4187da03
commit a70e6e4a7f
3 files changed
+80 -27

No files matched your search

@@ -23,18 +23,29 @@
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA * International Registered Trademark & Property of PrestaShop SA
*} *}
<form id="stock_cover" type="get">
{if count($stock_cover_periods) > 1}
<form type="get" onchange="submit();">
<label for="coverage_period">{l s="Select a period to cover:"}</label>
<input type="hidden" name="controller" value="AdminStockCover" /> <input type="hidden" name="controller" value="AdminStockCover" />
<input type="hidden" name="token" value="{$token}" /> <input type="hidden" name="token" value="{$token}" />
<select name="coverage_period"> {if count($stock_cover_periods) > 1}
<div id="stock_cover_form_period">
<label for="coverage_period">{l s="Select a period:"}</label>
<select name="coverage_period" onChange="$(this).parent().parent().submit();">
{foreach from=$stock_cover_periods key=k item=i} {foreach from=$stock_cover_periods key=k item=i}
<option {if $i == $stock_cover_cur_period} selected="selected"{/if} value="{$i}">{$k}</option> <option {if $i == $stock_cover_cur_period} selected="selected"{/if} value="{$i}">{$k}</option>
{/foreach} {/foreach}
</select> </select>
</form> </div>
{/if} {/if}
{if count($stock_cover_warehouses) > 1}
<div id="stock_cover_form_warehouse">
<label for="coverage_warehouse">{l s="Select a warehouse:"}</label>
<select name="coverage_warehouse" onChange="$(this).parent().parent().submit();">
{foreach from=$stock_cover_warehouses key=k item=i}
<option {if $i.id_warehouse == $stock_cover_cur_warehouse} selected="selected"{/if} value="{$i.id_warehouse}">{$i.name}</option>
{/foreach}
</select>
</div>
{/if}
</form>
{$content} {$content}
+5 -6
View File
@@ -519,14 +519,14 @@ class StockManagerCore implements StockManagerInterface
SELECT SUM(sm.`physical_quantity`) as quantity_out SELECT SUM(sm.`physical_quantity`) as quantity_out
FROM `'._DB_PREFIX_.'stock_mvt` sm FROM `'._DB_PREFIX_.'stock_mvt` sm
LEFT JOIN `'._DB_PREFIX_.'stock` s ON (sm.`id_stock` = s.`id_stock`) 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` p ON (p.`id_product` = s.`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_attribute` pa ON (p.`id_product` = pa.`id_product`)
WHERE sm.`sign` = -1 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 : '').' ($id_warehouse ? ' AND s.`id_warehouse` = '.(int)$id_warehouse : '').'
GROUP BY sm.`id_stock_mvt`
ORDER BY sm.`date_add` DESC'; ORDER BY sm.`date_add` DESC';
$quantity_out = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query); $quantity_out = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
if (!$quantity_out) if (!$quantity_out)
return 0; return 0;
@@ -536,7 +536,6 @@ class StockManagerCore implements StockManagerInterface
$id_product_attribute, $id_product_attribute,
($id_warehouse ? array($id_warehouse) : null), ($id_warehouse ? array($id_warehouse) : null),
true); true);
$time_left = ($quantity_per_day == 0) ? 365 : round($physical_quantity / $quantity_per_day); $time_left = ($quantity_per_day == 0) ? 365 : round($physical_quantity / $quantity_per_day);
return $time_left; return $time_left;
+58 -15
View File
@@ -30,6 +30,9 @@
*/ */
class AdminStockCoverControllerCore extends AdminController class AdminStockCoverControllerCore extends AdminController
{ {
private $stock_cover_warehouses;
private $stock_cover_periods;
public function __construct() public function __construct()
{ {
$this->context = Context::getContext(); $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(); 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_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_.'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) 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'; GROUP BY a.id_product_attribute';
$datas = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query); $datas = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
foreach ($datas as &$data) foreach ($datas as &$data)
{
if ($this->getCurrentCoverageWarehouse() == -1)
$data['coverage'] = StockManagerFactory::getManager()->getProductCoverage($data['id_product'], $data['id'], $this->getCurrentCoveragePeriod()); $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)); echo Tools::jsonEncode(array('data'=> $datas, 'fields_display' => $this->fieldsDisplay));
} }
@@ -126,18 +147,13 @@ class AdminStockCoverControllerCore extends AdminController
$this->_select = 'a.id_product as id, COUNT(pa.id_product_attribute) as variations, s.physical_quantity as stock'; $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) $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)'; 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->context->smarty->assign('stock_cover_periods', $this->stock_cover_periods);
$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_cur_period', $this->getCurrentCoveragePeriod()); $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->displayInformation(
$this->l('Considering the coverage period choosen and the quantity of products/combinations that you sold, $this->l('Considering the coverage period choosen and the quantity of products/combinations that you sold,
@@ -162,7 +178,10 @@ class AdminStockCoverControllerCore extends AdminController
$item = &$this->_list[$i]; $item = &$this->_list[$i];
if ((int)$item['variations'] <= 0) 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'])); $this->addRowActionSkipList('details', array($item['id']));
} }
} }
@@ -177,9 +196,33 @@ class AdminStockCoverControllerCore extends AdminController
{ {
static $coverage_period = 0; static $coverage_period = 0;
// if coverage period == 0 then it is set to 7, otherwise, checks if we can get it via $_GET if ($coverage_period == 0)
$coverage_period = ($coverage_period == 0 ? 7 : ((int)Tools::getValue('coverage_period') ? (int)Tools::getValue('coverage_period') : 7)); {
$coverage_period = 7; // Week by default
if ((int)Tools::getValue('coverage_period'))
$coverage_period = (int)Tools::getValue('coverage_period');
}
return $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;
}
} }