From 5bc895980e4347b7971b89bd73c83883160ac403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul=20=28ACSONE=29?= Date: Fri, 6 Dec 2019 10:19:55 +0100 Subject: [PATCH] [REF] contract: clarify _get_recurring_next_date First compute the next period end date, then derive the next invoice date from the next period stard and end date. --- contract/models/contract_line.py | 47 ++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/contract/models/contract_line.py b/contract/models/contract_line.py index 344ebebd..c16186e7 100644 --- a/contract/models/contract_line.py +++ b/contract/models/contract_line.py @@ -358,20 +358,51 @@ class ContractLine(models.Model): @api.model def _get_recurring_next_date( self, - date_start, + next_period_date_start, recurring_invoicing_type, recurring_rule_type, recurring_interval, ): + next_period_date_end = self._get_next_period_date_end( + next_period_date_start, + recurring_rule_type, + recurring_interval, + max_date_end=False, # TODO + ) + if recurring_rule_type == 'monthlylastday': + return next_period_date_end + elif recurring_invoicing_type == 'pre-paid': + return next_period_date_start + else: # post-paid + return next_period_date_end + relativedelta(days=1) + + @api.model + def _get_next_period_date_end( + self, + next_period_date_start, + recurring_rule_type, + recurring_interval, + max_date_end, + ): + """Compute the end date for the next period""" if recurring_rule_type == 'monthlylastday': - return date_start + self.get_relative_delta( - recurring_rule_type, recurring_interval - 1 + next_period_date_end = ( + next_period_date_start + + self.get_relative_delta( + recurring_rule_type, recurring_interval - 1 + ) ) - if recurring_invoicing_type == 'pre-paid': - return date_start - return date_start + self.get_relative_delta( - recurring_rule_type, recurring_interval - ) + else: + next_period_date_end = ( + next_period_date_start + + self.get_relative_delta( + recurring_rule_type, recurring_interval + ) + - relativedelta(days=1) + ) + if max_date_end and next_period_date_end > max_date_end: + next_period_date_end = max_date_end + return next_period_date_end @api.model def _get_first_date_end(