diff --git a/.gitignore b/.gitignore index 6ec07a054b..ea37c9bd87 100644 --- a/.gitignore +++ b/.gitignore @@ -86,3 +86,5 @@ docs/_build/ # OCA rules !static/lib/ +odoo.conf +coverage_report.txt diff --git a/purchase_operating_unit/README.rst b/purchase_operating_unit/README.rst new file mode 100644 index 0000000000..026d4e1b85 --- /dev/null +++ b/purchase_operating_unit/README.rst @@ -0,0 +1,103 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +================================= +Operating Unit in Purchase Orders +================================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:21d7222687f368a65b0a1ac162b9e04e3a51ce6fa7cc912aff062a4bb6a7f595 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/license-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Foperating--unit-lightgray.png?logo=github + :target: https://github.com/OCA/operating-unit/tree/19.0/purchase_operating_unit + :alt: OCA/operating-unit +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/operating-unit-19-0/operating-unit-19-0-purchase_operating_unit + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/operating-unit&target_branch=19.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module introduces the following features: + +- It introduces the operating unit to the purchase order. +- The operating unit is copied to the invoice. +- It implements user's security rules. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +1. Create a PO: the Default Operating Unit is assigned to the PO. If you + want, you can change to another Operating Unit. +2. Validate the PO: the Operating Unit is propagated to the Invoices. +3. From the invoice, it is not possible to change the Operating Unit, it + has to be the same as the one of the PO. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* ForgeFlow +* Serpent Consulting Services Pvt. Ltd. + +Contributors +------------ + +- Jordi Ballester Alomar +- Aaron Henriquez +- Sudhir Arya +- Nicola Studer +- Nikul Chaudhary +- Kitti U. +- Alan Ramos +- `Heliconia Solutions Pvt. Ltd. `__ + + - Bhavesh Heliconia + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +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. + +This module is part of the `OCA/operating-unit `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/purchase_operating_unit/__init__.py b/purchase_operating_unit/__init__.py new file mode 100644 index 0000000000..8668eed3d1 --- /dev/null +++ b/purchase_operating_unit/__init__.py @@ -0,0 +1,3 @@ +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from . import models +from . import report diff --git a/purchase_operating_unit/__manifest__.py b/purchase_operating_unit/__manifest__.py new file mode 100644 index 0000000000..ea2de5b7d1 --- /dev/null +++ b/purchase_operating_unit/__manifest__.py @@ -0,0 +1,27 @@ +# © 2015-17 ForgeFlow S.L. +# - Jordi Ballester Alomar +# © 2015-17 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya +# © 2026-27 CIT Services - Solomon Prabu +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). + +{ + "name": "Operating Unit in Purchase Orders", + "summary": "Adds the concecpt of operating unit (OU) in purchase order management", + "version": "19.0.1.0.0", + "author": "ForgeFlow, " + "Serpent Consulting Services Pvt. Ltd.," + "Odoo Community Association (OCA)", + "website": "https://github.com/OCA/operating-unit", + "category": "Purchase Management", + "depends": ["purchase", "account_operating_unit"], + "license": "LGPL-3", + "data": [ + "security/purchase_security.xml", + "report/purchase_report_view.xml", + "views/purchase_order_view.xml", + "views/purchase_order_line_view.xml", + "views/account_move_view.xml", + ], + "demo": ["demo/purchase_order_demo.xml"], + "installable": True, +} diff --git a/purchase_operating_unit/demo/purchase_order_demo.xml b/purchase_operating_unit/demo/purchase_order_demo.xml new file mode 100644 index 0000000000..af80cc9aac --- /dev/null +++ b/purchase_operating_unit/demo/purchase_order_demo.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/purchase_operating_unit/i18n/it.po b/purchase_operating_unit/i18n/it.po new file mode 100644 index 0000000000..114b37a5ee --- /dev/null +++ b/purchase_operating_unit/i18n/it.po @@ -0,0 +1,79 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * purchase_operating_unit +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 17.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2024-10-03 09:06+0000\n" +"Last-Translator: mymage \n" +"Language-Team: none\n" +"Language: it\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 5.6.2\n" + +#. module: purchase_operating_unit +#. odoo-python +#: code:addons/purchase_operating_unit/models/purchase_order.py:0 +#, python-format +msgid "" +"Configuration error. The Company in the Purchase Order and in the Operating " +"Unit must be the same." +msgstr "" +"Errore di configurazione. L'azienda nell'ordine di acquisto e nell'unità " +"operativa deve essere la stessa." + +#. module: purchase_operating_unit +#: model:ir.model,name:purchase_operating_unit.model_account_move +msgid "Journal Entry" +msgstr "Registrazione contabile" + +#. module: purchase_operating_unit +#: model:ir.model,name:purchase_operating_unit.model_account_move_line +msgid "Journal Item" +msgstr "Movimento contabile" + +#. module: purchase_operating_unit +#: model:ir.model.fields,field_description:purchase_operating_unit.field_purchase_order__operating_unit_id +#: model:ir.model.fields,field_description:purchase_operating_unit.field_purchase_order_line__operating_unit_id +#: model:ir.model.fields,field_description:purchase_operating_unit.field_purchase_report__operating_unit_id +#: model_terms:ir.ui.view,arch_db:purchase_operating_unit.purchase_order_line_search +#: model_terms:ir.ui.view,arch_db:purchase_operating_unit.view_purchase_order_filter +#: model_terms:ir.ui.view,arch_db:purchase_operating_unit.view_purchase_order_search +msgid "Operating Unit" +msgstr "Unità operativa" + +#. module: purchase_operating_unit +#: model:ir.model,name:purchase_operating_unit.model_purchase_order +msgid "Purchase Order" +msgstr "Ordine di acquisto" + +#. module: purchase_operating_unit +#: model:ir.model,name:purchase_operating_unit.model_purchase_order_line +msgid "Purchase Order Line" +msgstr "Riga ordine di acquisto" + +#. module: purchase_operating_unit +#: model:ir.model,name:purchase_operating_unit.model_purchase_report +msgid "Purchase Report" +msgstr "Resoconto di acquisto" + +#. module: purchase_operating_unit +#: model:ir.model.fields,field_description:purchase_operating_unit.field_purchase_order__requesting_operating_unit_id +msgid "Requesting Operating Unit" +msgstr "Unità operativa richiedente" + +#. module: purchase_operating_unit +#. odoo-python +#: code:addons/purchase_operating_unit/models/account_move.py:0 +#, python-format +msgid "" +"The operating unit of the purchase order must be the same as in the " +"associated invoices." +msgstr "" +"L'unità operativa dell'ordine di acquisto deve essere la stessa delle " +"fatture associate." diff --git a/purchase_operating_unit/i18n/purchase_operating_unit.pot b/purchase_operating_unit/i18n/purchase_operating_unit.pot new file mode 100644 index 0000000000..653a42297e --- /dev/null +++ b/purchase_operating_unit/i18n/purchase_operating_unit.pot @@ -0,0 +1,70 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * purchase_operating_unit +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: purchase_operating_unit +#. odoo-python +#: code:addons/purchase_operating_unit/models/purchase_order.py:0 +msgid "" +"Configuration error. The Company in the Purchase Order and in the Operating " +"Unit must be the same." +msgstr "" + +#. module: purchase_operating_unit +#: model:ir.model,name:purchase_operating_unit.model_account_move +msgid "Journal Entry" +msgstr "" + +#. module: purchase_operating_unit +#: model:ir.model,name:purchase_operating_unit.model_account_move_line +msgid "Journal Item" +msgstr "" + +#. module: purchase_operating_unit +#: model:ir.model.fields,field_description:purchase_operating_unit.field_purchase_order__operating_unit_id +#: model:ir.model.fields,field_description:purchase_operating_unit.field_purchase_order_line__operating_unit_id +#: model:ir.model.fields,field_description:purchase_operating_unit.field_purchase_report__operating_unit_id +#: model_terms:ir.ui.view,arch_db:purchase_operating_unit.purchase_order_line_search +#: model_terms:ir.ui.view,arch_db:purchase_operating_unit.view_purchase_order_filter +#: model_terms:ir.ui.view,arch_db:purchase_operating_unit.view_purchase_order_search +msgid "Operating Unit" +msgstr "" + +#. module: purchase_operating_unit +#: model:ir.model,name:purchase_operating_unit.model_purchase_order +msgid "Purchase Order" +msgstr "" + +#. module: purchase_operating_unit +#: model:ir.model,name:purchase_operating_unit.model_purchase_order_line +msgid "Purchase Order Line" +msgstr "" + +#. module: purchase_operating_unit +#: model:ir.model,name:purchase_operating_unit.model_purchase_report +msgid "Purchase Report" +msgstr "" + +#. module: purchase_operating_unit +#: model:ir.model.fields,field_description:purchase_operating_unit.field_purchase_order__requesting_operating_unit_id +msgid "Requesting Operating Unit" +msgstr "" + +#. module: purchase_operating_unit +#. odoo-python +#: code:addons/purchase_operating_unit/models/account_move.py:0 +msgid "" +"The operating unit of the purchase order must be the same as in the " +"associated invoices." +msgstr "" diff --git a/purchase_operating_unit/models/__init__.py b/purchase_operating_unit/models/__init__.py new file mode 100644 index 0000000000..105a10e6f7 --- /dev/null +++ b/purchase_operating_unit/models/__init__.py @@ -0,0 +1,3 @@ +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from . import purchase_order +from . import account_move diff --git a/purchase_operating_unit/models/account_move.py b/purchase_operating_unit/models/account_move.py new file mode 100644 index 0000000000..a03ceccad2 --- /dev/null +++ b/purchase_operating_unit/models/account_move.py @@ -0,0 +1,43 @@ +# Copyright 2015-17 ForgeFlow S.L. +# - Jordi Ballester Alomar +# Copyright 2015-17 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya +# © 2026-27 CIT Services - Solomon Prabu +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo import api, models +from odoo.exceptions import ValidationError + + +class AccountMove(models.Model): + _inherit = "account.move" + + # Load all unsold PO lines + @api.onchange("purchase_vendor_bill_id", "purchase_id") + def _onchange_purchase_auto_complete(self): + """ + Override to add Operating Unit from Purchase Order to Invoice. + """ + purchase_id = self.purchase_id + if self.purchase_vendor_bill_id.purchase_order_id: + purchase_id = self.purchase_vendor_bill_id.purchase_order_id + if purchase_id and purchase_id.operating_unit_id: + # Assign OU from PO to Invoice + self.operating_unit_id = purchase_id.operating_unit_id.id + return super()._onchange_purchase_auto_complete() + + +class AccountMoveLine(models.Model): + _inherit = "account.move.line" + + @api.constrains("operating_unit_id", "purchase_line_id") + def _check_invoice_ou(self): + for line in self: + if ( + line.purchase_line_id + and line.operating_unit_id != line.purchase_line_id.operating_unit_id + ): + raise ValidationError( + self.env._( + "The operating unit of the purchase order must " + "be the same as in the associated invoices." + ) + ) diff --git a/purchase_operating_unit/models/purchase_order.py b/purchase_operating_unit/models/purchase_order.py new file mode 100644 index 0000000000..1c6402d06f --- /dev/null +++ b/purchase_operating_unit/models/purchase_order.py @@ -0,0 +1,59 @@ +# Copyright 2015-17 ForgeFlow S.L. +# - Jordi Ballester Alomar +# Copyright 2015-17 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya +# © 2026-27 CIT Services - Solomon Prabu +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo import api, fields, models +from odoo.exceptions import ValidationError + + +class PurchaseOrder(models.Model): + _inherit = "purchase.order" + + operating_unit_id = fields.Many2one( + comodel_name="operating.unit", + string="Operating Unit", + default=lambda self: ( + self.env["res.users"]._get_default_operating_unit(self.env.uid) + ), + check_company=True, + ) + + requesting_operating_unit_id = fields.Many2one( + comodel_name="operating.unit", + string="Requesting Operating Unit", + default=lambda self: ( + self.env["res.users"]._get_default_operating_unit(self.env.uid) + ), + check_company=True, + ) + + @api.constrains("operating_unit_id", "company_id") + def _check_company_operating_unit(self): + for record in self: + if ( + record.company_id + and record.operating_unit_id + and record.company_id != record.operating_unit_id.company_id + ): + raise ValidationError( + self.env._( + "Configuration error. The Company in the Purchase Order " + "and in the Operating Unit must be the same." + ) + ) + + def _prepare_invoice(self): + invoice_vals = super()._prepare_invoice() + invoice_vals["operating_unit_id"] = self.operating_unit_id.id + return invoice_vals + + +class PurchaseOrderLine(models.Model): + _inherit = "purchase.order.line" + + operating_unit_id = fields.Many2one( + related="order_id.operating_unit_id", + string="Operating Unit", + check_company=True, + ) diff --git a/purchase_operating_unit/pyproject.toml b/purchase_operating_unit/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/purchase_operating_unit/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/purchase_operating_unit/readme/CONTRIBUTORS.md b/purchase_operating_unit/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..fba2cf27c8 --- /dev/null +++ b/purchase_operating_unit/readme/CONTRIBUTORS.md @@ -0,0 +1,9 @@ +- Jordi Ballester Alomar \<\> +- Aaron Henriquez \<\> +- Sudhir Arya \<\> +- Nicola Studer \<\> +- Nikul Chaudhary \<\> +- Kitti U. \<\> +- Alan Ramos \<\> +- [Heliconia Solutions Pvt. Ltd.](https://www.heliconia.io) + - Bhavesh Heliconia diff --git a/purchase_operating_unit/readme/DESCRIPTION.md b/purchase_operating_unit/readme/DESCRIPTION.md new file mode 100644 index 0000000000..10d7911fa2 --- /dev/null +++ b/purchase_operating_unit/readme/DESCRIPTION.md @@ -0,0 +1,5 @@ +This module introduces the following features: + +- It introduces the operating unit to the purchase order. +- The operating unit is copied to the invoice. +- It implements user's security rules. diff --git a/purchase_operating_unit/readme/USAGE.md b/purchase_operating_unit/readme/USAGE.md new file mode 100644 index 0000000000..80c84082ad --- /dev/null +++ b/purchase_operating_unit/readme/USAGE.md @@ -0,0 +1,5 @@ +1. Create a PO: the Default Operating Unit is assigned to the PO. If + you want, you can change to another Operating Unit. +2. Validate the PO: the Operating Unit is propagated to the Invoices. +3. From the invoice, it is not possible to change the Operating Unit, + it has to be the same as the one of the PO. diff --git a/purchase_operating_unit/report/__init__.py b/purchase_operating_unit/report/__init__.py new file mode 100644 index 0000000000..0be1a37c09 --- /dev/null +++ b/purchase_operating_unit/report/__init__.py @@ -0,0 +1,2 @@ +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from . import purchase_report diff --git a/purchase_operating_unit/report/purchase_report.py b/purchase_operating_unit/report/purchase_report.py new file mode 100644 index 0000000000..6c46e88903 --- /dev/null +++ b/purchase_operating_unit/report/purchase_report.py @@ -0,0 +1,24 @@ +# Copyright 2023 Ecosoft Co., Ltd (http://ecosoft.co.th/) +# © 2026-27 CIT Services - Solomon Prabu +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). + +from odoo import fields, models +from odoo.tools import SQL + + +class PurchaseReport(models.Model): + _inherit = "purchase.report" + + operating_unit_id = fields.Many2one( + comodel_name="operating.unit", + string="Operating Unit", + readonly=True, + ) + + def _select(self): + select_str = super()._select() + return SQL("%s, po.operating_unit_id", select_str) + + def _group_by(self): + group_by_str = super()._group_by() + return SQL("%s, po.operating_unit_id", group_by_str) diff --git a/purchase_operating_unit/report/purchase_report_view.xml b/purchase_operating_unit/report/purchase_report_view.xml new file mode 100644 index 0000000000..db99c04d73 --- /dev/null +++ b/purchase_operating_unit/report/purchase_report_view.xml @@ -0,0 +1,24 @@ + + + + report.purchase.order.search + purchase.report + + + + + + + + + + + diff --git a/purchase_operating_unit/security/purchase_security.xml b/purchase_operating_unit/security/purchase_security.xml new file mode 100644 index 0000000000..bcc10587cb --- /dev/null +++ b/purchase_operating_unit/security/purchase_security.xml @@ -0,0 +1,32 @@ + + + + + + + + + + ['|',('operating_unit_id','=',False),('operating_unit_id','in', operating_unit_ids)] + PO's from allowed operating units + + + + + + + + + ['|',('operating_unit_id','=',False),('operating_unit_id','in', operating_unit_ids)] + PO lines from allowed operating units + + + + + + + diff --git a/purchase_operating_unit/static/description/icon.png b/purchase_operating_unit/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/purchase_operating_unit/static/description/icon.png differ diff --git a/purchase_operating_unit/static/description/index.html b/purchase_operating_unit/static/description/index.html new file mode 100644 index 0000000000..0b9b9e80a5 --- /dev/null +++ b/purchase_operating_unit/static/description/index.html @@ -0,0 +1,456 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

