-
-
Notifications
You must be signed in to change notification settings - Fork 649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[16.0][MIG] stock_valuation_fifo_lot #1527
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d146777
[15.0][ADD] stock_valuation_fifo_lot
ps-tubtim 2998247
[FIX] sorted by create_date when case no lot_ids or there is 1 date
Saran440 e817032
[FIX] error when not loop
Saran440 f337436
[IMP] stock_valuation_fifo_lot: pre-commit stuff
Saran440 4a1f763
[MIG] stock_valuation_fifo_lot: Migration to 16.0
Saran440 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
setup/stock_valuation_fifo_lot/odoo/addons/stock_valuation_fifo_lot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../stock_valuation_fifo_lot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import setuptools | ||
|
||
setuptools.setup( | ||
setup_requires=['setuptools-odoo'], | ||
odoo_addon=True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <https://odoo-community.org/page/development-status>`_ | ||
|
||
**Table of contents** | ||
|
||
.. contents:: | ||
:local: | ||
|
||
Bug Tracker | ||
=========== | ||
|
||
Bugs are tracked on `GitHub Issues <https://github.com/OCA/stock-logistics-workflow/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 <https://github.com/OCA/stock-logistics-workflow/issues/new?body=module:%20stock_valuation_fifo_lot%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_. | ||
|
||
Do not contact contributors directly about support or help with technical issues. | ||
|
||
Credits | ||
======= | ||
|
||
Authors | ||
~~~~~~~ | ||
|
||
* Ecosoft | ||
|
||
Contributors | ||
~~~~~~~~~~~~ | ||
|
||
* `Ecosoft <http://ecosoft.co.th>`__: | ||
|
||
* Tharathip Chaweewongphan <[email protected]> | ||
* Saran Limpajitkutaporn <[email protected]> | ||
* Pimolnat Suntian <[email protected]> | ||
|
||
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 <https://odoo-community.org/page/maintainer-role>`__: | ||
|
||
|maintainer-newtratip| | ||
|
||
This module is part of the `OCA/stock-logistics-workflow <https://github.com/OCA/stock-logistics-workflow/tree/16.0/stock_valuation_fifo_lot>`_ project on GitHub. | ||
|
||
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) | ||
|
||
from . import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
* `Ecosoft <http://ecosoft.co.th>`__: | ||
|
||
* Tharathip Chaweewongphan <[email protected]> | ||
* Saran Limpajitkutaporn <[email protected]> | ||
* Pimolnat Suntian <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This module is used to calculate FIFO cost by lot. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In odoo 16, we have this kind of function https://github.com/odoo/odoo/blob/4d154a9f08cb2204df8566ed5c1e4f5e59e5d212/addons/stock_account/models/product.py#L331.
Should we extend and passing sort_by as a context?