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

Conversation

lmeyer1
Copy link
Contributor

@lmeyer1 lmeyer1 commented Sep 6, 2022

Questions Answers
Branch? develop
Description? Distinct best sales for each shop. Add column id_shop to table PREFIX_product_sale and adapt all queries
Type? bug fix
Category? CO
BC breaks? no
Deprecations? no
Fixed ticket? Fixes #29538.
Related PRs A PR in autoupdate module will follow.
How to test? The list of best sales is now distinct in each shop and reflects the orders passed in the current shop.
Possible impacts? Three functions in class ProductSaleCore change their signature.

These queries must be run in order to test this PR:

ALTER TABLE `ps_product_sale` ADD `id_shop` INT(10) UNSIGNED NOT NULL AFTER `id_product`;
ALTER TABLE `ps_product_sale` DROP PRIMARY KEY, ADD PRIMARY KEY (`id_product`, `id_shop`) USING BTREE;
TRUNCATE TABLE `ps_product_sale`;
INSERT INTO ps_product_sale (`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 ps_order_detail od GROUP BY od.product_id, od.id_shop;

@lmeyer1 lmeyer1 requested a review from a team as a code owner September 6, 2022 12:42
@prestonBot prestonBot added develop Branch Bug fix Type: Bug fix labels Sep 6, 2022
@@ -240,10 +240,11 @@ public static function getBestSalesLight($idLang, $pageNumber = 0, $nbProducts =
*/
public static function addProductSale($productId, $qty = 1)
{
$idShop = Context::getContext()->shop->id;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this code is only used in FO then only handling a single shop case would suffice But it maybe be also used in BO where the context can also be: All shops, and Shop groups

So those use cases should be anticipated, so you should use Shop::getContextListShopID() with the list of affected IDs and use this in a IN condition instead of a strict equality on one shop only.

This applies also for the read queries you modified, although be aware that when Shop::addSqlAssociation is used this is already partially done already.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I will look into this. Thanks

Copy link
Contributor Author

@lmeyer1 lmeyer1 Sep 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • In getNbSales(), Shop::addSqlAssociation will handle the problem.
  • If addProductSale() is called, there must always be added only one line for the shop the order belongs to. Shop::getContextListShopID() will not help, if someone is modifying an order while being in all-shop-context. We must add an argument id_shop.
  • For removeProductSale() it is the same case.
  • getNbrSales() is used only in ProductSaleCore and must also take id_shop as an argument

classes/ProductSale.php Outdated Show resolved Hide resolved
Copy link
Contributor

@kpodemski kpodemski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid there are BC Breaks.

If you want to avoid it, you need to:

  • add new arguments at the end of the methods
  • don't make them required, get shop from the Context::getContext() if the shop is not provided

@lmeyer1
Copy link
Contributor Author

lmeyer1 commented Sep 20, 2022

@kpodemski

I'm afraid there are BC Breaks

You are right, I didn't think about it before.

I think add new arguments at the end of the methods and don't make them required will not be enough to avoid BC break.
If the method is used from the BO, Context::getContext()->shop->id could return the wrong id_shop, if we are in All shop context or a shop group.

Thus, it does not seem possible to me to keep consistency AND to avoid a BC break.

classes/ProductSale.php Outdated Show resolved Hide resolved
classes/ProductSale.php Outdated Show resolved Hide resolved
classes/PaymentModule.php Outdated Show resolved Hide resolved
@jolelievre
Copy link
Contributor

Hi @lmeyer1

Thus, it does not seem possible to me to keep consistency AND to avoid a BC break.

I think it's possible, only when your $shopId optional parameter is empty you need to get the contextual list via Shop::getContextListShopID() then you can loop through this list and insert what you need.
When a $shopId is specified your list is simply [$shopId] this way the code is simpler because it's always based on a list.

For getNbrSales it's the same principle but you'll have to use an IN (shopIds) condition.

*
* @return bool Indicates whether the sale was successfully added
*/
public static function addProductSale($productId, $qty = 1)
public static function addProductSale($productId, $qty = 1, $idShop = null)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jolelievre, @kpodemski
I now changed like suggested.

But for these two functions addProductSale and removeProductSale working in shop group or all shop context is hurting the statistics. In both cases, we either add or remove one sale for each shop. This is duplicating sales or removing them duplicated.

We could argue, that it would be better do nothing, if $isShop is null, because in this case we can assess exactly the error: one sale has not be added or removed. When adding or removing on an array of shops, this array could be big, and the error introduced would be big too. (Suppose we have 20 shops, we would save 20 sales instead of 1).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lmeyer1, you are right. Unfortunately, I don't see any way to keep "BC Break-free" different way

We could:

  • introduce a new method addProductSaleForShop
  • deprecate or leave the old one
  • change the use of addProductSale for addProductSaleForShop
  • but still, there will be a problem with those who run addProductSale because id_shop will be NULL, and we won't get any information about it in methods getting sales

Maybe we should wait for PS 9.0 and change it then?

@FabienPapet FabienPapet added this to the 9.0.0 milestone Jan 16, 2023
@kpodemski kpodemski closed this May 12, 2023
@kpodemski kpodemski reopened this May 12, 2023
@kpodemski kpodemski requested a review from a team as a code owner May 12, 2023 15:21
@Progi1984 Progi1984 closed this Apr 18, 2024
@Progi1984 Progi1984 reopened this Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug fix Type: Bug fix develop Branch
Projects
Status: Ready for review
Development

Successfully merging this pull request may close these issues.

Best sales in multishop environment are not distinctive to shop
7 participants