Skip to content

Commit

Permalink
Make id_shop optional and working in shop group or all shops context
Browse files Browse the repository at this point in the history
  • Loading branch information
lmeyer1 committed Oct 26, 2022
1 parent 78b99fa commit bc1677f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
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'], $this->context->shop->id, (int) $product['cart_quantity']);
ProductSale::addProductSale((int) $product['id_product'], (int) $product['cart_quantity'], $order->id_shop);
}
}

Expand Down
58 changes: 40 additions & 18 deletions classes/ProductSale.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,18 +234,27 @@ 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
* @param int $idShop Shop ID
*
* @return bool Indicates whether the sale was successfully added
*/
public static function addProductSale($productId, $idShop, $qty = 1)
public static function addProductSale($productId, $qty = 1, $idShop = null)
{
return Db::getInstance()->execute('
if (is_null($idShop)) {
$idShops = Shop::getContextListShopID();
} else {
$idShops = [$idShop];
}
$success = true;
foreach ($idShops as $idShop) {
$success = $success && Db::getInstance()->execute('
INSERT INTO ' . _DB_PREFIX_ . 'product_sale
(`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()');
}
return $success;
}

/**
Expand All @@ -256,9 +265,14 @@ public static function addProductSale($productId, $idShop, $qty = 1)
*
* @return int Number of sales for the given Product
*/
public static function getNbrSales($idProduct, $idShop)
public static function getNbrSales($idProduct, $idShop = null)
{
$result = Db::getInstance()->getRow('SELECT `sale_nbr` FROM ' . _DB_PREFIX_ . 'product_sale WHERE `id_product` = ' . (int) $idProduct . ' and `id_shop` = ' . (int) $idShop);
if (is_null($idShop)) {
$idShops = Shop::getContextListShopID();
} else {
$idShops = [$idShop];
}
$result = Db::getInstance()->getRow('SELECT `sale_nbr` FROM ' . _DB_PREFIX_ . 'product_sale WHERE `id_product` = ' . (int) $idProduct . ' and `id_shop` in (' . implode(', ', $idShops) . ')');
if (empty($result) || !array_key_exists('sale_nbr', $result)) {
return -1;
}
Expand All @@ -270,25 +284,33 @@ public static function getNbrSales($idProduct, $idShop)
* Remove a Product sale.
*
* @param int $idProduct Product ID
* @param int $idShop Shop ID
* @param int $qty Quantity
* @param int $idShop Shop ID
*
* @return bool Indicates whether the product sale has been successfully removed
*/
public static function removeProductSale($idProduct, $idShop, $qty = 1)
public static function removeProductSale($idProduct, $qty = 1, $idShop = null)
{
$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 . ' and `id_shop` = ' . (int) $idShop
);
} elseif ($totalSales == 1) {
return Db::getInstance()->delete('product_sale', 'id_product = ' . (int) $idProduct . ' and `id_shop` = ' . (int) $idShop);
if (is_null($idShop)) {
$idShops = Shop::getContextListShopID();
} else {
$idShops = [$idShop];
}
$success = true;
foreach ($idShops as $idShop) {
$totalSales = ProductSale::getNbrSales($idProduct, $idShop);
if ($totalSales > 1) {
$success = $success && 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 . ' and `id_shop` = ' . (int) $idShop
);
} elseif ($totalSales == 1) {
$success = $success && Db::getInstance()->delete('product_sale', 'id_product = ' . (int) $idProduct . ' and `id_shop` = ' . (int) $idShop);
}
}

return true;
return $success;
}
}
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'], (int) $order->id_shop, $product['product_quantity']);
ProductSale::addProductSale($product['product_id'], $product['product_quantity'], (int) $order->id_shop);
// @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'], (int) $order->id_shop, $product['product_quantity']);
ProductSale::removeProductSale($product['product_id'], $product['product_quantity'], (int) $order->id_shop);

// @since 1.5.0 - Stock Management
if (!Pack::isPack($product['product_id']) &&
Expand Down

0 comments on commit bc1677f

Please sign in to comment.