Skip to content

Commit

Permalink
[IMP] account_invoice_merge: Add message after invoice merging
Browse files Browse the repository at this point in the history
  • Loading branch information
barkat-matthias committed Feb 24, 2025
1 parent bc4c069 commit 17dd546
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 20 deletions.
28 changes: 14 additions & 14 deletions account_invoice_merge/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ Account Invoice Merge
This module adds an action in the invoices list to merge invoices. Here
are the conditions to allow merging:

- Type should be the same (Customer Invoice, Supplier Invoice, Customer
Refund or Supplier Refund)
- Partner should be the same
- Currency should be the same
- Account receivable account should be the same
- Type should be the same (Customer Invoice, Supplier Invoice, Customer
Refund or Supplier Refund)
- Partner should be the same
- Currency should be the same
- Account receivable account should be the same

**Table of contents**

Expand Down Expand Up @@ -79,23 +79,23 @@ Authors
Contributors
------------

- Ian Li <[email protected]>
- Ian Li <[email protected]>

- Cédric Pigeon <[email protected]>
- Cédric Pigeon <[email protected]>

- Lois Rilo <[email protected]>
- Lois Rilo <[email protected]>

- Miquel Raïch <[email protected]>
- Miquel Raïch <[email protected]>

- Sylvain Van Hoof <[email protected]>
- Sylvain Van Hoof <[email protected]>

- Freni Patel <[email protected]>
- Freni Patel <[email protected]>

- Marcos Oitabén <[email protected]>
- Marcos Oitabén <[email protected]>

- `Trobz <https://trobz.com>`__:
- `Trobz <https://trobz.com>`__:

- Nguyễn Minh Chiến <[email protected]>
- Nguyễn Minh Chiến <[email protected]>

Other credits
-------------
Expand Down
48 changes: 47 additions & 1 deletion account_invoice_merge/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import numbers

from odoo import api, models
from odoo import _, api, models
from odoo.tools import float_is_zero


