Browse Source

[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`.
pull/260/head
Jairo Llopis 6 years ago
parent
commit
c23a752132
No known key found for this signature in database GPG Key ID: 59564BF1E22F314F
  1. 2
      contract/data/contract_cron.xml
  2. 11
      contract/models/account_analytic_account.py

2
contract/data/contract_cron.xml

@ -1,5 +1,5 @@
<?xml version="1.0" encoding='UTF-8'?> <?xml version="1.0" encoding='UTF-8'?>
<odoo>
<odoo noupdate="1">
<record model="ir.cron" id="account_analytic_cron_for_invoice"> <record model="ir.cron" id="account_analytic_cron_for_invoice">
<field name="name">Generate Recurring Invoices from Contracts</field> <field name="name">Generate Recurring Invoices from Contracts</field>

11
contract/models/account_analytic_account.py

@ -260,13 +260,18 @@ class AccountAnalyticAccount(models.Model):
return invoice return invoice
@api.multi @api.multi
def recurring_create_invoice(self):
def recurring_create_invoice(self, limit=None):
"""Create invoices from contracts """Create invoices from contracts
:param int limit:
Max of invoices to create.
:return: invoices created :return: invoices created
""" """
invoices = self.env['account.invoice'] invoices = self.env['account.invoice']
for contract in self: for contract in self:
if limit and len(invoices) >= limit:
break
ref_date = contract.recurring_next_date or fields.Date.today() ref_date = contract.recurring_next_date or fields.Date.today()
if (contract.date_start > ref_date or if (contract.date_start > ref_date or
contract.date_end and contract.date_end < ref_date): contract.date_end and contract.date_end < ref_date):
@ -302,8 +307,8 @@ class AccountAnalyticAccount(models.Model):
'|', '|',
('date_end', '=', False), ('date_end', '=', False),
('date_end', '>=', today), ('date_end', '>=', today),
], limit=limit)
return contracts.recurring_create_invoice()
])
return contracts.recurring_create_invoice(limit)
@api.multi @api.multi
def action_contract_send(self): def action_contract_send(self):

Loading…
Cancel
Save