Operating Unit in Purchase Orders

+ +

Beta License: LGPL-3 OCA/operating-unit Translate me on Weblate Try me on Runboat

+

This module introduces the following features:

+
    +
  • It introduces the operating unit to the purchase order.
  • +
  • The operating unit is copied to the invoice.
  • +
  • It implements user’s security rules.
  • +
+

Table of contents

+ +
+

Usage

+
    +
  1. Create a PO: the Default Operating Unit is assigned to the PO. If you +want, you can change to another Operating Unit.
  2. +
  3. Validate the PO: the Operating Unit is propagated to the Invoices.
  4. +
  5. From the invoice, it is not possible to change the Operating Unit, it +has to be the same as the one of the PO.
  6. +
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • ForgeFlow
  • +
  • Serpent Consulting Services Pvt. Ltd.
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

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.

+

This module is part of the OCA/operating-unit project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+
+ + diff --git a/purchase_operating_unit/tests/__init__.py b/purchase_operating_unit/tests/__init__.py new file mode 100644 index 0000000000..df19e1685a --- /dev/null +++ b/purchase_operating_unit/tests/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2026-27 CIT Services - Solomon Prabu +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from . import test_purchase_operating_unit +from . import test_po_security diff --git a/purchase_operating_unit/tests/test_po_security.py b/purchase_operating_unit/tests/test_po_security.py new file mode 100644 index 0000000000..dd27ddd70a --- /dev/null +++ b/purchase_operating_unit/tests/test_po_security.py @@ -0,0 +1,42 @@ +# Copyright 2015-17 ForgeFlow S.L. +# - Jordi Ballester Alomar +# Copyright 2015-17 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya +# © 2026-27 CIT Services - Solomon Prabu +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from . import test_purchase_operating_unit as test_po_ou # noqa + + +class TestPoSecurity(test_po_ou.TestPurchaseOperatingUnit): + def test_po_ou_security(self): + """Test Security of Purchase Operating Unit""" + # User 2 is only assigned to Operating Unit 2, and cannot list + # purchase orders from Operating Unit 1. + po_ids = ( + self.PurchaseOrder.with_user(self.user2) + .search([("operating_unit_id", "=", self.ou1.id)]) + .ids + ) + self.assertEqual(po_ids, []) + invoice = self._create_invoice(self.purchase1.with_user(self.user1)) + # User 2 cannot list the invoice that was created from PO 1 + invoice_ids = ( + self.AccountInvoice.with_user(self.user2) + .search([("id", "=", invoice.id)]) + .ids + ) + self.assertEqual(invoice_ids, []) + # User 1 is assigned to Operating Unit 1, and can list + # the purchase order 1 from Operating Unit 1. + po_ids = ( + self.PurchaseOrder.with_user(self.user1) + .search([("operating_unit_id", "=", self.ou1.id)]) + .ids + ) + self.assertNotEqual(po_ids, []) + # User 1 can list the invoice that was created from PO 2 + invoice_ids = ( + self.AccountInvoice.with_user(self.user1) + .search([("id", "=", invoice.id)]) + .ids + ) + self.assertNotEqual(invoice_ids, []) diff --git a/purchase_operating_unit/tests/test_purchase_operating_unit.py b/purchase_operating_unit/tests/test_purchase_operating_unit.py new file mode 100644 index 0000000000..74636e5d4d --- /dev/null +++ b/purchase_operating_unit/tests/test_purchase_operating_unit.py @@ -0,0 +1,131 @@ +# Copyright 2015-17 ForgeFlow S.L. +# - Jordi Ballester Alomar +# Copyright 2015-17 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya +# © 2026-27 CIT Services - Solomon Prabu +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo import fields +from odoo.exceptions import ValidationError +from odoo.fields import Command +from odoo.tests import Form + +from odoo.addons.operating_unit.tests.common import OperatingUnitCommon + + +class TestPurchaseOperatingUnit(OperatingUnitCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.PurchaseOrder = cls.env["purchase.order"] + cls.AccountInvoice = cls.env["account.move"] + cls.AccountAccount = cls.env["account.account"] + # groups + cls.group_purchase_user = cls.env.ref("purchase.group_purchase_user") + # Products + cls.product1 = cls.env["product.product"].create( + {"name": "Product 1", "type": "consu"} + ) + cls.product2 = cls.env["product.product"].create( + {"name": "Product 2", "type": "consu"} + ) + cls.product3 = cls.env["product.product"].create( + {"name": "Product 3", "type": "consu"} + ) + (cls.product1 | cls.product2).write({"purchase_method": "purchase"}) + # Account + cls.account = cls.AccountAccount.search( + [("account_type", "=", "liability_payable")], limit=1 + ) + # Update users + cls.user1.write( + { + "group_ids": [ + Command.link(cls.group_purchase_user.id), + ], + } + ) + cls.user2.write( + { + "group_ids": [ + Command.link(cls.group_purchase_user.id), + ], + "operating_unit_ids": [Command.set([cls.b2b.id])], + } + ) + cls.partner1 = cls.env["res.partner"].create({"name": "Partner 1"}) + cls.purchase1 = cls._create_purchase( + cls.user1, + [(cls.product1, 1000), (cls.product2, 500), (cls.product3, 800)], + ) + cls.purchase1.with_user(cls.user1).button_confirm() + + @classmethod + def _create_purchase(cls, user_id, line_products): + """Create a purchase order. + ``line_products`` is a list of tuple [(product, qty)] + """ + lines = [] + for product, qty in line_products: + line_values = { + "name": product.name, + "product_id": product.id, + "product_qty": qty, + "qty_received_manual": qty, + "product_uom_id": product.uom_id.id, + "price_unit": 50, + "date_planned": fields.Datetime.now(), + } + lines.append(Command.create(line_values)) + purchase = cls.PurchaseOrder.with_user(user_id).create( + { + "operating_unit_id": cls.ou1.id, + "requesting_operating_unit_id": cls.ou1.id, + "partner_id": cls.partner1.id, + "order_line": lines, + "company_id": cls.company.id, + } + ) + return purchase + + @classmethod + def _create_invoice(cls, purchase): + """Create a vendor invoice for the purchase order.""" + purchase.action_create_invoice() + return purchase.invoice_ids + + def test_01_purchase_operating_unit(self): + self.purchase1.button_cancel() + self.purchase1.button_draft() + # Check change operating unit in purchase + with self.assertRaises(ValidationError): + self.b2b.company_id = self.company_2 + with Form(self.purchase1) as po: + po.operating_unit_id = self.b2b + self.purchase1.with_user(self.user1).button_confirm() + invoice = self._create_invoice(self.purchase1) + self.assertEqual(invoice.operating_unit_id, self.purchase1.operating_unit_id) + self.assertEqual( + invoice.invoice_line_ids[0].operating_unit_id, + invoice.invoice_line_ids[0].purchase_line_id.operating_unit_id, + ) + # Check change operating unit in invoice line != purchase line, + # it should error. + with self.assertRaises(ValidationError): + with Form(invoice.invoice_line_ids[0]) as line: + line.operating_unit_id = self.b2b + + def test_02_purchase_report(self): + """Test the purchase report with operating unit.""" + # Generate the report to trigger the _select and _group_by methods + report = self.env["purchase.report"] + # Read records with a filter on operating unit to force using the custom methods + report_records = report.search([("operating_unit_id", "=", self.ou1.id)]) + + # Verify that records exist with the correct operating unit + self.assertTrue(report_records, "Purchase report should contain records") + # If records exist, verify at least one has the expected operating unit + if report_records: + self.assertEqual( + report_records[0].operating_unit_id, + self.ou1, + "Purchase report record should have the correct operating unit", + ) diff --git a/purchase_operating_unit/views/account_move_view.xml b/purchase_operating_unit/views/account_move_view.xml new file mode 100644 index 0000000000..ef275fe063 --- /dev/null +++ b/purchase_operating_unit/views/account_move_view.xml @@ -0,0 +1,16 @@ + + + + + account.move.form.inherit + account.move + + + + ['|',('operating_unit_id', '=', operating_unit_id),('operating_unit_id', '=', False)] + + + + diff --git a/purchase_operating_unit/views/purchase_order_line_view.xml b/purchase_operating_unit/views/purchase_order_line_view.xml new file mode 100644 index 0000000000..32c4b0b2d3 --- /dev/null +++ b/purchase_operating_unit/views/purchase_order_line_view.xml @@ -0,0 +1,50 @@ + + + + purchase_order_line_tree + purchase.order.line + + + + + + + + + purchase_order_line_form + purchase.order.line + + + + + + + + + purchase_order_line_search + purchase.order.line + + + + + + + + + + + diff --git a/purchase_operating_unit/views/purchase_order_view.xml b/purchase_operating_unit/views/purchase_order_view.xml new file mode 100644 index 0000000000..3c5c8fdef3 --- /dev/null +++ b/purchase_operating_unit/views/purchase_order_view.xml @@ -0,0 +1,67 @@ + + + + purchase_order_tree + purchase.order + + + + + + + + + purchase_order_form + purchase.order + + + + + + + + + {'default_state': 'draft', 'operating_unit_id': operating_unit_id} + + + + + view_purchase_order_filter + purchase.order + + + + + + + + + + +