Expand Down Expand Up @@ -229,8 +229,54 @@ def do_merge(
old_invoices = self.env["account.move"].browse(old_ids)
old_invoices.with_context(is_merge=True).button_cancel()
self.merge_callback(invoices_info, old_invoices)
for new_invoice_id, old_invoice_ids in invoices_info.items():
new_invoice = self.browse(new_invoice_id)
old_invoices = self.browse(old_invoice_ids)
# 1. message post on new invoice to indicate
# it has been created as result of merging original invoices
new_invoice.post_merge_message(old_invoices)
# 2. fill char/text fields as concatenation
# of original invoices `object_entry` field
new_invoice.post_process_fields(old_invoices)
return invoices_info

def _get_post_merge_message_invoice_identifier(self):
"""
Return the identifier of the invoice to be used in the post merge message
"""
return f"{self._name}({self.id})"

def post_merge_message(self, invoice_list):
message_body = _("Invoice merged from :")
message_body += "<ul>"
for invoice in invoice_list:
invoice_url = f"/web#id={invoice.id}&model=account.move&view_type=form"
message_body += (
f'<li><a href="{invoice_url}" >'
f"{invoice._get_post_merge_message_invoice_identifier()}"
f" - {invoice.amount_total}</a></li>"
)
message_body += "</ul>"

self.message_post(message_type="notification", body=message_body)

@api.model
def _get_fields_to_concatenate_after_merge(self):
"""
Return the list of fields to concatenate after merge
For example you merge two invoices with different `entry_object` field
The result invoice will have the `entry_object` field set to
`invoice1.entry_object // invoice2.entry_object`
"""
return []

def post_process_fields(self, invoice_list):
fields_to_concatenate = self._get_fields_to_concatenate_after_merge()
for field in fields_to_concatenate:
self[field] = " // ".join(
[invoice[field] for invoice in invoice_list if invoice[field]]
)

@staticmethod
def order_line_update_invoice_lines(todos, all_old_inv_line):
for todo in todos:
Expand Down
12 changes: 7 additions & 5 deletions account_invoice_merge/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand All @@ -9,10 +8,11 @@

/*
:Author: David Goodger ([email protected])
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Copyright: This stylesheet has been placed in the public domain.
Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.
See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -275,7 +275,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: grey; } /* line numbers */
pre.code .ln { color: gray; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -301,7 +301,7 @@
span.pre {
white-space: pre }

span.problematic {
span.problematic, pre.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -453,7 +453,9 @@ <h2><a class="toc-backref" href="#toc-entry-7">Other credits</a></h2>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-8">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
</a>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
Expand Down
91 changes: 91 additions & 0 deletions account_invoice_merge/tests/test_account_invoice_merge.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# (http://www.eficent.com)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from unittest import mock
from unittest.mock import patch

from odoo import Command, fields
from odoo.tests import tagged

Expand Down Expand Up @@ -69,6 +72,23 @@ def _get_wizard(self, active_ids, create=False):
wiz = wiz.create({})
return wiz

def _create_invoice(self, partner, name, journal=False, move_type=False):
if not journal:
journal = self.env["account.journal"].search(
[("type", "=", "sale")], limit=1
)
if not move_type:
move_type = "out_invoice"
invoice = self.inv_model.create(
{
"partner_id": partner.id,
"name": name,
"move_type": move_type,
"journal_id": journal.id,
}
)
return invoice

def test_invoice_merge(self):
self.assertEqual(len(self.invoice1.invoice_line_ids), 1)
self.assertEqual(len(self.invoice2.invoice_line_ids), 1)
Expand Down Expand Up @@ -456,3 +476,74 @@ def test_callback_same_purchase_order(self):
for line in purchase_order.order_line:
self.assertEqual(line.qty_received, 5)
self.assertEqual(line.qty_invoiced, 4)

@mock.patch(
"odoo.addons.account_invoice_merge.models.account_move.AccountMove.post_merge_message"
)
@mock.patch(
"odoo.addons.account_invoice_merge.models.account_move.AccountMove.post_process_fields"
)
def test_account_invoice_merge_4(
self, mock_post_process_fields, mock_post_merge_message
):
invoices = self.invoice1 | self.invoice2
wiz_id = self.wiz.with_context(
active_ids=invoices.ids,
active_model=invoices._name,
).create({})
wiz_id.get_view()
wiz_id.merge_invoices()
mock_post_process_fields.assert_called_once_with(invoices)
mock_post_merge_message.assert_called_once_with(invoices)

def test_get_post_merge_message_invoice_identifier(self):
"""Test that the post-merge message invoice identifier is correct."""
self.assertEqual(
self.invoice1._get_post_merge_message_invoice_identifier(),
f"account.move({self.invoice1.id})",
)

def test_post_merge_message(self):
"""Test that the post-merge message is displayed when merging invoices."""
invoices = self.invoice1 | self.invoice2
invoice_3 = self._create_invoice(self.partner_a, "C")
message_li_invoice_1 = (
f'<li><a href="/web#id={self.invoice1.id}'
f'&model=account.move&view_type=form" >'
f"{self.invoice1._get_post_merge_message_invoice_identifier()}"
f" - "
f"{self.invoice1.amount_total}</a></li>"
)

message_li_invoice_2 = (
f'<li><a href="/web#id={self.invoice2.id}'
f'&model=account.move&view_type=form" >'
f"{self.invoice2._get_post_merge_message_invoice_identifier()}"
f" - "
f"{self.invoice2.amount_total}</a></li>"
)
message_body = (
f"Invoice merged from :"
f"<ul>{message_li_invoice_1}{message_li_invoice_2}</ul>"
)
with patch(
"odoo.addons.mail.models.mail_thread.MailThread.message_post"
) as mock_message_post:
invoice_3.post_merge_message(invoices)
self.assertEqual(mock_message_post.call_count, 1)
self.assertEqual(mock_message_post.call_args[1].get("body"), message_body)

@patch(
(
"odoo.addons.account_invoice_merge.models.account_move.AccountMove."
"_get_fields_to_concatenate_after_merge"
),
return_value=["ref"],
)
def test_post_process_fields(self, mock_get_fields_to_concatenate_after_merge):
self.invoice1.ref = "Blabla1"
self.invoice2.ref = "Blabla2"
invoices = self.invoice1 | self.invoice2
invoice_3 = self._create_invoice(self.partner_a, "C")
invoice_3.post_process_fields(invoices)
self.assertEqual(invoice_3.ref, "Blabla1 // Blabla2")

0 comments on commit 17dd546

Please sign in to comment.