Debian Codegraph @

addons/sale_mrp/tests/test_sale_mrp_procurement.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4 import time
5
6 from odoo.tests import Form, TransactionCase
7 from odoo.tools import mute_logger
8 from odoo import Command
9
10
11 class TestSaleMrpProcurement(TransactionCase):
12
13 @classmethod
14 def setUpClass(cls):
15 super().setUpClass()
16 cls.env.ref('base.group_user').write({'implied_ids': [(4, cls.env.ref('product.group_product_variant').id)]})
17
18 def test_sale_mrp(self):
19 # Required for `uom_id` to be visible in the view
20 self.env.user.group_ids += self.env.ref('uom.group_uom')
21 self.env.ref('stock.route_warehouse0_mto').active = True
22 warehouse0 = self.env.ref('stock.warehouse0')
23 # In order to test the sale_mrp module in OpenERP, I start by creating a new product 'Slider Mobile'
24 # I define product category Mobile Products Sellable.
25
26 with mute_logger('odoo.tests.common.onchange'):
27 # Suppress warning on "Changing your cost method" when creating a
28 # product category
29 pc = Form(self.env['product.category'])
30 pc.name = 'Mobile Products Sellable'
31 product_category_allproductssellable0 = pc.save()
32
33 uom_unit = self.env.ref('uom.product_uom_unit')
34
35 self.assertIn("seller_ids", self.env['product.template'].fields_get())
36
37 # I define product for Slider Mobile.
38 product = Form(self.env['product.template'])
39
40 product.categ_id = product_category_allproductssellable0
41 product.list_price = 200.0
42 product.name = 'Slider Mobile'
43 product.is_storable = True
44 product.uom_id = uom_unit
45 product.route_ids.clear()
46 product.route_ids.add(warehouse0.manufacture_pull_id.route_id)
47 product.route_ids.add(warehouse0.mto_pull_id.route_id)
48 product_template_slidermobile0 = product.save()
49
50 product_template_slidermobile0.standard_price = 189
51
52 product_component = Form(self.env['product.product'])
53 product_component.name = 'Battery'
54 product_product_bettery = product_component.save()
55
56 with Form(self.env['mrp.bom']) as bom:
57 bom.product_tmpl_id = product_template_slidermobile0
58 with bom.bom_line_ids.new() as line:
59 line.product_id = product_product_bettery
60 line.product_qty = 4
61
62 # I create a sale order for product Slider mobile
63 so_form = Form(self.env['sale.order'])
64 so_form.partner_id = self.env['res.partner'].create({'name': 'Another Test Partner'})
65 with so_form.order_line.new() as line:
66 line.product_id = product_template_slidermobile0.product_variant_ids
67 line.price_unit = 200
68 line.product_uom_qty = 500.0
69 line.customer_lead = 7.0
70 sale_order_so0 = so_form.save()
71
72 # I confirm the sale order
73 sale_order_so0.action_confirm()
74
75 # I verify that a manufacturing order has been generated, and that its name and reference are correct
76 mo = self.env['mrp.production'].search([('origin', 'like', sale_order_so0.name)], limit=1)
77 self.assertTrue(mo, 'Manufacturing order has not been generated')
78
79 # Check the mo is displayed on the so
80 self.assertEqual(mo.id, sale_order_so0.action_view_mrp_production()['res_id'])
81
82 def test_sale_mrp_pickings(self):
83 """ Test sale of multiple mrp products in MTO
84 to avoid generating multiple deliveries
85 to the customer location
86 """
87 # Required for `uom_id` to be visible in the view
88 self.env.user.group_ids += self.env.ref('uom.group_uom')
89 # Required for `manufacture_step` to be visible in the view
90 self.env.user.group_ids += self.env.ref('stock.group_adv_location')
91 self.env.ref('stock.route_warehouse0_mto').active = True
92 # Create warehouse
93 self.customer_location = self.env['ir.model.data']._xmlid_to_res_id('stock.stock_location_customers')
94 self.warehouse = self.env['stock.warehouse'].create({
95 'name': 'Test Warehouse',
96 'code': 'TWH'
97 })
98
99 self.uom_unit = self.env.ref('uom.product_uom_unit')
100
101 # Create raw product for manufactured product
102 product_form = Form(self.env['product.product'])
103 product_form.name = 'Raw Stick'
104 product_form.is_storable = True
105 product_form.uom_id = self.uom_unit
106 self.raw_product = product_form.save()
107
108 # Create manufactured product
109 product_form = Form(self.env['product.product'])
110 product_form.name = 'Stick'
111 product_form.uom_id = self.uom_unit
112 product_form.is_storable = True
113 product_form.route_ids.clear()
114 product_form.route_ids.add(self.warehouse.manufacture_pull_id.route_id)
115 product_form.route_ids.add(self.warehouse.mto_pull_id.route_id)
116 self.finished_product = product_form.save()
117
118 # Create manifactured product which uses another manifactured
119 product_form = Form(self.env['product.product'])
120 product_form.name = 'Arrow'
121 product_form.is_storable = True
122 product_form.route_ids.clear()
123 product_form.route_ids.add(self.warehouse.manufacture_pull_id.route_id)
124 product_form.route_ids.add(self.warehouse.mto_pull_id.route_id)
125 self.complex_product = product_form.save()
126
127 ## Create raw product for manufactured product
128 product_form = Form(self.env['product.product'])
129 product_form.name = 'Raw Iron'
130 product_form.is_storable = True
131 product_form.uom_id = self.uom_unit
132 self.raw_product_2 = product_form.save()
133
134 # Create bom for manufactured product
135 bom_product_form = Form(self.env['mrp.bom'])
136 bom_product_form.product_tmpl_id = self.finished_product.product_tmpl_id
137 bom_product_form.product_qty = 1.0
138 bom_product_form.type = 'normal'
139 with bom_product_form.bom_line_ids.new() as bom_line:
140 bom_line.product_id = self.raw_product
141 bom_line.product_qty = 2.0
142
143 self.bom = bom_product_form.save()
144
145 ## Create bom for manufactured product
146 bom_product_form = Form(self.env['mrp.bom'])
147 bom_product_form.product_tmpl_id = self.complex_product.product_tmpl_id
148 with bom_product_form.bom_line_ids.new() as line:
149 line.product_id = self.finished_product
150 line.product_qty = 1.0
151 with bom_product_form.bom_line_ids.new() as line:
152 line.product_id = self.raw_product_2
153 line.product_qty = 1.0
154
155 self.complex_bom = bom_product_form.save()
156
157 with Form(self.warehouse) as warehouse:
158 warehouse.manufacture_steps = 'pbm_sam'
159
160 so_form = Form(self.env['sale.order'])
161 so_form.partner_id = self.env['res.partner'].create({'name': 'Another Test Partner'})
162 with so_form.order_line.new() as line:
163 line.product_id = self.complex_product
164 line.price_unit = 1
165 line.product_uom_qty = 1
166 with so_form.order_line.new() as line:
167 line.product_id = self.finished_product
168 line.price_unit = 1
169 line.product_uom_qty = 1
170 sale_order_so0 = so_form.save()
171
172 sale_order_so0.action_confirm()
173
174 # Verify buttons are working as expected
175 self.assertEqual(sale_order_so0.mrp_production_count, 2, "2 Mos for the 2 sale order line")
176 self.assertEqual(sale_order_so0.mrp_production_ids[0].product_qty, 1)
177 self.assertEqual(sale_order_so0.mrp_production_ids[1].product_qty, 2)
178
179 pickings = sale_order_so0.picking_ids
180
181 # One delivery...
182 self.assertEqual(len(pickings), 1)
183
184 # ...with two products
185 self.assertEqual(len(pickings[0].move_ids), 2)
186
187 def test_post_prod_location_child_of_stock_location(self):
188 """
189 3-steps manufacturing, the post-prod location is a child of the stock
190 location. Have a manufactured product with the manufacture route and a
191 RR min=max=0. Confirm a SO with that product -> It should generate a MO
192 """
193 warehouse = self.env['stock.warehouse'].search([('company_id', '=', self.env.company.id)], limit=1)
194 manufacture_route = warehouse.manufacture_pull_id.route_id
195
196 warehouse.manufacture_steps = 'pbm_sam'
197 warehouse.sam_loc_id.location_id = warehouse.lot_stock_id
198
199 product, component = self.env['product.product'].create([{
200 'name': 'Finished',
201 'is_storable': True,
202 'route_ids': [(6, 0, manufacture_route.ids)],
203 }, {
204 'name': 'Component',
205 'type': 'consu',
206 }])
207
208 self.env['mrp.bom'].create({
209 'product_id': product.id,
210 'product_tmpl_id': product.product_tmpl_id.id,
211 'product_uom_id': product.uom_id.id,
212 'product_qty': 1.0,
213 'type': 'normal',
214 'bom_line_ids': [
215 (0, 0, {'product_id': component.id, 'product_qty': 1}),
216 ],
217 })
218
219 self.env['stock.warehouse.orderpoint'].create({
220 'name': product.name,
221 'location_id': warehouse.lot_stock_id.id,
222 'product_id': product.id,
223 'product_min_qty': 0,
224 'product_max_qty': 0,
225 'trigger': 'auto',
226 })
227
228 so = self.env['sale.order'].create({
229 'partner_id': self.env['res.partner'].create({'name': 'Super Partner'}).id,
230 'order_line': [
231 (0, 0, {
232 'name': product.name,
233 'product_id': product.id,
234 'product_uom_qty': 1.0,
235 'price_unit': 1,
236 })],
237 })
238 so.action_confirm()
239 self.assertEqual(so.state, 'sale')
240
241 mo = self.env['mrp.production'].search([('product_id', '=', product.id)], order='id desc', limit=1)
242 self.assertIn(so.name, mo.origin)
243
244 def test_so_reordering_rule(self):
245 kit_1, component_1 = self.env['product.product'].create([{
246 'name': n,
247 'is_storable': True,
248 } for n in ['Kit 1', 'Compo 1']])
249
250 self.env['mrp.bom'].create([{
251 'product_tmpl_id': kit_1.product_tmpl_id.id,
252 'product_qty': 1,
253 'type': 'phantom',
254 'bom_line_ids': [
255 (0, 0, {'product_id': component_1.id, 'product_qty': 1}),
256 ],
257 }])
258 customer = self.env['res.partner'].create({
259 'name': 'customer',
260 })
261 so = self.env['sale.order'].create({
262 'partner_id': customer.id,
263 'order_line': [
264 (0, 0, {
265 'product_id': kit_1.id,
266 'product_uom_qty': 1.0,
267 })],
268 })
269 so.action_confirm()
270
271 self.env['stock.warehouse.orderpoint']._get_orderpoint_action()
272 orderpoint_product = self.env['stock.warehouse.orderpoint'].search(
273 [('product_id', '=', kit_1.id)])
274 self.assertFalse(orderpoint_product)
275
276 def test_so_reordering_rule_02(self):
277 """
278 Have a manufactured product in kg unit of measure with the manufacturing route,
279 the mto route and a BoM in grams.
280 Confirm a SO with that product in 510 grams -> It should generate a MO with 510g.
281 Create a second SO with 510g -> It should update the MO to 1020g.
282 """
283 warehouse = self.env['stock.warehouse'].search([('company_id', '=', self.env.company.id)], limit=1)
284 manufacture_route = warehouse.manufacture_pull_id.route_id
285 mto_route = warehouse.mto_pull_id.route_id
286 mto_route.active = True
287
288 uom_kg = self.env.ref('uom.product_uom_kgm')
289 uom_gram = self.env.ref('uom.product_uom_gram')
290
291 product, component = self.env['product.product'].create([{
292 'name': 'Finished',
293 'is_storable': True,
294 'uom_id': uom_kg.id,
295 'route_ids': [
296 Command.link(manufacture_route.id),
297 Command.link(mto_route.id),
298 ],
299 }, {
300 'name': 'Component',
301 'type': 'consu',
302 }])
303
304 self.env['mrp.bom'].create({
305 'product_id': product.id,
306 'product_tmpl_id': product.product_tmpl_id.id,
307 'product_uom_id': uom_gram.id,
308 'product_qty': 1.0,
309 'type': 'normal',
310 'bom_line_ids': [
311 (0, 0, {'product_id': component.id, 'product_qty': 1}),
312 ],
313 })
314
315 so = self.env['sale.order'].create({
316 'partner_id': self.env['res.partner'].create({'name': 'Super Partner'}).id,
317 'order_line': [
318 (0, 0, {
319 'name': product.name,
320 'product_id': product.id,
321 'product_uom_qty': 510,
322 'product_uom_id': uom_gram.id,
323 'price_unit': 1,
324 })],
325 })
326 so.action_confirm()
327 self.assertEqual(so.state, 'sale')
328
329 mo = self.env['mrp.production'].search([('product_id', '=', product.id)], order='id desc', limit=1)
330 self.assertIn(so.name, mo.origin)
331 self.assertEqual(mo.product_uom_id, uom_gram)
332 self.assertEqual(mo.product_qty, 510)
333
334 so.order_line.product_uom_qty = 510 * 2
335 self.assertEqual(mo.product_uom_id, uom_gram)
336 self.assertEqual(mo.product_qty, 1020)
337
338 def test_sale_mrp_avoid_multiple_pickings(self):
339 """
340 Test sale of multiple products. Avoid multiple pickings being
341 generated when we are not in 3 steps manufacturing.
342 """
343
344 warehouse = self.env['stock.warehouse'].search([('company_id', '=', self.env.company.id)], limit=1)
345 warehouse.sam_loc_id = warehouse.lot_stock_id
346
347 so = self.env['sale.order'].create({
348 'partner_id': self.env['res.partner'].create({'name': 'My Partner'}).id,
349 'order_line': [
350 Command.create({
351 'name': 'sol_p1',
352 'product_id': self.env['product.product'].create({'name': 'p1'}).id,
353 'product_uom_qty': 1,
354 }),
355 Command.create({
356 'name': 'sol_p2',
357 'product_id': self.env['product.product'].create({'name': 'p2'}).id,
358 'product_uom_qty': 1,
359 }),
360 ],
361 })
362
363 so.action_confirm()
364 self.assertEqual(len(so.picking_ids), 1)
365 self.assertEqual(so.picking_ids.picking_type_id, warehouse.out_type_id)