|
| 1 | +# Copyright 2022 Ecosoft Co., Ltd (https://ecosoft.co.th) |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) |
| 3 | + |
| 4 | +from odoo import models |
| 5 | + |
| 6 | + |
| 7 | +class StockMove(models.Model): |
| 8 | + _inherit = "stock.move" |
| 9 | + |
| 10 | + def _create_in_svl(self, forced_quantity=None): |
| 11 | + """ 1.Overwrite for skip check stock move - Multiple lot on the stock move is not |
| 12 | + allowed for incoming transfer |
| 13 | + 2.Create a `stock.valuation.layer` from `self`. |
| 14 | + :param forced_quantity: under some circunstances, the quantity to value is different than |
| 15 | + the initial demand of the move (Default value = None) |
| 16 | + """ |
| 17 | + layers = self.env["stock.valuation.layer"] |
| 18 | + for move in self: |
| 19 | + svl_vals_list = [] |
| 20 | + for move in self: |
| 21 | + move = move.with_company(move.company_id) |
| 22 | + valued_move_lines = move._get_in_move_lines() |
| 23 | + valued_quantity = 0 |
| 24 | + for valued_move_line in valued_move_lines: |
| 25 | + valued_quantity += valued_move_line.product_uom_id._compute_quantity(valued_move_line.qty_done, move.product_id.uom_id) |
| 26 | + unit_cost = abs(move._get_price_unit()) # May be negative (i.e. decrease an out move). |
| 27 | + if move.product_id.cost_method == 'standard': |
| 28 | + unit_cost = move.product_id.standard_price |
| 29 | + svl_vals = move.product_id._prepare_in_svl_vals(forced_quantity or valued_quantity, unit_cost) |
| 30 | + svl_vals.update(move._prepare_common_svl_vals()) |
| 31 | + if forced_quantity: |
| 32 | + svl_vals['description'] = 'Correction of %s (modification of past move)' % move.picking_id.name or move.name |
| 33 | + svl_vals_list.append(svl_vals) |
| 34 | + layer = self.env['stock.valuation.layer'].sudo().create(svl_vals_list) |
| 35 | + # Calculate standard price (Sorted by lot created date) |
| 36 | + if ( |
| 37 | + move.product_id.cost_method == "fifo" |
| 38 | + and move.product_id.tracking != "none" |
| 39 | + ): |
| 40 | + all_candidates = move.product_id._get_all_candidates( |
| 41 | + move.company_id, sort_by="lot_create_date" |
| 42 | + ) |
| 43 | + if all_candidates: |
| 44 | + move.product_id.sudo().with_company( |
| 45 | + move.company_id.id |
| 46 | + ).with_context( |
| 47 | + disable_auto_svl=True |
| 48 | + ).standard_price = all_candidates[ |
| 49 | + 0 |
| 50 | + ].unit_cost |
| 51 | + layers |= layer |
| 52 | + return layers |
| 53 | + |
0 commit comments