diff --git a/stock_account_fifo_return_origin/README.rst b/stock_account_fifo_return_origin/README.rst new file mode 100644 index 00000000..224670e0 --- /dev/null +++ b/stock_account_fifo_return_origin/README.rst @@ -0,0 +1,93 @@ +================================ +Stock Account FIFO Return Origin +================================ + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:f365b070c877cb49d483654699911d0f9d351de248cfca5a6cdf11258d8a7017 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fstock--logistics--workflow-lightgray.png?logo=github + :target: https://github.com/OCA/stock-logistics-workflow/tree/18.0/stock_account_fifo_return_origin + :alt: OCA/stock-logistics-workflow +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/stock-logistics-workflow-18-0/stock-logistics-workflow-18-0-stock_account_fifo_return_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/stock-logistics-workflow&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module makes adjustment to the standard behavior with regards to +candidate SVLs that are used in purchase returns; if the SVL linked to +the origin move (receipt) has remaining quantity, that SVL should be +consumed first. + +**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 +------- + +* Quartile + +Contributors +------------ + +- `Quartile `__: + + - Yoshi Tashiro + - Aung Ko Ko Lin + +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. + +.. |maintainer-yostashiro| image:: https://github.com/yostashiro.png?size=40px + :target: https://github.com/yostashiro + :alt: yostashiro +.. |maintainer-aungkokolin1997| image:: https://github.com/aungkokolin1997.png?size=40px + :target: https://github.com/aungkokolin1997 + :alt: aungkokolin1997 + +Current `maintainers `__: + +|maintainer-yostashiro| |maintainer-aungkokolin1997| + +This module is part of the `OCA/stock-logistics-workflow `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/stock_account_fifo_return_origin/__init__.py b/stock_account_fifo_return_origin/__init__.py new file mode 100644 index 00000000..0650744f --- /dev/null +++ b/stock_account_fifo_return_origin/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/stock_account_fifo_return_origin/__manifest__.py b/stock_account_fifo_return_origin/__manifest__.py new file mode 100644 index 00000000..3e6df4fb --- /dev/null +++ b/stock_account_fifo_return_origin/__manifest__.py @@ -0,0 +1,14 @@ +# Copyright 2024 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Stock Account FIFO Return Origin", + "summary": "Prioritize the origin receipt SVL for purchase return valuation.", + "version": "18.0.1.0.0", + "license": "AGPL-3", + "author": "Quartile, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/stock-logistics-workflow", + "depends": ["stock_account"], + "maintainers": ["yostashiro", "aungkokolin1997"], + "installable": True, +} diff --git a/stock_account_fifo_return_origin/models/__init__.py b/stock_account_fifo_return_origin/models/__init__.py new file mode 100644 index 00000000..b834c96a --- /dev/null +++ b/stock_account_fifo_return_origin/models/__init__.py @@ -0,0 +1,3 @@ +from . import product +from . import stock_move +from . import stock_move_line diff --git a/stock_account_fifo_return_origin/models/product.py b/stock_account_fifo_return_origin/models/product.py new file mode 100644 index 00000000..b5efada4 --- /dev/null +++ b/stock_account_fifo_return_origin/models/product.py @@ -0,0 +1,18 @@ +# Copyright 2024 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + + +class Product(models.Model): + _inherit = "product.product" + + def _get_fifo_candidates(self, company, lot=False): + candidates = super()._get_fifo_candidates(company, lot=lot) + origin_move = self.env.context.get("origin_returned_move") + if not origin_move: + return candidates + origin_svl = origin_move.stock_valuation_layer_ids.filtered( + lambda x: x.remaining_qty > 0.00 + ) + return origin_svl | candidates diff --git a/stock_account_fifo_return_origin/models/stock_move.py b/stock_account_fifo_return_origin/models/stock_move.py new file mode 100644 index 00000000..39235eb6 --- /dev/null +++ b/stock_account_fifo_return_origin/models/stock_move.py @@ -0,0 +1,23 @@ +# Copyright 2024 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + + +class StockMove(models.Model): + _inherit = "stock.move" + + def _get_out_svl_vals(self, forced_quantity): + return_moves = self.filtered(lambda m: m.origin_returned_move_id) + other_moves = self - return_moves + svl_vals_list = ( + super(StockMove, other_moves)._get_out_svl_vals(forced_quantity) + if other_moves + else [] + ) + for move in return_moves: + svl_vals_list += super( + StockMove, + move.with_context(origin_returned_move=move.origin_returned_move_id), + )._get_out_svl_vals(forced_quantity) + return svl_vals_list diff --git a/stock_account_fifo_return_origin/models/stock_move_line.py b/stock_account_fifo_return_origin/models/stock_move_line.py new file mode 100644 index 00000000..40b52609 --- /dev/null +++ b/stock_account_fifo_return_origin/models/stock_move_line.py @@ -0,0 +1,14 @@ +# Copyright 2024 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, models + + +class StockMoveLine(models.Model): + _inherit = "stock.move.line" + + @api.model + def _create_correction_svl(self, move, diff): + if move._is_in() and diff < 0: + move = move.with_context(origin_returned_move=move) + return super()._create_correction_svl(move, diff) diff --git a/stock_account_fifo_return_origin/pyproject.toml b/stock_account_fifo_return_origin/pyproject.toml new file mode 100644 index 00000000..4231d0cc --- /dev/null +++ b/stock_account_fifo_return_origin/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/stock_account_fifo_return_origin/readme/CONTRIBUTORS.md b/stock_account_fifo_return_origin/readme/CONTRIBUTORS.md new file mode 100644 index 00000000..4d16bdc2 --- /dev/null +++ b/stock_account_fifo_return_origin/readme/CONTRIBUTORS.md @@ -0,0 +1,3 @@ +- [Quartile](https://www.quartile.co): + - Yoshi Tashiro + - Aung Ko Ko Lin diff --git a/stock_account_fifo_return_origin/readme/DESCRIPTION.md b/stock_account_fifo_return_origin/readme/DESCRIPTION.md new file mode 100644 index 00000000..94d5d00a --- /dev/null +++ b/stock_account_fifo_return_origin/readme/DESCRIPTION.md @@ -0,0 +1,3 @@ +This module makes adjustment to the standard behavior with regards to candidate SVLs +that are used in purchase returns; if the SVL linked to the origin move (receipt) has +remaining quantity, that SVL should be consumed first. diff --git a/stock_account_fifo_return_origin/static/description/index.html b/stock_account_fifo_return_origin/static/description/index.html new file mode 100644 index 00000000..d9954af3 --- /dev/null +++ b/stock_account_fifo_return_origin/static/description/index.html @@ -0,0 +1,432 @@ + + + + + +Stock Account FIFO Return Origin + + + +
+

