diff --git a/account_invoice_merge/models/account_invoice.py b/account_invoice_merge/models/account_invoice.py index 4e27bf7ffb6..df40c58a357 100644 --- a/account_invoice_merge/models/account_invoice.py +++ b/account_invoice_merge/models/account_invoice.py @@ -53,6 +53,11 @@ def _get_first_invoice_fields(self, invoice): 'partner_bank_id': invoice.partner_bank_id.id, } + @api.multi + def _get_draft_invoices(self): + """Overridable function to return draft invoices to merge""" + return self.filtered(lambda x: x.state == 'draft') + @api.multi def do_merge(self, keep_references=True, date_invoice=False, remove_empty_invoice_lines=True): @@ -96,13 +101,10 @@ def make_key(br, fields): # compute what the new invoices should contain new_invoices = {} - draft_invoices = [invoice - for invoice in self - if invoice.state == 'draft'] seen_origins = {} seen_client_refs = {} - for account_invoice in draft_invoices: + for account_invoice in self._get_draft_invoices(): invoice_key = make_key( account_invoice, self._get_invoice_key_cols()) new_invoice = new_invoices.setdefault(invoice_key, ({}, [])) diff --git a/account_invoice_merge/wizard/invoice_merge.py b/account_invoice_merge/wizard/invoice_merge.py index 3a86e6b0076..c7ecad3783b 100644 --- a/account_invoice_merge/wizard/invoice_merge.py +++ b/account_invoice_merge/wizard/invoice_merge.py @@ -19,6 +19,20 @@ class InvoiceMerge(models.TransientModel): default=True) date_invoice = fields.Date('Invoice Date') + @api.model + def _get_not_mergeable_invoices_message(self, invoices): + """Overridable function to custom error message""" + key_fields = invoices._get_invoice_key_cols() + error_msg = {} + if len(invoices) != len(invoices._get_draft_invoices()): + error_msg['state'] = ( + _('Megeable State (ex : %s)') % + (invoices and invoices[0].state or _('Draf'))) + for field in key_fields: + if len(set(invoices.mapped(field))) > 1: + error_msg[field] = invoices._fields[field].string + return error_msg + @api.model def _dirty_check(self): if self.env.context.get('active_model', '') == 'account.invoice': @@ -29,29 +43,11 @@ def _dirty_check(self): 'view.')) invs = self.env['account.invoice'].browse(ids) - for d in invs: - if d['state'] != 'draft': - raise UserError( - _('At least one of the selected invoices is %s!') % - d['state']) - if d['account_id'] != invs[0]['account_id']: - raise UserError( - _('Not all invoices use the same account!')) - if d['company_id'] != invs[0]['company_id']: - raise UserError( - _('Not all invoices are at the same company!')) - if d['partner_id'] != invs[0]['partner_id']: - raise UserError( - _('Not all invoices are for the same partner!')) - if d['type'] != invs[0]['type']: - raise UserError( - _('Not all invoices are of the same type!')) - if d['currency_id'] != invs[0]['currency_id']: - raise UserError( - _('Not all invoices are at the same currency!')) - if d['journal_id'] != invs[0]['journal_id']: - raise UserError( - _('Not all invoices are at the same journal!')) + error_msg = self._get_not_mergeable_invoices_message(invs) + if error_msg: + all_msg = _("All invoices must have the same: \n") + all_msg += '\n'.join([value for value in error_msg.values()]) + raise UserError(all_msg) return {} @api.model