test_onchange_product_barcode.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 OpenSynergy Indonesia
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from openerp.tests.common import TransactionCase
  5. from openerp.exceptions import Warning as UserError
  6. class TestOnchangeProductBarcode(TransactionCase):
  7. def setUp(self, *args, **kwargs):
  8. super(TestOnchangeProductBarcode, self).setUp(*args, **kwargs)
  9. # Objects
  10. self.wiz = self.env['sale.order.barcode']
  11. self.obj_sale_order_line =\
  12. self.env['sale.order.line']
  13. # Data
  14. self.data_so = self.env['sale.order'].create({
  15. 'partner_id': self.env.ref('base.res_partner_1').id,
  16. })
  17. self.product_1 =\
  18. self.env.ref('product.product_product_25')
  19. self.product_2 =\
  20. self.env.ref('product.product_product_30')
  21. self.product_3 =\
  22. self.env.ref('product.product_product_3')
  23. self.uom_unit =\
  24. self.env.ref('product.product_uom_unit')
  25. self.uom_dozen =\
  26. self.env.ref('product.product_uom_dozen')
  27. def test_error_product_no_ean_13(self):
  28. new = self.wiz.with_context(
  29. active_model="sale.order",
  30. active_id=self.data_so.id
  31. ).new()
  32. new.product_barcode = 0000000000000
  33. res = new.product_barcode_onchange()
  34. self.assertEquals(
  35. 'No product found with this code as '
  36. 'EAN13 or product is not for sale. You should select '
  37. 'the right product manually.',
  38. res['warning']['message']
  39. )
  40. def test_error_product_duplicate_ean(self):
  41. new = self.wiz.with_context(
  42. active_model="sale.order",
  43. active_id=self.data_so.id
  44. ).new()
  45. self.product_1.ean13 = 8998989300155
  46. self.product_2.ean13 = 8998989300155
  47. new.product_barcode = 8998989300155
  48. res = new.product_barcode_onchange()
  49. self.assertIsNotNone(
  50. res['warning']['message']
  51. )
  52. def test_error_more_than_1_line(self):
  53. order_line = [
  54. (0, 0, {
  55. 'product_id': self.product_1.id,
  56. 'product_uom_qty': 5,
  57. }),
  58. (0, 0, {
  59. 'product_id': self.product_1.id,
  60. 'product_uom_qty': 1,
  61. }),
  62. ]
  63. self.data_so.update({
  64. 'order_line': order_line
  65. })
  66. new = self.wiz.with_context(
  67. active_model="sale.order",
  68. active_id=self.data_so.id
  69. ).new()
  70. self.product_1.ean13 = 8998989300155
  71. new.product_barcode = 8998989300155
  72. new.product_barcode_onchange()
  73. new.product_id_onchange()
  74. new.product_qty = 20.0
  75. msg = ("More than 1 line found")
  76. with self.assertRaises(UserError) as error:
  77. new.save()
  78. self.assertEqual(error.exception.message, msg)
  79. def test_product_barcode_condition_1(self):
  80. # CONDITION
  81. # 1. type = INSERT
  82. # 2. Uom Lines = Uom Wizard
  83. # 3. Using single product
  84. self.product_1.ean13 = 8998989300155
  85. new = self.wiz.with_context(
  86. active_model="sale.order",
  87. active_id=self.data_so.id
  88. ).new()
  89. new.type = 'insert'
  90. new.product_barcode = 8998989300155
  91. new.product_barcode_onchange()
  92. new.product_id_onchange()
  93. new.product_qty = 5.0
  94. new.save()
  95. new.type = 'insert'
  96. new.product_barcode = 8998989300155
  97. new.product_barcode_onchange()
  98. new.product_id_onchange()
  99. new.product_qty = 9.0
  100. new.save()
  101. line_1 = self.obj_sale_order_line.search([
  102. ('order_id', '=', self.data_so.id),
  103. ('product_id', '=', self.product_1.id)
  104. ])
  105. self.assertEqual(line_1.product_uom_qty, 14.0)
  106. def test_product_barcode_condition_2(self):
  107. # CONDITION
  108. # 1. type = INSERT
  109. # 2. Uom Lines != Uom Wizard
  110. # 3. Using single product
  111. self.product_2.ean13 = 8996001600382
  112. new = self.wiz.with_context(
  113. active_model="sale.order",
  114. active_id=self.data_so.id
  115. ).new()
  116. new.type = 'insert'
  117. new.product_barcode = 8996001600382
  118. new.product_barcode_onchange()
  119. new.product_id_onchange()
  120. new.product_qty = 1.0
  121. new.save()
  122. new.type = 'insert'
  123. new.product_barcode = 8996001600382
  124. new.product_barcode_onchange()
  125. new.product_id_onchange()
  126. new.product_qty = 2.0
  127. new.product_uom_id = self.uom_dozen.id
  128. new.save()
  129. line_1 = self.obj_sale_order_line.search([
  130. ('order_id', '=', self.data_so.id),
  131. ('product_id', '=', self.product_2.id)
  132. ])
  133. self.assertEqual(line_1.product_uom_qty, 25.0)
  134. def test_product_barcode_condition_3(self):
  135. # CONDITION
  136. # 1. type = UPDATE
  137. # 2. Uom Lines = Uom Wizard
  138. # 3. Using single product
  139. self.product_1.ean13 = 8998989300155
  140. new = self.wiz.with_context(
  141. active_model="sale.order",
  142. active_id=self.data_so.id
  143. ).new()
  144. new.type = 'insert'
  145. new.product_barcode = 8998989300155
  146. new.product_barcode_onchange()
  147. new.product_id_onchange()
  148. new.product_qty = 1.0
  149. new.save()
  150. new.type = 'update'
  151. new.product_barcode = 8998989300155
  152. new.product_barcode_onchange()
  153. new.product_id_onchange()
  154. new.product_qty = 2.0
  155. new.save()
  156. line_1 = self.obj_sale_order_line.search([
  157. ('order_id', '=', self.data_so.id),
  158. ('product_id', '=', self.product_1.id)
  159. ])
  160. self.assertEqual(line_1.product_uom_qty, 2.0)
  161. def test_product_barcode_condition_4(self):
  162. # CONDITION
  163. # 1. type = UPDATE
  164. # 2. Uom Lines != Uom Wizard
  165. # 3. Using single product
  166. self.product_3.ean13 = 8998666000903
  167. new = self.wiz.with_context(
  168. active_model="sale.order",
  169. active_id=self.data_so.id
  170. ).new()
  171. new.type = 'insert'
  172. new.product_barcode = 8998666000903
  173. new.product_barcode_onchange()
  174. new.product_id_onchange()
  175. new.product_qty = 4.0
  176. new.save()
  177. new.type = 'update'
  178. new.product_barcode = 8998666000903
  179. new.product_barcode_onchange()
  180. new.product_id_onchange()
  181. new.product_qty = 1.0
  182. new.product_uom_id = self.uom_dozen.id
  183. new.save()
  184. line_1 = self.obj_sale_order_line.search([
  185. ('order_id', '=', self.data_so.id),
  186. ('product_id', '=', self.product_3.id)
  187. ])
  188. self.assertEqual(line_1.product_uom_qty, 12.0)
  189. def test_product_barcode_condition_5(self):
  190. # CONDITION
  191. # 1. type = UPDATE & INSERT
  192. # 2. Uom Lines != Uom Wizard & Uom Lines = Uom Wizard
  193. # 3. Using multiple product
  194. self.product_1.ean13 = 8998989300155
  195. self.product_2.ean13 = 8996001600382
  196. self.product_3.ean13 = 8998666000903
  197. new = self.wiz.with_context(
  198. active_model="sale.order",
  199. active_id=self.data_so.id
  200. ).new()
  201. new.type = 'insert'
  202. new.product_barcode = 8998989300155
  203. new.product_barcode_onchange()
  204. new.product_id_onchange()
  205. new.product_qty = 3.0
  206. new.save()
  207. new.type = 'insert'
  208. new.product_barcode = 8996001600382
  209. new.product_barcode_onchange()
  210. new.product_id_onchange()
  211. new.product_qty = 10.0
  212. new.save()
  213. new.type = 'insert'
  214. new.product_barcode = 8998666000903
  215. new.product_barcode_onchange()
  216. new.product_id_onchange()
  217. new.product_qty = 8.0
  218. new.save()
  219. new.type = 'update'
  220. new.product_barcode = 8998989300155
  221. new.product_barcode_onchange()
  222. new.product_id_onchange()
  223. new.product_qty = 13.0
  224. new.save()
  225. new.type = 'insert'
  226. new.product_barcode = 8996001600382
  227. new.product_barcode_onchange()
  228. new.product_id_onchange()
  229. new.product_qty = 10.0
  230. new.save()
  231. new.type = 'update'
  232. new.product_barcode = 8998666000903
  233. new.product_barcode_onchange()
  234. new.product_id_onchange()
  235. new.product_qty = 10.0
  236. new.product_uom_id = self.uom_dozen.id
  237. new.save()
  238. line_1 = self.obj_sale_order_line.search([
  239. ('order_id', '=', self.data_so.id),
  240. ('product_id', '=', self.product_1.id)
  241. ])
  242. self.assertEqual(line_1.product_uom_qty, 13.0)
  243. line_2 = self.obj_sale_order_line.search([
  244. ('order_id', '=', self.data_so.id),
  245. ('product_id', '=', self.product_2.id)
  246. ])
  247. self.assertEqual(line_2.product_uom_qty, 20.0)
  248. line_3 = self.obj_sale_order_line.search([
  249. ('order_id', '=', self.data_so.id),
  250. ('product_id', '=', self.product_3.id)
  251. ])
  252. self.assertEqual(line_3.product_uom_qty, 120.0)