|
| 1 | +# Copyright 2025 360ERP (https://www.360erp.com) |
| 2 | +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). |
| 3 | + |
| 4 | +from odoo.tests.common import TransactionCase |
| 5 | + |
| 6 | + |
| 7 | +class TestSalePhantomBomProcurementMultiLine(TransactionCase): |
| 8 | + """ |
| 9 | + Tests Phantom BoM explosion for Kits selected on SO Lines. |
| 10 | + Focuses on a scenario with multiple lines for the same kit product, |
| 11 | + each specifying a different phantom BoM referencing the same component, |
| 12 | + to ensure component quantities are correctly exploded and aggregated |
| 13 | + within the resulting Stock Picking moves. |
| 14 | + """ |
| 15 | + |
| 16 | + @classmethod |
| 17 | + def setUpClass(cls): |
| 18 | + super().setUpClass() |
| 19 | + cls.env = cls.env( |
| 20 | + context=dict( |
| 21 | + cls.env.context, |
| 22 | + mail_create_nolog=True, |
| 23 | + mail_create_nosubscribe=True, |
| 24 | + mail_notrack=True, |
| 25 | + no_reset_password=True, |
| 26 | + tracking_disable=True, |
| 27 | + ) |
| 28 | + ) |
| 29 | + cls.company = cls.env.company |
| 30 | + |
| 31 | + # Required groups |
| 32 | + cls.env.user.groups_id += cls.env.ref("stock.group_adv_location") |
| 33 | + cls.env.user.groups_id += cls.env.ref("sale_mrp_bom.sale_mrp_bom_group") |
| 34 | + |
| 35 | + # Ensure MTO Route is Active |
| 36 | + cls.mto_route = cls.env.ref( |
| 37 | + "stock.route_warehouse0_mto", raise_if_not_found=True |
| 38 | + ) |
| 39 | + if not cls.mto_route.active: |
| 40 | + cls.mto_route.action_unarchive() |
| 41 | + |
| 42 | + # Products |
| 43 | + cls.product_mtokit = cls.env["product.product"].create( |
| 44 | + [ |
| 45 | + { |
| 46 | + "name": "MTOKIT", |
| 47 | + "type": "consu", |
| 48 | + "route_ids": [(6, 0, [cls.mto_route.id])], |
| 49 | + "categ_id": cls.env.ref("product.product_category_all").id, |
| 50 | + } |
| 51 | + ] |
| 52 | + ) |
| 53 | + cls.product_mtocomp = cls.env["product.product"].create( |
| 54 | + [ |
| 55 | + { |
| 56 | + "name": "MTOCOMP", |
| 57 | + "type": "consu", |
| 58 | + "route_ids": [], |
| 59 | + "categ_id": cls.env.ref("product.product_category_all").id, |
| 60 | + } |
| 61 | + ] |
| 62 | + ) |
| 63 | + |
| 64 | + # BoMs (Phantom) |
| 65 | + cls.bom_kit1 = cls.env["mrp.bom"].create( |
| 66 | + [ |
| 67 | + { |
| 68 | + "product_tmpl_id": cls.product_mtokit.product_tmpl_id.id, |
| 69 | + "product_qty": 1.0, |
| 70 | + "type": "phantom", |
| 71 | + "code": "KIT1", |
| 72 | + } |
| 73 | + ] |
| 74 | + ) |
| 75 | + # BoM Line 1: 1 x MTOCOMP |
| 76 | + cls.env["mrp.bom.line"].create( |
| 77 | + [ |
| 78 | + { |
| 79 | + "bom_id": cls.bom_kit1.id, |
| 80 | + "product_id": cls.product_mtocomp.id, |
| 81 | + "product_qty": 1, |
| 82 | + } |
| 83 | + ] |
| 84 | + ) |
| 85 | + cls.bom_kit2 = cls.env["mrp.bom"].create( |
| 86 | + [ |
| 87 | + { |
| 88 | + "product_tmpl_id": cls.product_mtokit.product_tmpl_id.id, |
| 89 | + "product_qty": 1.0, |
| 90 | + "type": "phantom", |
| 91 | + "code": "KIT2", |
| 92 | + } |
| 93 | + ] |
| 94 | + ) |
| 95 | + # BoM Line 2: 2 x MTOCOMP |
| 96 | + cls.env["mrp.bom.line"].create( |
| 97 | + [ |
| 98 | + { |
| 99 | + "bom_id": cls.bom_kit2.id, |
| 100 | + "product_id": cls.product_mtocomp.id, |
| 101 | + "product_qty": 2, |
| 102 | + } |
| 103 | + ] |
| 104 | + ) |
| 105 | + |
| 106 | + cls.partner = cls.env.ref("base.res_partner_2") # Customer |
| 107 | + cls.warehouse = cls.env.ref("stock.warehouse0") |
| 108 | + |
| 109 | + def _create_sale_order(self, partner): |
| 110 | + return self.env["sale.order"].create( |
| 111 | + [ |
| 112 | + { |
| 113 | + "partner_id": partner.id, |
| 114 | + "partner_invoice_id": partner.id, |
| 115 | + "partner_shipping_id": partner.id, |
| 116 | + "warehouse_id": self.warehouse.id, |
| 117 | + } |
| 118 | + ] |
| 119 | + ) |
| 120 | + |
| 121 | + def _create_sale_order_line(self, sale_order, product, qty, bom): |
| 122 | + sol = self.env["sale.order.line"].create( |
| 123 | + [ |
| 124 | + { |
| 125 | + "order_id": sale_order.id, |
| 126 | + "product_id": product.id, |
| 127 | + "product_uom_qty": qty, |
| 128 | + "bom_id": bom.id, |
| 129 | + "product_uom": product.uom_id.id, |
| 130 | + "price_unit": 1, |
| 131 | + } |
| 132 | + ] |
| 133 | + ) |
| 134 | + return sol |
| 135 | + |
| 136 | + def test_phantom_bom_explosion_multi_line_same_component(self): |
| 137 | + """ |
| 138 | + Test SO with 2 lines for MTOKIT (phantom): Line 1 uses KIT1 (1 comp), |
| 139 | + Line 2 uses KIT2 (2 comps). |
| 140 | + Verify that the resulting delivery picking moves contain lines for MTO_COMP |
| 141 | + with correctly aggregated quantities based on the phantom BoM explosion. |
| 142 | + """ |
| 143 | + # Create SO |
| 144 | + so = self._create_sale_order(self.partner) |
| 145 | + qty_line1 = 5 |
| 146 | + qty_line2 = 3 |
| 147 | + |
| 148 | + # Line 1: 5 x MTOKIT using KIT1 (-> 5 * 1 = 5 MTO_COMP) |
| 149 | + self._create_sale_order_line(so, self.product_mtokit, qty_line1, self.bom_kit1) |
| 150 | + # Line 2: 3 x MTOKIT using KIT2 (-> 3 * 2 = 6 MTO_COMP) |
| 151 | + self._create_sale_order_line(so, self.product_mtokit, qty_line2, self.bom_kit2) |
| 152 | + |
| 153 | + # Confirm the Sale Order - This triggers the delivery order creation |
| 154 | + # and the phantom BoM explosion for the delivery moves. |
| 155 | + so.action_confirm() |
| 156 | + |
| 157 | + # Find the picking associated with the Sale Order |
| 158 | + pickings = so.picking_ids |
| 159 | + self.assertEqual( |
| 160 | + len(pickings), |
| 161 | + 1, |
| 162 | + f"Expected one picking for {so.name}, found {len(pickings)}", |
| 163 | + ) |
| 164 | + picking = pickings[0] |
| 165 | + |
| 166 | + # Find stock moves within this picking |
| 167 | + moves = picking.move_ids |
| 168 | + |
| 169 | + # Verify no moves for the kit itself (it's phantom) |
| 170 | + kit_product_moves = moves.filtered( |
| 171 | + lambda m: m.product_id == self.product_mtokit |
| 172 | + ) |
| 173 | + self.assertFalse( |
| 174 | + kit_product_moves, |
| 175 | + "No stock move line should be created for the parent kit product (MTOKIT).", |
| 176 | + ) |
| 177 | + |
| 178 | + # Find the moves specifically for the component within this picking |
| 179 | + comp_product_moves = moves.filtered( |
| 180 | + lambda m: m.product_id == self.product_mtocomp |
| 181 | + ) |
| 182 | + self.assertTrue( |
| 183 | + comp_product_moves, |
| 184 | + f"Stock move lines for {self.product_mtocomp.name} should be created.", |
| 185 | + ) |
| 186 | + |
| 187 | + # Calculate expected *total* component quantity based on BoMs |
| 188 | + expected_comp_qty_line1 = ( |
| 189 | + qty_line1 |
| 190 | + * self.bom_kit1.bom_line_ids.filtered( |
| 191 | + lambda bl: bl.product_id == self.product_mtocomp |
| 192 | + ).product_qty |
| 193 | + ) |
| 194 | + expected_comp_qty_line2 = ( |
| 195 | + qty_line2 |
| 196 | + * self.bom_kit2.bom_line_ids.filtered( |
| 197 | + lambda bl: bl.product_id == self.product_mtocomp |
| 198 | + ).product_qty |
| 199 | + ) |
| 200 | + expected_total_qty = ( |
| 201 | + expected_comp_qty_line1 + expected_comp_qty_line2 |
| 202 | + ) # Should be 5 * 1 + 3 * 2 = 11 |
| 203 | + |
| 204 | + # Verify the total quantity demanded in the generated stock moves |
| 205 | + # Check the initial demand planned for the picking |
| 206 | + actual_total_move_qty = sum(comp_product_moves.mapped("product_uom_qty")) |
| 207 | + self.assertEqual( |
| 208 | + actual_total_move_qty, |
| 209 | + expected_total_qty, |
| 210 | + f"MTO_COMP: expected {expected_total_qty}, got {actual_total_move_qty}.", |
| 211 | + ) |
0 commit comments