From e6c330bd0cc8249e6392fcd6c701b3105fe0fcbd Mon Sep 17 00:00:00 2001 From: David Date: Mon, 14 Jan 2019 14:52:28 +0100 Subject: [PATCH 1/3] [10.0] contract: allow to limit records on cron --- contract/models/account_analytic_account.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contract/models/account_analytic_account.py b/contract/models/account_analytic_account.py index bc268f79..5aed2b99 100644 --- a/contract/models/account_analytic_account.py +++ b/contract/models/account_analytic_account.py @@ -312,7 +312,7 @@ class AccountAnalyticAccount(models.Model): return invoices @api.model - def cron_recurring_create_invoice(self): + def cron_recurring_create_invoice(self, limit=None): today = fields.Date.today() contracts = self.with_context(cron=True).search([ ('recurring_invoices', '=', True), @@ -320,7 +320,7 @@ class AccountAnalyticAccount(models.Model): '|', ('date_end', '=', False), ('date_end', '>=', today), - ]) + ], limit=limit) return contracts.recurring_create_invoice() @api.multi From ca3cc20bf0c551990bbebb3ae3daadde86abb274 Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Tue, 15 Jan 2019 08:30:10 +0000 Subject: [PATCH 2/3] [FIX] contract: Process invoices limit in a lower method To avoid blocking the queue when there are more than the specified limit of contracts, we process the limit while creating invoices instead of while searching for contracts, and break the process when the max of invoices has been created. See https://github.com/OCA/contract/pull/260#pullrequestreview-192187022 for more details. In case you need to use this new feature in the cron, it is also modified as `noupdate=1`. --- contract/models/account_analytic_account.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/contract/models/account_analytic_account.py b/contract/models/account_analytic_account.py index 5aed2b99..c6e37801 100644 --- a/contract/models/account_analytic_account.py +++ b/contract/models/account_analytic_account.py @@ -278,13 +278,18 @@ class AccountAnalyticAccount(models.Model): return invoice @api.multi - def recurring_create_invoice(self): + def recurring_create_invoice(self, limit=None): """Create invoices from contracts + :param int limit: + Max of invoices to create. + :return: invoices created """ invoices = self.env['account.invoice'] for contract in self: + if limit and len(invoices) >= limit: + break ref_date = contract.recurring_next_date or fields.Date.today() if (contract.date_start > ref_date or contract.date_end and contract.date_end < ref_date): @@ -320,8 +325,8 @@ class AccountAnalyticAccount(models.Model): '|', ('date_end', '=', False), ('date_end', '>=', today), - ], limit=limit) - return contracts.recurring_create_invoice() + ]) + return contracts.recurring_create_invoice(limit) @api.multi def action_contract_send(self): From b2b04690b3b5a2b10b441aa9701db02103e31db5 Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Tue, 15 Jan 2019 12:11:51 +0000 Subject: [PATCH 3/3] [IMP] contract: Use keyword argument --- contract/models/account_analytic_account.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contract/models/account_analytic_account.py b/contract/models/account_analytic_account.py index c6e37801..cd50b0ca 100644 --- a/contract/models/account_analytic_account.py +++ b/contract/models/account_analytic_account.py @@ -326,7 +326,7 @@ class AccountAnalyticAccount(models.Model): ('date_end', '=', False), ('date_end', '>=', today), ]) - return contracts.recurring_create_invoice(limit) + return contracts.recurring_create_invoice(limit=limit) @api.multi def action_contract_send(self):