test_replenish_wizard.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo.addons.stock.tests.common import TestStockCommon
  4. class TestReplenishWizard(TestStockCommon):
  5. @classmethod
  6. def setUpClass(cls):
  7. super().setUpClass()
  8. cls.vendor = cls.env['res.partner'].create(dict(name='The Replenisher'))
  9. cls.product1_price = 500
  10. # Create a supplier info witch the previous vendor
  11. cls.supplierinfo = cls.env['product.supplierinfo'].create({
  12. 'partner_id': cls.vendor.id,
  13. 'price': cls.product1_price,
  14. })
  15. # Create a product with the 'buy' route and
  16. # the 'supplierinfo' prevously created
  17. cls.product1 = cls.env['product.product'].create({
  18. 'name': 'product a',
  19. 'type': 'product',
  20. 'categ_id': cls.env.ref('product.product_category_all').id,
  21. 'seller_ids': [(4, cls.supplierinfo.id, 0)],
  22. 'route_ids': [(4, cls.env.ref('purchase_stock.route_warehouse0_buy').id, 0)],
  23. })
  24. # Additional Values required by the replenish wizard
  25. cls.uom_unit = cls.env.ref('uom.product_uom_unit')
  26. cls.wh = cls.env['stock.warehouse'].search([('company_id', '=', cls.env.user.id)], limit=1)
  27. def test_replenish_buy_1(self):
  28. """ Set a quantity to replenish via the "Buy" route and check if
  29. a purchase order is created with the correct values
  30. """
  31. self.product_uom_qty = 42
  32. replenish_wizard = self.env['product.replenish'].create({
  33. 'product_id': self.product1.id,
  34. 'product_tmpl_id': self.product1.product_tmpl_id.id,
  35. 'product_uom_id': self.uom_unit.id,
  36. 'quantity': self.product_uom_qty,
  37. 'warehouse_id': self.wh.id,
  38. })
  39. replenish_wizard.launch_replenishment()
  40. last_po_id = self.env['purchase.order'].search([
  41. ('origin', 'ilike', '%Manual Replenishment%'),
  42. ('partner_id', '=', self.vendor.id)
  43. ])[-1]
  44. self.assertTrue(last_po_id, 'Purchase Order not found')
  45. order_line = last_po_id.order_line.search([('product_id', '=', self.product1.id)])
  46. self.assertTrue(order_line, 'The product is not in the Purchase Order')
  47. self.assertEqual(order_line.product_qty, self.product_uom_qty, 'Quantities does not match')
  48. self.assertEqual(order_line.price_unit, self.product1_price, 'Prices does not match')
  49. def test_chose_supplier_1(self):
  50. """ Choose supplier based on the ordered quantity and minimum price
  51. replenish 10
  52. 1)seq1 vendor1 140 min qty 1
  53. 2)seq2 vendor1 100 min qty 10
  54. -> 2) should be chosen
  55. """
  56. product_to_buy = self.env['product.product'].create({
  57. 'name': "Furniture Service",
  58. 'type': 'product',
  59. 'categ_id': self.env.ref('product.product_category_all').id,
  60. 'route_ids': [(4, self.env.ref('purchase_stock.route_warehouse0_buy').id, 0)],
  61. })
  62. vendor1 = self.env['res.partner'].create({'name': 'vendor1', 'email': 'from.test@example.com'})
  63. supplierinfo1 = self.env['product.supplierinfo'].create({
  64. 'product_tmpl_id': product_to_buy.product_tmpl_id.id,
  65. 'partner_id': vendor1.id,
  66. 'min_qty': 1,
  67. 'price': 140,
  68. 'sequence': 1,
  69. })
  70. supplierinfo2 = self.env['product.supplierinfo'].create({
  71. 'product_tmpl_id': product_to_buy.product_tmpl_id.id,
  72. 'partner_id': vendor1.id,
  73. 'min_qty': 10,
  74. 'price': 100,
  75. 'sequence': 2,
  76. })
  77. replenish_wizard = self.env['product.replenish'].create({
  78. 'product_id': product_to_buy.id,
  79. 'product_tmpl_id': product_to_buy.product_tmpl_id.id,
  80. 'product_uom_id': self.uom_unit.id,
  81. 'quantity': 10,
  82. 'warehouse_id': self.wh.id,
  83. })
  84. replenish_wizard.launch_replenishment()
  85. last_po_id = self.env['purchase.order'].search([
  86. ('origin', 'ilike', '%Manual Replenishment%'),
  87. ])[-1]
  88. self.assertEqual(last_po_id.partner_id, vendor1)
  89. self.assertEqual(last_po_id.order_line.price_unit, 100)
  90. def test_chose_supplier_2(self):
  91. """ Choose supplier based on the ordered quantity and minimum price
  92. replenish 10
  93. 1)seq1 vendor1 140 min qty 1
  94. 2)seq2 vendor2 90 min qty 10
  95. 3)seq3 vendor1 100 min qty 10
  96. -> 3) should be chosen
  97. """
  98. product_to_buy = self.env['product.product'].create({
  99. 'name': "Furniture Service",
  100. 'type': 'product',
  101. 'categ_id': self.env.ref('product.product_category_all').id,
  102. 'route_ids': [(4, self.env.ref('purchase_stock.route_warehouse0_buy').id, 0)],
  103. })
  104. vendor1 = self.env['res.partner'].create({'name': 'vendor1', 'email': 'from.test@example.com'})
  105. vendor2 = self.env['res.partner'].create({'name': 'vendor2', 'email': 'from.test2@example.com'})
  106. supplierinfo1 = self.env['product.supplierinfo'].create({
  107. 'product_tmpl_id': product_to_buy.product_tmpl_id.id,
  108. 'partner_id': vendor1.id,
  109. 'min_qty': 1,
  110. 'price': 140,
  111. 'sequence': 1,
  112. })
  113. supplierinfo2 = self.env['product.supplierinfo'].create({
  114. 'product_tmpl_id': product_to_buy.product_tmpl_id.id,
  115. 'partner_id': vendor2.id,
  116. 'min_qty': 10,
  117. 'price': 90,
  118. 'sequence': 2,
  119. })
  120. supplierinfo3 = self.env['product.supplierinfo'].create({
  121. 'product_tmpl_id': product_to_buy.product_tmpl_id.id,
  122. 'partner_id': vendor1.id,
  123. 'min_qty': 10,
  124. 'price': 100,
  125. 'sequence': 3,
  126. })
  127. replenish_wizard = self.env['product.replenish'].create({
  128. 'product_id': product_to_buy.id,
  129. 'product_tmpl_id': product_to_buy.product_tmpl_id.id,
  130. 'product_uom_id': self.uom_unit.id,
  131. 'quantity': 10,
  132. 'warehouse_id': self.wh.id,
  133. })
  134. replenish_wizard.launch_replenishment()
  135. last_po_id = self.env['purchase.order'].search([
  136. ('origin', 'ilike', '%Manual Replenishment%'),
  137. ])[-1]
  138. self.assertEqual(last_po_id.partner_id, vendor1)
  139. self.assertEqual(last_po_id.order_line.price_unit, 100)
  140. def test_chose_supplier_3(self):
  141. """ Choose supplier based on the ordered quantity and minimum price
  142. replenish 10
  143. 1)seq2 vendor1 50
  144. 2)seq1 vendor2 50
  145. -> 2) should be chosen
  146. """
  147. product_to_buy = self.env['product.product'].create({
  148. 'name': "Furniture Service",
  149. 'type': 'product',
  150. 'categ_id': self.env.ref('product.product_category_all').id,
  151. 'route_ids': [(4, self.env.ref('purchase_stock.route_warehouse0_buy').id, 0)],
  152. })
  153. vendor1 = self.env['res.partner'].create({'name': 'vendor1', 'email': 'from.test@example.com'})
  154. vendor2 = self.env['res.partner'].create({'name': 'vendor2', 'email': 'from.test2@example.com'})
  155. supplierinfo1 = self.env['product.supplierinfo'].create({
  156. 'product_tmpl_id': product_to_buy.product_tmpl_id.id,
  157. 'partner_id': vendor1.id,
  158. 'price': 50,
  159. 'sequence': 2,
  160. })
  161. supplierinfo2 = self.env['product.supplierinfo'].create({
  162. 'product_tmpl_id': product_to_buy.product_tmpl_id.id,
  163. 'partner_id': vendor2.id,
  164. 'price': 50,
  165. 'sequence': 1,
  166. })
  167. replenish_wizard = self.env['product.replenish'].create({
  168. 'product_id': product_to_buy.id,
  169. 'product_tmpl_id': product_to_buy.product_tmpl_id.id,
  170. 'product_uom_id': self.uom_unit.id,
  171. 'quantity': 10,
  172. 'warehouse_id': self.wh.id,
  173. })
  174. replenish_wizard.launch_replenishment()
  175. last_po_id = self.env['purchase.order'].search([
  176. ('origin', 'ilike', '%Manual Replenishment%'),
  177. ])[-1]
  178. self.assertEqual(last_po_id.partner_id, vendor2)
  179. def test_chose_supplier_4(self):
  180. """ Choose supplier based on the ordered quantity and minimum price
  181. replenish 10
  182. 1)seq1 vendor1 100 min qty 2
  183. 2)seq2 vendor1 60 min qty 10
  184. 2)seq3 vendor1 80 min qty 5
  185. -> 2) should be chosen
  186. """
  187. product_to_buy = self.env['product.product'].create({
  188. 'name': "Furniture Service",
  189. 'type': 'product',
  190. 'categ_id': self.env.ref('product.product_category_all').id,
  191. 'route_ids': [(4, self.env.ref('purchase_stock.route_warehouse0_buy').id, 0)],
  192. })
  193. vendor1 = self.env['res.partner'].create({'name': 'vendor1', 'email': 'from.test@example.com'})
  194. supplierinfo1 = self.env['product.supplierinfo'].create({
  195. 'partner_id': vendor1.id,
  196. 'price': 100,
  197. 'product_tmpl_id': product_to_buy.product_tmpl_id.id,
  198. 'min_qty': 2
  199. })
  200. supplierinfo2 = self.env['product.supplierinfo'].create({
  201. 'partner_id': vendor1.id,
  202. 'price': 60,
  203. 'product_tmpl_id': product_to_buy.product_tmpl_id.id,
  204. 'min_qty': 10
  205. })
  206. supplierinfo3 = self.env['product.supplierinfo'].create({
  207. 'partner_id': vendor1.id,
  208. 'price': 80,
  209. 'product_tmpl_id': product_to_buy.product_tmpl_id.id,
  210. 'min_qty': 5
  211. })
  212. replenish_wizard = self.env['product.replenish'].create({
  213. 'product_id': product_to_buy.id,
  214. 'product_tmpl_id': product_to_buy.product_tmpl_id.id,
  215. 'product_uom_id': self.uom_unit.id,
  216. 'quantity': 10,
  217. 'warehouse_id': self.wh.id,
  218. })
  219. replenish_wizard.launch_replenishment()
  220. last_po_id = self.env['purchase.order'].search([
  221. ('origin', 'ilike', '%Manual Replenishment%'),
  222. ])[-1]
  223. self.assertEqual(last_po_id.partner_id, vendor1)
  224. self.assertEqual(last_po_id.order_line.price_unit, 60)