Skip to content

Commit

Permalink
IMP account_invoice_merge: Disable merge lines
Browse files Browse the repository at this point in the history
  • Loading branch information
angelmoya committed Feb 12, 2024
1 parent 6b87ee0 commit d8a5779
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
23 changes: 19 additions & 4 deletions account_invoice_merge/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def _get_invoice_key_cols(self):
]

@api.model
def _get_invoice_line_key_cols(self):
def _get_invoice_line_key_cols(self, disable_merge_lines=False):
fields = [
"name",
"discount",
Expand All @@ -39,6 +39,13 @@ def _get_invoice_line_key_cols(self):
"analytic_distribution",
"product_uom_id",
]
if disable_merge_lines:
extra_fields = [
"id",
"display_type",
"sequence",
]
fields += extra_fields
for field in [
"sale_line_ids", # odoo/sale
"purchase_line_id", # odoo/purchase
Expand Down Expand Up @@ -102,7 +109,11 @@ def make_key(self, br, fields):

# flake8: noqa: C901
def do_merge(
self, keep_references=True, date_invoice=False, remove_empty_invoice_lines=True
self,
keep_references=True,
date_invoice=False,
remove_empty_invoice_lines=True,
disable_merge_lines=False,
):
"""
To merge similar type of account invoices.
Expand Down Expand Up @@ -172,12 +183,16 @@ def do_merge(

for invoice_line in account_invoice.invoice_line_ids:
line_key = self.make_key(
invoice_line, self._get_invoice_line_key_cols()
invoice_line,
self._get_invoice_line_key_cols(
disable_merge_lines=disable_merge_lines
),
)
o_line = invoice_infos["invoice_line_ids"].setdefault(line_key, {})

if o_line:
if (not disable_merge_lines) and o_line:
# merge the line with an existing line

for sum_field in sum_fields:
if sum_field in invoice_line._fields:
sum_val = invoice_line[sum_field]
Expand Down
31 changes: 31 additions & 0 deletions account_invoice_merge/tests/test_account_invoice_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,37 @@ def test_invoice_merge(self):
self.assertEqual(len(end_inv[0].invoice_line_ids), 1)
self.assertEqual(end_inv[0].invoice_line_ids[0].quantity, 2.0)

def test_invoice_merge_disable_merge_lines(self):
self.assertEqual(len(self.invoice1.invoice_line_ids), 1)
self.assertEqual(len(self.invoice2.invoice_line_ids), 1)
invoice_len_args = [
("create_date", ">=", self.now),
("partner_id", "=", self.partner_a.id),
("state", "=", "draft"),
]
start_inv = self.inv_model.search(invoice_len_args)
self.assertEqual(len(start_inv), 5)

wiz = self._get_wizard([self.invoice1.id, self.invoice2.id], create=True)
wiz.disable_merge_lines = True
action = wiz.merge_invoices()

self.assertLessEqual(
{
"type": "ir.actions.act_window",
"binding_view_types": "list,form",
"xml_id": "account.action_move_out_invoice_type",
}.items(),
action.items(),
"There was an error and the two invoices were not merged.",
)

end_inv = self.inv_model.search(invoice_len_args)
self.assertEqual(len(end_inv), 4)
self.assertEqual(len(end_inv[0].invoice_line_ids), 2)
self.assertEqual(end_inv[0].invoice_line_ids[0].quantity, 1.0)
self.assertEqual(end_inv[0].invoice_line_ids[1].quantity, 1.0)

def test_error_check(self):
"""Check"""
# Different partner
Expand Down
5 changes: 4 additions & 1 deletion account_invoice_merge/wizard/invoice_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class InvoiceMerge(models.TransientModel):
)
date_invoice = fields.Date("Invoice Date")
error_message = fields.Text()
disable_merge_lines = fields.Boolean()

@api.model
def default_get(self, default_fields):
Expand Down Expand Up @@ -68,7 +69,9 @@ def merge_invoices(self):
ids = self.env.context.get("active_ids", [])
invoices = self.env["account.move"].browse(ids)
allinvoices = invoices.do_merge(
keep_references=self.keep_references, date_invoice=self.date_invoice
keep_references=self.keep_references,
date_invoice=self.date_invoice,
disable_merge_lines=self.disable_merge_lines,
)
xid = {
"out_invoice": "action_move_out_invoice_type",
Expand Down
4 changes: 3 additions & 1 deletion account_invoice_merge/wizard/invoice_merge_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
* Invoices belong to the same partner.<br />
* Invoices have the same company, partner, address,
currency, journal, salesman, account and type.<br /><br />
Lines will only be merged if:<br />
If you don check "Disable Merge Lines", lines will only be merged if:<br
/>
* Invoice lines are exactly the same except for the
product, quantity and unit.<br />
</p>
<group name="options" colspan="4">
<field name="keep_references" />
<field name="date_invoice" />
<field name="disable_merge_lines" />
</group>
</group>
</group>
Expand Down

0 comments on commit d8a5779

Please sign in to comment.