diff --git a/purchase_stock_manual_currency/README.rst b/purchase_stock_manual_currency/README.rst new file mode 100644 index 00000000000..e5cc14410b5 --- /dev/null +++ b/purchase_stock_manual_currency/README.rst @@ -0,0 +1,78 @@ +============================== +Purchase Stock Manual Currency +============================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:fe95182a164ac1b1eba2e5f70221421995ea14531c420a67e465d56590668837 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/15.0/purchase_stock_manual_currency + :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-15-0/purchase-workflow-15-0-purchase_stock_manual_currency + :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=15.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module integrates `purchase_manual_currency` with purchase stock +management, ensuring that manually set currency rates on purchase orders are +applied to stock moves, stock valuation layers, and product costs. + +**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 +~~~~~~~ + +* ForgeFlow + +Contributors +~~~~~~~~~~~~ + +* Laura Cazorla + +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_stock_manual_currency/__init__.py b/purchase_stock_manual_currency/__init__.py new file mode 100644 index 00000000000..5f667be1191 --- /dev/null +++ b/purchase_stock_manual_currency/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2024 ForgeFlow, S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0) + +from . import models diff --git a/purchase_stock_manual_currency/__manifest__.py b/purchase_stock_manual_currency/__manifest__.py new file mode 100644 index 00000000000..787f3f1a046 --- /dev/null +++ b/purchase_stock_manual_currency/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2024 ForgeFlow, S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0) + +{ + "name": "Purchase Stock Manual Currency", + "version": "15.0.1.0.0", + "category": "Purchase Management", + "summary": "Extends manual currency from purchase to stock moves", + "author": "ForgeFlow, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/purchase-workflow", + "license": "AGPL-3", + "depends": ["purchase_stock", "purchase_manual_currency"], + "data": [], + "installable": True, +} diff --git a/purchase_stock_manual_currency/models/__init__.py b/purchase_stock_manual_currency/models/__init__.py new file mode 100644 index 00000000000..23c2c3a2be3 --- /dev/null +++ b/purchase_stock_manual_currency/models/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2024 ForgeFlow, S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0) + +from . import stock_move diff --git a/purchase_stock_manual_currency/models/stock_move.py b/purchase_stock_manual_currency/models/stock_move.py new file mode 100644 index 00000000000..3c1e35e274f --- /dev/null +++ b/purchase_stock_manual_currency/models/stock_move.py @@ -0,0 +1,32 @@ +# Copyright 2024 ForgeFlow, S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0) + +from odoo import fields, models + + +class StockMove(models.Model): + _inherit = "stock.move" + + def _get_price_unit(self): + # If the purchase order is in manual currency, we need to convert the + # price unit to the company currency by using the manual rate, instead + # of the default rate. + self.ensure_one() + price_company_curr = super(StockMove, self)._get_price_unit() + p_order = self.purchase_line_id.order_id + if p_order and p_order.manual_currency: + company_curr = p_order.company_id.currency_id + po_curr = p_order.currency_id + date = fields.Date.context_today(self) + # Convert the price back to the PO's currency + price_po_curr = company_curr._convert( + price_company_curr, po_curr, p_order.company_id, date, round=False + ) + # Convert the price to the company currency, using the manual rate + rate = ( + p_order.manual_currency_rate + if p_order.type_currency == "inverse_company_rate" + else (1.0 / p_order.manual_currency_rate) + ) + price_company_curr = price_po_curr * rate + return price_company_curr diff --git a/purchase_stock_manual_currency/readme/CONTRIBUTORS.rst b/purchase_stock_manual_currency/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000000..6d1dbddb921 --- /dev/null +++ b/purchase_stock_manual_currency/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Laura Cazorla diff --git a/purchase_stock_manual_currency/readme/DESCRIPTION.rst b/purchase_stock_manual_currency/readme/DESCRIPTION.rst new file mode 100644 index 00000000000..71590501c14 --- /dev/null +++ b/purchase_stock_manual_currency/readme/DESCRIPTION.rst @@ -0,0 +1,3 @@ +This module integrates `purchase_manual_currency` with purchase stock +management, ensuring that manually set currency rates on purchase orders are +applied to stock moves, stock valuation layers, and product costs. diff --git a/purchase_stock_manual_currency/static/description/index.html b/purchase_stock_manual_currency/static/description/index.html new file mode 100644 index 00000000000..fd2ebfed3b8 --- /dev/null +++ b/purchase_stock_manual_currency/static/description/index.html @@ -0,0 +1,422 @@ + + + + + +Purchase Stock Manual Currency + + + +
+

