diff --git a/sale_margin_sync/README.rst b/sale_margin_sync/README.rst new file mode 100644 index 000000000..597daba2c --- /dev/null +++ b/sale_margin_sync/README.rst @@ -0,0 +1,84 @@ +================ +Sale margin sync +================ + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:8b69616f03524df8bef16a62a618ab5baa651460bc14413a92a6c51dcc869f49 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fmargin--analysis-lightgray.png?logo=github + :target: https://github.com/OCA/margin-analysis/tree/17.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-17-0/margin-analysis-17-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=17.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 + +- `Heliconia Solutions Pvt. Ltd. `__ + + - Bhavesh Heliconia + +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..68dec8f99 --- /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": "17.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..237a1cfdd --- /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 16.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_valuation_layer +msgid "Stock Valuation Layer" +msgstr "Nivel de valoración de existencias" diff --git a/sale_margin_sync/i18n/it.po b/sale_margin_sync/i18n/it.po new file mode 100644 index 000000000..fd746f4fd --- /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 16.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_valuation_layer +msgid "Stock Valuation Layer" +msgstr "Livello valutazione magazzino" 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..6990ede21 --- /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 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: sale_margin_sync +#: model:ir.model,name:sale_margin_sync.model_stock_valuation_layer +msgid "Stock Valuation Layer" +msgstr "" diff --git a/sale_margin_sync/models/__init__.py b/sale_margin_sync/models/__init__.py new file mode 100644 index 000000000..276dabe9e --- /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_valuation_layer diff --git a/sale_margin_sync/models/stock_valuation_layer.py b/sale_margin_sync/models/stock_valuation_layer.py new file mode 100644 index 000000000..c2ebb2a2c --- /dev/null +++ b/sale_margin_sync/models/stock_valuation_layer.py @@ -0,0 +1,36 @@ +# Copyright 2019 Tecnativa - Carlos Dauden +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, models + + +class StockValuationLayer(models.Model): + _inherit = "stock.valuation.layer" + + @api.model_create_multi + def create(self, vals_list): + """Update purchase price from sale line linked to svl record""" + svls = super().create(vals_list) + if not self.env.context.get("skip_sale_margin_sync", False): + svls.filtered("stock_move_id.sale_line_id").sale_margin_sync() + return svls + + def write(self, vals): + res = super().write(vals) + if "unit_cost" 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 outgoing moves""" + for svl in self.filtered(lambda line: (line.quantity < 0.0)): + sale_line_id = svl.stock_move_id.sale_line_id + product_cost = svl.unit_cost + if sale_line_id.product_uom and sale_line_id.product_uom != svl.uom_id: + product_cost = svl.uom_id._compute_price( + product_cost, + sale_line_id.product_uom, + ) + sale_line_id.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..afb731607 --- /dev/null +++ b/sale_margin_sync/readme/CONTRIBUTORS.md @@ -0,0 +1,7 @@ +- [Tecnativa](https://www.tecnativa.com): + + > - Carlos Dauden + > - Sergio Teruel + > - Carlos Roca +- [Heliconia Solutions Pvt. Ltd.](https://www.heliconia.io) + - Bhavesh Heliconia 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..2ed001135 --- /dev/null +++ b/sale_margin_sync/static/description/index.html @@ -0,0 +1,436 @@ + + + + + +Sale margin sync + + + +
+

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

+ +
+
+

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..0a6caf71b --- /dev/null +++ b/sale_margin_sync/tests/test_sale_margin_sync.py @@ -0,0 +1,83 @@ +# Copyright 2019 Tecnativa - Carlos Dauden +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import Command + +from odoo.addons.base.tests.common import DISABLED_MAIL_CONTEXT, BaseCommon + + +class TestSaleMarginSync(BaseCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, **DISABLED_MAIL_CONTEXT)) + 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": "product", "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": [ + Command.create( + { + "name": cls.product.name, + "product_id": cls.product.id, + "product_uom_qty": 10, + "product_uom": cls.product.uom_id.id, + "price_unit": 100.00, + }, + ), + Command.create( + { + "name": cls.product.name, + "product_id": cls.product.id, + "product_uom_qty": 2, + "product_uom": 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.quantity = 10 + so_line2 = self.order.order_line[1:2] + move2 = so_line2.move_ids[:1] + move2.quantity = 2 + self.order.picking_ids[:1]._action_done() + move1.stock_valuation_layer_ids[:1].unit_cost = 80.0 + move2.stock_valuation_layer_ids[:1].unit_cost = 80.0 + 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) + + 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.quantity = 10 + move1.stock_valuation_layer_ids[:1].unit_cost = 80.0 + so_line2 = self.order.order_line[1:2] + move2 = so_line2.move_ids[:1] + move2.quantity = 2 + move2.stock_valuation_layer_ids[:1].unit_cost = 80.0 + 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)