From c8be66739db47e406b69db86613a7e035264a18a Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Thu, 19 Jun 2014 10:55:17 +0200 Subject: [PATCH] [FIX] account_analytic_analysis: recurring_create_invoice If no contract found, just pass, to avoid making the sql request --- .../account_analytic_analysis.py | 51 ++++++++++---------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/addons/account_analytic_analysis/account_analytic_analysis.py b/addons/account_analytic_analysis/account_analytic_analysis.py index a03329d..dcb078b 100644 --- a/addons/account_analytic_analysis/account_analytic_analysis.py +++ b/addons/account_analytic_analysis/account_analytic_analysis.py @@ -745,31 +745,32 @@ class account_analytic_account(osv.osv): contract_ids = ids else: contract_ids = self.search(cr, uid, [('recurring_next_date','<=', current_date), ('state','=', 'open'), ('recurring_invoices','=', True), ('type', '=', 'contract')]) - cr.execute('SELECT company_id, array_agg(id) as ids FROM account_analytic_account WHERE id IN %s GROUP BY company_id', (tuple(contract_ids),)) - for company_id, ids in cr.fetchall(): - for contract in self.browse(cr, uid, ids, context=dict(context, company_id=company_id, force_company=company_id)): - try: - invoice_values = self._prepare_invoice(cr, uid, contract, context=context) - invoice_ids.append(self.pool['account.invoice'].create(cr, uid, invoice_values, context=context)) - next_date = datetime.datetime.strptime(contract.recurring_next_date or current_date, "%Y-%m-%d") - interval = contract.recurring_interval - if contract.recurring_rule_type == 'daily': - new_date = next_date+relativedelta(days=+interval) - elif contract.recurring_rule_type == 'weekly': - new_date = next_date+relativedelta(weeks=+interval) - elif contract.recurring_rule_type == 'yearly': - new_date = next_date+relativedelta(years=+interval) - else: - new_date = next_date+relativedelta(months=+interval) - self.write(cr, uid, [contract.id], {'recurring_next_date': new_date.strftime('%Y-%m-%d')}, context=context) - if automatic: - cr.commit() - except Exception: - if automatic: - cr.rollback() - _logger.exception('Fail to create recurring invoice for contract %s', contract.code) - else: - raise + if contract_ids: + cr.execute('SELECT company_id, array_agg(id) as ids FROM account_analytic_account WHERE id IN %s GROUP BY company_id', (tuple(contract_ids),)) + for company_id, ids in cr.fetchall(): + for contract in self.browse(cr, uid, ids, context=dict(context, company_id=company_id, force_company=company_id)): + try: + invoice_values = self._prepare_invoice(cr, uid, contract, context=context) + invoice_ids.append(self.pool['account.invoice'].create(cr, uid, invoice_values, context=context)) + next_date = datetime.datetime.strptime(contract.recurring_next_date or current_date, "%Y-%m-%d") + interval = contract.recurring_interval + if contract.recurring_rule_type == 'daily': + new_date = next_date+relativedelta(days=+interval) + elif contract.recurring_rule_type == 'weekly': + new_date = next_date+relativedelta(weeks=+interval) + elif contract.recurring_rule_type == 'yearly': + new_date = next_date+relativedelta(years=+interval) + else: + new_date = next_date+relativedelta(months=+interval) + self.write(cr, uid, [contract.id], {'recurring_next_date': new_date.strftime('%Y-%m-%d')}, context=context) + if automatic: + cr.commit() + except Exception: + if automatic: + cr.rollback() + _logger.exception('Fail to create recurring invoice for contract %s', contract.code) + else: + raise return invoice_ids class account_analytic_account_summary_user(osv.osv): -- 1.7.10.4