From 59bc46ed26f6452c954591f84a47ca16372174da Mon Sep 17 00:00:00 2001 From: DavidJForgeFlow Date: Tue, 13 Sep 2022 16:39:05 +0200 Subject: [PATCH] [15.0][FIX] account_move_line_purchase_info: separate vendor bills from journal entries on PO --- .../__manifest__.py | 6 ++- .../models/__init__.py | 1 + .../models/purchase_order.py | 49 ++++++++++++++++++- .../test_account_move_line_purchase_info.py | 18 +++++++ .../views/purchase_order_view.xml | 27 ++++++++++ 5 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 account_move_line_purchase_info/views/purchase_order_view.xml diff --git a/account_move_line_purchase_info/__manifest__.py b/account_move_line_purchase_info/__manifest__.py index 2719082b..7043a023 100644 --- a/account_move_line_purchase_info/__manifest__.py +++ b/account_move_line_purchase_info/__manifest__.py @@ -11,6 +11,10 @@ "category": "Generic", "depends": ["purchase_stock"], "license": "AGPL-3", - "data": ["security/account_security.xml", "views/account_move_view.xml"], + "data": [ + "security/account_security.xml", + "views/account_move_view.xml", + "views/purchase_order_view.xml", + ], "installable": True, } diff --git a/account_move_line_purchase_info/models/__init__.py b/account_move_line_purchase_info/models/__init__.py index 7e113f90..7f54fda9 100644 --- a/account_move_line_purchase_info/models/__init__.py +++ b/account_move_line_purchase_info/models/__init__.py @@ -1,3 +1,4 @@ from . import account_move from . import purchase_order_line from . import stock_move +from . import purchase_order diff --git a/account_move_line_purchase_info/models/purchase_order.py b/account_move_line_purchase_info/models/purchase_order.py index b0be2415..e588dab9 100644 --- a/account_move_line_purchase_info/models/purchase_order.py +++ b/account_move_line_purchase_info/models/purchase_order.py @@ -1,9 +1,26 @@ -from odoo import api, models +from odoo import api, fields, models class PurchaseOrder(models.Model): _inherit = "purchase.order" + @api.depends("order_line.invoice_lines.move_id") + def _compute_journal_entries(self): + for order in self: + journal_entries = order.mapped("order_line.invoice_lines.move_id").filtered( + lambda r: r.move_type == "entry" + ) + order.journal_entry_ids = journal_entries + order.journal_entries_count = len(journal_entries) + + journal_entries_count = fields.Integer(compute="_compute_journal_entries") + journal_entry_ids = fields.Many2many( + comodel_name="account.move", + relation="journal_entries_ids_purchase_order", + compute="_compute_journal_entries", + string="Journal Entries", + ) + @api.depends("order_line.invoice_lines.move_id") def _compute_invoice(self): """Overwritten compute to avoid show all Journal Entries with @@ -14,3 +31,33 @@ class PurchaseOrder(models.Model): ) order.invoice_ids = invoices order.invoice_count = len(invoices) + + def action_view_journal_entries(self, invoices=False): + """This function returns an action that display existing journal entries of + given purchase order ids. When only one found, show the journal entry + immediately. + """ + if not invoices: + self.sudo()._read(["journal_entry_ids"]) + invoices = self.journal_entry_ids + + result = self.env["ir.actions.act_window"]._for_xml_id( + "account.action_move_in_invoice_type" + ) + # choose the view_mode accordingly + if len(invoices) > 1: + result["domain"] = [("id", "in", invoices.ids)] + elif len(invoices) == 1: + res = self.env.ref("account.view_move_form", False) + form_view = [(res and res.id or False, "form")] + if "views" in result: + result["views"] = form_view + [ + (state, view) for state, view in result["views"] if view != "form" + ] + else: + result["views"] = form_view + result["res_id"] = invoices.id + else: + result = {"type": "ir.actions.act_window_close"} + + return result diff --git a/account_move_line_purchase_info/tests/test_account_move_line_purchase_info.py b/account_move_line_purchase_info/tests/test_account_move_line_purchase_info.py index 976ce07b..2db0b4d6 100644 --- a/account_move_line_purchase_info/tests/test_account_move_line_purchase_info.py +++ b/account_move_line_purchase_info/tests/test_account_move_line_purchase_info.py @@ -225,3 +225,21 @@ class TestAccountMoveLinePurchaseInfo(common.TransactionCase): ) name_get_no_ctx = po_line.name_get() self.assertEqual(name_get_no_ctx, [(po_line.id, po_line.name)]) + + def test_purchase_order_with_journal_entries_and_vendor_bills(self): + purchase = self._create_purchase([(self.product, 1)]) + purchase.button_confirm() + purchase._compute_invoice() + purchase._compute_journal_entries() + self.assertEqual(purchase.journal_entry_ids.id, False) + self.assertEqual(purchase.invoice_ids.id, False) + self.assertEqual(purchase.journal_entries_count, 0) + self.assertEqual(purchase.invoice_count, 0) + purchase.picking_ids.move_ids_without_package.quantity_done = 1 + purchase.picking_ids.button_validate() + self.assertEqual(purchase.journal_entries_count, 1) + self.assertEqual(purchase.invoice_count, 0) + purchase.action_create_invoice() + self.assertEqual(purchase.journal_entries_count, 1) + self.assertEqual(purchase.invoice_count, 1) + self.assertNotEqual(purchase.action_view_journal_entries(), None) diff --git a/account_move_line_purchase_info/views/purchase_order_view.xml b/account_move_line_purchase_info/views/purchase_order_view.xml new file mode 100644 index 00000000..1be1f075 --- /dev/null +++ b/account_move_line_purchase_info/views/purchase_order_view.xml @@ -0,0 +1,27 @@ + + + purchase.order.form.journal.entry + purchase.order + + + + + + + +