From 351e00900b08ec56d7ca0ccd313d1930b808c04d Mon Sep 17 00:00:00 2001 From: therampagerado Date: Mon, 1 Sep 2025 14:56:36 +0300 Subject: [PATCH] fix: calculate weighted inventory prices --- stats/statsstock.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/stats/statsstock.php b/stats/statsstock.php index 4eeef37..37fe51a 100644 --- a/stats/statsstock.php +++ b/stats/statsstock.php @@ -88,11 +88,14 @@ public function hookAdminStatsModules() $sql = 'SELECT p.id_product, p.reference, pl.name, p.cache_is_pack, IFNULL(( - SELECT AVG(product_attribute_shop.wholesale_price) + SELECT SUM(stock2.quantity * IF(product_attribute_shop.wholesale_price != 0, product_attribute_shop.wholesale_price, ps_shop.wholesale_price)) / SUM(stock2.quantity) FROM ' . _DB_PREFIX_ . 'product_attribute pa ' . Shop::addSqlAssociation('product_attribute', 'pa') . ' - WHERE p.id_product = pa.id_product - AND product_attribute_shop.wholesale_price != 0 + INNER JOIN ' . _DB_PREFIX_ . 'product ps ON (ps.id_product = pa.id_product) + ' . Shop::addSqlAssociation('product', 'ps') . ' + LEFT JOIN ' . _DB_PREFIX_ . 'stock_available stock2 + ON (stock2.id_product = pa.id_product AND stock2.id_product_attribute = pa.id_product_attribute AND stock2.id_shop = product_attribute_shop.id_shop AND stock2.id_shop_group = product_attribute_shop.id_shop_group) + WHERE p.id_product = pa.id_product AND stock2.quantity IS NOT NULL ), product_shop.wholesale_price) as wholesale_price, IFNULL(stock.quantity, 0) as quantity FROM ' . _DB_PREFIX_ . 'product p @@ -142,7 +145,7 @@ public function hookAdminStatsModules() if (!count($products)) { $this->html .= '

' . $this->l('No product matches criteria.') . '

'; } else { - $rollup = ['quantity' => 0, 'wholesale_price' => 0, 'stockvalue' => 0]; + $rollup = ['quantity' => 0, 'stockvalue' => 0]; $this->html .= ' @@ -159,7 +162,6 @@ public function hookAdminStatsModules() foreach ($products as $product) { if (!$product['cache_is_pack']) { $rollup['quantity'] += $product['quantity']; - $rollup['wholesale_price'] += $product['wholesale_price']; $rollup['stockvalue'] += $product['stockvalue']; } $this->html .= ' @@ -183,14 +185,12 @@ public function hookAdminStatsModules() - +
' . $rollup['quantity'] . '' . Tools::displayPrice($rollup['wholesale_price'] / max(1, count(array_filter($products, function ($p) { - return !$p['cache_is_pack']; - }))), $currency) . '' . Tools::displayPrice($rollup['stockvalue'] / max(1, $rollup['quantity']), $currency) . ' ' . Tools::displayPrice($rollup['stockvalue'], $currency) . '
- ' . $this->l('This section corresponds to the default wholesale price according to the default supplier for the product. An average price is used when the product has attributes.') . '
+ ' . $this->l('This section corresponds to the default wholesale price according to the default supplier for the product. A weighted average price is used when the product has attributes.') . '
' . $this->l('Pack products are marked with "-" and are NOT included in the total quantities or value calculation as they are already accounted for as separate items.') . ' '; }