diff --git a/purchase_mto_owner/README.rst b/purchase_mto_owner/README.rst new file mode 100644 index 00000000..a26bce1d --- /dev/null +++ b/purchase_mto_owner/README.rst @@ -0,0 +1,79 @@ +================== +Purchase MTO Owner +================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:dfd4619508fb207d098b536948205b8ba1506f569a9cbcaecd09c00c443a628b + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fpurchase--workflow-lightgray.png?logo=github + :target: https://github.com/OCA/purchase-workflow/tree/16.0/purchase_mto_owner + :alt: OCA/purchase-workflow +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/purchase-workflow-16-0/purchase-workflow-16-0-purchase_mto_owner + :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/purchase-workflow&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module propagates the owner_id from a source document to new +Purchase Order created by the stock rule. + +**Table of contents** + +.. contents:: + :local: + +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 +------- + +* Quartile + +Contributors +------------ + +- `Quartile `__: + + - Aung Ko Ko Lin + +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/purchase-workflow `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/purchase_mto_owner/__init__.py b/purchase_mto_owner/__init__.py new file mode 100644 index 00000000..0650744f --- /dev/null +++ b/purchase_mto_owner/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/purchase_mto_owner/__manifest__.py b/purchase_mto_owner/__manifest__.py new file mode 100644 index 00000000..a3536f47 --- /dev/null +++ b/purchase_mto_owner/__manifest__.py @@ -0,0 +1,12 @@ +# Copyright 2024 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Purchase MTO Owner", + "version": "16.0.1.0.0", + "author": "Quartile, Odoo Community Association (OCA)", + "category": "Purchase Management", + "website": "https://github.com/OCA/purchase-workflow", + "depends": ["purchase_order_owner"], + "license": "AGPL-3", + "installable": True, +} diff --git a/purchase_mto_owner/models/__init__.py b/purchase_mto_owner/models/__init__.py new file mode 100644 index 00000000..55eae172 --- /dev/null +++ b/purchase_mto_owner/models/__init__.py @@ -0,0 +1 @@ +from . import stock_rule diff --git a/purchase_mto_owner/models/stock_rule.py b/purchase_mto_owner/models/stock_rule.py new file mode 100644 index 00000000..b37308ee --- /dev/null +++ b/purchase_mto_owner/models/stock_rule.py @@ -0,0 +1,36 @@ +# Copyright 2024 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + + +class StockRule(models.Model): + _inherit = "stock.rule" + + def _make_po_get_domain(self, company_id, values, partner): + domain = super()._make_po_get_domain(company_id, values, partner) + move_dest_recs = values.get("move_dest_ids") + if move_dest_recs: + origin_move = move_dest_recs[0] + if origin_move.restrict_partner_id: + domain += (("owner_id", "=", origin_move.restrict_partner_id.id),) + return domain + + def _prepare_purchase_order(self, company_id, origins, values): + vals = super(StockRule, self)._prepare_purchase_order( + company_id, origins, values + ) + values = values[0] + move_dest = values.get("move_dest_ids") + if move_dest: + # TODO: Should we simply get restrict_partner_id from move_dest? + if move_dest.picking_id.owner_id: + vals["owner_id"] = move_dest.picking_id.owner_id.id + else: + # Handle the case where mrp_production_ids exists + mrp_productions = getattr( + move_dest.group_id, "mrp_production_ids", None + ) + if mrp_productions: + vals["owner_id"] = mrp_productions[0].owner_id.id + return vals diff --git a/purchase_mto_owner/readme/CONTRIBUTORS.md b/purchase_mto_owner/readme/CONTRIBUTORS.md new file mode 100644 index 00000000..faae3280 --- /dev/null +++ b/purchase_mto_owner/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- [Quartile](https://www.quartile.co): + - Aung Ko Ko Lin diff --git a/purchase_mto_owner/readme/DESCRIPTION.md b/purchase_mto_owner/readme/DESCRIPTION.md new file mode 100644 index 00000000..621a6116 --- /dev/null +++ b/purchase_mto_owner/readme/DESCRIPTION.md @@ -0,0 +1 @@ +This module propagates the owner_id from a source document to new Purchase Order created by the stock rule. diff --git a/purchase_mto_owner/static/description/index.html b/purchase_mto_owner/static/description/index.html new file mode 100644 index 00000000..f2f33a6b --- /dev/null +++ b/purchase_mto_owner/static/description/index.html @@ -0,0 +1,424 @@ + + + + + +Purchase MTO Owner + + + +
+

