Skip to content

Commit

Permalink
[MIG] stock_account_product_run_fifo_hook: Migration to 18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
JasminSForgeFlow authored and MiquelRForgeFlow committed Dec 17, 2024
1 parent c4075cb commit 55a1e66
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 41 deletions.
10 changes: 5 additions & 5 deletions stock_account_product_run_fifo_hook/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ Stock Account Product Run FIFO Hook
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-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/17.0/stock_account_product_run_fifo_hook
:target: https://github.com/OCA/stock-logistics-workflow/tree/18.0/stock_account_product_run_fifo_hook
: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-17-0/stock-logistics-workflow-17-0-stock_account_product_run_fifo_hook
:target: https://translation.odoo-community.org/projects/stock-logistics-workflow-18-0/stock-logistics-workflow-18-0-stock_account_product_run_fifo_hook
: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=17.0
:target: https://runboat.odoo-community.org/builds?repo=OCA/stock-logistics-workflow&target_branch=18.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|
Expand Down Expand Up @@ -51,7 +51,7 @@ 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_account_product_run_fifo_hook%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/stock-logistics-workflow/issues/new?body=module:%20stock_account_product_run_fifo_hook%0Aversion:%2018.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.

Expand Down Expand Up @@ -83,6 +83,6 @@ 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.

