From 3e529d0037e527372803aaef53535a94a546417e Mon Sep 17 00:00:00 2001 From: sbejaoui Date: Tue, 7 Jan 2020 12:06:14 +0100 Subject: [PATCH 1/4] [IMP] - Add failing test for forecast generation of auto renewal contract lines If a contract line is set to auto renew the forecast should continue after the date end and stop at the company forecast period --- .../test_contract_line_forecast_period.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/contract_forecast/tests/test_contract_line_forecast_period.py b/contract_forecast/tests/test_contract_line_forecast_period.py index 2414c91b7..b52efbcc8 100644 --- a/contract_forecast/tests/test_contract_line_forecast_period.py +++ b/contract_forecast/tests/test_contract_line_forecast_period.py @@ -260,3 +260,27 @@ class TestContractLineForecastPeriod(TestContractBase): Date.to_date("2020-01-01"), ), ) + + @mute_logger("odoo.addons.queue_job.models.base") + def test_forecast_period_for_auto_renew_contract(self): + """If a contract line is set to auto renew the forecast should continue + after the date end and stop at the company forecast period""" + # Set the company forecast period to three years + self.acct_line.contract_id.company_id.contract_forecast_interval = 36 + self.acct_line.write( + { + 'date_start': Date.today(), + 'recurring_next_date': Date.today(), + 'date_end': Date.today() + relativedelta(years=1), + 'recurring_rule_type': "monthlylastday", + 'last_date_invoiced': False, + 'recurring_invoicing_type': 'pre-paid', + 'is_auto_renew': False, + } + ) + self.assertTrue(self.acct_line.forecast_period_ids) + self.assertEqual(len(self.acct_line.forecast_period_ids), 13) + + self.acct_line.write({'is_auto_renew': True}) + self.assertTrue(self.acct_line.forecast_period_ids) + self.assertEqual(len(self.acct_line.forecast_period_ids), 37) From 01acbaa35578b44f1a632737fae84e0637ca2e8e Mon Sep 17 00:00:00 2001 From: sbejaoui Date: Tue, 7 Jan 2020 12:18:07 +0100 Subject: [PATCH 2/4] [IMP] - add a non-regression test: forecast for undefined date end contracts If a contract line have and undefined date end the forecast should continue to the company forecast period --- .../test_contract_line_forecast_period.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/contract_forecast/tests/test_contract_line_forecast_period.py b/contract_forecast/tests/test_contract_line_forecast_period.py index b52efbcc8..18d1c6dd1 100644 --- a/contract_forecast/tests/test_contract_line_forecast_period.py +++ b/contract_forecast/tests/test_contract_line_forecast_period.py @@ -284,3 +284,26 @@ class TestContractLineForecastPeriod(TestContractBase): self.acct_line.write({'is_auto_renew': True}) self.assertTrue(self.acct_line.forecast_period_ids) self.assertEqual(len(self.acct_line.forecast_period_ids), 37) + + @mute_logger("odoo.addons.queue_job.models.base") + def test_forecast_period_for_undefined_date_end_contract(self): + """If a contract line have and undefined date end the forecast should + continue to the company forecast period""" + self.acct_line.contract_id.company_id.contract_forecast_interval = 36 + self.acct_line.write( + { + 'date_start': Date.today(), + 'recurring_next_date': Date.today(), + 'date_end': Date.today() + relativedelta(years=1), + 'recurring_rule_type': "monthlylastday", + 'last_date_invoiced': False, + 'recurring_invoicing_type': 'pre-paid', + 'is_auto_renew': False, + } + ) + self.assertTrue(self.acct_line.forecast_period_ids) + self.assertEqual(len(self.acct_line.forecast_period_ids), 13) + + self.acct_line.write({'date_end': False}) + self.assertTrue(self.acct_line.forecast_period_ids) + self.assertEqual(len(self.acct_line.forecast_period_ids), 37) From 0aac5e71c865e9b04281703bc931295645f261fe Mon Sep 17 00:00:00 2001 From: sbejaoui Date: Tue, 7 Jan 2020 12:28:24 +0100 Subject: [PATCH 3/4] [FIX] - for auto-renew contract lines, don't stop the forecast at the date end --- contract_forecast/models/contract_line.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/contract_forecast/models/contract_line.py b/contract_forecast/models/contract_line.py index 1852ee32b..99ae59cf6 100644 --- a/contract_forecast/models/contract_line.py +++ b/contract_forecast/models/contract_line.py @@ -74,6 +74,7 @@ class ContractLine(models.Model): if rec.last_date_invoiced else rec.date_start - relativedelta(days=1) ) + max_date_end = rec.date_end if not rec.is_auto_renew else False while ( period_date_end and rec._get_generate_forecast_periods_criteria( @@ -85,7 +86,7 @@ class ContractLine(models.Model): period_date_start, rec.recurring_rule_type, rec.recurring_interval, - max_date_end=rec.date_end, + max_date_end=max_date_end, ) recurring_next_date = rec.get_next_invoice_date( period_date_start, @@ -93,7 +94,7 @@ class ContractLine(models.Model): rec.recurring_invoicing_offset, rec.recurring_rule_type, rec.recurring_interval, - rec.date_end, + max_date_end=max_date_end, ) if period_date_end and recurring_next_date: values.append( From 21ec2236537e6c3afc993880ad0913cf94e8ed9e Mon Sep 17 00:00:00 2001 From: sbejaoui Date: Tue, 7 Jan 2020 12:32:08 +0100 Subject: [PATCH 4/4] [FIX] - Fix unit test: is_auto_renew is considered in the forecast generation --- .../tests/test_contract_line_forecast_period.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/contract_forecast/tests/test_contract_line_forecast_period.py b/contract_forecast/tests/test_contract_line_forecast_period.py index 18d1c6dd1..e85504316 100644 --- a/contract_forecast/tests/test_contract_line_forecast_period.py +++ b/contract_forecast/tests/test_contract_line_forecast_period.py @@ -135,12 +135,15 @@ class TestContractLineForecastPeriod(TestContractBase): 'date_end': Date.today() + relativedelta(months=3), 'recurring_rule_type': "monthlylastday", 'recurring_invoicing_type': 'pre-paid', - 'is_auto_renew': True, + 'is_auto_renew': False, } ) self.acct_line._onchange_date_start() self.assertTrue(self.acct_line.forecast_period_ids) self.assertEqual(len(self.acct_line.forecast_period_ids), 4) + self.acct_line.write({'is_auto_renew': True}) + self.assertTrue(self.acct_line.forecast_period_ids) + self.assertEqual(len(self.acct_line.forecast_period_ids), 13) @mute_logger("odoo.addons.queue_job.models.base") def test_forecast_period_on_contract_line_update_8(self):