diff --git a/purchase_operating_unit/README.rst b/purchase_operating_unit/README.rst new file mode 100644 index 0000000000..c579c23cd9 --- /dev/null +++ b/purchase_operating_unit/README.rst @@ -0,0 +1,97 @@ +================================= +Operating Unit in Purchase Orders +================================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:21e7d9b3681736a1b5892f6dbda51bc620acedda3590df11bf4ed3e7cb3174be + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/licence-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/16.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-16-0/operating-unit-16-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=16.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 +===== + +#. Create a PO: the Default Operating Unit is assigned to the PO. If you want, + you can change to another Operating Unit. +#. Validate the PO: the Operating Unit is propagated to the Invoices. +#. 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 +* Nopparit S. + +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..8163d281e7 --- /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 +# 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": "16.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/purchase_operating_unit.pot b/purchase_operating_unit/i18n/purchase_operating_unit.pot new file mode 100644 index 0000000000..93b9ca291c --- /dev/null +++ b/purchase_operating_unit/i18n/purchase_operating_unit.pot @@ -0,0 +1,77 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * purchase_operating_unit +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.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 +#: 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 "" + +#. 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.fields,field_description:purchase_operating_unit.field_account_bank_statement_line__purchase_ou_domain +#: model:ir.model.fields,field_description:purchase_operating_unit.field_account_move__purchase_ou_domain +#: model:ir.model.fields,field_description:purchase_operating_unit.field_account_payment__purchase_ou_domain +msgid "Purchase Ou Domain" +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 +#: 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 "" 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..17e4933d6e --- /dev/null +++ b/purchase_operating_unit/models/account_move.py @@ -0,0 +1,55 @@ +# Copyright 2015-17 ForgeFlow S.L. +# - Jordi Ballester Alomar +# Copyright 2015-17 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya +# 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 AccountMove(models.Model): + _inherit = "account.move" + + purchase_ou_domain = fields.Many2many( + comodel_name="purchase.order", compute="_compute_purchase_ou_domain" + ) + + # 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() + + @api.depends("operating_unit_id") + def _compute_purchase_ou_domain(self): + for rec in self: + rec.purchase_ou_domain = ( + self.env["purchase.order"] + .sudo() + .search([("operating_unit_id", "=", rec.operating_unit_id.id)]) + ) + + +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( + _( + "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..07810dfe0a --- /dev/null +++ b/purchase_operating_unit/models/purchase_order.py @@ -0,0 +1,62 @@ +# Copyright 2015-17 ForgeFlow S.L. +# - Jordi Ballester Alomar +# Copyright 2015-17 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya +# 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" + + READONLY_STATES = { + "purchase": [("readonly", True)], + "done": [("readonly", True)], + "cancel": [("readonly", True)], + } + + operating_unit_id = fields.Many2one( + comodel_name="operating.unit", + string="Operating Unit", + states=READONLY_STATES, + default=lambda self: ( + self.env["res.users"].operating_unit_default_get(self.env.uid) + ), + ) + + requesting_operating_unit_id = fields.Many2one( + comodel_name="operating.unit", + string="Requesting Operating Unit", + states=READONLY_STATES, + default=lambda self: ( + self.env["res.users"].operating_unit_default_get(self.env.uid) + ), + ) + + @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( + _( + "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" + ) diff --git a/purchase_operating_unit/readme/CONTRIBUTORS.rst b/purchase_operating_unit/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..4363b1d574 --- /dev/null +++ b/purchase_operating_unit/readme/CONTRIBUTORS.rst @@ -0,0 +1,8 @@ +* Jordi Ballester Alomar +* Aaron Henriquez +* Sudhir Arya +* Nicola Studer +* Nikul Chaudhary +* Kitti U. +* Alan Ramos +* Nopparit S. diff --git a/purchase_operating_unit/readme/DESCRIPTION.rst b/purchase_operating_unit/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..10d7911fa2 --- /dev/null +++ b/purchase_operating_unit/readme/DESCRIPTION.rst @@ -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.rst b/purchase_operating_unit/readme/USAGE.rst new file mode 100644 index 0000000000..fb2deacb52 --- /dev/null +++ b/purchase_operating_unit/readme/USAGE.rst @@ -0,0 +1,5 @@ +#. Create a PO: the Default Operating Unit is assigned to the PO. If you want, + you can change to another Operating Unit. +#. Validate the PO: the Operating Unit is propagated to the Invoices. +#. 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..9193741115 --- /dev/null +++ b/purchase_operating_unit/report/purchase_report.py @@ -0,0 +1,24 @@ +# Copyright 2023 Ecosoft Co., Ltd (http://ecosoft.co.th/) +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). + +from odoo import fields, models + + +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() + select_str += """, po.operating_unit_id""" + return select_str + + def _group_by(self): + group_by_str = super()._group_by() + group_by_str += """, po.operating_unit_id""" + return 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..970d195ac3 --- /dev/null +++ b/purchase_operating_unit/security/purchase_security.xml @@ -0,0 +1,31 @@ + + + + + + + + + ['|',('operating_unit_id','=',False),('operating_unit_id','in', user.operating_unit_ids.ids)] + PO's from allowed operating units + + + + + + + + + ['|',('operating_unit_id','=',False),('operating_unit_id','in', user.operating_unit_ids.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..a8a8fd6029 --- /dev/null +++ b/purchase_operating_unit/static/description/index.html @@ -0,0 +1,447 @@ + + + + + +Operating Unit in Purchase Orders + + + +
+

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..9c86acd517 --- /dev/null +++ b/purchase_operating_unit/tests/__init__.py @@ -0,0 +1,3 @@ +# 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..ae0a82f5e8 --- /dev/null +++ b/purchase_operating_unit/tests/test_po_security.py @@ -0,0 +1,40 @@ +# Copyright 2015-17 ForgeFlow S.L. +# - Jordi Ballester Alomar +# Copyright 2015-17 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya +# 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_id) + .search([("operating_unit_id", "=", self.ou1.id)]) + .ids + ) + self.assertEqual(po_ids, []) + # User 2 cannot list the invoice that was created from PO 1 + invoice_ids = ( + self.AccountInvoice.with_user(self.user2_id) + .search([("id", "=", self.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_id) + .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_id) + .search([("id", "=", self.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..3cb596fd8e --- /dev/null +++ b/purchase_operating_unit/tests/test_purchase_operating_unit.py @@ -0,0 +1,143 @@ +# Copyright 2015-17 ForgeFlow S.L. +# - Jordi Ballester Alomar +# Copyright 2015-17 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +import time + +from odoo.exceptions import ValidationError +from odoo.tests import Form, common +from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT + + +class TestPurchaseOperatingUnit(common.TransactionCase): + def setUp(self): + super().setUp() + self.ResUsers = self.env["res.users"] + self.PurchaseOrder = self.env["purchase.order"] + self.AccountInvoice = self.env["account.move"] + self.AccountAccount = self.env["account.account"] + # company + self.company = self.env.ref("base.main_company") + # groups + self.group_purchase_user = self.env.ref("purchase.group_purchase_user") + # Main Operating Unit + self.ou1 = self.env.ref("operating_unit.main_operating_unit") + # B2B Operating Unit + self.b2b = self.env.ref("operating_unit.b2b_operating_unit") + # Partner + self.partner1 = self.env.ref("base.res_partner_1") + # Products + self.product1 = self.env.ref("product.product_product_7") + self.product2 = self.env.ref("product.product_product_9") + self.product3 = self.env.ref("product.product_product_11") + # Account + payable_acc_type = self.env.ref("account.data_account_type_payable").id + self.account = self.AccountAccount.search( + [("user_type_id", "=", payable_acc_type)], limit=1 + ) + # Create users + self.user1_id = self._create_user( + "user_1", + [self.group_purchase_user], + self.company, + [self.ou1], + ) + self.user2_id = self._create_user( + "user_2", + [self.group_purchase_user], + self.company, + [self.b2b], + ) + self.purchase1 = self._create_purchase( + self.user1_id, + [(self.product1, 1000), (self.product2, 500), (self.product3, 800)], + ) + self.purchase1.with_user(self.user1_id).button_confirm() + self.invoice = self._create_invoice(self.purchase1, self.partner1, self.account) + + def _create_user(self, login, groups, company, operating_units): + """Create a user.""" + group_ids = [group.id for group in groups] + user = self.ResUsers.with_context(**{"no_reset_password": True}).create( + { + "name": "Chicago Purchase User", + "login": login, + "password": "demo", + "email": "chicago@yourcompany.com", + "company_id": company.id, + "company_ids": [(4, company.id)], + "operating_unit_ids": [(4, ou.id) for ou in operating_units], + "groups_id": [(6, 0, group_ids)], + } + ) + return user.id + + def _create_purchase(self, 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, + "product_uom": product.uom_id.id, + "price_unit": 50, + "date_planned": time.strftime(DEFAULT_SERVER_DATETIME_FORMAT), + } + lines.append((0, 0, line_values)) + purchase = self.PurchaseOrder.with_user(user_id).create( + { + "operating_unit_id": self.ou1.id, + "requesting_operating_unit_id": self.ou1.id, + "partner_id": self.partner1.id, + "order_line": lines, + "company_id": self.company.id, + } + ) + return purchase + + def _create_invoice(self, purchase, partner, account): + """Create a vendor invoice for the purchase order.""" + invoice_vals = { + "purchase_id": purchase.id, + "partner_id": partner.id, + "move_type": "in_invoice", + } + purchase_context = { + "active_id": purchase.id, + "active_ids": purchase.ids, + "active_model": "purchase.order", + } + res = ( + self.env["account.move"] + .with_context(**purchase_context) + .create(invoice_vals) + ) + return res + + 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 = False + with Form(self.purchase1) as po: + po.operating_unit_id = self.b2b + self.purchase1.with_user(self.user1_id).button_confirm() + # Create Vendor Bill + f = Form(self.env["account.move"].with_context(default_move_type="in_invoice")) + f.partner_id = self.purchase1.partner_id + f.purchase_id = self.purchase1 + invoice = f.save() + 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 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..028c677b79 --- /dev/null +++ b/purchase_operating_unit/views/account_move_view.xml @@ -0,0 +1,17 @@ + + + + + account.move.form.inherit + account.move + + + + [('id', 'in', purchase_ou_domain)] + + + + + + + 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..97ceea8898 --- /dev/null +++ b/purchase_operating_unit/views/purchase_order_view.xml @@ -0,0 +1,62 @@ + + + + purchase_order_tree + purchase.order + + + + + + + + + purchase_order_form + purchase.order + + + + + + + + operating_unit.group_multi_operating_unit + {'default_state': 'draft', 'operating_unit_id': operating_unit_id} + + + + + view_purchase_order_filter + purchase.order + + + + + + + + + + + diff --git a/setup/purchase_operating_unit/odoo/addons/purchase_operating_unit b/setup/purchase_operating_unit/odoo/addons/purchase_operating_unit new file mode 120000 index 0000000000..4695973c4e --- /dev/null +++ b/setup/purchase_operating_unit/odoo/addons/purchase_operating_unit @@ -0,0 +1 @@ +../../../../purchase_operating_unit \ No newline at end of file diff --git a/setup/purchase_operating_unit/setup.py b/setup/purchase_operating_unit/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/purchase_operating_unit/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)