From a3b606d139f91344fd08f28f585b07986f43779e Mon Sep 17 00:00:00 2001 From: sergio-teruel Date: Fri, 24 Jan 2025 22:26:10 +0100 Subject: [PATCH] [FIX] product_contract: Compute date_start and date_end correctly. Make fields computed --- product_contract/README.rst | 1 + product_contract/models/sale_order_line.py | 96 ++++++++++++------- product_contract/readme/CONTRIBUTORS.md | 1 + .../static/description/index.html | 1 + .../wizards/product_contract_configurator.py | 4 +- 5 files changed, 65 insertions(+), 38 deletions(-) diff --git a/product_contract/README.rst b/product_contract/README.rst index e9f75bda91..920f62ac35 100644 --- a/product_contract/README.rst +++ b/product_contract/README.rst @@ -87,6 +87,7 @@ Contributors - Ernesto Tejeda - Pedro M. Baeza - Carlos Roca + - Sergio Teruel - David Jaen diff --git a/product_contract/models/sale_order_line.py b/product_contract/models/sale_order_line.py index fa86047c89..d28705920f 100644 --- a/product_contract/models/sale_order_line.py +++ b/product_contract/models/sale_order_line.py @@ -33,19 +33,16 @@ class SaleOrderLine(models.Model): recurring_invoicing_type = fields.Selection( related="product_id.recurring_invoicing_type" ) - date_start = fields.Date() - date_end = fields.Date() - + date_start = fields.Date(compute="_compute_date_start", readonly=False, store=True) + date_end = fields.Date(compute="_compute_date_end", readonly=False, store=True) contract_line_id = fields.Many2one( comodel_name="contract.line", string="Contract Line to replace", - required=False, copy=False, ) is_auto_renew = fields.Boolean( string="Auto Renew", compute="_compute_auto_renew", - default=False, store=True, readonly=False, ) @@ -93,6 +90,60 @@ def _compute_contract_template_id(self): rec.order_id.company_id ).property_contract_template_id + @api.depends("product_id", "order_id.date_order") + def _compute_date_start(self): + for sol in self: + if sol.contract_start_date_method == "start_this": + sol.date_start = sol.order_id.date_order.replace(day=1) + elif sol.contract_start_date_method == "end_this": + sol.date_start = ( + sol.order_id.date_order + + self.get_relative_delta( + sol.recurring_rule_type, sol.product_id.default_qty + ) + ).replace(day=1) - relativedelta(days=1) + elif sol.contract_start_date_method == "start_next": + # Dia 1 del siguiente recurring_rule_type + sol.date_start = ( + sol.order_id.date_order + + self.get_relative_delta( + sol.recurring_rule_type, sol.product_id.default_qty + ) + ).replace(day=1) + elif sol.contract_start_date_method == "end_next": + # Last day of next recurring period + sol.date_start = ( + sol.order_id.date_order + + self.get_relative_delta( + sol.recurring_rule_type, sol.product_id.default_qty + 1 + ) + ).replace(day=1) - relativedelta(days=1) + else: + # Manual method + sol.date_start = False + + @api.depends( + "order_id.date_order", + "is_auto_renew", + "date_start", + "auto_renew_interval", + "auto_renew_rule_type", + ) + def _compute_date_end(self): + for sol in self: + if sol.is_auto_renew and sol.date_start: + sol.date_end = self.env["contract.line"]._get_first_date_end( + sol.date_start, sol.auto_renew_rule_type, sol.auto_renew_interval + ) + else: + sol.date_end = False + + @api.model + def get_relative_delta(self, recurring_rule_type, interval): + return self.env["contract.recurrency.mixin"].get_relative_delta( + recurring_rule_type, interval + ) + def _get_auto_renew_rule_type(self): """monthly last day don't make sense for auto_renew_rule_type""" self.ensure_one() @@ -100,40 +151,13 @@ def _get_auto_renew_rule_type(self): return "monthly" return self.recurring_rule_type - def _get_date_end(self): - self.ensure_one() - contract_start_date_method = self.product_id.contract_start_date_method - date_end = False - if contract_start_date_method == "manual": - contract_line_model = self.env["contract.line"] - date_end = ( - self.date_start - + contract_line_model.get_relative_delta( - self._get_auto_renew_rule_type(), - int(self.product_uom_qty), - ) - - relativedelta(days=1) - ) - return date_end - @api.depends("product_id") def _compute_auto_renew(self): - for rec in self: - if rec.product_id.is_contract: - rec.product_uom_qty = rec.product_id.default_qty - contract_start_date_method = rec.product_id.contract_start_date_method - if contract_start_date_method == "manual": - rec.date_start = rec.date_start or fields.Date.today() - rec.date_end = rec._get_date_end() - rec.is_auto_renew = rec.product_id.is_auto_renew - if rec.is_auto_renew: - rec.auto_renew_interval = rec.product_id.auto_renew_interval - rec.auto_renew_rule_type = rec.product_id.auto_renew_rule_type - - @api.onchange("date_start", "product_uom_qty") - def onchange_date_start(self): for rec in self.filtered("product_id.is_contract"): - rec.date_end = rec._get_date_end() if rec.date_start else False + rec.product_uom_qty = rec.product_id.default_qty + rec.is_auto_renew = rec.product_id.is_auto_renew + rec.auto_renew_interval = rec.product_id.auto_renew_interval + rec.auto_renew_rule_type = rec.product_id.auto_renew_rule_type def _get_contract_line_qty(self): """Returns the amount that will be placed in new contract lines.""" diff --git a/product_contract/readme/CONTRIBUTORS.md b/product_contract/readme/CONTRIBUTORS.md index c80ad3d118..f310ec169a 100644 --- a/product_contract/readme/CONTRIBUTORS.md +++ b/product_contract/readme/CONTRIBUTORS.md @@ -4,4 +4,5 @@ - Ernesto Tejeda - Pedro M. Baeza - Carlos Roca + - Sergio Teruel - David Jaen \<\> diff --git a/product_contract/static/description/index.html b/product_contract/static/description/index.html index 9dfa5c9ec0..65937877c3 100644 --- a/product_contract/static/description/index.html +++ b/product_contract/static/description/index.html @@ -433,6 +433,7 @@

Contributors

  • Ernesto Tejeda
  • Pedro M. Baeza
  • Carlos Roca
  • +
  • Sergio Teruel
  • David Jaen <david.jaen.revert@gmail.com>
  • diff --git a/product_contract/wizards/product_contract_configurator.py b/product_contract/wizards/product_contract_configurator.py index 4c6a63252f..86b5253cec 100644 --- a/product_contract/wizards/product_contract_configurator.py +++ b/product_contract/wizards/product_contract_configurator.py @@ -88,9 +88,9 @@ def _compute_auto_renew(self): def _get_auto_renew_rule_type(self): """monthly last day don't make sense for auto_renew_rule_type""" self.ensure_one() - if self.recurring_rule_type == "monthlylastday": + if self.auto_renew_rule_type == "monthlylastday": return "monthly" - return self.recurring_rule_type + return self.auto_renew_rule_type def _get_date_end(self): self.ensure_one()