Purchase Stock Manual Currency

+ + +

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

+

This module integrates purchase_manual_currency with purchase stock +management, ensuring that manually set currency rates on purchase orders are +applied to stock moves, stock valuation layers, and product costs.

+

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

+
    +
  • ForgeFlow
  • +
+
+
+

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_stock_manual_currency/tests/__init__.py b/purchase_stock_manual_currency/tests/__init__.py new file mode 100644 index 00000000000..acc8bd0a111 --- /dev/null +++ b/purchase_stock_manual_currency/tests/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2024 ForgeFlow, S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0) + +from . import test_purchase_stock_manual_currency diff --git a/purchase_stock_manual_currency/tests/test_purchase_stock_manual_currency.py b/purchase_stock_manual_currency/tests/test_purchase_stock_manual_currency.py new file mode 100644 index 00000000000..104d7ba7c68 --- /dev/null +++ b/purchase_stock_manual_currency/tests/test_purchase_stock_manual_currency.py @@ -0,0 +1,167 @@ +# Copyright 2024 ForgeFlow, S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0) + +from odoo.tests.common import TransactionCase + + +class TestPurchaseStockManualCurrency(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.partner_model = cls.env["res.partner"] + cls.product_model = cls.env["product.product"] + cls.product_category_model = cls.env["product.category"] + cls.purchase_order_model = cls.env["purchase.order"] + cls.currency_model = cls.env["res.currency"] + cls.currency_rate_model = cls.env["res.currency.rate"] + cls.stock_quant_model = cls.env["stock.quant"] + cls.company_model = cls.env["res.company"] + + cls.eur_currency = cls.env.ref("base.EUR") + cls.usd_currency = cls.env.ref("base.USD") + cls.company = cls.company_model.create( + {"name": "Test Company", "currency_id": cls.eur_currency.id} + ) + + cls.env.company = cls.company + cls.partner = cls.partner_model.create({"name": "Test Partner"}) + + cls.conv_rate = cls.currency_rate_model.create( + { + "name": "2024-08-30", + "currency_id": cls.usd_currency.id, + "rate": 1.11, + } + ) + + cls.product_category = cls.product_category_model.create( + { + "name": "Average", + "property_cost_method": "average", + } + ) + cls.product = cls.product_model.create( + { + "name": "Test Product 1", + "type": "product", + "uom_id": cls.env.ref("uom.product_uom_unit").id, + "categ_id": cls.product_category.id, + } + ) + cls.quants = cls.env["stock.quant"].create( + { + "product_id": cls.product.id, + "location_id": cls.env.ref("stock.stock_location_stock").id, + "quantity": 100.0, + } + ) + + cls.p_order = cls.purchase_order_model.create( + { + "partner_id": cls.partner.id, + "currency_id": cls.usd_currency.id, + "order_line": [ + ( + 0, + 0, + { + "name": "Test line", + "product_qty": 10.0, + "product_id": cls.product.id, + "product_uom": cls.product.uom_id.id, + "price_unit": 8.0, + }, + ), + ], + } + ) + + def test_01_purchase_stock_manual_currency(self): + self.p_order.manual_currency = False + self.assertEqual(round(self.p_order.order_line.price_unit, 2), 8.0) + self.assertEqual(round(self.p_order.order_line.price_subtotal, 2), 80.0) + self.assertEqual( + round(self.p_order.order_line.subtotal_company_currency, 2), 72.07 + ) + self.assertEqual(round(self.p_order.amount_untaxed, 2), 80.00) + + self.p_order.button_confirm() + self.assertTrue(self.p_order.picking_ids) + self.assertEqual(len(self.p_order.picking_ids), 1) + stock_picking = self.p_order.picking_ids + + self.assertTrue(stock_picking.move_lines) + self.assertEqual(len(stock_picking.move_lines), 1) + stock_move = stock_picking.move_lines + price = stock_move._get_price_unit() + self.assertEqual(round(price, 2), 7.21) + stock_picking.move_lines.write({"quantity_done": 10}) + stock_picking.button_validate() + + self.assertTrue(stock_move.stock_valuation_layer_ids) + self.assertEqual(len(stock_move.stock_valuation_layer_ids), 1) + svl = stock_move.stock_valuation_layer_ids + self.assertEqual(svl.quantity, 10.0) + self.assertEqual(round(svl.unit_cost, 2), 7.21) + self.assertEqual(round(svl.value, 2), 72.07) + + def test_02_purchase_stock_manual_currency(self): + self.p_order.manual_currency = True + self.p_order.manual_currency_rate = 2.0 + self.p_order.type_currency = "company_rate" + self.assertEqual(round(self.p_order.order_line.price_unit, 2), 8.0) + self.assertEqual(round(self.p_order.order_line.price_subtotal, 2), 80.0) + self.assertEqual( + round(self.p_order.order_line.subtotal_company_currency, 2), 40.0 + ) + self.assertEqual(round(self.p_order.amount_untaxed, 2), 80.00) + + self.p_order.button_confirm() + self.assertTrue(self.p_order.picking_ids) + self.assertEqual(len(self.p_order.picking_ids), 1) + stock_picking = self.p_order.picking_ids + + self.assertTrue(stock_picking.move_lines) + self.assertEqual(len(stock_picking.move_lines), 1) + stock_move = stock_picking.move_lines + price = stock_move._get_price_unit() + self.assertEqual(round(price, 2), 4.00) + stock_picking.move_lines.write({"quantity_done": 10}) + stock_picking.button_validate() + + self.assertTrue(stock_move.stock_valuation_layer_ids) + self.assertEqual(len(stock_move.stock_valuation_layer_ids), 1) + svl = stock_move.stock_valuation_layer_ids + self.assertEqual(svl.quantity, 10.0) + self.assertEqual(round(svl.unit_cost, 2), 4.00) + self.assertEqual(round(svl.value, 2), 40.00) + + def test_03_purchase_stock_manual_currency(self): + self.p_order.currency_id = self.eur_currency + self.p_order._onchange_currency_change_rate() + self.assertEqual(round(self.p_order.order_line.price_unit, 2), 8.0) + self.assertEqual(round(self.p_order.order_line.price_subtotal, 2), 80.0) + self.assertEqual( + round(self.p_order.order_line.subtotal_company_currency, 2), 80.0 + ) + self.assertEqual(round(self.p_order.amount_untaxed, 2), 80.00) + + self.p_order.button_confirm() + self.assertTrue(self.p_order.picking_ids) + self.assertEqual(len(self.p_order.picking_ids), 1) + stock_picking = self.p_order.picking_ids + + self.assertTrue(stock_picking.move_lines) + self.assertEqual(len(stock_picking.move_lines), 1) + stock_move = stock_picking.move_lines + price = stock_move._get_price_unit() + self.assertEqual(round(price, 2), 8.00) + stock_picking.move_lines.write({"quantity_done": 10}) + stock_picking.button_validate() + + self.assertTrue(stock_move.stock_valuation_layer_ids) + self.assertEqual(len(stock_move.stock_valuation_layer_ids), 1) + svl = stock_move.stock_valuation_layer_ids + self.assertEqual(svl.quantity, 10.0) + self.assertEqual(round(svl.unit_cost, 2), 8.00) + self.assertEqual(round(svl.value, 2), 80.00) diff --git a/setup/purchase_stock_manual_currency/odoo/addons/purchase_stock_manual_currency b/setup/purchase_stock_manual_currency/odoo/addons/purchase_stock_manual_currency new file mode 120000 index 00000000000..5298d170f1a --- /dev/null +++ b/setup/purchase_stock_manual_currency/odoo/addons/purchase_stock_manual_currency @@ -0,0 +1 @@ +../../../../purchase_stock_manual_currency \ No newline at end of file diff --git a/setup/purchase_stock_manual_currency/setup.py b/setup/purchase_stock_manual_currency/setup.py new file mode 100644 index 00000000000..28c57bb6403 --- /dev/null +++ b/setup/purchase_stock_manual_currency/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)