test_wise_operator.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo.tests.common import TransactionCase
  4. class TestWiseOperator(TransactionCase):
  5. def test_wise_operator(self):
  6. # Create a new storable product
  7. product_wise = self.env['product.product'].create({
  8. 'name': 'Wise Unit',
  9. 'type': 'product',
  10. 'categ_id': self.ref('product.product_category_1'),
  11. 'uom_id': self.ref('uom.product_uom_unit'),
  12. 'uom_po_id': self.ref('uom.product_uom_unit'),
  13. })
  14. self.partner = self.env['res.partner'].create({'name': 'Deco Addict'})
  15. warehouse = self.env['stock.warehouse'].search([('company_id', '=', self.env.company.id)], limit=1)
  16. self.shelf2 = self.env['stock.location'].create({
  17. 'name': 'Shelf 2',
  18. 'barcode': 1231985,
  19. 'location_id': warehouse.lot_stock_id.id
  20. })
  21. self.shelf1 = self.env['stock.location'].create({
  22. 'name': 'Shelf 1',
  23. 'barcode': 1231892,
  24. 'location_id': warehouse.lot_stock_id.id
  25. })
  26. self.partner2 = self.env['res.partner'].create({'name': 'Ready Mat'})
  27. # Create an incoming picking for this product of 10 PCE from suppliers to stock
  28. vals = {
  29. 'name': 'Incoming picking (wise unit)',
  30. 'partner_id': self.partner.id,
  31. 'picking_type_id': self.ref('stock.picking_type_in'),
  32. 'location_id': self.ref('stock.stock_location_suppliers'),
  33. 'location_dest_id': self.ref('stock.stock_location_stock'),
  34. 'move_ids': [(0, 0, {
  35. 'name': '/',
  36. 'product_id': product_wise.id,
  37. 'product_uom': product_wise.uom_id.id,
  38. 'product_uom_qty': 10.00,
  39. 'location_id': self.ref('stock.stock_location_suppliers'),
  40. 'location_dest_id': self.ref('stock.stock_location_stock'),
  41. })],
  42. }
  43. pick1_wise = self.env['stock.picking'].create(vals)
  44. pick1_wise._onchange_picking_type()
  45. pick1_wise.move_ids._onchange_product_id()
  46. # Confirm and assign picking and prepare partial
  47. pick1_wise.action_confirm()
  48. pick1_wise.action_assign()
  49. # Put 4 pieces in shelf1 and 6 pieces in shelf2
  50. package1 = self.env['stock.quant.package'].create({'name': 'Pack 1'})
  51. pick1_wise.move_line_ids[0].write({
  52. 'result_package_id': package1.id,
  53. 'qty_done': 4,
  54. 'location_dest_id': self.shelf1.id
  55. })
  56. new_pack1 = self.env['stock.move.line'].create({
  57. 'product_id': product_wise.id,
  58. 'product_uom_id': self.ref('uom.product_uom_unit'),
  59. 'picking_id': pick1_wise.id,
  60. 'qty_done': 6.0,
  61. 'location_id': self.ref('stock.stock_location_suppliers'),
  62. 'location_dest_id': self.shelf2.id
  63. })
  64. # Transfer the receipt
  65. pick1_wise._action_done()
  66. # Check the system created 3 quants
  67. records = self.env['stock.quant'].search([('product_id', '=', product_wise.id)])
  68. self.assertEqual(len(records.ids), 3, "The number of quants created is not correct")
  69. # Make a delivery order of 5 pieces to the customer
  70. vals = {
  71. 'name': 'outgoing picking 1 (wise unit)',
  72. 'partner_id': self.partner2.id,
  73. 'picking_type_id': self.ref('stock.picking_type_out'),
  74. 'location_id': self.ref('stock.stock_location_stock'),
  75. 'location_dest_id': self.ref('stock.stock_location_customers'),
  76. 'move_ids': [(0, 0, {
  77. 'name': '/',
  78. 'product_id': product_wise.id,
  79. 'product_uom': product_wise.uom_id.id,
  80. 'product_uom_qty': 5.0,
  81. 'location_id': self.ref('stock.stock_location_stock'),
  82. 'location_dest_id': self.ref('stock.stock_location_customers'),
  83. })],
  84. }
  85. delivery_order_wise1 = self.env['stock.picking'].create(vals)
  86. delivery_order_wise1._onchange_picking_type()
  87. delivery_order_wise1.move_ids._onchange_product_id()
  88. # Assign and confirm
  89. delivery_order_wise1.action_confirm()
  90. delivery_order_wise1.action_assign()
  91. self.assertEqual(delivery_order_wise1.state, 'assigned')
  92. # Make a delivery order of 5 pieces to the customer
  93. vals = {
  94. 'name': 'outgoing picking 2 (wise unit)',
  95. 'partner_id': self.partner2.id,
  96. 'picking_type_id': self.ref('stock.picking_type_out'),
  97. 'location_id': self.ref('stock.stock_location_stock'),
  98. 'location_dest_id': self.ref('stock.stock_location_customers'),
  99. 'move_ids': [(0, 0, {
  100. 'name': '/',
  101. 'product_id': product_wise.id,
  102. 'product_uom': product_wise.uom_id.id,
  103. 'product_uom_qty': 5.0,
  104. 'location_id': self.ref('stock.stock_location_stock'),
  105. 'location_dest_id': self.ref('stock.stock_location_customers'),
  106. })],
  107. }
  108. delivery_order_wise2 = self.env['stock.picking'].create(vals)
  109. delivery_order_wise2._onchange_picking_type()
  110. delivery_order_wise2.move_ids._onchange_product_id()
  111. # Assign and confirm
  112. delivery_order_wise2.action_confirm()
  113. delivery_order_wise2.action_assign()
  114. self.assertEqual(delivery_order_wise2.state, 'assigned')
  115. # The operator is a wise guy and decides to do the opposite of what Odoo proposes.
  116. # He uses the products reserved on picking 1 on picking 2 and vice versa
  117. move1 = delivery_order_wise1.move_ids[0]
  118. move2 = delivery_order_wise2.move_ids[0]
  119. pack_ids1 = delivery_order_wise1.move_line_ids
  120. pack_ids2 = delivery_order_wise2.move_line_ids
  121. self.assertEqual(pack_ids1.location_id.id, self.shelf2.id)
  122. self.assertEqual(set(pack_ids2.mapped('location_id.id')), set([
  123. self.shelf1.id,
  124. self.shelf2.id]))
  125. # put the move lines from delivery_order_wise2 into delivery_order_wise1
  126. for pack_id2 in pack_ids2:
  127. new_pack_id1 = pack_id2.copy(default={'picking_id': delivery_order_wise1.id, 'move_id': move1.id})
  128. new_pack_id1.qty_done = pack_id2.reserved_qty
  129. new_move_lines = delivery_order_wise1.move_line_ids.filtered(lambda p: p.qty_done)
  130. self.assertEqual(sum(new_move_lines.mapped('reserved_qty')), 0)
  131. self.assertEqual(sum(new_move_lines.mapped('qty_done')), 5)
  132. self.assertEqual(set(new_move_lines.mapped('location_id.id')), set([
  133. self.shelf1.id,
  134. self.shelf2.id]))
  135. # put the move line from delivery_order_wise1 into delivery_order_wise2
  136. new_pack_id2 = pack_ids1.copy(default={'picking_id': delivery_order_wise2.id, 'move_id': move2.id})
  137. new_pack_id2.qty_done = pack_ids1.reserved_qty
  138. new_move_lines = delivery_order_wise2.move_line_ids.filtered(lambda p: p.qty_done)
  139. self.assertEqual(len(new_move_lines), 1)
  140. self.assertEqual(sum(new_move_lines.mapped('reserved_qty')), 0)
  141. self.assertEqual(sum(new_move_lines.mapped('qty_done')), 5)
  142. self.assertEqual(new_move_lines.location_id.id, self.shelf2.id)
  143. # Process this picking
  144. delivery_order_wise1._action_done()
  145. # Check there was no negative quant created by this picking
  146. records = self.env['stock.quant'].search([
  147. ('product_id', '=', product_wise.id),
  148. ('quantity', '<', 0.0),
  149. ('location_id', '=', self.ref('stock.stock_location_stock'))])
  150. self.assertEqual(len(records.ids), 0, 'This should not have created a negative quant')
  151. # Check the other delivery order has changed its state back to ready
  152. self.assertEqual(delivery_order_wise2.state, 'assigned', "Delivery order 2 should be back in ready state")
  153. # Process the second picking
  154. delivery_order_wise2._action_done()
  155. # Check all quants are in Customers and there are no negative quants anymore
  156. records = self.env['stock.quant'].search([
  157. ('product_id', '=', product_wise.id),
  158. ('location_id', '!=', self.ref('stock.stock_location_suppliers'))])
  159. self.assertTrue(all([x.location_id.id == self.ref('stock.stock_location_customers') and x.quantity > 0.0 or
  160. x.location_id.id != self.ref('stock.stock_location_customers') and x.quantity == 0.0 for x in records]),
  161. "Negative quant or wrong location detected")