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.

172 lines
5.7 KiB

  1. # Copyright (C) 2018 - Today: GRAP (http://www.grap.coop)
  2. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import fields
  5. from odoo.tests.common import TransactionCase
  6. from odoo.exceptions import UserError
  7. class TestModule(TransactionCase):
  8. """Tests for 'Point of Sale - Change Payment' Module"""
  9. def setUp(self):
  10. super().setUp()
  11. self.PosSession = self.env["pos.session"]
  12. self.PosOrder = self.env["pos.order"]
  13. self.AccountJournal = self.env["account.journal"]
  14. self.PosMakePayment = self.env['pos.make.payment']
  15. self.PosPaymentChangeWizard = self.env["pos.payment.change.wizard"]
  16. self.PosPaymentChangeWizardNewLine = self.env[
  17. "pos.payment.change.wizard.new.line"
  18. ]
  19. self.product = self.env.ref("product.product_product_3")
  20. self.pos_config = self.env.ref("point_of_sale.pos_config_main").copy()
  21. def _initialize_journals_open_session(self):
  22. self.check_journal = self.AccountJournal.create({
  23. "name": "Demo Check Journal",
  24. "type": "bank",
  25. "journal_user": True,
  26. })
  27. self.cash_journal = self.AccountJournal.create({
  28. "name": "Demo Cash Journal",
  29. "type": "cash",
  30. "journal_user": True,
  31. })
  32. # create new session and open it
  33. self.pos_config.journal_ids = [
  34. self.check_journal.id,
  35. self.cash_journal.id,
  36. ]
  37. self.pos_config.open_session_cb()
  38. self.session = self.pos_config.current_session_id
  39. self.check_statement = self.session.statement_ids.filtered(
  40. lambda x: x.journal_id == self.check_journal
  41. )
  42. self.cash_statement = self.session.statement_ids.filtered(
  43. lambda x: x.journal_id == self.cash_journal
  44. )
  45. def _sale(self, journal_1, price_1, journal_2=False, price_2=0.0):
  46. price = price_1 + price_2
  47. line_vals = {
  48. "name": "OL/0001",
  49. "product_id": self.product.id,
  50. "qty": 1.0,
  51. "price_unit": price,
  52. "price_subtotal": price,
  53. "price_subtotal_incl": price,
  54. }
  55. order = self.PosOrder.create({
  56. "session_id": self.session.id,
  57. "amount_tax": 0,
  58. "amount_total": price,
  59. "amount_paid": price,
  60. "amount_return": 0,
  61. "lines": [[0, False, line_vals]],
  62. })
  63. order.add_payment({
  64. 'amount': price_1,
  65. 'payment_date': fields.Date.today(),
  66. 'payment_name': "Demo",
  67. 'journal': journal_1.id,
  68. })
  69. if journal_2:
  70. order.add_payment({
  71. 'amount': price_2,
  72. 'payment_date': fields.Date.today(),
  73. 'payment_name': "Demo",
  74. 'journal': journal_2.id,
  75. })
  76. order.action_pos_order_paid()
  77. return order
  78. def _change_payment(
  79. self, order, journal_1, amount_1, journal_2=False, amount_2=0.0
  80. ):
  81. # Switch to check journal
  82. wizard = self.PosPaymentChangeWizard.with_context(
  83. active_id=order.id
  84. ).create({})
  85. self.PosPaymentChangeWizardNewLine.with_context(
  86. active_id=order.id
  87. ).create(
  88. {
  89. "wizard_id": wizard.id,
  90. "new_journal_id": journal_1.id,
  91. "amount": amount_1,
  92. }
  93. )
  94. if journal_2:
  95. self.PosPaymentChangeWizardNewLine.with_context(
  96. active_id=order.id
  97. ).create(
  98. {
  99. "wizard_id": wizard.id,
  100. "new_journal_id": journal_2.id,
  101. "amount": amount_2,
  102. }
  103. )
  104. wizard.button_change_payment()
  105. # Test Section
  106. def test_01_payment_change_policy_update(self):
  107. self.pos_config.payment_change_policy = "update"
  108. self._initialize_journals_open_session()
  109. # Make a sale with 35 in cash journal and 65 in check
  110. order = self._sale(self.cash_journal, 35, self.check_journal, 65)
  111. order_qty = len(self.PosOrder.search([]))
  112. with self.assertRaises(UserError):
  113. # Should not work if total is not correct
  114. self._change_payment(
  115. order, self.cash_journal, 10, self.check_journal, 10)
  116. self._change_payment(
  117. order, self.cash_journal, 10, self.check_journal, 90)
  118. # check Session
  119. self.assertEqual(
  120. self.cash_statement.balance_end,
  121. 10,
  122. "Bad recompute of the balance for the statement cash",
  123. )
  124. self.assertEqual(
  125. self.check_statement.balance_end,
  126. 90,
  127. "Bad recompute of the balance for the statement check",
  128. )
  129. # Check Order quantity
  130. self.assertEqual(
  131. order_qty,
  132. len(self.PosOrder.search([])),
  133. "In 'Update' mode, changing payment should not create"
  134. " other PoS Orders",
  135. )
  136. def test_02_payment_change_policy_refund(self):
  137. self.pos_config.payment_change_policy = "refund"
  138. self._initialize_journals_open_session()
  139. # Make a sale with 35 in cash journal and 65 in check
  140. order = self._sale(self.cash_journal, 35, self.check_journal, 65)
  141. order_qty = len(self.PosOrder.search([]))
  142. self._change_payment(
  143. order, self.cash_journal, 50, self.check_journal, 50)
  144. # Check Order quantity
  145. self.assertEqual(
  146. order_qty + 2,
  147. len(self.PosOrder.search([])),
  148. "In 'Refund' mode, changing payment should generate"
  149. " two new PoS Orders",
  150. )