Debian Codegraph @

addons/l10n_latam_check/tests/test_third_party_checks.py

raw
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3 from freezegun import freeze_time
4 from unittest.mock import patch
5 from datetime import datetime, timedelta
6
7 from odoo.addons.l10n_latam_check.tests.common import L10nLatamCheckTest
8 from odoo.exceptions import ValidationError, UserError
9 from odoo.tests.common import tagged
10 from odoo import fields, Command
11
12
13 @tagged('post_install_l10n', 'post_install', '-at_install')
14 class TestThirdChecks(L10nLatamCheckTest):
15
16 def create_third_party_check(self, journal=False, check_numbers=['00000001', '00000002']):
17 if not journal:
18 journal = self.third_party_check_journal
19 vals = {
20 'partner_id': self.partner_a.id,
21 'payment_type': 'inbound',
22 'journal_id': journal.id,
23 'l10n_latam_new_check_ids': [
24 Command.create({'name': check_numbers[0], 'payment_date': fields.Date.add(fields.Date.today(), months=1), 'amount': 1}),
25 Command.create({'name': check_numbers[1], 'payment_date': fields.Date.add(fields.Date.today(), months=1), 'amount': 1}),
26 ],
27 'payment_method_line_id': journal._get_available_payment_method_lines('inbound').filtered(lambda x: x.code == 'new_third_party_checks').id,
28 }
29
30 payment = self.env['account.payment'].create(vals)
31 payment.action_post()
32 return payment
33
34 def test_01_get_paid_with_multiple_checks(self):
35 """ This a generic test to check that we are able to pay with checks
36 We pay directly with multiple checks instead of just one check, just to ensure the create multi
37 is properly working. """
38 payment = self.create_third_party_check()
39
40 self.assertEqual(len(payment.l10n_latam_new_check_ids), 2, 'Checks where not created properly')
41 self.assertRecordValues(payment.l10n_latam_new_check_ids, [{
42 'current_journal_id': self.third_party_check_journal.id,
43 }]*2)
44
45 # delivery (assert) dd un cheque tmb un return (assert) y un claim (assert)
46 def test_02_third_party_check_delivery(self):
47 payment = self.create_third_party_check()
48 check = payment.l10n_latam_new_check_ids[0]
49 # Check Delivery
50 vals = {
51 'l10n_latam_move_check_ids': [Command.set([check.id])],
52 'partner_id': self.partner_a.id,
53 'payment_type': 'outbound',
54 'journal_id': self.third_party_check_journal.id,
55 'payment_method_line_id': self.third_party_check_journal._get_available_payment_method_lines('outbound').filtered(lambda x: x.code in ('out_third_party_checks', 'return_third_party_checks')).id,
56 }
57 delivery = self.env['account.payment'].create(vals)
58 delivery.action_post()
59 self.assertFalse(check.current_journal_id, 'Current journal was not computed properly on delivery')
60 # check dont delivery twice
61 with self.assertRaisesRegex(ValidationError, "it seems it has been moved by another payment"):
62 self.env['account.payment'].create(vals).action_post()
63
64 # Check Return / Rejection
65 vals = {
66 'l10n_latam_move_check_ids': [Command.set([check.id])],
67 'amount': 1,
68 'partner_id': self.partner_a.id,
69 'payment_type': 'inbound',
70 'journal_id': self.rejected_check_journal.id,
71 'payment_method_line_id': self.rejected_check_journal._get_available_payment_method_lines('inbound').filtered(lambda x: x.code == 'in_third_party_checks').id,
72 }
73 supplier_return = self.env['account.payment'].create(vals)
74 supplier_return.action_post()
75 self.assertEqual(check.current_journal_id, self.rejected_check_journal, 'Current journal was not computed properly on return')
76 # check dont return twice
77 with self.assertRaisesRegex(ValidationError, "Some checks are already in hand and can't be received again"):
78 self.env['account.payment'].create(vals).action_post()
79
80 # Check Claim/Return to customer
81 vals = {
82 'l10n_latam_move_check_ids': [Command.set([check.id])],
83 'partner_id': self.partner_a.id,
84 'payment_type': 'outbound',
85 'journal_id': self.rejected_check_journal.id,
86 'payment_method_line_id': self.rejected_check_journal._get_available_payment_method_lines('outbound').filtered(lambda x: x.code in ('out_third_party_checks', 'return_third_party_checks')).id,
87 }
88 customer_return = self.env['account.payment'].create(vals)
89 customer_return.action_post()
90 self.assertFalse(check.current_journal_id, 'Current journal was not computed properly on customer return')
91 # check dont claim twice
92 with self.assertRaisesRegex(ValidationError, "Some checks are not anymore in journal,"):
93 self.env['account.payment'].create(vals).action_post()
94
95 operations = self.env['account.payment'].search([('l10n_latam_move_check_ids', '=', check.id), ('state', '!=', 'draft')], order="date desc, id desc")
96 self.assertEqual(len(operations), 3, 'There should be 3 operations on the check')
97 self.assertEqual(operations, customer_return | supplier_return | delivery)
98
99 def test_03_deposit(self):
100 payment = self.create_third_party_check()
101 check = payment.l10n_latam_new_check_ids[0]
102 bank_journal = self.company_data_3['default_journal_bank']
103
104 # Deposit the check to the bank
105 self.env['l10n_latam.payment.mass.transfer'].with_context(
106 active_model='l10n_latam.check', active_ids=[check.id]
107 ).create({
108 'destination_journal_id': bank_journal.id,
109 })._create_payments()
110 self.assertEqual(check.current_journal_id.id, bank_journal.id, 'Current journal was not computed properly on delivery')
111 self.assertEqual(len(check.operation_ids + payment), 3, 'Check that all three payments were created')
112
113 # If the bank tells you that the check has been rejected you have to do a new transfer of the previous check
114 self.env['l10n_latam.payment.mass.transfer'].with_context(
115 active_model='l10n_latam.check', active_ids=[check.id]
116 ).create({
117 'destination_journal_id': self.rejected_check_journal.id,
118 })._create_payments()
119 self.assertEqual(check.current_journal_id.id, self.rejected_check_journal.id, 'Current journal was not computed properly on delivery')
120 self.assertEqual(len(check.operation_ids + payment), 5, 'Check that all five payments were created')
121
122 # Sent back to customer (with payment) - check if we can use the check
123 self.env['account.payment'].create({
124 'partner_id': self.partner_a.id,
125 'payment_type': 'outbound',
126 'journal_id': self.rejected_check_journal.id,
127 'l10n_latam_move_check_ids': [Command.set([check.id])],
128 'payment_method_line_id': self.rejected_check_journal._get_available_payment_method_lines('inbound').filtered(lambda x: x.code == 'new_third_party_checks').id,
129 }).action_post()
130
131 def test_04_check_transfer(self):
132 """ Test transfer between third party checks journals """
133 payment = self.create_third_party_check()
134 check = payment.l10n_latam_new_check_ids[0]
135
136 # Transfer to rejected checks journal (usually is to another third party checks journal, but for test purpose is the same)
137 self.env['l10n_latam.payment.mass.transfer'].with_context(
138 active_model='l10n_latam.check', active_ids=[check.id]).create({'destination_journal_id': self.rejected_check_journal.id})._create_payments()
139 self.assertEqual(check.current_journal_id, self.rejected_check_journal, 'Current journal was not computed properly on delivery')
140
141 # test that checks created on different journals but that are on same current journal, can be transfered together
142 payment2 = self.create_third_party_check(journal=self.rejected_check_journal)
143 check2 = payment2.l10n_latam_new_check_ids[0]
144 self.env['l10n_latam.payment.mass.transfer'].with_context(
145 active_model='l10n_latam.check', active_ids=[check.id, check2.id]).create({'destination_journal_id': self.third_party_check_journal.id})._create_payments()
146
147 def test_05_check_current_journal_with_both_operations(self):
148 # -------------------------------
149 # Case 1: inbound first, then outbound
150 # -------------------------------
151 inbound_payment = self.create_third_party_check()
152 check = inbound_payment.l10n_latam_new_check_ids[0]
153
154 # Check should be on hand after receiving
155 self.assertEqual(
156 check.current_journal_id,
157 self.third_party_check_journal,
158 "Check should be available after inbound operation"
159 )
160
161 # Create outbound payment and consume check
162 outbound_payment = self.env['account.payment'].create({
163 'l10n_latam_move_check_ids': [Command.set([check.id])],
164 'partner_id': self.partner_a.id,
165 'payment_type': 'outbound',
166 'journal_id': self.third_party_check_journal.id,
167 'payment_method_line_id': self.third_party_check_journal
168 ._get_available_payment_method_lines('outbound')
169 .filtered(lambda x: x.code == 'out_third_party_checks').id,
170 })
171 outbound_payment.action_post()
172
173 # Check should not be on hand after both operations
174 self.assertFalse(
175 check.current_journal_id,
176 "Check with both inbound and outbound operations should not have current_journal_id set"
177 )
178
179 # -------------------------------
180 # Case 2: outbound first, then inbound
181 # -------------------------------
182 first_now = datetime(2023, 11, 6, 8, 0, 0)
183 second_now = first_now + timedelta(seconds=1)
184
185 # Outbound creation with fixed now
186 with patch.object(self.env.cr, 'now', lambda: first_now), freeze_time(first_now):
187 outbound_payment_2 = self.env['account.payment'].create({
188 'partner_id': self.partner_a.id,
189 'payment_type': 'outbound',
190 'journal_id': self.third_party_check_journal.id,
191 'payment_method_line_id': self.third_party_check_journal
192 ._get_available_payment_method_lines('outbound')
193 .filtered(lambda x: x.code == 'out_third_party_checks').id,
194 })
195
196 # Inbound creation with slightly later now
197 with patch.object(self.env.cr, 'now', lambda: second_now), freeze_time(second_now):
198 inbound_payment_2 = self.create_third_party_check()
199 check_2 = inbound_payment_2.l10n_latam_new_check_ids[0]
200
201 # Link check to outbound afterwards
202 outbound_payment_2.write({'l10n_latam_move_check_ids': [Command.set([check_2.id])]})
203 outbound_payment_2.action_post()
204
205 # Check should also not be on hand in this order
206 self.assertFalse(
207 check_2.current_journal_id,
208 "Check should not be on hand even if outbound was created before inbound"
209 )