From 745dea14a4ae72d07d469c7a36388a90bbf4a732 Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Fri, 25 Feb 2022 11:44:34 +0100 Subject: [PATCH 1/7] [14.0][FIX] contract: Don't update recurring_next_date as computed --- contract/models/contract_line.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/contract/models/contract_line.py b/contract/models/contract_line.py index b9152c424..0c9b6a940 100644 --- a/contract/models/contract_line.py +++ b/contract/models/contract_line.py @@ -613,17 +613,8 @@ class ContractLine(models.Model): def _update_recurring_next_date(self): for rec in self: last_date_invoiced = rec.next_period_date_end - recurring_next_date = rec.get_next_invoice_date( - last_date_invoiced + relativedelta(days=1), - rec.recurring_invoicing_type, - rec.recurring_invoicing_offset, - rec.recurring_rule_type, - rec.recurring_interval, - max_date_end=rec.date_end, - ) rec.write( { - "recurring_next_date": recurring_next_date, "last_date_invoiced": last_date_invoiced, } ) From b56236796b6fc2738c7d400a54f4c14f4d73858a Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Mon, 7 Mar 2022 13:36:44 +0100 Subject: [PATCH 2/7] [14.0][FIX] contract: Compute recurring_next_date for False values too --- contract/models/contract_recurrency_mixin.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/contract/models/contract_recurrency_mixin.py b/contract/models/contract_recurrency_mixin.py index c7f7ad0c8..2bb49c5bd 100644 --- a/contract/models/contract_recurrency_mixin.py +++ b/contract/models/contract_recurrency_mixin.py @@ -95,7 +95,8 @@ class ContractRecurrencyMixin(models.AbstractModel): @api.depends("next_period_date_start") def _compute_recurring_next_date(self): - for rec in self.filtered("next_period_date_start"): + records_with_date_start = self.filtered("next_period_date_start") + for rec in records_with_date_start: rec.recurring_next_date = self.get_next_invoice_date( rec.next_period_date_start, rec.recurring_invoicing_type, @@ -104,6 +105,11 @@ class ContractRecurrencyMixin(models.AbstractModel): rec.recurring_interval, max_date_end=rec.date_end, ) + (self - records_with_date_start).update( + { + "recurring_next_date": False, + } + ) @api.depends("last_date_invoiced", "date_start", "date_end") def _compute_next_period_date_start(self): From 39046a6adee4786cffabbdaa120bf466afe14e81 Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Mon, 7 Mar 2022 14:35:52 +0100 Subject: [PATCH 3/7] [14.0][FIX] contract_sale_generation: Recompute next recurring date after generation on contract level As in contract module, recompute recurring_next_date after sale generation on contract level --- contract_sale_generation/models/contract.py | 1 + 1 file changed, 1 insertion(+) diff --git a/contract_sale_generation/models/contract.py b/contract_sale_generation/models/contract.py index d63c6080f..f15d2a694 100644 --- a/contract_sale_generation/models/contract.py +++ b/contract_sale_generation/models/contract.py @@ -120,6 +120,7 @@ class ContractContract(models.Model): lambda sale: sale.contract_auto_confirm ) sale_orders_to_confirm.action_confirm() + self._compute_recurring_next_date() return sale_orders @api.model From fa042e7340177c0c73c82b61d4abb82a6d4bcb92 Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Mon, 7 Mar 2022 14:36:38 +0100 Subject: [PATCH 4/7] [14.0][FIX] contract_sale_generation: Add start date on contract level (for recurrency on contract) --- contract_sale_generation/tests/common.py | 1 + 1 file changed, 1 insertion(+) diff --git a/contract_sale_generation/tests/common.py b/contract_sale_generation/tests/common.py index f6e1acb86..cea97f152 100644 --- a/contract_sale_generation/tests/common.py +++ b/contract_sale_generation/tests/common.py @@ -76,6 +76,7 @@ class ContractSaleCommon: "generation_type": "sale", "sale_autoconfirm": False, "group_id": cls.analytic_account.id, + "date_start": "2020-01-15", } ) cls.line_vals = { From f509b56386a9b9d278345c0132757251291a4c51 Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Mon, 7 Mar 2022 14:37:16 +0100 Subject: [PATCH 5/7] [14.0][IMP] contract_sale_generation: Add tests with another recurrency --- .../tests/test_contract_sale_recurrency.py | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 contract_sale_generation/tests/test_contract_sale_recurrency.py diff --git a/contract_sale_generation/tests/test_contract_sale_recurrency.py b/contract_sale_generation/tests/test_contract_sale_recurrency.py new file mode 100644 index 000000000..63ffd539b --- /dev/null +++ b/contract_sale_generation/tests/test_contract_sale_recurrency.py @@ -0,0 +1,78 @@ +# Copyright 2022 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from freezegun.api import freeze_time + +from odoo import fields +from odoo.tests import Form +from odoo.tests.common import SavepointCase + +from .common import ContractSaleCommon + + +def to_date(date): + return fields.Date.to_date(date) + + +today = "2020-01-15" + + +class TestContractSale(ContractSaleCommon, SavepointCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.contract_obj = cls.env["contract.contract"] + + @classmethod + def _create_contract(cls): + cls.contract = cls.contract.create( + { + "name": "Test Contract", + "partner_id": cls.partner.id, + } + ) + with Form(cls.contract) as contract_form: + contract_form.partner_id = cls.partner + contract_form.generation_type = "sale" + contract_form.group_id = cls.analytic_account + cls.contract = contract_form.save() + + def test_contract_next_date(self): + """ + Change recurrence to weekly + Check the recurring next date value on lines + """ + with freeze_time(today): + self._create_contract() + self.contract.recurring_rule_type = "weekly" + with freeze_time(today): + with Form(self.contract) as contract_form: + with contract_form.contract_line_ids.new() as line_form: + line_form.product_id = self.product_1 + line_form.name = "Services from #START# to #END#" + line_form.quantity = 1 + line_form.price_unit = 100.0 + line_form.discount = 50 + line_form.recurring_rule_type = "weekly" + + with freeze_time(today): + with Form(self.contract) as contract_form: + with contract_form.contract_line_ids.new() as line_form: + line_form.product_id = self.product_1 + line_form.name = "Services from #START# to #END#" + line_form.quantity = 2 + line_form.price_unit = 50.0 + line_form.recurring_rule_type = "weekly" + + self.assertEqual( + fields.Date.to_date("2020-01-15"), self.contract.recurring_next_date + ) + + self.contract.recurring_create_sale() + self.assertEqual( + fields.Date.to_date("2020-01-22"), self.contract.recurring_next_date + ) + self.contract.recurring_create_sale() + self.assertEqual( + fields.Date.to_date("2020-01-29"), self.contract.recurring_next_date + ) From e5a2be35cc68ac6ee460c71881bfcdbebabee9ce Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Mon, 7 Mar 2022 15:12:53 +0100 Subject: [PATCH 6/7] [14.0][IMP] contract: simplify compute as False values are already taken into account In get_next_invoice_date, False values are already taken into account, so, simplifying compute_recurring_next_date function. --- contract/models/contract_recurrency_mixin.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/contract/models/contract_recurrency_mixin.py b/contract/models/contract_recurrency_mixin.py index 2bb49c5bd..0d38a085e 100644 --- a/contract/models/contract_recurrency_mixin.py +++ b/contract/models/contract_recurrency_mixin.py @@ -95,8 +95,7 @@ class ContractRecurrencyMixin(models.AbstractModel): @api.depends("next_period_date_start") def _compute_recurring_next_date(self): - records_with_date_start = self.filtered("next_period_date_start") - for rec in records_with_date_start: + for rec in self: rec.recurring_next_date = self.get_next_invoice_date( rec.next_period_date_start, rec.recurring_invoicing_type, @@ -105,11 +104,6 @@ class ContractRecurrencyMixin(models.AbstractModel): rec.recurring_interval, max_date_end=rec.date_end, ) - (self - records_with_date_start).update( - { - "recurring_next_date": False, - } - ) @api.depends("last_date_invoiced", "date_start", "date_end") def _compute_next_period_date_start(self): From 20b538e9e9584892d9272d5405ce611f6d995e96 Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Tue, 8 Mar 2022 17:43:51 +0100 Subject: [PATCH 7/7] [14.0][IMP] contract: Add comment to remind updating method in next version --- contract/models/contract_line.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contract/models/contract_line.py b/contract/models/contract_line.py index 0c9b6a940..c38f75ed3 100644 --- a/contract/models/contract_line.py +++ b/contract/models/contract_line.py @@ -611,6 +611,8 @@ class ContractLine(models.Model): return name def _update_recurring_next_date(self): + # FIXME: Change method name according to real updated field + # e.g.: _update_last_date_invoiced() for rec in self: last_date_invoiced = rec.next_period_date_end rec.write(