Debian Codegraph @

addons/sale_mrp/tests/test_sale_mrp_flow.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 import Command
6 from odoo.exceptions import UserError
7 from odoo.tests import Form, common
8 from odoo.tools import float_compare, mute_logger
9
10 from odoo.addons.sale.tests.common import TestSaleCommon
11 from odoo.addons.stock_account.tests.test_anglo_saxon_valuation_reconciliation_common import (
12 ValuationReconciliationTestCommon,
13 )
14
15
16 # these tests create accounting entries, and therefore need a chart of accounts
17 class TestSaleMrpFlowCommon(ValuationReconciliationTestCommon, TestSaleCommon):
18
19 @classmethod
20 def setUpClass(cls):
21 super().setUpClass()
22
23 # Required for `uom_id` to be visible in the view
24 cls._enable_uom()
25 cls.env.ref('stock.route_warehouse0_mto').active = True
26
27 # Useful models
28 cls.StockMove = cls.env['stock.move']
29 cls.UoM = cls.env['uom.uom']
30 cls.MrpProduction = cls.env['mrp.production']
31 cls.Quant = cls.env['stock.quant']
32 cls.ProductCategory = cls.env['product.category']
33
34 cls.uom_kg = cls.uom_kgm
35 cls.uom_gm = cls.uom_gram
36 cls.uom_ten = cls.UoM.create({
37 'name': 'Test-Ten',
38 'relative_factor': 10,
39 'relative_uom_id': cls.uom_unit.id,
40 })
41
42 # Creating all components
43 cls.component_a = cls._cls_create_product('Comp A', cls.uom_unit)
44 cls.component_b = cls._cls_create_product('Comp B', cls.uom_unit)
45 cls.component_c = cls._cls_create_product('Comp C', cls.uom_unit)
46 cls.component_d = cls._cls_create_product('Comp D', cls.uom_unit)
47 cls.component_e = cls._cls_create_product('Comp E', cls.uom_unit)
48 cls.component_f = cls._cls_create_product('Comp F', cls.uom_unit)
49 cls.component_g = cls._cls_create_product('Comp G', cls.uom_unit)
50
51 # Create a kit 'kit_1' :
52 # -----------------------
53 #
54 # kit_1 --|- component_a x2
55 # |- component_b x1
56 # |- component_c x3
57
58 cls.kit_1 = cls._cls_create_product('Kit 1', cls.uom_unit)
59
60 cls.bom_kit_1 = cls.env['mrp.bom'].create({
61 'product_tmpl_id': cls.kit_1.product_tmpl_id.id,
62 'product_qty': 1.0,
63 'type': 'phantom'})
64
65 BomLine = cls.env['mrp.bom.line']
66 BomLine.create({
67 'product_id': cls.component_a.id,
68 'product_qty': 2.0,
69 'bom_id': cls.bom_kit_1.id})
70 BomLine.create({
71 'product_id': cls.component_b.id,
72 'product_qty': 1.0,
73 'bom_id': cls.bom_kit_1.id})
74 BomLine.create({
75 'product_id': cls.component_c.id,
76 'product_qty': 3.0,
77 'bom_id': cls.bom_kit_1.id})
78
79 # Create a kit 'kit_parent' :
80 # ---------------------------
81 #
82 # kit_parent --|- kit_2 x2 --|- component_d x1
83 # | |- kit_1 x2 -------|- component_a x2
84 # | |- component_b x1
85 # | |- component_c x3
86 # |
87 # |- kit_3 x1 --|- component_f x1
88 # | |- component_g x2
89 # |
90 # |- component_e x1
91
92 # Creating all kits
93 cls.kit_2 = cls._cls_create_product('Kit 2', cls.uom_unit)
94 cls.kit_3 = cls._cls_create_product('kit 3', cls.uom_unit)
95 cls.kit_parent = cls._cls_create_product('Kit Parent', cls.uom_unit)
96
97 # Linking the kits and the components via some 'phantom' BoMs
98 bom_kit_2 = cls.env['mrp.bom'].create({
99 'product_tmpl_id': cls.kit_2.product_tmpl_id.id,
100 'product_qty': 1.0,
101 'type': 'phantom'})
102
103 BomLine.create({
104 'product_id': cls.component_d.id,
105 'product_qty': 1.0,
106 'bom_id': bom_kit_2.id})
107 BomLine.create({
108 'product_id': cls.kit_1.id,
109 'product_qty': 2.0,
110 'bom_id': bom_kit_2.id})
111
112 bom_kit_parent = cls.env['mrp.bom'].create({
113 'product_tmpl_id': cls.kit_parent.product_tmpl_id.id,
114 'product_qty': 1.0,
115 'type': 'phantom'})
116
117 BomLine.create({
118 'product_id': cls.component_e.id,
119 'product_qty': 1.0,
120 'bom_id': bom_kit_parent.id})
121 BomLine.create({
122 'product_id': cls.kit_2.id,
123 'product_qty': 2.0,
124 'bom_id': bom_kit_parent.id})
125
126 bom_kit_3 = cls.env['mrp.bom'].create({
127 'product_tmpl_id': cls.kit_3.product_tmpl_id.id,
128 'product_qty': 1.0,
129 'type': 'phantom'})
130
131 BomLine.create({
132 'product_id': cls.component_f.id,
133 'product_qty': 1.0,
134 'bom_id': bom_kit_3.id})
135 BomLine.create({
136 'product_id': cls.component_g.id,
137 'product_qty': 2.0,
138 'bom_id': bom_kit_3.id})
139
140 BomLine.create({
141 'product_id': cls.kit_3.id,
142 'product_qty': 2.0,
143 'bom_id': bom_kit_parent.id})
144
145 @classmethod
146 def _cls_create_product(cls, name, uom_id, routes=()):
147 p = Form(cls.env['product.product'])
148 p.name = name
149 p.is_storable = True
150 p.uom_id = uom_id
151 p.route_ids.clear()
152 for r in routes:
153 p.route_ids.add(r)
154 return p.save()
155
156 # Helper to process quantities based on a dict following this structure :
157 #
158 # qty_to_process = {
159 # product_id: qty
160 # }
161
162 def _process_quantities(self, moves, quantities_to_process):
163 """ Helper to process quantities based on a dict following this structure :
164 qty_to_process = {
165 product_id: qty
166 }
167 """
168 moves_to_process = moves.filtered(lambda m: m.product_id in quantities_to_process.keys())
169 for move in moves_to_process:
170 move.write({
171 'quantity': quantities_to_process[move.product_id],
172 'picked': True
173 })
174
175 def _assert_quantities(self, moves, quantities_to_process):
176 """ Helper to check expected quantities based on a dict following this structure :
177 qty_to_process = {
178 product_id: qty
179 ...
180 }
181 """
182 moves_to_process = moves.filtered(lambda m: m.product_id in quantities_to_process.keys())
183 for move in moves_to_process:
184 self.assertEqual(move.product_uom_qty, quantities_to_process[move.product_id])
185
186 def _create_move_quantities(self, qty_to_process, components, warehouse):
187 """ Helper to creates moves in order to update the quantities of components
188 on a specific warehouse. This ensure that all compute fields are triggered.
189 The structure of qty_to_process should be the following :
190
191 qty_to_process = {
192 component: (qty, uom),
193 ...
194 }
195 """
196 for comp in components:
197 f = Form(self.env['stock.move'])
198 # <field name="name" invisible="1"/>
199 f.location_id = self.env.ref('stock.stock_location_suppliers')
200 f.location_dest_id = warehouse.lot_stock_id
201 f.product_id = comp
202 f.product_uom = qty_to_process[comp][1]
203 f.product_uom_qty = qty_to_process[comp][0]
204 move = f.save()
205 move._action_confirm()
206 move._action_assign()
207 move_line = move.move_line_ids[0]
208 move_line.quantity = qty_to_process[comp][0]
209 move._action_done()
210
211
212 @common.tagged('post_install', '-at_install')
213 class TestSaleMrpFlow(TestSaleMrpFlowCommon):
214 @skip('Temporary to fast merge new valuation')
215 def test_00_sale_mrp_flow(self):
216 """ Test sale to mrp flow with diffrent unit of measure."""
217
218
219 # Create product A, B, C, D.
220 # --------------------------
221 route_manufacture = self.company_data['default_warehouse'].manufacture_pull_id.route_id
222 route_mto = self.company_data['default_warehouse'].mto_pull_id.route_id
223 product_a = self._cls_create_product('Product A', self.uom_unit, routes=[route_manufacture, route_mto])
224 product_c = self._cls_create_product('Product C', self.uom_kg)
225 product_b = self._cls_create_product('Product B', self.uom_dozen, routes=[route_manufacture, route_mto])
226 product_d = self._cls_create_product('Product D', self.uom_unit, routes=[route_manufacture, route_mto])
227
228 # ------------------------------------------------------------------------------------------
229 # Bill of materials for product A, B, D.
230 # ------------------------------------------------------------------------------------------
231
232 # Bill of materials for Product A.
233 with Form(self.env['mrp.bom']) as f:
234 f.product_tmpl_id = product_a.product_tmpl_id
235 f.product_qty = 2
236 f.product_uom_id = self.uom_dozen
237 with f.bom_line_ids.new() as line:
238 line.product_id = product_b
239 line.product_qty = 3
240 line.product_uom_id = self.uom_unit
241 with f.bom_line_ids.new() as line:
242 line.product_id = product_c
243 line.product_qty = 300.0
244 line.product_uom_id = self.uom_gm
245 with f.bom_line_ids.new() as line:
246 line.product_id = product_d
247 line.product_qty = 4
248 line.product_uom_id = self.uom_unit
249
250 # Bill of materials for Product B.
251 with Form(self.env['mrp.bom']) as f:
252 f.product_tmpl_id = product_b.product_tmpl_id
253 f.product_qty = 1
254 f.product_uom_id = self.uom_unit
255 f.type = 'phantom'
256 with f.bom_line_ids.new() as line:
257 line.product_id = product_c
258 line.product_qty = 0.400
259 line.product_uom_id = self.uom_kg
260
261 # Bill of materials for Product D.
262 with Form(self.env['mrp.bom']) as f:
263 f.product_tmpl_id = product_d.product_tmpl_id
264 f.product_qty = 1
265 f.product_uom_id = self.uom_unit
266 with f.bom_line_ids.new() as line:
267 line.product_id = product_c
268 line.product_qty = 1
269 line.product_uom_id = self.uom_kg
270
271 # ----------------------------------------
272 # Create sales order of 10 Dozen product A.
273 # ----------------------------------------
274
275 order_form = Form(self.env['sale.order'])
276 order_form.partner_id = self.env['res.partner'].create({'name': 'My Test Partner'})
277 with order_form.order_line.new() as line:
278 line.product_id = product_a
279 line.product_uom_id = self.uom_dozen
280 line.product_uom_qty = 10
281 order = order_form.save()
282 order.action_confirm()
283
284 # Verify buttons are working as expected
285 self.assertEqual(order.mrp_production_count, 2, "Mo for product A + child mo for product B")
286
287 # ===============================================================================
288 # Sales order of 10 Dozen product A should create production order
289 # like ..
290 # ===============================================================================
291 # Product A 10 Dozen.
292 # Product C 6 kg
293 # As product B phantom in bom A, product A will consume product C
294 # ================================================================
295 # For 1 unit product B it will consume 400 gm
296 # then for 15 unit (Product B 3 unit per 2 Dozen product A)
297 # product B it will consume [ 6 kg ] product C)
298 # Product A will consume 6 kg product C.
299 #
300 # [15 * 400 gm ( 6 kg product C)] = 6 kg product C
301 #
302 # Product C 1500.0 gm.
303 # [
304 # For 2 Dozen product A will consume 300.0 gm product C
305 # then for 10 Dozen product A will consume 1500.0 gm product C.
306 # ]
307 #
308 # product D 20 Unit.
309 # [
310 # For 2 dozen product A will consume 4 unit product D
311 # then for 10 Dozen product A will consume 20 unit of product D.
312 # ]
313 # --------------------------------------------------------------------------------
314
315 # <><><><><><><><><><><><><><><><><><><><>
316 # Check manufacturing order for product A.
317 # <><><><><><><><><><><><><><><><><><><><>
318
319 # Check quantity, unit of measure and state of manufacturing order.
320 # -----------------------------------------------------------------
321 self.env['stock.rule'].run_scheduler()
322 mnf_product_a = self.env['mrp.production'].search([('product_id', '=', product_a.id)])
323
324 self.assertTrue(mnf_product_a, 'Manufacturing order not created.')
325 self.assertEqual(mnf_product_a.product_qty, 10, 'Wrong product quantity in manufacturing order.')
326 self.assertEqual(mnf_product_a.product_uom_id, self.uom_dozen, 'Wrong unit of measure in manufacturing order.')
327 self.assertEqual(mnf_product_a.state, 'confirmed', 'Manufacturing order should be confirmed.')
328
329 # ------------------------------------------------------------------------------------------
330 # Check 'To consume line' for production order of product A.
331 # ------------------------------------------------------------------------------------------
332
333 # Check 'To consume line' with product c and uom kg.
334 # -------------------------------------------------
335
336 moves = self.StockMove.search([
337 ('raw_material_production_id', '=', mnf_product_a.id),
338 ('product_id', '=', product_c.id),
339 ('product_uom', '=', self.uom_kg.id)])
340
341 # Check total consume line with product c and uom kg.
342 self.assertEqual(len(moves), 1, 'Production move lines are not generated proper.')
343 list_qty = {move.product_uom_qty for move in moves}
344 self.assertEqual(list_qty, {6.0}, "Wrong product quantity in 'To consume line' of manufacturing order.")
345 # Check state of consume line with product c and uom kg.
346 for move in moves:
347 self.assertEqual(move.state, 'confirmed', "Wrong state in 'To consume line' of manufacturing order.")
348
349 # Check 'To consume line' with product c and uom gm.
350 # ---------------------------------------------------
351
352 move = self.StockMove.search([
353 ('raw_material_production_id', '=', mnf_product_a.id),
354 ('product_id', '=', product_c.id),
355 ('product_uom', '=', self.uom_gm.id)])
356
357 # Check total consume line of product c with gm.
358 self.assertEqual(len(move), 1, 'Production move lines are not generated proper.')
359 # Check quantity should be with 1500.0 ( 2 Dozen product A consume 300.0 gm then 10 Dozen (300.0 * (10/2)).
360 self.assertEqual(move.product_uom_qty, 1500.0, "Wrong product quantity in 'To consume line' of manufacturing order.")
361 # Check state of consume line with product c with and uom gm.
362 self.assertEqual(move.state, 'confirmed', "Wrong state in 'To consume line' of manufacturing order.")
363
364 # Check 'To consume line' with product D.
365 # ---------------------------------------
366
367 move = self.StockMove.search([
368 ('raw_material_production_id', '=', mnf_product_a.id),
369 ('product_id', '=', product_d.id)])
370
371 # Check total consume line with product D.
372 self.assertEqual(len(move), 1, 'Production lines are not generated proper.')
373
374 # <><><><><><><><><><><><><><><><><><><><><><>
375 # Manufacturing order for product D (20 unit).
376 # <><><><><><><><><><><><><><><><><><><><><><>
377
378 # FP Todo: find a better way to look for the production order
379 mnf_product_d = self.MrpProduction.search([('product_id', '=', product_d.id)], order='id desc', limit=1)
380 # Check state of production order D.
381 self.assertEqual(mnf_product_d.state, 'confirmed', 'Manufacturing order should be confirmed.')
382
383 # Check 'To consume line' state, quantity, uom of production order (product D).
384 # -----------------------------------------------------------------------------
385
386 move = self.StockMove.search([('raw_material_production_id', '=', mnf_product_d.id), ('product_id', '=', product_c.id)])
387 self.assertEqual(move.product_uom_qty, 20, "Wrong product quantity in 'To consume line' of manufacturing order.")
388 self.assertEqual(move.product_uom.id, self.uom_kg.id, "Wrong unit of measure in 'To consume line' of manufacturing order.")
389 self.assertEqual(move.state, 'confirmed', "Wrong state in 'To consume line' of manufacturing order.")
390
391 # -------------------------------
392 # Create inventory for product c.
393 # -------------------------------
394 # Need 20 kg product c to produce 20 unit product D.
395 # --------------------------------------------------
396
397 self.Quant.with_context(inventory_mode=True).create({
398 'product_id': product_c.id, # uom = uom_kg
399 'inventory_quantity': 20,
400 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
401 }).action_apply_inventory()
402
403 # --------------------------------------------------
404 # Assign product c to manufacturing order of product D.
405 # --------------------------------------------------
406
407 mnf_product_d.action_assign()
408 self.assertEqual(mnf_product_d.reservation_state, 'assigned', 'Availability should be assigned')
409 self.assertEqual(move.state, 'assigned', "Wrong state in 'To consume line' of manufacturing order.")
410
411 # ------------------
412 # produce product D.
413 # ------------------
414
415 mo_form = Form(mnf_product_d)
416 mo_form.qty_producing = 20
417 mnf_product_d = mo_form.save()
418 mnf_product_d.button_mark_done()
419
420 # Check state of manufacturing order.
421 self.assertEqual(mnf_product_d.state, 'done', 'Manufacturing order should still be in progress state.')
422 # Check available quantity of product D.
423 self.assertEqual(product_d.qty_available, 20, 'Wrong quantity available of product D.')
424
425 # -----------------------------------------------------------------
426 # Check product D assigned or not to production order of product A.
427 # -----------------------------------------------------------------
428
429 self.assertEqual(mnf_product_a.state, 'confirmed', 'Manufacturing order should be confirmed.')
430 move = self.StockMove.search([('raw_material_production_id', '=', mnf_product_a.id), ('product_id', '=', product_d.id)])
431 self.assertEqual(move.state, 'assigned', "Wrong state in 'To consume line' of manufacturing order.")
432
433 # Create inventory for product C.
434 # ------------------------------
435 # Need product C ( 20 kg + 6 kg + 1500.0 gm = 27.500 kg)
436 # -------------------------------------------------------
437
438 self.Quant.with_context(inventory_mode=True).create({
439 'product_id': product_c.id, # uom = uom_kg
440 'inventory_quantity': 27.51, # round up due to kg.rounding = 0.01
441 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
442 }).action_apply_inventory()
443
444 # Assign product to manufacturing order of product A.
445 # ---------------------------------------------------
446
447 mnf_product_a.action_assign()
448 self.assertEqual(mnf_product_a.reservation_state, 'assigned', 'Manufacturing order inventory state should be available.')
449 moves = self.StockMove.search([('raw_material_production_id', '=', mnf_product_a.id), ('product_id', '=', product_c.id)])
450
451 # Check product c move line state.
452 for move in moves:
453 self.assertEqual(move.state, 'assigned', "Wrong state in 'To consume line' of manufacturing order.")
454
455 # Produce product A.
456 # ------------------
457
458 mo_form = Form(mnf_product_a)
459 mo_form.qty_producing = mo_form.product_qty
460 mnf_product_a = mo_form.save()
461 mnf_product_a._post_inventory()
462 # Check state of manufacturing order product A.
463 self.assertEqual(mnf_product_a.state, 'done', 'Manufacturing order should still be in the progress state.')
464 # Check product A avaialble quantity should be 120.
465 self.assertEqual(product_a.qty_available, 120, 'Wrong quantity available of product A.')
466
467 @skip('Temporary to fast merge new valuation')
468 def test_01_sale_mrp_delivery_kit(self):
469 """ Test delivered quantity on SO based on delivered quantity in pickings."""
470 # intial so
471 product = self.env['product.product'].create({
472 'name': 'Table Kit',
473 'type': 'consu',
474 'invoice_policy': 'delivery',
475 })
476 # Remove the MTO route as purchase is not installed and since the procurement removal the exception is directly raised
477 product.write({'route_ids': [(6, 0, [self.company_data['default_warehouse'].manufacture_pull_id.route_id.id])]})
478
479 product_wood_panel = self.env['product.product'].create({
480 'name': 'Wood Panel',
481 'is_storable': True,
482 })
483 product_desk_bolt = self.env['product.product'].create({
484 'name': 'Bolt',
485 'is_storable': True,
486 })
487 self.env['mrp.bom'].create({
488 'product_tmpl_id': product.product_tmpl_id.id,
489 'product_uom_id': self.env.ref('uom.product_uom_unit').id,
490 'sequence': 2,
491 'type': 'phantom',
492 'bom_line_ids': [
493 (0, 0, {
494 'product_id': product_wood_panel.id,
495 'product_qty': 1,
496 'product_uom_id': self.env.ref('uom.product_uom_unit').id,
497 }), (0, 0, {
498 'product_id': product_desk_bolt.id,
499 'product_qty': 4,
500 'product_uom_id': self.env.ref('uom.product_uom_unit').id,
501 })
502 ]
503 })
504
505 partner = self.env['res.partner'].create({'name': 'My Test Partner'})
506 # if `delivery` module is installed, a default property is set for the carrier to use
507 # However this will lead to an extra line on the SO (the delivery line), which will force
508 # the SO to have a different flow (and `invoice_state` value)
509 if 'property_delivery_carrier_id' in partner:
510 partner.property_delivery_carrier_id = False
511
512 f = Form(self.env['sale.order'])
513 f.partner_id = partner
514 with f.order_line.new() as line:
515 line.product_id = product
516 line.product_uom_qty = 5
517 so = f.save()
518
519 # confirm our standard so, check the picking
520 so.action_confirm()
521 self.assertTrue(so.picking_ids, 'Sale MRP: no picking created for "invoice on delivery" storable products')
522
523 # invoice in on delivery, nothing should be invoiced
524 with self.assertRaises(UserError):
525 so._create_invoices()
526 self.assertEqual(so.invoice_status, 'no', 'Sale MRP: so invoice_status should be "nothing to invoice" after invoicing')
527
528 # deliver partially (1 of each instead of 5), check the so's invoice_status and delivered quantities
529 pick = so.picking_ids
530 pick.move_ids.write({'quantity': 1, 'picked': True})
531 Form.from_action(self.env, pick.button_validate()).save().process()
532 self.assertEqual(so.invoice_status, 'no', 'Sale MRP: so invoice_status should be "no" after partial delivery of a kit')
533 del_qty = sum(sol.qty_delivered for sol in so.order_line)
534 self.assertEqual(del_qty, 0.0, 'Sale MRP: delivered quantity should be zero after partial delivery of a kit')
535 # deliver remaining products, check the so's invoice_status and delivered quantities
536 self.assertEqual(len(so.picking_ids), 2, 'Sale MRP: number of pickings should be 2')
537 pick_2 = so.picking_ids.filtered('backorder_id')
538 for move in pick_2.move_ids:
539 if move.product_id.id == product_desk_bolt.id:
540 move.write({'quantity': 19, 'picked': True})
541 else:
542 move.write({'quantity': 4, 'picked': True})
543 pick_2.button_validate()
544
545 del_qty = sum(sol.qty_delivered for sol in so.order_line)
546 self.assertEqual(del_qty, 5.0, 'Sale MRP: delivered quantity should be 5.0 after complete delivery of a kit')
547 self.assertEqual(so.invoice_status, 'to invoice', 'Sale MRP: so invoice_status should be "to invoice" after complete delivery of a kit')
548
549 @skip('Temporary to fast merge new valuation')
550 def test_02_sale_mrp_anglo_saxon(self):
551 """Test the price unit of a kit"""
552 # This test will check that the correct journal entries are created when a stockable product in real time valuation
553 # and in fifo cost method is sold in a company using anglo-saxon.
554 # For this test, let's consider a product category called Test category in real-time valuation and real price costing method
555 # Let's also consider a finished product with a bom with two components: component1(cost = 20) and component2(cost = 10)
556 # These products are in the Test category
557 # The bom consists of 2 component1 and 1 component2
558 # The invoice policy of the finished product is based on delivered quantities
559 self.env.company.currency_id = self.env.ref('base.USD')
560 self.uom_unit = self.UoM.create({
561 'name': 'Test-Unit',
562 'relative_factor': 1,
563 })
564 self.company = self.company_data['company']
565 self.company.anglo_saxon_accounting = True
566 self.partner = self.env['res.partner'].create({'name': 'My Test Partner'})
567 self.category = self.env.ref('product.product_category_goods').copy({
568 'name': 'Test category',
569 'property_valuation': 'real_time',
570 'property_cost_method': 'fifo',
571 })
572 self.account_receiv = self.env['account.account'].create({'name': 'Receivable', 'code': 'RCV00', 'account_type': 'asset_receivable', 'reconcile': True})
573 account_expense = self.env['account.account'].create({'name': 'Expense', 'code': 'EXP00', 'account_type': 'liability_current', 'reconcile': True})
574 account_income = self.env['account.account'].create({'name': 'Income', 'code': 'INC00', 'account_type': 'asset_current', 'reconcile': True})
575 account_valuation = self.env['account.account'].create({'name': 'Valuation', 'code': 'STV00', 'account_type': 'asset_receivable', 'reconcile': True})
576 self.partner.property_account_receivable_id = self.account_receiv
577 self.category.property_account_income_categ_id = account_income
578 self.category.property_account_expense_categ_id = account_expense
579 self.category.property_stock_valuation_account_id = account_valuation
580 self.category.property_stock_journal = self.env['account.journal'].create({'name': 'Stock journal', 'type': 'sale', 'code': 'STK00'})
581
582 Product = self.env['product.product']
583 self.finished_product = Product.create({
584 'name': 'Finished product',
585 'is_storable': True,
586 'uom_id': self.uom_unit.id,
587 'invoice_policy': 'delivery',
588 'categ_id': self.category.id})
589 self.component1 = Product.create({
590 'name': 'Component 1',
591 'is_storable': True,
592 'uom_id': self.uom_unit.id,
593 'categ_id': self.category.id,
594 'standard_price': 20})
595 self.component2 = Product.create({
596 'name': 'Component 2',
597 'is_storable': True,
598 'uom_id': self.uom_unit.id,
599 'categ_id': self.category.id,
600 'standard_price': 10})
601
602 # Create quants with sudo to avoid:
603 # "You are not allowed to create 'Quants' (stock.quant) records. No group currently allows this operation."
604 self.env['stock.quant'].sudo().create({
605 'product_id': self.component1.id,
606 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
607 'quantity': 6.0,
608 })
609 self.env['stock.quant'].sudo().create({
610 'product_id': self.component2.id,
611 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
612 'quantity': 3.0,
613 })
614 self.bom = self.env['mrp.bom'].create({
615 'product_tmpl_id': self.finished_product.product_tmpl_id.id,
616 'product_qty': 1.0,
617 'type': 'phantom'})
618 BomLine = self.env['mrp.bom.line']
619 BomLine.create({
620 'product_id': self.component1.id,
621 'product_qty': 2.0,
622 'bom_id': self.bom.id})
623 BomLine.create({
624 'product_id': self.component2.id,
625 'product_qty': 1.0,
626 'bom_id': self.bom.id})
627
628 # Create a SO for a specific partner for three units of the finished product
629 so_vals = {
630 'partner_id': self.partner.id,
631 'partner_invoice_id': self.partner.id,
632 'partner_shipping_id': self.partner.id,
633 'order_line': [(0, 0, {
634 'name': self.finished_product.name,
635 'product_id': self.finished_product.id,
636 'product_uom_qty': 3,
637 'price_unit': self.finished_product.list_price
638 })],
639 'company_id': self.company.id,
640 }
641 self.so = self.env['sale.order'].create(so_vals)
642 # Validate the SO
643 self.so.action_confirm()
644 # Deliver the three finished products
645 pick = self.so.picking_ids
646 # To check the products on the picking
647 self.assertEqual(pick.move_ids.mapped('product_id'), self.component1 | self.component2)
648 pick.button_validate()
649 # Create the invoice
650 self.so._create_invoices()
651 self.invoice = self.so.invoice_ids
652 # Changed the invoiced quantity of the finished product to 2
653 move_form = Form(self.invoice)
654 with move_form.invoice_line_ids.edit(0) as line_form:
655 line_form.quantity = 2.0
656 self.invoice = move_form.save()
657 self.invoice.action_post()
658 aml = self.invoice.line_ids
659 aml_expense = aml.filtered(lambda l: l.display_type == 'cogs' and l.debit > 0)
660 aml_output = aml.filtered(lambda l: l.display_type == 'cogs' and l.credit > 0)
661 # Check that the cost of Good Sold entries are equal to 2* (2 * 20 + 1 * 10) = 100
662 self.assertEqual(aml_expense.debit, 100, "Cost of Good Sold entry missing or mismatching")
663 self.assertEqual(aml_output.credit, 100, "Cost of Good Sold entry missing or mismatching")
664
665 def test_03_sale_mrp_simple_kit_qty_delivered(self):
666 """ Test that the quantities delivered are correct when
667 a simple kit is ordered with multiple backorders
668 """
669
670 # kit_1 structure:
671 # ================
672
673 # kit_1 ---|- component_a x2
674 # |- component_b x1
675 # |- component_c x3
676
677 # Updating the quantities in stock to prevent
678 # a 'Not enough inventory' warning message.
679 stock_location = self.company_data['default_warehouse'].lot_stock_id
680 self.env['stock.quant']._update_available_quantity(self.component_a, stock_location, 20)
681 self.env['stock.quant']._update_available_quantity(self.component_b, stock_location, 10)
682 self.env['stock.quant']._update_available_quantity(self.component_c, stock_location, 30)
683
684 # Creation of a sale order for x10 kit_1
685 partner = self.env['res.partner'].create({'name': 'My Test Partner'})
686 f = Form(self.env['sale.order'])
687 f.partner_id = partner
688 with f.order_line.new() as line:
689 line.product_id = self.kit_1
690 line.product_uom_qty = 10.0
691
692 # Confirming the SO to trigger the picking creation
693 so = f.save()
694 so.action_confirm()
695
696 # Check picking creation
697 self.assertEqual(len(so.picking_ids), 1)
698 picking_original = so.picking_ids[0]
699 move_ids = picking_original.move_ids
700
701 # Check if the correct amount of stock.moves are created
702 self.assertEqual(len(move_ids), 3)
703
704 # Check if BoM is created and is for a 'Kit'
705 bom_from_k1 = self.env['mrp.bom']._bom_find(self.kit_1)[self.kit_1]
706 self.assertEqual(self.bom_kit_1.id, bom_from_k1.id)
707 self.assertEqual(bom_from_k1.type, 'phantom')
708
709 # Check there's only 1 order line on the SO and it's for x10 'kit_1'
710 order_lines = so.order_line
711 self.assertEqual(len(order_lines), 1)
712 order_line = order_lines[0]
713 self.assertEqual(order_line.product_id.id, self.kit_1.id)
714 self.assertEqual(order_line.product_uom_qty, 10.0)
715
716 # Check if correct qty is ordered for each component of the kit
717 expected_quantities = {
718 self.component_a: 20,
719 self.component_b: 10,
720 self.component_c: 30,
721 }
722 self._assert_quantities(move_ids, expected_quantities)
723
724 # Process only x1 of the first component then create a backorder for the missing components
725 picking_original.move_ids.sorted()[0].write({'quantity': 1, 'picked': True})
726
727 Form.from_action(self.env, so.picking_ids[0].button_validate()).save().process()
728
729 # Check that the backorder was created, no kit should be delivered at this point
730 self.assertEqual(len(so.picking_ids), 2)
731 backorder_1 = so.picking_ids - picking_original
732 self.assertEqual(backorder_1.backorder_id.id, picking_original.id)
733 self.assertEqual(order_line.qty_delivered, 0)
734
735 # Process only x6 each componenent in the picking
736 # Then create a backorder for the missing components
737 backorder_1.move_ids.write({'quantity': 6, 'picked': True})
738 Form.from_action(self.env, backorder_1.button_validate()).save().process()
739
740 # Check that a backorder is created
741 self.assertEqual(len(so.picking_ids), 3)
742 backorder_2 = so.picking_ids - picking_original - backorder_1
743 self.assertEqual(backorder_2.backorder_id.id, backorder_1.id)
744
745 # With x6 unit of each components, we can only make 2 kits.
746 # So only 2 kits should be delivered
747 self.assertEqual(order_line.qty_delivered, 2)
748
749 # Process x3 more unit of each components :
750 # - Now only 3 kits should be delivered
751 # - A backorder will be created, the SO should have 3 picking_ids linked to it.
752 backorder_2.move_ids.write({'quantity': 3, 'picked': True})
753
754 Form.from_action(self.env, backorder_2.button_validate()).save().process()
755
756 self.assertEqual(len(so.picking_ids), 4)
757 backorder_3 = so.picking_ids - picking_original - backorder_2 - backorder_1
758 self.assertEqual(backorder_3.backorder_id.id, backorder_2.id)
759 self.assertEqual(order_line.qty_delivered, 3)
760
761 # Adding missing components
762 qty_to_process = {
763 self.component_a: 10,
764 self.component_b: 1,
765 self.component_c: 21,
766 }
767 self._process_quantities(backorder_3.move_ids, qty_to_process)
768
769 # Validating the last backorder now it's complete
770 backorder_3.button_validate()
771 order_line._compute_qty_delivered()
772
773 # All kits should be delivered
774 self.assertEqual(order_line.qty_delivered, 10)
775
776 def test_04_sale_mrp_kit_qty_delivered(self):
777 """ Test that the quantities delivered are correct when
778 a kit with subkits is ordered with multiple backorders and returns
779 """
780
781 # 'kit_parent' structure:
782 # ---------------------------
783 #
784 # kit_parent --|- kit_2 x2 --|- component_d x1
785 # | |- kit_1 x2 -------|- component_a x2
786 # | |- component_b x1
787 # | |- component_c x3
788 # |
789 # |- kit_3 x1 --|- component_f x1
790 # | |- component_g x2
791 # |
792 # |- component_e x1
793
794 # Updating the quantities in stock to prevent
795 # a 'Not enough inventory' warning message.
796 stock_location = self.company_data['default_warehouse'].lot_stock_id
797 self.env['stock.quant']._update_available_quantity(self.component_a, stock_location, 56)
798 self.env['stock.quant']._update_available_quantity(self.component_b, stock_location, 28)
799 self.env['stock.quant']._update_available_quantity(self.component_c, stock_location, 84)
800 self.env['stock.quant']._update_available_quantity(self.component_d, stock_location, 14)
801 self.env['stock.quant']._update_available_quantity(self.component_e, stock_location, 7)
802 self.env['stock.quant']._update_available_quantity(self.component_f, stock_location, 14)
803 self.env['stock.quant']._update_available_quantity(self.component_g, stock_location, 28)
804
805 # Creation of a sale order for x7 kit_parent
806 partner = self.env['res.partner'].create({'name': 'My Test Partner'})
807 f = Form(self.env['sale.order'])
808 f.partner_id = partner
809 with f.order_line.new() as line:
810 line.product_id = self.kit_parent
811 line.product_uom_qty = 7.0
812
813 so = f.save()
814 so.action_confirm()
815
816 # Check picking creation, its move lines should concern
817 # only components. Also checks that the quantities are corresponding
818 # to the SO
819 self.assertEqual(len(so.picking_ids), 1)
820 order_line = so.order_line[0]
821 picking_original = so.picking_ids[0]
822 move_ids = picking_original.move_ids
823 products = move_ids.product_id
824 kits = [self.kit_parent, self.kit_3, self.kit_2, self.kit_1]
825 components = [self.component_a, self.component_b, self.component_c, self.component_d, self.component_e, self.component_f, self.component_g]
826 expected_quantities = {
827 self.component_a: 56.0,
828 self.component_b: 28.0,
829 self.component_c: 84.0,
830 self.component_d: 14.0,
831 self.component_e: 7.0,
832 self.component_f: 14.0,
833 self.component_g: 28.0
834 }
835
836 self.assertEqual(len(move_ids), 7)
837 self.assertTrue(not any(kit in products for kit in kits))
838 self.assertTrue(all(component in products for component in components))
839 self._assert_quantities(move_ids, expected_quantities)
840
841 # Process only 7 units of each component
842 qty_to_process = 7
843 move_ids.write({'quantity': qty_to_process, 'picked': True})
844
845 # Create a backorder for the missing componenents
846 Form.from_action(self.env, picking_original.button_validate()).save().process()
847
848 # Check that a backorded is created
849 self.assertEqual(len(so.picking_ids), 2)
850 backorder_1 = so.picking_ids - picking_original
851 self.assertEqual(backorder_1.backorder_id.id, picking_original.id)
852
853 # Even if some components are delivered completely,
854 # no KitParent should be delivered
855 self.assertEqual(order_line.qty_delivered, 0)
856
857 # Process just enough components to make 1 kit_parent
858 qty_to_process = {
859 self.component_a: 1,
860 self.component_c: 5,
861 }
862 self._process_quantities(backorder_1.move_ids, qty_to_process)
863
864 # Create a backorder for the missing componenents
865 Form.from_action(self.env, backorder_1.button_validate()).save().process()
866
867 # Only 1 kit_parent should be delivered at this point
868 self.assertEqual(order_line.qty_delivered, 1)
869
870 # Check that the second backorder is created
871 self.assertEqual(len(so.picking_ids), 3)
872 backorder_2 = so.picking_ids - picking_original - backorder_1
873 self.assertEqual(backorder_2.backorder_id.id, backorder_1.id)
874
875 # Set the components quantities that backorder_2 should have
876 expected_quantities = {
877 self.component_a: 48,
878 self.component_b: 21,
879 self.component_c: 72,
880 self.component_d: 7,
881 self.component_f: 7,
882 self.component_g: 21
883 }
884
885 # Check that the computed quantities are matching the theorical ones.
886 # Since component_e was totally processed, this componenent shouldn't be
887 # present in backorder_2
888 self.assertEqual(len(backorder_2.move_ids), 6)
889 move_comp_e = backorder_2.move_ids.filtered(lambda m: m.product_id.id == self.component_e.id)
890 self.assertFalse(move_comp_e)
891 self._assert_quantities(backorder_2.move_ids, expected_quantities)
892
893 # Process enough components to make x3 kit_parents
894 qty_to_process = {
895 self.component_a: 16,
896 self.component_b: 5,
897 self.component_c: 24,
898 self.component_g: 5
899 }
900 self._process_quantities(backorder_2.move_ids, qty_to_process)
901
902 # Create a backorder for the missing componenents
903 Form.from_action(self.env, backorder_2.button_validate()).save().process()
904
905 # Check that x3 kit_parents are indeed delivered
906 self.assertEqual(order_line.qty_delivered, 3)
907
908 # Check that the third backorder is created
909 self.assertEqual(len(so.picking_ids), 4)
910 backorder_3 = so.picking_ids - (picking_original + backorder_1 + backorder_2)
911 self.assertEqual(backorder_3.backorder_id.id, backorder_2.id)
912
913 # Check the components quantities that backorder_3 should have
914 expected_quantities = {
915 self.component_a: 32,
916 self.component_b: 16,
917 self.component_c: 48,
918 self.component_d: 7,
919 self.component_f: 7,
920 self.component_g: 16
921 }
922 self._assert_quantities(backorder_3.move_ids, expected_quantities)
923
924 # Process all missing components
925 self._process_quantities(backorder_3.move_ids, expected_quantities)
926
927 # Validating the last backorder now it's complete.
928 # All kits should be delivered
929 backorder_3.button_validate()
930 self.assertEqual(order_line.qty_delivered, 7.0)
931
932 # Return all components processed by backorder_3
933 stock_return_picking_form = Form(self.env['stock.return.picking']
934 .with_context(active_ids=backorder_3.ids, active_id=backorder_3.ids[0],
935 active_model='stock.picking'))
936 return_wiz = stock_return_picking_form.save()
937 for return_move in return_wiz.product_return_moves:
938 return_move.write({
939 'quantity': expected_quantities[return_move.product_id],
940 'to_refund': True
941 })
942 res = return_wiz.action_create_returns()
943 return_pick = self.env['stock.picking'].browse(res['res_id'])
944
945 # Process all components and validate the picking
946 return_pick.button_validate()
947
948 # Now quantity delivered should be 3 again
949 self.assertEqual(order_line.qty_delivered, 3)
950
951 stock_return_picking_form = Form(self.env['stock.return.picking']
952 .with_context(active_ids=return_pick.ids, active_id=return_pick.ids[0],
953 active_model='stock.picking'))
954 return_wiz = stock_return_picking_form.save()
955 for move in return_wiz.product_return_moves:
956 move.quantity = expected_quantities[move.product_id]
957 res = return_wiz.action_create_returns()
958 return_of_return_pick = self.env['stock.picking'].browse(res['res_id'])
959
960 # Process all components except one of each
961 for move in return_of_return_pick.move_ids:
962 move.write({
963 'quantity': expected_quantities[move.product_id] - 1,
964 'picked': True,
965 'to_refund': True
966 })
967
968 Form.from_action(self.env, return_of_return_pick.button_validate()).save().process()
969
970 # As one of each component is missing, only 6 kit_parents should be delivered
971 self.assertEqual(order_line.qty_delivered, 6)
972
973 # Check that the 4th backorder is created.
974 self.assertEqual(len(so.picking_ids), 7)
975 backorder_4 = so.picking_ids - (picking_original + backorder_1 + backorder_2 + backorder_3 + return_of_return_pick + return_pick)
976 self.assertEqual(backorder_4.backorder_id.id, return_of_return_pick.id)
977
978 # Check the components quantities that backorder_4 should have
979 for move in backorder_4.move_ids:
980 self.assertEqual(move.product_qty, 1)
981
982 @mute_logger('odoo.tests.common.onchange')
983 def test_05_mrp_sale_kit_availability(self):
984 """
985 Check that the 'Not enough inventory' warning message shows correct
986 informations when a kit is ordered
987 """
988
989 warehouse_1 = self.env['stock.warehouse'].create({
990 'name': 'Warehouse 1',
991 'code': 'WH1'
992 })
993 warehouse_2 = self.env['stock.warehouse'].create({
994 'name': 'Warehouse 2',
995 'code': 'WH2'
996 })
997
998 # Those are all componenents needed to make kit_parents
999 components = [self.component_a, self.component_b, self.component_c, self.component_d, self.component_e,
1000 self.component_f, self.component_g]
1001
1002 # Set enough quantities to make 1 kit_uom_in_kit in WH1
1003 self.env['stock.quant']._update_available_quantity(self.component_a, warehouse_1.lot_stock_id, 8)
1004 self.env['stock.quant']._update_available_quantity(self.component_b, warehouse_1.lot_stock_id, 4)
1005 self.env['stock.quant']._update_available_quantity(self.component_c, warehouse_1.lot_stock_id, 12)
1006 self.env['stock.quant']._update_available_quantity(self.component_d, warehouse_1.lot_stock_id, 2)
1007 self.env['stock.quant']._update_available_quantity(self.component_e, warehouse_1.lot_stock_id, 1)
1008 self.env['stock.quant']._update_available_quantity(self.component_f, warehouse_1.lot_stock_id, 2)
1009 self.env['stock.quant']._update_available_quantity(self.component_g, warehouse_1.lot_stock_id, 4)
1010
1011 # Set quantities on WH2, but not enough to make 1 kit_parent
1012 self.env['stock.quant']._update_available_quantity(self.component_a, warehouse_2.lot_stock_id, 7)
1013 self.env['stock.quant']._update_available_quantity(self.component_b, warehouse_2.lot_stock_id, 3)
1014 self.env['stock.quant']._update_available_quantity(self.component_c, warehouse_2.lot_stock_id, 12)
1015 self.env['stock.quant']._update_available_quantity(self.component_d, warehouse_2.lot_stock_id, 1)
1016 self.env['stock.quant']._update_available_quantity(self.component_e, warehouse_2.lot_stock_id, 1)
1017 self.env['stock.quant']._update_available_quantity(self.component_f, warehouse_2.lot_stock_id, 1)
1018 self.env['stock.quant']._update_available_quantity(self.component_g, warehouse_2.lot_stock_id, 4)
1019
1020 # Creation of a sale order for x7 kit_parent
1021 qty_ordered = 7
1022 f = Form(self.env['sale.order'])
1023 f.partner_id = self.env['res.partner'].create({'name': 'My Test Partner'})
1024 f.warehouse_id = warehouse_2
1025 with f.order_line.new() as line:
1026 line.product_id = self.kit_parent
1027 line.product_uom_qty = qty_ordered
1028 so = f.save()
1029 order_line = so.order_line[0]
1030
1031 # Check that not enough enough quantities are available in the warehouse set in the SO
1032 # but there are enough quantities in Warehouse 1 for 1 kit_parent
1033 kit_parent_wh_order = self.kit_parent.with_context(warehouse_id=so.warehouse_id.id)
1034
1035 # Check that not enough enough quantities are available in the warehouse set in the SO
1036 # but there are enough quantities in Warehouse 1 for 1 kit_parent
1037 self.assertEqual(kit_parent_wh_order.virtual_available, 0)
1038 self.env.invalidate_all()
1039 kit_parent_wh1 = self.kit_parent.with_context(warehouse_id=warehouse_1.id)
1040 self.assertEqual(kit_parent_wh1.virtual_available, 1)
1041
1042 # Check there arn't enough quantities available for the sale order
1043 self.assertTrue(line.product_uom_id.compare(order_line.virtual_available_at_date - order_line.product_uom_qty, 0) == -1)
1044
1045 # We receive enoug of each component in Warehouse 2 to make 3 kit_parent
1046 qty_to_process = {
1047 self.component_a: (17, self.uom_unit),
1048 self.component_b: (12, self.uom_unit),
1049 self.component_c: (25, self.uom_unit),
1050 self.component_d: (5, self.uom_unit),
1051 self.component_e: (2, self.uom_unit),
1052 self.component_f: (5, self.uom_unit),
1053 self.component_g: (8, self.uom_unit),
1054 }
1055 self._create_move_quantities(qty_to_process, components, warehouse_2)
1056
1057 # As 'Warehouse 2' is the warehouse linked to the SO, 3 kits should be available
1058 # But the quantity available in Warehouse 1 should stay 1
1059 kit_parent_wh_order = self.kit_parent.with_context(warehouse_id=so.warehouse_id.id)
1060 self.assertEqual(kit_parent_wh_order.virtual_available, 3)
1061 self.env.invalidate_all()
1062 kit_parent_wh1 = self.kit_parent.with_context(warehouse_id=warehouse_1.id)
1063 self.assertEqual(kit_parent_wh1.virtual_available, 1)
1064
1065 # Check there arn't enough quantities available for the sale order
1066 self.assertTrue(line.product_uom_id.compare(order_line.virtual_available_at_date - order_line.product_uom_qty, 0) == -1)
1067
1068 # We receive enough of each component in Warehouse 2 to make 7 kit_parent
1069 qty_to_process = {
1070 self.component_a: (32, self.uom_unit),
1071 self.component_b: (16, self.uom_unit),
1072 self.component_c: (48, self.uom_unit),
1073 self.component_d: (8, self.uom_unit),
1074 self.component_e: (4, self.uom_unit),
1075 self.component_f: (8, self.uom_unit),
1076 self.component_g: (16, self.uom_unit),
1077 }
1078 self._create_move_quantities(qty_to_process, components, warehouse_2)
1079
1080 # Enough quantities should be available, no warning message should be displayed
1081 kit_parent_wh_order = self.kit_parent.with_context(warehouse_id=so.warehouse_id.id)
1082 self.assertEqual(kit_parent_wh_order.virtual_available, 7)
1083
1084 def test_06_kit_qty_delivered_mixed_uom(self):
1085 """
1086 Check that the quantities delivered are correct when a kit involves
1087 multiple UoMs on its components
1088 """
1089 # Create some components
1090 component_uom_unit = self._cls_create_product('Comp Unit', self.uom_unit)
1091 component_uom_dozen = self._cls_create_product('Comp Dozen', self.uom_dozen)
1092 component_uom_kg = self._cls_create_product('Comp Kg', self.uom_kg)
1093
1094 # Create a kit 'kit_uom_1' :
1095 # -----------------------
1096 #
1097 # kit_uom_1 --|- component_uom_unit x2 Test-Dozen
1098 # |- component_uom_dozen x1 Test-Dozen
1099 # |- component_uom_kg x3 Test-G
1100
1101 kit_uom_1 = self._cls_create_product('Kit 1', self.uom_unit)
1102
1103 bom_kit_uom_1 = self.env['mrp.bom'].create({
1104 'product_tmpl_id': kit_uom_1.product_tmpl_id.id,
1105 'product_qty': 1.0,
1106 'type': 'phantom'})
1107
1108 BomLine = self.env['mrp.bom.line']
1109 BomLine.create({
1110 'product_id': component_uom_unit.id,
1111 'product_qty': 2.0,
1112 'product_uom_id': self.uom_dozen.id,
1113 'bom_id': bom_kit_uom_1.id})
1114 BomLine.create({
1115 'product_id': component_uom_dozen.id,
1116 'product_qty': 1.0,
1117 'product_uom_id': self.uom_dozen.id,
1118 'bom_id': bom_kit_uom_1.id})
1119 BomLine.create({
1120 'product_id': component_uom_kg.id,
1121 'product_qty': 3.0,
1122 'product_uom_id': self.uom_gm.id,
1123 'bom_id': bom_kit_uom_1.id})
1124
1125 # Updating the quantities in stock to prevent
1126 # a 'Not enough inventory' warning message.
1127 stock_location = self.company_data['default_warehouse'].lot_stock_id
1128 self.env['stock.quant']._update_available_quantity(component_uom_unit, stock_location, 240)
1129 self.env['stock.quant']._update_available_quantity(component_uom_dozen, stock_location, 10)
1130 self.env['stock.quant']._update_available_quantity(component_uom_kg, stock_location, 0.03)
1131
1132 # Creation of a sale order for x10 kit_1
1133 partner = self.env['res.partner'].create({'name': 'My Test Partner'})
1134 f = Form(self.env['sale.order'])
1135 f.partner_id = partner
1136 with f.order_line.new() as line:
1137 line.product_id = kit_uom_1
1138 line.product_uom_qty = 10.0
1139
1140 so = f.save()
1141 so.action_confirm()
1142
1143 picking_original = so.picking_ids[0]
1144 move_ids = picking_original.move_ids
1145 order_line = so.order_line[0]
1146
1147 # Check that the quantities on the picking are the one expected for each components
1148 for move in move_ids:
1149 corr_bom_line = bom_kit_uom_1.bom_line_ids.filtered(lambda b: b.product_id.id == move.product_id.id)
1150 computed_qty = move.product_uom._compute_quantity(move.product_uom_qty, corr_bom_line.product_uom_id)
1151 self.assertEqual(computed_qty, order_line.product_uom_qty * corr_bom_line.product_qty)
1152
1153 # Processe enough componenents in the picking to make 2 kit_uom_1
1154 # Then create a backorder for the missing components
1155 qty_to_process = {
1156 component_uom_unit: 48,
1157 component_uom_dozen: 3,
1158 component_uom_kg: 0.006
1159 }
1160 self._process_quantities(move_ids, qty_to_process)
1161 Form.from_action(self.env, move_ids.picking_id.button_validate()).save().process()
1162
1163 # Check that a backorder is created
1164 self.assertEqual(len(so.picking_ids), 2)
1165 backorder_1 = so.picking_ids - picking_original
1166 self.assertEqual(backorder_1.backorder_id.id, picking_original.id)
1167
1168 # Only 2 kits should be delivered
1169 self.assertEqual(order_line.qty_delivered, 2)
1170
1171 # Adding missing components
1172 qty_to_process = {
1173 component_uom_unit: 192,
1174 component_uom_dozen: 7,
1175 component_uom_kg: 0.024
1176 }
1177 self._process_quantities(backorder_1.move_ids, qty_to_process)
1178
1179 # Validating the last backorder now it's complete
1180 backorder_1.button_validate()
1181 order_line._compute_qty_delivered()
1182 # All kits should be delivered
1183 self.assertEqual(order_line.qty_delivered, 10)
1184
1185 @mute_logger('odoo.tests.common.onchange')
1186 def test_07_kit_availability_mixed_uom(self):
1187 """
1188 Check that the 'Not enough inventory' warning message displays correct
1189 informations when a kit with multiple UoMs on its components is ordered
1190 """
1191
1192 # Create some components
1193 component_uom_unit = self._cls_create_product('Comp Unit', self.uom_unit)
1194 component_uom_dozen = self._cls_create_product('Comp Dozen', self.uom_dozen)
1195 component_uom_kg = self._cls_create_product('Comp Kg', self.uom_kg)
1196 component_uom_gm = self._cls_create_product('Comp g', self.uom_gm)
1197 components = [component_uom_unit, component_uom_dozen, component_uom_kg, component_uom_gm]
1198
1199 # Create a kit 'kit_uom_in_kit' :
1200 # -----------------------
1201 # kit_uom_in_kit --|- component_uom_gm x3 Test-KG
1202 # |- kit_uom_1 x2 Test-Dozen --|- component_uom_unit x2 Test-Dozen
1203 # |- component_uom_dozen x1 Test-Dozen
1204 # |- component_uom_kg x5 Test-G
1205
1206 kit_uom_1 = self._cls_create_product('Sub Kit 1', self.uom_unit)
1207 kit_uom_in_kit = self._cls_create_product('Parent Kit', self.uom_unit)
1208
1209 bom_kit_uom_1 = self.env['mrp.bom'].create({
1210 'product_tmpl_id': kit_uom_1.product_tmpl_id.id,
1211 'product_qty': 1.0,
1212 'type': 'phantom'})
1213
1214 BomLine = self.env['mrp.bom.line']
1215 BomLine.create({
1216 'product_id': component_uom_unit.id,
1217 'product_qty': 2.0,
1218 'product_uom_id': self.uom_dozen.id,
1219 'bom_id': bom_kit_uom_1.id})
1220 BomLine.create({
1221 'product_id': component_uom_dozen.id,
1222 'product_qty': 1.0,
1223 'product_uom_id': self.uom_dozen.id,
1224 'bom_id': bom_kit_uom_1.id})
1225 BomLine.create({
1226 'product_id': component_uom_kg.id,
1227 'product_qty': 5.0,
1228 'product_uom_id': self.uom_gm.id,
1229 'bom_id': bom_kit_uom_1.id})
1230
1231 bom_kit_uom_in_kit = self.env['mrp.bom'].create({
1232 'product_tmpl_id': kit_uom_in_kit.product_tmpl_id.id,
1233 'product_qty': 1.0,
1234 'type': 'phantom'})
1235
1236 BomLine.create({
1237 'product_id': component_uom_gm.id,
1238 'product_qty': 3.0,
1239 'product_uom_id': self.uom_kg.id,
1240 'bom_id': bom_kit_uom_in_kit.id})
1241 BomLine.create({
1242 'product_id': kit_uom_1.id,
1243 'product_qty': 2.0,
1244 'product_uom_id': self.uom_dozen.id,
1245 'bom_id': bom_kit_uom_in_kit.id})
1246
1247 # Create a simple warehouse to receives some products
1248 warehouse_1 = self.env['stock.warehouse'].create({
1249 'name': 'Warehouse 1',
1250 'code': 'WH1'
1251 })
1252
1253 # Set enough quantities to make 1 kit_uom_in_kit in WH1
1254 self.env['stock.quant']._update_available_quantity(component_uom_unit, warehouse_1.lot_stock_id, 576)
1255 self.env['stock.quant']._update_available_quantity(component_uom_dozen, warehouse_1.lot_stock_id, 24)
1256 self.env['stock.quant']._update_available_quantity(component_uom_kg, warehouse_1.lot_stock_id, 0.12)
1257 self.env['stock.quant']._update_available_quantity(component_uom_gm, warehouse_1.lot_stock_id, 3000)
1258
1259 # Creation of a sale order for x5 kit_uom_in_kit
1260 qty_ordered = 5
1261 f = Form(self.env['sale.order'])
1262 f.partner_id = self.env['res.partner'].create({'name': 'My Test Partner'})
1263 f.warehouse_id = warehouse_1
1264 with f.order_line.new() as line:
1265 line.product_id = kit_uom_in_kit
1266 line.product_uom_qty = qty_ordered
1267
1268 so = f.save()
1269 order_line = so.order_line[0]
1270
1271 # Check that not enough enough quantities are available in the warehouse set in the SO
1272 # but there are enough quantities in Warehouse 1 for 1 kit_parent
1273 kit_uom_in_kit.with_context(warehouse_id=warehouse_1.id)._compute_quantities()
1274 virtual_available_wh_order = kit_uom_in_kit.virtual_available
1275 self.assertEqual(virtual_available_wh_order, 1)
1276
1277 # Check there arn't enough quantities available for the sale order
1278 self.assertTrue(line.product_uom_id.compare(order_line.virtual_available_at_date - order_line.product_uom_qty, 0) == -1)
1279
1280 # We receive enough of each component in Warehouse 1 to make 3 kit_uom_in_kit.
1281 # Moves are created instead of only updating the quant quantities in order to trigger every compute fields.
1282 qty_to_process = {
1283 component_uom_unit: (1152, self.uom_unit),
1284 component_uom_dozen: (48, self.uom_dozen),
1285 component_uom_kg: (0.24, self.uom_kg),
1286 component_uom_gm: (6000, self.uom_gm)
1287 }
1288 self._create_move_quantities(qty_to_process, components, warehouse_1)
1289
1290 # Check there arn't enough quantities available for the sale order
1291 self.assertTrue(line.product_uom_id.compare(order_line.virtual_available_at_date - order_line.product_uom_qty, 0) == -1)
1292 kit_uom_in_kit.with_context(warehouse_id=warehouse_1.id)._compute_quantities()
1293 virtual_available_wh_order = kit_uom_in_kit.virtual_available
1294 self.assertEqual(virtual_available_wh_order, 3)
1295
1296 # We process enough quantities to have enough kit_uom_in_kit available for the sale order.
1297 self._create_move_quantities(qty_to_process, components, warehouse_1)
1298
1299 # We check that enough quantities were processed to sell 5 kit_uom_in_kit
1300 kit_uom_in_kit.with_context(warehouse_id=warehouse_1.id)._compute_quantities()
1301 self.assertEqual(kit_uom_in_kit.virtual_available, 5)
1302
1303 def test_10_sale_mrp_kits_routes(self):
1304
1305 # Create a kit 'kit_1' :
1306 # -----------------------
1307 #
1308 # kit_1 --|- component_shelf1 x3
1309 # |- component_shelf2 x2
1310
1311 stock_shelf_1 = self.env['stock.location'].create({
1312 'name': 'Shelf 1',
1313 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
1314 })
1315 stock_shelf_2 = self.env['stock.location'].create({
1316 'name': 'Shelf 2',
1317 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
1318 })
1319
1320 kit_1 = self._cls_create_product('Kit1', self.uom_unit)
1321 component_shelf1 = self._cls_create_product('Comp Shelf1', self.uom_unit)
1322 component_shelf2 = self._cls_create_product('Comp Shelf2', self.uom_unit)
1323
1324 with Form(self.env['mrp.bom']) as bom:
1325 bom.product_tmpl_id = kit_1.product_tmpl_id
1326 bom.product_qty = 1
1327 bom.product_uom_id = self.uom_unit
1328 bom.type = 'phantom'
1329 with bom.bom_line_ids.new() as line:
1330 line.product_id = component_shelf1
1331 line.product_qty = 3
1332 line.product_uom_id = self.uom_unit
1333 with bom.bom_line_ids.new() as line:
1334 line.product_id = component_shelf2
1335 line.product_qty = 2
1336 line.product_uom_id = self.uom_unit
1337
1338 # Creating 2 specific routes for each of the components of the kit
1339 route_shelf1 = self.env['stock.route'].create({
1340 'name': 'Shelf1 -> Customer',
1341 'product_selectable': True,
1342 'rule_ids': [(0, 0, {
1343 'name': 'Shelf1 -> Customer',
1344 'action': 'pull',
1345 'picking_type_id': self.company_data['default_warehouse'].out_type_id.id,
1346 'location_src_id': stock_shelf_1.id,
1347 'location_dest_id': self.ref('stock.stock_location_customers'),
1348 })],
1349 })
1350
1351 route_shelf2 = self.env['stock.route'].create({
1352 'name': 'Shelf2 -> Customer',
1353 'product_selectable': True,
1354 'rule_ids': [(0, 0, {
1355 'name': 'Shelf2 -> Customer',
1356 'action': 'pull',
1357 'picking_type_id': self.company_data['default_warehouse'].out_type_id.id,
1358 'location_src_id': stock_shelf_2.id,
1359 'location_dest_id': self.ref('stock.stock_location_customers'),
1360 })],
1361 })
1362
1363 component_shelf1.write({
1364 'route_ids': [(4, route_shelf1.id)]})
1365 component_shelf2.write({
1366 'route_ids': [(4, route_shelf2.id)]})
1367
1368 # Set enough quantities to make 1 kit_uom_in_kit in WH1
1369 self.env['stock.quant']._update_available_quantity(component_shelf1, self.company_data['default_warehouse'].lot_stock_id, 15)
1370 self.env['stock.quant']._update_available_quantity(component_shelf2, self.company_data['default_warehouse'].lot_stock_id, 10)
1371
1372 # Creating a sale order for 5 kits and confirming it
1373 order_form = Form(self.env['sale.order'])
1374 order_form.partner_id = self.env['res.partner'].create({'name': 'My Test Partner'})
1375 with order_form.order_line.new() as line:
1376 line.product_id = kit_1
1377 line.product_uom_qty = 5
1378 order = order_form.save()
1379 order.action_confirm()
1380
1381 # Now we check that the routes of the components were applied, in order to make sure the routes set
1382 # on the kit itself are ignored
1383 self.assertEqual(len(order.picking_ids), 2)
1384 self.assertEqual(len(order.picking_ids[0].move_ids), 1)
1385 self.assertEqual(len(order.picking_ids[1].move_ids), 1)
1386 moves = order.picking_ids.move_ids
1387 move_shelf1 = moves.filtered(lambda m: m.product_id == component_shelf1)
1388 move_shelf2 = moves.filtered(lambda m: m.product_id == component_shelf2)
1389 self.assertEqual(move_shelf1.location_id.id, stock_shelf_1.id)
1390 self.assertEqual(move_shelf1.location_dest_id.id, self.ref('stock.stock_location_customers'))
1391 self.assertEqual(move_shelf2.location_id.id, stock_shelf_2.id)
1392 self.assertEqual(move_shelf2.location_dest_id.id, self.ref('stock.stock_location_customers'))
1393
1394 def test_11_sale_mrp_explode_kits_uom_quantities(self):
1395
1396 # Create a kit 'kit_1' :
1397 # -----------------------
1398 #
1399 # 2x Dozens kit_1 --|- component_unit x6 Units
1400 # |- component_kg x7 Kg
1401
1402 kit_1 = self._cls_create_product('Kit1', self.uom_unit)
1403 component_unit = self._cls_create_product('Comp Unit', self.uom_unit)
1404 component_kg = self._cls_create_product('Comp Kg', self.uom_kg)
1405
1406 with Form(self.env['mrp.bom']) as bom:
1407 bom.product_tmpl_id = kit_1.product_tmpl_id
1408 bom.product_qty = 2
1409 bom.product_uom_id = self.uom_dozen
1410 bom.type = 'phantom'
1411 with bom.bom_line_ids.new() as line:
1412 line.product_id = component_unit
1413 line.product_qty = 6
1414 line.product_uom_id = self.uom_unit
1415 with bom.bom_line_ids.new() as line:
1416 line.product_id = component_kg
1417 line.product_qty = 7
1418 line.product_uom_id = self.uom_kg
1419
1420 # Create a simple warehouse to receives some products
1421 warehouse_1 = self.env['stock.warehouse'].create({
1422 'name': 'Warehouse 1',
1423 'code': 'WH1'
1424 })
1425 # Set enough quantities to make 1 Test-Dozen kit_uom_in_kit
1426 self.env['stock.quant']._update_available_quantity(component_unit, warehouse_1.lot_stock_id, 12)
1427 self.env['stock.quant']._update_available_quantity(component_kg, warehouse_1.lot_stock_id, 14)
1428
1429 # Creating a sale order for 3 Units of kit_1 and confirming it
1430 order_form = Form(self.env['sale.order'])
1431 order_form.partner_id = self.env['res.partner'].create({'name': 'My Test Partner'})
1432 order_form.warehouse_id = warehouse_1
1433 with order_form.order_line.new() as line:
1434 line.product_id = kit_1
1435 line.product_uom_qty = 2
1436 order = order_form.save()
1437 order.action_confirm()
1438
1439 # Now we check that the routes of the components were applied, in order to make sure the routes set
1440 # on the kit itself are ignored
1441 self.assertEqual(len(order.picking_ids), 1)
1442 self.assertEqual(len(order.picking_ids[0].move_ids), 2)
1443
1444 # Finally, we check the quantities for each component on the picking
1445 move_component_unit = order.picking_ids[0].move_ids.filtered(lambda m: m.product_id == component_unit)
1446 move_component_kg = order.picking_ids[0].move_ids - move_component_unit
1447 self.assertEqual(move_component_unit.product_uom_qty, 0.5)
1448 self.assertEqual(move_component_kg.product_uom_qty, 0.59)
1449
1450 def test_product_type_service_1(self):
1451 route_manufacture = self.company_data['default_warehouse'].manufacture_pull_id.route_id.id
1452 route_mto = self.company_data['default_warehouse'].mto_pull_id.route_id.id
1453 self.uom_unit = self.env.ref('uom.product_uom_unit')
1454
1455 # Create finished product
1456 finished_product = self.env['product.product'].create({
1457 'name': 'Geyser',
1458 'is_storable': True,
1459 'route_ids': [(4, route_mto), (4, route_manufacture)],
1460 })
1461
1462 # Create service type product
1463 product_raw = self.env['product.product'].create({
1464 'name': 'raw Geyser',
1465 'type': 'service',
1466 })
1467
1468 # Create bom for finish product
1469 bom = self.env['mrp.bom'].create({
1470 'product_id': finished_product.id,
1471 'product_tmpl_id': finished_product.product_tmpl_id.id,
1472 'product_uom_id': self.env.ref('uom.product_uom_unit').id,
1473 'product_qty': 1.0,
1474 'type': 'normal',
1475 'bom_line_ids': [(5, 0), (0, 0, {'product_id': product_raw.id})]
1476 })
1477
1478 # Create sale order
1479 sale_form = Form(self.env['sale.order'])
1480 sale_form.partner_id = self.env['res.partner'].create({'name': 'My Test Partner'})
1481 with sale_form.order_line.new() as line:
1482 line.name = finished_product.name
1483 line.product_id = finished_product
1484 line.product_uom_qty = 1.0
1485 line.price_unit = 10.0
1486 sale_order = sale_form.save()
1487
1488 sale_order.action_confirm()
1489
1490 mo = self.env['mrp.production'].search([('product_id', '=', finished_product.id)])
1491
1492 self.assertTrue(mo, 'Manufacturing order created.')
1493
1494 def test_cancel_flow_1(self):
1495 """ Sell a MTO/manufacture product.
1496
1497 Cancel the delivery and the production order. Then duplicate
1498 the delivery. Another production order should be created."""
1499 route_manufacture = self.company_data['default_warehouse'].manufacture_pull_id.route_id
1500 route_mto = self.company_data['default_warehouse'].mto_pull_id.route_id
1501 route_mto.rule_ids.procure_method = "make_to_order"
1502 self.uom_unit = self.env.ref('uom.product_uom_unit')
1503
1504 # Create finished product
1505 finished_product = self.env['product.product'].create({
1506 'name': 'Geyser',
1507 'is_storable': True,
1508 'route_ids': [(4, route_mto.id), (4, route_manufacture.id)],
1509 })
1510
1511 product_raw = self.env['product.product'].create({
1512 'name': 'raw Geyser',
1513 'is_storable': True,
1514 })
1515
1516 # Create bom for finish product
1517 bom = self.env['mrp.bom'].create({
1518 'product_id': finished_product.id,
1519 'product_tmpl_id': finished_product.product_tmpl_id.id,
1520 'product_uom_id': self.env.ref('uom.product_uom_unit').id,
1521 'product_qty': 1.0,
1522 'type': 'normal',
1523 'bom_line_ids': [(5, 0), (0, 0, {'product_id': product_raw.id})]
1524 })
1525
1526 # Create sale order
1527 sale_form = Form(self.env['sale.order'])
1528 sale_form.partner_id = self.env['res.partner'].create({'name': 'My Test Partner'})
1529 with sale_form.order_line.new() as line:
1530 line.name = finished_product.name
1531 line.product_id = finished_product
1532 line.product_uom_qty = 1.0
1533 line.price_unit = 10.0
1534 sale_order = sale_form.save()
1535
1536 sale_order.action_confirm()
1537
1538 mo = self.env['mrp.production'].search([('product_id', '=', finished_product.id)])
1539 delivery = sale_order.picking_ids
1540 delivery.action_cancel()
1541 mo.action_cancel()
1542 copied_delivery = delivery.copy()
1543 copied_delivery.action_confirm()
1544 mos = self.env['mrp.production'].search([('product_id', '=', finished_product.id)])
1545 self.assertEqual(len(mos), 1)
1546 self.assertEqual(mos.state, 'cancel')
1547
1548 def test_cancel_flow_2(self):
1549 """ Sell a MTO/manufacture product.
1550
1551 Cancel the production order and the delivery. Then duplicate
1552 the delivery. Another production order should be created."""
1553 route_manufacture = self.company_data['default_warehouse'].manufacture_pull_id.route_id
1554 route_mto = self.company_data['default_warehouse'].mto_pull_id.route_id
1555 route_mto.rule_ids.procure_method = "make_to_order"
1556 self.uom_unit = self.env.ref('uom.product_uom_unit')
1557
1558 # Create finished product
1559 finished_product = self.env['product.product'].create({
1560 'name': 'Geyser',
1561 'is_storable': True,
1562 'route_ids': [(4, route_mto.id), (4, route_manufacture.id)],
1563 })
1564
1565 product_raw = self.env['product.product'].create({
1566 'name': 'raw Geyser',
1567 'is_storable': True,
1568 })
1569
1570 # Create bom for finish product
1571 bom = self.env['mrp.bom'].create({
1572 'product_id': finished_product.id,
1573 'product_tmpl_id': finished_product.product_tmpl_id.id,
1574 'product_uom_id': self.env.ref('uom.product_uom_unit').id,
1575 'product_qty': 1.0,
1576 'type': 'normal',
1577 'bom_line_ids': [(5, 0), (0, 0, {'product_id': product_raw.id})]
1578 })
1579
1580 # Create sale order
1581 sale_form = Form(self.env['sale.order'])
1582 sale_form.partner_id = self.env['res.partner'].create({'name': 'My Test Partner'})
1583 with sale_form.order_line.new() as line:
1584 line.name = finished_product.name
1585 line.product_id = finished_product
1586 line.product_uom_qty = 1.0
1587 line.price_unit = 10.0
1588 sale_order = sale_form.save()
1589
1590 sale_order.action_confirm()
1591
1592 mo = self.env['mrp.production'].search([('product_id', '=', finished_product.id)])
1593 delivery = sale_order.picking_ids
1594 mo.action_cancel()
1595 delivery.action_cancel()
1596 copied_delivery = delivery.copy()
1597 copied_delivery.action_confirm()
1598 mos = self.env['mrp.production'].search([('product_id', '=', finished_product.id)])
1599 self.assertEqual(len(mos), 1)
1600 self.assertEqual(mos.state, 'cancel')
1601
1602 def test_13_so_return_kit(self):
1603 """
1604 Test that when returning a SO containing only a kit that contains another kit, the
1605 SO delivered quantities is set to 0 (with the all-or-nothing policy).
1606 Products :
1607 Main Kit
1608 Nested Kit
1609 Screw
1610 BoMs :
1611 Main Kit BoM (kit), recipe :
1612 Nested Kit Bom (kit), recipe :
1613 Screw
1614 Business flow :
1615 Create those
1616 Create a Sales order selling one Main Kit BoM
1617 Confirm the sales order
1618 Validate the delivery (outgoing) (qty_delivered = 1)
1619 Create a return for the delivery
1620 Validate return for delivery (ingoing) (qty_delivered = 0)
1621 """
1622 main_kit_product = self.env['product.product'].create({
1623 'name': 'Main Kit',
1624 'is_storable': True,
1625 })
1626
1627 nested_kit_product = self.env['product.product'].create({
1628 'name': 'Nested Kit',
1629 'is_storable': True,
1630 })
1631
1632 product = self.env['product.product'].create({
1633 'name': 'Screw',
1634 'is_storable': True,
1635 })
1636
1637 self.env['mrp.bom'].create({
1638 'product_id': nested_kit_product.id,
1639 'product_tmpl_id': nested_kit_product.product_tmpl_id.id,
1640 'product_qty': 1.0,
1641 'type': 'phantom',
1642 'bom_line_ids': [(5, 0), (0, 0, {'product_id': product.id})]
1643 })
1644
1645 self.env['mrp.bom'].create({
1646 'product_id': main_kit_product.id,
1647 'product_tmpl_id': main_kit_product.product_tmpl_id.id,
1648 'product_qty': 1.0,
1649 'type': 'phantom',
1650 'bom_line_ids': [(5, 0), (0, 0, {'product_id': nested_kit_product.id})]
1651 })
1652
1653 # Create a SO for product Main Kit Product
1654 order_form = Form(self.env['sale.order'])
1655 order_form.partner_id = self.env['res.partner'].create({'name': 'Test Partner'})
1656 with order_form.order_line.new() as line:
1657 line.product_id = main_kit_product
1658 line.product_uom_qty = 1
1659 order = order_form.save()
1660 order.action_confirm()
1661 qty_del_not_yet_validated = sum(sol.qty_delivered for sol in order.order_line)
1662 self.assertEqual(qty_del_not_yet_validated, 0.0, 'No delivery validated yet')
1663
1664 # Validate delivery
1665 pick = order.picking_ids
1666 pick.move_ids.write({'quantity': 1, 'picked': True})
1667 pick.button_validate()
1668 qty_del_validated = sum(sol.qty_delivered for sol in order.order_line)
1669 self.assertEqual(qty_del_validated, 1.0, 'The order went from warehouse to client, so it has been delivered')
1670
1671 # 1 was delivered, now create a return
1672 stock_return_picking_form = Form(self.env['stock.return.picking'].with_context(
1673 active_ids=pick.ids, active_id=pick.ids[0], active_model='stock.picking'))
1674 return_wiz = stock_return_picking_form.save()
1675 for return_move in return_wiz.product_return_moves:
1676 return_move.write({
1677 'quantity': 1,
1678 'to_refund': True
1679 })
1680 res = return_wiz.action_create_returns()
1681 return_pick = self.env['stock.picking'].browse(res['res_id'])
1682 return_pick.move_line_ids.quantity = 1
1683 return_pick.button_validate() # validate return
1684
1685 # Delivered quantities to the client should be 0
1686 qty_del_return_validated = sum(sol.qty_delivered for sol in order.order_line)
1687 self.assertNotEqual(qty_del_return_validated, 1.0, "The return was validated, therefore the delivery from client to"
1688 " company was successful, and the client is left without his 1 product.")
1689 self.assertEqual(qty_del_return_validated, 0.0, "The return has processed, client doesn't have any quantity anymore")
1690
1691 def test_14_change_bom_type(self):
1692 """ This test ensures that updating a Bom type during a flow does not lead to any error """
1693 p1 = self._cls_create_product('Master', self.uom_unit)
1694 p2 = self._cls_create_product('Component', self.uom_unit)
1695 p3 = self.component_a
1696 p1.categ_id.write({
1697 'property_cost_method': 'average',
1698 'property_valuation': 'real_time',
1699 })
1700 stock_location = self.company_data['default_warehouse'].lot_stock_id
1701 self.env['stock.quant']._update_available_quantity(self.component_a, stock_location, 1)
1702
1703 self.env['mrp.bom'].create({
1704 'product_tmpl_id': p1.product_tmpl_id.id,
1705 'product_qty': 1.0,
1706 'type': 'phantom',
1707 'bom_line_ids': [(0, 0, {
1708 'product_id': p2.id,
1709 'product_qty': 1.0,
1710 })]
1711 })
1712
1713 p2_bom = self.env['mrp.bom'].create({
1714 'product_tmpl_id': p2.product_tmpl_id.id,
1715 'product_qty': 1.0,
1716 'type': 'phantom',
1717 'bom_line_ids': [(0, 0, {
1718 'product_id': p3.id,
1719 'product_qty': 1.0,
1720 })]
1721 })
1722
1723 so_form = Form(self.env['sale.order'])
1724 so_form.partner_id = self.env['res.partner'].create({'name': 'Super Partner'})
1725 with so_form.order_line.new() as so_line:
1726 so_line.product_id = p1
1727 so = so_form.save()
1728 so.action_confirm()
1729
1730 so.picking_ids.button_validate()
1731
1732 p2_bom.type = "normal"
1733
1734 so._create_invoices()
1735 invoice = so.invoice_ids
1736 invoice.action_post()
1737 self.assertEqual(invoice.state, 'posted')
1738
1739 @skip('Temporary to fast merge new valuation')
1740 def test_15_anglo_saxon_variant_price_unit(self):
1741 """
1742 Test the price unit of a variant from which template has another variant with kit bom.
1743 Products:
1744 Template A
1745 variant NOKIT
1746 variant KIT:
1747 Component A
1748 Business Flow:
1749 create products and kit
1750 create SO selling both variants
1751 validate the delivery
1752 create the invoice
1753 post the invoice
1754 """
1755
1756 # Create environment
1757 self.env.company.currency_id = self.env.ref('base.USD')
1758 self.env.company.anglo_saxon_accounting = True
1759 self.partner = self.env['res.partner'].create({'name': 'Test Partner'})
1760 self.category = self.env.ref('product.product_category_goods').copy({
1761 'name': 'Test category',
1762 'property_valuation': 'real_time',
1763 'property_cost_method': 'fifo',
1764 })
1765 account_receiv = self.env['account.account'].create({'name': 'Receivable', 'code': 'RCV00', 'account_type': 'asset_receivable', 'reconcile': True})
1766 account_expense = self.env['account.account'].create({'name': 'Expense', 'code': 'EXP00', 'account_type': 'liability_current', 'reconcile': True})
1767 account_income = self.env['account.account'].create({'name': 'Income', 'code': 'INC00', 'account_type': 'asset_current', 'reconcile': True})
1768 account_valuation = self.env['account.account'].create({'name': 'Valuation', 'code': 'STV00', 'account_type': 'asset_receivable', 'reconcile': True})
1769 self.stock_location = self.company_data['default_warehouse'].lot_stock_id
1770 self.partner.property_account_receivable_id = account_receiv
1771 self.category.property_account_income_categ_id = account_income
1772 self.category.property_account_expense_categ_id = account_expense
1773 self.category.property_stock_account_input_categ_id = account_receiv
1774 self.category.property_stock_valuation_account_id = account_valuation
1775
1776 # Create variant attributes
1777 self.prod_att_test = self.env['product.attribute'].create({'name': 'test'})
1778 self.prod_attr_KIT = self.env['product.attribute.value'].create({'name': 'KIT', 'attribute_id': self.prod_att_test.id, 'sequence': 1})
1779 self.prod_attr_NOKIT = self.env['product.attribute.value'].create({'name': 'NOKIT', 'attribute_id': self.prod_att_test.id, 'sequence': 2})
1780
1781 # Create the template
1782 self.product_template = self.env['product.template'].create({
1783 'name': 'Template A',
1784 'is_storable': True,
1785 'uom_id': self.uom_unit.id,
1786 'invoice_policy': 'delivery',
1787 'categ_id': self.category.id,
1788 'attribute_line_ids': [(0, 0, {
1789 'attribute_id': self.prod_att_test.id,
1790 'value_ids': [(6, 0, [self.prod_attr_KIT.id, self.prod_attr_NOKIT.id])]
1791 })]
1792 })
1793
1794 # Create the variants
1795 self.pt_attr_KIT = self.product_template.attribute_line_ids[0].product_template_value_ids[0]
1796 self.pt_attr_NOKIT = self.product_template.attribute_line_ids[0].product_template_value_ids[1]
1797 self.variant_KIT = self.product_template._get_variant_for_combination(self.pt_attr_KIT)
1798 self.variant_NOKIT = self.product_template._get_variant_for_combination(self.pt_attr_NOKIT)
1799 # Assign a cost to the NOKIT variant
1800 self.variant_NOKIT.write({'standard_price': 25})
1801
1802 # Create the components
1803 self.comp_kit_a = self.env['product.product'].create({
1804 'name': 'Component Kit A',
1805 'is_storable': True,
1806 'uom_id': self.uom_unit.id,
1807 'categ_id': self.category.id,
1808 'standard_price': 20
1809 })
1810 self.comp_kit_b = self.env['product.product'].create({
1811 'name': 'Component Kit B',
1812 'is_storable': True,
1813 'uom_id': self.uom_unit.id,
1814 'categ_id': self.category.id,
1815 'standard_price': 10
1816 })
1817
1818 # Create the bom
1819 bom = self.env['mrp.bom'].create({
1820 'product_tmpl_id': self.product_template.id,
1821 'product_id': self.variant_KIT.id,
1822 'product_qty': 1.0,
1823 'type': 'phantom'
1824 })
1825 self.env['mrp.bom.line'].create({
1826 'product_id': self.comp_kit_a.id,
1827 'product_qty': 2.0,
1828 'bom_id': bom.id
1829 })
1830 self.env['mrp.bom.line'].create({
1831 'product_id': self.comp_kit_b.id,
1832 'product_qty': 1.0,
1833 'bom_id': bom.id
1834 })
1835
1836 # Create the quants
1837 self.env['stock.quant']._update_available_quantity(self.comp_kit_a, self.stock_location, 2)
1838 self.env['stock.quant']._update_available_quantity(self.comp_kit_b, self.stock_location, 1)
1839 self.env['stock.quant']._update_available_quantity(self.variant_NOKIT, self.stock_location, 1)
1840
1841 # Create the sale order
1842 so_vals = {
1843 'partner_id': self.partner.id,
1844 'partner_invoice_id': self.partner.id,
1845 'partner_shipping_id': self.partner.id,
1846 'order_line': [(0, 0, {
1847 'name': self.variant_KIT.name,
1848 'product_id': self.variant_KIT.id,
1849 'product_uom_qty': 1,
1850 'price_unit': 100,
1851 }), (0, 0, {
1852 'name': self.variant_NOKIT.name,
1853 'product_id': self.variant_NOKIT.id,
1854 'product_uom_qty': 1,
1855 'price_unit': 50
1856 })],
1857 'company_id': self.env.company.id
1858 }
1859 so = self.env['sale.order'].create(so_vals)
1860 # Validate the sale order
1861 so.action_confirm()
1862 # Deliver the products
1863 pick = so.picking_ids
1864 pick.button_validate()
1865 # Create the invoice
1866 so._create_invoices()
1867 # Validate the invoice
1868 invoice = so.invoice_ids
1869 invoice.action_post()
1870
1871 amls = invoice.line_ids
1872 aml_kit_expense = amls.filtered(lambda l: l.display_type == 'cogs' and l.debit > 0 and l.product_id == self.variant_KIT)
1873 aml_kit_output = amls.filtered(lambda l: l.display_type == 'cogs' and l.credit > 0 and l.product_id == self.variant_KIT)
1874 aml_nokit_expense = amls.filtered(lambda l: l.display_type == 'cogs' and l.debit > 0 and l.product_id == self.variant_NOKIT)
1875 aml_nokit_output = amls.filtered(lambda l: l.display_type == 'cogs' and l.credit > 0 and l.product_id == self.variant_NOKIT)
1876
1877 # Check that the Cost of Goods Sold for variant KIT is equal to (2*20)+10 = 50
1878 self.assertEqual(aml_kit_expense.debit, 50, "Cost of Good Sold entry missing or mismatching for variant with kit")
1879 self.assertEqual(aml_kit_output.credit, 50, "Cost of Good Sold entry missing or mismatching for variant with kit")
1880 # Check that the Cost of Goods Sold for variant NOKIT is equal to its standard_price = 25
1881 self.assertEqual(aml_nokit_expense.debit, 25, "Cost of Good Sold entry missing or mismatching for variant without kit")
1882 self.assertEqual(aml_nokit_output.credit, 25, "Cost of Good Sold entry missing or mismatching for variant without kit")
1883
1884 @skip('Temporary to fast merge new valuation')
1885 def test_16_anglo_saxon_variant_price_unit_multi_company(self):
1886 """
1887 Test the price unit of the BOM of the stock move is taken
1888 Products:
1889 Template A
1890 variant KIT 1
1891 variant KIT 2
1892 Business Flow:
1893 create SO
1894 validate the delivery
1895 (cannot archive the BOM, because it is used in a non-invoiced line)
1896 update the BOM and create a new one
1897 create the invoice
1898 post the invoice
1899 """
1900
1901 # Create environment
1902 self.partner = self.env['res.partner'].create({'name': 'Test Partner'})
1903 self.category = self.env.ref('product.product_category_goods').copy({
1904 'name': 'Test category',
1905 'property_valuation': 'real_time',
1906 'property_cost_method': 'fifo',
1907 })
1908 account_receiv = self.env['account.account'].create({'name': 'Receivable', 'code': 'RCV00', 'account_type': 'asset_receivable', 'reconcile': True})
1909 account_income = self.env['account.account'].create({'name': 'Income', 'code': 'INC00', 'account_type': 'asset_current', 'reconcile': True})
1910 account_expense = self.env['account.account'].create({'name': 'Expense', 'code': 'EXP00', 'account_type': 'liability_current', 'reconcile': True})
1911 account_valuation = self.env['account.account'].create({'name': 'Valuation', 'code': 'STV00', 'account_type': 'asset_receivable', 'reconcile': True})
1912 self.stock_location = self.company_data['default_warehouse'].lot_stock_id
1913 self.partner.property_account_receivable_id = account_receiv
1914 self.category.property_account_income_categ_id = account_income
1915 self.category.property_account_expense_categ_id = account_expense
1916 self.category.property_stock_account_input_categ_id = account_income
1917 self.category.property_stock_valuation_account_id = account_valuation
1918
1919 # Create variant attributes
1920 self.prod_att_test = self.env['product.attribute'].create({'name': 'test'})
1921 self.prod_attr_KIT_A = self.env['product.attribute.value'].create({'name': 'KIT A', 'attribute_id': self.prod_att_test.id, 'sequence': 1})
1922
1923 # Create the template
1924 self.product_template = self.env['product.template'].create({
1925 'name': 'Template A',
1926 'is_storable': True,
1927 'uom_id': self.uom_unit.id,
1928 'invoice_policy': 'delivery',
1929 'categ_id': self.category.id,
1930 'attribute_line_ids': [(0, 0, {
1931 'attribute_id': self.prod_att_test.id,
1932 'value_ids': [(6, 0, [self.prod_attr_KIT_A.id])]
1933 })]
1934 })
1935
1936 # Create another variant
1937 self.pt_attr_KIT_A = self.product_template.attribute_line_ids[0].product_template_value_ids[0]
1938 self.variant_KIT_A = self.product_template._get_variant_for_combination(self.pt_attr_KIT_A)
1939 # Assign a cost to the NOKIT variant
1940 self.variant_KIT_A.write({'standard_price': 25})
1941
1942 # Create the components
1943 self.comp_kit_a = self.env['product.product'].create({
1944 'name': 'Component Kit A',
1945 'is_storable': True,
1946 'uom_id': self.uom_unit.id,
1947 'categ_id': self.category.id,
1948 'standard_price': 20
1949 })
1950 self.comp_kit_b = self.env['product.product'].create({
1951 'name': 'Component Kit B',
1952 'is_storable': True,
1953 'uom_id': self.uom_unit.id,
1954 'categ_id': self.category.id,
1955 'standard_price': 10
1956 })
1957
1958 # Create the bom
1959 bom = self.env['mrp.bom'].create({
1960 'product_tmpl_id': self.product_template.id,
1961 'product_id': self.variant_KIT_A.id,
1962 'product_qty': 1.0,
1963 'type': 'phantom',
1964 'company_id': self.env.company.id,
1965 })
1966 self.env['mrp.bom.line'].create({
1967 'product_id': self.comp_kit_a.id,
1968 'product_qty': 1.0,
1969 'company_id': self.env.company.id,
1970 'bom_id': bom.id
1971 })
1972
1973 # Create the quants
1974 self.env['stock.quant']._update_available_quantity(self.comp_kit_a, self.stock_location, 2)
1975 self.env['stock.quant']._update_available_quantity(self.comp_kit_b, self.stock_location, 1)
1976
1977 # Create the sale order
1978 so_vals = {
1979 'partner_id': self.partner.id,
1980 'partner_invoice_id': self.partner.id,
1981 'partner_shipping_id': self.partner.id,
1982 'order_line': [(0, 0, {
1983 'name': self.variant_KIT_A.name,
1984 'product_id': self.variant_KIT_A.id,
1985 'product_uom_qty': 1,
1986 'price_unit': 50
1987 })],
1988 'company_id': self.env.company.id,
1989 }
1990 so = self.env['sale.order'].create(so_vals)
1991 # Validate the sale order
1992 so.action_confirm()
1993 # Deliver the products
1994 pick = so.picking_ids
1995 pick.button_validate()
1996 # Update BOM
1997 bom_updated = self.env['mrp.bom'].create({
1998 'product_tmpl_id': self.product_template.id,
1999 'product_id': self.variant_KIT_A.id,
2000 'product_qty': 1.0,
2001 'type': 'phantom',
2002 'company_id': self.env.company.id,
2003 })
2004 self.env['mrp.bom.line'].create({
2005 'product_id': self.comp_kit_b.id,
2006 'product_qty': 1.0,
2007 'company_id': self.env.company.id,
2008 'bom_id': bom_updated.id
2009 })
2010
2011 # Create the invoice
2012 so._create_invoices()
2013 # Validate the invoice
2014 invoice = so.invoice_ids
2015 invoice.action_post()
2016
2017 amls = invoice.line_ids
2018 aml_nokit_expense = amls.filtered(lambda l: l.display_type == 'cogs' and l.debit > 0 and l.product_id == self.variant_KIT_A)
2019 aml_nokit_output = amls.filtered(lambda l: l.display_type == 'cogs' and l.credit > 0 and l.product_id == self.variant_KIT_A)
2020
2021 # Check that the Cost of Goods Sold for variant NOKIT is equal to the cost of the first BOM
2022 self.assertEqual(aml_nokit_expense.debit, 20, "Cost of Good Sold entry missing or mismatching for variant without kit")
2023 self.assertEqual(aml_nokit_output.credit, 20, "Cost of Good Sold entry missing or mismatching for variant without kit")
2024
2025 def test_reconfirm_cancelled_kit(self):
2026 so = self.env['sale.order'].create({
2027 'partner_id': self.env['res.partner'].create({'name': 'Test Partner'}).id,
2028 'order_line': [
2029 (0, 0, {
2030 'name': self.kit_1.name,
2031 'product_id': self.kit_1.id,
2032 'product_uom_qty': 1.0,
2033 'price_unit': 1.0,
2034 })
2035 ],
2036 })
2037
2038 # Updating the quantities in stock to prevent a 'Not enough inventory' warning message.
2039 stock_location = self.company_data['default_warehouse'].lot_stock_id
2040 self.env['stock.quant']._update_available_quantity(self.component_a, stock_location, 10)
2041 self.env['stock.quant']._update_available_quantity(self.component_b, stock_location, 10)
2042 self.env['stock.quant']._update_available_quantity(self.component_c, stock_location, 10)
2043
2044 so.action_confirm()
2045 # Check picking creation
2046 self.assertEqual(len(so.picking_ids), 1, "A picking should be created after the SO validation")
2047
2048 so.picking_ids.button_validate()
2049
2050 so._action_cancel()
2051 so.action_draft()
2052 so.action_confirm()
2053 self.assertEqual(len(so.picking_ids), 1, "The product was already delivered, no need to re-create a delivery order")
2054
2055 @skip('Temporary to fast merge new valuation')
2056 def test_kit_margin_and_return_picking(self):
2057 """ This test ensure that, when returning the components of a sold kit, the
2058 sale order line cost does not change"""
2059 kit = self._cls_create_product('Super Kit', self.uom_unit)
2060 (kit + self.component_a).categ_id.property_cost_method = 'fifo'
2061
2062 self.env['mrp.bom'].create({
2063 'product_tmpl_id': kit.product_tmpl_id.id,
2064 'product_qty': 1.0,
2065 'type': 'phantom',
2066 'bom_line_ids': [(0, 0, {
2067 'product_id': self.component_a.id,
2068 'product_qty': 1.0,
2069 })]
2070 })
2071
2072 self.component_a.standard_price = 10
2073 kit.button_bom_cost()
2074
2075 stock_location = self.company_data['default_warehouse'].lot_stock_id
2076 self.env['stock.quant']._update_available_quantity(self.component_a, stock_location, 1)
2077
2078 so_form = Form(self.env['sale.order'])
2079 so_form.partner_id = self.partner_a
2080 with so_form.order_line.new() as line:
2081 line.product_id = kit
2082 so = so_form.save()
2083 so.action_confirm()
2084
2085 line = so.order_line
2086 price = line.product_id.with_company(line.company_id)._compute_average_price(0, line.product_uom_qty, line.move_ids)
2087 self.assertEqual(price, 10)
2088
2089 picking = so.picking_ids
2090 picking.button_validate()
2091
2092 ctx = {'active_ids':picking.ids, 'active_id': picking.ids[0], 'active_model': 'stock.picking'}
2093 return_picking_wizard_form = Form(self.env['stock.return.picking'].with_context(ctx))
2094 return_picking_wizard = return_picking_wizard_form.save()
2095 return_picking_wizard.product_return_moves.quantity = 1
2096 return_picking_wizard.action_create_returns()
2097
2098 price = line.product_id.with_company(line.company_id)._compute_average_price(0, line.product_uom_qty, line.move_ids)
2099 self.assertEqual(price, 10)
2100
2101 def test_kit_decrease_sol_qty(self):
2102 """
2103 Create and confirm a SO with a qty. Increasing/Decreasing the SOL qty
2104 should update the qty on the delivery. Then, process the delivery, make
2105 a return and adapt the SOL qty -> there should not be any new picking
2106 """
2107 stock_location = self.company_data['default_warehouse'].lot_stock_id
2108 custo_location = self.env.ref('stock.stock_location_customers')
2109
2110 grp_uom = self.env.ref('uom.group_uom')
2111 self.env.user.write({'group_ids': [(4, grp_uom.id)]})
2112
2113 # 100 kit_3 = 100 x compo_f + 200 x compo_g
2114 self.env['stock.quant']._update_available_quantity(self.component_f, stock_location, 100)
2115 self.env['stock.quant']._update_available_quantity(self.component_g, stock_location, 200)
2116
2117 so_form = Form(self.env['sale.order'])
2118 so_form.partner_id = self.partner_a
2119 with so_form.order_line.new() as line:
2120 line.product_id = self.kit_3
2121 line.product_uom_qty = 7
2122 line.product_uom_id = self.uom_ten
2123 so = so_form.save()
2124 so.action_confirm()
2125
2126 delivery = so.picking_ids
2127 self.assertRecordValues(delivery.move_ids, [
2128 {'product_id': self.component_f.id, 'product_uom_qty': 70},
2129 {'product_id': self.component_g.id, 'product_uom_qty': 140},
2130 ])
2131
2132 # Decrease
2133 with Form(so) as so_form:
2134 with so_form.order_line.edit(0) as line:
2135 line.product_uom_qty = 6
2136 self.assertRecordValues(delivery.move_ids, [
2137 {'product_id': self.component_f.id, 'product_uom_qty': 60},
2138 {'product_id': self.component_g.id, 'product_uom_qty': 120},
2139 ])
2140
2141 # Increase
2142 with Form(so) as so_form:
2143 with so_form.order_line.edit(0) as line:
2144 line.product_uom_qty = 10
2145 self.assertRecordValues(delivery.move_ids, [
2146 {'product_id': self.component_f.id, 'product_uom_qty': 100},
2147 {'product_id': self.component_g.id, 'product_uom_qty': 200},
2148 ])
2149 delivery.button_validate()
2150
2151 # Return 2 [uom_ten] x kit_3
2152 return_wizard_form = Form(self.env['stock.return.picking'].with_context(active_ids=delivery.ids, active_id=delivery.id, active_model='stock.picking'))
2153 return_wizard = return_wizard_form.save()
2154 return_wizard.product_return_moves[0].quantity = 20
2155 return_wizard.product_return_moves[1].quantity = 40
2156 action = return_wizard.action_create_returns()
2157 return_picking = self.env['stock.picking'].browse(action['res_id'])
2158 return_picking.move_ids.picked = True
2159 return_picking.button_validate()
2160
2161 # Adapt the SOL qty according to the delivered one
2162 with Form(so) as so_form:
2163 with so_form.order_line.edit(0) as line:
2164 line.product_uom_qty = 8
2165
2166 self.assertRecordValues(so.picking_ids.sorted('id').move_ids, [
2167 {'product_id': self.component_f.id, 'location_dest_id': custo_location.id, 'quantity': 100, 'state': 'done'},
2168 {'product_id': self.component_g.id, 'location_dest_id': custo_location.id, 'quantity': 200, 'state': 'done'},
2169 {'product_id': self.component_f.id, 'location_dest_id': stock_location.id, 'quantity': 20, 'state': 'done'},
2170 {'product_id': self.component_g.id, 'location_dest_id': stock_location.id, 'quantity': 40, 'state': 'done'},
2171 ])
2172
2173 def test_kit_decrease_sol_qty_to_zero(self):
2174 """
2175 Create and confirm a SO with a kit product. Increasing/Decreasing the SOL qty
2176 should update the qty on the delivery.
2177 """
2178 stock_location = self.company_data['default_warehouse'].lot_stock_id
2179
2180 grp_uom = self.env.ref('uom.group_uom')
2181 self.env.user.write({'group_ids': [(4, grp_uom.id)]})
2182
2183 # 10 kit_3 = 10 x compo_f + 20 x compo_g
2184 self.env['stock.quant']._update_available_quantity(self.component_f, stock_location, 10)
2185 self.env['stock.quant']._update_available_quantity(self.component_g, stock_location, 20)
2186
2187 so_form = Form(self.env['sale.order'])
2188 so_form.partner_id = self.partner_a
2189 with so_form.order_line.new() as line:
2190 line.product_id = self.kit_3
2191 line.product_uom_qty = 2
2192 line.product_uom_id = self.uom_ten
2193 so = so_form.save()
2194 so.action_confirm()
2195
2196 delivery = so.picking_ids
2197 self.assertRecordValues(delivery.move_ids, [
2198 {'product_id': self.component_f.id, 'product_uom_qty': 20},
2199 {'product_id': self.component_g.id, 'product_uom_qty': 40},
2200 ])
2201
2202 # Decrease the qty to 0
2203 with Form(so) as so_form:
2204 with so_form.order_line.edit(0) as line:
2205 line.product_uom_qty = 0
2206 self.assertRecordValues(delivery.move_ids, [
2207 {'product_id': self.component_f.id, 'product_uom_qty': 0},
2208 {'product_id': self.component_g.id, 'product_uom_qty': 0},
2209 ])
2210
2211 def test_kit_return_and_decrease_sol_qty_to_zero(self):
2212 """
2213 Create and confirm a SO with a kit product.
2214 Deliver in two steps & Return the components
2215 Set the SOL qty to 0
2216
2217 Check that the move chain is adapted accordingly.
2218 """
2219 stock_location = self.company_data['default_warehouse'].lot_stock_id
2220 self.company_data['default_warehouse'].delivery_steps = 'pick_ship'
2221
2222 grp_uom = self.env.ref('uom.group_uom')
2223 self.env.user.write({'group_ids': [(4, grp_uom.id)]})
2224
2225 # 10 kit_3 = 10 x compo_f + 20 x compo_g
2226 self.env['stock.quant']._update_available_quantity(self.component_f, stock_location, 10)
2227 self.env['stock.quant']._update_available_quantity(self.component_g, stock_location, 20)
2228
2229 so_form = Form(self.env['sale.order'])
2230 so_form.partner_id = self.partner_a
2231 with so_form.order_line.new() as line:
2232 line.product_id = self.kit_3
2233 line.product_uom_qty = 2
2234 line.product_uom_id = self.uom_ten
2235 so = so_form.save()
2236 so.action_confirm()
2237
2238 pick = so.picking_ids
2239 for m in pick.move_ids:
2240 m.write({'quantity': m.product_uom_qty, 'picked': True})
2241 pick.button_validate()
2242 self.assertEqual(pick.state, 'done')
2243 delivery = so.picking_ids - pick
2244 for m in delivery.move_ids:
2245 m.write({'quantity': m.product_uom_qty, 'picked': True})
2246 delivery.button_validate()
2247 self.assertEqual(delivery.state, 'done')
2248 self.assertEqual(so.order_line.qty_delivered, 2)
2249
2250 ctx = {'active_id': delivery.id, 'active_model': 'stock.picking'}
2251 return_wizard = Form(self.env['stock.return.picking'].with_context(ctx)).save()
2252 for line in return_wizard.product_return_moves:
2253 line.quantity = line.move_id.quantity
2254 return_picking = return_wizard._create_return()
2255 for m in return_picking.move_ids:
2256 m.write({'quantity': m.product_uom_qty, 'picked': True})
2257 return_picking.button_validate()
2258
2259 self.assertEqual(return_picking.state, 'done')
2260 self.assertEqual(so.order_line.qty_delivered, 0)
2261
2262 with Form(so) as so_form:
2263 with so_form.order_line.edit(0) as line:
2264 line.product_uom_qty = 0
2265
2266 self.assertEqual(so.picking_ids, pick | delivery | return_picking)
2267 self.assertRecordValues(so.picking_ids.move_ids.sorted(lambda m: (m.picking_id.id, m.product_id.id)), [
2268 {'picking_id': pick.id, 'product_id': self.component_f.id, 'quantity': 20.0},
2269 {'picking_id': pick.id, 'product_id': self.component_g.id, 'quantity': 40.0},
2270 {'picking_id': delivery.id, 'product_id': self.component_f.id, 'quantity': 20.0},
2271 {'picking_id': delivery.id, 'product_id': self.component_g.id, 'quantity': 40.0},
2272 {'picking_id': return_picking.id, 'product_id': self.component_f.id, 'quantity': 20.0},
2273 {'picking_id': return_picking.id, 'product_id': self.component_g.id, 'quantity': 40.0},
2274 ])
2275
2276 @skip('Temporary to fast merge new valuation')
2277 def test_fifo_reverse_and_create_new_invoice(self):
2278 """
2279 FIFO automated
2280 Kit with one component
2281 Receive the component: 1@10, 1@50
2282 Deliver 1 kit
2283 Post the invoice, add a credit note with option 'new draft inv'
2284 Post the second invoice
2285 COGS should be based on the delivered kit
2286 """
2287 kit = self._cls_create_product('Simple Kit', self.uom_unit)
2288 categ_form = Form(self.env['product.category'])
2289 categ_form.name = 'Super Fifo'
2290 categ_form.property_cost_method = 'fifo'
2291 categ_form.property_valuation = 'real_time'
2292 categ = categ_form.save()
2293 (kit + self.component_a).categ_id = categ
2294
2295 self.env['mrp.bom'].create({
2296 'product_tmpl_id': kit.product_tmpl_id.id,
2297 'product_qty': 1.0,
2298 'type': 'phantom',
2299 'bom_line_ids': [(0, 0, {'product_id': self.component_a.id, 'product_qty': 1.0})]
2300 })
2301
2302 in_moves = self.env['stock.move'].create([{
2303 'product_id': self.component_a.id,
2304 'location_id': self.env.ref('stock.stock_location_suppliers').id,
2305 'location_dest_id': self.company_data['default_warehouse'].lot_stock_id.id,
2306 'product_uom': self.component_a.uom_id.id,
2307 'product_uom_qty': 1,
2308 'price_unit': p,
2309 } for p in [10, 50]])
2310 in_moves._action_confirm()
2311 in_moves.write({'quantity': 1, 'picked': True})
2312 in_moves._action_done()
2313
2314 so = self.env['sale.order'].create({
2315 'partner_id': self.env['res.partner'].create({'name': 'Test Partner'}).id,
2316 'order_line': [
2317 (0, 0, {
2318 'name': kit.name,
2319 'product_id': kit.id,
2320 'product_uom_qty': 1.0,
2321 'price_unit': 100,
2322 'tax_ids': False,
2323 })],
2324 })
2325 so.action_confirm()
2326
2327 picking = so.picking_ids
2328 picking.move_ids.write({'quantity': 1.0, 'picked': True})
2329 picking.button_validate()
2330
2331 invoice01 = so._create_invoices()
2332 invoice01.action_post()
2333
2334 move_reversal = self.env['account.move.reversal'].with_context(active_model="account.move", active_ids=invoice01.ids).create({
2335 'journal_id': invoice01.journal_id.id,
2336 })
2337 reversal = move_reversal.modify_moves()
2338 invoice02 = self.env['account.move'].browse(reversal['res_id'])
2339 invoice02.action_post()
2340
2341 amls = invoice02.line_ids
2342 stock_out_aml = amls.filtered(lambda aml: aml.account_id == categ.property_stock_account_output_categ_id)
2343 self.assertEqual(stock_out_aml.debit, 0)
2344 self.assertEqual(stock_out_aml.credit, 10)
2345 cogs_aml = amls.filtered(lambda aml: aml.account_id == categ.property_account_expense_categ_id)
2346 self.assertEqual(cogs_aml.debit, 10)
2347 self.assertEqual(cogs_aml.credit, 0)
2348
2349 @skip('Temporary to fast merge new valuation')
2350 def test_kit_avco_amls_reconciliation(self):
2351 self.stock_account_product_categ.property_cost_method = 'average'
2352
2353 compo01, compo02, kit = self.env['product.product'].create([{
2354 'name': name,
2355 'is_storable': True,
2356 'standard_price': price,
2357 'categ_id': self.stock_account_product_categ.id,
2358 'invoice_policy': 'delivery',
2359 } for name, price in [
2360 ('Compo 01', 10),
2361 ('Compo 02', 20),
2362 ('Kit', 0),
2363 ]])
2364
2365 self.env['stock.quant']._update_available_quantity(compo01, self.company_data['default_warehouse'].lot_stock_id, 1)
2366 self.env['stock.quant']._update_available_quantity(compo02, self.company_data['default_warehouse'].lot_stock_id, 1)
2367
2368 self.env['mrp.bom'].create({
2369 'product_id': kit.id,
2370 'product_tmpl_id': kit.product_tmpl_id.id,
2371 'product_uom_id': kit.uom_id.id,
2372 'product_qty': 1.0,
2373 'type': 'phantom',
2374 'bom_line_ids': [
2375 (0, 0, {'product_id': compo01.id, 'product_qty': 1.0}),
2376 (0, 0, {'product_id': compo02.id, 'product_qty': 1.0}),
2377 ],
2378 })
2379
2380 so = self.env['sale.order'].create({
2381 'partner_id': self.partner_a.id,
2382 'order_line': [
2383 (0, 0, {
2384 'name': kit.name,
2385 'product_id': kit.id,
2386 'product_uom_qty': 1.0,
2387 'price_unit': 5,
2388 'tax_ids': False,
2389 })],
2390 })
2391 so.action_confirm()
2392 so.picking_ids.move_line_ids.quantity = 1
2393 so.picking_ids.move_ids.picked = True
2394 so.picking_ids.button_validate()
2395
2396 invoice = so._create_invoices()
2397 invoice.action_post()
2398
2399 self.assertEqual(len(invoice.line_ids.filtered('reconciled')), 1)
2400
2401 def test_avoid_removing_kit_bom_in_use(self):
2402 so = self.env['sale.order'].create({
2403 'partner_id': self.partner_a.id,
2404 'order_line': [
2405 (0, 0, {
2406 'name': self.kit_1.name,
2407 'product_id': self.kit_1.id,
2408 'product_uom_qty': 1.0,
2409 'price_unit': 5,
2410 'tax_ids': False,
2411 })],
2412 })
2413 self.bom_kit_1.action_archive()
2414 self.bom_kit_1.action_unarchive()
2415
2416 so.action_confirm()
2417 with self.assertRaises(UserError):
2418 self.bom_kit_1.write({'type': 'normal'})
2419 with self.assertRaises(UserError):
2420 self.bom_kit_1.action_archive()
2421 with self.assertRaises(UserError):
2422 self.bom_kit_1.unlink()
2423
2424 for move in so.order_line.move_ids:
2425 move.write({'quantity': move.product_uom_qty, 'picked': True})
2426 so.picking_ids.button_validate()
2427
2428 self.assertEqual(so.picking_ids.state, 'done')
2429 with self.assertRaises(UserError):
2430 self.bom_kit_1.write({'type': 'normal'})
2431 with self.assertRaises(UserError):
2432 self.bom_kit_1.action_archive()
2433 with self.assertRaises(UserError):
2434 self.bom_kit_1.unlink()
2435
2436 invoice = so._create_invoices()
2437 invoice.action_post()
2438
2439 self.assertEqual(invoice.state, 'posted')
2440 self.bom_kit_1.action_archive()
2441 self.bom_kit_1.action_unarchive()
2442 self.bom_kit_1.write({'type': 'normal'})
2443 self.bom_kit_1.write({'type': 'phantom'})
2444 self.bom_kit_1.unlink()
2445
2446 def test_merge_move_kit_on_adding_new_sol(self):
2447 """
2448 Create and confirm an SO for 2 similar kit products.
2449 Add a new sale order line for an other unrelated prodcut.
2450
2451 Check that the delivery kit moves were not merged by the confirmation of the new move.
2452 """
2453 warehouse = self.company_data['default_warehouse']
2454 warehouse.delivery_steps = 'pick_ship'
2455 kit = self.kit_3
2456 # create a similar kit
2457 bom_copy = kit.bom_ids[0].copy()
2458 kit_copy = kit.copy()
2459 bom_copy.product_tmpl_id = kit_copy.product_tmpl_id
2460 # put component in stock: 10 kit = 10 x comp_f + 20 x comp_g
2461 self.env['stock.quant']._update_available_quantity(self.component_f, warehouse.lot_stock_id, 10)
2462 self.env['stock.quant']._update_available_quantity(self.component_g, warehouse.lot_stock_id, 20)
2463 self.env['stock.quant']._update_available_quantity(self.component_a, warehouse.lot_stock_id, 5)
2464
2465 so_form = Form(self.env['sale.order'])
2466 so_form.partner_id = self.partner_a
2467 with so_form.order_line.new() as line:
2468 line.product_id = kit
2469 line.product_uom_qty = 2
2470 with so_form.order_line.new() as line:
2471 line.product_id = kit_copy
2472 line.product_uom_qty = 3
2473 so = so_form.save()
2474 so.action_confirm()
2475
2476 pick = so.picking_ids.filtered(lambda p: p.picking_type_id == warehouse.pick_type_id)
2477 expected_pick_moves = [
2478 { 'quantity': 2.0, 'product_id': self.component_f.id, 'bom_line_id': kit.bom_ids[0].bom_line_ids.filtered(lambda bl: bl.product_id == self.component_f).id},
2479 { 'quantity': 3.0, 'product_id': self.component_f.id, 'bom_line_id': bom_copy.bom_line_ids.filtered(lambda bl: bl.product_id == self.component_f).id},
2480 { 'quantity': 4.0, 'product_id': self.component_g.id, 'bom_line_id': kit.bom_ids[0].bom_line_ids.filtered(lambda bl: bl.product_id == self.component_g).id},
2481 { 'quantity': 6.0, 'product_id': self.component_g.id, 'bom_line_id': bom_copy.bom_line_ids.filtered(lambda bl: bl.product_id == self.component_g).id},
2482 ]
2483 self.assertRecordValues(pick.move_ids.sorted(lambda m: m.quantity), expected_pick_moves)
2484 with Form(so) as so_form:
2485 with so_form.order_line.new() as line:
2486 line.product_id = self.component_a
2487 line.product_uom_qty = 1
2488 expected_pick_moves = [
2489 { 'quantity': 1.0, 'product_id': self.component_a.id, 'bom_line_id': False},
2490 ] + expected_pick_moves
2491 self.assertRecordValues(pick.move_ids.sorted(lambda m: m.quantity), expected_pick_moves)
2492
2493 def test_return_kit_in_quarantine_location(self):
2494 """
2495 Return a kit to WH/Return Location
2496 Push Rule: WH/Return -> WH/Stock
2497 Ensure the delivered qty is correctly updated
2498 """
2499 wh = self.company_data['default_warehouse']
2500 stock_location = wh.lot_stock_id
2501
2502 return_location = self.env['stock.location'].create({
2503 'location_id': stock_location.location_id.id,
2504 'name': 'Return Location',
2505 'usage': 'internal',
2506 })
2507
2508 self.env['stock.route'].create({
2509 'name': 'Return Route',
2510 'warehouse_selectable': True,
2511 'warehouse_ids': [(4, wh.id)],
2512 'rule_ids': [(0, 0, {
2513 'name': 'Return to Stock',
2514 'location_src_id': return_location.id,
2515 'location_dest_id': stock_location.id,
2516 'company_id': self.company_data['company'].id,
2517 'action': 'push',
2518 'auto': 'manual',
2519 'picking_type_id': wh.int_type_id.id,
2520 })],
2521 })
2522
2523 order = self.env['sale.order'].create({
2524 'partner_id': self.partner_a.id,
2525 'order_line': [
2526 (0, 0, {'product_id': self.kit_1.id}),
2527 ],
2528 })
2529 order.action_confirm()
2530
2531 delivery = order.picking_ids
2532 for move in delivery.move_ids:
2533 move.quantity = move.product_qty
2534 delivery.button_validate()
2535 self.assertEqual(delivery.state, 'done')
2536
2537 return_wizard = self.env['stock.return.picking'].with_context(active_id=delivery.id, active_model='stock.picking').create({})
2538 for line in return_wizard.product_return_moves:
2539 line.quantity = line.move_quantity
2540 res = return_wizard.action_create_returns()
2541
2542 return_picking = self.env['stock.picking'].browse(res["res_id"])
2543 return_picking.location_dest_id = return_location
2544 for move in return_picking.move_ids:
2545 move.quantity = move.product_qty
2546 return_picking.button_validate()
2547 self.assertEqual(return_picking.state, 'done')
2548 self.assertEqual(order.order_line.qty_delivered, 0)
2549
2550 internal_picking = return_picking.move_ids.move_dest_ids.picking_id
2551 self.assertTrue(internal_picking)
2552
2553 for move in internal_picking.move_ids:
2554 move.quantity = move.product_qty
2555 internal_picking.button_validate()
2556 self.assertEqual(internal_picking.state, 'done')
2557 self.assertEqual(order.order_line.qty_delivered, 0)
2558
2559 def test_return_for_exchange_kit_product_component(self):
2560 """ Returning for exchange a kit's component should leave the original sale order line's
2561 qty_delivered with the correct value.
2562 """
2563 for comp in self.bom_kit_1.bom_line_ids.product_id:
2564 self.env['stock.quant']._update_available_quantity(comp, self.company_data['default_warehouse'].lot_stock_id, quantity=10)
2565
2566 comp_to_return = self.bom_kit_1.bom_line_ids.filtered(lambda bl: bl.product_qty == 1).product_id
2567 kit_product = self.kit_1
2568 sale_order = self.env['sale.order'].create({
2569 'partner_id': self.partner.id,
2570 'order_line': [Command.create({
2571 'product_id': kit_product.id,
2572 'product_uom_qty': 1.0,
2573 })],
2574 })
2575 sale_order.action_confirm()
2576 delivery = sale_order.picking_ids
2577 delivery.action_assign()
2578 delivery.button_validate()
2579 return_picking_form = Form(self.env['stock.return.picking'].with_context(active_id=delivery.id, active_model='stock.picking'))
2580 return_wizard = return_picking_form.save()
2581 return_wizard.product_return_moves.filtered(lambda prm: prm.product_id == comp_to_return).quantity = 1
2582 res = return_wizard.action_create_exchanges()
2583 return_picking = self.env['stock.picking'].browse(res['res_id'])
2584 return_picking.button_validate()
2585 exchange_picking = sale_order.picking_ids.filtered(lambda so: so.state != 'done')
2586 exchange_picking.button_validate()
2587 self.assertEqual(sale_order.order_line.qty_delivered, 1)
2588
2589 def test_bidirectional_so_mo_link_with_mtso(self):
2590 """Test the link from the Manufacturing Order to the Sale Order
2591 when using the MTSO (Make To Stock or Make To Order) procurement method."""
2592 # Set the MTO and Manufacture routes on the product
2593 route_manufacture = self.company_data['default_warehouse'].manufacture_pull_id.route_id
2594 route_mto = self.company_data['default_warehouse'].mto_pull_id.route_id
2595 self.product_a.route_ids = [Command.set([route_manufacture.id, route_mto.id])]
2596 # Set the procure method to 'mts_else_mto'
2597 route_mto.rule_ids.filtered(lambda r: r.location_dest_id.usage == 'production').procure_method = 'mts_else_mto'
2598 # Create and confirm a Sale Order
2599 sale_order = self.env['sale.order'].create({
2600 'partner_id': self.partner.id,
2601 'order_line': [Command.create({
2602 'product_id': self.product_a.id,
2603 'product_uom_qty': 1.0,
2604 })],
2605 })
2606 sale_order.action_confirm()
2607 # Check the link between the SO and the MO
2608 self.assertEqual(sale_order.mrp_production_count, 1)
2609 mo = sale_order.mrp_production_ids
2610 self.assertEqual(mo.sale_order_count, 1)
2611
2612 def test_so_with_kit_and_multiple_same_component(self):
2613 """Test that a Sale Order with a kit product containing multiple identical components
2614 can be confirmed, and that the picking is created correctly. Then verify that the
2615 Sale Order can be cancelled and re-confirmed, resulting in a new picking with moves
2616 properly linked to each BOM line. Finally, test returning a kit component for exchange."""
2617 # Create a kit product with two identical components by duplicating the first BOM line
2618 self.bom_kit_1.bom_line_ids = self.bom_kit_1.bom_line_ids[0]
2619 self.env['mrp.bom.line'].create([
2620 {'product_id': self.bom_kit_1.bom_line_ids[0].product_id.id, 'product_qty': 1.0, 'bom_id': self.bom_kit_1.id},
2621 ])
2622 # Create a Sale Order with the kit product
2623 so = self.env['sale.order'].create({
2624 'partner_id': self.partner.id,
2625 'order_line': [Command.create({
2626 'product_id': self.kit_1.id,
2627 'product_uom_qty': 1.0,
2628 })],
2629 })
2630 so.action_confirm()
2631
2632 # Check that the picking has 2 moves for the same component
2633 picking = so.picking_ids
2634 self.assertEqual(len(picking.move_ids), 2, "There should be 2 moves for the same component in the picking")
2635 self.assertEqual(picking.move_ids.product_id, self.component_a, "All moves should be for the same component")
2636 self.assertEqual(picking.move_ids.bom_line_id, self.bom_kit_1.bom_line_ids, "Each move should be linked to a BOM line of the kit")
2637
2638 # Cancel the Sale Order
2639 so._action_cancel()
2640 self.assertEqual(so.state, 'cancel', "The Sale Order should be cancelled")
2641 self.assertEqual(picking.state, 'cancel', "The picking should be cancelled when the Sale Order is cancelled")
2642
2643 # Set the Sale Order back to draft and confirm again
2644 so.action_draft()
2645 so.action_confirm()
2646
2647 # Check that a new picking is created with correct moves
2648 second_picking = so.picking_ids - picking
2649 self.assertEqual(len(second_picking.move_ids), 2, "The second picking should have 2 moves for the component")
2650 self.assertEqual(second_picking.move_ids.product_id, self.component_a, "All moves in the second picking should be for the same component")
2651 self.assertEqual(second_picking.move_ids.bom_line_id, self.bom_kit_1.bom_line_ids, "Each move in the second picking should be linked to a BOM line of the kit")
2652 # Returning for exchange a kit's component
2653 self.env['stock.quant']._update_available_quantity(self.component_a, self.company_data['default_warehouse'].lot_stock_id, quantity=10)
2654 second_picking.action_assign()
2655 second_picking.button_validate()
2656 return_picking_form = Form(self.env['stock.return.picking'].with_context(active_id=second_picking.id, active_model='stock.picking'))
2657 return_wizard = return_picking_form.save()
2658 return_wizard.product_return_moves.filtered(lambda prm: prm.product_id == self.component_a).quantity = 1
2659 res = return_wizard.action_create_exchanges()
2660 return_picking = self.env['stock.picking'].browse(res['res_id'])
2661 return_picking.button_validate()
2662 exchange_picking = so.picking_ids.filtered(lambda so: so.state == 'assigned')
2663 exchange_picking.button_validate()
2664 so.order_line._compute_qty_delivered()
2665 # In the case where the kit has multiple identical components, only the first BOM line
2666 # is linked to all moves (this is a known limitation).
2667 self.assertEqual(exchange_picking.move_ids.bom_line_id, self.bom_kit_1.bom_line_ids[0], "All moves in the exchange picking should be linked to the first BOM line.")
2668 self.assertEqual(exchange_picking.move_ids.quantity, 2)
2669
2670 def test_delivery_after_splitting_production(self):
2671 """
2672 Test that processing the different MOs of a split production correctly
2673 updates the picking SM's quantity.
2674 """
2675 # Set product up with MTO + Manufacture with (empty) BoM
2676 product = self._cls_create_product('Split Product', self.uom_unit, routes=[
2677 self.company_data['default_warehouse'].mto_pull_id.route_id,
2678 self.company_data['default_warehouse'].manufacture_pull_id.route_id,
2679 ])
2680 self.env['mrp.bom'].create({
2681 'product_tmpl_id': product.product_tmpl_id.id,
2682 'product_uom_id': self.env.ref('uom.product_uom_unit').id,
2683 })
2684
2685 sale_order = self.env['sale.order'].create({
2686 'partner_id': self.partner.id,
2687 'order_line': [Command.create({
2688 'name': f"2 of {self.product.name}",
2689 'product_id': product.id,
2690 'product_uom_qty': 2,
2691 })],
2692 })
2693 sale_order.action_confirm()
2694 sale_picking = sale_order.picking_ids
2695 self.assertTrue(sale_picking)
2696
2697 mo = self.env['mrp.production'].search([('product_id', '=', product.id)], limit=1)
2698 action = mo.action_split()
2699 wizard = Form(self.env[action['res_model']].with_context(action['context']))
2700 wizard.max_batch_size = 1
2701 wizard.save().action_split()
2702 self.assertEqual(len(mo.production_group_id.production_ids), 2)
2703
2704 mo.production_group_id.production_ids[0].button_mark_done()
2705 self.assertEqual(sale_picking.move_ids.quantity, 1)
2706 mo.production_group_id.production_ids[1].button_mark_done()
2707 self.assertEqual(sale_picking.move_ids.quantity, 2)
2708 sale_picking.button_validate()
2709 self.assertEqual(sale_order.order_line.qty_delivered, 2.0)