diff --git a/sale_margin_sync/README.rst b/sale_margin_sync/README.rst new file mode 100644 index 000000000..0e6a5b829 --- /dev/null +++ b/sale_margin_sync/README.rst @@ -0,0 +1,85 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +================ +Sale margin sync +================ + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:ea10a341b17b35a1f1ab3bfee8a6cc16cd698e300b3897fe0a18c03aaa569667 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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-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%2Fmargin--analysis-lightgray.png?logo=github + :target: https://github.com/OCA/margin-analysis/tree/19.0/sale_margin_sync + :alt: OCA/margin-analysis +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/margin-analysis-19-0/margin-analysis-19-0-sale_margin_sync + :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/margin-analysis&target_branch=19.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Recompute sale margin when related stock move cost price is changed. + +**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 +------- + +* Tecnativa + +Contributors +------------ + +- `Tecnativa `__: + + - Carlos Dauden + - Sergio Teruel + - Carlos Roca + - Carlos Lopez + +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/margin-analysis `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/sale_margin_sync/__init__.py b/sale_margin_sync/__init__.py new file mode 100644 index 000000000..31660d6a9 --- /dev/null +++ b/sale_margin_sync/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import models diff --git a/sale_margin_sync/__manifest__.py b/sale_margin_sync/__manifest__.py new file mode 100644 index 000000000..06390be0f --- /dev/null +++ b/sale_margin_sync/__manifest__.py @@ -0,0 +1,13 @@ +# Copyright 2019 Tecnativa - Carlos Dauden +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Sale margin sync", + "summary": "Recompute sale margin when stock move cost price is changed", + "version": "19.0.1.0.0", + "category": "Sales", + "website": "https://github.com/OCA/margin-analysis", + "author": "Tecnativa, Odoo Community Association (OCA)", + "license": "AGPL-3", + "installable": True, + "depends": ["sale_margin", "sale_stock"], +} diff --git a/sale_margin_sync/i18n/es.po b/sale_margin_sync/i18n/es.po new file mode 100644 index 000000000..decc855d1 --- /dev/null +++ b/sale_margin_sync/i18n/es.po @@ -0,0 +1,22 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_margin_sync +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 19.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2023-09-02 18:17+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_margin_sync +#: model:ir.model,name:sale_margin_sync.model_stock_move +msgid "Stock Move" +msgstr "Movimiento de stock" diff --git a/sale_margin_sync/i18n/it.po b/sale_margin_sync/i18n/it.po new file mode 100644 index 000000000..7410f28e1 --- /dev/null +++ b/sale_margin_sync/i18n/it.po @@ -0,0 +1,22 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_margin_sync +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 19.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2024-01-29 17:38+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: sale_margin_sync +#: model:ir.model,name:sale_margin_sync.model_stock_move +msgid "Stock Move" +msgstr "Movimento di magazzino" diff --git a/sale_margin_sync/i18n/ja.po b/sale_margin_sync/i18n/ja.po new file mode 100644 index 000000000..e3f58f043 --- /dev/null +++ b/sale_margin_sync/i18n/ja.po @@ -0,0 +1,20 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_margin_sync +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 19.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: Automatically generated\n" +"Language-Team: none\n" +"Language: ja\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=1; plural=0;\n" + +#. module: sale_margin_sync +#: model:ir.model,name:sale_margin_sync.model_stock_move +msgid "Stock Move" +msgstr "" diff --git a/sale_margin_sync/i18n/sale_margin_sync.pot b/sale_margin_sync/i18n/sale_margin_sync.pot new file mode 100644 index 000000000..3b927931c --- /dev/null +++ b/sale_margin_sync/i18n/sale_margin_sync.pot @@ -0,0 +1,19 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_margin_sync +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 19.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_margin_sync +#: model:ir.model,name:sale_margin_sync.model_stock_move +msgid "Stock Move" +msgstr "" diff --git a/sale_margin_sync/i18n/tr.po b/sale_margin_sync/i18n/tr.po new file mode 100644 index 000000000..630fdb8c8 --- /dev/null +++ b/sale_margin_sync/i18n/tr.po @@ -0,0 +1,20 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_margin_sync +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 19.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: Automatically generated\n" +"Language-Team: none\n" +"Language: tr\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" + +#. module: sale_margin_sync +#: model:ir.model,name:sale_margin_sync.model_stock_move +msgid "Stock Move" +msgstr "" diff --git a/sale_margin_sync/models/__init__.py b/sale_margin_sync/models/__init__.py new file mode 100644 index 000000000..1fec16564 --- /dev/null +++ b/sale_margin_sync/models/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import stock_move diff --git a/sale_margin_sync/models/stock_move.py b/sale_margin_sync/models/stock_move.py new file mode 100644 index 000000000..c950b01c7 --- /dev/null +++ b/sale_margin_sync/models/stock_move.py @@ -0,0 +1,34 @@ +# Copyright 2019 Tecnativa - Carlos Dauden +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + + +class StockMove(models.Model): + _inherit = "stock.move" + + def write(self, vals): + res = super().write(vals) + # Update purchase price from sale line linked to the move + if "value" in vals and not self.env.context.get("skip_sale_margin_sync", False): + self.sale_margin_sync() + return res + + def sale_margin_sync(self): + """Only synchronize moves that deliver goods to customers (deliveries + and dropshipments)""" + moves = self.filtered( + lambda m: (m._is_out() or m._is_dropshipped()) and m.sale_line_id + ) + for move in moves: + sale_line = move.sale_line_id + product_cost = move._get_price_unit() + if ( + sale_line.product_uom_id + and sale_line.product_uom_id != move.product_id.uom_id + ): + product_cost = move.product_id.uom_id._compute_price( + product_cost, + sale_line.product_uom_id, + ) + sale_line.purchase_price = product_cost diff --git a/sale_margin_sync/pyproject.toml b/sale_margin_sync/pyproject.toml new file mode 100644 index 000000000..4231d0ccc --- /dev/null +++ b/sale_margin_sync/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/sale_margin_sync/readme/CONTRIBUTORS.md b/sale_margin_sync/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..90cf01459 --- /dev/null +++ b/sale_margin_sync/readme/CONTRIBUTORS.md @@ -0,0 +1,6 @@ +- [Tecnativa](https://www.tecnativa.com): + + > - Carlos Dauden + > - Sergio Teruel + > - Carlos Roca + > - Carlos Lopez diff --git a/sale_margin_sync/readme/DESCRIPTION.md b/sale_margin_sync/readme/DESCRIPTION.md new file mode 100644 index 000000000..03ba88c69 --- /dev/null +++ b/sale_margin_sync/readme/DESCRIPTION.md @@ -0,0 +1 @@ +Recompute sale margin when related stock move cost price is changed. diff --git a/sale_margin_sync/static/description/icon.png b/sale_margin_sync/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/sale_margin_sync/static/description/icon.png differ diff --git a/sale_margin_sync/static/description/index.html b/sale_margin_sync/static/description/index.html new file mode 100644 index 000000000..cc731371e --- /dev/null +++ b/sale_margin_sync/static/description/index.html @@ -0,0 +1,438 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

