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: Migration to 16.0 #1676

Open
wants to merge 17 commits into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 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,
)
6 changes: 6 additions & 0 deletions setup/stock_valuation_fifo_lot_mrp_landed_cost/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.
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
19 changes: 19 additions & 0 deletions stock_valuation_fifo_lot/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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/res_config_settings_views.xml",
"views/stock_valuation_layer_views.xml",
],
"installable": True,
"maintainers": ["newtratip"],
}
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 ""
7 changes: 7 additions & 0 deletions stock_valuation_fifo_lot/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 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_move
from . import stock_valuation_layer
111 changes: 111 additions & 0 deletions stock_valuation_fifo_lot/models/product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# 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_fifo_candidates(self, company):
all_candidates = super()._get_fifo_candidates(company)
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"))
else:
return candidate.create_date

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

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L24

Added line #L24 was not covered by tests

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)

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

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L28

Added line #L28 was not covered by tests
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:
return super()._run_fifo(quantity, company)

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
)
# Find incoming stock valuation layers that have lot_ids on their moves
# Check with stock_move_id.lot_ids to cover the situation where the stock
# was received either before or after the installation of this module
candidates = self._get_fifo_candidates(company).filtered(
lambda l: move_line.lot_id in l.stock_move_id.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

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

View check run for this annotation

Codecov / codecov/patch

stock_valuation_fifo_lot/models/product.py#L86-L87

Added lines #L86 - L87 were not covered by tests

# Calculate standard price (Sorted by lot created date)
all_candidates = self.with_context(
sort_by="lot_create_date"
)._get_fifo_candidates(company)
new_standard_price = 0.0
if all_candidates:
new_standard_price = all_candidates[0].unit_cost
elif candidates:
new_standard_price = candidate.unit_cost

Check warning on line 97 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

Added line #L97 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(
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
12 changes: 12 additions & 0 deletions stock_valuation_fifo_lot/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 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_get_price_unit_fifo = fields.Boolean(
default=True, help="Use the FIFO price unit by lot when there is no PO."
)
14 changes: 14 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,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 ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"

use_lot_get_price_unit_fifo = fields.Boolean(
related="company_id.use_lot_get_price_unit_fifo",
readonly=False,
help="Use the FIFO price unit by lot when there is no PO.",
)
Loading
Loading