|
| 1 | +from odoo import Command, fields, models |
| 2 | + |
| 3 | + |
| 4 | +class StockMove(models.Model): |
| 5 | + _inherit = "stock.move" |
| 6 | + |
| 7 | + kit_original_qty = fields.Float(string="Original Kit Quantity", required=False) |
| 8 | + |
| 9 | + def write(self, vals): |
| 10 | + """ |
| 11 | + When increasing `product_uom_qty` beyond the BoM limit, create a new move |
| 12 | + manually instead of using `_split`, since core Odoo restricts splitting |
| 13 | + for repair moves. |
| 14 | + """ |
| 15 | + for move in self: |
| 16 | + if "product_uom_qty" in vals and move.bom_line_id: |
| 17 | + new_qty = vals["product_uom_qty"] |
| 18 | + max_qty = move.bom_line_id.product_qty * move.kit_original_qty |
| 19 | + if new_qty > max_qty: |
| 20 | + surplus_qty = new_qty - max_qty |
| 21 | + new_move_vals = move.copy_data( |
| 22 | + { |
| 23 | + "product_uom_qty": surplus_qty, |
| 24 | + "origin": move.origin, |
| 25 | + "repair_id": move.repair_id.id, |
| 26 | + "sale_line_id": False, |
| 27 | + "bom_line_id": False, |
| 28 | + "kit_original_qty": False, |
| 29 | + } |
| 30 | + )[0] |
| 31 | + self.env["stock.move"].create(new_move_vals) |
| 32 | + vals["product_uom_qty"] = max_qty |
| 33 | + return super( |
| 34 | + StockMove, self.with_context(not_update_or_create_repair_so_line=True) |
| 35 | + ).write(vals) |
| 36 | + |
| 37 | + def _can_form_full_kit(self, moves, bom): |
| 38 | + """ |
| 39 | + Determines whether we can form a full kit for the given moves and BoM. |
| 40 | + """ |
| 41 | + kit_qty = moves[0].kit_original_qty or 0.0 |
| 42 | + relevant_moves = [m for m in moves if m.bom_line_id.bom_id == bom] |
| 43 | + return all( |
| 44 | + sum( |
| 45 | + m.product_uom_qty |
| 46 | + for m in relevant_moves |
| 47 | + if m.product_id == line.product_id |
| 48 | + ) |
| 49 | + >= line.product_qty * kit_qty |
| 50 | + for line in bom.bom_line_ids |
| 51 | + ) |
| 52 | + |
| 53 | + def _create_repair_sale_order_line(self): |
| 54 | + if self.env.context.get("not_update_or_create_repair_so_line"): |
| 55 | + return False |
| 56 | + |
| 57 | + so_line_vals = [] |
| 58 | + grouped_boms = {} |
| 59 | + standard_moves = self.filtered(lambda m: not m.bom_line_id) |
| 60 | + |
| 61 | + # 1) Group BoM kit-related moves |
| 62 | + for move in self - standard_moves: |
| 63 | + bom_id = move.bom_line_id.bom_id |
| 64 | + grouped_boms.setdefault(bom_id, []).append(move) |
| 65 | + |
| 66 | + # 2) Process kit-based moves |
| 67 | + for bom, moves in grouped_boms.items(): |
| 68 | + is_full = self._can_form_full_kit(moves, bom) |
| 69 | + |
| 70 | + if not is_full: |
| 71 | + # Partial kit => treat as standard moves |
| 72 | + standard_moves |= self.env["stock.move"].browse( |
| 73 | + [m.id for m in moves] |
| 74 | + ) # Add these moves to the standard logic |
| 75 | + else: |
| 76 | + # Full kit => create a single sale order line |
| 77 | + first_move = moves[0] |
| 78 | + kit_qty = first_move.kit_original_qty |
| 79 | + so_line_vals.append( |
| 80 | + { |
| 81 | + "order_id": first_move.repair_id.sale_order_id.id, |
| 82 | + "name": bom.product_tmpl_id.display_name or "Kit", |
| 83 | + "product_id": bom.product_tmpl_id.product_variant_id.id, |
| 84 | + "product_uom_qty": kit_qty, |
| 85 | + "move_ids": [Command.link(m.id) for m in moves], |
| 86 | + "price_unit": 0.0 |
| 87 | + if first_move.repair_id.under_warranty |
| 88 | + else first_move.price_unit, |
| 89 | + } |
| 90 | + ) |
| 91 | + |
| 92 | + # 3) Process standard (non-kit and partial kit) moves using Odoo's logic |
| 93 | + if standard_moves: |
| 94 | + super(StockMove, standard_moves)._create_repair_sale_order_line() |
| 95 | + |
| 96 | + # 4) Create all sale order lines at once |
| 97 | + if so_line_vals: |
| 98 | + self.env["sale.order.line"].create(so_line_vals) |
| 99 | + |
| 100 | + return True |
0 commit comments