Sale margin sync

+ +

Beta License: AGPL-3 OCA/margin-analysis Translate me on Weblate Try me on Runboat

+

Recompute sale margin when related stock move cost price is changed.

+

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

+
    +
  • Tecnativa
  • +
+
+
+

Contributors

+
    +
  • Tecnativa:

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

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/margin-analysis project on GitHub.

+

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

+
+
+
+
+ + diff --git a/sale_margin_sync/tests/__init__.py b/sale_margin_sync/tests/__init__.py new file mode 100644 index 000000000..8df8dcdcb --- /dev/null +++ b/sale_margin_sync/tests/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import test_sale_margin_sync diff --git a/sale_margin_sync/tests/test_sale_margin_sync.py b/sale_margin_sync/tests/test_sale_margin_sync.py new file mode 100644 index 000000000..38712d1ca --- /dev/null +++ b/sale_margin_sync/tests/test_sale_margin_sync.py @@ -0,0 +1,88 @@ +# Copyright 2019 Tecnativa - Carlos Dauden +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo.addons.base.tests.common import BaseCommon + + +class TestSaleMarginSync(BaseCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.pricelist = cls.env["product.pricelist"].create( + {"name": "Pricelist for testing sale_margin_sync"} + ) + cls.partner = cls.env["res.partner"].create({"name": "Test"}) + cls.product = cls.env["product.product"].create( + { + "name": "test_product", + "type": "consu", + "is_storable": True, + "standard_price": 70, + } + ) + cls.env["stock.quant"].create( + { + "product_id": cls.product.id, + "location_id": cls.env.ref("stock.stock_location_stock").id, + "quantity": 30.0, + } + ) + cls.order = cls.env["sale.order"].create( + { + "partner_id": cls.partner.id, + "order_line": [ + ( + 0, + 0, + { + "name": cls.product.name, + "product_id": cls.product.id, + "product_uom_qty": 10, + "product_uom_id": cls.product.uom_id.id, + "price_unit": 100.00, + }, + ), + ( + 0, + 0, + { + "name": cls.product.name, + "product_id": cls.product.id, + "product_uom_qty": 2, + "product_uom_id": cls.env.ref("uom.product_uom_dozen").id, + "price_unit": 1200.00, + }, + ), + ], + "pricelist_id": cls.pricelist.id, + } + ) + + def test_sale_margin_sync(self): + self.order.action_confirm() + so_line1 = self.order.order_line[:1] + move1 = so_line1.move_ids[:1] + move1.write({"quantity": 10, "picked": True}) + so_line2 = self.order.order_line[1:2] + move2 = so_line2.move_ids[:1] + move2.write({"quantity": 2, "picked": True}) + self.order.picking_ids[:1]._action_done() + # Simulate a unit cost of 80 in the product UOM + move1.value = 80.0 * move1._get_valued_qty() + move2.value = 80.0 * move2._get_valued_qty() + self.assertEqual(so_line1.purchase_price, 80.0) + self.assertEqual(so_line1.margin, 200.0) + self.assertEqual(so_line2.purchase_price, 960.0) + self.assertEqual(so_line2.margin, 480) + + def test_sale_margin_sync_unvalidated_move(self): + self.order.action_confirm() + so_line1 = self.order.order_line[:1] + move1 = so_line1.move_ids[:1] + move1.write({"quantity": 10, "picked": True}) + so_line2 = self.order.order_line[1:2] + move2 = so_line2.move_ids[:1] + move2.write({"quantity": 2, "picked": True}) + self.assertEqual(so_line1.purchase_price, 70.0) + self.assertEqual(so_line1.margin, 300.0) + self.assertEqual(so_line2.purchase_price, 840.0) + self.assertEqual(so_line2.margin, 720) diff --git a/setup/_metapackage/pyproject.toml b/setup/_metapackage/pyproject.toml index d628462e9..999df8395 100644 --- a/setup/_metapackage/pyproject.toml +++ b/setup/_metapackage/pyproject.toml @@ -7,6 +7,7 @@ dependencies = [ "odoo-addon-product_standard_margin==19.0.*", "odoo-addon-sale_margin_security==19.0.*", "odoo-addon-sale_report_margin==19.0.*", + "odoo-addon-sale_margin_sync==19.0.*", ] classifiers=[ "Programming Language :: Python",