Purchase MTO Owner

+ + +

Beta License: AGPL-3 OCA/purchase-workflow Translate me on Weblate Try me on Runboat

+

This module propagates the owner_id from a source document to new +Purchase Order created by the stock rule.

+

Table of contents

+ +
+

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

+
    +
  • Quartile
  • +
+
+
+

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/purchase-workflow project on GitHub.

+

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

+
+
+
+ + diff --git a/purchase_mto_owner/tests/__init__.py b/purchase_mto_owner/tests/__init__.py new file mode 100644 index 00000000..17cefdc4 --- /dev/null +++ b/purchase_mto_owner/tests/__init__.py @@ -0,0 +1 @@ +from . import test_purchase_mto_owner diff --git a/purchase_mto_owner/tests/test_purchase_mto_owner.py b/purchase_mto_owner/tests/test_purchase_mto_owner.py new file mode 100644 index 00000000..b0ba489b --- /dev/null +++ b/purchase_mto_owner/tests/test_purchase_mto_owner.py @@ -0,0 +1,68 @@ +# Copyright 2024 Quartile Limited +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import Command +from odoo.tests.common import TransactionCase + + +class TestPurchaseMtoOwner(TransactionCase): + @classmethod + def setUpClass(cls): + super(TestPurchaseMtoOwner, cls).setUpClass() + # Get the MTO route and activate it if necessary + mto_route = cls.env.ref("stock.route_warehouse0_mto") + purchase_route = cls.env.ref("purchase_stock.route_warehouse0_buy") + if not mto_route.active: + mto_route.write({"active": True}) + # Create a product + cls.product = cls.env["product.product"].create( + { + "name": "Test", + "type": "product", + "route_ids": [Command.set([mto_route.id, purchase_route.id])], + } + ) + # Create a vendor + cls.vendor = cls.env["res.partner"].create({"name": "Vendor"}) + # Link the vendor to the product + cls.env["product.supplierinfo"].create( + { + "partner_id": cls.vendor.id, + "product_tmpl_id": cls.product.product_tmpl_id.id, + } + ) + cls.owner_id = cls.env["res.partner"].create({"name": "Owner"}) + + def test_purchase_owner_id(self): + # Create an outgoing stock picking for the product + stock_location = self.env.ref("stock.stock_location_stock") + customer_location = self.env.ref("stock.stock_location_customers") + picking_type_out = self.env.ref("stock.picking_type_out") + stock_picking = self.env["stock.picking"].create( + { + "picking_type_id": picking_type_out.id, + "location_id": stock_location.id, + "location_dest_id": customer_location.id, + "owner_id": self.owner_id.id, + "move_ids": [ + Command.create( + { + "name": "test: move out", + "product_id": self.product.id, + "product_uom_qty": 10, + "procure_method": "make_to_order", + "product_uom": self.product.uom_id.id, + "location_id": stock_location.id, + "location_dest_id": customer_location.id, + } + ) + ], + } + ) + # Confirm the stock picking + stock_picking.action_confirm() + purchase_order = self.env["purchase.order"].search( + [("partner_id", "=", self.vendor.id)] + ) + self.assertTrue(purchase_order, "No purchase order created.") + self.assertEqual(purchase_order.owner_id, self.owner_id) diff --git a/setup/purchase_mto_owner/odoo/addons/purchase_mto_owner b/setup/purchase_mto_owner/odoo/addons/purchase_mto_owner new file mode 120000 index 00000000..83fad123 --- /dev/null +++ b/setup/purchase_mto_owner/odoo/addons/purchase_mto_owner @@ -0,0 +1 @@ +../../../../purchase_mto_owner \ No newline at end of file diff --git a/setup/purchase_mto_owner/setup.py b/setup/purchase_mto_owner/setup.py new file mode 100644 index 00000000..28c57bb6 --- /dev/null +++ b/setup/purchase_mto_owner/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)