1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
# -*- coding: utf-8 -*-
from odoo.addons.account.tests.common import AccountTestInvoicingCommon
from odoo.tests import tagged
import json
@tagged('post_install', '-at_install')
class TestAccountMovePaymentsWidget(AccountTestInvoicingCommon):
@classmethod
def setUpClass(cls, chart_template_ref=None):
super().setUpClass(chart_template_ref=chart_template_ref)
cls.receivable_account = cls.company_data['default_account_receivable']
cls.payable_account = cls.company_data['default_account_payable']
cls.currency_data_2 = cls.setup_multi_currency_data(default_values={
'name': 'Stars',
'symbol': '☆',
'currency_unit_label': 'Stars',
'currency_subunit_label': 'Little Stars',
}, rate2016=6.0, rate2017=4.0)
cls.curr_1 = cls.company_data['currency']
cls.curr_2 = cls.currency_data['currency']
cls.curr_3 = cls.currency_data_2['currency']
cls.payment_2016_curr_1 = cls.env['account.move'].create({
'date': '2016-01-01',
'line_ids': [
(0, 0, {'debit': 0.0, 'credit': 500.0, 'amount_currency': -500.0, 'currency_id': cls.curr_1.id, 'account_id': cls.receivable_account.id, 'partner_id': cls.partner_a.id}),
(0, 0, {'debit': 500.0, 'credit': 0.0, 'amount_currency': 500.0, 'currency_id': cls.curr_1.id, 'account_id': cls.payable_account.id, 'partner_id': cls.partner_a.id}),
],
})
cls.payment_2016_curr_1.action_post()
cls.payment_2016_curr_2 = cls.env['account.move'].create({
'date': '2016-01-01',
'line_ids': [
(0, 0, {'debit': 0.0, 'credit': 500.0, 'amount_currency': -1550.0, 'currency_id': cls.curr_2.id, 'account_id': cls.receivable_account.id, 'partner_id': cls.partner_a.id}),
(0, 0, {'debit': 500.0, 'credit': 0.0, 'amount_currency': 1550.0, 'currency_id': cls.curr_2.id, 'account_id': cls.payable_account.id, 'partner_id': cls.partner_a.id}),
],
})
cls.payment_2016_curr_2.action_post()
cls.payment_2017_curr_2 = cls.env['account.move'].create({
'date': '2017-01-01',
'line_ids': [
(0, 0, {'debit': 0.0, 'credit': 500.0, 'amount_currency': -950.0, 'currency_id': cls.curr_2.id, 'account_id': cls.receivable_account.id, 'partner_id': cls.partner_a.id}),
(0, 0, {'debit': 500.0, 'credit': 0.0, 'amount_currency': 950.0, 'currency_id': cls.curr_2.id, 'account_id': cls.payable_account.id, 'partner_id': cls.partner_a.id}),
],
})
cls.payment_2017_curr_2.action_post()
cls.payment_2016_curr_3 = cls.env['account.move'].create({
'date': '2016-01-01',
'line_ids': [
(0, 0, {'debit': 0.0, 'credit': 500.0, 'amount_currency': -3050.0, 'currency_id': cls.curr_3.id, 'account_id': cls.receivable_account.id, 'partner_id': cls.partner_a.id}),
(0, 0, {'debit': 500.0, 'credit': 0.0, 'amount_currency': 3050.0, 'currency_id': cls.curr_3.id, 'account_id': cls.payable_account.id, 'partner_id': cls.partner_a.id}),
],
})
cls.payment_2016_curr_3.action_post()
cls.payment_2017_curr_3 = cls.env['account.move'].create({
'date': '2017-01-01',
'line_ids': [
(0, 0, {'debit': 0.0, 'credit': 500.0, 'amount_currency': -1950.0, 'currency_id': cls.curr_3.id, 'account_id': cls.receivable_account.id, 'partner_id': cls.partner_a.id}),
(0, 0, {'debit': 500.0, 'credit': 0.0, 'amount_currency': 1950.0, 'currency_id': cls.curr_3.id, 'account_id': cls.payable_account.id, 'partner_id': cls.partner_a.id}),
],
})
cls.payment_2017_curr_3.action_post()
# -------------------------------------------------------------------------
# HELPERS
# -------------------------------------------------------------------------
def _test_all_outstanding_payments(self, invoice, expected_amounts):
''' Check the outstanding payments widget before/after the reconciliation.
:param invoice: An account.move record.
:param expected_amounts: A map <move_id> -> <amount>
'''
# Check suggested outstanding payments.
to_reconcile_payments_widget_vals = json.loads(invoice.invoice_outstanding_credits_debits_widget)
self.assertTrue(to_reconcile_payments_widget_vals)
current_amounts = {vals['move_id']: vals['amount'] for vals in to_reconcile_payments_widget_vals['content']}
self.assertDictEqual(current_amounts, expected_amounts)
# Reconcile
pay_term_lines = invoice.line_ids\
.filtered(lambda line: line.account_id.user_type_id.type in ('receivable', 'payable'))
to_reconcile = self.env['account.move'].browse(list(current_amounts.keys()))\
.line_ids\
.filtered(lambda line: line.account_id == pay_term_lines.account_id)
(pay_term_lines + to_reconcile).reconcile()
# Check payments after reconciliation.
reconciled_payments_widget_vals = json.loads(invoice.invoice_payments_widget)
self.assertTrue(reconciled_payments_widget_vals)
current_amounts = {vals['move_id']: vals['amount'] for vals in reconciled_payments_widget_vals['content']}
self.assertDictEqual(current_amounts, expected_amounts)
# -------------------------------------------------------------------------
# TESTS
# -------------------------------------------------------------------------
def test_outstanding_payments_single_currency(self):
''' Test the outstanding payments widget on invoices having the same currency
as the company one.
'''
# Customer invoice of 2500.0 in curr_1.
out_invoice = self.env['account.move'].create({
'move_type': 'out_invoice',
'date': '2017-01-01',
'invoice_date': '2017-01-01',
'partner_id': self.partner_a.id,
'currency_id': self.curr_1.id,
'invoice_line_ids': [(0, 0, {'name': '/', 'price_unit': 2500.0})],
})
out_invoice.action_post()
# Vendor bill of 2500.0 in curr_1.
in_invoice = self.env['account.move'].create({
'move_type': 'in_invoice',
'date': '2017-01-01',
'invoice_date': '2017-01-01',
'partner_id': self.partner_a.id,
'currency_id': self.curr_1.id,
'invoice_line_ids': [(0, 0, {'name': '/', 'price_unit': 2500.0})],
})
in_invoice.action_post()
expected_amounts = {
self.payment_2016_curr_1.id: 500.0,
self.payment_2016_curr_2.id: 500.0,
self.payment_2017_curr_2.id: 500.0,
self.payment_2016_curr_3.id: 500.0,
self.payment_2017_curr_3.id: 500.0,
}
self._test_all_outstanding_payments(out_invoice, expected_amounts)
self._test_all_outstanding_payments(in_invoice, expected_amounts)
def test_outstanding_payments_foreign_currency(self):
''' Test the outstanding payments widget on invoices having a foreign currency. '''
# Customer invoice of 2500.0 in curr_1.
out_invoice = self.env['account.move'].create({
'move_type': 'out_invoice',
'date': '2017-01-01',
'invoice_date': '2017-01-01',
'partner_id': self.partner_a.id,
'currency_id': self.curr_2.id,
'invoice_line_ids': [(0, 0, {'name': '/', 'price_unit': 7500.0})],
})
out_invoice.action_post()
# Vendor bill of 2500.0 in curr_1.
in_invoice = self.env['account.move'].create({
'move_type': 'in_invoice',
'date': '2017-01-01',
'invoice_date': '2017-01-01',
'partner_id': self.partner_a.id,
'currency_id': self.curr_2.id,
'invoice_line_ids': [(0, 0, {'name': '/', 'price_unit': 7500.0})],
})
in_invoice.action_post()
expected_amounts = {
self.payment_2016_curr_1.id: 1500.0,
self.payment_2016_curr_2.id: 1550.0,
self.payment_2017_curr_2.id: 950.0,
self.payment_2016_curr_3.id: 1500.0,
self.payment_2017_curr_3.id: 1000.0,
}
self._test_all_outstanding_payments(out_invoice, expected_amounts)
self._test_all_outstanding_payments(in_invoice, expected_amounts)
|