Skip to content

Commit

Permalink
Merge pull request #1 from akretion/12.0-account_invoice_merge-not_me…
Browse files Browse the repository at this point in the history
…rgeable_msg_draft-inv

[IMP] Make the function to get draft invoices & _dirty_check overridable
  • Loading branch information
sylvainvh authored Jun 19, 2020
2 parents 5764427 + 21b6f5d commit 75f9a9c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
10 changes: 6 additions & 4 deletions account_invoice_merge/models/account_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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, ({}, []))
Expand Down
42 changes: 19 additions & 23 deletions account_invoice_merge/wizard/invoice_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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
Expand Down

0 comments on commit 75f9a9c

Please sign in to comment.