-
Notifications
You must be signed in to change notification settings - Fork 2
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
[4650][ADD] stock_valuation_fifo_lot #156
base: 16.0
Are you sure you want to change the base?
Changes from 2 commits
fe8efb7
86aef2f
d4945b9
84a2277
3f0c591
9d5d4d2
fa44d0d
647c3c0
88e3356
afbb94d
adaaf0b
c227feb
21c8a7e
d6f910c
94d03a6
7f11c13
4140a04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../stock_valuation_fifo_lot |
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, | ||
) |
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. |
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 |
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"], | ||
} |
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 |
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 | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
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) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
and then remove the else ladder to save some space and improve the readability. |
||||||||||||||
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 | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this have been the intention? Please double check.
Suggested change
|
||||||||||||||
|
||||||||||||||
# 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 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AungKoKoLin1997 I actually have no idea what this adjustment is for. Can you please explain what it is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This adjustment is for using first lot product price when we receive this product.
Now, the standard price of product is 100.
After that, the standard price of product is 200.
We want to assign this price as standard cost because this lot is created first. So, the standard price is 300 with current design. The odoo standard behavior will keep 200 as standard price. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Is the assumption here to change the price of the PO line for the first receipt of this lot from 100 (or 200) to 300, and then receive on the same stock move line as the first receipt by increasing the done quantity (sounds like a corner-case scenario to me)?
I fail to see the point of doing this manipulation. What business goal does it try to address? |
||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't feel comfortable with the current logic. I think this logic is primarily for increasing the quantity of the existing lot in an inventory adjustment, IIUC. I think the correct approach may be just to follow the standard logic, or do the following (ideally based on the configuration):
@AungKoKoLin1997 What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this is also for using lot price that is used when we create incoming or outgoing transfers. |
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", | ||
) |
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]> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This module is used to calculate FIFO cost by lot. |
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.
@AungKoKoLin1997 Please follow up on your own comment OCA/stock-logistics-workflow#1527 (review), first by creating a new PR in the OCA repo perhaps (I think we've given it enough time).