Debian Codegraph @

addons/sale_mrp/tests/test_sale_mrp_kit_bom.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from unittest import skip
4
5 from odoo.tests import Form, tagged
6
7 from odoo import Command
8 from odoo.addons.base.tests.common import BaseCommon
9
10
11 @tagged('post_install', '-at_install')
12 class TestSaleMrpKitBom(BaseCommon):
13
14 @classmethod
15 def setUpClass(cls):
16 super().setUpClass()
17 cls.env.ref('base.user_admin').write({
18 'email': '[email protected]',
19 })
20 cls.env.user.group_ids += cls.quick_ref('product.group_product_variant')
21
22 def _create_product(self, name, storable, price):
23 return self.env['product.product'].create({
24 'name': name,
25 'is_storable': storable,
26 'standard_price': price,
27 })
28
29 def test_reset_avco_kit(self):
30 """
31 Test a specific use case : One product with 2 variant, each variant has its own BoM with either component_1 or
32 component_2. Create a SO for one of the variant, confirm, cancel, reset to draft and then change the product of
33 the SO -> There should be no traceback
34 """
35 component_1 = self.env['product.product'].create({'name': 'compo 1'})
36 component_2 = self.env['product.product'].create({'name': 'compo 2'})
37
38 product_category = self.env['product.category'].create({
39 'name': 'test avco kit',
40 'property_cost_method': 'average'
41 })
42 attributes = self.env['product.attribute'].create({'name': 'Legs'})
43 steel_legs = self.env['product.attribute.value'].create({'attribute_id': attributes.id, 'name': 'Steel'})
44 aluminium_legs = self.env['product.attribute.value'].create(
45 {'attribute_id': attributes.id, 'name': 'Aluminium'})
46
47 product_template = self.env['product.template'].create({
48 'name': 'test product',
49 'categ_id': product_category.id,
50 'attribute_line_ids': [(0, 0, {
51 'attribute_id': attributes.id,
52 'value_ids': [(6, 0, [steel_legs.id, aluminium_legs.id])]
53 })]
54 })
55 product_variant_ids = product_template.product_variant_ids
56 # BoM 1 with component_1
57 self.env['mrp.bom'].create({
58 'product_id': product_variant_ids[0].id,
59 'product_tmpl_id': product_variant_ids[0].product_tmpl_id.id,
60 'product_qty': 1.0,
61 'consumption': 'flexible',
62 'type': 'phantom',
63 'bom_line_ids': [(0, 0, {'product_id': component_1.id, 'product_qty': 1})]
64 })
65 # BoM 2 with component_2
66 self.env['mrp.bom'].create({
67 'product_id': product_variant_ids[1].id,
68 'product_tmpl_id': product_variant_ids[1].product_tmpl_id.id,
69 'product_qty': 1.0,
70 'consumption': 'flexible',
71 'type': 'phantom',
72 'bom_line_ids': [(0, 0, {'product_id': component_2.id, 'product_qty': 1})]
73 })
74 partner = self.env['res.partner'].create({'name': 'Testing Man'})
75 so = self.env['sale.order'].create({
76 'partner_id': partner.id,
77 })
78 # Create the order line
79 self.env['sale.order.line'].create({
80 'name': "Order line",
81 'product_id': product_variant_ids[0].id,
82 'order_id': so.id,
83 })
84 so.action_confirm()
85 so._action_cancel()
86 so.action_draft()
87 with Form(so) as so_form:
88 with so_form.order_line.edit(0) as order_line_change:
89 # The actual test, there should be no traceback here
90 order_line_change.product_id = product_variant_ids[1]
91
92 @skip('Temporary to fast merge new valuation')
93 def test_sale_mrp_kit_cost(self):
94 """
95 Check the total cost of a KIT:
96 # BoM of Kit A:
97 # - BoM Type: Kit
98 # - Quantity: 1
99 # - Components:
100 # * 1 x Component A (Cost: $ 6, QTY: 1, UOM: Dozens)
101 # * 1 x Component B (Cost: $ 10, QTY: 2, UOM: Unit)
102 # cost of Kit A = (6 * 1 * 12) + (10 * 2) = $ 92
103 """
104 self.customer = self.env['res.partner'].create({
105 'name': 'customer'
106 })
107
108 self.kit_product = self._create_product('Kit Product', True, 1.00)
109 # Creating components
110 self.component_a = self._create_product('Component A', True, 1.00)
111 self.component_a.product_tmpl_id.standard_price = 6
112 self.component_b = self._create_product('Component B', True, 1.00)
113 self.component_b.product_tmpl_id.standard_price = 10
114
115 cat = self.env['product.category'].create({
116 'name': 'fifo',
117 'property_cost_method': 'fifo'
118 })
119 self.kit_product.product_tmpl_id.categ_id = cat
120 self.component_a.product_tmpl_id.categ_id = cat
121 self.component_b.product_tmpl_id.categ_id = cat
122
123 self.bom = self.env['mrp.bom'].create({
124 'product_tmpl_id': self.kit_product.product_tmpl_id.id,
125 'product_qty': 1.0,
126 'type': 'phantom'
127 })
128
129 self.env['mrp.bom.line'].create({
130 'product_id': self.component_a.id,
131 'product_qty': 1.0,
132 'bom_id': self.bom.id,
133 'product_uom_id': self.env.ref('uom.product_uom_dozen').id,
134 })
135 self.env['mrp.bom.line'].create({
136 'product_id': self.component_b.id,
137 'product_qty': 2.0,
138 'bom_id': self.bom.id,
139 'product_uom_id': self.env.ref('uom.product_uom_unit').id,
140 })
141
142 # Create a SO with one unit of the kit product
143 so = self.env['sale.order'].create({
144 'partner_id': self.customer.id,
145 'order_line': [
146 (0, 0, {
147 'name': self.kit_product.name,
148 'product_id': self.kit_product.id,
149 'product_uom_qty': 1.0,
150 })],
151 })
152 so.action_confirm()
153 line = so.order_line
154 purchase_price = line.product_id.with_company(line.company_id)._compute_average_price(0, line.product_uom_qty, line.move_ids)
155 self.assertEqual(line.move_ids.mapped('description_picking'), ['Kit Product - 1/2', 'Kit Product - 2/2'])
156 self.assertEqual(purchase_price, 92, "The purchase price must be the total cost of the components multiplied by their unit of measure")
157
158 def test_sale_mrp_kit_sale_price(self):
159 """Check the total sale price of a KIT:
160 # BoM of Kit A:
161 # - BoM Type: Kit
162 # - Quantity: 1
163 # - Components:
164 # * 1 x Component A (Price: $ 8, QTY: 10, UOM: Meter)
165 # * 1 x Component B (Price: $ 5, QTY: 2, UOM: Dozen)
166 # sale price of Kit A = (8 * 10) + (5 * 2 * 12) = $ 200
167 """
168 if "sale_price" not in self.env["stock.move.line"]._fields:
169 self.skipTest("This test only runs with both sale_mrp and stock_delivery installed")
170
171 self.customer = self.env['res.partner'].create({
172 'name': 'customer',
173 })
174 self.warehouse = self.env["stock.warehouse"].create({
175 'name': 'Warehouse #2',
176 'code': 'WH02',
177 })
178
179 self.kit_product = self._create_product('Kit Product', 'product', 1.00)
180 # Creating components
181 self.component_a = self._create_product('Component A', 'product', 1.00)
182 self.component_a.uom_id = self.env.ref('uom.product_uom_meter').id
183 self.component_a.product_tmpl_id.list_price = 8
184 self.component_b = self._create_product('Component B', 'product', 1.00)
185 self.component_b.product_tmpl_id.list_price = 5
186
187 location_id = self.warehouse.lot_stock_id.id
188 self.env["stock.quant"].with_context(inventory_mode=True).create([
189 {"product_id": self.component_a.id, "inventory_quantity": 10, "location_id": location_id},
190 {"product_id": self.component_b.id, "inventory_quantity": 24, "location_id": location_id},
191 ]).action_apply_inventory()
192
193 self.bom = self.env['mrp.bom'].create({
194 'product_tmpl_id': self.kit_product.product_tmpl_id.id,
195 'product_qty': 1.0,
196 'type': 'phantom',
197 'bom_line_ids': [
198 Command.create({
199 'product_id': self.component_a.id,
200 'product_qty': 10.0,
201 'product_uom_id': self.env.ref('uom.product_uom_meter').id,
202 }),
203 Command.create({
204 'product_id': self.component_b.id,
205 'product_qty': 2.0,
206 'product_uom_id': self.env.ref('uom.product_uom_dozen').id,
207 }),
208 ]
209 })
210
211 # Create a SO with one unit of the kit product
212 so = self.env['sale.order'].create({
213 'partner_id': self.customer.id,
214 'order_line': [
215 (0, 0, {
216 'name': self.kit_product.name,
217 'product_id': self.kit_product.id,
218 'product_uom_qty': 1.0,
219 'product_uom_id': self.kit_product.uom_id.id,
220 })],
221 'warehouse_id': self.warehouse.id,
222 })
223 so.action_confirm()
224 so.picking_ids._action_done()
225 move_lines = so.picking_ids.move_ids.move_line_ids
226 self.assertEqual(move_lines.mapped("sale_price"), [80, 120], 'wrong shipping value')
227
228 def test_qty_delivered_with_bom(self):
229 """Check the quantity delivered, when a bom line has a non integer quantity"""
230
231 self.env.ref('uom.decimal_product_uom').digits = 5
232
233 self.kit = self._create_product('Kit', True, 0.00)
234 self.comp = self._create_product('Component', True, 0.00)
235
236 # Create BoM for Kit
237 bom_product_form = Form(self.env['mrp.bom'])
238 bom_product_form.product_tmpl_id = self.kit.product_tmpl_id
239 bom_product_form.product_qty = 1.0
240 bom_product_form.type = 'phantom'
241 with bom_product_form.bom_line_ids.new() as bom_line:
242 bom_line.product_id = self.comp
243 bom_line.product_qty = 0.08600
244 self.bom = bom_product_form.save()
245
246
247 self.customer = self.env['res.partner'].create({
248 'name': 'customer',
249 })
250
251 so = self.env['sale.order'].create({
252 'partner_id': self.customer.id,
253 'order_line': [
254 (0, 0, {
255 'name': self.kit.name,
256 'product_id': self.kit.id,
257 'product_uom_qty': 10.0,
258 'price_unit': 1,
259 'tax_ids': False,
260 })],
261 })
262 so.action_confirm()
263
264 self.assertTrue(so.picking_ids)
265 self.assertEqual(so.order_line.qty_delivered, 0)
266
267 picking = so.picking_ids
268 picking.move_ids.write({'quantity': 0.86000, 'picked': True})
269 picking.button_validate()
270
271 # Checks the delivery amount (must be 10).
272 self.assertEqual(so.order_line.qty_delivered, 10)
273
274 def test_qty_delivered_with_bom_using_kit(self):
275 """Check the quantity delivered, when one product is a kit
276 and his bom uses another product that is also a kit"""
277
278 self.kitA = self._create_product('Kit A', False, 0.00)
279 self.kitB = self._create_product('Kit B', False, 0.00)
280 self.compA = self._create_product('ComponentA', False, 0.00)
281 self.compB = self._create_product('ComponentB', False, 0.00)
282
283 # Create BoM for KitB
284 bom_product_formA = Form(self.env['mrp.bom'])
285 bom_product_formA.product_tmpl_id = self.kitB.product_tmpl_id
286 bom_product_formA.product_qty = 1.0
287 bom_product_formA.type = 'phantom'
288 with bom_product_formA.bom_line_ids.new() as bom_line:
289 bom_line.product_id = self.compA
290 bom_line.product_qty = 1
291 with bom_product_formA.bom_line_ids.new() as bom_line:
292 bom_line.product_id = self.compB
293 bom_line.product_qty = 1
294 self.bomA = bom_product_formA.save()
295
296 # Create BoM for KitA
297 bom_product_formB = Form(self.env['mrp.bom'])
298 bom_product_formB.product_tmpl_id = self.kitA.product_tmpl_id
299 bom_product_formB.product_qty = 1.0
300 bom_product_formB.type = 'phantom'
301 with bom_product_formB.bom_line_ids.new() as bom_line:
302 bom_line.product_id = self.compA
303 bom_line.product_qty = 1
304 with bom_product_formB.bom_line_ids.new() as bom_line:
305 bom_line.product_id = self.kitB
306 bom_line.product_qty = 1
307 self.bomB = bom_product_formB.save()
308
309 self.customer = self.env['res.partner'].create({
310 'name': 'customer',
311 })
312
313 so = self.env['sale.order'].create({
314 'partner_id': self.customer.id,
315 'order_line': [
316 (0, 0, {
317 'name': self.kitA.name,
318 'product_id': self.kitA.id,
319 'product_uom_qty': 1.0,
320 'price_unit': 1,
321 'tax_ids': False,
322 })],
323 })
324 so.action_confirm()
325
326 self.assertTrue(so.picking_ids)
327 self.assertEqual(so.order_line.qty_delivered, 0)
328
329 picking = so.picking_ids
330 picking.button_validate()
331
332 # Checks the delivery amount (must be 1).
333 self.assertEqual(so.order_line.qty_delivered, 1)
334
335 def test_sale_kit_show_kit_in_delivery(self):
336 """Create a kit with 2 product and activate 2 steps
337 delivery and check that every stock move contains
338 a bom_line_id
339 """
340
341 wh = self.env['stock.warehouse'].search([('company_id', '=', self.env.user.id)], limit=1)
342 wh.write({'delivery_steps': 'pick_ship'})
343
344 kitA = self._create_product('Kit Product', True, 0.00)
345 compA = self._create_product('ComponentA', True, 0.00)
346 compB = self._create_product('ComponentB', True, 0.00)
347
348 # Create BoM for KitB
349 bom_product_formA = Form(self.env['mrp.bom'])
350 bom_product_formA.product_tmpl_id = kitA.product_tmpl_id
351 bom_product_formA.product_qty = 1.0
352 bom_product_formA.type = 'phantom'
353 with bom_product_formA.bom_line_ids.new() as bom_line:
354 bom_line.product_id = compA
355 bom_line.product_qty = 1
356 with bom_product_formA.bom_line_ids.new() as bom_line:
357 bom_line.product_id = compB
358 bom_line.product_qty = 1
359 bom_product_formA.save()
360
361 customer = self.env['res.partner'].create({
362 'name': 'customer',
363 })
364
365 so = self.env['sale.order'].create({
366 'partner_id': customer.id,
367 'order_line': [
368 (0, 0, {
369 'name': kitA.name,
370 'product_id': kitA.id,
371 'product_uom_qty': 1.0,
372 'price_unit': 1,
373 'tax_ids': False,
374 })]
375 })
376 so.action_confirm()
377
378 pick = so.picking_ids[0]
379 self.assertTrue(pick.move_ids[0].bom_line_id, "All component from kits should have a bom line")
380 self.assertTrue(pick.move_ids[1].bom_line_id, "All component from kits should have a bom line")
381 pick.move_ids.write({'quantity': 1, 'picked': True})
382 pick.button_validate()
383
384 ship = so.picking_ids[1]
385 self.assertTrue(ship.move_ids[0].bom_line_id, "All component from kits should have a bom line")
386 self.assertTrue(ship.move_ids[1].bom_line_id, "All component from kits should have a bom line")
387
388 def test_qty_delivered_with_bom_using_kit2(self):
389 """Create 2 kits products that have common components and activate 2 steps delivery
390 Then create a sale order with these 2 products, and put everything in a pack in
391 the first step of the delivery. After the shipping is done, check the done quantity
392 is correct for each products.
393 """
394
395 wh = self.env['stock.warehouse'].search([('company_id', '=', self.env.user.id)], limit=1)
396 wh.write({'delivery_steps': 'pick_ship'})
397
398 kitAB = self._create_product('Kit AB', True, 0.00)
399 kitABC = self._create_product('Kit ABC', True, 0.00)
400 compA = self._create_product('ComponentA', True, 0.00)
401 compB = self._create_product('ComponentB', True, 0.00)
402 compC = self._create_product('ComponentC', True, 0.00)
403
404 # Create BoM for KitB
405 bom_product_formA = Form(self.env['mrp.bom'])
406 bom_product_formA.product_tmpl_id = kitAB.product_tmpl_id
407 bom_product_formA.product_qty = 1.0
408 bom_product_formA.type = 'phantom'
409 with bom_product_formA.bom_line_ids.new() as bom_line:
410 bom_line.product_id = compA
411 bom_line.product_qty = 1
412 with bom_product_formA.bom_line_ids.new() as bom_line:
413 bom_line.product_id = compB
414 bom_line.product_qty = 1
415 bom_product_formA.save()
416
417 # Create BoM for KitA
418 bom_product_formB = Form(self.env['mrp.bom'])
419 bom_product_formB.product_tmpl_id = kitABC.product_tmpl_id
420 bom_product_formB.product_qty = 1.0
421 bom_product_formB.type = 'phantom'
422 with bom_product_formB.bom_line_ids.new() as bom_line:
423 bom_line.product_id = compA
424 bom_line.product_qty = 1
425 with bom_product_formB.bom_line_ids.new() as bom_line:
426 bom_line.product_id = compB
427 bom_line.product_qty = 1
428 with bom_product_formB.bom_line_ids.new() as bom_line:
429 bom_line.product_id = compC
430 bom_line.product_qty = 1
431 bom_product_formB.save()
432
433 customer = self.env['res.partner'].create({
434 'name': 'customer',
435 })
436
437 so = self.env['sale.order'].create({
438 'partner_id': customer.id,
439 'order_line': [
440 (0, 0, {
441 'name': kitAB.name,
442 'product_id': kitAB.id,
443 'product_uom_qty': 1.0,
444 'price_unit': 1,
445 'tax_ids': False,
446 }),
447 (0, 0, {
448 'name': kitABC.name,
449 'product_id': kitABC.id,
450 'product_uom_qty': 1.0,
451 'price_unit': 1,
452 'tax_ids': False,
453 })],
454 })
455 so.action_confirm()
456
457 pick = so.picking_ids[0]
458 for move in pick.move_ids:
459 move.write({'quantity': 1, 'picked': True})
460
461 pick.action_put_in_pack()
462 pick.button_validate()
463
464 ship = so.picking_ids[1]
465
466 for move_line in ship.move_line_ids:
467 self.assertEqual(move_line.move_id.product_uom_qty, move_line.quantity, "Quantity done should be equal to the quantity reserved in the move line")
468
469 def test_kit_in_delivery_slip(self):
470 """
471 Suppose this structure:
472 Sale order:
473 - Kit 1 with a sales description("test"):
474 |- Compo 1
475 - Product 1
476 - Kit 2
477 * Variant 1
478 - Compo 1
479 * Variant 2
480 - Compo 1
481 - Kit 4:
482 - Compo 1
483 - Kit 5
484 - Kit 4
485 - Compo 1
486
487 This test ensures that, when delivering a Kit product with a sales description,
488 the delivery report is correctly printed with all the products.
489 """
490 kit_1, component_1, product_1, kit_3, kit_4 = self.env['product.product'].create([{
491 'name': n,
492 'is_storable': True,
493 } for n in ['Kit 1', 'Compo 1', 'Product 1', 'Kit 3', 'Kit 4']])
494 kit_1.description_sale = "test"
495
496 self.env['mrp.bom'].create([{
497 'product_tmpl_id': kit_1.product_tmpl_id.id,
498 'product_qty': 1,
499 'type': 'phantom',
500 'bom_line_ids': [
501 (0, 0, {'product_id': component_1.id, 'product_qty': 1}),
502 ],
503 }])
504 colors = ['red', 'blue']
505 prod_attr = self.env['product.attribute'].create({'name': 'Color', 'create_variant': 'always'})
506 prod_attr_values = self.env['product.attribute.value'].create([{'name': color, 'attribute_id': prod_attr.id, 'sequence': 1} for color in colors])
507 kit_2 = self.env['product.template'].create({
508 'name': 'Kit 2',
509 'attribute_line_ids': [(0, 0, {
510 'attribute_id': prod_attr.id,
511 'value_ids': [(6, 0, prod_attr_values.ids)]
512 })]
513 })
514 self.env['mrp.bom'].create([{
515 'product_tmpl_id': kit_2.id,
516 'product_id': kit_2.product_variant_ids[0].id,
517 'product_qty': 1,
518 'type': 'phantom',
519 'bom_line_ids': [
520 (0, 0, {'product_id': component_1.id, 'product_qty': 1}),
521 ],
522 }])
523 self.env['mrp.bom'].create([{
524 'product_tmpl_id': kit_2.id,
525 'product_id': kit_2.product_variant_ids[1].id,
526 'product_qty': 1,
527 'type': 'phantom',
528 'bom_line_ids': [
529 (0, 0, {'product_id': component_1.id, 'product_qty': 1}),
530 ],
531 }])
532 self.env['mrp.bom'].create([{
533 'product_tmpl_id': kit_3.product_tmpl_id.id,
534 'product_qty': 1,
535 'type': 'phantom',
536 'bom_line_ids': [
537 (0, 0, {'product_id': component_1.id, 'product_qty': 1}),
538 ],
539 }])
540 self.env['mrp.bom'].create([{
541 'product_tmpl_id': kit_4.product_tmpl_id.id,
542 'product_qty': 1,
543 'type': 'phantom',
544 'bom_line_ids': [
545 (0, 0, {'product_id': component_1.id, 'product_qty': 1}),
546 (0, 0, {'product_id': kit_3.id, 'product_qty': 1}),
547 ],
548 }])
549 customer = self.env['res.partner'].create({
550 'name': 'customer',
551 })
552 so = self.env['sale.order'].create({
553 'partner_id': customer.id,
554 'order_line': [
555 (0, 0, {
556 'product_id': kit_1.id,
557 'product_uom_qty': 1.0,
558 }),
559 (0, 0, {
560 'product_id': product_1.id,
561 'product_uom_qty': 1.0,
562 }),
563 (0, 0, {
564 'product_id': kit_2.product_variant_ids[0].id,
565 'product_uom_qty': 1.0,
566 }),
567 (0, 0, {
568 'product_id': kit_2.product_variant_ids[1].id,
569 'product_uom_qty': 1.0,
570 }),
571 (0, 0, {
572 'product_id': kit_3.id,
573 'product_uom_qty': 1.0,
574 }),
575 (0, 0, {
576 'product_id': kit_4.id,
577 'product_uom_qty': 1.0,
578 })],
579 })
580 so.action_confirm()
581 picking = so.picking_ids
582 self.assertEqual(len(so.picking_ids.move_ids), 7)
583 picking.move_ids.write({'quantity': 1, 'picked': True})
584 picking.button_validate()
585 self.assertEqual(picking.state, 'done')
586
587 html_report = self.env['ir.actions.report']._render_qweb_html('stock.report_deliveryslip', picking.ids)[0].decode('utf-8').split('\n')
588 keys = [
589 "Kit 1", "Compo 1", "Kit 2 (red)", "Compo 1", "Kit 2 (blue)", "Compo 1",
590 "Kit 3", "Compo 1", "Kit 4", "Compo 1",
591 "Products not associated with a kit", "Product 1",
592 ]
593 for line in html_report:
594 if not keys:
595 break
596 if keys[0] in line:
597 keys = keys[1:]
598 self.assertFalse(keys, "All keys should be in the report with the defined order")
599
600 def test_sale_multistep_kit_qty_change(self):
601 warehouse = self.env['stock.warehouse'].search([], limit=1)
602 warehouse.write({'delivery_steps': 'pick_ship'})
603 self.partner = self.env['res.partner'].create({'name': 'Test Partner'})
604
605 kit_prod = self._create_product('kit_prod', 'product', 0.00)
606 sub_kit = self._create_product('sub_kit', 'product', 0.00)
607 component = self._create_product('component', 'product', 0.00)
608 component.uom_id = self.env.ref('uom.product_uom_dozen')
609 self.env['stock.quant']._update_available_quantity(component, warehouse.lot_stock_id, 30)
610 # 6 kit_prod == 5 component
611 self.env['mrp.bom'].create([{ # 2 kit_prod == 5 sub_kit
612 'product_tmpl_id': kit_prod.product_tmpl_id.id,
613 'product_qty': 2.0,
614 'type': 'phantom',
615 'bom_line_ids': [(0, 0, {
616 'product_id': sub_kit.id,
617 'product_qty': 5,
618 })],
619 }, { # 3 sub_kit == 1 component
620 'product_tmpl_id': sub_kit.product_tmpl_id.id,
621 'product_qty': 3.0,
622 'type': 'phantom',
623 'bom_line_ids': [(0, 0, {
624 'product_id': component.id,
625 'product_qty': 1,
626 })],
627 }])
628
629 so = self.env['sale.order'].create({
630 'partner_id': self.partner.id,
631 'order_line': [(0, 0, {
632 'name': kit_prod.name,
633 'product_id': kit_prod.id,
634 'product_uom_qty': 30,
635 })],
636 })
637 # Validate the SO
638 so.action_confirm()
639 picking_pick = so.picking_ids[0]
640 picking_pick.picking_type_id.create_backorder = 'never'
641
642 # Check the component qty in the created picking should be 25
643 self.assertEqual(picking_pick.move_ids.product_qty, 30 * 5 / 6)
644
645 # Update the kit quantity in the SO
646 so.order_line[0].product_uom_qty = 60
647 # Check the component qty after the update should be 50
648 self.assertEqual(picking_pick.move_ids.product_qty, 60 * 5 / 6)
649
650 # Deliver half the quantity 25 component == 30 kit_prod
651 picking_pick.move_ids.quantity = 25
652 picking_pick.button_validate()
653
654 picking_ship = so.picking_ids[1]
655 picking_ship.picking_type_id.create_backorder = 'never'
656 picking_ship.move_ids.quantity = 25
657 picking_ship.button_validate()
658 self.assertEqual(so.order_line.qty_delivered, 25 / 5 * 6)
659
660 # Return 10 components
661 stock_return_picking_form = Form(self.env['stock.return.picking']
662 .with_context(active_ids=picking_ship.ids, active_id=picking_ship.id,
663 active_model='stock.picking'))
664 return_wiz = stock_return_picking_form.save()
665 for return_move in return_wiz.product_return_moves:
666 return_move.write({
667 'quantity': 10,
668 'to_refund': True
669 })
670 res = return_wiz.action_create_returns()
671 return_pick = self.env['stock.picking'].browse(res['res_id'])
672
673 # Process all components and validate the return
674 return_pick.button_validate()
675 self.assertEqual(so.order_line.qty_delivered, 15 / 5 * 6)
676
677 # Resend 5 components
678 stock_return_picking_form = Form(self.env['stock.return.picking']
679 .with_context(active_ids=return_pick.ids, active_id=return_pick.id,
680 active_model='stock.picking'))
681 return_wiz = stock_return_picking_form.save()
682 for return_move in return_wiz.product_return_moves:
683 return_move.write({
684 'quantity': 5,
685 'to_refund': True
686 })
687 res = return_wiz.action_create_returns()
688
689 # Validate the return
690 self.env['stock.picking'].browse(res['res_id']).button_validate()
691 self.assertEqual(so.order_line.qty_delivered, 20 / 5 * 6)
692
693 def test_sale_kit_qty_change(self):
694
695 # Create record rule
696 mrp_bom_model = self.env['ir.model']._get('mrp.bom')
697 self.env['ir.rule'].create({
698 'name': "No one allowed to access BoMs",
699 'model_id': mrp_bom_model.id,
700 'domain_force': [(0, '=', 1)],
701 })
702
703 # Create BoM
704 kit_product = self._create_product('Kit Product', 'product', 1)
705 component_a = self._create_product('Component A', 'product', 1)
706 self.env['mrp.bom'].create({
707 'product_id': kit_product.id,
708 'product_tmpl_id': kit_product.product_tmpl_id.id,
709 'product_qty': 1,
710 'consumption': 'flexible',
711 'type': 'phantom',
712 'bom_line_ids': [(0, 0, {'product_id': component_a.id, 'product_qty': 1})]
713 })
714
715 # Create sale order
716 partner = self.env['res.partner'].create({'name': 'Testing Man'})
717 so = self.env['sale.order'].create({
718 'partner_id': partner.id,
719 })
720 sol = self.env['sale.order.line'].create({
721 'name': "Order line",
722 'product_id': kit_product.id,
723 'order_id': so.id,
724 })
725 so.action_confirm()
726
727 user_admin = self.env['res.users'].search([('login', '=', 'admin')])
728 sol.with_user(user_admin).write({'product_uom_qty': 5})
729
730 self.assertEqual(sum(sol.move_ids.mapped('product_uom_qty')), 5)
731
732 def test_sale_kit_with_mto_components_qty_change(self):
733 """
734 Check that updating the demand on a sale order line for a kit product
735 updates the associated deliveries accordingly
736 """
737 partner = self.env['res.partner'].create({'name': 'Test Partner'})
738 warehouse = self.env.ref('stock.warehouse0')
739 mto_route = self.env.ref('stock.route_warehouse0_mto')
740 mto_route.action_unarchive()
741 manufacturing_route_id = self.ref('mrp.route_warehouse0_manufacture')
742 kit_product, comp, mto_comp, subcomp = self.env['product.product'].create([
743 {
744 'name': 'kit_product',
745 'is_storable': True,
746 'route_ids': [],
747 },
748 {
749 'name': 'component',
750 'is_storable': True,
751 'route_ids': [],
752 },
753 {
754 'name': 'mto_component',
755 'is_storable': True,
756 'route_ids': [Command.set([mto_route.id, manufacturing_route_id])],
757 },
758 {
759 'name': 'subcomponent',
760 'is_storable': True,
761 'route_ids': [],
762 },
763 ])
764 self.env['stock.quant']._update_available_quantity(comp, warehouse.lot_stock_id, 30.0)
765 self.env['mrp.bom'].create([
766 { # 2 kit_prod -> 5 comp and 3 mto_comp
767 'product_tmpl_id': kit_product.product_tmpl_id.id,
768 'product_qty': 2.0,
769 'type': 'phantom',
770 'bom_line_ids': [
771 Command.create({'product_id': comp.id, 'product_qty': 5}),
772 Command.create({'product_id': mto_comp.id, 'product_qty': 3}),
773 ],
774 },
775 { # bom to manufacture mto_comp
776 'product_tmpl_id': mto_comp.product_tmpl_id.id,
777 'product_qty': 1.0,
778 'bom_line_ids': [
779 Command.create({'product_id': subcomp.id, 'product_qty': 1}),
780 ],
781 }
782 ])
783
784 so = self.env['sale.order'].create({
785 'partner_id': partner.id,
786 'order_line': [
787 Command.create({
788 'name': kit_product.name,
789 'product_id': kit_product.id,
790 'product_uom_qty': 4,
791 })],
792 })
793 # confirm the SO and check the delivery
794 so.action_confirm()
795 self.assertRecordValues(so.picking_ids.move_ids.sorted('product_uom_qty'), [
796 {'product_id': mto_comp.id, 'product_uom_qty': 6.0},
797 {'product_id': comp.id, 'product_uom_qty': 10.0},
798 ])
799 with Form(so) as so_form:
800 with so_form.order_line.edit(0) as line_form:
801 line_form.product_uom_qty = 10
802 # the moves assocaited to the mto component are expected to be separated as
803 # the are linked to a different MO
804 self.assertRecordValues(so.picking_ids.move_ids.sorted('product_uom_qty'), [
805 {'product_id': mto_comp.id, 'product_uom_qty': 6.0},
806 {'product_id': mto_comp.id, 'product_uom_qty': 9.0},
807 {'product_id': comp.id, 'product_uom_qty': 25.0},
808 ])
809
810 def test_inter_company_qty_delivered_with_kit(self):
811 """
812 Test that the delivered quantity is updated on a sale order line when selling a kit
813 through an inter-company transaction.
814 """
815 self.env.user.write({'group_ids': [(4, self.env.ref('base.group_multi_company').id)]})
816 # Create the kit product and BoM
817 kit_product = self._create_product('Kit', 'product', 1)
818 component_product = self._create_product('Component', 'product', 1)
819 self.env['mrp.bom'].create({
820 'product_id': kit_product.id,
821 'product_tmpl_id': kit_product.product_tmpl_id.id,
822 'product_qty': 1,
823 'consumption': 'flexible',
824 'type': 'phantom',
825 'bom_line_ids': [(0, 0, {'product_id': component_product.id, 'product_qty': 1})]
826 })
827
828 # Create the sale order with a partner that uses the inter company location
829 inter_comp_location = self.env.ref('stock.stock_location_inter_company')
830 partner = self.env['res.partner'].create({'name': 'Testing Partner'})
831 partner.property_stock_customer = inter_comp_location
832 partner.property_stock_supplier = inter_comp_location
833 so = self.env['sale.order'].create({
834 'partner_id': partner.id,
835 'order_line': [
836 (0, 0, {
837 'name': kit_product.name,
838 'product_id': kit_product.id,
839 'product_uom_qty': 1.0,
840 })
841 ]
842 })
843 so.action_confirm()
844
845 self.assertTrue(so.picking_ids)
846 self.assertEqual(so.order_line.qty_delivered, 0)
847
848 picking = so.picking_ids
849 picking.move_ids.write({'quantity': 1, 'picked': True})
850 picking.button_validate()
851
852 self.assertEqual(so.order_line.qty_delivered, 1)