Stock Account FIFO Return Origin

+ + +

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

+

This module makes adjustment to the standard behavior with regards to +candidate SVLs that are used in purchase returns; if the SVL linked to +the origin move (receipt) has remaining quantity, that SVL should be +consumed first.

+

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

+
    +
  • Quartile
  • +
+
+
+

Contributors

+
    +
  • Quartile:
      +
    • Yoshi Tashiro
    • +
    • Aung Ko Ko Lin
    • +
    +
  • +
+
+
+

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.

+

Current maintainers:

+

yostashiro aungkokolin1997

+

This module is part of the OCA/stock-logistics-workflow project on GitHub.

+

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

+
+
+
+ + diff --git a/stock_account_fifo_return_origin/tests/__init__.py b/stock_account_fifo_return_origin/tests/__init__.py new file mode 100644 index 00000000..3f123147 --- /dev/null +++ b/stock_account_fifo_return_origin/tests/__init__.py @@ -0,0 +1 @@ +from . import test_stock_account_fifo_return_origin diff --git a/stock_account_fifo_return_origin/tests/test_stock_account_fifo_return_origin.py b/stock_account_fifo_return_origin/tests/test_stock_account_fifo_return_origin.py new file mode 100644 index 00000000..efc6604c --- /dev/null +++ b/stock_account_fifo_return_origin/tests/test_stock_account_fifo_return_origin.py @@ -0,0 +1,97 @@ +# Copyright 2024 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + + +from odoo.tests import Form + +from odoo.addons.base.tests.common import BaseCommon + + +class TestStockAccountFifoReturnOrigin(BaseCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + product_category = cls.env["product.category"].create( + { + "name": "Test Category", + "property_cost_method": "fifo", + "property_valuation": "real_time", + } + ) + cls.product = cls.env["product.product"].create( + { + "name": "Test Product", + "categ_id": product_category.id, + } + ) + cls.supplier_location = cls.env.ref("stock.stock_location_suppliers") + cls.stock_location = cls.env.ref("stock.stock_location_stock") + cls.picking_type_in = cls.env.ref("stock.picking_type_in") + + def create_receipt_picking(self, price_unit): + picking = self.env["stock.picking"].create( + { + "location_id": self.supplier_location.id, + "location_dest_id": self.stock_location.id, + "picking_type_id": self.picking_type_in.id, + } + ) + move = self.env["stock.move"].create( + { + "name": "Test Move", + "product_id": self.product.id, + "quantity": 10, + "product_uom": self.product.uom_id.id, + "location_id": self.supplier_location.id, + "location_dest_id": self.stock_location.id, + "picking_id": picking.id, + "price_unit": price_unit, + } + ) + move._action_confirm() + move._action_assign() + move.move_line_ids.quantity = 10 + picking.button_validate() + return picking + + def test_stock_account_fifo_return(self): + picking = self.create_receipt_picking(100) + move = picking.move_ids[0] + stock_valuation_layer = move.stock_valuation_layer_ids[0] + self.assertEqual(stock_valuation_layer.value, 1000) + picking = self.create_receipt_picking(200) + move = picking.move_ids[0] + stock_valuation_layer = move.stock_valuation_layer_ids[0] + self.assertEqual(stock_valuation_layer.value, 2000) + return_picking_wizard_form = Form( + self.env["stock.return.picking"].with_context( + active_ids=picking.ids, + active_id=picking.id, + active_model="stock.picking", + ) + ) + return_picking_wizard = return_picking_wizard_form.save() + return_picking_wizard.product_return_moves.write({"quantity": 10}) + return_picking_wizard_action = return_picking_wizard.action_create_returns() + return_picking = self.env["stock.picking"].browse( + return_picking_wizard_action["res_id"] + ) + return_move = return_picking.move_ids + return_move.move_line_ids.quantity = 10 + return_picking.button_validate() + return_valuation_layer = return_move.stock_valuation_layer_ids[0] + self.assertEqual(abs(return_valuation_layer.value), 2000) + + def test_create_correction_svl(self): + self.create_receipt_picking(100) + picking = self.create_receipt_picking(200) + picking.action_toggle_is_locked() + move = picking.move_ids[0] + move_line = move.move_line_ids[0] + move_line.quantity = 8 + correction_svl = move.stock_valuation_layer_ids.filtered( + lambda svl: svl.quantity < 0 + ) + self.assertTrue(correction_svl) + # Should use receipt's cost (200), not FIFO oldest (100) + self.assertEqual(correction_svl.value, -400.0)