From c4e240f1573110212eb79edb8790175f7102dc38 Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Thu, 4 Nov 2021 13:27:41 +0100 Subject: [PATCH] [IMP] contract: Allows to get several functions to create recurring documents --- contract/models/contract.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/contract/models/contract.py b/contract/models/contract.py index f41ed0428..728cb89dd 100644 --- a/contract/models/contract.py +++ b/contract/models/contract.py @@ -6,6 +6,7 @@ # Copyright 2018 ACSONE SA/NV # Copyright 2021 Tecnativa - Víctor Martínez # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import logging from odoo import api, fields, models from odoo.exceptions import UserError, ValidationError @@ -13,6 +14,8 @@ from odoo.osv import expression from odoo.tests import Form from odoo.tools.translate import _ +_logger = logging.getLogger(__name__) + class ContractContract(models.Model): _name = "contract.contract" @@ -597,6 +600,20 @@ class ContractContract(models.Model): @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 if not date_ref: date_ref = fields.Date.context_today(self) domain = self._get_contracts_to_invoice_domain(date_ref) @@ -606,22 +623,16 @@ class ContractContract(models.Model): [("generation_type", "=", create_type)], ] ) - invoice_obj = self.env["account.move"] - contracts = self.search(domain) companies = set(contracts.mapped("company_id")) - invoice_ids = [] # Invoice by companies, so assignation emails get correct context - # Use ids instead of recordset for perfomances reasons for company in companies: contracts_to_invoice = contracts.filtered( lambda c: c.company_id == company and (not c.date_end or c.recurring_next_date <= c.date_end) ).with_company(company) - invoice_ids.extend( - contracts_to_invoice._recurring_create_invoice(date_ref).ids - ) - return invoice_obj.browse(invoice_ids) + getattr(contracts_to_invoice, _recurring_create_func)(date_ref) + return True @api.model def cron_recurring_create_invoice(self, date_ref=None):