diff --git a/sale_purchase_secondary_unit/README.rst b/sale_purchase_secondary_unit/README.rst new file mode 100644 index 00000000000..0f0623c159c --- /dev/null +++ b/sale_purchase_secondary_unit/README.rst @@ -0,0 +1,98 @@ +============================ +Sale Purchase Secondary Unit +============================ + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:07b642f027170cc76550985cd80609dfa1d446700ee8c1ef963fc22fa0b12725 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/sale_purchase_secondary_unit + :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-sale_purchase_secondary_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/purchase-workflow&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module extends the functionality of sale_order_secondary_unit +module and purchase_order_secondary_unit to allow copy secondary uom +from sale orders to purchase orders when a sale order is confirmed. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To use this module you need to: + +1. Go to a *Product > General Information tab*. +2. Create any record in "Secondary unit of measure". +3. Set the conversion factor. +4. Set a product with a buy rule + mto rule +5. Set vendors on product +6. Go to *Sales > orders* and create a sale order with this product and + any second unit. +7. Confirm the sale order. +8. Go to purchaser order created and you will see the second unit in + purchase order line. + +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 +------- + +* Tecnativa + +Contributors +------------ + +- `Tecnativa `__: + + - Sergio Teruel + - Carlos Dauden + - Carlos Roca + +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/sale_purchase_secondary_unit/__init__.py b/sale_purchase_secondary_unit/__init__.py new file mode 100644 index 00000000000..2bc1433e720 --- /dev/null +++ b/sale_purchase_secondary_unit/__init__.py @@ -0,0 +1,3 @@ +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +from . import models diff --git a/sale_purchase_secondary_unit/__manifest__.py b/sale_purchase_secondary_unit/__manifest__.py new file mode 100644 index 00000000000..39c758d004b --- /dev/null +++ b/sale_purchase_secondary_unit/__manifest__.py @@ -0,0 +1,20 @@ +# Copyright 2023 Tecnativa - Sergio Teruel +# Copyright 2023 Tecnativa - Carlos Dauden +# License AGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). +{ + "name": "Sale Purchase Secondary Unit", + "summary": "Propagate sale secondary uom to purchase orders", + "version": "18.0.1.0.0", + "development_status": "Beta", + "category": "stock", + "website": "https://github.com/OCA/purchase-workflow", + "author": "Tecnativa, Odoo Community Association (OCA)", + "license": "AGPL-3", + "installable": True, + "depends": [ + "sale_purchase_stock", + "purchase_order_secondary_unit", + "sale_stock_secondary_unit", + ], + "auto_install": True, +} diff --git a/sale_purchase_secondary_unit/i18n/es.po b/sale_purchase_secondary_unit/i18n/es.po new file mode 100644 index 00000000000..a763b07f780 --- /dev/null +++ b/sale_purchase_secondary_unit/i18n/es.po @@ -0,0 +1,38 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_purchase_secondary_unit +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2023-11-15 21:38+0000\n" +"Last-Translator: Ivorra78 \n" +"Language-Team: none\n" +"Language: es\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: sale_purchase_secondary_unit +#: model:ir.model,name:sale_purchase_secondary_unit.model_purchase_order_line +msgid "Purchase Order Line" +msgstr "Línea de Orden de Compra" + +#. module: sale_purchase_secondary_unit +#: model:ir.model,name:sale_purchase_secondary_unit.model_sale_order +msgid "Sales Order" +msgstr "Orden de Venta" + +#. module: sale_purchase_secondary_unit +#: model:ir.model,name:sale_purchase_secondary_unit.model_stock_rule +msgid "Stock Rule" +msgstr "Regla de Existencias" + +#~ msgid "Sales Order Line" +#~ msgstr "Línea de la Orden de Venta" + +#~ msgid "Stock Move" +#~ msgstr "Movimiento de Existencias" diff --git a/sale_purchase_secondary_unit/i18n/sale_purchase_secondary_unit.pot b/sale_purchase_secondary_unit/i18n/sale_purchase_secondary_unit.pot new file mode 100644 index 00000000000..3cb3357093f --- /dev/null +++ b/sale_purchase_secondary_unit/i18n/sale_purchase_secondary_unit.pot @@ -0,0 +1,29 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_purchase_secondary_unit +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.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: sale_purchase_secondary_unit +#: model:ir.model,name:sale_purchase_secondary_unit.model_purchase_order_line +msgid "Purchase Order Line" +msgstr "" + +#. module: sale_purchase_secondary_unit +#: model:ir.model,name:sale_purchase_secondary_unit.model_sale_order +msgid "Sales Order" +msgstr "" + +#. module: sale_purchase_secondary_unit +#: model:ir.model,name:sale_purchase_secondary_unit.model_stock_rule +msgid "Stock Rule" +msgstr "" diff --git a/sale_purchase_secondary_unit/models/__init__.py b/sale_purchase_secondary_unit/models/__init__.py new file mode 100644 index 00000000000..e324d32e18f --- /dev/null +++ b/sale_purchase_secondary_unit/models/__init__.py @@ -0,0 +1,6 @@ +# Copyright 2023 Tecnativa - Sergio Teruel +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +from . import purchase_order_line +from . import sale_order +from . import stock_rule diff --git a/sale_purchase_secondary_unit/models/purchase_order_line.py b/sale_purchase_secondary_unit/models/purchase_order_line.py new file mode 100644 index 00000000000..bd2cecea660 --- /dev/null +++ b/sale_purchase_secondary_unit/models/purchase_order_line.py @@ -0,0 +1,62 @@ +# Copyright 2023 Tecnativa - Carlos Dauden +# Copyright 2023 Tecnativa - Sergio Teruel +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) + +from odoo import api, models + + +class PurchaseOrderLine(models.Model): + _inherit = "purchase.order.line" + + def _find_candidate( + self, + product_id, + product_qty, + product_uom, + location_id, + name, + origin, + company_id, + values, + ): + secondary_uom_id = values.get("secondary_uom_id", False) + po_lines = self.filtered( + lambda x, sec_uom_id=secondary_uom_id: x.secondary_uom_id.id == sec_uom_id + ) + return super(PurchaseOrderLine, po_lines)._find_candidate( + product_id, + product_qty, + product_uom, + location_id, + name, + origin, + company_id, + values, + ) + + @api.model + def _prepare_purchase_order_line_from_procurement( + self, + product_id, + product_qty, + product_uom, + location_dest_id, + name, + origin, + company_id, + values, + po, + ): + res = super()._prepare_purchase_order_line_from_procurement( + product_id, + product_qty, + product_uom, + location_dest_id, + name, + origin, + company_id, + values, + po, + ) + res["secondary_uom_id"] = values.get("secondary_uom_id", False) + return res diff --git a/sale_purchase_secondary_unit/models/sale_order.py b/sale_purchase_secondary_unit/models/sale_order.py new file mode 100644 index 00000000000..9ad837ce0d8 --- /dev/null +++ b/sale_purchase_secondary_unit/models/sale_order.py @@ -0,0 +1,16 @@ +# Copyright 2023 Tecnativa - Carlos Dauden +# Copyright 2023 Tecnativa - Sergio Teruel +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) + +from odoo import models + + +class SaleOrder(models.Model): + _inherit = "sale.order" + + def action_cancel(self): + # When a SO linked to a purchase order is cancelled we must update the + # secondary_uom_qty on po line + return super( + SaleOrder, self.with_context(cancelled_so_lines=self.order_line.ids) + ).action_cancel() diff --git a/sale_purchase_secondary_unit/models/stock_rule.py b/sale_purchase_secondary_unit/models/stock_rule.py new file mode 100644 index 00000000000..5d58891c45c --- /dev/null +++ b/sale_purchase_secondary_unit/models/stock_rule.py @@ -0,0 +1,70 @@ +# Copyright 2023 Tecnativa - Sergio Teruel +# Copyright 2023 Tecnativa - Carlos Dauden +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) + +from odoo import api, models + + +class StockRule(models.Model): + _inherit = "stock.rule" + + @api.model + def _get_procurements_to_merge_groupby(self, procurement): + """Do not group purchase order line if they have different secondary_uom in + sale order line. + """ + return ( + procurement.values.get("secondary_uom_id"), + super()._get_procurements_to_merge_groupby(procurement), + ) + + @api.model + def _run_buy(self, procurements): + return super( + StockRule, self.with_context(skip_default_secondary_uom_qty=True) + )._run_buy(procurements) + + def _update_purchase_order_line( + self, product_id, product_qty, product_uom, company_id, values, line + ): + vals = super()._update_purchase_order_line( + product_id, product_qty, product_uom, company_id, values, line + ) + if values.get("secondary_uom_id", False): + moves_dest = line.move_dest_ids + values.get( + "move_dest_ids", self.env["stock.move"] + ) + moves = self.env["stock.move"].browse(list(moves_dest._rollup_move_dests())) + cancelled_so_lines = self.env.context.get("cancelled_so_lines", []) + sale_lines = moves.mapped("sale_line_id").filtered( + lambda ln: ln.id not in cancelled_so_lines + ) + vals["secondary_uom_qty"] = sum(sale_lines.mapped("secondary_uom_qty")) + return vals + + @api.model + def get_stock_move_sale_line(self, stock_move): + for move in stock_move.move_dest_ids: + if move.sale_line_id: + return move.sale_line_id + return self.get_stock_move_sale_line(move) + + @api.model + def _merge_procurements(self, procurements_to_merge): + # The quantity accumulation is embedded in this method and is not heritable + # Avoid process only one procurement + if len(procurements_to_merge) == 1 and len(procurements_to_merge[0]) == 1: + return super()._merge_procurements(procurements_to_merge) + secondary_index_dic = {} + # Fill dict with total secondary_uom_qty by group of procurements to merge + for index, procurements in enumerate(procurements_to_merge): + secondary_index_dic[index] = 0 + for procurement in procurements: + secondary_index_dic[index] += procurement.values.get( + "secondary_uom_qty", 0 + ) + merged_procurements = super()._merge_procurements(procurements_to_merge) + # Set total secondary_uom_qty from previous dict + for index, merged_procurement in enumerate(merged_procurements): + merged_procurement.values["secondary_uom_qty"] = secondary_index_dic[index] + return merged_procurements diff --git a/sale_purchase_secondary_unit/pyproject.toml b/sale_purchase_secondary_unit/pyproject.toml new file mode 100644 index 00000000000..4231d0cccb3 --- /dev/null +++ b/sale_purchase_secondary_unit/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/sale_purchase_secondary_unit/readme/CONTRIBUTORS.md b/sale_purchase_secondary_unit/readme/CONTRIBUTORS.md new file mode 100644 index 00000000000..69360a4f2f8 --- /dev/null +++ b/sale_purchase_secondary_unit/readme/CONTRIBUTORS.md @@ -0,0 +1,5 @@ +- [Tecnativa](https://www.tecnativa.com): + + > - Sergio Teruel + > - Carlos Dauden + > - Carlos Roca diff --git a/sale_purchase_secondary_unit/readme/DESCRIPTION.md b/sale_purchase_secondary_unit/readme/DESCRIPTION.md new file mode 100644 index 00000000000..37377710353 --- /dev/null +++ b/sale_purchase_secondary_unit/readme/DESCRIPTION.md @@ -0,0 +1,3 @@ +This module extends the functionality of sale_order_secondary_unit +module and purchase_order_secondary_unit to allow copy secondary uom +from sale orders to purchase orders when a sale order is confirmed. diff --git a/sale_purchase_secondary_unit/readme/USAGE.md b/sale_purchase_secondary_unit/readme/USAGE.md new file mode 100644 index 00000000000..4c576959b42 --- /dev/null +++ b/sale_purchase_secondary_unit/readme/USAGE.md @@ -0,0 +1,12 @@ +To use this module you need to: + +1. Go to a *Product \> General Information tab*. +2. Create any record in "Secondary unit of measure". +3. Set the conversion factor. +4. Set a product with a buy rule + mto rule +5. Set vendors on product +6. Go to *Sales \> orders* and create a sale order with this product + and any second unit. +7. Confirm the sale order. +8. Go to purchaser order created and you will see the second unit in + purchase order line. diff --git a/sale_purchase_secondary_unit/static/description/icon.png b/sale_purchase_secondary_unit/static/description/icon.png new file mode 100644 index 00000000000..3a0328b516c Binary files /dev/null and b/sale_purchase_secondary_unit/static/description/icon.png differ diff --git a/sale_purchase_secondary_unit/static/description/index.html b/sale_purchase_secondary_unit/static/description/index.html new file mode 100644 index 00000000000..a3c6e71b7fe --- /dev/null +++ b/sale_purchase_secondary_unit/static/description/index.html @@ -0,0 +1,450 @@ + + + + + +Sale Purchase Secondary Unit + + + +
+

