Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a column id_shop in product_sale #29547

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion classes/PaymentModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ public function validateOrder(

foreach ($this->context->cart->getProducts() as $product) {
if ($order_status->logable) {
ProductSale::addProductSale((int) $product['id_product'], (int) $product['cart_quantity']);
ProductSale::addProductSale((int) $product['id_product'], $this->context->shop->id, (int) $product['cart_quantity']);
lmeyer1 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
39 changes: 21 additions & 18 deletions classes/ProductSale.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class ProductSaleCore
public static function fillProductSales()
{
$sql = 'REPLACE INTO ' . _DB_PREFIX_ . 'product_sale
(`id_product`, `quantity`, `sale_nbr`, `date_upd`)
SELECT od.product_id, SUM(od.product_quantity), COUNT(od.product_id), NOW()
FROM ' . _DB_PREFIX_ . 'order_detail od GROUP BY od.product_id';
(`id_product`, `id_shop`,`quantity`, `sale_nbr`, `date_upd`)
SELECT od.product_id, od.id_shop, SUM(od.product_quantity), COUNT(od.product_id), NOW()
FROM ' . _DB_PREFIX_ . 'order_detail od GROUP BY od.product_id, od.id_shop';

return Db::getInstance()->execute($sql);
}
Expand All @@ -53,8 +53,8 @@ public static function getNbSales()
{
$sql = 'SELECT COUNT(ps.`id_product`) AS nb
FROM `' . _DB_PREFIX_ . 'product_sale` ps
LEFT JOIN `' . _DB_PREFIX_ . 'product` p ON p.`id_product` = ps.`id_product`
' . Shop::addSqlAssociation('product', 'p', false) . '
INNER JOIN `' . _DB_PREFIX_ . 'product` p ON p.`id_product` = ps.`id_product`
' . Shop::addSqlAssociation('product', 'p') . ' and product_shop.id_shop = ps.id_shop
WHERE product_shop.`active` = 1';

return (int) Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
Expand Down Expand Up @@ -111,8 +111,8 @@ public static function getBestSales($idLang, $pageNumber = 0, $nbProducts = 10,
DATEDIFF(p.`date_add`, DATE_SUB("' . date('Y-m-d') . ' 00:00:00",
INTERVAL ' . (int) $interval . ' DAY)) > 0 AS new'
. ' FROM `' . _DB_PREFIX_ . 'product_sale` ps
LEFT JOIN `' . _DB_PREFIX_ . 'product` p ON ps.`id_product` = p.`id_product`
' . Shop::addSqlAssociation('product', 'p', false);
INNER JOIN `' . _DB_PREFIX_ . 'product` p ON ps.`id_product` = p.`id_product`
' . Shop::addSqlAssociation('product', 'p') . ' and product_shop.id_shop = ps.id_shop';
if (Combination::isFeatureActive()) {
$sql .= ' LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute_shop` product_attribute_shop
ON (p.`id_product` = product_attribute_shop.`id_product` AND product_attribute_shop.`default_on` = 1 AND product_attribute_shop.id_shop=' . (int) $context->shop->id . ')';
Expand Down Expand Up @@ -193,8 +193,8 @@ public static function getBestSalesLight($idLang, $pageNumber = 0, $nbProducts =
product_shop.`date_add` > "' . date('Y-m-d', strtotime('-' . (Configuration::get('PS_NB_DAYS_NEW_PRODUCT') ? (int) Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20) . ' DAY')) . '" as new,
product_shop.`on_sale`, product_attribute_shop.minimal_quantity AS product_attribute_minimal_quantity
FROM `' . _DB_PREFIX_ . 'product_sale` ps
LEFT JOIN `' . _DB_PREFIX_ . 'product` p ON ps.`id_product` = p.`id_product`
' . Shop::addSqlAssociation('product', 'p') . '
INNER JOIN `' . _DB_PREFIX_ . 'product` p ON ps.`id_product` = p.`id_product`
' . Shop::addSqlAssociation('product', 'p') . ' and product_shop.id_shop = ps.id_shop
LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute_shop` product_attribute_shop
ON (p.`id_product` = product_attribute_shop.`id_product` AND product_attribute_shop.`default_on` = 1 AND product_attribute_shop.id_shop=' . (int) $context->shop->id . ')
LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute` pa ON (product_attribute_shop.id_product_attribute=pa.id_product_attribute)
Expand Down Expand Up @@ -234,29 +234,31 @@ public static function getBestSalesLight($idLang, $pageNumber = 0, $nbProducts =
* Add Product sale.
*
* @param int $productId Product ID
* @param int $idShop Shop ID
* @param int $qty Quantity
*
* @return bool Indicates whether the sale was successfully added
*/
public static function addProductSale($productId, $qty = 1)
public static function addProductSale($productId, $idShop, $qty = 1)
lmeyer1 marked this conversation as resolved.
Show resolved Hide resolved
{
return Db::getInstance()->execute('
INSERT INTO ' . _DB_PREFIX_ . 'product_sale
(`id_product`, `quantity`, `sale_nbr`, `date_upd`)
VALUES (' . (int) $productId . ', ' . (int) $qty . ', 1, NOW())
(`id_product`, `id_shop`, `quantity`, `sale_nbr`, `date_upd`)
VALUES (' . (int) $productId . ', ' . (int) $idShop . ', ' . (int) $qty . ', 1, NOW())
ON DUPLICATE KEY UPDATE `quantity` = `quantity` + ' . (int) $qty . ', `sale_nbr` = `sale_nbr` + 1, `date_upd` = NOW()');
}

/**
* Get number of sales.
*
* @param int $idProduct Product ID
* @param int $idShop Shop ID
*
* @return int Number of sales for the given Product
*/
public static function getNbrSales($idProduct)
public static function getNbrSales($idProduct, $idShop)
lmeyer1 marked this conversation as resolved.
Show resolved Hide resolved
{
$result = Db::getInstance()->getRow('SELECT `sale_nbr` FROM ' . _DB_PREFIX_ . 'product_sale WHERE `id_product` = ' . (int) $idProduct);
$result = Db::getInstance()->getRow('SELECT `sale_nbr` FROM ' . _DB_PREFIX_ . 'product_sale WHERE `id_product` = ' . (int) $idProduct . ' and `id_shop` = ' . (int) $idShop);
if (empty($result) || !array_key_exists('sale_nbr', $result)) {
return -1;
}
Expand All @@ -268,22 +270,23 @@ public static function getNbrSales($idProduct)
* Remove a Product sale.
*
* @param int $idProduct Product ID
* @param int $idShop Shop ID
* @param int $qty Quantity
*
* @return bool Indicates whether the product sale has been successfully removed
*/
public static function removeProductSale($idProduct, $qty = 1)
public static function removeProductSale($idProduct, $idShop, $qty = 1)
{
$totalSales = ProductSale::getNbrSales($idProduct);
$totalSales = ProductSale::getNbrSales($idProduct, $idShop);
if ($totalSales > 1) {
return Db::getInstance()->execute(
'
UPDATE ' . _DB_PREFIX_ . 'product_sale
SET `quantity` = CAST(`quantity` AS SIGNED) - ' . (int) $qty . ', `sale_nbr` = CAST(`sale_nbr` AS SIGNED) - 1, `date_upd` = NOW()
WHERE `id_product` = ' . (int) $idProduct
WHERE `id_product` = ' . (int) $idProduct . ' and `id_shop` = ' . (int) $idShop
);
} elseif ($totalSales == 1) {
return Db::getInstance()->delete('product_sale', 'id_product = ' . (int) $idProduct);
return Db::getInstance()->delete('product_sale', 'id_product = ' . (int) $idProduct . ' and `id_shop` = ' . (int) $idShop);
}

return true;
Expand Down
4 changes: 2 additions & 2 deletions classes/order/OrderHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function changeIdOrderState($new_order_state, $id_order, $use_existing_pa
if (Validate::isLoadedObject($old_os)) {
// if becoming logable => adds sale
if ($new_os->logable && !$old_os->logable) {
ProductSale::addProductSale($product['product_id'], $product['product_quantity']);
ProductSale::addProductSale($product['product_id'], (int) $order->id_shop, $product['product_quantity']);
// @since 1.5.0 - Stock Management
if (!Pack::isPack($product['product_id']) &&
in_array($old_os->id, $error_or_canceled_statuses) &&
Expand All @@ -230,7 +230,7 @@ public function changeIdOrderState($new_order_state, $id_order, $use_existing_pa
}
} elseif (!$new_os->logable && $old_os->logable) {
// if becoming unlogable => removes sale
ProductSale::removeProductSale($product['product_id'], $product['product_quantity']);
ProductSale::removeProductSale($product['product_id'], (int) $order->id_shop, $product['product_quantity']);

// @since 1.5.0 - Stock Management
if (!Pack::isPack($product['product_id']) &&
Expand Down
3 changes: 2 additions & 1 deletion install-dev/data/db_structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1838,10 +1838,11 @@ CREATE TABLE `PREFIX_product_lang` (
/* info about number of products sold */
CREATE TABLE `PREFIX_product_sale` (
`id_product` int(10) unsigned NOT NULL,
`id_shop` int(10) unsigned NOT NULL,
`quantity` int(10) unsigned NOT NULL DEFAULT '0',
`sale_nbr` int(10) unsigned NOT NULL DEFAULT '0',
`date_upd` date DEFAULT NULL,
PRIMARY KEY (`id_product`),
PRIMARY KEY (`id_product`, `id_shop`),
KEY `quantity` (`quantity`)
) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8mb4 COLLATION;

Expand Down