Skip to content

Commit 75f9a9c

Browse files
authored
Merge pull request #1 from akretion/12.0-account_invoice_merge-not_mergeable_msg_draft-inv
[IMP] Make the function to get draft invoices & _dirty_check overridable
2 parents 5764427 + 21b6f5d commit 75f9a9c

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

account_invoice_merge/models/account_invoice.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ def _get_first_invoice_fields(self, invoice):
5353
'partner_bank_id': invoice.partner_bank_id.id,
5454
}
5555

56+
@api.multi
57+
def _get_draft_invoices(self):
58+
"""Overridable function to return draft invoices to merge"""
59+
return self.filtered(lambda x: x.state == 'draft')
60+
5661
@api.multi
5762
def do_merge(self, keep_references=True, date_invoice=False,
5863
remove_empty_invoice_lines=True):
@@ -96,13 +101,10 @@ def make_key(br, fields):
96101

97102
# compute what the new invoices should contain
98103
new_invoices = {}
99-
draft_invoices = [invoice
100-
for invoice in self
101-
if invoice.state == 'draft']
102104
seen_origins = {}
103105
seen_client_refs = {}
104106

105-
for account_invoice in draft_invoices:
107+
for account_invoice in self._get_draft_invoices():
106108
invoice_key = make_key(
107109
account_invoice, self._get_invoice_key_cols())
108110
new_invoice = new_invoices.setdefault(invoice_key, ({}, []))

account_invoice_merge/wizard/invoice_merge.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ class InvoiceMerge(models.TransientModel):
1919
default=True)
2020
date_invoice = fields.Date('Invoice Date')
2121

22+
@api.model
23+
def _get_not_mergeable_invoices_message(self, invoices):
24+
"""Overridable function to custom error message"""
25+
key_fields = invoices._get_invoice_key_cols()
26+
error_msg = {}
27+
if len(invoices) != len(invoices._get_draft_invoices()):
28+
error_msg['state'] = (
29+
_('Megeable State (ex : %s)') %
30+
(invoices and invoices[0].state or _('Draf')))
31+
for field in key_fields:
32+
if len(set(invoices.mapped(field))) > 1:
33+
error_msg[field] = invoices._fields[field].string
34+
return error_msg
35+
2236
@api.model
2337
def _dirty_check(self):
2438
if self.env.context.get('active_model', '') == 'account.invoice':
@@ -29,29 +43,11 @@ def _dirty_check(self):
2943
'view.'))
3044

3145
invs = self.env['account.invoice'].browse(ids)
32-
for d in invs:
33-
if d['state'] != 'draft':
34-
raise UserError(
35-
_('At least one of the selected invoices is %s!') %
36-
d['state'])
37-
if d['account_id'] != invs[0]['account_id']:
38-
raise UserError(
39-
_('Not all invoices use the same account!'))
40-
if d['company_id'] != invs[0]['company_id']:
41-
raise UserError(
42-
_('Not all invoices are at the same company!'))
43-
if d['partner_id'] != invs[0]['partner_id']:
44-
raise UserError(
45-
_('Not all invoices are for the same partner!'))
46-
if d['type'] != invs[0]['type']:
47-
raise UserError(
48-
_('Not all invoices are of the same type!'))
49-
if d['currency_id'] != invs[0]['currency_id']:
50-
raise UserError(
51-
_('Not all invoices are at the same currency!'))
52-
if d['journal_id'] != invs[0]['journal_id']:
53-
raise UserError(
54-
_('Not all invoices are at the same journal!'))
46+
error_msg = self._get_not_mergeable_invoices_message(invs)
47+
if error_msg:
48+
all_msg = _("All invoices must have the same: \n")
49+
all_msg += '\n'.join([value for value in error_msg.values()])
50+
raise UserError(all_msg)
5551
return {}
5652

5753
@api.model

0 commit comments

Comments
 (0)