common.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. # -*- coding: utf-8 -*-
  2. from odoo.tests import Form
  3. from odoo.addons.mail.tests.common import mail_new_test_user
  4. from odoo.addons.stock.tests import common2
  5. class TestMrpCommon(common2.TestStockCommon):
  6. @classmethod
  7. def generate_mo(cls, tracking_final='none', tracking_base_1='none', tracking_base_2='none', qty_final=5, qty_base_1=4, qty_base_2=1, picking_type_id=False, consumption=False):
  8. """ This function generate a manufacturing order with one final
  9. product and two consumed product. Arguments allows to choose
  10. the tracking/qty for each different products. It returns the
  11. MO, used bom and the tree products.
  12. """
  13. product_to_build = cls.env['product.product'].create({
  14. 'name': 'Young Tom',
  15. 'type': 'product',
  16. 'tracking': tracking_final,
  17. })
  18. product_to_use_1 = cls.env['product.product'].create({
  19. 'name': 'Botox',
  20. 'type': 'product',
  21. 'tracking': tracking_base_1,
  22. })
  23. product_to_use_2 = cls.env['product.product'].create({
  24. 'name': 'Old Tom',
  25. 'type': 'product',
  26. 'tracking': tracking_base_2,
  27. })
  28. bom_1 = cls.env['mrp.bom'].create({
  29. 'product_id': product_to_build.id,
  30. 'product_tmpl_id': product_to_build.product_tmpl_id.id,
  31. 'product_uom_id': cls.uom_unit.id,
  32. 'product_qty': 1.0,
  33. 'type': 'normal',
  34. 'consumption': consumption if consumption else 'flexible',
  35. 'bom_line_ids': [
  36. (0, 0, {'product_id': product_to_use_2.id, 'product_qty': qty_base_2}),
  37. (0, 0, {'product_id': product_to_use_1.id, 'product_qty': qty_base_1})
  38. ]})
  39. mo_form = Form(cls.env['mrp.production'])
  40. mo_form.product_id = product_to_build
  41. if picking_type_id:
  42. mo_form.picking_type_id = picking_type_id
  43. mo_form.bom_id = bom_1
  44. mo_form.product_qty = qty_final
  45. mo = mo_form.save()
  46. mo.action_confirm()
  47. return mo, bom_1, product_to_build, product_to_use_1, product_to_use_2
  48. @classmethod
  49. def setUpClass(cls):
  50. super(TestMrpCommon, cls).setUpClass()
  51. (
  52. cls.product_4,
  53. cls.product_5,
  54. cls.product_6,
  55. cls.product_8,
  56. ) = cls.env['product.product'].create([{
  57. 'name': 'Stick', # product_4
  58. 'uom_id': cls.uom_dozen.id,
  59. 'uom_po_id': cls.uom_dozen.id,
  60. }, {
  61. 'name': 'Stone Tools', # product_5
  62. }, {
  63. 'name': 'Door', # product_6
  64. }, {
  65. 'name': 'House', # product_8
  66. }])
  67. # Update demo products
  68. (cls.product_2 | cls.product_3 | cls.product_4 | cls.product_5 | cls.product_6 | cls.product_7_3 | cls.product_8).write({
  69. 'type': 'product',
  70. })
  71. # User Data: mrp user and mrp manager
  72. cls.user_mrp_user = mail_new_test_user(
  73. cls.env,
  74. name='Hilda Ferachwal',
  75. login='hilda',
  76. email='h.h@example.com',
  77. notification_type='inbox',
  78. groups='mrp.group_mrp_user, stock.group_stock_user, mrp.group_mrp_byproducts, uom.group_uom',
  79. )
  80. cls.user_mrp_manager = mail_new_test_user(
  81. cls.env,
  82. name='Gary Youngwomen',
  83. login='gary',
  84. email='g.g@example.com',
  85. notification_type='inbox',
  86. groups='mrp.group_mrp_manager, stock.group_stock_user, mrp.group_mrp_byproducts, uom.group_uom',
  87. )
  88. # Required for `product_uom_id` to be visible in the view
  89. # This class is used by a lot of tests which sets `product_uom_id` on `mrp.production`
  90. cls.env.user.groups_id += cls.env.ref('uom.group_uom')
  91. cls.workcenter_1 = cls.env['mrp.workcenter'].create({
  92. 'name': 'Nuclear Workcenter',
  93. 'default_capacity': 2,
  94. 'time_start': 10,
  95. 'time_stop': 5,
  96. 'time_efficiency': 80,
  97. })
  98. cls.workcenter_2 = cls.env['mrp.workcenter'].create({
  99. 'name': 'Simple Workcenter',
  100. 'default_capacity': 1,
  101. 'time_start': 0,
  102. 'time_stop': 0,
  103. 'time_efficiency': 100,
  104. })
  105. cls.workcenter_3 = cls.env['mrp.workcenter'].create({
  106. 'name': 'Double Workcenter',
  107. 'default_capacity': 2,
  108. 'time_start': 0,
  109. 'time_stop': 0,
  110. 'time_efficiency': 100,
  111. })
  112. cls.bom_1 = cls.env['mrp.bom'].create({
  113. 'product_id': cls.product_4.id,
  114. 'product_tmpl_id': cls.product_4.product_tmpl_id.id,
  115. 'product_uom_id': cls.uom_unit.id,
  116. 'product_qty': 4.0,
  117. 'consumption': 'flexible',
  118. 'operation_ids': [
  119. ],
  120. 'type': 'normal',
  121. 'bom_line_ids': [
  122. (0, 0, {'product_id': cls.product_2.id, 'product_qty': 2}),
  123. (0, 0, {'product_id': cls.product_1.id, 'product_qty': 4})
  124. ]})
  125. cls.bom_2 = cls.env['mrp.bom'].create({
  126. 'product_id': cls.product_5.id,
  127. 'product_tmpl_id': cls.product_5.product_tmpl_id.id,
  128. 'product_uom_id': cls.product_5.uom_id.id,
  129. 'consumption': 'flexible',
  130. 'product_qty': 1.0,
  131. 'operation_ids': [
  132. (0, 0, {'name': 'Gift Wrap Maching', 'workcenter_id': cls.workcenter_1.id, 'time_cycle': 15, 'sequence': 1}),
  133. ],
  134. 'type': 'phantom',
  135. 'sequence': 2,
  136. 'bom_line_ids': [
  137. (0, 0, {'product_id': cls.product_4.id, 'product_qty': 2}),
  138. (0, 0, {'product_id': cls.product_3.id, 'product_qty': 3})
  139. ]})
  140. cls.bom_3 = cls.env['mrp.bom'].create({
  141. 'product_id': cls.product_6.id,
  142. 'product_tmpl_id': cls.product_6.product_tmpl_id.id,
  143. 'product_uom_id': cls.uom_dozen.id,
  144. 'ready_to_produce': 'asap',
  145. 'consumption': 'flexible',
  146. 'product_qty': 2.0,
  147. 'operation_ids': [
  148. (0, 0, {'name': 'Cutting Machine', 'workcenter_id': cls.workcenter_1.id, 'time_cycle': 12, 'sequence': 1}),
  149. (0, 0, {'name': 'Weld Machine', 'workcenter_id': cls.workcenter_1.id, 'time_cycle': 18, 'sequence': 2}),
  150. ],
  151. 'type': 'normal',
  152. 'bom_line_ids': [
  153. (0, 0, {'product_id': cls.product_5.id, 'product_qty': 2}),
  154. (0, 0, {'product_id': cls.product_4.id, 'product_qty': 8}),
  155. (0, 0, {'product_id': cls.product_2.id, 'product_qty': 12})
  156. ]})
  157. cls.bom_4 = cls.env['mrp.bom'].create({
  158. 'product_id': cls.product_6.id,
  159. 'product_tmpl_id': cls.product_6.product_tmpl_id.id,
  160. 'consumption': 'flexible',
  161. 'product_qty': 1.0,
  162. 'operation_ids': [
  163. (0, 0, {'name': 'Rub it gently with a cloth', 'workcenter_id': cls.workcenter_2.id,
  164. 'time_mode_batch': 1, 'time_mode': "auto", 'sequence': 1}),
  165. ],
  166. 'type': 'normal',
  167. 'bom_line_ids': [
  168. (0, 0, {'product_id': cls.product_1.id, 'product_qty': 1}),
  169. ]})
  170. cls.bom_5 = cls.env['mrp.bom'].create({
  171. 'product_id': cls.product_6.id,
  172. 'product_tmpl_id': cls.product_6.product_tmpl_id.id,
  173. 'consumption': 'flexible',
  174. 'product_qty': 1.0,
  175. 'operation_ids': [
  176. (0, 0, {'name': 'Rub it gently with a cloth two at once', 'workcenter_id': cls.workcenter_3.id,
  177. 'time_mode_batch': 2, 'time_mode': "auto", 'sequence': 1}),
  178. ],
  179. 'type': 'normal',
  180. 'bom_line_ids': [
  181. (0, 0, {'product_id': cls.product_1.id, 'product_qty': 1}),
  182. ]})
  183. cls.bom_6 = cls.env['mrp.bom'].create({
  184. 'product_id': cls.product_6.id,
  185. 'product_tmpl_id': cls.product_6.product_tmpl_id.id,
  186. 'consumption': 'flexible',
  187. 'product_qty': 1.0,
  188. 'operation_ids': [
  189. (0, 0, {'name': 'Rub it gently with a cloth two at once', 'workcenter_id': cls.workcenter_3.id,
  190. 'time_mode_batch': 1, 'time_mode': "auto", 'sequence': 1}),
  191. ],
  192. 'type': 'normal',
  193. 'bom_line_ids': [
  194. (0, 0, {'product_id': cls.product_1.id, 'product_qty': 1}),
  195. ]})
  196. cls.stock_location_14 = cls.env['stock.location'].create({
  197. 'name': 'Shelf 2',
  198. 'location_id': cls.env.ref('stock.warehouse0').lot_stock_id.id,
  199. })
  200. cls.stock_location_components = cls.env['stock.location'].create({
  201. 'name': 'Shelf 1',
  202. 'location_id': cls.env.ref('stock.warehouse0').lot_stock_id.id,
  203. })
  204. cls.laptop = cls.env['product.product'].create({
  205. 'name': 'Acoustic Bloc Screens',
  206. 'uom_id': cls.env.ref("uom.product_uom_unit").id,
  207. 'uom_po_id': cls.env.ref("uom.product_uom_unit").id,
  208. 'type': 'product',
  209. 'tracking': 'none',
  210. 'categ_id': cls.env.ref('product.product_category_all').id,
  211. })
  212. cls.graphics_card = cls.env['product.product'].create({
  213. 'name': 'Individual Workplace',
  214. 'uom_id': cls.env.ref("uom.product_uom_unit").id,
  215. 'uom_po_id': cls.env.ref("uom.product_uom_unit").id,
  216. 'type': 'product',
  217. 'tracking': 'none',
  218. 'categ_id': cls.env.ref('product.product_category_all').id,
  219. })
  220. @classmethod
  221. def make_prods(cls, n):
  222. return [
  223. cls.env["product.product"].create(
  224. {"name": f"p{k + 1}", "type": "product"}
  225. )
  226. for k in range(n)
  227. ]
  228. @classmethod
  229. def make_bom(cls, p, *cs):
  230. return cls.env["mrp.bom"].create(
  231. {
  232. "product_tmpl_id": p.product_tmpl_id.id,
  233. "product_id": p.id,
  234. "product_qty": 1,
  235. "type": "phantom",
  236. "product_uom_id": cls.uom_unit.id,
  237. "bom_line_ids": [
  238. (0, 0, {
  239. "product_id": c.id,
  240. "product_qty": 1,
  241. "product_uom_id": cls.uom_unit.id
  242. })
  243. for c in cs
  244. ],
  245. }
  246. )