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

[4650][ADD] stock_valuation_fifo_lot #156

Open
wants to merge 17 commits into
base: 16.0
Choose a base branch
from
Open
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,
)
104 changes: 104 additions & 0 deletions stock_valuation_fifo_lot/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
========================
Stock Valuation Fifo Lot
========================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:877af52a350ab6a61b6c128c4fbcffe909e4a8274c8ec064390dc9b1c36d9253
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |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:

Configuration
=============

If necessary, update the 'Use FIFO cost by lot' setting under Inventory > Configuration > Settings to use the lot cost instead of the standard _get_price() behavior when there is no relation to a purchase order in the stock move.
(enabled by default).

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]>

* `Quartile <https://www.quartile.co>`__:

* 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-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.
4 changes: 4 additions & 0 deletions stock_valuation_fifo_lot/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)

from . import models
from .hooks import post_init_hook
23 changes: 23 additions & 0 deletions stock_valuation_fifo_lot/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2023 Ecosoft Co., Ltd (https://ecosoft.co.th)
# Copyright 2024 Quartile (https://www.quartile.co)
# 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, Quartile, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/stock-logistics-workflow",
"depends": ["stock_account"],
"data": [
"views/res_config_settings_views.xml",
"views/stock_move_line_views.xml",
"views/stock_package_level_views.xml",
"views/stock_valuation_layer_views.xml",
],
"installable": True,
"post_init_hook": "post_init_hook",
"maintainers": ["newtratip"],
}
42 changes: 42 additions & 0 deletions stock_valuation_fifo_lot/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2024 Quartile (https://www.quartile.co)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)

from odoo import SUPERUSER_ID, api
from odoo.tools import float_is_zero


def post_init_hook(cr, registry):
env = api.Environment(cr, SUPERUSER_ID, {})

moves = env["stock.move"].search([("stock_valuation_layer_ids", "!=", False)])
for move in moves:
if (
move.product_id.with_company(move.company_id).cost_method != "fifo"
or not move.lot_ids
):
continue
svls = move.stock_valuation_layer_ids
svls.lot_ids = move.lot_ids
if move._is_out():
remaining_qty = sum(svls.mapped("remaining_qty"))
if remaining_qty:
# The case where outgoing done qty is reduced
# Let the first move line take such adjustments.
move.move_line_ids[0].qty_base = remaining_qty
continue
consumed_qty = consumed_qty_bal = sum(svls.mapped("quantity")) - sum(
svls.mapped("remaining_qty")
)
total_value = sum(svls.mapped("value")) + sum(
svls.stock_valuation_layer_ids.mapped("value")
)
consumed_value = total_value - sum(svls.mapped("remaining_value"))
product_uom = move.product_id.uom_id
for ml in move.move_line_ids.sorted("id"):
ml.qty_base = ml.product_uom_id._compute_quantity(ml.qty_done, product_uom)
if float_is_zero(consumed_qty_bal, precision_rounding=product_uom.rounding):
continue
qty_to_allocate = min(consumed_qty_bal, ml.qty_base)
ml.qty_consumed += qty_to_allocate
consumed_qty_bal -= qty_to_allocate
ml.value_consumed += consumed_value * qty_to_allocate / consumed_qty
39 changes: 39 additions & 0 deletions stock_valuation_fifo_lot/i18n/stock_valuation_fifo_lot.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * stock_valuation_fifo_lot
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 15.0\n"
"Report-Msgid-Bugs-To: \n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: stock_valuation_fifo_lot
#: model:ir.model.fields,field_description:stock_valuation_fifo_lot.field_stock_valuation_layer__lot_ids
msgid "Lots/Serial Numbers"
msgstr ""

#. module: stock_valuation_fifo_lot
#: model:ir.model,name:stock_valuation_fifo_lot.model_product_product
msgid "Product"
msgstr ""

#. module: stock_valuation_fifo_lot
#: model:ir.model,name:stock_valuation_fifo_lot.model_stock_landed_cost
msgid "Stock Landed Cost"
msgstr ""

#. module: stock_valuation_fifo_lot
#: model:ir.model,name:stock_valuation_fifo_lot.model_stock_move
msgid "Stock Move"
msgstr ""

#. module: stock_valuation_fifo_lot
#: model:ir.model,name:stock_valuation_fifo_lot.model_stock_valuation_layer
msgid "Stock Valuation Layer"
msgstr ""
9 changes: 9 additions & 0 deletions stock_valuation_fifo_lot/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)

