diff --git a/product_contract/models/sale_order_line.py b/product_contract/models/sale_order_line.py index 07195dcc..836f9937 100644 --- a/product_contract/models/sale_order_line.py +++ b/product_contract/models/sale_order_line.py @@ -63,39 +63,55 @@ class SaleOrderLine(models.Model): @api.onchange('product_id') def onchange_product(self): - if self.product_id.is_contract: - self.recurring_rule_type = self.product_id.recurring_rule_type - self.recurring_invoicing_type = ( - self.product_id.recurring_invoicing_type - ) - self.recurring_interval = self.product_id.recurring_interval - self.date_start = self.date_start or fields.Date.today() - if self.is_auto_renew: - self.date_end = self.date_start + self.env[ - 'account.analytic.invoice.line' - ].get_relative_delta( - self.product_id.auto_renew_rule_type, - self.product_id.auto_renew_interval, + contract_line_env = self.env['account.analytic.invoice.line'] + for rec in self: + if rec.product_id.is_contract: + rec.recurring_rule_type = rec.product_id.recurring_rule_type + rec.recurring_invoicing_type = ( + rec.product_id.recurring_invoicing_type ) + rec.recurring_interval = rec.product_id.recurring_interval + rec.date_start = rec.date_start or fields.Date.today() + if rec.is_auto_renew: + rec.date_end = ( + rec.date_start + + contract_line_env.get_relative_delta( + rec.product_id.auto_renew_rule_type, + rec.product_id.auto_renew_interval, + ) + ) @api.onchange('date_start') def onchange_date_start(self): for rec in self: if rec.is_auto_renew: - if not self.date_start: + if not rec.date_start: rec.date_end = False else: - self.date_end = self.date_start + self.env[ + rec.date_end = rec.date_start + self.env[ 'account.analytic.invoice.line' ].get_relative_delta( - self.product_id.auto_renew_rule_type, - self.product_id.auto_renew_interval, + rec.product_id.auto_renew_rule_type, + rec.product_id.auto_renew_interval, ) @api.multi def _prepare_contract_line_values(self, contract): self.ensure_one() - contract_line_env = self.env['account.analytic.invoice.line'] + recurring_next_date = self.env[ + 'account.analytic.invoice.line' + ]._compute_first_recurring_next_date( + self.date_start or fields.Date.today(), + self.recurring_invoicing_type, + self.recurring_rule_type, + self.recurring_interval, + ) + termination_notice_interval = ( + self.product_id.termination_notice_interval + ) + termination_notice_rule_type = ( + self.product_id.termination_notice_rule_type + ) return { 'sequence': self.sequence, 'product_id': self.product_id.id, @@ -106,23 +122,15 @@ class SaleOrderLine(models.Model): 'discount': self.discount, 'date_end': self.date_end, 'date_start': self.date_start or fields.Date.today(), - 'recurring_next_date': - contract_line_env._compute_first_recurring_next_date( - self.date_start or fields.Date.today(), - self.recurring_invoicing_type, - self.recurring_rule_type, - self.recurring_interval, - ), + 'recurring_next_date': recurring_next_date, 'recurring_interval': self.recurring_interval, 'recurring_invoicing_type': self.recurring_invoicing_type, 'recurring_rule_type': self.recurring_rule_type, 'is_auto_renew': self.product_id.is_auto_renew, 'auto_renew_interval': self.product_id.auto_renew_interval, 'auto_renew_rule_type': self.product_id.auto_renew_rule_type, - 'termination_notice_interval': - self.product_id.termination_notice_interval, - 'termination_notice_rule_type': - self.product_id.termination_notice_rule_type, + 'termination_notice_interval': termination_notice_interval, + 'termination_notice_rule_type': termination_notice_rule_type, 'contract_id': contract.id, 'sale_order_line_id': self.id, } diff --git a/product_contract/tests/test_sale_order.py b/product_contract/tests/test_sale_order.py index 5409ebc3..73715d12 100644 --- a/product_contract/tests/test_sale_order.py +++ b/product_contract/tests/test_sale_order.py @@ -54,11 +54,12 @@ class TestSaleOrder(TransactionCase): lambda l: l.product_id == self.product1 ) self.order_line1.date_start = '2018-01-01' + pricelist = self.sale.partner_id.property_product_pricelist.id self.contract = self.env["account.analytic.account"].create( { "name": "Test Contract 2", "partner_id": self.sale.partner_id.id, - "pricelist_id": self.sale.partner_id.property_product_pricelist.id, + "pricelist_id": pricelist, "recurring_invoices": True, "contract_type": "purchase", "contract_template_id": self.contract_template1.id,