Sale Purchase Secondary Unit

+ + +

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

+

This module extends the functionality of sale_order_secondary_unit +module and purchase_order_secondary_unit to allow copy secondary uom +from sale orders to purchase orders when a sale order is confirmed.

+

Table of contents

+ +
+

Usage

+

To use this module you need to:

+
    +
  1. Go to a Product > General Information tab.
  2. +
  3. Create any record in “Secondary unit of measure”.
  4. +
  5. Set the conversion factor.
  6. +
  7. Set a product with a buy rule + mto rule
  8. +
  9. Set vendors on product
  10. +
  11. Go to Sales > orders and create a sale order with this product and +any second unit.
  12. +
  13. Confirm the sale order.
  14. +
  15. Go to purchaser order created and you will see the second unit in +purchase order line.
  16. +
+
+
+

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

+
    +
  • Tecnativa
  • +
+
+
+

Contributors

+
    +
  • Tecnativa:

    +
    +
      +
    • Sergio Teruel
    • +
    • Carlos Dauden
    • +
    • Carlos Roca
    • +
    +
    +
  • +
+
+
+

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/sale_purchase_secondary_unit/tests/__init__.py b/sale_purchase_secondary_unit/tests/__init__.py new file mode 100644 index 00000000000..2f2fd532807 --- /dev/null +++ b/sale_purchase_secondary_unit/tests/__init__.py @@ -0,0 +1,3 @@ +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +from . import test_sale_purchase_secondary_unit diff --git a/sale_purchase_secondary_unit/tests/test_sale_purchase_secondary_unit.py b/sale_purchase_secondary_unit/tests/test_sale_purchase_secondary_unit.py new file mode 100644 index 00000000000..921c06c5b65 --- /dev/null +++ b/sale_purchase_secondary_unit/tests/test_sale_purchase_secondary_unit.py @@ -0,0 +1,62 @@ +# Copyright 2023 Tecnativa - Carlos Dauden +# Copyright 2023 Tecnativa - Sergio Teruel +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). +import logging + +from odoo.tests import Form, TransactionCase + +_logger = logging.getLogger(__name__) + + +class TestPurchaseStockSecondaryUnit(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + # Active multiple units of measure security group for user + cls.env.user.groups_id = [(4, cls.env.ref("uom.group_uom").id)] + cls.warehouse = cls.env.ref("stock.warehouse0") + cls.product_uom_kg = cls.env.ref("uom.product_uom_kgm") + cls.product_uom_gram = cls.env.ref("uom.product_uom_gram") + cls.product_uom_unit = cls.env.ref("uom.product_uom_unit") + cls.ProductSecondaryUnit = cls.env["product.secondary.unit"] + cls.mto_route = cls.env.ref("stock.route_warehouse0_mto") + cls.mto_route.active = True + cls.buy_route = cls.env.ref("purchase_stock.route_warehouse0_buy") + cls.partner = cls.env["res.partner"].create({"name": "test - partner"}) + cls.supplier = cls.env["res.partner"].create({"name": "test - supplier"}) + with Form(cls.env["product.product"]) as product_form: + product_form.name = "Test" + product_form.type = "consu" + product_form.is_storable = True + with product_form.secondary_uom_ids.new() as secondary_uom: + secondary_uom.name = "box" + secondary_uom.uom_id = cls.product_uom_unit + secondary_uom.factor = 5.0 + cls.product = product_form.save() + cls.supplierinfo = cls.env["product.supplierinfo"].create( + { + "partner_id": cls.supplier.id, + "product_id": cls.product.id, + "product_tmpl_id": cls.product.product_tmpl_id.id, + } + ) + cls.product.route_ids = [(6, 0, (cls.mto_route + cls.buy_route).ids)] + cls.secondary_product_uom = cls.product.secondary_uom_ids[:1] + cls.sale_order = cls.env["sale.order"] + with Form(cls.sale_order) as so_form: + so_form.partner_id = cls.partner + with so_form.order_line.new() as line: + line.product_id = cls.product + line.secondary_uom_id = cls.secondary_product_uom + line.secondary_uom_qty = 2.0 + cls.sale_order = so_form.save() + + def test_sale_order_propagate_secondary_uom(self): + # self.sale_order.order_line[0].route_id = self.buy_route + self.sale_order.action_confirm() + purchase_order = self.sale_order._get_purchase_orders() + self.assertEqual( + purchase_order.order_line[0].secondary_uom_id, + self.product.secondary_uom_ids[0], + ) + self.assertEqual(purchase_order.order_line[0].secondary_uom_qty, 2.0)