from . import product
from . import res_company
from . import res_config_settings
from . import stock_lot
from . import stock_move
from . import stock_move_line
from . import stock_valuation_layer
99 changes: 99 additions & 0 deletions stock_valuation_fifo_lot/models/product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Copyright 2023 Ecosoft Co., Ltd (https://ecosoft.co.th)
# Copyright 2024 Quartile (https://www.quartile.co)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)

from collections import defaultdict

from odoo import _, models
from odoo.exceptions import UserError
from odoo.osv import expression
from odoo.tools import float_is_zero


class ProductProduct(models.Model):
_inherit = "product.product"

def _get_fifo_candidates_domain(self, company):
res = super()._get_fifo_candidates_domain(company)
fifo_lot = self.env.context.get("fifo_lot")
if not fifo_lot:
return res
return expression.AND([res, [("lot_ids", "in", fifo_lot.ids)]])

def _sort_by_all_candidates(self, all_candidates, sort_by):
"""Hook function for other sort by"""
return all_candidates

def _get_fifo_candidates(self, company):
all_candidates = super()._get_fifo_candidates(company)
fifo_lot = self.env.context.get("fifo_lot")
if fifo_lot:
for svl in all_candidates:
if not svl._get_unconsumed_in_move_line(fifo_lot):
all_candidates -= svl
if not all_candidates:
raise UserError(
_(
"There is no remaining balance for FIFO valuation for the "
"lot/serial %s. Please select a Force FIFO Lot/Serial in the "
"detailed operation line."
)
% fifo_lot.display_name
)
sort_by = self.env.context.get("sort_by")
if sort_by == "lot_create_date":

def sorting_key(candidate):
if candidate.lot_ids:
return min(candidate.lot_ids.mapped("create_date"))
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

# Depends on https://github.com/odoo/odoo/pull/180245
def _get_qty_taken_on_candidate(self, qty_to_take_on_candidates, candidate):
fifo_lot = self.env.context.get("fifo_lot")
if fifo_lot:
candidate_ml = candidate._get_unconsumed_in_move_line(fifo_lot)
qty_to_take_on_candidates = min(
qty_to_take_on_candidates, candidate_ml.qty_remaining
)
candidate_ml.qty_consumed += qty_to_take_on_candidates
candidate_ml.value_consumed += qty_to_take_on_candidates * (
candidate.remaining_value / candidate.remaining_qty
)
return super()._get_qty_taken_on_candidate(qty_to_take_on_candidates, candidate)

def _run_fifo(self, quantity, company):
self.ensure_one()
fifo_move = self._context.get("fifo_move")
if self.tracking == "none" or not fifo_move:
return super()._run_fifo(quantity, company)
remaining_qty = quantity
vals = defaultdict(float)
correction_ml = self.env.context.get("correction_move_line")
move_lines = correction_ml or fifo_move._get_out_move_lines()
moved_qty = 0
for ml in move_lines:
fifo_lot = ml.force_fifo_lot_id or ml.lot_id
if correction_ml:
moved_qty = quantity
else:
moved_qty = ml.product_uom_id._compute_quantity(
ml.qty_done, self.uom_id
)
fifo_qty = min(remaining_qty, moved_qty)
self = self.with_context(fifo_lot=fifo_lot, fifo_qty=fifo_qty)
ml_fifo_vals = super()._run_fifo(fifo_qty, company)
for key, value in ml_fifo_vals.items():
if key in ("remaining_qty", "value"):
vals[key] += value
continue
vals[key] = value # unit_cost
remaining_qty -= fifo_qty
if float_is_zero(remaining_qty, precision_rounding=self.uom_id.rounding):
break
return vals
14 changes: 14 additions & 0 deletions stock_valuation_fifo_lot/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2024 Quartile (https://www.quartile.co)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from odoo import fields, models


class ResCompany(models.Model):
_inherit = "res.company"

use_lot_cost_for_new_stock = fields.Boolean(
"Use Last Lot/Serial Cost for New Stock",
default=True,
help="Use the lot/serial cost for FIFO products for non-purchase receipts.",
)
15 changes: 15 additions & 0 deletions stock_valuation_fifo_lot/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2024 Quartile (https://www.quartile.co)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from odoo import fields, models


class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"

use_lot_cost_for_new_stock = fields.Boolean(
"Use Last Lot/Serial Cost for New Stock",
related="company_id.use_lot_cost_for_new_stock",
readonly=False,
help="Use the lot/serial cost for FIFO products for non-purchase receipts.",
)
Loading
Loading