Skip to content

Commit

Permalink
[IMP] add some ux improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronHForgeFlow authored and EvaSForgeFlow committed Jun 30, 2023
1 parent 0059bf1 commit c913644
Showing 1 changed file with 64 additions and 2 deletions.
66 changes: 64 additions & 2 deletions stock_account_product_run_fifo_hook/hooks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Copyright 2020 ForgeFlow, S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from odoo import _
from odoo.tools import float_is_zero

from odoo.addons.stock_account.models.product import ProductProduct
from odoo.addons.stock_account.models.stock_move import StockMove


# flake8: noqa: C901
Expand All @@ -22,17 +24,23 @@ def _run_fifo_new(self, quantity, company):
# END HOOK Search Candidates
new_standard_price = 0
tmp_value = 0 # to accumulate the value taken on the candidates
taken_data = {}
for candidate in candidates:
qty_taken_on_candidate = min(
qty_to_take_on_candidates, candidate.remaining_qty
)

taken_data[candidate.id] = {"quantity": qty_taken_on_candidate}
candidate_unit_cost = candidate.remaining_value / candidate.remaining_qty
new_standard_price = candidate_unit_cost
value_taken_on_candidate = qty_taken_on_candidate * candidate_unit_cost
value_taken_on_candidate = candidate.currency_id.round(
value_taken_on_candidate
)
taken_data[candidate.id].update(
{
"value": 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,
Expand Down Expand Up @@ -93,6 +101,7 @@ def _run_fifo_new(self, quantity, company):
vals = {
"value": -tmp_value,
"unit_cost": tmp_value / quantity,
"taken_data": taken_data,
}
else:
assert qty_to_take_on_candidates > 0
Expand Down Expand Up @@ -137,6 +146,12 @@ def _run_fifo_vacuum_new(self, company=None):
("remaining_qty", ">", 0),
("create_date", ">=", svls_to_vacuum[0].create_date),
]
if self.env.context.get("use_past_svl", False):
domain = [
("company_id", "=", company.id),
("product_id", "=", self.id),
("remaining_qty", ">", 0),
]
all_candidates = self.env["stock.valuation.layer"].sudo().search(domain)
for svl_to_vacuum in svls_to_vacuum:
# We don't use search to avoid executing _flush_search and to decrease interaction with DB
Expand All @@ -145,15 +160,19 @@ def _run_fifo_vacuum_new(self, company=None):
or r.create_date == svl_to_vacuum.create_date
and r.id > svl_to_vacuum.id
)
if self.env.context.get("use_past_svl", False):
candidates = all_candidates
if not candidates:
break
qty_to_take_on_candidates = abs(svl_to_vacuum.remaining_qty)
qty_taken_on_candidates = 0
tmp_value = 0
taken_data = {}
for candidate in candidates:
qty_taken_on_candidate = min(
candidate.remaining_qty, qty_to_take_on_candidates
)
taken_data[candidate.id] = {"quantity": qty_taken_on_candidate}
qty_taken_on_candidates += qty_taken_on_candidate

candidate_unit_cost = (
Expand All @@ -163,6 +182,11 @@ def _run_fifo_vacuum_new(self, company=None):
value_taken_on_candidate = candidate.currency_id.round(
value_taken_on_candidate
)
taken_data[candidate.id].update(
{
"value": value_taken_on_candidate,
}
)
new_remaining_value = (
candidate.remaining_value - value_taken_on_candidate
)
Expand Down Expand Up @@ -197,7 +221,7 @@ def _run_fifo_vacuum_new(self, company=None):
)
new_remaining_qty = svl_to_vacuum.remaining_qty + qty_taken_on_candidates
corrected_value = remaining_value_before_vacuum - tmp_value
svl_to_vacuum.write(
svl_to_vacuum.with_context(taken_data=taken_data).write(
{
"remaining_qty": new_remaining_qty,
}
Expand Down Expand Up @@ -250,3 +274,41 @@ def _run_fifo_vacuum_new(self, company=None):
if not hasattr(ProductProduct, "_run_fifo_vacuum_original"):
ProductProduct._run_fifo_vacuum_original = ProductProduct._run_fifo_vacuum
ProductProduct._run_fifo_vacuum = _run_fifo_vacuum_new

def _create_out_svl_new(self, forced_quantity=None):
"""Create a `stock.valuation.layer` from `self`.
:param forced_quantity: under some circunstances, the quantity to value is different than
the initial demand of the move (Default value = None)
"""
svl_vals_list = []
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.qty_done, move.product_id.uom_id
)
if float_is_zero(
forced_quantity or valued_quantity,
precision_rounding=move.product_id.uom_id.rounding,
):
continue
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)
return self.env["stock.valuation.layer"].sudo().create(svl_vals_list)

if not hasattr(StockMove, "_run_fifo_vacuum_original"):
StockMove._create_out_svl_original = StockMove._create_out_svl
StockMove._create_out_svl = _create_out_svl_new

0 comments on commit c913644

Please sign in to comment.