test_pos_margin.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. # -*- coding: utf-8 -*-
  2. import odoo
  3. from odoo.addons.point_of_sale.tests.common import TestPoSCommon
  4. @odoo.tests.tagged('post_install', '-at_install')
  5. class TestPosMargin(TestPoSCommon):
  6. """
  7. Test the margin computation on orders with basic configuration
  8. The tests contain the base scenarios.
  9. """
  10. def setUp(self):
  11. super(TestPosMargin, self).setUp()
  12. self.config = self.basic_config
  13. self.stock_location = self.env['stock.warehouse'].create({
  14. 'partner_id': self.env.user.partner_id.id,
  15. 'name': 'Stock location',
  16. 'code': 'WH'
  17. }).lot_stock_id
  18. self.customer_location = self.env.ref('stock.stock_location_customers')
  19. self.supplier_location = self.env.ref('stock.stock_location_suppliers')
  20. self.uom_unit = self.env.ref('uom.product_uom_unit')
  21. def test_positive_margin(self):
  22. """
  23. Test margin where it should be more than zero
  24. """
  25. product1 = self.create_product('Product 1', self.categ_basic, 10, 5)
  26. product2 = self.create_product('Product 2', self.categ_basic, 50, 30)
  27. # open a session
  28. self.open_new_session()
  29. # create orders
  30. orders = [self.create_ui_order_data([(product1, 1)]),
  31. self.create_ui_order_data([(product2, 1)]),
  32. self.create_ui_order_data([(product1, 2), (product2, 2)])]
  33. # sync orders
  34. self.env['pos.order'].create_from_ui(orders)
  35. # check margins
  36. self.assertEqual(self.pos_session.order_ids[0].margin, 5)
  37. self.assertEqual(self.pos_session.order_ids[1].margin, 20)
  38. self.assertEqual(self.pos_session.order_ids[2].margin, 50)
  39. # check margins percent
  40. self.assertEqual(self.pos_session.order_ids[0].margin_percent, 0.5)
  41. self.assertEqual(self.pos_session.order_ids[1].margin_percent, 0.4)
  42. self.assertEqual(round(self.pos_session.order_ids[2].margin_percent, 2), 0.42)
  43. # close session
  44. self.pos_session.action_pos_session_validate()
  45. def test_negative_margin(self):
  46. """
  47. Test margin where it should be less than zero
  48. """
  49. product1 = self.create_product('Product 1', self.categ_basic, 10, 15)
  50. product2 = self.create_product('Product 2', self.categ_basic, 50, 100)
  51. # open a session
  52. self.open_new_session()
  53. # create orders
  54. orders = [self.create_ui_order_data([(product1, 1)]),
  55. self.create_ui_order_data([(product2, 1)]),
  56. self.create_ui_order_data([(product1, 2), (product2, 2)])]
  57. # sync orders
  58. self.env['pos.order'].create_from_ui(orders)
  59. # check margins
  60. self.assertEqual(self.pos_session.order_ids[0].margin, -5)
  61. self.assertEqual(self.pos_session.order_ids[1].margin, -50)
  62. self.assertEqual(self.pos_session.order_ids[2].margin, -110)
  63. # check margins percent
  64. self.assertEqual(self.pos_session.order_ids[0].margin_percent, -0.5)
  65. self.assertEqual(self.pos_session.order_ids[1].margin_percent, -1)
  66. self.assertEqual(round(self.pos_session.order_ids[2].margin_percent, 2), -0.92)
  67. # close session
  68. self.pos_session.action_pos_session_validate()
  69. def test_full_margin(self):
  70. """
  71. Test margin where the product cost is always 0
  72. """
  73. product1 = self.create_product('Product 1', self.categ_basic, 10)
  74. product2 = self.create_product('Product 2', self.categ_basic, 50)
  75. # open a session
  76. self.open_new_session()
  77. # create orders
  78. orders = [self.create_ui_order_data([(product1, 1)]),
  79. self.create_ui_order_data([(product2, 1)]),
  80. self.create_ui_order_data([(product1, 2), (product2, 2)])]
  81. # sync orders
  82. self.env['pos.order'].create_from_ui(orders)
  83. # check margins
  84. self.assertEqual(self.pos_session.order_ids[0].margin, 10)
  85. self.assertEqual(self.pos_session.order_ids[1].margin, 50)
  86. self.assertEqual(self.pos_session.order_ids[2].margin, 120)
  87. # check margins percent
  88. self.assertEqual(self.pos_session.order_ids[0].margin_percent, 1)
  89. self.assertEqual(self.pos_session.order_ids[1].margin_percent, 1)
  90. self.assertEqual(self.pos_session.order_ids[2].margin_percent, 1)
  91. # close session
  92. self.pos_session.action_pos_session_validate()
  93. def test_tax_margin(self):
  94. """
  95. Test margin with tax on products
  96. Product 1 price without tax = 10
  97. Product 2 price without tax = 50
  98. """
  99. product1 = self.create_product('Product 1', self.categ_basic, 10, 5, self.taxes['tax7'].ids)
  100. product2 = self.create_product('Product 2', self.categ_basic, 55, 30, self.taxes['tax10'].ids)
  101. # open a session
  102. self.open_new_session()
  103. # create orders
  104. orders = [self.create_ui_order_data([(product1, 1)]),
  105. self.create_ui_order_data([(product2, 1)]),
  106. self.create_ui_order_data([(product1, 2), (product2, 2)])]
  107. # sync orders
  108. self.env['pos.order'].create_from_ui(orders)
  109. # check margins
  110. self.assertEqual(self.pos_session.order_ids[0].margin, 5)
  111. self.assertEqual(self.pos_session.order_ids[1].margin, 20)
  112. self.assertEqual(self.pos_session.order_ids[2].margin, 50)
  113. # check margins percent
  114. self.assertEqual(self.pos_session.order_ids[0].margin_percent, 0.5)
  115. self.assertEqual(self.pos_session.order_ids[1].margin_percent, 0.4)
  116. self.assertEqual(round(self.pos_session.order_ids[2].margin_percent, 2), 0.42)
  117. # close session
  118. self.pos_session.action_pos_session_validate()
  119. def test_other_currency_margin(self):
  120. """
  121. Test margin with tax on products and with different currency
  122. The currency rate is 0.5 so the product price is halved in this currency.
  123. """
  124. # change the config
  125. current_config = self.config
  126. self.config = self.other_currency_config
  127. # same parameters as test_positive_margin
  128. product1 = self.create_product('Product 1', self.categ_basic, 10, 5)
  129. product2 = self.create_product('Product 2', self.categ_basic, 50, 30)
  130. # open a session
  131. self.open_new_session()
  132. # create orders
  133. orders = [self.create_ui_order_data([(product1, 1)]),
  134. self.create_ui_order_data([(product2, 1)]),
  135. self.create_ui_order_data([(product1, 2), (product2, 2)])]
  136. # sync orders
  137. self.env['pos.order'].create_from_ui(orders)
  138. # check margins in the config currency
  139. self.assertEqual(self.pos_session.order_ids[0].margin, 2.5)
  140. self.assertEqual(self.pos_session.order_ids[1].margin, 10)
  141. self.assertEqual(self.pos_session.order_ids[2].margin, 25)
  142. # check margins percent which should be the same as test_positive_margin
  143. self.assertEqual(self.pos_session.order_ids[0].margin_percent, 0.5)
  144. self.assertEqual(self.pos_session.order_ids[1].margin_percent, 0.4)
  145. self.assertEqual(round(self.pos_session.order_ids[2].margin_percent, 2), 0.42)
  146. # close session
  147. self.pos_session.action_pos_session_validate()
  148. # set the config back
  149. self.config = current_config
  150. def test_tax_and_other_currency_margin(self):
  151. """
  152. Test margin with different currency between products and config with taxes.
  153. Product 1 price without tax = 10
  154. Product 2 price without tax = 50
  155. The currency rate is 0.5 so the product price is halved in this currency.
  156. """
  157. # change the config
  158. current_config = self.config
  159. self.config = self.other_currency_config
  160. product1 = self.create_product('Product 1', self.categ_basic, 10, 5, self.taxes['tax7'].ids)
  161. product2 = self.create_product('Product 2', self.categ_basic, 55, 30, self.taxes['tax10'].ids)
  162. # open a session
  163. self.open_new_session()
  164. # create orders
  165. orders = [self.create_ui_order_data([(product1, 1)]),
  166. self.create_ui_order_data([(product2, 1)]),
  167. self.create_ui_order_data([(product1, 2), (product2, 2)])]
  168. # sync orders
  169. self.env['pos.order'].create_from_ui(orders)
  170. # check margins in the config currency
  171. self.assertEqual(self.pos_session.order_ids[0].margin, 2.5)
  172. self.assertEqual(self.pos_session.order_ids[1].margin, 10)
  173. self.assertEqual(self.pos_session.order_ids[2].margin, 25)
  174. # check margins percent which should be the same as test_tax_margin
  175. self.assertEqual(self.pos_session.order_ids[0].margin_percent, 0.5)
  176. self.assertEqual(self.pos_session.order_ids[1].margin_percent, 0.4)
  177. self.assertEqual(self.pos_session.order_ids[2].margin_percent, 0.4167)
  178. # close session
  179. self.pos_session.action_pos_session_validate()
  180. # set the config back
  181. self.config = current_config
  182. def test_return_margin(self):
  183. """
  184. Test margin where we return product (negative line quantity)
  185. """
  186. product1 = self.create_product('Product 1', self.categ_basic, 10, 5)
  187. product2 = self.create_product('Product 2', self.categ_basic, 50, 30)
  188. # open a session
  189. self.open_new_session()
  190. # create orders
  191. orders = [self.create_ui_order_data([(product1, -1)]),
  192. self.create_ui_order_data([(product2, -1)]),
  193. self.create_ui_order_data([(product1, -2), (product2, -2)])]
  194. # sync orders
  195. self.env['pos.order'].create_from_ui(orders)
  196. # check margins
  197. self.assertEqual(self.pos_session.order_ids[0].margin, -5)
  198. self.assertEqual(self.pos_session.order_ids[1].margin, -20)
  199. self.assertEqual(self.pos_session.order_ids[2].margin, -50)
  200. # check margins percent
  201. self.assertEqual(self.pos_session.order_ids[0].margin_percent, 0.5)
  202. self.assertEqual(self.pos_session.order_ids[1].margin_percent, 0.4)
  203. self.assertEqual(round(self.pos_session.order_ids[2].margin_percent, 2), 0.42)
  204. # close session
  205. self.pos_session.action_pos_session_validate()
  206. def test_fifo_margin_real_time(self):
  207. """
  208. Test margin where there is product in FIFO with stock update in real time
  209. """
  210. product1 = self.create_product('Product 1', self.categ_anglo, 10, 5)
  211. product2 = self.create_product('Product 2', self.categ_basic, 50, 30)
  212. move1 = self.env['stock.move'].create({
  213. 'name': 'IN 2 unit @ 3 per unit',
  214. 'location_id': self.supplier_location.id,
  215. 'location_dest_id': self.stock_location.id,
  216. 'product_id': product1.id,
  217. 'product_uom': self.uom_unit.id,
  218. 'product_uom_qty': 2,
  219. 'price_unit': 3,
  220. }).sudo()
  221. move1._action_confirm()
  222. move1._action_assign()
  223. move1.move_line_ids.qty_done = 2
  224. move1._action_done()
  225. move2 = self.env['stock.move'].create({
  226. 'name': 'IN 1 unit @ 7 per unit',
  227. 'location_id': self.supplier_location.id,
  228. 'location_dest_id': self.stock_location.id,
  229. 'product_id': product1.id,
  230. 'product_uom': self.uom_unit.id,
  231. 'product_uom_qty': 1,
  232. 'price_unit': 7,
  233. }).sudo()
  234. move2._action_confirm()
  235. move2._action_assign()
  236. move2.move_line_ids.qty_done = 1
  237. move2._action_done()
  238. # open a session
  239. self.open_new_session()
  240. # create orders
  241. orders = [self.create_ui_order_data([(product1, 1), (product2, 1)]),
  242. self.create_ui_order_data([(product1, 2)])]
  243. # sync orders
  244. self.env['pos.order'].create_from_ui(orders)
  245. # check margins
  246. self.assertEqual(self.pos_session.order_ids[0].margin, 27)
  247. self.assertEqual(self.pos_session.order_ids[1].margin, 10)
  248. # check margins percent
  249. self.assertEqual(self.pos_session.order_ids[0].margin_percent, 0.45)
  250. self.assertEqual(self.pos_session.order_ids[1].margin_percent, 0.5)
  251. # close session
  252. self.pos_session.action_pos_session_validate()
  253. def test_avco_margin_closing_time(self):
  254. """
  255. Test margin where there is product in AVCO with stock update in closing
  256. """
  257. self.categ_anglo.property_cost_method = 'average'
  258. product1 = self.create_product('Product 1', self.categ_anglo, 10, 5)
  259. product2 = self.create_product('Product 2', self.categ_basic, 50, 30)
  260. self.env.company.point_of_sale_update_stock_quantities = 'closing'
  261. move1 = self.env['stock.move'].create({
  262. 'name': 'IN 2 unit @ 3 per unit',
  263. 'location_id': self.supplier_location.id,
  264. 'location_dest_id': self.stock_location.id,
  265. 'product_id': product1.id,
  266. 'product_uom': self.uom_unit.id,
  267. 'product_uom_qty': 2,
  268. 'price_unit': 3,
  269. }).sudo()
  270. move1._action_confirm()
  271. move1._action_assign()
  272. move1.move_line_ids.qty_done = 2
  273. move1._action_done()
  274. move2 = self.env['stock.move'].create({
  275. 'name': 'IN 1 unit @ 6 per unit',
  276. 'location_id': self.supplier_location.id,
  277. 'location_dest_id': self.stock_location.id,
  278. 'product_id': product1.id,
  279. 'product_uom': self.uom_unit.id,
  280. 'product_uom_qty': 1,
  281. 'price_unit': 6,
  282. }).sudo()
  283. move2._action_confirm()
  284. move2._action_assign()
  285. move2.move_line_ids.qty_done = 1
  286. move2._action_done()
  287. # open a session
  288. self.open_new_session()
  289. # create orders
  290. orders = [self.create_ui_order_data([(product1, 1), (product2, 1)]),
  291. self.create_ui_order_data([(product1, 2)])]
  292. # sync orders
  293. self.env['pos.order'].create_from_ui(orders)
  294. # check margins which are not really computed so it should be 0
  295. self.assertEqual(self.pos_session.order_ids[0].margin, 0)
  296. self.assertEqual(self.pos_session.order_ids[1].margin, 0)
  297. # check margins percent (same as above)
  298. self.assertEqual(self.pos_session.order_ids[1].margin_percent, 0)
  299. self.assertEqual(self.pos_session.order_ids[1].margin_percent, 0)
  300. # close session
  301. total_cash_payment = sum(self.pos_session.mapped('order_ids.payment_ids').filtered(lambda payment: payment.payment_method_id.type == 'cash').mapped('amount'))
  302. self.pos_session.post_closing_cash_details(total_cash_payment)
  303. self.pos_session.close_session_from_ui()
  304. # check margins
  305. self.assertEqual(self.pos_session.order_ids[0].margin, 26)
  306. self.assertEqual(self.pos_session.order_ids[1].margin, 12)
  307. # check margins percent
  308. self.assertEqual(self.pos_session.order_ids[0].margin_percent, 0.4333)
  309. self.assertEqual(self.pos_session.order_ids[1].margin_percent, 0.6)
  310. self.env.company.point_of_sale_update_stock_quantities = 'real'