Skip to content
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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions setup/stock_valuation_fifo_lot/setup.py
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,
)
93 changes: 93 additions & 0 deletions stock_valuation_fifo_lot/README.rst
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.
3 changes: 3 additions & 0 deletions stock_valuation_fifo_lot/__init__.py
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
21 changes: 21 additions & 0 deletions stock_valuation_fifo_lot/__manifest__.py
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"],
}
5 changes: 5 additions & 0 deletions stock_valuation_fifo_lot/models/__init__.py
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
122 changes: 122 additions & 0 deletions stock_valuation_fifo_lot/models/product.py
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

Check warning on line 13 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L13

Added line #L13 was not covered by tests

def _get_all_candidates(self, company, sort_by=None):
Copy link
Contributor

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?

all_candidates = (

Check warning on line 16 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L16

Added line #L16 was not covered by tests
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):

Check warning on line 29 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L29

Added line #L29 was not covered by tests
if len(candidate.lot_ids) > 1:
return min(candidate.lot_ids.mapped("create_date"))

Check warning on line 31 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L31

Added line #L31 was not covered by tests
elif candidate.lot_ids:
return candidate.lot_ids[0].create_date

Check warning on line 33 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L33

Added line #L33 was not covered by tests
else:
return candidate.create_date

Check warning on line 35 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L35

Added line #L35 was not covered by tests

all_candidates = all_candidates.sorted(key=sorting_key)

Check warning on line 37 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L37

Added line #L37 was not covered by tests
elif sort_by is not None:
all_candidates = self._sort_by_all_candidates(all_candidates, sort_by)
return all_candidates

Check warning on line 40 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L39-L40

Added lines #L39 - L40 were not covered by tests

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

Check warning on line 51 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L48-L51

Added lines #L48 - L51 were not covered by tests
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(

Check warning on line 55 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L55

Added line #L55 was not covered by tests
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(

Check warning on line 62 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L62

Added line #L62 was not covered by tests
qty_to_take_on_candidates, candidate.remaining_qty
)

candidate_unit_cost = (

Check warning on line 66 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L66

Added line #L66 was not covered by tests
candidate.remaining_value / candidate.remaining_qty
)
value_taken_on_candidate = (

Check warning on line 69 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L69

Added line #L69 was not covered by tests
qty_taken_on_candidate * candidate_unit_cost
)
value_taken_on_candidate = candidate.currency_id.round(

Check warning on line 72 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L72

Added line #L72 was not covered by tests
value_taken_on_candidate
)
new_remaining_value = (

Check warning on line 75 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L75

Added line #L75 was not covered by tests
candidate.remaining_value - value_taken_on_candidate
)

candidate_vals = {

Check warning on line 79 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L79

Added line #L79 was not covered by tests
"remaining_qty": candidate.remaining_qty
- qty_taken_on_candidate,
"remaining_value": new_remaining_value,
}

candidate.write(candidate_vals)

Check warning on line 85 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L85

Added line #L85 was not covered by tests

qty_to_take_on_candidates -= qty_taken_on_candidate
tmp_value += value_taken_on_candidate

Check warning on line 88 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L87-L88

Added lines #L87 - L88 were not covered by tests

if float_is_zero(
qty_to_take_on_candidates,
precision_rounding=self.uom_id.rounding,
):
break

Check warning on line 94 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L94

Added line #L94 was not covered by tests

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

Check warning on line 98 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L97-L98

Added lines #L97 - L98 were not covered by tests

# Calculate standard price (Sorted by lot created date)
all_candidates = self._get_all_candidates(

Check warning on line 101 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L101

Added line #L101 was not covered by tests
company, sort_by="lot_create_date"
)
new_standard_price = 0.0

Check warning on line 104 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L104

Added line #L104 was not covered by tests
if all_candidates:
new_standard_price = all_candidates[0].unit_cost

Check warning on line 106 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L106

Added line #L106 was not covered by tests
elif candidates:
new_standard_price = candidate.unit_cost

Check warning on line 108 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L108

Added line #L108 was not covered by tests

# Update standard price
if new_standard_price and self.cost_method == "fifo":
self.sudo().with_company(company.id).with_context(

Check warning on line 112 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L112

Added line #L112 was not covered by tests
disable_auto_svl=True
).standard_price = new_standard_price

# Value
vals = {

Check warning on line 117 in stock_valuation_fifo_lot/models/product.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L117

Added line #L117 was not covered by tests
"remaining_qty": -tmp_remaining_qty,
"value": -tmp_value,
"unit_cost": tmp_value / (quantity + tmp_remaining_qty),
}
return vals
97 changes: 97 additions & 0 deletions stock_valuation_fifo_lot/models/stock_move.py
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(

Check warning on line 52 in stock_valuation_fifo_lot/models/stock_move.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/stock_move.py#L52

Added line #L52 was not covered by tests
move.company_id, sort_by="lot_create_date"
)
if all_candidates:
move.product_id.sudo().with_company(

Check warning on line 56 in stock_valuation_fifo_lot/models/stock_move.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/stock_move.py#L56

Added line #L56 was not covered by tests
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 = (

Check warning on line 77 in stock_valuation_fifo_lot/models/stock_move.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/stock_move.py#L77

Added line #L77 was not covered by tests
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

Check warning on line 96 in stock_valuation_fifo_lot/models/stock_move.py

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/stock_move.py#L96

Added line #L96 was not covered by tests
return price_unit
13 changes: 13 additions & 0 deletions stock_valuation_fifo_lot/models/stock_valuation_layer.py
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",
)
5 changes: 5 additions & 0 deletions stock_valuation_fifo_lot/readme/CONTRIBUTORS.rst
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]>
1 change: 1 addition & 0 deletions stock_valuation_fifo_lot/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module is used to calculate FIFO cost by lot.
Loading
Loading