diff --git a/setup/stock_valuation_fifo_lot/odoo/addons/stock_valuation_fifo_lot b/setup/stock_valuation_fifo_lot/odoo/addons/stock_valuation_fifo_lot new file mode 120000 index 000000000000..bf4290934245 --- /dev/null +++ b/setup/stock_valuation_fifo_lot/odoo/addons/stock_valuation_fifo_lot @@ -0,0 +1 @@ +../../../../stock_valuation_fifo_lot \ No newline at end of file diff --git a/setup/stock_valuation_fifo_lot/setup.py b/setup/stock_valuation_fifo_lot/setup.py new file mode 100644 index 000000000000..28c57bb64031 --- /dev/null +++ b/setup/stock_valuation_fifo_lot/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/stock_valuation_fifo_lot/README.rst b/stock_valuation_fifo_lot/README.rst new file mode 100644 index 000000000000..b8809519e4d4 --- /dev/null +++ b/stock_valuation_fifo_lot/README.rst @@ -0,0 +1,93 @@ +======================== +Stock Valuation Fifo Lot +======================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:c2a4a8addfa492b1a12ab3bd17b857715a3f32304bd2839b9e69ced26533f74a + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png + :target: https://odoo-community.org/page/development-status + :alt: Alpha +.. |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/16.0/stock_valuation_fifo_lot + :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-16-0/stock-logistics-workflow-16-0-stock_valuation_fifo_lot + :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=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module is used to calculate FIFO cost by lot. + +.. IMPORTANT:: + This is an alpha version, the data model and design can change at any time without warning. + Only for development or testing purpose, do not use in production. + `More details on development status `_ + +**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 +~~~~~~~ + +* Ecosoft + +Contributors +~~~~~~~~~~~~ + +* `Ecosoft `__: + + * Tharathip Chaweewongphan + * Saran Limpajitkutaporn + * Pimolnat Suntian + +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-newtratip| image:: https://github.com/newtratip.png?size=40px + :target: https://github.com/newtratip + :alt: newtratip + +Current `maintainer `__: + +|maintainer-newtratip| + +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_valuation_fifo_lot/__init__.py b/stock_valuation_fifo_lot/__init__.py new file mode 100644 index 000000000000..8ebc8a7c5e74 --- /dev/null +++ b/stock_valuation_fifo_lot/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) + +from . import models diff --git a/stock_valuation_fifo_lot/__manifest__.py b/stock_valuation_fifo_lot/__manifest__.py new file mode 100644 index 000000000000..586b5f485351 --- /dev/null +++ b/stock_valuation_fifo_lot/__manifest__.py @@ -0,0 +1,21 @@ +# Copyright 2023 Ecosoft Co., Ltd (https://ecosoft.co.th) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) + +{ + "name": "Stock Valuation Fifo Lot", + "version": "16.0.1.0.0", + "category": "Warehouse Management", + "development_status": "Alpha", + "license": "AGPL-3", + "author": "Ecosoft, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/stock-logistics-workflow", + "depends": [ + "stock_account", + "stock_no_negative", + ], + "data": [ + "views/stock_valuation_layer_views.xml", + ], + "installable": True, + "maintainers": ["newtratip"], +} diff --git a/stock_valuation_fifo_lot/models/__init__.py b/stock_valuation_fifo_lot/models/__init__.py new file mode 100644 index 000000000000..c5490433909a --- /dev/null +++ b/stock_valuation_fifo_lot/models/__init__.py @@ -0,0 +1,5 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) + +from . import product +from . import stock_move +from . import stock_valuation_layer diff --git a/stock_valuation_fifo_lot/models/product.py b/stock_valuation_fifo_lot/models/product.py new file mode 100644 index 000000000000..6d652d5d0f4c --- /dev/null +++ b/stock_valuation_fifo_lot/models/product.py @@ -0,0 +1,122 @@ +# Copyright 2023 Ecosoft Co., Ltd (https://ecosoft.co.th) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) + +from odoo import models +from odoo.tools import float_is_zero + + +class ProductProduct(models.Model): + _inherit = "product.product" + + def _sort_by_all_candidates(self, all_candidates, sort_by): + """Hook function for other sort by""" + return all_candidates + + def _get_all_candidates(self, company, sort_by=None): + all_candidates = ( + self.env["stock.valuation.layer"] + .sudo() + .search( + [ + ("product_id", "=", self.id), + ("remaining_qty", ">", 0), + ("company_id", "=", company.id), + ] + ) + ) + if sort_by == "lot_create_date": + + def sorting_key(candidate): + if len(candidate.lot_ids) > 1: + return min(candidate.lot_ids.mapped("create_date")) + elif candidate.lot_ids: + return candidate.lot_ids[0].create_date + else: + return candidate.create_date + + all_candidates = all_candidates.sorted(key=sorting_key) + elif sort_by is not None: + all_candidates = self._sort_by_all_candidates(all_candidates, sort_by) + return all_candidates + + def _run_fifo(self, quantity, company): + self.ensure_one() + move_id = self._context.get("used_in_move_id") + if self.tracking == "none" or not move_id: + vals = super()._run_fifo(quantity, company) + else: + move = self.env["stock.move"].browse(move_id) + move_lines = move._get_out_move_lines() + tmp_value = 0 + tmp_remaining_qty = 0 + for move_line in move_lines: + # Find back incoming stock valuation layers + # (called candidates here) to value `quantity`. + qty_to_take_on_candidates = move_line.product_uom_id._compute_quantity( + move_line.qty_done, move.product_id.uom_id + ) + candidates = self._get_all_candidates(company).filtered( + lambda l: move_line.lot_id in l.lot_ids + ) + for candidate in candidates: + qty_taken_on_candidate = min( + qty_to_take_on_candidates, candidate.remaining_qty + ) + + candidate_unit_cost = ( + candidate.remaining_value / candidate.remaining_qty + ) + value_taken_on_candidate = ( + qty_taken_on_candidate * candidate_unit_cost + ) + value_taken_on_candidate = candidate.currency_id.round( + value_taken_on_candidate + ) + new_remaining_value = ( + candidate.remaining_value - value_taken_on_candidate + ) + + candidate_vals = { + "remaining_qty": candidate.remaining_qty + - qty_taken_on_candidate, + "remaining_value": new_remaining_value, + } + + candidate.write(candidate_vals) + + qty_to_take_on_candidates -= qty_taken_on_candidate + tmp_value += value_taken_on_candidate + + if float_is_zero( + qty_to_take_on_candidates, + precision_rounding=self.uom_id.rounding, + ): + break + + if candidates and qty_to_take_on_candidates > 0: + tmp_value += abs(candidate.unit_cost * -qty_to_take_on_candidates) + tmp_remaining_qty += qty_to_take_on_candidates + + # Calculate standard price (Sorted by lot created date) + all_candidates = self._get_all_candidates( + company, sort_by="lot_create_date" + ) + new_standard_price = 0.0 + if all_candidates: + new_standard_price = all_candidates[0].unit_cost + elif candidates: + new_standard_price = candidate.unit_cost + + # Update standard price + if new_standard_price and self.cost_method == "fifo": + self.sudo().with_company(company.id).with_context( + disable_auto_svl=True + ).standard_price = new_standard_price + + # Value + vals = { + "remaining_qty": -tmp_remaining_qty, + "value": -tmp_value, + "unit_cost": tmp_value / (quantity + tmp_remaining_qty), + } + return vals diff --git a/stock_valuation_fifo_lot/models/stock_move.py b/stock_valuation_fifo_lot/models/stock_move.py new file mode 100644 index 000000000000..11690bfd2134 --- /dev/null +++ b/stock_valuation_fifo_lot/models/stock_move.py @@ -0,0 +1,97 @@ +# Copyright 2023 Ecosoft Co., Ltd (https://ecosoft.co.th) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) + +from odoo import models + + +class StockMove(models.Model): + _inherit = "stock.move" + + def _prepare_common_svl_vals(self): + """ + Prepare lots/serial numbers on stock valuation report + """ + self.ensure_one() + res = super()._prepare_common_svl_vals() + res.update( + { + "lot_ids": [(6, 0, self.lot_ids.ids)], + } + ) + return res + + def _create_out_svl(self, forced_quantity=None): + """ + Send context current move to _create_out_svl function + """ + layers = self.env["stock.valuation.layer"] + for move in self: + move = move.with_context(used_in_move_id=move.id) + layer = super(StockMove, move)._create_out_svl( + forced_quantity=forced_quantity + ) + layers |= layer + return layers + + def _create_in_svl(self, forced_quantity=None): + """ + 1. Check stock move - Multiple lot on the stock move is not + allowed for incoming transfer + 2. Change product standard price to first available lot price + """ + layers = self.env["stock.valuation.layer"] + for move in self: + layer = super(StockMove, move)._create_in_svl( + forced_quantity=forced_quantity + ) + # Calculate standard price (Sorted by lot created date) + if ( + move.product_id.cost_method == "fifo" + and move.product_id.tracking != "none" + ): + all_candidates = move.product_id._get_all_candidates( + move.company_id, sort_by="lot_create_date" + ) + if all_candidates: + move.product_id.sudo().with_company( + move.company_id.id + ).with_context( + disable_auto_svl=True + ).standard_price = all_candidates[ + 0 + ].unit_cost + layers |= layer + return layers + + def _get_price_unit(self): + """ + No PO, Get price unit from lot price + """ + self.ensure_one() + price_unit = super()._get_price_unit() + if ( + not self.purchase_line_id + and self.product_id.cost_method == "fifo" + and len(self.lot_ids) == 1 + ): + candidates = ( + self.env["stock.valuation.layer"] + .sudo() + .search( + [ + ("product_id", "=", self.product_id.id), + ( + "lot_ids", + "in", + self.lot_ids.ids, + ), + ("quantity", ">", 0), + ("value", ">", 0), + ("company_id", "=", self.company_id.id), + ], + limit=1, + ) + ) + if candidates: + price_unit = candidates[0].unit_cost + return price_unit diff --git a/stock_valuation_fifo_lot/models/stock_valuation_layer.py b/stock_valuation_fifo_lot/models/stock_valuation_layer.py new file mode 100644 index 000000000000..0b2c10d4a0d7 --- /dev/null +++ b/stock_valuation_fifo_lot/models/stock_valuation_layer.py @@ -0,0 +1,13 @@ +# Copyright 2023 Ecosoft Co., Ltd (https://ecosoft.co.th) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) + +from odoo import fields, models + + +class StockValuationLayer(models.Model): + _inherit = "stock.valuation.layer" + + lot_ids = fields.Many2many( + comodel_name="stock.lot", + string="Lots/Serial Numbers", + ) diff --git a/stock_valuation_fifo_lot/readme/CONTRIBUTORS.rst b/stock_valuation_fifo_lot/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000000..eef9b340fbba --- /dev/null +++ b/stock_valuation_fifo_lot/readme/CONTRIBUTORS.rst @@ -0,0 +1,5 @@ +* `Ecosoft `__: + + * Tharathip Chaweewongphan + * Saran Limpajitkutaporn + * Pimolnat Suntian diff --git a/stock_valuation_fifo_lot/readme/DESCRIPTION.rst b/stock_valuation_fifo_lot/readme/DESCRIPTION.rst new file mode 100644 index 000000000000..e17874381dea --- /dev/null +++ b/stock_valuation_fifo_lot/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module is used to calculate FIFO cost by lot. diff --git a/stock_valuation_fifo_lot/static/description/index.html b/stock_valuation_fifo_lot/static/description/index.html new file mode 100644 index 000000000000..cd3867593647 --- /dev/null +++ b/stock_valuation_fifo_lot/static/description/index.html @@ -0,0 +1,433 @@ + + + + + +Stock Valuation Fifo Lot + + + +
+

Stock Valuation Fifo Lot

+ + +

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

+

This module is used to calculate FIFO cost by lot.

+
+

Important

+

This is an alpha version, the data model and design can change at any time without warning. +Only for development or testing purpose, do not use in production. +More details on development status

+
+

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

+
    +
  • Ecosoft
  • +
+
+
+

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.

+

Current maintainer:

+

newtratip

+

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_valuation_fifo_lot/views/stock_valuation_layer_views.xml b/stock_valuation_fifo_lot/views/stock_valuation_layer_views.xml new file mode 100644 index 000000000000..1901131c87ce --- /dev/null +++ b/stock_valuation_fifo_lot/views/stock_valuation_layer_views.xml @@ -0,0 +1,23 @@ + + + stock.valuation.layer.tree + stock.valuation.layer + + + + + + + + + + stock.valuation.layer.form + stock.valuation.layer + + + + + + + +