From 7d88ddef77fb5936ccfa386a29f4fba9bec11e5d Mon Sep 17 00:00:00 2001 From: dSevere Date: Tue, 6 Dec 2011 14:02:50 +0000 Subject: [PATCH] // Update Stats interface relative to stock available functionnality + retrocompatibility for supplier reference and wholesale_price for a product/product attribute --- controllers/admin/AdminProductsController.php | 36 +++++++++++++++---- .../statsbestproducts/statsbestproducts.php | 12 ++++--- modules/statscheckup/statscheckup.php | 18 +++++----- modules/statsproduct/statsproduct.php | 9 ++--- modules/statsstock/statsstock.php | 29 ++++++++------- 5 files changed, 64 insertions(+), 40 deletions(-) diff --git a/controllers/admin/AdminProductsController.php b/controllers/admin/AdminProductsController.php index 2acdb725a..7ee809d1d 100644 --- a/controllers/admin/AdminProductsController.php +++ b/controllers/admin/AdminProductsController.php @@ -2044,6 +2044,8 @@ class AdminProductsControllerCore extends AdminController { if (Validate::isLoadedObject($product = new Product((int)Tools::getValue('id_product')))) { + $update_product = false; + // Get all available suppliers $suppliers = Supplier::getSuppliers(); @@ -2089,6 +2091,13 @@ class AdminProductsControllerCore extends AdminController } } + // Manage defaut supplier for product + if ($new_default_supplier != 0 && $new_default_supplier != $product->id_supplier && Supplier::supplierExists($new_default_supplier)) + { + $product->id_supplier = $new_default_supplier; + $update_product = true; + } + $this->confirmations[] = $this->l('Suppliers of the product have been updated'); // Get all id_product_attribute @@ -2169,17 +2178,32 @@ class AdminProductsControllerCore extends AdminController } } + // Retro-compatibility code if ($product->id_supplier == $supplier->id_supplier) - $product->wholesale_price = Tools::convertPrice($price, $id_currency); + { + if ((int)$attribute['id_product_attribute'] > 0) + { + Db::getInstance()->execute(' + UPDATE '._DB_PREFIX_.'product_attribute + SET supplier_reference = '.$reference.', + wholesale_price = '.Tools::convertPrice($price, $id_currency).' + WHERE id_product = '.(int)$product->id.' + AND id_product_attribute = '.(int)$attribute['id_product_attribute'].' + LIMIT 1 + '); + } + else + { + $product->wholesale_price = Tools::convertPrice($price, $id_currency); //converted in the default currency + $product->supplier_reference = $reference; + $update_product = true; + } + } } } - // Manage defaut supplier for product - if ($new_default_supplier != 0 && $new_default_supplier != $product->id_supplier && Supplier::supplierExists($new_default_supplier)) - { - $product->id_supplier = $new_default_supplier; + if ($update_product) $product->update(); - } $this->confirmations[] = $this->l('Supplier Reference(s) of the product have been updated'); } diff --git a/modules/statsbestproducts/statsbestproducts.php b/modules/statsbestproducts/statsbestproducts.php index f5269f0d8..ce0289fe0 100644 --- a/modules/statsbestproducts/statsbestproducts.php +++ b/modules/statsbestproducts/statsbestproducts.php @@ -1,6 +1,6 @@ 'quantity', - 'header' => $this->l('Stock'), + 'header' => $this->l('Available Quantity for sale'), 'dataIndex' => 'quantity', - 'width' => 50, + 'width' => 150, 'align' => 'right' ) ); @@ -149,8 +149,9 @@ class StatsBestProducts extends ModuleGrid $dateBetween = $this->getDate(); $arrayDateBetween = explode(' AND ', $dateBetween); - $this->_query = 'SELECT SQL_CALC_FOUND_ROWS p.reference, p.id_product, pl.name, ROUND(AVG(od.product_price / o.conversion_rate), 2) as avgPriceSold, - IFNULL((SELECT SUM(pa.quantity) FROM '._DB_PREFIX_.'product_attribute pa WHERE pa.id_product = p.id_product GROUP BY pa.id_product), p.quantity) as quantity, + $this->_query = 'SELECT SQL_CALC_FOUND_ROWS p.reference, p.id_product, pl.name, + ROUND(AVG(od.product_price / o.conversion_rate), 2) as avgPriceSold, + IFNULL(stock.quantity, 0) as quantity, IFNULL(SUM(od.product_quantity), 0) AS totalQuantitySold, ROUND(IFNULL(IFNULL(SUM(od.product_quantity), 0) / (1 + LEAST(TO_DAYS('.$arrayDateBetween[1].'), TO_DAYS(NOW())) - GREATEST(TO_DAYS('.$arrayDateBetween[0].'), TO_DAYS(p.date_add))), 0), 2) as averageQuantitySold, ROUND(IFNULL(SUM((od.product_price * od.product_quantity) / o.conversion_rate), 0), 2) AS totalPriceSold, @@ -168,6 +169,7 @@ class StatsBestProducts extends ModuleGrid LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (p.id_product = pl.id_product AND pl.id_lang = '.(int)$this->getLang().$this->context->shop->addSqlRestrictionOnLang('pl').') LEFT JOIN '._DB_PREFIX_.'order_detail od ON od.product_id = p.id_product LEFT JOIN '._DB_PREFIX_.'orders o ON od.id_order = o.id_order + '.Product::sqlStock('p', 0).' WHERE p.active = 1 AND o.valid = 1 AND o.invoice_date BETWEEN '.$dateBetween.' diff --git a/modules/statscheckup/statscheckup.php b/modules/statscheckup/statscheckup.php index 0144a2171..eb7bf84f6 100644 --- a/modules/statscheckup/statscheckup.php +++ b/modules/statscheckup/statscheckup.php @@ -110,7 +110,7 @@ class StatsCheckUp extends Module 1 => ''.$this->l('average').'', 2 => ''.$this->l('good').'' ); - $tokenProducts = Tools::getAdminToken('AdminCatalog'.(int)(Tab::getIdFromClassName('AdminCatalog')).(int)Context::getContext()->employee->id); + $tokenProducts = Tools::getAdminToken('AdminProducts'.(int)(Tab::getIdFromClassName('AdminProducts')).(int)Context::getContext()->employee->id); $divisor = 4; $totals = array('products' => 0, 'active' => 0, 'images' => 0, 'sales' => 0, 'stock' => 0); foreach ($languages as $language) @@ -138,12 +138,10 @@ class StatsCheckUp extends Module WHERE od.product_id = p.id_product AND o.invoice_date BETWEEN '.ModuleGraph::getDateBetween().' '.$this->sqlShopRestriction(Shop::SHARE_ORDER, 'o').' - ) as nbSales, IFNULL(( - SELECT SUM(pa.quantity) - FROM '._DB_PREFIX_.'product_attribute pa - WHERE pa.id_product = p.id_product - ), p.quantity) as stock + ) as nbSales, + IFNULL(stock.quantity, 0) as stock FROM '._DB_PREFIX_.'product p + '.Product::sqlStock('p', 0).' LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (p.id_product = pl.id_product AND pl.id_lang = '.(int)$this->context->language->id.$this->context->shop->addSqlRestrictionOnLang('pl').') '.$this->context->shop->addSqlAssociation('product', 'p').' @@ -157,7 +155,7 @@ class StatsCheckUp extends Module 'DESCRIPTIONS' => array('name' => $this->l('Descriptions'), 'text' => $this->l('chars (without HTML)')), 'IMAGES' => array('name' => $this->l('Images'), 'text' => $this->l('images')), 'SALES' => array('name' => $this->l('Sales'), 'text' => $this->l('orders / month')), - 'STOCK' => array('name' => $this->l('Stock'), 'text' => $this->l('items')) + 'STOCK' => array('name' => $this->l('Available quantities for sale'), 'text' => $this->l('items')) ); $this->html = ' @@ -213,7 +211,7 @@ class StatsCheckUp extends Module $this->html .= ' '.$this->l('Images').' '.$this->l('Sales').' - '.$this->l('Stock').' + '.$this->l('Available quantities for sale').' '.$this->l('Global').' '; foreach ($result as $row) @@ -246,7 +244,7 @@ class StatsCheckUp extends Module $this->html .= ' '.$row['id_product'].' - '.Tools::substr($row['name'], 0, 42).' + '.Tools::substr($row['name'], 0, 42).' '.$arrayColors[$scores['active']].''; foreach ($languages as $language) if (isset($row['desclength_'.$language['iso_code']])) @@ -286,7 +284,7 @@ class StatsCheckUp extends Module $this->html .= ' '.$this->l('Images').' '.$this->l('Sales').' - '.$this->l('Stock').' + '.$this->l('Available quantities for sale').' '.$this->l('Global').' diff --git a/modules/statsproduct/statsproduct.php b/modules/statsproduct/statsproduct.php index 5f8f0d9f7..6e9733609 100644 --- a/modules/statsproduct/statsproduct.php +++ b/modules/statsproduct/statsproduct.php @@ -99,15 +99,16 @@ class StatsProduct extends ModuleGraph private function getProducts($id_lang) { - $sql = 'SELECT p.`id_product`, p.reference, pl.`name`, IFNULL( - (SELECT SUM(pa.quantity) FROM '._DB_PREFIX_.'product_attribute pa WHERE pa.id_product = p.id_product), p.quantity) as quantity + $sql = 'SELECT p.`id_product`, p.reference, pl.`name`, IFNULL(stock.quantity, 0) as quantity FROM `'._DB_PREFIX_.'product` p + '.Product::sqlStock('p', 0).' LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON p.`id_product` = pl.`id_product`'.$this->context->shop->addSqlRestrictionOnLang('pl').' '.$this->context->shop->addSqlAssociation('product', 'p').' '.(Tools::getValue('id_category') ? 'LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON p.`id_product` = cp.`id_product`' : '').' WHERE pl.`id_lang` = '.(int)$id_lang.' '.(Tools::getValue('id_category') ? 'AND cp.id_category = '.(int)Tools::getValue('id_category') : '').' ORDER BY pl.`name`'; + return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); } @@ -258,7 +259,7 @@ class StatsProduct extends ModuleGraph '.$this->l('Ref.').' '.$this->l('Name').' - '.$this->l('Stock').' + '.$this->l('Available Quantity for sale').' '; @@ -283,7 +284,7 @@ class StatsProduct extends ModuleGraph '.$this->l('After choosing a category and selecting a product, informational graphs will appear. Then, you will be able to analyze them.').' diff --git a/modules/statsstock/statsstock.php b/modules/statsstock/statsstock.php index 2289d4f16..d1b710a5c 100644 --- a/modules/statsstock/statsstock.php +++ b/modules/statsstock/statsstock.php @@ -1,6 +1,6 @@ displayName = $this->l('Stock stats'); + $this->displayName = $this->l('Available quantities stats'); $this->description = ''; } @@ -66,25 +66,24 @@ class StatsStock extends Module FROM '._DB_PREFIX_.'product_attribute pa WHERE p.id_product = pa.id_product AND wholesale_price != 0 ), p.wholesale_price) as wholesale_price, - IFNULL(( - SELECT SUM(pa.quantity) - FROM '._DB_PREFIX_.'product_attribute pa WHERE p.id_product = pa.id_product - ), p.quantity) as quantity, - IFNULL(( - SELECT SUM(IF(pa.wholesale_price > 0, pa.wholesale_price, p.wholesale_price) * pa.quantity) - FROM '._DB_PREFIX_.'product_attribute pa WHERE p.id_product = pa.id_product - ), p.wholesale_price * p.quantity) as stockvalue + IFNULL(stock.quantity, 0) as quantity FROM '._DB_PREFIX_.'product p '.$this->context->shop->addSqlAssociation('product', 'p').' INNER JOIN '._DB_PREFIX_.'product_lang pl ON (p.id_product = pl.id_product AND pl.id_lang = '.(int)$this->context->language->id.$this->context->shop->addSqlRestrictionOnLang('pl').') + '.Product::sqlStock('p', 0).' WHERE 1 = 1 '.$filter; $products = Db::getInstance()->executeS($sql); + foreach ($products as $key => $p) + { + $products[$key]['stockvalue'] = $p['wholesale_price'] * $p['quantity']; + } + $this->html .= ' -

'.$this->l('Stock value').'

+

'.$this->l('Available quantities for sale valuation').'

'.$this->l('Category').' :