Debian Codegraph @

addons/sale_mrp/tests/test_multistep_manufacturing.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4 from odoo.tests import Form
5 from odoo.addons.mrp.tests.common import TestMrpCommon
6
7
8 class TestMultistepManufacturing(TestMrpCommon):
9
10 @classmethod
11 def setUpClass(cls):
12 super().setUpClass()
13
14 # Required for `uom_id ` to be visible in the view
15 cls.env.user.group_ids += cls.env.ref('uom.group_uom')
16 # Required for `manufacture_steps` to be visible in the view
17 cls.env.user.group_ids += cls.env.ref('stock.group_adv_location')
18 # Required for `product_id` to be visible in the view
19 cls._enable_variants()
20
21 cls.route_mto.active = True
22 cls.MrpProduction = cls.env['mrp.production']
23 # Create warehouse
24 warehouse_form = Form(cls.env['stock.warehouse'])
25 warehouse_form.name = 'Test'
26 warehouse_form.code = 'Test'
27 cls.warehouse = warehouse_form.save()
28 cls.warehouse.mto_pull_id.route_id.rule_ids.procure_method = "make_to_order"
29
30 cls.uom_unit = cls.env.ref('uom.product_uom_unit')
31
32 # Create manufactured product
33 product_form = Form(cls.env['product.product'])
34 product_form.name = 'Stick'
35 product_form.uom_id = cls.uom_unit
36 product_form.route_ids.clear()
37 product_form.route_ids.add(cls.warehouse.manufacture_pull_id.route_id)
38 product_form.route_ids.add(cls.warehouse.mto_pull_id.route_id)
39 cls.product_manu = product_form.save()
40
41 # Create raw product for manufactured product
42 product_form = Form(cls.env['product.product'])
43 product_form.name = 'Raw Stick'
44 product_form.uom_id = cls.uom_unit
45 cls.product_raw = product_form.save()
46
47 # Create bom for manufactured product
48 bom_product_form = Form(cls.env['mrp.bom'])
49 bom_product_form.product_tmpl_id = cls.product_manu.product_tmpl_id
50 bom_product_form.product_qty = 1.0
51 bom_product_form.type = 'normal'
52 with bom_product_form.bom_line_ids.new() as bom_line:
53 bom_line.product_id = cls.product_raw
54 bom_line.product_qty = 2.0
55 cls.bom_prod_manu = bom_product_form.save()
56
57 # Create sale order
58 sale_form = Form(cls.env['sale.order'])
59 sale_form.partner_id = cls.env['res.partner'].create({'name': 'My Test Partner'})
60 sale_form.picking_policy = 'direct'
61 sale_form.warehouse_id = cls.warehouse
62 with sale_form.order_line.new() as line:
63 line.name = cls.product_manu.name
64 line.product_id = cls.product_manu
65 line.product_uom_qty = 1.0
66 line.price_unit = 10.0
67 cls.sale_order = sale_form.save()
68
69 def test_00_manufacturing_step_one(self):
70 """ Testing for Step-1 """
71 # Change steps of manufacturing.
72 with Form(self.warehouse) as warehouse:
73 warehouse.manufacture_steps = 'mrp_one_step'
74 # Confirm sale order.
75 self.sale_order.action_confirm()
76 # Check all procurements for created sale order
77 mo_procurement = self.MrpProduction.search([('origin', '=', self.sale_order.name)])
78 # Get manufactured procurement
79 self.assertEqual(mo_procurement.location_src_id.id, self.warehouse.lot_stock_id.id, "Source loction does not match.")
80 self.assertEqual(mo_procurement.location_dest_id.id, self.warehouse.lot_stock_id.id, "Destination location does not match.")
81 self.assertEqual(len(mo_procurement), 1, "No Procurement !")
82
83 def test_01_manufacturing_step_two(self):
84 """ Testing for Step-2 """
85 with Form(self.warehouse) as warehouse:
86 warehouse.manufacture_steps = 'pbm'
87 self.sale_order.action_confirm()
88 # Get manufactured procurement
89 mo_procurement = self.MrpProduction.search([('origin', '=', self.sale_order.name)])
90 mo = self.env['mrp.production'].search([
91 ('origin', '=', self.sale_order.name),
92 ('product_id', '=', self.product_manu.id),
93 ])
94 self.assertEqual(self.sale_order.action_view_mrp_production()['res_id'], mo.id)
95 self.assertEqual(mo_procurement.location_src_id.id, self.warehouse.pbm_loc_id.id, "Source loction does not match.")
96 self.assertEqual(mo_procurement.location_dest_id.id, self.warehouse.lot_stock_id.id, "Destination location does not match.")
97
98 self.assertEqual(len(mo_procurement), 1, "No Procurement !")
99
100 def test_cancel_multilevel_manufacturing(self):
101 """ Testing for multilevel Manufacturing orders.
102 When user creates multi-level manufacturing orders,
103 and then cancelles child manufacturing order,
104 an activity should be generated on parent MO, to notify user that
105 demands from child MO has been cancelled.
106 """
107
108 product_form = Form(self.env['product.product'])
109 product_form.name = 'Screw'
110 self.product_screw = product_form.save()
111
112 # Add routes for manufacturing and make to order to the raw material product
113 with Form(self.product_raw) as p1:
114 p1.route_ids.clear()
115 p1.route_ids.add(self.warehouse_1.manufacture_pull_id.route_id)
116 p1.route_ids.add(self.warehouse_1.mto_pull_id.route_id)
117
118 # New BoM for raw material product, it will generate another Production order i.e. child Production order
119 bom_product_form = Form(self.env['mrp.bom'])
120 bom_product_form.product_tmpl_id = self.product_raw.product_tmpl_id
121 bom_product_form.product_qty = 1.0
122 with bom_product_form.bom_line_ids.new() as bom_line:
123 bom_line.product_id = self.product_screw
124 bom_line.product_qty = 5.0
125 self.bom_prod_manu = bom_product_form.save()
126
127 # create MO from sale order.
128 self.sale_order.action_confirm()
129 # Find child MO.
130 child_manufaturing = self.env['mrp.production'].search([('product_id', '=', self.product_raw.id)])
131 self.assertTrue((len(child_manufaturing.ids) == 1), 'Manufacturing order of raw material must be generated.')
132 # Cancel child MO.
133 child_manufaturing.action_cancel()
134 manufaturing_from_so = self.env['mrp.production'].search([('product_id', '=', self.product_manu.id)])
135 # Check if activity is generated or not on parent MO.
136 exception = self.env['mail.activity'].search([('res_model', '=', 'mrp.production'),
137 ('res_id', '=', manufaturing_from_so.id)])
138 self.assertEqual(len(exception.ids), 1, 'When user cancelled child manufacturing, exception must be generated on parent manufacturing.')
139
140 def test_manufacturing_step_three(self):
141 """ Testing for Step-3 """
142 with Form(self.warehouse) as warehouse:
143 warehouse.manufacture_steps = 'pbm_sam'
144 self.sale_order.action_confirm()
145
146 mo = self.env['mrp.production'].search([
147 ('origin', '=', self.sale_order.name),
148 ('product_id', '=', self.product_manu.id),
149 ])
150
151 self.assertEqual(self.sale_order.mrp_production_count, 1)
152 self.assertEqual(mo.sale_order_count, 1)
153
154 self.assertEqual(self.sale_order.action_view_mrp_production()['res_id'], mo.id)
155 self.assertEqual(mo.action_view_sale_orders()['res_id'], self.sale_order.id)
156
157 def test_sales_order_with_mto_manufacturing(self):
158 self.route_mto.active = True
159 warehouse = self.warehouse_1
160 warehouse.manufacture_steps = 'pbm_sam'
161 prod1 = self.env['product.product'].create({
162 'name': 'elct1',
163 'type': 'consu',
164 'route_ids': [(6, 0, [
165 warehouse.manufacture_pull_id.route_id.id,
166 warehouse.mto_pull_id.route_id.id
167 ])],
168 })
169 prod2 = self.env['product.product'].create({
170 'name': 'elct2',
171 'type': 'consu',
172 'route_ids': [(6, 0, [
173 warehouse.manufacture_pull_id.route_id.id,
174 warehouse.mto_pull_id.route_id.id
175 ])],
176 })
177 partner = self.env['res.partner'].create({'name': 'Steve Buscemi'})
178 so = self.env['sale.order'].create({
179 'partner_id': partner.id,
180 'order_line': [(0, 0, {'product_id': prod1.id, 'product_uom_qty': 1}),
181 (0, 0, {'product_id': prod2.id, 'product_uom_qty': 1})],
182 'client_order_ref': 'Test Reference'
183 })
184 so.action_confirm()
185
186 def test_mto_cancel_3_steps_mo(self):
187 '''
188 In 3 step manufacturing, test that when the MO gets cancelled, the
189 delivery (to the client) can be made from stock.
190 '''
191 self.warehouse.manufacture_steps = 'pbm_sam'
192 self.sale_order.order_line.product_id.is_storable = True
193 self.env['stock.quant']._update_available_quantity(
194 self.sale_order.order_line.product_id,
195 self.sale_order.warehouse_id.lot_stock_id,
196 10
197 )
198 self.sale_order.action_confirm()
199 self.assertEqual(self.sale_order.picking_ids.state, 'waiting')
200 self.assertEqual(self.sale_order.picking_ids.move_ids.procure_method, 'make_to_order')
201 mo = self.sale_order.mrp_production_ids
202 self.assertTrue(mo)
203 self.assertEqual(self.sale_order.picking_ids.move_ids.move_orig_ids, mo.move_finished_ids)
204 mo.action_cancel()
205 self.assertEqual(self.sale_order.picking_ids.state, 'confirmed')
206 self.assertFalse(self.sale_order.picking_ids.move_ids.move_orig_ids)
207 self.sale_order.picking_ids.action_assign()
208 self.assertEqual(self.sale_order.picking_ids.move_ids.quantity, 1.0)