Skip to content

Commit

Permalink
[17.0][UPD] osi_fifo_serialized_fix: overhaul functionality for sales…
Browse files Browse the repository at this point in the history
… and MOs
  • Loading branch information
cbeddies committed Jun 10, 2024
1 parent 3650e0e commit 3aaafa9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
6 changes: 5 additions & 1 deletion osi_fifo_serialized_fix/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def _run_fifo(self, quantity, company):

# Find back incoming stock valuation layers
# (called candidates here) to value `quantity`.
used_candidates = []
for line in rel_stock_move.move_line_ids:
quantity = line.quantity
qty_to_take_on_candidates = quantity
Expand Down Expand Up @@ -57,10 +58,11 @@ def _run_fifo(self, quantity, company):
}

candidate.write(candidate_vals)
used_candidates.append(candidate.id)

qty_to_take_on_candidates -= qty_taken_on_candidate
tmp_value += value_taken_on_candidate

candidate.write({"used_price": new_standard_price, "used_tmp_value": tmp_value})
if float_is_zero(
qty_to_take_on_candidates, precision_rounding=self.uom_id.rounding
):
Expand All @@ -75,6 +77,7 @@ def _run_fifo(self, quantity, company):
and next_candidates[0].unit_cost
or new_standard_price
)
candidate.write({"used_price": new_standard_price, "used_tmp_value": tmp_value})
break

# Update the standard price with the price of the last used candidate, if any.
Expand Down Expand Up @@ -104,4 +107,5 @@ def _run_fifo(self, quantity, company):
"value": -tmp_value,
"unit_cost": last_fifo_price,
}
vals.update({"used_candidates": used_candidates})
return vals
5 changes: 5 additions & 0 deletions osi_fifo_serialized_fix/models/stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ def _create_out_svl(self, forced_quantity=None):
)._create_out_svl(forced_quantity)
res += result
return res

class StockMoveLine(models.Model):
_inherit = "stock.move.line"

unit_cost = fields.Float()
25 changes: 11 additions & 14 deletions osi_fifo_serialized_fix/models/stock_valuation_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class StockValuationLayer(models.Model):

lot_ids = fields.Many2many("stock.lot", string="Lot ID's")
repair_type = fields.Selection([("add", "ADD"), ("remove", "Remove")])
used_candidates = fields.Many2many("stock.valuation.layer", "stock_valuation_layer_rel", "stock_valuation_layer_id", "new_svl_id")
used_tmp_value = fields.Float()
used_price = fields.Float()

@api.model_create_multi
def create(self, vals_list):
Expand All @@ -35,6 +38,12 @@ def create(self, vals_list):
new_val["value"] = quantity * new_val.get("unit_cost", 0.0) * sign
new_val["remaining_value"] = new_val["value"] if sign > 0 else 0
new_val["lot_ids"] = [lot_id.id]
if stock_move_id.location_dest_id.usage == "customer" or stock_move_id.raw_material_production_id:
if val.get("used_candidates"):
used_candidates = self.env["stock.valuation.layer"].browse(val.get("used_candidates"))
for candidate in used_candidates.filtered(lambda x: x.lot_ids.ids == [lot_id.id]):
new_val["unit_cost"] = candidate.used_price
new_val["value"] = candidate.used_tmp_value * sign
new_vals_list.append(new_val)
for product in lotless_move_lines.mapped("product_id"):
move_lines = stock_move_id.move_line_ids.filtered(lambda x: x.product_id == product)
Expand Down Expand Up @@ -75,19 +84,6 @@ def create(self, vals_list):
{"real_price": total_value / total_qty if total_qty else 1}
)

# Product is consumed in Manufacturing Order
if layer.stock_move_id.raw_material_production_id:
total_lot_price = 0
for lot in layer.lot_ids:
move_lines = layer.stock_move_id.move_line_ids.filtered(lambda x: x.lot_id.id == lot.id)
move_quantity = sum(move_line.quantity for move_line in move_lines)
real_price = move_lines.mapped("lot_id").mapped("real_price")[0]
lot.write({"real_price": real_price})
layer.remaining_value = 0
layer.value = -(real_price * move_quantity)

layer.unit_cost = real_price

if (
layer.stock_move_id.production_id
and layer.stock_move_id.product_id.cost_method in ("fifo", "average")
Expand All @@ -102,7 +98,8 @@ def create(self, vals_list):
and layer.stock_move_id.location_dest_id.usage in ("inventory")
and "inventory_mode" in self._context.keys()
):
layer.unit_cost = sum(layer.mapped("lot_ids").mapped("real_price"))
#TODO Fix how unit_cost is calculated
# layer.unit_cost = sum(layer.mapped("lot_ids").mapped("real_price"))
layer.value = layer.unit_cost * layer.quantity
return res

Expand Down

0 comments on commit 3aaafa9

Please sign in to comment.