From eb4055198967c32c1f62997faa9f212bf7fe3fd4 Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Tue, 25 Jan 2022 14:59:55 +0100 Subject: [PATCH] [14.0][IMP] contract: Improve function call for cron recurring creates --- contract/models/contract.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/contract/models/contract.py b/contract/models/contract.py index 6226d9e9a..6ddf7182c 100644 --- a/contract/models/contract.py +++ b/contract/models/contract.py @@ -598,22 +598,24 @@ class ContractContract(models.Model): self._compute_recurring_next_date() return moves + @api.model + def _get_recurring_create_func(self, create_type="invoice"): + """ + Allows to retrieve the recurring create function depending + on generate_type attribute + """ + if create_type == "invoice": + return self.__class__._recurring_create_invoice + @api.model def _cron_recurring_create(self, date_ref=False, create_type="invoice"): """ The cron function in order to create recurrent documents from contracts. """ - _recurring_create_func = f"_recurring_create_{create_type}" - if not hasattr(self, _recurring_create_func): - _logger.info( - _( - "No function to create %s documents automatically is " - "declared in contract.contract model. Passing." - ), - create_type, - ) - return False + _recurring_create_func = self._get_recurring_create_func( + create_type=create_type + ) if not date_ref: date_ref = fields.Date.context_today(self) domain = self._get_contracts_to_invoice_domain(date_ref) @@ -631,7 +633,7 @@ class ContractContract(models.Model): lambda c: c.company_id == company and (not c.date_end or c.recurring_next_date <= c.date_end) ).with_company(company) - getattr(contracts_to_invoice, _recurring_create_func)(date_ref) + _recurring_create_func(contracts_to_invoice, date_ref) return True @api.model