test_create_picking.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from datetime import date, datetime, timedelta
  4. from odoo.addons.mail.tests.common import mail_new_test_user
  5. from odoo.addons.product.tests import common
  6. from odoo.tests import Form
  7. class TestCreatePicking(common.TestProductCommon):
  8. @classmethod
  9. def setUpClass(cls):
  10. super().setUpClass()
  11. cls.partner_id = cls.env['res.partner'].create({'name': 'Wood Corner Partner'})
  12. cls.product_id_1 = cls.env['product.product'].create({'name': 'Large Desk'})
  13. cls.product_id_2 = cls.env['product.product'].create({'name': 'Conference Chair'})
  14. cls.user_purchase_user = mail_new_test_user(
  15. cls.env,
  16. name='Pauline Poivraisselle',
  17. login='pauline',
  18. email='pur@example.com',
  19. notification_type='inbox',
  20. groups='purchase.group_purchase_user',
  21. )
  22. cls.po_vals = {
  23. 'partner_id': cls.partner_id.id,
  24. 'order_line': [
  25. (0, 0, {
  26. 'name': cls.product_id_1.name,
  27. 'product_id': cls.product_id_1.id,
  28. 'product_qty': 5.0,
  29. 'product_uom': cls.product_id_1.uom_po_id.id,
  30. 'price_unit': 500.0,
  31. })],
  32. }
  33. def test_00_create_picking(self):
  34. # Draft purchase order created
  35. self.po = self.env['purchase.order'].create(self.po_vals)
  36. self.assertTrue(self.po, 'Purchase: no purchase order created')
  37. # Purchase order confirm
  38. self.po.button_confirm()
  39. self.assertEqual(self.po.state, 'purchase', 'Purchase: PO state should be "Purchase')
  40. self.assertEqual(self.po.incoming_picking_count, 1, 'Purchase: one picking should be created')
  41. self.assertEqual(len(self.po.order_line.move_ids), 1, 'One move should be created')
  42. # Change purchase order line product quantity
  43. self.po.order_line.write({'product_qty': 7.0})
  44. self.assertEqual(len(self.po.order_line.move_ids), 1, 'The two moves should be merged in one')
  45. # Validate first shipment
  46. self.picking = self.po.picking_ids[0]
  47. for ml in self.picking.move_line_ids:
  48. ml.qty_done = ml.reserved_uom_qty
  49. self.picking._action_done()
  50. self.assertEqual(self.po.order_line.mapped('qty_received'), [7.0], 'Purchase: all products should be received')
  51. # create new order line
  52. self.po.write({'order_line': [
  53. (0, 0, {
  54. 'name': self.product_id_2.name,
  55. 'product_id': self.product_id_2.id,
  56. 'product_qty': 5.0,
  57. 'product_uom': self.product_id_2.uom_po_id.id,
  58. 'price_unit': 250.0,
  59. })]})
  60. self.assertEqual(self.po.incoming_picking_count, 2, 'New picking should be created')
  61. moves = self.po.order_line.mapped('move_ids').filtered(lambda x: x.state not in ('done', 'cancel'))
  62. self.assertEqual(len(moves), 1, 'One moves should have been created')
  63. def test_01_check_double_validation(self):
  64. # make double validation two step
  65. self.env.company.write({'po_double_validation': 'two_step','po_double_validation_amount':2000.00})
  66. # Draft purchase order created
  67. self.po = self.env['purchase.order'].with_user(self.user_purchase_user).create(self.po_vals)
  68. self.assertTrue(self.po, 'Purchase: no purchase order created')
  69. # Purchase order confirm
  70. self.po.button_confirm()
  71. self.assertEqual(self.po.state, 'to approve', 'Purchase: PO state should be "to approve".')
  72. # PO approved by manager
  73. self.po.env.user.groups_id += self.env.ref("purchase.group_purchase_manager")
  74. self.po.button_approve()
  75. self.assertEqual(self.po.state, 'purchase', 'PO state should be "Purchase".')
  76. def test_02_check_mto_chain(self):
  77. """ Simulate a mto chain with a purchase order. Cancel the
  78. purchase order should also change the procure_method of the
  79. following move to MTS in order to be able to link it to a
  80. manually created purchase order.
  81. """
  82. stock_location = self.env.ref('stock.stock_location_stock')
  83. customer_location = self.env.ref('stock.stock_location_customers')
  84. picking_type_out = self.env.ref('stock.picking_type_out')
  85. # route buy should be there by default
  86. partner = self.env['res.partner'].create({
  87. 'name': 'Jhon'
  88. })
  89. vendor = self.env['res.partner'].create({
  90. 'name': 'Roger'
  91. })
  92. seller = self.env['product.supplierinfo'].create({
  93. 'partner_id': partner.id,
  94. 'price': 12.0,
  95. })
  96. product = self.env['product.product'].create({
  97. 'name': 'product',
  98. 'type': 'product',
  99. 'route_ids': [(4, self.ref('stock.route_warehouse0_mto')), (4, self.ref('purchase_stock.route_warehouse0_buy'))],
  100. 'seller_ids': [(6, 0, [seller.id])],
  101. 'categ_id': self.env.ref('product.product_category_all').id,
  102. 'supplier_taxes_id': [(6, 0, [])],
  103. })
  104. customer_move = self.env['stock.move'].create({
  105. 'name': 'move out',
  106. 'location_id': stock_location.id,
  107. 'location_dest_id': customer_location.id,
  108. 'product_id': product.id,
  109. 'product_uom': product.uom_id.id,
  110. 'product_uom_qty': 100.0,
  111. 'procure_method': 'make_to_order',
  112. 'picking_type_id': picking_type_out.id,
  113. })
  114. customer_move._action_confirm()
  115. purchase_order = self.env['purchase.order'].search([('partner_id', '=', partner.id)])
  116. self.assertTrue(purchase_order, 'No purchase order created.')
  117. # Check purchase order line data.
  118. purchase_order_line = purchase_order.order_line
  119. self.assertEqual(purchase_order_line.product_id, product, 'The product on the purchase order line is not correct.')
  120. self.assertEqual(purchase_order_line.price_unit, seller.price, 'The purchase order line price should be the same as the seller.')
  121. self.assertEqual(purchase_order_line.product_qty, customer_move.product_uom_qty, 'The purchase order line qty should be the same as the move.')
  122. self.assertEqual(purchase_order_line.price_subtotal, 1200.0, 'The purchase order line subtotal should be equal to the move qty * seller price.')
  123. purchase_order.button_cancel()
  124. self.assertEqual(purchase_order.state, 'cancel', 'Purchase order should be cancelled.')
  125. self.assertEqual(customer_move.procure_method, 'make_to_stock', 'Customer move should be passed to mts.')
  126. purchase = purchase_order.create({
  127. 'partner_id': vendor.id,
  128. 'order_line': [
  129. (0, 0, {
  130. 'name': product.name,
  131. 'product_id': product.id,
  132. 'product_qty': 100.0,
  133. 'product_uom': product.uom_po_id.id,
  134. 'price_unit': 11.0,
  135. })],
  136. })
  137. self.assertTrue(purchase, 'RFQ should be created')
  138. purchase.button_confirm()
  139. picking = purchase.picking_ids
  140. self.assertTrue(picking, 'Picking should be created')
  141. # Process pickings
  142. picking.action_confirm()
  143. picking.move_ids.quantity_done = 100.0
  144. picking.button_validate()
  145. # mts move will be automatically assigned
  146. self.assertEqual(customer_move.state, 'assigned', 'Automatically assigned due to the incoming move makes it available.')
  147. self.assertEqual(self.env['stock.quant']._get_available_quantity(product, stock_location), 0.0, 'Wrong quantity in stock.')
  148. def test_03_uom(self):
  149. """ Buy a dozen of products stocked in units. Check that the quantities on the purchase order
  150. lines as well as the received quantities are handled in dozen while the moves themselves
  151. are handled in units. Edit the ordered quantities, check that the quantities are correctly
  152. updated on the moves. Edit the ir.config_parameter to propagate the uom of the purchase order
  153. lines to the moves and edit a last time the ordered quantities. Receive, check the quantities.
  154. """
  155. uom_unit = self.env.ref('uom.product_uom_unit')
  156. uom_dozen = self.env.ref('uom.product_uom_dozen')
  157. self.assertEqual(self.product_id_1.uom_po_id.id, uom_unit.id)
  158. # buy a dozen
  159. po = self.env['purchase.order'].create(self.po_vals)
  160. po.order_line.product_qty = 1
  161. po.order_line.product_uom = uom_dozen.id
  162. po.button_confirm()
  163. # the move should be 12 units
  164. # note: move.product_qty = computed field, always in the uom of the quant
  165. # move.product_uom_qty = stored field representing the initial demand in move.product_uom
  166. move1 = po.picking_ids.move_ids.sorted()[0]
  167. self.assertEqual(move1.product_uom_qty, 12)
  168. self.assertEqual(move1.product_uom.id, uom_unit.id)
  169. self.assertEqual(move1.product_qty, 12)
  170. # edit the so line, sell 2 dozen, the move should now be 24 units
  171. po.order_line.product_qty = 2
  172. move1 = po.picking_ids.move_ids.sorted()[0]
  173. self.assertEqual(move1.product_uom_qty, 24)
  174. self.assertEqual(move1.product_uom.id, uom_unit.id)
  175. self.assertEqual(move1.product_qty, 24)
  176. # force the propagation of the uom, sell 3 dozen
  177. self.env['ir.config_parameter'].sudo().set_param('stock.propagate_uom', '1')
  178. po.order_line.product_qty = 3
  179. move2 = po.picking_ids.move_ids.filtered(lambda m: m.product_uom.id == uom_dozen.id)
  180. self.assertEqual(move2.product_uom_qty, 1)
  181. self.assertEqual(move2.product_uom.id, uom_dozen.id)
  182. self.assertEqual(move2.product_qty, 12)
  183. # deliver everything
  184. move1.quantity_done = 24
  185. move2.quantity_done = 1
  186. po.picking_ids.button_validate()
  187. # check the delivered quantity
  188. self.assertEqual(po.order_line.qty_received, 3.0)
  189. def test_04_mto_multiple_po(self):
  190. """ Simulate a mto chain with 2 purchase order.
  191. Create a move with qty 1, confirm the RFQ then create a new
  192. move that will not be merged in the first one(simulate an increase
  193. order quantity on a SO). It should generate a new RFQ, validate
  194. and receipt the picking then try to reserve the delivery
  195. picking.
  196. """
  197. stock_location = self.env.ref('stock.stock_location_stock')
  198. customer_location = self.env.ref('stock.stock_location_customers')
  199. picking_type_out = self.env.ref('stock.picking_type_out')
  200. # route buy should be there by default
  201. partner = self.env['res.partner'].create({
  202. 'name': 'Jhon'
  203. })
  204. seller = self.env['product.supplierinfo'].create({
  205. 'partner_id': partner.id,
  206. 'price': 12.0,
  207. })
  208. product = self.env['product.product'].create({
  209. 'name': 'product',
  210. 'type': 'product',
  211. 'route_ids': [(4, self.ref('stock.route_warehouse0_mto')), (4, self.ref('purchase_stock.route_warehouse0_buy'))],
  212. 'seller_ids': [(6, 0, [seller.id])],
  213. 'categ_id': self.env.ref('product.product_category_all').id,
  214. })
  215. # A picking is require since only moves inside the same picking are merged.
  216. customer_picking = self.env['stock.picking'].create({
  217. 'location_id': stock_location.id,
  218. 'location_dest_id': customer_location.id,
  219. 'partner_id': partner.id,
  220. 'picking_type_id': picking_type_out.id,
  221. })
  222. customer_move = self.env['stock.move'].create({
  223. 'name': 'move out',
  224. 'location_id': stock_location.id,
  225. 'location_dest_id': customer_location.id,
  226. 'product_id': product.id,
  227. 'product_uom': product.uom_id.id,
  228. 'product_uom_qty': 80.0,
  229. 'procure_method': 'make_to_order',
  230. 'picking_id': customer_picking.id,
  231. })
  232. customer_move._action_confirm()
  233. purchase_order = self.env['purchase.order'].search([('partner_id', '=', partner.id)])
  234. self.assertTrue(purchase_order, 'No purchase order created.')
  235. # Check purchase order line data.
  236. purchase_order_line = purchase_order.order_line
  237. self.assertEqual(purchase_order_line.product_id, product, 'The product on the purchase order line is not correct.')
  238. self.assertEqual(purchase_order_line.price_unit, seller.price, 'The purchase order line price should be the same as the seller.')
  239. self.assertEqual(purchase_order_line.product_qty, customer_move.product_uom_qty, 'The purchase order line qty should be the same as the move.')
  240. purchase_order.button_confirm()
  241. customer_move_2 = self.env['stock.move'].create({
  242. 'name': 'move out',
  243. 'location_id': stock_location.id,
  244. 'location_dest_id': customer_location.id,
  245. 'product_id': product.id,
  246. 'product_uom': product.uom_id.id,
  247. 'product_uom_qty': 20.0,
  248. 'procure_method': 'make_to_order',
  249. 'picking_id': customer_picking.id,
  250. })
  251. customer_move_2._action_confirm()
  252. self.assertTrue(customer_move_2.exists(), 'The second customer move should not be merged in the first.')
  253. self.assertEqual(sum(customer_picking.move_ids.mapped('product_uom_qty')), 100.0)
  254. purchase_order_2 = self.env['purchase.order'].search([('partner_id', '=', partner.id), ('state', '=', 'draft')])
  255. self.assertTrue(purchase_order_2, 'No purchase order created.')
  256. purchase_order_2.button_confirm()
  257. purchase_order.picking_ids.move_ids.quantity_done = 80.0
  258. purchase_order.picking_ids.button_validate()
  259. purchase_order_2.picking_ids.move_ids.quantity_done = 20.0
  260. purchase_order_2.picking_ids.button_validate()
  261. self.assertEqual(sum(customer_picking.move_ids.mapped('reserved_availability')), 100.0, 'The total quantity for the customer move should be available and reserved.')
  262. def test_04_rounding(self):
  263. """ We set the Unit(s) rounding to 1.0 and ensure buying 1.2 units in a PO is rounded to 1.0
  264. at reception.
  265. """
  266. uom_unit = self.env.ref('uom.product_uom_unit')
  267. uom_unit.rounding = 1.0
  268. # buy a dozen
  269. po = self.env['purchase.order'].create(self.po_vals)
  270. po.order_line.product_qty = 1.2
  271. po.button_confirm()
  272. # the move should be 1.0 units
  273. move1 = po.picking_ids.move_ids[0]
  274. self.assertEqual(move1.product_uom_qty, 1.0)
  275. self.assertEqual(move1.product_uom.id, uom_unit.id)
  276. self.assertEqual(move1.product_qty, 1.0)
  277. # edit the so line, buy 2.4 units, the move should now be 2.0 units
  278. po.order_line.product_qty = 2.0
  279. self.assertEqual(move1.product_uom_qty, 2.0)
  280. self.assertEqual(move1.product_uom.id, uom_unit.id)
  281. self.assertEqual(move1.product_qty, 2.0)
  282. # deliver everything
  283. move1.quantity_done = 2.0
  284. po.picking_ids.button_validate()
  285. # check the delivered quantity
  286. self.assertEqual(po.order_line.qty_received, 2.0)
  287. def test_05_uom_rounding(self):
  288. """ We set the Unit(s) and Dozen(s) rounding to 1.0 and ensure buying 1.3 dozens in a PO is
  289. rounded to 1.0 at reception.
  290. """
  291. uom_unit = self.env.ref('uom.product_uom_unit')
  292. uom_dozen = self.env.ref('uom.product_uom_dozen')
  293. uom_unit.rounding = 1.0
  294. uom_dozen.rounding = 1.0
  295. # buy 1.3 dozen
  296. po = self.env['purchase.order'].create(self.po_vals)
  297. po.order_line.product_qty = 1.3
  298. po.order_line.product_uom = uom_dozen.id
  299. po.button_confirm()
  300. # the move should be 16.0 units
  301. move1 = po.picking_ids.move_ids[0]
  302. self.assertEqual(move1.product_uom_qty, 16.0)
  303. self.assertEqual(move1.product_uom.id, uom_unit.id)
  304. self.assertEqual(move1.product_qty, 16.0)
  305. # force the propagation of the uom, buy 2.6 dozens, the move 2 should have 2 dozens
  306. self.env['ir.config_parameter'].sudo().set_param('stock.propagate_uom', '1')
  307. po.order_line.product_qty = 2.6
  308. move2 = po.picking_ids.move_ids.filtered(lambda m: m.product_uom.id == uom_dozen.id)
  309. self.assertEqual(move2.product_uom_qty, 2)
  310. self.assertEqual(move2.product_uom.id, uom_dozen.id)
  311. self.assertEqual(move2.product_qty, 24)
  312. def create_delivery_order(self):
  313. stock_location = self.env.ref('stock.stock_location_stock')
  314. customer_location = self.env.ref('stock.stock_location_customers')
  315. unit = self.ref("uom.product_uom_unit")
  316. picking_type_out = self.env.ref('stock.picking_type_out')
  317. partner = self.env['res.partner'].create({'name': 'AAA', 'email': 'from.test@example.com'})
  318. supplier_info1 = self.env['product.supplierinfo'].create({
  319. 'partner_id': partner.id,
  320. 'price': 50,
  321. })
  322. warehouse1 = self.env.ref('stock.warehouse0')
  323. route_buy = warehouse1.buy_pull_id.route_id
  324. route_mto = warehouse1.mto_pull_id.route_id
  325. product = self.env['product.product'].create({
  326. 'name': 'Usb Keyboard',
  327. 'type': 'product',
  328. 'uom_id': unit,
  329. 'uom_po_id': unit,
  330. 'seller_ids': [(6, 0, [supplier_info1.id])],
  331. 'route_ids': [(6, 0, [route_buy.id, route_mto.id])]
  332. })
  333. delivery_order = self.env['stock.picking'].create({
  334. 'location_id': stock_location.id,
  335. 'location_dest_id': customer_location.id,
  336. 'partner_id': partner.id,
  337. 'picking_type_id': picking_type_out.id,
  338. })
  339. customer_move = self.env['stock.move'].create({
  340. 'name': 'move out',
  341. 'location_id': stock_location.id,
  342. 'location_dest_id': customer_location.id,
  343. 'product_id': product.id,
  344. 'product_uom': product.uom_id.id,
  345. 'product_uom_qty': 10.0,
  346. 'procure_method': 'make_to_order',
  347. 'picking_id': delivery_order.id,
  348. })
  349. customer_move._action_confirm()
  350. # find created po the product
  351. purchase_order = self.env['purchase.order'].search([('partner_id', '=', partner.id)])
  352. return delivery_order, purchase_order
  353. def test_05_propagate_deadline(self):
  354. """ In order to check deadline date of the delivery order is changed and the planned date not."""
  355. # Create Delivery Order and with propagate date and minimum delta
  356. delivery_order, purchase_order = self.create_delivery_order()
  357. # check po is created or not
  358. self.assertTrue(purchase_order, 'No purchase order created.')
  359. purchase_order_line = purchase_order.order_line
  360. # change scheduled date of po line.
  361. purchase_order_line.write({'date_planned': purchase_order_line.date_planned + timedelta(days=5)})
  362. # Now check scheduled date and deadline of delivery order.
  363. self.assertNotEqual(
  364. purchase_order_line.date_planned, delivery_order.scheduled_date,
  365. 'Scheduled delivery order date should not changed.')
  366. self.assertEqual(
  367. purchase_order_line.date_planned, delivery_order.date_deadline,
  368. 'Delivery deadline date should be changed.')
  369. def test_07_differed_schedule_date(self):
  370. # Required for `reception_steps` to be visible in the view
  371. self.env.user.groups_id += self.env.ref('stock.group_adv_location')
  372. warehouse = self.env['stock.warehouse'].search([], limit=1)
  373. with Form(warehouse) as w:
  374. w.reception_steps = 'three_steps'
  375. po_form = Form(self.env['purchase.order'])
  376. po_form.partner_id = self.partner_id
  377. with po_form.order_line.new() as line:
  378. line.product_id = self.product_id_1
  379. line.date_planned = datetime.today()
  380. line.product_qty = 1.0
  381. with po_form.order_line.new() as line:
  382. line.product_id = self.product_id_1
  383. line.date_planned = datetime.today() + timedelta(days=7)
  384. line.product_qty = 1.0
  385. po = po_form.save()
  386. po.button_approve()
  387. po.picking_ids.move_line_ids.write({
  388. 'qty_done': 1.0
  389. })
  390. po.picking_ids.button_validate()
  391. pickings = self.env['stock.picking'].search([('group_id', '=', po.group_id.id)])
  392. for picking in pickings:
  393. self.assertEqual(picking.scheduled_date.date(), date.today())
  394. def test_update_quantity_and_return(self):
  395. po = self.env['purchase.order'].create(self.po_vals)
  396. po.order_line.product_qty = 10
  397. po.button_confirm()
  398. first_picking = po.picking_ids
  399. first_picking.move_ids.quantity_done = 5
  400. # create the backorder
  401. backorder_wizard_dict = first_picking.button_validate()
  402. backorder_wizard = Form(self.env[backorder_wizard_dict['res_model']].with_context(backorder_wizard_dict['context'])).save()
  403. backorder_wizard.process()
  404. self.assertEqual(len(po.picking_ids), 2)
  405. # Create a partial return
  406. stock_return_picking_form = Form(
  407. self.env['stock.return.picking'].with_context(
  408. active_ids=first_picking.ids,
  409. active_id=first_picking.ids[0],
  410. active_model='stock.picking'
  411. )
  412. )
  413. stock_return_picking = stock_return_picking_form.save()
  414. stock_return_picking.product_return_moves.quantity = 2.0
  415. stock_return_picking_action = stock_return_picking.create_returns()
  416. return_pick = self.env['stock.picking'].browse(stock_return_picking_action['res_id'])
  417. return_pick.action_assign()
  418. return_pick.move_ids.quantity_done = 2
  419. return_pick._action_done()
  420. self.assertEqual(po.order_line.qty_received, 3)
  421. po.order_line.product_qty += 2
  422. backorder = po.picking_ids.filtered(lambda picking: picking.state == 'assigned')
  423. self.assertEqual(backorder.move_ids.product_uom_qty, 9)
  424. def test_08_check_update_qty_mto_chain(self):
  425. """ Simulate a mto chain with a purchase order. Updating the
  426. initial demand should also impact the initial move and the
  427. purchase order if it wasn't yet confirmed.
  428. """
  429. def create_run_procurement(product, product_qty, values=None):
  430. if not values:
  431. values = {
  432. 'warehouse_id': picking_type_out.warehouse_id,
  433. 'action': 'pull_push',
  434. 'group_id': procurement_group,
  435. }
  436. return self.env['procurement.group'].run([self.env['procurement.group'].Procurement(
  437. product, product_qty, self.uom_unit, vendor.property_stock_customer,
  438. product.name, '/', self.env.company, values)
  439. ])
  440. # Prepare procurement that replicates a sale order.
  441. picking_type_out = self.env.ref('stock.picking_type_out')
  442. partner = self.env['res.partner'].create({
  443. 'name': 'Jhon'
  444. })
  445. seller = self.env['product.supplierinfo'].create({
  446. 'partner_id': partner.id,
  447. 'price': 12.0,
  448. })
  449. vendor = self.env['res.partner'].create({
  450. 'name': 'Roger'
  451. })
  452. # This needs to be tried with MTO route activated
  453. self.env['stock.route'].browse(self.ref('stock.route_warehouse0_mto')).action_unarchive()
  454. product = self.env['product.product'].create({
  455. 'name': 'product',
  456. 'type': 'product',
  457. 'route_ids': [(4, self.ref('stock.route_warehouse0_mto')), (4, self.ref('purchase_stock.route_warehouse0_buy'))],
  458. 'seller_ids': [(6, 0, [seller.id])],
  459. 'categ_id': self.env.ref('product.product_category_all').id,
  460. 'supplier_taxes_id': [(6, 0, [])],
  461. })
  462. procurement_group = self.env['procurement.group'].create({
  463. 'move_type': 'direct',
  464. 'partner_id': vendor.id
  465. })
  466. # Create initial procurement that will generate the initial move and its picking.
  467. create_run_procurement(product, 50, {
  468. 'group_id': procurement_group,
  469. 'warehouse_id': picking_type_out.warehouse_id,
  470. 'partner_id': vendor
  471. })
  472. customer_move = self.env['stock.move'].search([('group_id', '=', procurement_group.id)])
  473. purchase_order = self.env['purchase.order'].search([('partner_id', '=', partner.id)])
  474. self.assertTrue(purchase_order, 'No purchase order created.')
  475. # Check purchase order line data.
  476. purchase_order_line = purchase_order.order_line
  477. self.assertEqual(purchase_order_line.product_id, product, 'The product on the purchase order line is not correct.')
  478. self.assertEqual(purchase_order_line.product_qty, 50, 'The purchase order line qty should be the same as the move.')
  479. # Create procurement to decrease quantity in the initial move and the related RFQ.
  480. create_run_procurement(product, -10.00)
  481. self.assertEqual(customer_move.product_uom_qty, 40, 'The demand on the initial move should have been decreased when merged with the procurement.')
  482. self.assertEqual(purchase_order_line.product_qty, 40, 'The demand on the Purchase Order should have been decreased since it is still a RFQ.')
  483. # Create procurement to increase quantity on the initial move and the related RFQ.
  484. create_run_procurement(product, 5.00)
  485. self.assertEqual(customer_move.product_uom_qty, 45, 'The demand on the initial move should have been increased when merged with the procurement.')
  486. self.assertEqual(purchase_order_line.product_qty, 45, 'The demand on the Purchase Order should have been increased since it is still a RFQ.')
  487. purchase_order.button_confirm()
  488. # Create procurement to decrease quantity in the initial move but not the confirmed PO.
  489. create_run_procurement(product, -10.00)
  490. self.assertEqual(customer_move.product_uom_qty, 35, 'The demand on the initial move should have been decreased when merged with the procurement.')
  491. self.assertEqual(purchase_order_line.product_qty, 45, 'The demand on the Purchase Order should not have been decreased since it is has been confirmed.')
  492. purchase_orders = self.env['purchase.order'].search([('partner_id', '=', partner.id)])
  493. self.assertEqual(len(purchase_orders), 1, 'No RFQ should have been created for a negative demand')
  494. # Create procurement to increase quantity on the initial move that will create a new move and a new RFQ for missing demand.
  495. create_run_procurement(product, 5.00)
  496. self.assertEqual(customer_move.product_uom_qty, 35, 'The demand on the initial move should not have been increased since it should be a new move.')
  497. self.assertEqual(purchase_order_line.product_qty, 45, 'The demand on the Purchase Order should not have been increased since it is has been confirmed.')
  498. purchase_orders = self.env['purchase.order'].search([('partner_id', '=', partner.id)])
  499. self.assertEqual(len(purchase_orders), 2, 'A new RFQ should have been created for missing demand.')
  500. def test_update_qty_purchased(self):
  501. """
  502. Test that the price unit in the purchase order line and the move is updated
  503. according to the pricelist defined in the product, and that the "stock.moves"
  504. are merged correctly when changing the qty purchased.
  505. """
  506. # add vendor to the product
  507. self.product_id_1.seller_ids = [(0, 0, {
  508. 'partner_id': self.partner_id.id,
  509. 'min_qty': 10,
  510. 'price': 15,
  511. })]
  512. # Create PO
  513. po_form = Form(self.env['purchase.order'])
  514. po_form.partner_id = self.partner_id
  515. with po_form.order_line.new() as po_line:
  516. po_line.product_id = self.product_id_1
  517. po_line.product_qty = 10
  518. purchase_order = po_form.save()
  519. # Confirm Purchase order
  520. purchase_order.button_confirm()
  521. # Check purchase order state, it should be "purchase".
  522. self.assertEqual(purchase_order.state, 'purchase', 'Purchase order should be in purchase state.')
  523. # Make sure that picking has been created
  524. self.assertEqual(len(purchase_order.picking_ids), 1)
  525. # check that the price list has been applied
  526. self.assertEqual(purchase_order.order_line.price_unit, 15)
  527. self.assertEqual(purchase_order.picking_ids.move_ids.price_unit, 15)
  528. # update the product qty purchased
  529. with po_form.order_line.edit(0) as po_line:
  530. po_line.product_qty = 9
  531. purchase_order = po_form.save()
  532. # verify that the move for the decreased qty has been merged with the initial move
  533. self.assertEqual(len(purchase_order.picking_ids), 1)
  534. self.assertEqual(len(purchase_order.picking_ids.move_ids), 1)
  535. # check that the price has been updated in the purchase order line and in the stock.move
  536. self.assertEqual(purchase_order.order_line.price_unit, 0)
  537. self.assertEqual(purchase_order.picking_ids.move_ids.price_unit, 0)