You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
6.0 KiB

  1. # coding: utf-8
  2. # Copyright (C) 2018 - Today: GRAP (http://www.grap.coop)
  3. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from openerp.tests.common import TransactionCase
  6. from openerp.exceptions import Warning as UserError
  7. class TestModule(TransactionCase):
  8. """Tests for 'Point of Sale - Change Payment' Module"""
  9. def setUp(self):
  10. super(TestModule, self).setUp()
  11. self.PosSession = self.env['pos.session']
  12. self.PosOrder = self.env['pos.order']
  13. self.PosMakePayment = self.env['pos.make.payment']
  14. self.PosSwitchJournalWizard = self.env['pos.switch.journal.wizard']
  15. self.PosChangePaymentsWizard = self.env['pos.change.payments.wizard']
  16. self.PosChangePaymentsWizardLine =\
  17. self.env['pos.change.payments.wizard.line']
  18. self.product = self.env.ref('product.product_product_3')
  19. self.pos_config = self.env.ref('point_of_sale.pos_config_main')
  20. self.check_journal = self.env.ref('account.check_journal')
  21. self.cash_journal = self.env.ref('account.cash_journal')
  22. # create new session and open it
  23. self.session = self.PosSession.create(
  24. {'config_id': self.pos_config.id})
  25. self.session.open_cb()
  26. self.check_statement = self.session.statement_ids.filtered(
  27. lambda x: x.journal_id == self.check_journal)
  28. self.cash_statement = self.session.statement_ids.filtered(
  29. lambda x: x.journal_id == self.cash_journal)
  30. def _sale(self, session, journal_1, price_1, journal_2=False, price_2=0.0):
  31. order = self.PosOrder.create({
  32. 'session_id': session.id,
  33. 'lines': [[0, False, {
  34. 'name': 'OL/0001',
  35. 'product_id': self.product.id,
  36. 'qty': 1.0,
  37. 'price_unit': price_1 + price_2,
  38. }]],
  39. })
  40. payment = self.PosMakePayment.with_context(active_id=order.id).create({
  41. 'journal_id': journal_1.id,
  42. 'amount': price_1,
  43. })
  44. payment.with_context(active_id=order.id).check()
  45. if journal_2:
  46. payment = self.PosMakePayment.with_context(
  47. active_id=order.id).create({
  48. 'journal_id': journal_2.id,
  49. 'amount': price_2,
  50. })
  51. payment.with_context(active_id=order.id).check()
  52. return order
  53. # Test Section
  54. def test_01_pos_switch_journal(self):
  55. # Make a sale with 100 in cash journal
  56. order = self._sale(self.session, self.cash_journal, 100)
  57. statement_line = order.statement_ids[0]
  58. # Switch to check journal
  59. wizard = self.PosSwitchJournalWizard.with_context(
  60. active_id=statement_line.id).create({
  61. 'new_journal_id': self.check_journal.id,
  62. })
  63. wizard.button_switch_journal()
  64. # Check Order
  65. self.assertEqual(
  66. len(order.statement_ids.filtered(
  67. lambda x: x.journal_id == self.cash_journal)), 0,
  68. "Altered order should not have the original payment journal")
  69. self.assertEqual(
  70. len(order.statement_ids.filtered(
  71. lambda x: x.journal_id == self.check_journal)), 1,
  72. "Altered order should have the final payment journal")
  73. # Check Session
  74. self.assertEqual(
  75. self.cash_statement.balance_end, 0,
  76. "Bad recompute of the balance for the old statement")
  77. self.assertEqual(
  78. self.check_statement.balance_end, 100,
  79. "Bad recompute of the balance for the new statement")
  80. def test_02_pos_change_payment(self):
  81. # Make a sale with 35 in cash journal and 65 in check
  82. order = self._sale(
  83. self.session, self.cash_journal, 35, self.check_journal, 65)
  84. # Switch to check journal
  85. wizard = self.PosChangePaymentsWizard.with_context(
  86. active_id=order.id).create({})
  87. self.PosChangePaymentsWizardLine.with_context(
  88. active_id=order.id).create({
  89. 'wizard_id': wizard.id,
  90. 'new_journal_id': self.cash_journal.id,
  91. 'amount': 10,
  92. })
  93. self.PosChangePaymentsWizardLine.with_context(
  94. active_id=order.id).create({
  95. 'wizard_id': wizard.id,
  96. 'new_journal_id': self.check_journal.id,
  97. 'amount': 40,
  98. })
  99. with self.assertRaises(UserError):
  100. # Should not work if total is not correct
  101. wizard.button_change_payments()
  102. # Finish payement
  103. self.PosChangePaymentsWizardLine.with_context(
  104. active_id=order.id).create({
  105. 'wizard_id': wizard.id,
  106. 'new_journal_id': self.check_journal.id,
  107. 'amount': 50,
  108. })
  109. wizard.button_change_payments()
  110. # check Session
  111. self.assertEqual(
  112. self.cash_statement.balance_end, 10,
  113. "Bad recompute of the balance for the old statement")
  114. self.assertEqual(
  115. self.check_statement.balance_end, 90,
  116. "Bad recompute of the balance for the new statement")
  117. def test_03_merge_statement(self):
  118. # Make a sale with multiple cash payement
  119. order = self._sale(
  120. self.session, self.cash_journal, 100,
  121. journal_2=self.cash_journal, price_2=200)
  122. # Check that statement has been merged
  123. self.assertEqual(
  124. len(order.statement_ids), 1,
  125. "Adding many cash statement for an order should merge them.")
  126. self.assertEqual(
  127. order.statement_ids[0].amount, 300,
  128. "Invalid total amount for merged cash statements")
  129. # Make a sale with multiple check payement
  130. order = self._sale(
  131. self.session, self.check_journal, 100,
  132. self.check_journal, 200)
  133. # Check that statement has been merged
  134. self.assertEqual(
  135. len(order.statement_ids), 2,
  136. "Adding many check statement for an order should not merge them.")