This module is part of the `OCA/stock-logistics-workflow <https://github.com/OCA/stock-logistics-workflow/tree/17.0/stock_account_product_run_fifo_hook>`_ project on GitHub.
This module is part of the `OCA/stock-logistics-workflow <https://github.com/OCA/stock-logistics-workflow/tree/18.0/stock_account_product_run_fifo_hook>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 1 addition & 1 deletion stock_account_product_run_fifo_hook/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Stock Account Product Run FIFO Hook",
"summary": "Add more flexibility in the run fifo method.",
"author": "ForgeFlow, Odoo Community Association (OCA)",
"version": "17.0.1.0.1",
"version": "18.0.1.0.1",
"category": "Warehouse Management",
"website": "https://github.com/OCA/stock-logistics-workflow",
"license": "LGPL-3",
Expand Down
98 changes: 66 additions & 32 deletions stock_account_product_run_fifo_hook/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

# flake8: noqa: C901
def post_load_hook():
def _run_fifo_new(self, quantity, company):
def _run_fifo_new(self, quantity, company, lot=False):
if not hasattr(self, "_run_fifo_prepare_candidate_update"):
return self._run_fifo_original(quantity, company)
return self._run_fifo_original(quantity, company, lot)

Check warning on line 17 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L17

Added line #L17 was not covered by tests

# Find back incoming stock valuation layers (called candidates here)
# to value `quantity`.
# Find back incoming stock valuation layers
# (called candidates here) to value `quantity`.
qty_to_take_on_candidates = quantity
candidates = self._get_fifo_candidates(company)
candidates = self._get_fifo_candidates(company, lot=lot)
new_standard_price = 0
tmp_value = 0 # to accumulate the value taken on the candidates
taken_data = {}

Check warning on line 25 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L21-L25

Added lines #L21 - L25 were not covered by tests
Expand All @@ -40,6 +40,7 @@ def _run_fifo_new(self, quantity, company):
}
)
new_remaining_value = candidate.remaining_value - value_taken_on_candidate

Check warning on line 42 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L42

Added line #L42 was not covered by tests

candidate_vals = {

Check warning on line 44 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L44

Added line #L44 was not covered by tests
"remaining_qty": candidate.remaining_qty - qty_taken_on_candidate,
"remaining_value": new_remaining_value,
Expand All @@ -56,6 +57,7 @@ def _run_fifo_new(self, quantity, company):

qty_to_take_on_candidates -= qty_taken_on_candidate
tmp_value += value_taken_on_candidate

Check warning on line 59 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L58-L59

Added lines #L58 - L59 were not covered by tests

if float_is_zero(
qty_to_take_on_candidates, precision_rounding=self.uom_id.rounding
):
Expand Down Expand Up @@ -155,6 +157,7 @@ def _run_fifo_vacuum_new(self, company=None):
)
min_create_date = min(min_create_date, group[2])
all_candidates_by_product = defaultdict(lambda: ValuationLayer)
lot_to_update = []
domain = [

Check warning on line 161 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L158-L161

Added lines #L158 - L161 were not covered by tests
("product_id", "in", self.ids),
("remaining_qty", ">", 0),
Expand All @@ -179,14 +182,19 @@ def _run_fifo_vacuum_new(self, company=None):
all_candidates = all_candidates_by_product[product.id]
current_real_time_svls = ValuationLayer

Check warning on line 183 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L182-L183

Added lines #L182 - L183 were not covered by tests
for svl_to_vacuum in svls_to_vacuum_by_product[product.id]:
# We don't use search to avoid executing _flush_search
# and to decrease interaction with DB
# We don't use search to avoid executing
# _flush_search and to decrease interaction with DB
candidates = all_candidates.filtered(

Check warning on line 187 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L187

Added line #L187 was not covered by tests
lambda r, svl_to_vacuum=svl_to_vacuum: r.create_date
> svl_to_vacuum.create_date
or r.create_date == svl_to_vacuum.create_date
and r.id > svl_to_vacuum.id
)
if product.lot_valuated:
candidates = candidates.filtered(

Check warning on line 194 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L194

Added line #L194 was not covered by tests
lambda r, svl_to_vacuum=svl_to_vacuum: r.lot_id
== svl_to_vacuum.lot_id
)
if self.env.context.get("use_past_svl", False):
candidates = all_candidates

Check warning on line 199 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L199

Added line #L199 was not covered by tests
if not candidates:
Expand Down Expand Up @@ -263,8 +271,8 @@ def _run_fifo_vacuum_new(self, company=None):
}
)

# Don't create a layer or an accounting
# entry if the corrected value is zero.
# Don't create a layer or an accounting entry
# if the corrected value is zero.
if svl_to_vacuum.currency_id.is_zero(corrected_value):
continue

Check warning on line 277 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L277

Added line #L277 was not covered by tests

Expand All @@ -288,8 +296,10 @@ def _run_fifo_vacuum_new(self, company=None):
"description": "Revaluation of %s (negative inventory)"
% (move.picking_id.name or move.name),
"stock_valuation_layer_id": svl_to_vacuum.id,
"lot_id": svl_to_vacuum.lot_id.id,
}
)
lot_to_update.append(svl_to_vacuum.lot_id)

Check warning on line 302 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L302

Added line #L302 was not covered by tests
if product.valuation == "real_time":
current_real_time_svls |= svl_to_vacuum
real_time_svls_to_vacuum |= current_real_time_svls
Expand All @@ -301,12 +311,22 @@ def _run_fifo_vacuum_new(self, company=None):
product = product.with_company(company.id)

Check warning on line 311 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L311

Added line #L311 was not covered by tests
if not svls_to_vacuum_by_product[product.id]:
continue

Check warning on line 313 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L313

Added line #L313 was not covered by tests
if product.cost_method in ["average", "fifo"] and not float_is_zero(
if product.cost_method not in ["average", "fifo"] or float_is_zero(
product.quantity_svl, precision_rounding=product.uom_id.rounding
):
product.sudo().with_context(disable_auto_svl=True).write(
{"standard_price": product.value_svl / product.quantity_svl}
)
continue

Check warning on line 317 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L317

Added line #L317 was not covered by tests
if product.lot_valuated:
for lot in lot_to_update:
if float_is_zero(
lot.quantity_svl, precision_rounding=product.uom_id.rounding
):
continue
lot.sudo().with_context(disable_auto_svl=True).write(

Check warning on line 324 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L323-L324

Added lines #L323 - L324 were not covered by tests
{"standard_price": lot.value_svl / lot.quantity_svl}
)
product.sudo().with_context(disable_auto_svl=True).write(

Check warning on line 327 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L327

Added line #L327 was not covered by tests
{"standard_price": product.value_svl / product.quantity_svl}
)

vacuum_svls._validate_accounting_entries()
self._create_fifo_vacuum_anglo_saxon_expense_entries(

Check warning on line 332 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L331-L332

Added lines #L331 - L332 were not covered by tests
Expand All @@ -321,29 +341,43 @@ def _get_out_svl_vals_new(self, forced_quantity):
svl_vals_list = []

Check warning on line 341 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L341

Added line #L341 was not covered by tests
for move in self:
move = move.with_company(move.company_id)
valued_move_lines = move._get_out_move_lines()
valued_quantity = 0
for valued_move_line in valued_move_lines:
valued_quantity += valued_move_line.product_uom_id._compute_quantity(
valued_move_line.quantity, move.product_id.uom_id
)
lines = move._get_out_move_lines()
quantities = defaultdict(float)

Check warning on line 345 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L343-L345

Added lines #L343 - L345 were not covered by tests
if forced_quantity:
quantities[forced_quantity[0]] += forced_quantity[1]

Check warning on line 347 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L347

Added line #L347 was not covered by tests
else:
for line in lines:
quantities[line.lot_id] += line.product_uom_id._compute_quantity(

Check warning on line 350 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L350

Added line #L350 was not covered by tests
line.quantity, move.product_id.uom_id
)
if float_is_zero(
forced_quantity or valued_quantity,
sum(quantities.values()),
precision_rounding=move.product_id.uom_id.rounding,
):
continue

Check warning on line 357 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L357

Added line #L357 was not covered by tests
svl_vals = move.product_id.with_context(
move_requestor=move.id
)._prepare_out_svl_vals(forced_quantity or valued_quantity, move.company_id)
svl_vals.update(move._prepare_common_svl_vals())
if forced_quantity:
svl_vals["description"] = (
_("Correction of %s (modification of past move)")
% move.picking_id.name
or move.name
)
svl_vals["description"] += svl_vals.pop("rounding_adjustment", "")
svl_vals_list.append(svl_vals)

if move.product_id.lot_valuated:
vals = []

Check warning on line 360 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L360

Added line #L360 was not covered by tests
for lot_id, qty in quantities.items():
out_vals = move.product_id.with_context(

Check warning on line 362 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L362

Added line #L362 was not covered by tests
move_requestor=move.id
)._prepare_out_svl_vals(qty, move.company_id, lot=lot_id)
vals.append(out_vals)

Check warning on line 365 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L365

Added line #L365 was not covered by tests
else:
vals = [

Check warning on line 367 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L367

Added line #L367 was not covered by tests
move.product_id.with_context(
move_requestor=move.id
)._prepare_out_svl_vals(sum(quantities.values()), move.company_id)
]
for val in vals:
val.update(move._prepare_common_svl_vals())

Check warning on line 373 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L373

Added line #L373 was not covered by tests
if forced_quantity:
val["description"] = _(

Check warning on line 375 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L375

Added line #L375 was not covered by tests
"Correction of %s (modification of past move)",
move.picking_id.name or move.name,
)
val["description"] += val.pop("rounding_adjustment", "")
svl_vals_list += vals
return svl_vals_list

Check warning on line 381 in stock_account_product_run_fifo_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_account_product_run_fifo_hook/hooks.py#L379-L381

Added lines #L379 - L381 were not covered by tests

if not hasattr(StockMove, "_get_out_svl_vals_original"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ <h1 class="title">Stock Account Product Run FIFO Hook</h1>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:16ff2bf6dff55b6e2490baba84bf78dccb176514585090dd54df0f46c5f7ddcb
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/stock-logistics-workflow/tree/17.0/stock_account_product_run_fifo_hook"><img alt="OCA/stock-logistics-workflow" src="https://img.shields.io/badge/github-OCA%2Fstock--logistics--workflow-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/stock-logistics-workflow-17-0/stock-logistics-workflow-17-0-stock_account_product_run_fifo_hook"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/stock-logistics-workflow&amp;target_branch=17.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/stock-logistics-workflow/tree/18.0/stock_account_product_run_fifo_hook"><img alt="OCA/stock-logistics-workflow" src="https://img.shields.io/badge/github-OCA%2Fstock--logistics--workflow-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/stock-logistics-workflow-18-0/stock-logistics-workflow-18-0-stock_account_product_run_fifo_hook"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/stock-logistics-workflow&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module adds hook points in product_product._run_fifo in order to
add more flexibility in the information that is stored in the fifo
candidates.</p>
Expand Down Expand Up @@ -400,7 +400,7 @@ <h1><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/stock-logistics-workflow/issues">GitHub Issues</a>.
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
<a class="reference external" href="https://github.com/OCA/stock-logistics-workflow/issues/new?body=module:%20stock_account_product_run_fifo_hook%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/OCA/stock-logistics-workflow/issues/new?body=module:%20stock_account_product_run_fifo_hook%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand Down Expand Up @@ -432,7 +432,7 @@ <h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
<p>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.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/stock-logistics-workflow/tree/17.0/stock_account_product_run_fifo_hook">OCA/stock-logistics-workflow</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/stock-logistics-workflow/tree/18.0/stock_account_product_run_fifo_hook">OCA/stock-logistics-workflow</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
</div>
Expand Down

0 comments on commit 55a1e66

Please sign in to comment.