From 2a181deca96cf34f04d3ff99ad46df958aae95ef Mon Sep 17 00:00:00 2001 From: nans Date: Fri, 11 Dec 2020 13:16:56 +0100 Subject: [PATCH] [REF] product_contract: micro-refactoring to ease overrides --- product_contract/models/sale_order_line.py | 68 +++++++++++----------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/product_contract/models/sale_order_line.py b/product_contract/models/sale_order_line.py index 4883306db..cb1d7b5e7 100644 --- a/product_contract/models/sale_order_line.py +++ b/product_contract/models/sale_order_line.py @@ -96,9 +96,20 @@ class SaleOrderLine(models.Model): return "monthly" return self.recurring_rule_type + def _get_date_end(self): + self.ensure_one() + 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.onchange('product_id') def onchange_product(self): - contract_line_model = self.env['contract.line'] for rec in self: if rec.product_id.is_contract: rec.product_uom_qty = rec.product_id.default_qty @@ -108,14 +119,7 @@ class SaleOrderLine(models.Model): ) rec.date_start = rec.date_start or fields.Date.today() - rec.date_end = ( - rec.date_start - + contract_line_model.get_relative_delta( - rec._get_auto_renew_rule_type(), - int(rec.product_uom_qty), - ) - - relativedelta(days=1) - ) + 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 = ( @@ -127,19 +131,25 @@ class SaleOrderLine(models.Model): @api.onchange('date_start', 'product_uom_qty', 'recurring_rule_type') def onchange_date_start(self): - contract_line_model = self.env['contract.line'] - for rec in self.filtered('product_id.is_contract'): - if not rec.date_start: - rec.date_end = False - else: - rec.date_end = ( - rec.date_start - + contract_line_model.get_relative_delta( - rec._get_auto_renew_rule_type(), - int(rec.product_uom_qty), - ) - - relativedelta(days=1) - ) + for rec in self.filtered("product_id.is_contract"): + rec.date_end = rec._get_date_end() if rec.date_start else False + + def _get_contract_line_qty(self): + """Returns the quantity to be put on new contract lines.""" + self.ensure_one() + # The quantity on the generated contract line is 1, as it + # correspond to the most common use cases: + # - quantity on the SO line = number of periods sold and unit + # price the price of one period, so the + # total amount of the SO corresponds to the planned value + # of the contract; in this case the quantity on the contract + # line must be 1 + # - quantity on the SO line = number of hours sold, + # automatic invoicing of the actual hours through a variable + # quantity formula, in which case the quantity on the contract + # line is not used + # Other use cases are easy to implement by overriding this method. + return 1.0 @api.multi def _prepare_contract_line_values( @@ -169,19 +179,7 @@ class SaleOrderLine(models.Model): 'sequence': self.sequence, 'product_id': self.product_id.id, 'name': self.name, - # The quantity on the generated contract line is 1, as it - # correspond to the most common use cases: - # - quantity on the SO line = number of periods sold and unit - # price the price of one period, so the - # total amount of the SO corresponds to the planned value - # of the contract; in this case the quantity on the contract - # line must be 1 - # - quantity on the SO line = number of hours sold, - # automatic invoicing of the actual hours through a variable - # quantity formula, in which case the quantity on the contract - # line is not used - # Other use cases are easy to implement by overriding this method. - 'quantity': 1.0, + 'quantity': self._get_contract_line_qty(), 'uom_id': self.product_uom.id, 'price_unit': self.price_unit, 'discount': self.discount,