diff --git a/purchase_sale_link_by_origin/README.rst b/purchase_sale_link_by_origin/README.rst new file mode 100644 index 00000000000..27dcd24f011 --- /dev/null +++ b/purchase_sale_link_by_origin/README.rst @@ -0,0 +1,93 @@ +============================= +Purchase/Sale: link by origin +============================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:6a985505564224fc77ab73c9de04a4bf823306324aac43642f872dff40d3df9b + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/18.0/purchase_sale_link_by_origin + :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-18-0/purchase-workflow-18-0-purchase_sale_link_by_origin + :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=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +By default since `Odoo +14.0 `__, +PO and SO are linked by their order lines. + +This module also link them by the PO's Origin field, to cover more +cases. For example: + +- If a user cancels a PO, by default the link would have been broken; + now it won't; +- Or if a user manually defines or updates the Origin field of a PO, it + will be taken into account. + +**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 +------- + +* Camptocamp + +Contributors +------------ + +- Phuc (Kieu Hoang) + +Other credits +------------- + +The development of this module has been financially supported by: + +- Camptocamp + +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_sale_link_by_origin/__init__.py b/purchase_sale_link_by_origin/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/purchase_sale_link_by_origin/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/purchase_sale_link_by_origin/__manifest__.py b/purchase_sale_link_by_origin/__manifest__.py new file mode 100644 index 00000000000..a25622bdbd4 --- /dev/null +++ b/purchase_sale_link_by_origin/__manifest__.py @@ -0,0 +1,18 @@ +# Copyright 2023 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) +{ + "name": "Purchase/Sale: link by origin", + "summary": "Link PO/SO by the PO's Origin in addition to the default " + "behavior that only links them by their lines", + "version": "18.0.1.0.0", + "author": "Camptocamp, Odoo Community Association (OCA)", + "license": "AGPL-3", + "category": "Purchase", + "website": "https://github.com/OCA/purchase-workflow", + "depends": [ + "sale_purchase_stock", + ], + "data": [], + "application": False, + "installable": True, +} diff --git a/purchase_sale_link_by_origin/i18n/it.po b/purchase_sale_link_by_origin/i18n/it.po new file mode 100644 index 00000000000..7cace93aa6c --- /dev/null +++ b/purchase_sale_link_by_origin/i18n/it.po @@ -0,0 +1,27 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * purchase_sale_link_by_origin +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2024-03-19 15:41+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 4.17\n" + +#. module: purchase_sale_link_by_origin +#: model:ir.model,name:purchase_sale_link_by_origin.model_purchase_order +msgid "Purchase Order" +msgstr "Ordine di acquisto" + +#. module: purchase_sale_link_by_origin +#: model:ir.model,name:purchase_sale_link_by_origin.model_sale_order +msgid "Sales Order" +msgstr "Ordine di vendita" diff --git a/purchase_sale_link_by_origin/i18n/purchase_sale_link_by_origin.pot b/purchase_sale_link_by_origin/i18n/purchase_sale_link_by_origin.pot new file mode 100644 index 00000000000..3961d45d98f --- /dev/null +++ b/purchase_sale_link_by_origin/i18n/purchase_sale_link_by_origin.pot @@ -0,0 +1,24 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * purchase_sale_link_by_origin +# +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_sale_link_by_origin +#: model:ir.model,name:purchase_sale_link_by_origin.model_purchase_order +msgid "Purchase Order" +msgstr "" + +#. module: purchase_sale_link_by_origin +#: model:ir.model,name:purchase_sale_link_by_origin.model_sale_order +msgid "Sales Order" +msgstr "" diff --git a/purchase_sale_link_by_origin/models/__init__.py b/purchase_sale_link_by_origin/models/__init__.py new file mode 100644 index 00000000000..6274a41870a --- /dev/null +++ b/purchase_sale_link_by_origin/models/__init__.py @@ -0,0 +1,2 @@ +from . import purchase_order +from . import sale_order diff --git a/purchase_sale_link_by_origin/models/purchase_order.py b/purchase_sale_link_by_origin/models/purchase_order.py new file mode 100644 index 00000000000..3bd841cfd19 --- /dev/null +++ b/purchase_sale_link_by_origin/models/purchase_order.py @@ -0,0 +1,20 @@ +# Copyright 2023 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +from odoo import api, models + + +class PurchaseOrder(models.Model): + _inherit = "purchase.order" + + @api.depends("origin") + def _compute_sale_order_count(self): + return super()._compute_sale_order_count() + + def _get_sale_orders(self): + origins = set() + for order in self: + if order.origin: + origins.update(order.origin.split(", ")) + so_origins = self.env["sale.order"].search([["name", "in", list(origins)]]) + return super()._get_sale_orders() | so_origins diff --git a/purchase_sale_link_by_origin/models/sale_order.py b/purchase_sale_link_by_origin/models/sale_order.py new file mode 100644 index 00000000000..2fb18d86336 --- /dev/null +++ b/purchase_sale_link_by_origin/models/sale_order.py @@ -0,0 +1,20 @@ +# Copyright 2023 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +from odoo import api, models + + +class SaleOrder(models.Model): + _inherit = "sale.order" + + @api.depends("origin") + def _compute_purchase_order_count(self): + return super()._compute_purchase_order_count() + + def _get_purchase_orders(self): + po_related = self.env["purchase.order"] + for order in self: + po_related |= self.env["purchase.order"].search( + [("origin", "ilike", order.name)] + ) + return super()._get_purchase_orders() | po_related diff --git a/purchase_sale_link_by_origin/pyproject.toml b/purchase_sale_link_by_origin/pyproject.toml new file mode 100644 index 00000000000..4231d0cccb3 --- /dev/null +++ b/purchase_sale_link_by_origin/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/purchase_sale_link_by_origin/readme/CONTRIBUTORS.md b/purchase_sale_link_by_origin/readme/CONTRIBUTORS.md new file mode 100644 index 00000000000..61600e0f51c --- /dev/null +++ b/purchase_sale_link_by_origin/readme/CONTRIBUTORS.md @@ -0,0 +1 @@ +- Phuc (Kieu Hoang) \<\> diff --git a/purchase_sale_link_by_origin/readme/CREDITS.md b/purchase_sale_link_by_origin/readme/CREDITS.md new file mode 100644 index 00000000000..705d3b30cad --- /dev/null +++ b/purchase_sale_link_by_origin/readme/CREDITS.md @@ -0,0 +1,3 @@ +The development of this module has been financially supported by: + +- Camptocamp diff --git a/purchase_sale_link_by_origin/readme/DESCRIPTION.md b/purchase_sale_link_by_origin/readme/DESCRIPTION.md new file mode 100644 index 00000000000..995d6e735c1 --- /dev/null +++ b/purchase_sale_link_by_origin/readme/DESCRIPTION.md @@ -0,0 +1,5 @@ +By default since [Odoo 14.0](https://github.com/odoo/odoo/commit/5a1645a8f8f3560eb778da90b6160b322ce0722e), PO and SO are linked by their order lines. + +This module also link them by the PO's Origin field, to cover more cases. For example: +- If a user cancels a PO, by default the link would have been broken; now it won't; +- Or if a user manually defines or updates the Origin field of a PO, it will be taken into account. diff --git a/purchase_sale_link_by_origin/static/description/icon.png b/purchase_sale_link_by_origin/static/description/icon.png new file mode 100755 index 00000000000..3a0328b516c Binary files /dev/null and b/purchase_sale_link_by_origin/static/description/icon.png differ diff --git a/purchase_sale_link_by_origin/static/description/index.html b/purchase_sale_link_by_origin/static/description/index.html new file mode 100644 index 00000000000..5587d8bc3b0 --- /dev/null +++ b/purchase_sale_link_by_origin/static/description/index.html @@ -0,0 +1,441 @@ + + + + + +Purchase/Sale: link by origin + + + + + + diff --git a/purchase_sale_link_by_origin/tests/__init__.py b/purchase_sale_link_by_origin/tests/__init__.py new file mode 100644 index 00000000000..94bdaacc25c --- /dev/null +++ b/purchase_sale_link_by_origin/tests/__init__.py @@ -0,0 +1 @@ +from . import test_sale_purchase diff --git a/purchase_sale_link_by_origin/tests/test_sale_purchase.py b/purchase_sale_link_by_origin/tests/test_sale_purchase.py new file mode 100644 index 00000000000..a9196a45c2f --- /dev/null +++ b/purchase_sale_link_by_origin/tests/test_sale_purchase.py @@ -0,0 +1,75 @@ +# Copyright 2023 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +from odoo import Command +from odoo.tests.common import TransactionCase + + +class TestSalePurchaseOrder(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) + cls.partner = cls.env["res.partner"].create({"name": "Test Partner"}) + cls.mto_route = cls.env["stock.route"].search( + [ + ("name", "=", "Replenish on Order (MTO)"), + "|", + ("active", "=", False), + ("active", "=", True), + ] + ) + cls.mto_route.active = True + cls.buy_route = cls.env["stock.route"].search([("name", "=", "Buy")]) + + cls.sale_order = cls.env["sale.order"].create( + { + "partner_id": cls.partner.id, + "order_line": [ + Command.create( + { + "product_id": cls.env["product.product"] + .create( + { + "name": "Product A", + "list_price": 100, + "seller_ids": [ + ( + 0, + 0, + {"partner_id": cls.partner.id, "delay": 5}, + ) + ], + "route_ids": [ + (4, cls.mto_route.id), + (4, cls.buy_route.id), + ], + } + ) + .id, + "name": "Product A", + } + ) + ], + } + ) + + def test_sale_purchase(self): + self.sale_order.action_confirm() + + purchase_order = self.env["purchase.order"].search( + [("id", "in", self.sale_order._get_purchase_orders().ids)] + ) + + purchase_order_count_after_confirm = len(self.sale_order._get_purchase_orders()) + sale_order_count_after_confirm = len(purchase_order._get_sale_orders()) + + purchase_order.button_cancel() + + purchase_order_count_after_cancel = len(self.sale_order._get_purchase_orders()) + sale_order_count_after_cancel = len(purchase_order._get_sale_orders()) + + self.assertEqual( + purchase_order_count_after_confirm, purchase_order_count_after_cancel + ) + self.assertEqual(sale_order_count_after_confirm, sale_order_count_after_cancel)