// Updated OrderHistory / StockManager. Added feature to StockCover: it is now possible to display the number of sales per product / and highlight products which coverage is less than a given threshold

This commit is contained in:
bMancone
2011-12-13 09:48:52 +00:00
parent 133de02a11
commit 4180588c40
7 changed files with 132 additions and 36 deletions

View File

@@ -39,6 +39,7 @@ class AdminStockCoverControllerCore extends AdminController
$this->table = 'product';
$this->className = 'Product';
$this->lang = true;
$this->colorOnBackground = true;
$this->fieldsDisplay = array(
'reference' => array(
@@ -63,12 +64,19 @@ class AdminStockCoverControllerCore extends AdminController
'title' => $this->l('Name'),
'filter_key' => 'b!name'
),
'qty_sold' => array(
'title' => $this->l('Quantity sold'),
'width' => 160,
'orderby' => false,
'search' => false,
'hint' => $this->l('Quantity sold during the defined period.'),
),
'coverage' => array(
'title' => $this->l('Coverage'),
'width' => 160,
'orderby' => false,
'search' => false,
'hint' => $this->l('Days left before you run out of stock.')
'hint' => $this->l('Days left before you run out of stock.'),
),
'stock' => array(
'title' => $this->l('Quantity'),
@@ -135,11 +143,25 @@ class AdminStockCoverControllerCore extends AdminController
{
$data['name'] = Product::getProductName($data['id_product'], $data['id']);
if ($this->getCurrentCoverageWarehouse() == -1)
$data['coverage'] = StockManagerFactory::getManager()->getProductCoverage($data['id_product'], $data['id'], $period);
else
$data['coverage'] = StockManagerFactory::getManager()->getProductCoverage($data['id_product'], $data['id'], $period, $warehouse);
// computes coverage
$coverage = StockManagerFactory::getManager()->getProductCoverage(
$data['id_product'],
$data['id'],
$period,
(($this->getCurrentCoverageWarehouse() == -1) ? null : $warehouse)
);
if ($coverage != -1) // if coverage is available
{
if ($coverage < $this->getCurrentWarning()) // if highlight needed
$data['color'] = '#BDE5F8';
$data['coverage'] = $coverage;
}
else // infinity
$data['coverage'] = '--';
$data['qty_sold'] = $this->getQuantitySold($data['id_product'], $data['id'], $this->getCurrentCoveragePeriod());
}
echo Tools::jsonEncode(array('data'=> $datas, 'fields_display' => $this->fieldsDisplay));
}
die;
@@ -169,7 +191,12 @@ class AdminStockCoverControllerCore extends AdminController
$this->tpl_list_vars['stock_cover_cur_period'] = $this->getCurrentCoveragePeriod();
$this->tpl_list_vars['stock_cover_warehouses'] = $this->stock_cover_warehouses;
$this->tpl_list_vars['stock_cover_cur_warehouse'] = $this->getCurrentCoverageWarehouse();
$this->ajax_params = array('period' => $this->getCurrentCoveragePeriod(), 'id_warehouse' => $this->getCurrentCoverageWarehouse());
$this->tpl_list_vars['stock_cover_warn_days'] = $this->getCurrentWarning();
$this->ajax_params = array(
'period' => $this->getCurrentCoveragePeriod(),
'id_warehouse' => $this->getCurrentCoverageWarehouse(),
'warn_days' => $this->getCurrentWarning()
);
$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.'));
@@ -191,22 +218,30 @@ class AdminStockCoverControllerCore extends AdminController
$item = &$this->_list[$i];
if ((int)$item['variations'] <= 0)
{
if ($this->getCurrentCoverageWarehouse() == -1) // if all warehouses
$item['coverage'] = StockManagerFactory::getManager()->getProductCoverage(
$item['id'],
0,
$this->getCurrentCoveragePeriod()
);
else // else selected warehouse
$item['coverage'] = StockManagerFactory::getManager()->getProductCoverage(
$item['id'],
0,
$this->getCurrentCoveragePeriod(),
$this->getCurrentCoverageWarehouse()
);
// computes coverage and displays (highlights if needed)
$coverage = StockManagerFactory::getManager()->getProductCoverage(
$item['id'],
0,
$this->getCurrentCoveragePeriod(),
(($this->getCurrentCoverageWarehouse() == -1) ? null : $this->getCurrentCoverageWarehouse())
);
if ($coverage != -1) // coverage is available
{
if ($coverage < $this->getCurrentWarning())
$item['color'] = '#BDE5F8';
$item['coverage'] = $coverage;
}
else // infinity
$item['coverage'] = '--';
// computes quantity sold
$item['qty_sold'] = $this->getQuantitySold($item['id'], 0, $this->getCurrentCoveragePeriod());
// removes 'details' action on products without attributes
$this->addRowActionSkipList('details', array($item['id']));
}
else
{
@@ -253,4 +288,40 @@ class AdminStockCoverControllerCore extends AdminController
}
return $warehouse;
}
/**
* Gets the current warning
*
* @return int warn period
*/
private function getCurrentWarning()
{
static $warning = 0;
if ($warning == 0)
{
$warning = 0;
if (Tools::getValue('warn_days') && Validate::isInt(Tools::getValue('warn_days')))
$warning = (int)Tools::getValue('warn_days');
}
return $warning;
}
protected function getQuantitySold($id_product, $id_product_attribute, $coverage)
{
$query = new DbQuery();
$query->select('SUM(od.product_quantity)');
$query->from('order_detail od');
$query->leftJoin('orders o ON (od.id_order = o.id_order)');
$query->leftJoin('order_history oh ON (o.date_upd = oh.date_add)');
$query->leftJoin('order_state os ON (os.id_order_state = oh.id_order_state)');
$query->where('od.product_id = '.(int)$id_product);
$query->where('od.product_attribute_id = '.(int)$id_product_attribute);
$query->where('TO_DAYS(NOW()) - TO_DAYS(oh.date_add) <= '.(int)$coverage);
$query->where('o.valid = 1');
$query->where('os.logable = 1 AND os.delivery = 1 AND os.shipped = 1');
$quantity = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
return $quantity;
}
}