From 1e2096a7fc88fd226cd9b41ac6d6a15563496711 Mon Sep 17 00:00:00 2001 From: RLeeOSI Date: Tue, 28 Nov 2023 09:54:14 -0800 Subject: [PATCH] [REF] pre-commit --- osi_fifo_serialized_fix/__manifest__.py | 3 +- osi_fifo_serialized_fix/models/stock_move.py | 26 +- .../models/stock_valuation_layer.py | 240 ++++++++++++------ 3 files changed, 183 insertions(+), 86 deletions(-) diff --git a/osi_fifo_serialized_fix/__manifest__.py b/osi_fifo_serialized_fix/__manifest__.py index 153af9555..504cd86b9 100644 --- a/osi_fifo_serialized_fix/__manifest__.py +++ b/osi_fifo_serialized_fix/__manifest__.py @@ -1,4 +1,3 @@ - # Copyright (C) 2021, Open Source Integrators # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). @@ -13,6 +12,6 @@ "category": "Accounting", "depends": ["stock_account", "mrp_account"], "data": ["views/stock_valuation_layer.xml"], - "maintainers": ["osi-scampbell","Chanakya-OSI"], + "maintainers": ["osi-scampbell", "Chanakya-OSI"], "installable": True, } diff --git a/osi_fifo_serialized_fix/models/stock_move.py b/osi_fifo_serialized_fix/models/stock_move.py index 3d8ead884..558a03a5e 100644 --- a/osi_fifo_serialized_fix/models/stock_move.py +++ b/osi_fifo_serialized_fix/models/stock_move.py @@ -3,7 +3,6 @@ from odoo import _, models from odoo.exceptions import UserError -from odoo.tools import float_is_zero, float_repr, float_round, float_compare class StockMove(models.Model): @@ -66,13 +65,20 @@ def get_ji_ids(self, move, svl_ids): else: move.stock_valuation_layer_ids.lot_ids = [(6, 0, svl_ids[0][2])] svl = self.env["stock.valuation.layer"].browse(svl_ids[0][0]) - linked_svl = self.env["stock.valuation.layer"].search([('stock_valuation_layer_id', '=', svl.id)]) + linked_svl = self.env["stock.valuation.layer"].search( + [("stock_valuation_layer_id", "=", svl.id)] + ) unit_cost = svl.unit_cost for link_svl in linked_svl: unit_cost += link_svl.value move.stock_valuation_layer_ids.unit_cost = unit_cost - move.stock_valuation_layer_ids.value = (move.stock_valuation_layer_ids.quantity * move.stock_valuation_layer_ids.unit_cost) - move.stock_valuation_layer_ids.remaining_value = move.stock_valuation_layer_ids.value + move.stock_valuation_layer_ids.value = ( + move.stock_valuation_layer_ids.quantity + * move.stock_valuation_layer_ids.unit_cost + ) + move.stock_valuation_layer_ids.remaining_value = ( + move.stock_valuation_layer_ids.value + ) ji_ids[0].move_id.button_draft() # The Valuation Layer has been changed, # now we have to edit the STJ Entry @@ -96,7 +102,11 @@ def get_svl_ids(self, move): svl_ids = [] # We need to get all of the valuation layers associated with this product test_vals = self.env["stock.valuation.layer"].search( - [("product_id", "=", move.product_id.id), ('stock_valuation_layer_id', '=', False)], order="id desc" + [ + ("product_id", "=", move.product_id.id), + ("stock_valuation_layer_id", "=", False), + ], + order="id desc", ) for line_id in move.move_line_ids: for valuation in test_vals: @@ -116,10 +126,8 @@ def get_svl_ids(self, move): self.increment_qty(valuation.id, svl_ids, line_id.lot_id.id) if len(svl_ids) == 0 and self.location_id.create_je is True: raise UserError( - _( - "Need Check Stock Valution Layer For this Product :- %s" - % (move.product_id.name) - ) + _("Need Check Stock Valution Layer For this Product :- %s") + % (move.product_id.name) ) self.get_ji_ids(move, svl_ids) diff --git a/osi_fifo_serialized_fix/models/stock_valuation_layer.py b/osi_fifo_serialized_fix/models/stock_valuation_layer.py index 0e63e2b3c..d6a59347c 100644 --- a/osi_fifo_serialized_fix/models/stock_valuation_layer.py +++ b/osi_fifo_serialized_fix/models/stock_valuation_layer.py @@ -1,8 +1,7 @@ # Copyright (C) 2023, Open Source Integrators # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). -from odoo import fields, models, api -from odoo.tools import float_is_zero, float_round +from odoo import api, fields, models class StockProductionLot(models.Model): @@ -15,100 +14,197 @@ class StockValuationLayer(models.Model): _inherit = "stock.valuation.layer" lot_ids = fields.Many2many("stock.lot", string="Lot ID's", readonly=True) - repair_type = fields.Selection([('add', 'ADD'), ('remove', 'Remove')], string="Repair Type") + repair_type = fields.Selection([("add", "ADD"), ("remove", "Remove")]) @api.model def create(self, vals): res = super().create(vals) - for layer in res.filtered(lambda l:l.stock_move_id.lot_ids or l.stock_valuation_layer_id.lot_ids): + for layer in res.filtered( + lambda l: l.stock_move_id.lot_ids or l.stock_valuation_layer_id.lot_ids + ): layer.lot_ids = [(6, 0, layer.stock_move_id.lot_ids.ids)] - #Below code is only for Cabintouch as they are using pre-component / pick component location as a Production location - if layer.stock_move_id.mapped('move_dest_ids').mapped('raw_material_production_id'): + # Below code is only for Cabinotch as they are using 2-step manufacturing + if layer.stock_move_id.mapped("move_dest_ids").mapped( + "raw_material_production_id" + ): total_lot_price = 0 for lot in layer.lot_ids: - all_lot_layers = self.search([('lot_ids', 'in', lot.id), ('id', '!=', layer.id)]) + all_lot_layers = self.search( + [("lot_ids", "in", lot.id), ("id", "!=", layer.id)] + ) total_qty = sum([svl.quantity for svl in all_lot_layers]) total_value = sum([svl.value for svl in all_lot_layers]) svl_value = total_value / total_qty if total_qty else 1.0 - lot.write({'real_price':svl_value}) - total_lot_price+=svl_value - layer.value=-total_lot_price - layer.unit_cost = -(total_lot_price / layer.quantity if layer.quantity else 1) + lot.write({"real_price": svl_value}) + total_lot_price += svl_value + layer.value = -total_lot_price + layer.unit_cost = -( + total_lot_price / layer.quantity if layer.quantity else 1 + ) layer.remaining_value = layer.value layer.lot_ids = [(6, 0, [])] - #=================================================================================== - #Update the lot price when we do incoming lots - #update the lot_ids for linked layer. - #i.g. Landed cost layers, Price diff layers. + # Update the lot price when we do incoming lots + # update the lot_ids for linked layer. + # i.g. Landed cost layers, Price diff layers. if layer.stock_valuation_layer_id and not layer.lot_ids: layer.lot_ids = layer.stock_valuation_layer_id.lot_ids - if layer.stock_move_id.location_id.usage in ('inventory', 'supplier'): - layer_ids = self.search([('lot_ids','in',layer.lot_ids.ids)]) + if layer.stock_move_id.location_id.usage in ("inventory", "supplier"): + layer_ids = self.search([("lot_ids", "in", layer.lot_ids.ids)]) total_qty = sum(layer_ids.mapped("quantity")) total_value = sum(layer_ids.mapped("value")) - layer.lot_ids.write({'real_price': total_value/total_qty if total_qty else 1}) + layer.lot_ids.write( + {"real_price": total_value / total_qty if total_qty else 1} + ) - #Update the lot price for the raw material moves. - #Update the layer values for raw materual moves + # Update the lot price for the raw material moves. + # Update the layer values for raw materual moves if layer.stock_move_id.raw_material_production_id: total_lot_price = 0 for lot in layer.lot_ids: - all_lot_layers = self.search([('lot_ids', 'in', lot.id), ('id', '!=', layer.id)]) - # all_moves = all_lot_layers.mapped('stock_move_id') or self.env['stock.move'] - # rawmaterial_moves = all_lot_layers.mapped('stock_move_id').mapped('move_dest_ids').filtered(lambda l : l.raw_material_production_id) or self.env['stock.move'] + all_lot_layers = self.search( + [("lot_ids", "in", lot.id), ("id", "!=", layer.id)] + ) + # all_moves = ( + # all_lot_layers.mapped("stock_move_id") + # or self.env["stock.move"] + # ) + # rawmaterial_moves = all_lot_layers.mapped( + # "stock_move_id" + # ).mapped("move_dest_ids").filtered( + # lambda l : l.raw_material_production_id + # ) or self.env["stock.move"] # required_moves = all_moves - rawmaterial_moves - - # required_layers = self.search([('stock_move_id','in',required_moves.ids)]) + # required_layers = self.search( + # [('stock_move_id','in',required_moves.ids)] + # ) total_qty = sum([svl.quantity for svl in all_lot_layers]) total_value = sum([svl.value for svl in all_lot_layers]) svl_value = total_value / total_qty if total_qty else 1.0 - lot.write({'real_price':svl_value}) - total_lot_price+=svl_value - layer.value=-total_lot_price - layer.unit_cost = -(total_lot_price / layer.quantity if layer.quantity else 1) + lot.write({"real_price": svl_value}) + total_lot_price += svl_value + layer.value = -total_lot_price + layer.unit_cost = -( + total_lot_price / layer.quantity if layer.quantity else 1 + ) layer.remaining_value = layer.value - if layer.stock_move_id.production_id and layer.stock_move_id.product_id.cost_method in ('fifo', 'average'): - layer.stock_move_id.production_id.lot_producing_id.real_price = layer.stock_move_id.price_unit - - #Update the lot price for the FG moves. - #Update the layer values for FG moves -# if layer.stock_move_id.production_id: -# production = layer.stock_move_id.production_id -# work_center_cost = 0 -# consumed_moves = production.move_raw_ids.filtered(lambda x: x.state == 'done') -# finished_move = layer.stock_move_id -# if finished_move and consumed_moves.mapped('lot_ids'): -# finished_move.ensure_one() -# # total_cost = finished_move.price_unit - (sum(-m.stock_valuation_layer_ids.value for m in consumed_moves.filtered(lambda sm : sm.product_id.tracking == 'serial'))) -# qty_done = finished_move.product_uom._compute_quantity(finished_move.quantity_done, finished_move.product_id.uom_id) -# raw_sn_price = sum(lot.real_price for lot in consumed_moves.mapped('lot_ids')) -# raw_non_sn_price = - sum(consumed_moves.filtered(lambda l:l.product_id.tracking !='serial').sudo().stock_valuation_layer_ids.mapped('value')) -# total_cost = finished_move.price_unit + raw_non_sn_price -# byproduct_moves = production.move_byproduct_ids.filtered(lambda m: m.state in ('done', 'cancel') and m.quantity_done > 0) -# byproduct_cost_share = 0 -# for byproduct in byproduct_moves: -# if byproduct.cost_share == 0: -# continue -# byproduct_cost_share += byproduct.cost_share -# if byproduct.product_id.cost_method in ('fifo', 'average') and byproduct.product_id.tracking =='serial': -# byproduct.price_unit = total_cost * byproduct.cost_share / 100 / byproduct.product_uom._compute_quantity(byproduct.quantity_done, byproduct.product_id.uom_id) -# byproduct.stock_valuation_layer_ids.write({'unit_cost':byproduct.price_unit, -# 'value':byproduct.price_unit* byproduct.product_uom._compute_quantity(byproduct.quantity_done, byproduct.product_id.uom_id), -# 'remaining_value' : byproduct.price_unit* byproduct.product_uom._compute_quantity(byproduct.quantity_done, byproduct.product_id.uom_id) -# }) -# byproduct.lot_ids.write({'real_price':byproduct.price_unit}) -# else: -# byproduct.price_unit = total_cost * byproduct.cost_share / 100 / byproduct.product_uom._compute_quantity(byproduct.quantity_done, byproduct.product_id.uom_id) -# byproduct.stock_valuation_layer_ids.write({'unit_cost':byproduct.price_unit, -# 'value':byproduct.price_unit* byproduct.product_uom._compute_quantity(byproduct.quantity_done, byproduct.product_id.uom_id), -# 'remaining_value':byproduct.price_unit* byproduct.product_uom._compute_quantity(byproduct.quantity_done, byproduct.product_id.uom_id)}) -# if finished_move.product_id.cost_method in ('fifo', 'average'): -# finished_move.price_unit = total_cost * float_round(1 - byproduct_cost_share / 100, precision_rounding=0.0001) / qty_done -# production.lot_producing_id.real_price = finished_move.price_unit -# layer.value = finished_move.price_unit -# layer.unit_cost= finished_move.price_unit + if ( + layer.stock_move_id.production_id + and layer.stock_move_id.product_id.cost_method in ("fifo", "average") + ): + layer.stock_move_id.production_id.lot_producing_id.real_price = ( + layer.stock_move_id.price_unit + ) + + # Update the lot price for the FG moves. + # Update the layer values for FG moves + # if layer.stock_move_id.production_id: + # production = layer.stock_move_id.production_id + # consumed_moves = production.move_raw_ids.filtered( + # lambda x: x.state == "done" + # ) + # finished_move = layer.stock_move_id + # if finished_move and consumed_moves.mapped("lot_ids"): + # finished_move.ensure_one() + # total_cost = finished_move.price_unit - ( + # sum( + # -m.stock_valuation_layer_ids.value + # for m in consumed_moves.filtered( + # lambda sm: sm.product_id.tracking == "serial" + # ) + # ) + # ) + # qty_done = finished_move.product_uom._compute_quantity( + # finished_move.quantity_done, finished_move.product_id.uom_id + # ) + # raw_sn_price = sum( + # lot.real_price for lot in consumed_moves.mapped("lot_ids") + # ) + # raw_non_sn_price = -sum( + # consumed_moves.filtered( + # lambda l: l.product_id.tracking != "serial" + # ) + # .sudo() + # .stock_valuation_layer_ids.mapped("value") + # ) + # total_cost = finished_move.price_unit + raw_non_sn_price + # byproduct_moves = production.move_byproduct_ids.filtered( + # lambda m: m.state in ("done", "cancel") and m.quantity_done > 0 + # ) + # byproduct_cost_share = 0 + # for byproduct in byproduct_moves: + # if byproduct.cost_share == 0: + # continue + # byproduct_cost_share += byproduct.cost_share + # if ( + # byproduct.product_id.cost_method in ("fifo", "average") + # and byproduct.product_id.tracking == "serial" + # ): + # byproduct.price_unit = ( + # total_cost + # * byproduct.cost_share + # / 100 + # / byproduct.product_uom._compute_quantity( + # byproduct.quantity_done, byproduct.product_id.uom_id + # ) + # ) + # byproduct.stock_valuation_layer_ids.write( + # { + # "unit_cost": byproduct.price_unit, + # "value": byproduct.price_unit + # * byproduct.product_uom._compute_quantity( + # byproduct.quantity_done, + # byproduct.product_id.uom_id, + # ), + # "remaining_value": byproduct.price_unit + # * byproduct.product_uom._compute_quantity( + # byproduct.quantity_done, + # byproduct.product_id.uom_id, + # ), + # } + # ) + # byproduct.lot_ids.write( + # {"real_price": byproduct.price_unit} + # ) + # else: + # byproduct.price_unit = ( + # total_cost + # * byproduct.cost_share + # / 100 + # / byproduct.product_uom._compute_quantity( + # byproduct.quantity_done, byproduct.product_id.uom_id + # ) + # ) + # byproduct.stock_valuation_layer_ids.write( + # { + # "unit_cost": byproduct.price_unit, + # "value": byproduct.price_unit + # * byproduct.product_uom._compute_quantity( + # byproduct.quantity_done, + # byproduct.product_id.uom_id, + # ), + # "remaining_value": byproduct.price_unit + # * byproduct.product_uom._compute_quantity( + # byproduct.quantity_done, + # byproduct.product_id.uom_id, + # ), + # } + # ) + # if finished_move.product_id.cost_method in ("fifo", "average"): + # finished_move.price_unit = ( + # total_cost + # * float_round( + # 1 - byproduct_cost_share / 100, + # precision_rounding=0.0001, + # ) + # / qty_done + # ) + # production.lot_producing_id.real_price = ( + # finished_move.price_unit + # ) + # layer.value = finished_move.price_unit + # layer.unit_cost = finished_move.price_unit return res def svl_lots_update(self): @@ -117,9 +213,3 @@ def svl_lots_update(self): svl.lot_ids = [ (6, 0, svl.stock_move_id.mapped("move_line_ids").mapped("lot_id").ids) ] - - - - - -