purchase_order_product_recommendation_supermarket: make prevous period data optional

This commit is contained in:
Luis 2025-10-06 12:10:46 +02:00
parent b153fdc7af
commit 3a35955d24
2 changed files with 53 additions and 32 deletions

View file

@ -20,6 +20,11 @@ class PurchaseOrderRecommendationSupermarketWizard(models.TransientModel):
default=False, default=False,
help="If enabled, days when the product stock was 0 or less will not be considered in the daily sales calculation.", 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( total_days = fields.Integer(
string="Total days", string="Total days",
compute="_compute_total_days", compute="_compute_total_days",
@ -193,36 +198,37 @@ class PurchaseOrderRecommendationSupermarketWizard(models.TransientModel):
if len(found_scrapped): if len(found_scrapped):
res["units_scrapped"] = found_scrapped[0]["qty_done"] res["units_scrapped"] = found_scrapped[0]["qty_done"]
days = self._get_total_days() if self.include_previous_period:
prev_date_end = self.date_begin - timedelta(days=1) days = self._get_total_days()
prev_date_begin = self.date_begin - timedelta(days=days) prev_date_end = self.date_begin - timedelta(days=1)
domain = self.with_context( 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, "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( )._get_move_line_domain(product_id, src="internal", dst="customer")
domain, ["product_id", "qty_done"], ["product_id"] 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 len(found_previous_period):
if self.ignore_zero_stock_days: res["units_delivered_prev"] = found_previous_period[0]["qty_done"]
days_with_stock = days - self.with_context( 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, "period_date_begin": prev_date_begin,
} "period_date_end": prev_date_end,
)._get_days_out_of_stock(product_id) }
res["units_avg_delivered_prev"] = ( )._get_days_out_of_stock(product_id)
res["units_delivered_prev"] / days_with_stock res["units_avg_delivered_prev"] = (
if days_with_stock != 0 res["units_delivered_prev"] / days_with_stock
else 1 if days_with_stock != 0
) else 1
else: )
res["units_avg_delivered_prev"] = ( else:
found_previous_period[0]["qty_done"] / days if days != 0 else 1 res["units_avg_delivered_prev"] = (
) found_previous_period[0]["qty_done"] / days if days != 0 else 1
)
seller = product_id._select_seller( seller = product_id._select_seller(
partner_id=self.order_id.partner_id, partner_id=self.order_id.partner_id,
date=fields.Date.today(), date=fields.Date.today(),
@ -273,6 +279,10 @@ class PurchaseOrderRecommendationSupermarketWizard(models.TransientModel):
class PurchaseOrderRecommendationLine(models.TransientModel): class PurchaseOrderRecommendationLine(models.TransientModel):
_inherit = "purchase.order.recommendation.line" _inherit = "purchase.order.recommendation.line"
include_previous_period = fields.Boolean(
related="wizard_id.include_previous_period", store=False, readonly=True
)
packaging_id = fields.Many2one( packaging_id = fields.Many2one(
comodel_name="product.packaging", comodel_name="product.packaging",
string="Packaging", string="Packaging",

View file

@ -25,6 +25,7 @@
<field name="show_all_products" position="after"> <field name="show_all_products" position="after">
<field name="order_by_packages" /> <field name="order_by_packages" />
<field name="ignore_zero_stock_days" /> <field name="ignore_zero_stock_days" />
<field name="include_previous_period" />
<field name="last_order_total_amount" widget="monetary" options="{'currency_field': 'currency_id'}" /> <field name="last_order_total_amount" widget="monetary" options="{'currency_field': 'currency_id'}" />
<field name="order_total_amount" widget="monetary" options="{'currency_field': 'currency_id'}" /> <field name="order_total_amount" widget="monetary" options="{'currency_field': 'currency_id'}" />
<field name="currency_id" invisible="1"/> <field name="currency_id" invisible="1"/>
@ -32,6 +33,15 @@
<field name="product_id" position="attributes"> <field name="product_id" position="attributes">
<attribute name="optional">hide</attribute> <attribute name="optional">hide</attribute>
</field> </field>
<field name="price_unit" position="attributes">
<attribute name="optional">show</attribute>
</field>
<field name="units_available" position="attributes">
<attribute name="optional">show</attribute>
</field>
<field name="units_avg_delivered" position="attributes">
<attribute name="optional">show</attribute>
</field>
<field name="product_name" position="attributes"> <field name="product_name" position="attributes">
<attribute name="optional">show</attribute> <attribute name="optional">show</attribute>
</field> </field>
@ -39,8 +49,9 @@
<field name="units_scrapped" string="Qty scrapped" optional="hide" /> <field name="units_scrapped" string="Qty scrapped" optional="hide" />
<field name="stock_duration" string="Stock Duration" optional="hide" /> <field name="stock_duration" string="Stock Duration" optional="hide" />
<field name="days_without_stock" optional="hide" /> <field name="days_without_stock" optional="hide" />
<field name="units_delivered_prev" string="Prev Period" optional="hide" /> <field name="include_previous_period" invisible="1"/>
<field name="units_avg_delivered_prev" string="Avg Prev Period" optional="hide" /> <field name="units_delivered_prev" string="Prev Period" optional="hide" attrs="{'invisible': [('include_previous_period','=',False)]}" />
<field name="units_avg_delivered_prev" string="Avg Prev Period" optional="hide" attrs="{'invisible': [('include_previous_period','=',False)]}" />
<field name="packaging_id" optional="show" /> <field name="packaging_id" optional="show" />
<field name="packaging_contained_qty" string="Packaging Contained Qty" optional="hide" /> <field name="packaging_contained_qty" string="Packaging Contained Qty" optional="hide" />
<field name="subtotal_amount" string="Subtotal Amount" optional="hide" /> <field name="subtotal_amount" string="Subtotal Amount" optional="hide" />