From c91a170ffcbfac1c610f62f05c5ac6308f7a4af7 Mon Sep 17 00:00:00 2001 From: Maxime Chambreuil Date: Fri, 20 Dec 2019 13:47:54 -0600 Subject: [PATCH] [IMP] contract_sale_invoicing # Conflicts: # contract_sale_invoicing/models/contract.py --- contract_sale_invoicing/models/contract.py | 26 ++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/contract_sale_invoicing/models/contract.py b/contract_sale_invoicing/models/contract.py index d161c9f9..5ae4e836 100644 --- a/contract_sale_invoicing/models/contract.py +++ b/contract_sale_invoicing/models/contract.py @@ -13,7 +13,21 @@ class ContractContract(models.Model): group_by = fields.Selection([ ('analytic_account', 'Analytic Account'), ('contract', 'Contract')], - default='analytic_account', string='Group By') + default='analytic_account', string='Group invoices by') + + def get_sale_order_domain(self): + domain = [ + ('partner_invoice_id', 'child_of', + self.partner_id.commercial_partner_id.ids), + ('invoice_status', '=', 'to invoice'), + ('date_order', '<=', + '{} 23:59:59'.format(self.recurring_next_date)), + ] + if self.group_by == 'analytic_account': + domain.append(('analytic_account_id', '=', self.group_id.id)) + elif self.group_by == 'contract': + domain.append(('contract_id', '=', self.id)) + return domain @api.multi def _recurring_create_invoice(self, date_ref=False): @@ -21,14 +35,8 @@ class ContractContract(models.Model): for rec in self: if not rec.invoicing_sales: return invoices - sales = self.env['sale.order'].search([ - ('analytic_account_id', '=', rec.group_id.id), - ('partner_invoice_id', 'child_of', - rec.partner_id.commercial_partner_id.ids), - ('invoice_status', '=', 'to invoice'), - ('date_order', '<=', - '{} 23:59:59'.format(rec.recurring_next_date)), - ]) + so_domain = rec.get_sale_order_domain() + sales = self.env['sale.order'].search(so_domain) if sales: invoice_ids = sales.action_invoice_create() invoices |= self.env['account.invoice'].browse(invoice_ids)[:1]