test_backorder.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from datetime import datetime, timedelta
  4. from odoo.addons.mrp.tests.common import TestMrpCommon
  5. from odoo.tests import Form
  6. from odoo.tests.common import TransactionCase
  7. class TestMrpProductionBackorder(TestMrpCommon):
  8. @classmethod
  9. def setUpClass(cls):
  10. super().setUpClass()
  11. cls.stock_location = cls.env.ref('stock.stock_location_stock')
  12. warehouse_form = Form(cls.env['stock.warehouse'])
  13. warehouse_form.name = 'Test Warehouse'
  14. warehouse_form.code = 'TWH'
  15. cls.warehouse = warehouse_form.save()
  16. def test_no_tracking_1(self):
  17. """Create a MO for 4 product. Produce 4. The backorder button should
  18. not appear and hitting mark as done should not open the backorder wizard.
  19. The name of the MO should be MO/001.
  20. """
  21. mo = self.generate_mo(qty_final=4)[0]
  22. mo_form = Form(mo)
  23. mo_form.qty_producing = 4
  24. mo = mo_form.save()
  25. # No backorder is proposed
  26. self.assertTrue(mo.button_mark_done())
  27. self.assertEqual(mo._get_quantity_to_backorder(), 0)
  28. self.assertTrue("-001" not in mo.name)
  29. def test_no_tracking_2(self):
  30. """Create a MO for 4 product. Produce 1. The backorder button should
  31. appear and hitting mark as done should open the backorder wizard. In the backorder
  32. wizard, choose to do the backorder. A new MO for 3 self.untracked_bom should be
  33. created.
  34. The sequence of the first MO should be MO/001-01, the sequence of the second MO
  35. should be MO/001-02.
  36. Check that all MO are reachable through the procurement group.
  37. """
  38. production, _, _, product_to_use_1, _ = self.generate_mo(qty_final=4, qty_base_1=3)
  39. self.assertEqual(production.state, 'confirmed')
  40. self.assertEqual(production.reserve_visible, True)
  41. # Make some stock and reserve
  42. for product in production.move_raw_ids.product_id:
  43. self.env['stock.quant'].with_context(inventory_mode=True).create({
  44. 'product_id': product.id,
  45. 'inventory_quantity': 100,
  46. 'location_id': production.location_src_id.id,
  47. })._apply_inventory()
  48. production.action_assign()
  49. self.assertEqual(production.state, 'confirmed')
  50. self.assertEqual(production.reserve_visible, False)
  51. mo_form = Form(production)
  52. mo_form.qty_producing = 1
  53. production = mo_form.save()
  54. action = production.button_mark_done()
  55. backorder = Form(self.env['mrp.production.backorder'].with_context(**action['context']))
  56. backorder.save().action_backorder()
  57. # Two related MO to the procurement group
  58. self.assertEqual(len(production.procurement_group_id.mrp_production_ids), 2)
  59. # Check MO backorder
  60. mo_backorder = production.procurement_group_id.mrp_production_ids[-1]
  61. self.assertEqual(mo_backorder.product_id.id, production.product_id.id)
  62. self.assertEqual(mo_backorder.product_qty, 3)
  63. self.assertEqual(sum(mo_backorder.move_raw_ids.filtered(lambda m: m.product_id.id == product_to_use_1.id).mapped("product_uom_qty")), 9)
  64. self.assertEqual(mo_backorder.reserve_visible, False) # the reservation of the first MO should've been moved here
  65. def test_no_tracking_pbm_1(self):
  66. """Create a MO for 4 product. Produce 1. The backorder button should
  67. appear and hitting mark as done should open the backorder wizard. In the backorder
  68. wizard, choose to do the backorder. A new MO for 3 self.untracked_bom should be
  69. created.
  70. The sequence of the first MO should be MO/001-01, the sequence of the second MO
  71. should be MO/001-02.
  72. Check that all MO are reachable through the procurement group.
  73. """
  74. # Required for `manufacture_steps` to be visible in the view
  75. self.env.user.groups_id += self.env.ref("stock.group_adv_location")
  76. with Form(self.warehouse) as warehouse:
  77. warehouse.manufacture_steps = 'pbm'
  78. production, _, product_to_build, product_to_use_1, product_to_use_2 = self.generate_mo(qty_base_1=4, qty_final=4, picking_type_id=self.warehouse.manu_type_id)
  79. move_raw_ids = production.move_raw_ids
  80. self.assertEqual(len(move_raw_ids), 2)
  81. self.assertEqual(set(move_raw_ids.mapped("product_id")), {product_to_use_1, product_to_use_2})
  82. pbm_move = move_raw_ids.move_orig_ids
  83. self.assertEqual(len(pbm_move), 2)
  84. self.assertEqual(set(pbm_move.mapped("product_id")), {product_to_use_1, product_to_use_2})
  85. self.assertFalse(pbm_move.move_orig_ids)
  86. mo_form = Form(production)
  87. mo_form.qty_producing = 1
  88. production = mo_form.save()
  89. self.assertEqual(sum(pbm_move.filtered(lambda m: m.product_id.id == product_to_use_1.id).mapped("product_qty")), 16)
  90. self.assertEqual(sum(pbm_move.filtered(lambda m: m.product_id.id == product_to_use_2.id).mapped("product_qty")), 4)
  91. action = production.button_mark_done()
  92. backorder = Form(self.env['mrp.production.backorder'].with_context(**action['context']))
  93. backorder.save().action_backorder()
  94. mo_backorder = production.procurement_group_id.mrp_production_ids[-1]
  95. self.assertEqual(mo_backorder.delivery_count, 1)
  96. pbm_move |= mo_backorder.move_raw_ids.move_orig_ids
  97. # Check that quantity is correct
  98. self.assertEqual(sum(pbm_move.filtered(lambda m: m.product_id.id == product_to_use_1.id).mapped("product_qty")), 16)
  99. self.assertEqual(sum(pbm_move.filtered(lambda m: m.product_id.id == product_to_use_2.id).mapped("product_qty")), 4)
  100. self.assertFalse(pbm_move.move_orig_ids)
  101. def test_no_tracking_pbm_sam_1(self):
  102. """Create a MO for 4 product. Produce 1. The backorder button should
  103. appear and hitting mark as done should open the backorder wizard. In the backorder
  104. wizard, choose to do the backorder. A new MO for 3 self.untracked_bom should be
  105. created.
  106. The sequence of the first MO should be MO/001-01, the sequence of the second MO
  107. should be MO/001-02.
  108. Check that all MO are reachable through the procurement group.
  109. """
  110. # Required for `manufacture_steps` to be visible in the view
  111. self.env.user.groups_id += self.env.ref("stock.group_adv_location")
  112. with Form(self.warehouse) as warehouse:
  113. warehouse.manufacture_steps = 'pbm_sam'
  114. production, _, product_to_build, product_to_use_1, product_to_use_2 = self.generate_mo(qty_base_1=4, qty_final=4, picking_type_id=self.warehouse.manu_type_id)
  115. move_raw_ids = production.move_raw_ids
  116. self.assertEqual(len(move_raw_ids), 2)
  117. self.assertEqual(set(move_raw_ids.mapped("product_id")), {product_to_use_1, product_to_use_2})
  118. pbm_move = move_raw_ids.move_orig_ids
  119. self.assertEqual(len(pbm_move), 2)
  120. self.assertEqual(set(pbm_move.mapped("product_id")), {product_to_use_1, product_to_use_2})
  121. self.assertFalse(pbm_move.move_orig_ids)
  122. self.assertEqual(sum(pbm_move.filtered(lambda m: m.product_id.id == product_to_use_1.id).mapped("product_qty")), 16)
  123. self.assertEqual(sum(pbm_move.filtered(lambda m: m.product_id.id == product_to_use_2.id).mapped("product_qty")), 4)
  124. sam_move = production.move_finished_ids.move_dest_ids
  125. self.assertEqual(len(sam_move), 1)
  126. self.assertEqual(sam_move.product_id.id, product_to_build.id)
  127. self.assertEqual(sum(sam_move.mapped("product_qty")), 4)
  128. mo_form = Form(production)
  129. mo_form.qty_producing = 1
  130. production = mo_form.save()
  131. action = production.button_mark_done()
  132. backorder = Form(self.env['mrp.production.backorder'].with_context(**action['context']))
  133. backorder.save().action_backorder()
  134. mo_backorder = production.procurement_group_id.mrp_production_ids[-1]
  135. self.assertEqual(mo_backorder.delivery_count, 2)
  136. pbm_move |= mo_backorder.move_raw_ids.move_orig_ids
  137. self.assertEqual(sum(pbm_move.filtered(lambda m: m.product_id.id == product_to_use_1.id).mapped("product_qty")), 16)
  138. self.assertEqual(sum(pbm_move.filtered(lambda m: m.product_id.id == product_to_use_2.id).mapped("product_qty")), 4)
  139. sam_move |= mo_backorder.move_finished_ids.move_orig_ids
  140. self.assertEqual(sum(sam_move.mapped("product_qty")), 4)
  141. def test_tracking_backorder_series_lot_1(self):
  142. """ Create a MO of 4 tracked products. all component is tracked by lots
  143. Produce one by one with one bakorder for each until end.
  144. """
  145. nb_product_todo = 4
  146. production, _, p_final, p1, p2 = self.generate_mo(qty_final=nb_product_todo, tracking_final='lot', tracking_base_1='lot', tracking_base_2='lot')
  147. lot_final = self.env['stock.lot'].create({
  148. 'name': 'lot_final',
  149. 'product_id': p_final.id,
  150. 'company_id': self.env.company.id,
  151. })
  152. lot_1 = self.env['stock.lot'].create({
  153. 'name': 'lot_consumed_1',
  154. 'product_id': p1.id,
  155. 'company_id': self.env.company.id,
  156. })
  157. lot_2 = self.env['stock.lot'].create({
  158. 'name': 'lot_consumed_2',
  159. 'product_id': p2.id,
  160. 'company_id': self.env.company.id,
  161. })
  162. self.env['stock.quant']._update_available_quantity(p1, self.stock_location, nb_product_todo*4, lot_id=lot_1)
  163. self.env['stock.quant']._update_available_quantity(p2, self.stock_location, nb_product_todo, lot_id=lot_2)
  164. production.action_assign()
  165. active_production = production
  166. for i in range(nb_product_todo):
  167. details_operation_form = Form(active_production.move_raw_ids.filtered(lambda m: m.product_id == p1), view=self.env.ref('stock.view_stock_move_operations'))
  168. with details_operation_form.move_line_ids.edit(0) as ml:
  169. ml.qty_done = 4
  170. ml.lot_id = lot_1
  171. details_operation_form.save()
  172. details_operation_form = Form(active_production.move_raw_ids.filtered(lambda m: m.product_id == p2), view=self.env.ref('stock.view_stock_move_operations'))
  173. with details_operation_form.move_line_ids.edit(0) as ml:
  174. ml.qty_done = 1
  175. ml.lot_id = lot_2
  176. details_operation_form.save()
  177. production_form = Form(active_production)
  178. production_form.qty_producing = 1
  179. production_form.lot_producing_id = lot_final
  180. active_production = production_form.save()
  181. active_production.button_mark_done()
  182. if i + 1 != nb_product_todo: # If last MO, don't make a backorder
  183. action = active_production.button_mark_done()
  184. backorder = Form(self.env['mrp.production.backorder'].with_context(**action['context']))
  185. backorder.save().action_backorder()
  186. active_production = active_production.procurement_group_id.mrp_production_ids[-1]
  187. self.assertEqual(self.env['stock.quant']._get_available_quantity(p_final, self.stock_location, lot_id=lot_final), nb_product_todo, f'You should have the {nb_product_todo} final product in stock')
  188. self.assertEqual(len(production.procurement_group_id.mrp_production_ids), nb_product_todo)
  189. def test_tracking_backorder_series_lot_2(self):
  190. """
  191. Create a MO with component tracked by lots. Produce a part of the demand
  192. by using some specific lots (not the ones suggested by the onchange).
  193. The components' reservation of the backorder should consider which lots
  194. have been consumed in the initial MO
  195. """
  196. production, _, _, p1, p2 = self.generate_mo(tracking_base_2='lot')
  197. lot1, lot2 = self.env['stock.lot'].create([{
  198. 'name': f'lot_consumed_{i}',
  199. 'product_id': p2.id,
  200. 'company_id': self.env.company.id,
  201. } for i in range(2)])
  202. self.env['stock.quant']._update_available_quantity(p1, self.stock_location, 20)
  203. self.env['stock.quant']._update_available_quantity(p2, self.stock_location, 3, lot_id=lot1)
  204. self.env['stock.quant']._update_available_quantity(p2, self.stock_location, 2, lot_id=lot2)
  205. production.action_assign()
  206. production_form = Form(production)
  207. production_form.qty_producing = 3
  208. details_operation_form = Form(production.move_raw_ids.filtered(lambda m: m.product_id == p1), view=self.env.ref('stock.view_stock_move_operations'))
  209. with details_operation_form.move_line_ids.edit(0) as ml:
  210. ml.qty_done = 4 * 3
  211. details_operation_form.save()
  212. # Consume 1 Product from lot1 and 2 from lot 2
  213. p2_smls = production.move_raw_ids.filtered(lambda m: m.product_id == p2).move_line_ids
  214. self.assertEqual(len(p2_smls), 2, 'One for each lot')
  215. details_operation_form = Form(production.move_raw_ids.filtered(lambda m: m.product_id == p2), view=self.env.ref('stock.view_stock_move_operations'))
  216. with details_operation_form.move_line_ids.edit(0) as ml:
  217. ml.qty_done = 1
  218. ml.lot_id = lot1
  219. with details_operation_form.move_line_ids.edit(1) as ml:
  220. ml.qty_done = 2
  221. ml.lot_id = lot2
  222. details_operation_form.save()
  223. production = production_form.save()
  224. action = production.button_mark_done()
  225. backorder = Form(self.env['mrp.production.backorder'].with_context(**action['context']))
  226. backorder.save().action_backorder()
  227. p2_bo_mls = production.procurement_group_id.mrp_production_ids[-1].move_raw_ids.filtered(lambda m: m.product_id == p2).move_line_ids
  228. self.assertEqual(len(p2_bo_mls), 1)
  229. self.assertEqual(p2_bo_mls.lot_id, lot1)
  230. self.assertEqual(p2_bo_mls.reserved_qty, 2)
  231. def test_uom_backorder(self):
  232. """
  233. test backorder component UoM different from the bom's UoM
  234. """
  235. product_finished = self.env['product.product'].create({
  236. 'name': 'Young Tom',
  237. 'type': 'product',
  238. })
  239. product_component = self.env['product.product'].create({
  240. 'name': 'Botox',
  241. 'type': 'product',
  242. 'uom_id': self.env.ref('uom.product_uom_kgm').id,
  243. 'uom_po_id': self.env.ref('uom.product_uom_kgm').id,
  244. })
  245. mo_form = Form(self.env['mrp.production'])
  246. mo_form.product_id = product_finished
  247. mo_form.bom_id = self.env['mrp.bom'].create({
  248. 'product_id': product_finished.id,
  249. 'product_tmpl_id': product_finished.product_tmpl_id.id,
  250. 'product_uom_id': self.uom_unit.id,
  251. 'product_qty': 1.0,
  252. 'type': 'normal',
  253. 'consumption': 'flexible',
  254. 'bom_line_ids': [(0, 0, {
  255. 'product_id': product_component.id,
  256. 'product_qty': 1,
  257. 'product_uom_id':self.env.ref('uom.product_uom_gram').id,
  258. }),]
  259. })
  260. mo_form.product_qty = 1000
  261. mo = mo_form.save()
  262. mo.action_confirm()
  263. self.env['stock.quant']._update_available_quantity(product_component, self.stock_location, 1000)
  264. mo.action_assign()
  265. production_form = Form(mo)
  266. production_form.qty_producing = 300
  267. mo = production_form.save()
  268. action = mo.button_mark_done()
  269. backorder_form = Form(self.env['mrp.production.backorder'].with_context(**action['context']))
  270. backorder_form.save().action_backorder()
  271. # 300 Grams consumed and 700 reserved
  272. self.assertAlmostEqual(self.env['stock.quant']._gather(product_component, self.stock_location).reserved_quantity, 0.7)
  273. def test_tracking_backorder_series_serial_1(self):
  274. """ Create a MO of 4 tracked products (serial) with pbm_sam.
  275. all component is tracked by serial
  276. Produce one by one with one bakorder for each until end.
  277. """
  278. nb_product_todo = 4
  279. production, _, p_final, p1, p2 = self.generate_mo(qty_final=nb_product_todo, tracking_final='serial', tracking_base_1='serial', tracking_base_2='serial', qty_base_1=1)
  280. serials_final, serials_p1, serials_p2 = [], [], []
  281. for i in range(nb_product_todo):
  282. serials_final.append(self.env['stock.lot'].create({
  283. 'name': f'lot_final_{i}',
  284. 'product_id': p_final.id,
  285. 'company_id': self.env.company.id,
  286. }))
  287. serials_p1.append(self.env['stock.lot'].create({
  288. 'name': f'lot_consumed_1_{i}',
  289. 'product_id': p1.id,
  290. 'company_id': self.env.company.id,
  291. }))
  292. serials_p2.append(self.env['stock.lot'].create({
  293. 'name': f'lot_consumed_2_{i}',
  294. 'product_id': p2.id,
  295. 'company_id': self.env.company.id,
  296. }))
  297. self.env['stock.quant']._update_available_quantity(p1, self.stock_location, 1, lot_id=serials_p1[-1])
  298. self.env['stock.quant']._update_available_quantity(p2, self.stock_location, 1, lot_id=serials_p2[-1])
  299. production.action_assign()
  300. active_production = production
  301. for i in range(nb_product_todo):
  302. details_operation_form = Form(active_production.move_raw_ids.filtered(lambda m: m.product_id == p1), view=self.env.ref('stock.view_stock_move_operations'))
  303. with details_operation_form.move_line_ids.edit(0) as ml:
  304. ml.qty_done = 1
  305. ml.lot_id = serials_p1[i]
  306. details_operation_form.save()
  307. details_operation_form = Form(active_production.move_raw_ids.filtered(lambda m: m.product_id == p2), view=self.env.ref('stock.view_stock_move_operations'))
  308. with details_operation_form.move_line_ids.edit(0) as ml:
  309. ml.qty_done = 1
  310. ml.lot_id = serials_p2[i]
  311. details_operation_form.save()
  312. production_form = Form(active_production)
  313. production_form.qty_producing = 1
  314. production_form.lot_producing_id = serials_final[i]
  315. active_production = production_form.save()
  316. active_production.button_mark_done()
  317. if i + 1 != nb_product_todo: # If last MO, don't make a backorder
  318. action = active_production.button_mark_done()
  319. backorder = Form(self.env['mrp.production.backorder'].with_context(**action['context']))
  320. backorder.save().action_backorder()
  321. active_production = active_production.procurement_group_id.mrp_production_ids[-1]
  322. self.assertEqual(self.env['stock.quant']._get_available_quantity(p_final, self.stock_location), nb_product_todo, f'You should have the {nb_product_todo} final product in stock')
  323. self.assertEqual(len(production.procurement_group_id.mrp_production_ids), nb_product_todo)
  324. def test_tracking_backorder_immediate_production_serial_1(self):
  325. """ Create a MO to build 2 of a SN tracked product.
  326. Build both the starting MO and its backorder as immediate productions
  327. (i.e. Mark As Done without setting SN/filling any quantities)
  328. """
  329. mo, _, p_final, p1, p2 = self.generate_mo(qty_final=2, tracking_final='serial', qty_base_1=2, qty_base_2=2)
  330. self.env['stock.quant']._update_available_quantity(p1, self.stock_location_components, 2.0)
  331. self.env['stock.quant']._update_available_quantity(p2, self.stock_location_components, 2.0)
  332. mo.action_assign()
  333. res_dict = mo.button_mark_done()
  334. self.assertEqual(res_dict.get('res_model'), 'mrp.immediate.production')
  335. immediate_wizard = Form(self.env[res_dict['res_model']].with_context(res_dict['context'])).save()
  336. res_dict = immediate_wizard.process()
  337. self.assertEqual(res_dict.get('res_model'), 'mrp.production.backorder')
  338. backorder_wizard = Form(self.env[res_dict['res_model']].with_context(res_dict['context']))
  339. # backorder should automatically open
  340. action = backorder_wizard.save().action_backorder()
  341. self.assertEqual(action.get('res_model'), 'mrp.production')
  342. backorder_mo_form = Form(self.env[action['res_model']].with_context(action['context']).browse(action['res_id']))
  343. backorder_mo = backorder_mo_form.save()
  344. res_dict = backorder_mo.button_mark_done()
  345. self.assertEqual(res_dict.get('res_model'), 'mrp.immediate.production')
  346. immediate_wizard = Form(self.env[res_dict['res_model']].with_context(res_dict['context'])).save()
  347. immediate_wizard.process()
  348. self.assertEqual(self.env['stock.quant']._get_available_quantity(p_final, self.stock_location), 2, "Incorrect number of final product produced.")
  349. self.assertEqual(len(self.env['stock.lot'].search([('product_id', '=', p_final.id)])), 2, "Serial Numbers were not correctly produced.")
  350. def test_backorder_name(self):
  351. def produce_one(mo):
  352. mo_form = Form(mo)
  353. mo_form.qty_producing = 1
  354. mo = mo_form.save()
  355. action = mo.button_mark_done()
  356. backorder = Form(self.env['mrp.production.backorder'].with_context(**action['context']))
  357. backorder.save().action_backorder()
  358. return mo.procurement_group_id.mrp_production_ids[-1]
  359. default_picking_type_id = self.env['mrp.production']._get_default_picking_type_id(self.env.company.id)
  360. default_picking_type = self.env['stock.picking.type'].browse(default_picking_type_id)
  361. mo_sequence = default_picking_type.sequence_id
  362. mo_sequence.prefix = "WH-MO-"
  363. initial_mo_name = mo_sequence.prefix + str(mo_sequence.number_next_actual).zfill(mo_sequence.padding)
  364. production = self.generate_mo(qty_final=5)[0]
  365. self.assertEqual(production.name, initial_mo_name)
  366. backorder = produce_one(production)
  367. self.assertEqual(production.name, initial_mo_name + "-001")
  368. self.assertEqual(backorder.name, initial_mo_name + "-002")
  369. backorder.backorder_sequence = 998
  370. for seq in [998, 999, 1000]:
  371. new_backorder = produce_one(backorder)
  372. self.assertEqual(backorder.name, initial_mo_name + "-" + str(seq))
  373. self.assertEqual(new_backorder.name, initial_mo_name + "-" + str(seq + 1))
  374. backorder = new_backorder
  375. def test_backorder_name_without_procurement_group(self):
  376. production = self.generate_mo(qty_final=5)[0]
  377. mo_form = Form(production)
  378. mo_form.qty_producing = 1
  379. mo = mo_form.save()
  380. # Remove pg to trigger fallback on backorder name
  381. mo.procurement_group_id = False
  382. action = mo.button_mark_done()
  383. backorder_form = Form(self.env['mrp.production.backorder'].with_context(**action['context']))
  384. backorder_form.save().action_backorder()
  385. # The pg is back
  386. self.assertTrue(production.procurement_group_id)
  387. backorder_ids = production.procurement_group_id.mrp_production_ids[1]
  388. self.assertEqual(production.name.split('-')[0], backorder_ids.name.split('-')[0])
  389. self.assertEqual(int(production.name.split('-')[1]) + 1, int(backorder_ids.name.split('-')[1]))
  390. def test_split_merge(self):
  391. # Change 'Units' rounding to 1 (integer only quantities)
  392. self.uom_unit.rounding = 1
  393. # Create a mo for 10 products
  394. mo, _, _, p1, p2 = self.generate_mo(qty_final=10)
  395. # Split in 3 parts
  396. action = mo.action_split()
  397. wizard = Form(self.env[action['res_model']].with_context(action['context']))
  398. wizard.counter = 3
  399. action = wizard.save().action_split()
  400. # Should have 3 mos
  401. self.assertEqual(len(mo.procurement_group_id.mrp_production_ids), 3)
  402. mo1 = mo.procurement_group_id.mrp_production_ids[0]
  403. mo2 = mo.procurement_group_id.mrp_production_ids[1]
  404. mo3 = mo.procurement_group_id.mrp_production_ids[2]
  405. # Check quantities
  406. self.assertEqual(mo1.product_qty, 3)
  407. self.assertEqual(mo2.product_qty, 3)
  408. self.assertEqual(mo3.product_qty, 4)
  409. # Check raw movew quantities
  410. self.assertEqual(mo1.move_raw_ids.filtered(lambda m: m.product_id == p1).product_qty, 12)
  411. self.assertEqual(mo2.move_raw_ids.filtered(lambda m: m.product_id == p1).product_qty, 12)
  412. self.assertEqual(mo3.move_raw_ids.filtered(lambda m: m.product_id == p1).product_qty, 16)
  413. self.assertEqual(mo1.move_raw_ids.filtered(lambda m: m.product_id == p2).product_qty, 3)
  414. self.assertEqual(mo2.move_raw_ids.filtered(lambda m: m.product_id == p2).product_qty, 3)
  415. self.assertEqual(mo3.move_raw_ids.filtered(lambda m: m.product_id == p2).product_qty, 4)
  416. # Merge them back
  417. expected_origin = ",".join([mo1.name, mo2.name, mo3.name])
  418. action = (mo1 + mo2 + mo3).action_merge()
  419. mo = self.env[action['res_model']].browse(action['res_id'])
  420. # Check origin & initial quantity
  421. self.assertEqual(mo.origin, expected_origin)
  422. self.assertEqual(mo.product_qty, 10)
  423. def test_reservation_method_w_mo(self):
  424. """ Create a MO for 2 units, Produce 1 and create a backorder.
  425. The MO and the backorder should be assigned according to the reservation method
  426. defined in the default manufacturing operation type
  427. """
  428. def create_mo(date_planned_start=False):
  429. mo_form = Form(self.env['mrp.production'])
  430. mo_form.product_id = self.bom_1.product_id
  431. mo_form.bom_id = self.bom_1
  432. mo_form.product_qty = 2
  433. if date_planned_start:
  434. mo_form.date_planned_start = date_planned_start
  435. mo = mo_form.save()
  436. mo.action_confirm()
  437. return mo
  438. def produce_one(mo):
  439. mo_form = Form(mo)
  440. mo_form.qty_producing = 1
  441. mo = mo_form.save()
  442. action = mo.button_mark_done()
  443. backorder = Form(self.env['mrp.production.backorder'].with_context(**action['context']))
  444. backorder.save().action_backorder()
  445. return mo.procurement_group_id.mrp_production_ids[-1]
  446. # Make some stock and reserve
  447. for product in self.bom_1.bom_line_ids.product_id:
  448. product.type = 'product'
  449. self.env['stock.quant'].with_context(inventory_mode=True).create({
  450. 'product_id': product.id,
  451. 'inventory_quantity': 100,
  452. 'location_id': self.stock_location.id,
  453. })._apply_inventory()
  454. default_picking_type_id = self.env['mrp.production']._get_default_picking_type_id(self.env.company.id)
  455. default_picking_type = self.env['stock.picking.type'].browse(default_picking_type_id)
  456. # make sure generated MO will auto-assign
  457. default_picking_type.reservation_method = 'at_confirm'
  458. production = create_mo()
  459. self.assertEqual(production.state, 'confirmed')
  460. self.assertEqual(production.reserve_visible, False)
  461. # check whether the backorder follows the same scenario as the original MO
  462. backorder = produce_one(production)
  463. self.assertEqual(backorder.state, 'confirmed')
  464. self.assertEqual(backorder.reserve_visible, False)
  465. # make sure generated MO will does not auto-assign
  466. default_picking_type.reservation_method = 'manual'
  467. production = create_mo()
  468. self.assertEqual(production.state, 'confirmed')
  469. self.assertEqual(production.reserve_visible, True)
  470. backorder = produce_one(production)
  471. self.assertEqual(backorder.state, 'confirmed')
  472. self.assertEqual(backorder.reserve_visible, True)
  473. # make sure generated MO auto-assigns according to scheduled date
  474. default_picking_type.reservation_method = 'by_date'
  475. default_picking_type.reservation_days_before = 2
  476. # too early for scheduled date => don't auto-assign
  477. production = create_mo(datetime.now() + timedelta(days=10))
  478. self.assertEqual(production.state, 'confirmed')
  479. self.assertEqual(production.reserve_visible, True)
  480. backorder = produce_one(production)
  481. self.assertEqual(backorder.state, 'confirmed')
  482. self.assertEqual(backorder.reserve_visible, True)
  483. # within scheduled date + reservation days before => auto-assign
  484. production = create_mo()
  485. self.assertEqual(production.state, 'confirmed')
  486. self.assertEqual(production.reserve_visible, False)
  487. backorder = produce_one(production)
  488. self.assertEqual(backorder.state, 'confirmed')
  489. self.assertEqual(backorder.reserve_visible, False)
  490. class TestMrpWorkorderBackorder(TransactionCase):
  491. @classmethod
  492. def setUpClass(cls):
  493. super(TestMrpWorkorderBackorder, cls).setUpClass()
  494. cls.uom_unit = cls.env['uom.uom'].search([
  495. ('category_id', '=', cls.env.ref('uom.product_uom_categ_unit').id),
  496. ('uom_type', '=', 'reference')
  497. ], limit=1)
  498. cls.finished1 = cls.env['product.product'].create({
  499. 'name': 'finished1',
  500. 'type': 'product',
  501. })
  502. cls.compfinished1 = cls.env['product.product'].create({
  503. 'name': 'compfinished1',
  504. 'type': 'product',
  505. })
  506. cls.compfinished2 = cls.env['product.product'].create({
  507. 'name': 'compfinished2',
  508. 'type': 'product',
  509. })
  510. cls.workcenter1 = cls.env['mrp.workcenter'].create({
  511. 'name': 'workcenter1',
  512. })
  513. cls.workcenter2 = cls.env['mrp.workcenter'].create({
  514. 'name': 'workcenter2',
  515. })
  516. cls.bom_finished1 = cls.env['mrp.bom'].create({
  517. 'product_id': cls.finished1.id,
  518. 'product_tmpl_id': cls.finished1.product_tmpl_id.id,
  519. 'product_uom_id': cls.uom_unit.id,
  520. 'product_qty': 1,
  521. 'consumption': 'flexible',
  522. 'type': 'normal',
  523. 'bom_line_ids': [
  524. (0, 0, {'product_id': cls.compfinished1.id, 'product_qty': 1}),
  525. (0, 0, {'product_id': cls.compfinished2.id, 'product_qty': 1}),
  526. ],
  527. 'operation_ids': [
  528. (0, 0, {'sequence': 1, 'name': 'finished operation 1', 'workcenter_id': cls.workcenter1.id}),
  529. (0, 0, {'sequence': 2, 'name': 'finished operation 2', 'workcenter_id': cls.workcenter2.id}),
  530. ],
  531. })
  532. cls.bom_finished1.bom_line_ids[0].operation_id = cls.bom_finished1.operation_ids[0].id
  533. cls.bom_finished1.bom_line_ids[1].operation_id = cls.bom_finished1.operation_ids[1].id