From 3a35955d24a5c71c841d64295d5e4bb648a25022 Mon Sep 17 00:00:00 2001 From: luis Date: Mon, 6 Oct 2025 12:10:46 +0200 Subject: [PATCH] purchase_order_product_recommendation_supermarket: make prevous period data optional --- .../wizards/purchase_order_recommendation.py | 70 +++++++++++-------- .../wizards/purchase_order_recommendation.xml | 15 +++- 2 files changed, 53 insertions(+), 32 deletions(-) diff --git a/purchase_order_product_recommendation_supermarket/wizards/purchase_order_recommendation.py b/purchase_order_product_recommendation_supermarket/wizards/purchase_order_recommendation.py index 95355aa..6731a91 100644 --- a/purchase_order_product_recommendation_supermarket/wizards/purchase_order_recommendation.py +++ b/purchase_order_product_recommendation_supermarket/wizards/purchase_order_recommendation.py @@ -20,6 +20,11 @@ class PurchaseOrderRecommendationSupermarketWizard(models.TransientModel): default=False, help="If enabled, days when the product stock was 0 or less will not be considered in the daily sales calculation.", ) + include_previous_period = fields.Boolean( + string="Include previous period stats", + default=True, + help="If disabled, the previous period (same number of days immediately preceding) is NOT queried. This saves a read_group on stock move lines and skips zero-stock day analysis for that window.", + ) total_days = fields.Integer( string="Total days", compute="_compute_total_days", @@ -193,36 +198,37 @@ class PurchaseOrderRecommendationSupermarketWizard(models.TransientModel): if len(found_scrapped): res["units_scrapped"] = found_scrapped[0]["qty_done"] - days = self._get_total_days() - prev_date_end = self.date_begin - timedelta(days=1) - prev_date_begin = self.date_begin - timedelta(days=days) - domain = self.with_context( - { - "period_date_begin": prev_date_begin, - "period_date_end": prev_date_end, - } - )._get_move_line_domain(product_id, src="internal", dst="customer") - found_previous_period = self.env["stock.move.line"].read_group( - domain, ["product_id", "qty_done"], ["product_id"] - ) - if len(found_previous_period): - res["units_delivered_prev"] = found_previous_period[0]["qty_done"] - if self.ignore_zero_stock_days: - days_with_stock = days - self.with_context( - { - "period_date_begin": prev_date_begin, - "period_date_end": prev_date_end, - } - )._get_days_out_of_stock(product_id) - res["units_avg_delivered_prev"] = ( - res["units_delivered_prev"] / days_with_stock - if days_with_stock != 0 - else 1 - ) - else: - res["units_avg_delivered_prev"] = ( - found_previous_period[0]["qty_done"] / days if days != 0 else 1 - ) + if self.include_previous_period: + days = self._get_total_days() + prev_date_end = self.date_begin - timedelta(days=1) + prev_date_begin = self.date_begin - timedelta(days=days) + domain = self.with_context( + { + "period_date_begin": prev_date_begin, + "period_date_end": prev_date_end, + } + )._get_move_line_domain(product_id, src="internal", dst="customer") + found_previous_period = self.env["stock.move.line"].read_group( + domain, ["product_id", "qty_done"], ["product_id"] + ) + if len(found_previous_period): + res["units_delivered_prev"] = found_previous_period[0]["qty_done"] + if self.ignore_zero_stock_days: + days_with_stock = days - self.with_context( + { + "period_date_begin": prev_date_begin, + "period_date_end": prev_date_end, + } + )._get_days_out_of_stock(product_id) + res["units_avg_delivered_prev"] = ( + res["units_delivered_prev"] / days_with_stock + if days_with_stock != 0 + else 1 + ) + else: + res["units_avg_delivered_prev"] = ( + found_previous_period[0]["qty_done"] / days if days != 0 else 1 + ) seller = product_id._select_seller( partner_id=self.order_id.partner_id, date=fields.Date.today(), @@ -273,6 +279,10 @@ class PurchaseOrderRecommendationSupermarketWizard(models.TransientModel): class PurchaseOrderRecommendationLine(models.TransientModel): _inherit = "purchase.order.recommendation.line" + include_previous_period = fields.Boolean( + related="wizard_id.include_previous_period", store=False, readonly=True + ) + packaging_id = fields.Many2one( comodel_name="product.packaging", string="Packaging", diff --git a/purchase_order_product_recommendation_supermarket/wizards/purchase_order_recommendation.xml b/purchase_order_product_recommendation_supermarket/wizards/purchase_order_recommendation.xml index 27d67ed..f62749b 100644 --- a/purchase_order_product_recommendation_supermarket/wizards/purchase_order_recommendation.xml +++ b/purchase_order_product_recommendation_supermarket/wizards/purchase_order_recommendation.xml @@ -25,6 +25,7 @@ + @@ -32,6 +33,15 @@ hide + + show + + + show + + + show + show @@ -39,8 +49,9 @@ - - + + +