diff --git a/contract/models/contract_line.py b/contract/models/contract_line.py index b9152c424..c38f75ed3 100644 --- a/contract/models/contract_line.py +++ b/contract/models/contract_line.py @@ -611,19 +611,12 @@ 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 - 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, } ) diff --git a/contract/models/contract_recurrency_mixin.py b/contract/models/contract_recurrency_mixin.py index c7f7ad0c8..0d38a085e 100644 --- a/contract/models/contract_recurrency_mixin.py +++ b/contract/models/contract_recurrency_mixin.py @@ -95,7 +95,7 @@ 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"): + for rec in self: rec.recurring_next_date = self.get_next_invoice_date( rec.next_period_date_start, rec.recurring_invoicing_type, 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 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 = { 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 + )