From edf6833becbce04d51974c115462f97ac2b46c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul=20=28ACSONE=29?= Date: Fri, 6 Dec 2019 11:09:32 +0100 Subject: [PATCH] [IMP] contract: add next period start/end fields Add two computed field showing the next period start and end date. This improve the UX and will enable further simplifications in the code. --- contract/models/contract_line.py | 38 ++++++++++++++++++++++++++++++++ contract/views/contract_line.xml | 2 ++ 2 files changed, 40 insertions(+) diff --git a/contract/models/contract_line.py b/contract/models/contract_line.py index 7b4565a6..afbc23f2 100644 --- a/contract/models/contract_line.py +++ b/contract/models/contract_line.py @@ -41,6 +41,14 @@ class ContractLine(models.Model): last_date_invoiced = fields.Date( string='Last Date Invoiced', readonly=True, copy=False ) + next_period_date_start = fields.Date( + string='Next Period Start', + compute='_compute_next_period_date_start', + ) + next_period_date_end = fields.Date( + string='Next Period End', + compute='_compute_next_period_date_end', + ) termination_notice_date = fields.Date( string='Termination notice date', compute="_compute_termination_notice_date", @@ -389,6 +397,8 @@ class ContractLine(models.Model): max_date_end, ): """Compute the end date for the next period""" + if not next_period_date_start: + return False if max_date_end and next_period_date_start > max_date_end: # start is past max date end: there is no next period return False @@ -412,6 +422,34 @@ class ContractLine(models.Model): next_period_date_end = max_date_end return next_period_date_end + @api.depends('last_date_invoiced', 'date_start', 'date_end') + def _compute_next_period_date_start(self): + for rec in self: + if rec.last_date_invoiced: + next_period_date_start = ( + rec.last_date_invoiced + relativedelta(days=1) + ) + else: + next_period_date_start = rec.date_start + if rec.date_end and next_period_date_start > rec.date_end: + next_period_date_start = False + rec.next_period_date_start = next_period_date_start + + @api.depends( + 'next_period_date_start', + 'recurring_rule_type', + 'recurring_interval', + 'date_end', + ) + def _compute_next_period_date_end(self): + for rec in self: + rec.next_period_date_end = self._get_next_period_date_end( + rec.next_period_date_start, + rec.recurring_rule_type, + rec.recurring_interval, + max_date_end=rec.date_end, + ) + @api.model def _get_first_date_end( self, date_start, auto_renew_rule_type, auto_renew_interval diff --git a/contract/views/contract_line.xml b/contract/views/contract_line.xml index 2d14a81f..85f96db4 100644 --- a/contract/views/contract_line.xml +++ b/contract/views/contract_line.xml @@ -15,11 +15,13 @@ + +