test_sale_mrp_procurement.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import time
  4. from odoo.tests.common import TransactionCase, Form
  5. from odoo.tools import mute_logger
  6. class TestSaleMrpProcurement(TransactionCase):
  7. def test_sale_mrp(self):
  8. # Required for `uom_id` to be visible in the view
  9. self.env.user.groups_id += self.env.ref('uom.group_uom')
  10. self.env.ref('stock.route_warehouse0_mto').active = True
  11. warehouse0 = self.env.ref('stock.warehouse0')
  12. # In order to test the sale_mrp module in OpenERP, I start by creating a new product 'Slider Mobile'
  13. # I define product category Mobile Products Sellable.
  14. with mute_logger('odoo.tests.common.onchange'):
  15. # Suppress warning on "Changing your cost method" when creating a
  16. # product category
  17. pc = Form(self.env['product.category'])
  18. pc.name = 'Mobile Products Sellable'
  19. product_category_allproductssellable0 = pc.save()
  20. uom_unit = self.env.ref('uom.product_uom_unit')
  21. self.assertIn("seller_ids", self.env['product.template'].fields_get())
  22. # I define product for Slider Mobile.
  23. product = Form(self.env['product.template'])
  24. product.categ_id = product_category_allproductssellable0
  25. product.list_price = 200.0
  26. product.name = 'Slider Mobile'
  27. product.detailed_type = 'product'
  28. product.uom_id = uom_unit
  29. product.uom_po_id = uom_unit
  30. product.route_ids.clear()
  31. product.route_ids.add(warehouse0.manufacture_pull_id.route_id)
  32. product.route_ids.add(warehouse0.mto_pull_id.route_id)
  33. product_template_slidermobile0 = product.save()
  34. product_template_slidermobile0.standard_price = 189
  35. product_component = Form(self.env['product.product'])
  36. product_component.name = 'Battery'
  37. product_product_bettery = product_component.save()
  38. with Form(self.env['mrp.bom']) as bom:
  39. bom.product_tmpl_id = product_template_slidermobile0
  40. with bom.bom_line_ids.new() as line:
  41. line.product_id = product_product_bettery
  42. line.product_qty = 4
  43. # I create a sale order for product Slider mobile
  44. so_form = Form(self.env['sale.order'])
  45. so_form.partner_id = self.env['res.partner'].create({'name': 'Another Test Partner'})
  46. with so_form.order_line.new() as line:
  47. line.product_id = product_template_slidermobile0.product_variant_ids
  48. line.price_unit = 200
  49. line.product_uom_qty = 500.0
  50. line.customer_lead = 7.0
  51. sale_order_so0 = so_form.save()
  52. # I confirm the sale order
  53. sale_order_so0.action_confirm()
  54. # I verify that a manufacturing order has been generated, and that its name and reference are correct
  55. mo = self.env['mrp.production'].search([('origin', 'like', sale_order_so0.name)], limit=1)
  56. self.assertTrue(mo, 'Manufacturing order has not been generated')
  57. # Check the mo is displayed on the so
  58. self.assertEqual(mo.id, sale_order_so0.action_view_mrp_production()['res_id'])
  59. def test_sale_mrp_pickings(self):
  60. """ Test sale of multiple mrp products in MTO
  61. to avoid generating multiple deliveries
  62. to the customer location
  63. """
  64. # Required for `uom_id` to be visible in the view
  65. self.env.user.groups_id += self.env.ref('uom.group_uom')
  66. # Required for `manufacture_step` to be visible in the view
  67. self.env.user.groups_id += self.env.ref('stock.group_adv_location')
  68. self.env.ref('stock.route_warehouse0_mto').active = True
  69. # Create warehouse
  70. self.customer_location = self.env['ir.model.data']._xmlid_to_res_id('stock.stock_location_customers')
  71. self.warehouse = self.env['stock.warehouse'].create({
  72. 'name': 'Test Warehouse',
  73. 'code': 'TWH'
  74. })
  75. self.uom_unit = self.env.ref('uom.product_uom_unit')
  76. # Create raw product for manufactured product
  77. product_form = Form(self.env['product.product'])
  78. product_form.name = 'Raw Stick'
  79. product_form.detailed_type = 'product'
  80. product_form.uom_id = self.uom_unit
  81. product_form.uom_po_id = self.uom_unit
  82. self.raw_product = product_form.save()
  83. # Create manufactured product
  84. product_form = Form(self.env['product.product'])
  85. product_form.name = 'Stick'
  86. product_form.uom_id = self.uom_unit
  87. product_form.uom_po_id = self.uom_unit
  88. product_form.detailed_type = 'product'
  89. product_form.route_ids.clear()
  90. product_form.route_ids.add(self.warehouse.manufacture_pull_id.route_id)
  91. product_form.route_ids.add(self.warehouse.mto_pull_id.route_id)
  92. self.finished_product = product_form.save()
  93. # Create manifactured product which uses another manifactured
  94. product_form = Form(self.env['product.product'])
  95. product_form.name = 'Arrow'
  96. product_form.detailed_type = 'product'
  97. product_form.route_ids.clear()
  98. product_form.route_ids.add(self.warehouse.manufacture_pull_id.route_id)
  99. product_form.route_ids.add(self.warehouse.mto_pull_id.route_id)
  100. self.complex_product = product_form.save()
  101. ## Create raw product for manufactured product
  102. product_form = Form(self.env['product.product'])
  103. product_form.name = 'Raw Iron'
  104. product_form.detailed_type = 'product'
  105. product_form.uom_id = self.uom_unit
  106. product_form.uom_po_id = self.uom_unit
  107. self.raw_product_2 = product_form.save()
  108. # Create bom for manufactured product
  109. bom_product_form = Form(self.env['mrp.bom'])
  110. bom_product_form.product_id = self.finished_product
  111. bom_product_form.product_tmpl_id = self.finished_product.product_tmpl_id
  112. bom_product_form.product_qty = 1.0
  113. bom_product_form.type = 'normal'
  114. with bom_product_form.bom_line_ids.new() as bom_line:
  115. bom_line.product_id = self.raw_product
  116. bom_line.product_qty = 2.0
  117. self.bom = bom_product_form.save()
  118. ## Create bom for manufactured product
  119. bom_product_form = Form(self.env['mrp.bom'])
  120. bom_product_form.product_id = self.complex_product
  121. bom_product_form.product_tmpl_id = self.complex_product.product_tmpl_id
  122. with bom_product_form.bom_line_ids.new() as line:
  123. line.product_id = self.finished_product
  124. line.product_qty = 1.0
  125. with bom_product_form.bom_line_ids.new() as line:
  126. line.product_id = self.raw_product_2
  127. line.product_qty = 1.0
  128. self.complex_bom = bom_product_form.save()
  129. with Form(self.warehouse) as warehouse:
  130. warehouse.manufacture_steps = 'pbm_sam'
  131. so_form = Form(self.env['sale.order'])
  132. so_form.partner_id = self.env['res.partner'].create({'name': 'Another Test Partner'})
  133. with so_form.order_line.new() as line:
  134. line.product_id = self.complex_product
  135. line.price_unit = 1
  136. line.product_uom_qty = 1
  137. with so_form.order_line.new() as line:
  138. line.product_id = self.finished_product
  139. line.price_unit = 1
  140. line.product_uom_qty = 1
  141. sale_order_so0 = so_form.save()
  142. sale_order_so0.action_confirm()
  143. # Verify buttons are working as expected
  144. self.assertEqual(sale_order_so0.mrp_production_count, 2, "User should see the correct number of manufacture orders in smart button")
  145. pickings = sale_order_so0.picking_ids
  146. # One delivery...
  147. self.assertEqual(len(pickings), 1)
  148. # ...with two products
  149. self.assertEqual(len(pickings[0].move_ids), 2)
  150. def test_post_prod_location_child_of_stock_location(self):
  151. """
  152. 3-steps manufacturing, the post-prod location is a child of the stock
  153. location. Have a manufactured product with the manufacture route and a
  154. RR min=max=0. Confirm a SO with that product -> It should generate a MO
  155. """
  156. warehouse = self.env['stock.warehouse'].search([('company_id', '=', self.env.company.id)], limit=1)
  157. manufacture_route = warehouse.manufacture_pull_id.route_id
  158. warehouse.manufacture_steps = 'pbm_sam'
  159. warehouse.sam_loc_id.location_id = warehouse.lot_stock_id
  160. product, component = self.env['product.product'].create([{
  161. 'name': 'Finished',
  162. 'type': 'product',
  163. 'route_ids': [(6, 0, manufacture_route.ids)],
  164. }, {
  165. 'name': 'Component',
  166. 'type': 'consu',
  167. }])
  168. self.env['mrp.bom'].create({
  169. 'product_id': product.id,
  170. 'product_tmpl_id': product.product_tmpl_id.id,
  171. 'product_uom_id': product.uom_id.id,
  172. 'product_qty': 1.0,
  173. 'type': 'normal',
  174. 'bom_line_ids': [
  175. (0, 0, {'product_id': component.id, 'product_qty': 1}),
  176. ],
  177. })
  178. self.env['stock.warehouse.orderpoint'].create({
  179. 'name': product.name,
  180. 'location_id': warehouse.lot_stock_id.id,
  181. 'product_id': product.id,
  182. 'product_min_qty': 0,
  183. 'product_max_qty': 0,
  184. 'trigger': 'auto',
  185. })
  186. so = self.env['sale.order'].create({
  187. 'partner_id': self.env['res.partner'].create({'name': 'Super Partner'}).id,
  188. 'order_line': [
  189. (0, 0, {
  190. 'name': product.name,
  191. 'product_id': product.id,
  192. 'product_uom_qty': 1.0,
  193. 'product_uom': product.uom_id.id,
  194. 'price_unit': 1,
  195. })],
  196. })
  197. so.action_confirm()
  198. self.assertEqual(so.state, 'sale')
  199. mo = self.env['mrp.production'].search([('product_id', '=', product.id)], order='id desc', limit=1)
  200. self.assertIn(so.name, mo.origin)
  201. def test_so_reordering_rule(self):
  202. kit_1, component_1 = self.env['product.product'].create([{
  203. 'name': n,
  204. 'type': 'product',
  205. } for n in ['Kit 1', 'Compo 1']])
  206. self.env['mrp.bom'].create([{
  207. 'product_tmpl_id': kit_1.product_tmpl_id.id,
  208. 'product_qty': 1,
  209. 'type': 'phantom',
  210. 'bom_line_ids': [
  211. (0, 0, {'product_id': component_1.id, 'product_qty': 1}),
  212. ],
  213. }])
  214. customer = self.env['res.partner'].create({
  215. 'name': 'customer',
  216. })
  217. so = self.env['sale.order'].create({
  218. 'partner_id': customer.id,
  219. 'order_line': [
  220. (0, 0, {
  221. 'product_id': kit_1.id,
  222. 'product_uom_qty': 1.0,
  223. })],
  224. })
  225. so.action_confirm()
  226. self.env['stock.warehouse.orderpoint']._get_orderpoint_action()
  227. orderpoint_product = self.env['stock.warehouse.orderpoint'].search(
  228. [('product_id', '=', kit_1.id)])
  229. self.assertFalse(orderpoint_product)