test_procurement.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from datetime import timedelta
  4. from odoo import fields
  5. from odoo.tests import Form
  6. from odoo.addons.mrp.tests.common import TestMrpCommon
  7. from odoo.exceptions import UserError
  8. class TestProcurement(TestMrpCommon):
  9. def test_procurement(self):
  10. """This test case when create production order check procurement is create"""
  11. # Update BOM
  12. self.bom_3.bom_line_ids.filtered(lambda x: x.product_id == self.product_5).unlink()
  13. self.bom_1.bom_line_ids.filtered(lambda x: x.product_id == self.product_1).unlink()
  14. # Update route
  15. self.warehouse = self.env.ref('stock.warehouse0')
  16. self.warehouse.mto_pull_id.route_id.active = True
  17. route_manufacture = self.warehouse.manufacture_pull_id.route_id.id
  18. route_mto = self.warehouse.mto_pull_id.route_id.id
  19. self.product_4.write({'route_ids': [(6, 0, [route_manufacture, route_mto])]})
  20. # Create production order
  21. # -------------------------
  22. # Product6 Unit 24
  23. # Product4 8 Dozen
  24. # Product2 12 Unit
  25. # -----------------------
  26. production_form = Form(self.env['mrp.production'])
  27. production_form.product_id = self.product_6
  28. production_form.bom_id = self.bom_3
  29. production_form.product_qty = 24
  30. production_form.product_uom_id = self.product_6.uom_id
  31. production_product_6 = production_form.save()
  32. production_product_6.action_confirm()
  33. production_product_6.action_assign()
  34. # check production state is Confirmed
  35. self.assertEqual(production_product_6.state, 'confirmed')
  36. # Check procurement for product 4 created or not.
  37. # Check it created a purchase order
  38. move_raw_product4 = production_product_6.move_raw_ids.filtered(lambda x: x.product_id == self.product_4)
  39. produce_product_4 = self.env['mrp.production'].search([('product_id', '=', self.product_4.id),
  40. ('move_dest_ids', '=', move_raw_product4[0].id)])
  41. # produce product
  42. self.assertEqual(produce_product_4.reservation_state, 'confirmed', "Consume material not available")
  43. # Create production order
  44. # -------------------------
  45. # Product 4 96 Unit
  46. # Product2 48 Unit
  47. # ---------------------
  48. # Update Inventory
  49. self.env['stock.quant'].with_context(inventory_mode=True).create({
  50. 'product_id': self.product_2.id,
  51. 'inventory_quantity': 48,
  52. 'location_id': self.warehouse.lot_stock_id.id,
  53. }).action_apply_inventory()
  54. produce_product_4.action_assign()
  55. self.assertEqual(produce_product_4.product_qty, 96, "Wrong quantity of finish product.")
  56. self.assertEqual(produce_product_4.product_uom_id, self.uom_unit, "Wrong quantity of finish product.")
  57. self.assertEqual(produce_product_4.reservation_state, 'assigned', "Consume material not available")
  58. # produce product4
  59. # ---------------
  60. mo_form = Form(produce_product_4)
  61. mo_form.qty_producing = produce_product_4.product_qty
  62. produce_product_4 = mo_form.save()
  63. # Check procurement and Production state for product 4.
  64. produce_product_4.button_mark_done()
  65. self.assertEqual(produce_product_4.state, 'done', 'Production order should be in state done')
  66. # Produce product 6
  67. # ------------------
  68. # Update Inventory
  69. self.env['stock.quant'].with_context(inventory_mode=True).create({
  70. 'product_id': self.product_2.id,
  71. 'inventory_quantity': 12,
  72. 'location_id': self.warehouse.lot_stock_id.id,
  73. }).action_apply_inventory()
  74. production_product_6.action_assign()
  75. # ------------------------------------
  76. self.assertEqual(production_product_6.reservation_state, 'assigned', "Consume material not available")
  77. mo_form = Form(production_product_6)
  78. mo_form.qty_producing = production_product_6.product_qty
  79. production_product_6 = mo_form.save()
  80. # Check procurement and Production state for product 6.
  81. production_product_6.button_mark_done()
  82. self.assertEqual(production_product_6.state, 'done', 'Production order should be in state done')
  83. self.assertEqual(self.product_6.qty_available, 24, 'Wrong quantity available of finished product.')
  84. def test_procurement_2(self):
  85. """Check that a manufacturing order create the right procurements when the route are set on
  86. a parent category of a product"""
  87. # find a child category id
  88. all_categ_id = self.env['product.category'].search([('parent_id', '=', None)], limit=1)
  89. child_categ_id = self.env['product.category'].search([('parent_id', '=', all_categ_id.id)], limit=1)
  90. # set the product of `self.bom_1` to this child category
  91. for bom_line_id in self.bom_1.bom_line_ids:
  92. # check that no routes are defined on the product
  93. self.assertEqual(len(bom_line_id.product_id.route_ids), 0)
  94. # set the category of the product to a child category
  95. bom_line_id.product_id.categ_id = child_categ_id
  96. # set the MTO route to the parent category (all)
  97. self.warehouse = self.env.ref('stock.warehouse0')
  98. mto_route = self.warehouse.mto_pull_id.route_id
  99. mto_route.active = True
  100. mto_route.product_categ_selectable = True
  101. all_categ_id.write({'route_ids': [(6, 0, [mto_route.id])]})
  102. # create MO, but check it raises error as components are in make to order and not everyone has
  103. with self.assertRaises(UserError):
  104. production_form = Form(self.env['mrp.production'])
  105. production_form.product_id = self.product_4
  106. production_form.product_uom_id = self.product_4.uom_id
  107. production_form.product_qty = 1
  108. production_product_4 = production_form.save()
  109. production_product_4.action_confirm()
  110. def test_procurement_3(self):
  111. warehouse = self.env['stock.warehouse'].search([], limit=1)
  112. warehouse.write({'reception_steps': 'three_steps'})
  113. warehouse.mto_pull_id.route_id.active = True
  114. self.env['stock.location']._parent_store_compute()
  115. warehouse.reception_route_id.rule_ids.filtered(
  116. lambda p: p.location_src_id == warehouse.wh_input_stock_loc_id and
  117. p.location_dest_id == warehouse.wh_qc_stock_loc_id).write({
  118. 'procure_method': 'make_to_stock'
  119. })
  120. finished_product = self.env['product.product'].create({
  121. 'name': 'Finished Product',
  122. 'type': 'product',
  123. })
  124. component = self.env['product.product'].create({
  125. 'name': 'Component',
  126. 'type': 'product',
  127. 'route_ids': [(4, warehouse.mto_pull_id.route_id.id)]
  128. })
  129. self.env['stock.quant']._update_available_quantity(component, warehouse.wh_input_stock_loc_id, 100)
  130. bom = self.env['mrp.bom'].create({
  131. 'product_id': finished_product.id,
  132. 'product_tmpl_id': finished_product.product_tmpl_id.id,
  133. 'product_uom_id': self.uom_unit.id,
  134. 'product_qty': 1.0,
  135. 'type': 'normal',
  136. 'bom_line_ids': [
  137. (0, 0, {'product_id': component.id, 'product_qty': 1.0})
  138. ]})
  139. mo_form = Form(self.env['mrp.production'])
  140. mo_form.product_id = finished_product
  141. mo_form.bom_id = bom
  142. mo_form.product_qty = 5
  143. mo_form.product_uom_id = finished_product.uom_id
  144. mo_form.location_src_id = warehouse.lot_stock_id
  145. mo = mo_form.save()
  146. mo.action_confirm()
  147. pickings = self.env['stock.picking'].search([('product_id', '=', component.id)])
  148. self.assertEqual(len(pickings), 2.0)
  149. picking_input_to_qc = pickings.filtered(lambda p: p.location_id == warehouse.wh_input_stock_loc_id)
  150. picking_qc_to_stock = pickings - picking_input_to_qc
  151. self.assertTrue(picking_input_to_qc)
  152. self.assertTrue(picking_qc_to_stock)
  153. picking_input_to_qc.action_assign()
  154. self.assertEqual(picking_input_to_qc.state, 'assigned')
  155. picking_input_to_qc.move_line_ids.write({'qty_done': 5.0})
  156. picking_input_to_qc._action_done()
  157. picking_qc_to_stock.action_assign()
  158. self.assertEqual(picking_qc_to_stock.state, 'assigned')
  159. picking_qc_to_stock.move_line_ids.write({'qty_done': 3.0})
  160. picking_qc_to_stock.with_context(skip_backorder=True, picking_ids_not_to_backorder=picking_qc_to_stock.ids).button_validate()
  161. self.assertEqual(picking_qc_to_stock.state, 'done')
  162. mo.action_assign()
  163. self.assertEqual(mo.move_raw_ids.reserved_availability, 3.0)
  164. produce_form = Form(mo)
  165. produce_form.qty_producing = 3.0
  166. mo = produce_form.save()
  167. self.assertEqual(mo.move_raw_ids.quantity_done, 3.0)
  168. picking_qc_to_stock.move_line_ids.qty_done = 5.0
  169. self.assertEqual(mo.move_raw_ids.reserved_availability, 5.0)
  170. self.assertEqual(mo.move_raw_ids.quantity_done, 3.0)
  171. def test_link_date_mo_moves(self):
  172. """ Check link of shedule date for manufaturing with date stock move."""
  173. # create a product with manufacture route
  174. product_1 = self.env['product.product'].create({
  175. 'name': 'AAA',
  176. 'route_ids': [(4, self.ref('mrp.route_warehouse0_manufacture'))]
  177. })
  178. component_1 = self.env['product.product'].create({
  179. 'name': 'component',
  180. })
  181. self.env['mrp.bom'].create({
  182. 'product_id': product_1.id,
  183. 'product_tmpl_id': product_1.product_tmpl_id.id,
  184. 'product_uom_id': self.uom_unit.id,
  185. 'product_qty': 1.0,
  186. 'type': 'normal',
  187. 'bom_line_ids': [
  188. (0, 0, {'product_id': component_1.id, 'product_qty': 1}),
  189. ]})
  190. # create a move for product_1 from stock to output and reserve to trigger the
  191. # rule
  192. move_dest = self.env['stock.move'].create({
  193. 'name': 'move_orig',
  194. 'product_id': product_1.id,
  195. 'product_uom': self.ref('uom.product_uom_unit'),
  196. 'location_id': self.ref('stock.stock_location_stock'),
  197. 'location_dest_id': self.ref('stock.stock_location_output'),
  198. 'product_uom_qty': 10,
  199. 'procure_method': 'make_to_order'
  200. })
  201. move_dest._action_confirm()
  202. mo = self.env['mrp.production'].search([
  203. ('product_id', '=', product_1.id),
  204. ('state', '=', 'confirmed')
  205. ])
  206. self.assertAlmostEqual(mo.move_finished_ids.date, mo.move_raw_ids.date + timedelta(hours=1), delta=timedelta(seconds=1))
  207. self.assertEqual(len(mo), 1, 'the manufacture order is not created')
  208. mo_form = Form(mo)
  209. self.assertEqual(mo_form.product_qty, 10, 'the quantity to produce is not good relative to the move')
  210. mo = mo_form.save()
  211. # Confirming mo create finished move
  212. move_orig = self.env['stock.move'].search([
  213. ('move_dest_ids', 'in', move_dest.ids)
  214. ], limit=1)
  215. self.assertEqual(len(move_orig), 1, 'the move orig is not created')
  216. self.assertEqual(move_orig.product_qty, 10, 'the quantity to produce is not good relative to the move')
  217. new_sheduled_date = fields.Datetime.to_datetime(mo.date_planned_start) + timedelta(days=5)
  218. mo.date_planned_start = new_sheduled_date
  219. self.assertAlmostEqual(mo.move_raw_ids.date, mo.date_planned_start, delta=timedelta(seconds=1))
  220. self.assertAlmostEqual(mo.move_finished_ids.date, mo.date_planned_finished, delta=timedelta(seconds=1))
  221. def test_finished_move_cancellation(self):
  222. """Check state of finished move on cancellation of raw moves. """
  223. product_bottle = self.env['product.product'].create({
  224. 'name': 'Plastic Bottle',
  225. 'route_ids': [(4, self.ref('mrp.route_warehouse0_manufacture'))]
  226. })
  227. component_mold = self.env['product.product'].create({
  228. 'name': 'Plastic Mold',
  229. })
  230. self.env['mrp.bom'].create({
  231. 'product_id': product_bottle.id,
  232. 'product_tmpl_id': product_bottle.product_tmpl_id.id,
  233. 'product_uom_id': self.uom_unit.id,
  234. 'product_qty': 1.0,
  235. 'type': 'normal',
  236. 'bom_line_ids': [
  237. (0, 0, {'product_id': component_mold.id, 'product_qty': 1}),
  238. ]})
  239. move_dest = self.env['stock.move'].create({
  240. 'name': 'move_bottle',
  241. 'product_id': product_bottle.id,
  242. 'product_uom': self.ref('uom.product_uom_unit'),
  243. 'location_id': self.ref('stock.stock_location_stock'),
  244. 'location_dest_id': self.ref('stock.stock_location_output'),
  245. 'product_uom_qty': 10,
  246. 'procure_method': 'make_to_order',
  247. })
  248. move_dest._action_confirm()
  249. mo = self.env['mrp.production'].search([
  250. ('product_id', '=', product_bottle.id),
  251. ('state', '=', 'confirmed')
  252. ])
  253. mo.move_raw_ids[0]._action_cancel()
  254. self.assertEqual(mo.state, 'cancel', 'Manufacturing order should be cancelled.')
  255. self.assertEqual(mo.move_finished_ids[0].state, 'cancel', 'Finished move should be cancelled if mo is cancelled.')
  256. self.assertEqual(mo.move_dest_ids[0].state, 'waiting', 'Destination move should not be cancelled if prapogation cancel is False on manufacturing rule.')
  257. def test_procurement_with_empty_bom(self):
  258. """Ensure that a procurement request using a product with an empty BoM
  259. will create an empty MO in draft state that can be completed afterwards.
  260. """
  261. self.warehouse = self.env.ref('stock.warehouse0')
  262. route_manufacture = self.warehouse.manufacture_pull_id.route_id.id
  263. route_mto = self.warehouse.mto_pull_id.route_id.id
  264. product = self.env['product.product'].create({
  265. 'name': 'Clafoutis',
  266. 'route_ids': [(6, 0, [route_manufacture, route_mto])]
  267. })
  268. self.env['mrp.bom'].create({
  269. 'product_id': product.id,
  270. 'product_tmpl_id': product.product_tmpl_id.id,
  271. 'product_uom_id': self.uom_unit.id,
  272. 'product_qty': 1.0,
  273. 'type': 'normal',
  274. })
  275. move_dest = self.env['stock.move'].create({
  276. 'name': 'Customer MTO Move',
  277. 'product_id': product.id,
  278. 'product_uom': self.ref('uom.product_uom_unit'),
  279. 'location_id': self.ref('stock.stock_location_stock'),
  280. 'location_dest_id': self.ref('stock.stock_location_output'),
  281. 'product_uom_qty': 10,
  282. 'procure_method': 'make_to_order',
  283. })
  284. move_dest._action_confirm()
  285. production = self.env['mrp.production'].search([('product_id', '=', product.id)])
  286. self.assertTrue(production)
  287. self.assertFalse(production.move_raw_ids)
  288. self.assertEqual(production.state, 'draft')
  289. comp1 = self.env['product.product'].create({
  290. 'name': 'egg',
  291. })
  292. move_values = production._get_move_raw_values(comp1, 40.0, self.env.ref('uom.product_uom_unit'))
  293. self.env['stock.move'].create(move_values)
  294. production.action_confirm()
  295. produce_form = Form(production)
  296. produce_form.qty_producing = production.product_qty
  297. production = produce_form.save()
  298. production.button_mark_done()
  299. move_dest._action_assign()
  300. self.assertEqual(move_dest.reserved_availability, 10.0)
  301. def test_auto_assign(self):
  302. """ When auto reordering rule exists, check for when:
  303. 1. There is not enough of a manufactured product to assign (reserve for) a picking => auto-create 1st MO
  304. 2. There is not enough of a manufactured component to assign the created MO => auto-create 2nd MO
  305. 3. Add an extra manufactured component (not in stock) to 1st MO => auto-create 3rd MO
  306. 4. When 2nd MO is completed => auto-assign to 1st MO
  307. 5. When 1st MO is completed => auto-assign to picking
  308. 6. Additionally check that a MO that has component in stock auto-reserves when MO is confirmed (since default setting = 'at_confirm')"""
  309. self.warehouse = self.env.ref('stock.warehouse0')
  310. route_manufacture = self.warehouse.manufacture_pull_id.route_id
  311. product_1 = self.env['product.product'].create({
  312. 'name': 'Cake',
  313. 'type': 'product',
  314. 'route_ids': [(6, 0, [route_manufacture.id])]
  315. })
  316. product_2 = self.env['product.product'].create({
  317. 'name': 'Cake Mix',
  318. 'type': 'product',
  319. 'route_ids': [(6, 0, [route_manufacture.id])]
  320. })
  321. product_3 = self.env['product.product'].create({
  322. 'name': 'Flour',
  323. 'type': 'consu',
  324. })
  325. bom1 = self.env['mrp.bom'].create({
  326. 'product_id': product_1.id,
  327. 'product_tmpl_id': product_1.product_tmpl_id.id,
  328. 'product_uom_id': self.uom_unit.id,
  329. 'product_qty': 1,
  330. 'consumption': 'flexible',
  331. 'type': 'normal',
  332. 'bom_line_ids': [
  333. (0, 0, {'product_id': product_2.id, 'product_qty': 1}),
  334. ]})
  335. self.env['mrp.bom'].create({
  336. 'product_id': product_2.id,
  337. 'product_tmpl_id': product_2.product_tmpl_id.id,
  338. 'product_uom_id': self.uom_unit.id,
  339. 'product_qty': 1,
  340. 'type': 'normal',
  341. 'bom_line_ids': [
  342. (0, 0, {'product_id': product_3.id, 'product_qty': 1}),
  343. ]})
  344. # extra manufactured component added to 1st MO after it is already confirmed
  345. product_4 = self.env['product.product'].create({
  346. 'name': 'Flavor Enchancer',
  347. 'type': 'product',
  348. 'route_ids': [(6, 0, [route_manufacture.id])]
  349. })
  350. product_5 = self.env['product.product'].create({
  351. 'name': 'MSG',
  352. 'type': 'consu',
  353. })
  354. self.env['mrp.bom'].create({
  355. 'product_id': product_4.id,
  356. 'product_tmpl_id': product_4.product_tmpl_id.id,
  357. 'product_uom_id': self.uom_unit.id,
  358. 'product_qty': 1,
  359. 'type': 'normal',
  360. 'bom_line_ids': [
  361. (0, 0, {'product_id': product_5.id, 'product_qty': 1}),
  362. ]})
  363. # setup auto orderpoints (reordering rules)
  364. self.env['stock.warehouse.orderpoint'].create({
  365. 'name': 'Cake RR',
  366. 'location_id': self.warehouse.lot_stock_id.id,
  367. 'product_id': product_1.id,
  368. 'product_min_qty': 0,
  369. 'product_max_qty': 5,
  370. })
  371. self.env['stock.warehouse.orderpoint'].create({
  372. 'name': 'Cake Mix RR',
  373. 'location_id': self.warehouse.lot_stock_id.id,
  374. 'product_id': product_2.id,
  375. 'product_min_qty': 0,
  376. 'product_max_qty': 5,
  377. })
  378. self.env['stock.warehouse.orderpoint'].create({
  379. 'name': 'Flavor Enchancer RR',
  380. 'location_id': self.warehouse.lot_stock_id.id,
  381. 'product_id': product_4.id,
  382. 'product_min_qty': 0,
  383. 'product_max_qty': 5,
  384. })
  385. # create picking output to trigger creating MO for reordering product_1
  386. pick_output = self.env['stock.picking'].create({
  387. 'name': 'Cake Delivery Order',
  388. 'picking_type_id': self.ref('stock.picking_type_out'),
  389. 'location_id': self.warehouse.lot_stock_id.id,
  390. 'location_dest_id': self.ref('stock.stock_location_customers'),
  391. 'move_ids': [(0, 0, {
  392. 'name': '/',
  393. 'product_id': product_1.id,
  394. 'product_uom': product_1.uom_id.id,
  395. 'product_uom_qty': 10.00,
  396. 'procure_method': 'make_to_stock',
  397. 'location_id': self.warehouse.lot_stock_id.id,
  398. 'location_dest_id': self.ref('stock.stock_location_customers'),
  399. })],
  400. })
  401. pick_output.action_confirm() # should trigger orderpoint to create and confirm 1st MO
  402. pick_output.action_assign()
  403. mo = self.env['mrp.production'].search([
  404. ('product_id', '=', product_1.id),
  405. ('state', '=', 'confirmed')
  406. ])
  407. self.assertEqual(len(mo), 1, "Manufacture order was not automatically created")
  408. mo.action_assign()
  409. mo.is_locked = False
  410. self.assertEqual(mo.move_raw_ids.reserved_availability, 0, "No components should be reserved yet")
  411. self.assertEqual(mo.product_qty, 15, "Quantity to produce should be picking demand + reordering rule max qty")
  412. # 2nd MO for product_2 should have been created and confirmed when 1st MO for product_1 was confirmed
  413. mo2 = self.env['mrp.production'].search([
  414. ('product_id', '=', product_2.id),
  415. ('state', '=', 'confirmed')
  416. ])
  417. self.assertEqual(len(mo2), 1, 'Second manufacture order was not created')
  418. self.assertEqual(mo2.product_qty, 20, "Quantity to produce should be MO's 'to consume' qty + reordering rule max qty")
  419. mo2_form = Form(mo2)
  420. mo2_form.qty_producing = 20
  421. mo2 = mo2_form.save()
  422. mo2.button_mark_done()
  423. self.assertEqual(mo.move_raw_ids.reserved_availability, 15, "Components should have been auto-reserved")
  424. # add new component to 1st MO
  425. mo_form = Form(mo)
  426. with mo_form.move_raw_ids.new() as line:
  427. line.product_id = product_4
  428. line.product_uom_qty = 1
  429. mo_form.save() # should trigger orderpoint to create and confirm 3rd MO
  430. mo3 = self.env['mrp.production'].search([
  431. ('product_id', '=', product_4.id),
  432. ('state', '=', 'confirmed')
  433. ])
  434. self.assertEqual(len(mo3), 1, 'Third manufacture order for added component was not created')
  435. self.assertEqual(mo3.product_qty, 6, "Quantity to produce should be 1 + reordering rule max qty")
  436. mo_form = Form(mo)
  437. mo.move_raw_ids.quantity_done = 15
  438. mo_form.qty_producing = 15
  439. mo = mo_form.save()
  440. mo.button_mark_done()
  441. self.assertEqual(pick_output.move_ids_without_package.reserved_availability, 10, "Completed products should have been auto-reserved in picking")
  442. # make sure next MO auto-reserves components now that they are in stock since
  443. # default reservation_method = 'at_confirm'
  444. mo_form = Form(self.env['mrp.production'])
  445. mo_form.product_id = product_1
  446. mo_form.bom_id = bom1
  447. mo_form.product_qty = 5
  448. mo_form.product_uom_id = product_1.uom_id
  449. mo_assign_at_confirm = mo_form.save()
  450. mo_assign_at_confirm.action_confirm()
  451. self.assertEqual(mo_assign_at_confirm.move_raw_ids.reserved_availability, 5, "Components should have been auto-reserved")
  452. def test_check_update_qty_mto_chain(self):
  453. """ Simulate a mto chain with a manufacturing order. Updating the
  454. initial demand should also impact the initial move but not the
  455. linked manufacturing order.
  456. """
  457. def create_run_procurement(product, product_qty, values=None):
  458. if not values:
  459. values = {
  460. 'warehouse_id': picking_type_out.warehouse_id,
  461. 'action': 'pull_push',
  462. 'group_id': procurement_group,
  463. }
  464. return self.env['procurement.group'].run([self.env['procurement.group'].Procurement(
  465. product, product_qty, self.uom_unit, vendor.property_stock_customer,
  466. product.name, '/', self.env.company, values)
  467. ])
  468. picking_type_out = self.env.ref('stock.picking_type_out')
  469. vendor = self.env['res.partner'].create({
  470. 'name': 'Roger'
  471. })
  472. # This needs to be tried with MTO route activated
  473. self.env['stock.route'].browse(self.ref('stock.route_warehouse0_mto')).action_unarchive()
  474. # Define products requested for this BoM.
  475. product = self.env['product.product'].create({
  476. 'name': 'product',
  477. 'type': 'product',
  478. 'route_ids': [(4, self.ref('stock.route_warehouse0_mto')), (4, self.ref('mrp.route_warehouse0_manufacture'))],
  479. 'categ_id': self.env.ref('product.product_category_all').id
  480. })
  481. component = self.env['product.product'].create({
  482. 'name': 'component',
  483. 'type': 'product',
  484. 'categ_id': self.env.ref('product.product_category_all').id
  485. })
  486. self.env['mrp.bom'].create({
  487. 'product_id': product.id,
  488. 'product_tmpl_id': product.product_tmpl_id.id,
  489. 'product_uom_id': product.uom_id.id,
  490. 'product_qty': 1.0,
  491. 'consumption': 'flexible',
  492. 'type': 'normal',
  493. 'bom_line_ids': [
  494. (0, 0, {'product_id': component.id, 'product_qty': 1}),
  495. ]
  496. })
  497. procurement_group = self.env['procurement.group'].create({
  498. 'move_type': 'direct',
  499. 'partner_id': vendor.id
  500. })
  501. # Create initial procurement that will generate the initial move and its picking.
  502. create_run_procurement(product, 10, {
  503. 'group_id': procurement_group,
  504. 'warehouse_id': picking_type_out.warehouse_id,
  505. 'partner_id': vendor
  506. })
  507. customer_move = self.env['stock.move'].search([('group_id', '=', procurement_group.id)])
  508. manufacturing_order = self.env['mrp.production'].search([('product_id', '=', product.id)])
  509. self.assertTrue(manufacturing_order, 'No manufacturing order created.')
  510. # Check manufacturing order data.
  511. self.assertEqual(manufacturing_order.product_qty, 10, 'The manufacturing order qty should be the same as the move.')
  512. # Create procurement to decrease quantity in the initial move but not in the related MO.
  513. create_run_procurement(product, -5.00)
  514. self.assertEqual(customer_move.product_uom_qty, 5, 'The demand on the initial move should have been decreased when merged with the procurement.')
  515. self.assertEqual(manufacturing_order.product_qty, 10, 'The demand on the manufacturing order should not have been decreased.')
  516. # Create procurement to increase quantity on the initial move and should create a new MO for the missing qty.
  517. create_run_procurement(product, 2.00)
  518. self.assertEqual(customer_move.product_uom_qty, 5, 'The demand on the initial move should not have been increased since it should be a new move.')
  519. self.assertEqual(manufacturing_order.product_qty, 10, 'The demand on the initial manufacturing order should not have been increased.')
  520. manufacturing_orders = self.env['mrp.production'].search([('product_id', '=', product.id)])
  521. self.assertEqual(len(manufacturing_orders), 2, 'A new MO should have been created for missing demand.')
  522. def test_rr_with_dependance_between_bom(self):
  523. self.warehouse = self.env.ref('stock.warehouse0')
  524. route_mto = self.warehouse.mto_pull_id.route_id
  525. route_mto.active = True
  526. route_manufacture = self.warehouse.manufacture_pull_id.route_id
  527. product_1 = self.env['product.product'].create({
  528. 'name': 'Product A',
  529. 'type': 'product',
  530. 'route_ids': [(6, 0, [route_manufacture.id])]
  531. })
  532. product_2 = self.env['product.product'].create({
  533. 'name': 'Product B',
  534. 'type': 'product',
  535. 'route_ids': [(6, 0, [route_manufacture.id, route_mto.id])]
  536. })
  537. product_3 = self.env['product.product'].create({
  538. 'name': 'Product B',
  539. 'type': 'product',
  540. 'route_ids': [(6, 0, [route_manufacture.id])]
  541. })
  542. product_4 = self.env['product.product'].create({
  543. 'name': 'Product C',
  544. 'type': 'consu',
  545. })
  546. op1 = self.env['stock.warehouse.orderpoint'].create({
  547. 'name': 'Product A',
  548. 'location_id': self.warehouse.lot_stock_id.id,
  549. 'product_id': product_1.id,
  550. 'product_min_qty': 1,
  551. 'product_max_qty': 20,
  552. })
  553. op2 = self.env['stock.warehouse.orderpoint'].create({
  554. 'name': 'Product B',
  555. 'location_id': self.warehouse.lot_stock_id.id,
  556. 'product_id': product_3.id,
  557. 'product_min_qty': 5,
  558. 'product_max_qty': 50,
  559. })
  560. self.env['mrp.bom'].create({
  561. 'product_id': product_1.id,
  562. 'product_tmpl_id': product_1.product_tmpl_id.id,
  563. 'product_uom_id': self.uom_unit.id,
  564. 'product_qty': 1,
  565. 'consumption': 'flexible',
  566. 'type': 'normal',
  567. 'bom_line_ids': [(0, 0, {'product_id': product_2.id, 'product_qty': 1})]
  568. })
  569. self.env['mrp.bom'].create({
  570. 'product_id': product_2.id,
  571. 'product_tmpl_id': product_2.product_tmpl_id.id,
  572. 'product_uom_id': self.uom_unit.id,
  573. 'product_qty': 1,
  574. 'consumption': 'flexible',
  575. 'type': 'normal',
  576. 'bom_line_ids': [(0, 0, {'product_id': product_3.id, 'product_qty': 1})]
  577. })
  578. self.env['mrp.bom'].create({
  579. 'product_id': product_3.id,
  580. 'product_tmpl_id': product_3.product_tmpl_id.id,
  581. 'product_uom_id': self.uom_unit.id,
  582. 'product_qty': 1,
  583. 'consumption': 'flexible',
  584. 'type': 'normal',
  585. 'bom_line_ids': [(0, 0, {'product_id': product_4.id, 'product_qty': 1})]
  586. })
  587. (op1 | op2)._procure_orderpoint_confirm()
  588. mo1 = self.env['mrp.production'].search([('product_id', '=', product_1.id)])
  589. mo3 = self.env['mrp.production'].search([('product_id', '=', product_3.id)])
  590. self.assertEqual(len(mo1), 1)
  591. self.assertEqual(len(mo3), 1)
  592. self.assertEqual(mo1.product_qty, 20)
  593. self.assertEqual(mo3.product_qty, 50)
  594. def test_several_boms_same_finished_product(self):
  595. """
  596. Suppose a product with two BoMs, each one based on a different operation type
  597. This test ensures that, when running the scheduler, the generated MOs are based
  598. on the correct BoMs
  599. """
  600. # Required for `picking_type_id` to be visible in the view
  601. self.env.user.groups_id += self.env.ref('stock.group_adv_location')
  602. warehouse = self.env.ref('stock.warehouse0')
  603. stock_location01 = warehouse.lot_stock_id
  604. stock_location02 = stock_location01.copy()
  605. manu_operation01 = warehouse.manu_type_id
  606. manu_operation02 = manu_operation01.copy()
  607. with Form(manu_operation02) as form:
  608. form.name = 'Manufacturing 02'
  609. form.sequence_code = 'MO2'
  610. form.default_location_dest_id = stock_location02
  611. manu_rule01 = warehouse.manufacture_pull_id
  612. manu_route = manu_rule01.route_id
  613. manu_rule02 = manu_rule01.copy()
  614. with Form(manu_rule02) as form:
  615. form.picking_type_id = manu_operation02
  616. manu_route.rule_ids = [(6, 0, (manu_rule01 + manu_rule02).ids)]
  617. compo01, compo02, finished = self.env['product.product'].create([{
  618. 'name': 'compo 01',
  619. 'type': 'consu',
  620. }, {
  621. 'name': 'compo 02',
  622. 'type': 'consu',
  623. }, {
  624. 'name': 'finished',
  625. 'type': 'product',
  626. 'route_ids': [(6, 0, manu_route.ids)],
  627. }])
  628. bom01_form = Form(self.env['mrp.bom'])
  629. bom01_form.product_tmpl_id = finished.product_tmpl_id
  630. bom01_form.code = '01'
  631. bom01_form.picking_type_id = manu_operation01
  632. with bom01_form.bom_line_ids.new() as line:
  633. line.product_id = compo01
  634. bom01 = bom01_form.save()
  635. bom02_form = Form(self.env['mrp.bom'])
  636. bom02_form.product_tmpl_id = finished.product_tmpl_id
  637. bom02_form.code = '02'
  638. bom02_form.picking_type_id = manu_operation02
  639. with bom02_form.bom_line_ids.new() as line:
  640. line.product_id = compo02
  641. bom02 = bom02_form.save()
  642. self.env['stock.warehouse.orderpoint'].create([{
  643. 'warehouse_id': warehouse.id,
  644. 'location_id': stock_location01.id,
  645. 'product_id': finished.id,
  646. 'product_min_qty': 1,
  647. 'product_max_qty': 1,
  648. }, {
  649. 'warehouse_id': warehouse.id,
  650. 'location_id': stock_location02.id,
  651. 'product_id': finished.id,
  652. 'product_min_qty': 2,
  653. 'product_max_qty': 2,
  654. }])
  655. self.env['procurement.group'].run_scheduler()
  656. mos = self.env['mrp.production'].search([('product_id', '=', finished.id)], order='origin')
  657. self.assertRecordValues(mos, [
  658. {'product_qty': 1, 'bom_id': bom01.id, 'picking_type_id': manu_operation01.id, 'location_dest_id': stock_location01.id},
  659. {'product_qty': 2, 'bom_id': bom02.id, 'picking_type_id': manu_operation02.id, 'location_dest_id': stock_location02.id},
  660. ])
  661. def test_pbm_and_additionnal_components(self):
  662. """
  663. 2-steps manufacturring.
  664. When adding a new component to a confirmed MO, it should add an SM in
  665. the PBM picking. Also, it should be possible to define the to-consume
  666. qty of the new line even if the MO is locked
  667. """
  668. warehouse = self.env['stock.warehouse'].search([('company_id', '=', self.env.company.id)], limit=1)
  669. warehouse.manufacture_steps = 'pbm'
  670. mo_form = Form(self.env['mrp.production'])
  671. mo_form.bom_id = self.bom_4
  672. mo = mo_form.save()
  673. mo.action_confirm()
  674. if not mo.is_locked:
  675. mo.action_toggle_is_locked()
  676. with Form(mo) as mo_form:
  677. with mo_form.move_raw_ids.new() as raw_line:
  678. raw_line.product_id = self.product_2
  679. raw_line.product_uom_qty = 2.0
  680. move_vals = mo._get_move_raw_values(self.product_3, 0, self.product_3.uom_id)
  681. mo.move_raw_ids = [(0, 0, move_vals)]
  682. mo.move_raw_ids[-1].product_uom_qty = 3.0
  683. expected_vals = [
  684. {'product_id': self.product_1.id, 'product_uom_qty': 1.0},
  685. {'product_id': self.product_2.id, 'product_uom_qty': 2.0},
  686. {'product_id': self.product_3.id, 'product_uom_qty': 3.0},
  687. ]
  688. self.assertRecordValues(mo.move_raw_ids, expected_vals)
  689. self.assertRecordValues(mo.picking_ids.move_ids, expected_vals)