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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
|
# -*- coding: utf-8 -*-
from odoo.addons.account.tests.common import AccountTestInvoicingCommon
from odoo.tests import tagged, new_test_user
from odoo.tests.common import Form
from odoo import fields, api, SUPERUSER_ID
from odoo.exceptions import ValidationError, UserError, RedirectWarning
from odoo.tools import mute_logger
from dateutil.relativedelta import relativedelta
from functools import reduce
import json
import psycopg2
@tagged('post_install', '-at_install')
class TestAccountMove(AccountTestInvoicingCommon):
@classmethod
def setUpClass(cls, chart_template_ref=None):
super().setUpClass(chart_template_ref=chart_template_ref)
tax_repartition_line = cls.company_data['default_tax_sale'].refund_repartition_line_ids\
.filtered(lambda line: line.repartition_type == 'tax')
cls.test_move = cls.env['account.move'].create({
'move_type': 'entry',
'date': fields.Date.from_string('2016-01-01'),
'line_ids': [
(0, None, {
'name': 'revenue line 1',
'account_id': cls.company_data['default_account_revenue'].id,
'debit': 500.0,
'credit': 0.0,
}),
(0, None, {
'name': 'revenue line 2',
'account_id': cls.company_data['default_account_revenue'].id,
'debit': 1000.0,
'credit': 0.0,
'tax_ids': [(6, 0, cls.company_data['default_tax_sale'].ids)],
}),
(0, None, {
'name': 'tax line',
'account_id': cls.company_data['default_account_tax_sale'].id,
'debit': 150.0,
'credit': 0.0,
'tax_repartition_line_id': tax_repartition_line.id,
}),
(0, None, {
'name': 'counterpart line',
'account_id': cls.company_data['default_account_expense'].id,
'debit': 0.0,
'credit': 1650.0,
}),
]
})
def test_custom_currency_on_account_1(self):
custom_account = self.company_data['default_account_revenue'].copy()
# The currency set on the account is not the same as the one set on the company.
# It should raise an error.
custom_account.currency_id = self.currency_data['currency']
with self.assertRaises(UserError), self.cr.savepoint():
self.test_move.line_ids[0].account_id = custom_account
# The currency set on the account is the same as the one set on the company.
# It should not raise an error.
custom_account.currency_id = self.company_data['currency']
self.test_move.line_ids[0].account_id = custom_account
def test_misc_fiscalyear_lock_date_1(self):
self.test_move.action_post()
# Set the lock date after the journal entry date.
self.test_move.company_id.fiscalyear_lock_date = fields.Date.from_string('2017-01-01')
# lines[0] = 'counterpart line'
# lines[1] = 'tax line'
# lines[2] = 'revenue line 1'
# lines[3] = 'revenue line 2'
lines = self.test_move.line_ids.sorted('debit')
# Editing the reference should be allowed.
self.test_move.ref = 'whatever'
# Try to edit a line into a locked fiscal year.
with self.assertRaises(UserError), self.cr.savepoint():
self.test_move.write({
'line_ids': [
(1, lines[0].id, {'credit': lines[0].credit + 100.0}),
(1, lines[2].id, {'debit': lines[2].debit + 100.0}),
],
})
# Try to edit the account of a line.
with self.assertRaises(UserError), self.cr.savepoint():
self.test_move.line_ids[0].write({'account_id': self.test_move.line_ids[0].account_id.copy().id})
# Try to edit a line.
with self.assertRaises(UserError), self.cr.savepoint():
self.test_move.write({
'line_ids': [
(1, lines[0].id, {'credit': lines[0].credit + 100.0}),
(1, lines[3].id, {'debit': lines[3].debit + 100.0}),
],
})
# Try to add a new tax on a line.
with self.assertRaises(UserError), self.cr.savepoint():
self.test_move.write({
'line_ids': [
(1, lines[2].id, {'tax_ids': [(6, 0, self.company_data['default_tax_purchase'].ids)]}),
],
})
# Try to create a new line.
with self.assertRaises(UserError), self.cr.savepoint():
self.test_move.write({
'line_ids': [
(1, lines[0].id, {'credit': lines[0].credit + 100.0}),
(0, None, {
'name': 'revenue line 1',
'account_id': self.company_data['default_account_revenue'].id,
'debit': 100.0,
'credit': 0.0,
}),
],
})
# You can't remove the journal entry from a locked period.
with self.assertRaises(UserError), self.cr.savepoint():
self.test_move.date = fields.Date.from_string('2018-01-01')
with self.assertRaises(UserError), self.cr.savepoint():
self.test_move.unlink()
with self.assertRaises(UserError), self.cr.savepoint():
self.test_move.button_draft()
# Try to add a new journal entry prior to the lock date.
copy_move = self.test_move.copy({'date': '2017-01-01'})
# The date has been changed to the first valid date.
self.assertEqual(copy_move.date, copy_move.company_id.fiscalyear_lock_date + relativedelta(days=1))
def test_misc_fiscalyear_lock_date_2(self):
self.test_move.action_post()
# Create a bank statement to get a balance in the suspense account.
statement = self.env['account.bank.statement'].create({
'journal_id': self.company_data['default_journal_bank'].id,
'date': '2016-01-01',
'line_ids': [
(0, 0, {'payment_ref': 'test', 'amount': 10.0})
],
})
statement.button_post()
# You can't lock the fiscal year if there is some unreconciled statement.
with self.assertRaises(RedirectWarning), self.cr.savepoint():
self.test_move.company_id.fiscalyear_lock_date = fields.Date.from_string('2017-01-01')
def test_misc_tax_lock_date_1(self):
self.test_move.action_post()
# Set the tax lock date after the journal entry date.
self.test_move.company_id.tax_lock_date = fields.Date.from_string('2017-01-01')
# lines[0] = 'counterpart line'
# lines[1] = 'tax line'
# lines[2] = 'revenue line 1'
# lines[3] = 'revenue line 2'
lines = self.test_move.line_ids.sorted('debit')
# Try to edit a line not affecting the taxes.
self.test_move.write({
'line_ids': [
(1, lines[0].id, {'credit': lines[0].credit + 100.0}),
(1, lines[2].id, {'debit': lines[2].debit + 100.0}),
],
})
# Try to edit the account of a line.
self.test_move.line_ids[0].write({'account_id': self.test_move.line_ids[0].account_id.copy().id})
# Try to edit a line having some taxes.
with self.assertRaises(UserError), self.cr.savepoint():
self.test_move.write({
'line_ids': [
(1, lines[0].id, {'credit': lines[0].credit + 100.0}),
(1, lines[3].id, {'debit': lines[3].debit + 100.0}),
],
})
# Try to add a new tax on a line.
with self.assertRaises(UserError), self.cr.savepoint():
self.test_move.write({
'line_ids': [
(1, lines[2].id, {'tax_ids': [(6, 0, self.company_data['default_tax_purchase'].ids)]}),
],
})
# Try to edit a tax line.
with self.assertRaises(UserError), self.cr.savepoint():
self.test_move.write({
'line_ids': [
(1, lines[0].id, {'credit': lines[0].credit + 100.0}),
(1, lines[1].id, {'debit': lines[1].debit + 100.0}),
],
})
# Try to create a line not affecting the taxes.
self.test_move.write({
'line_ids': [
(1, lines[0].id, {'credit': lines[0].credit + 100.0}),
(0, None, {
'name': 'revenue line 1',
'account_id': self.company_data['default_account_revenue'].id,
'debit': 100.0,
'credit': 0.0,
}),
],
})
# Try to create a line affecting the taxes.
with self.assertRaises(UserError), self.cr.savepoint():
self.test_move.write({
'line_ids': [
(1, lines[0].id, {'credit': lines[0].credit + 100.0}),
(0, None, {
'name': 'revenue line 2',
'account_id': self.company_data['default_account_revenue'].id,
'debit': 1000.0,
'credit': 0.0,
'tax_ids': [(6, 0, self.company_data['default_tax_sale'].ids)],
}),
],
})
# You can't remove the journal entry from a locked period.
with self.assertRaises(UserError), self.cr.savepoint():
self.test_move.date = fields.Date.from_string('2018-01-01')
with self.assertRaises(UserError), self.cr.savepoint():
self.test_move.unlink()
with self.assertRaises(UserError), self.cr.savepoint():
self.test_move.button_draft()
copy_move = self.test_move.copy({'date': self.test_move.date})
# /!\ The date is changed automatically to the next available one during the post.
copy_move.action_post()
# You can't change the date to one being in a locked period.
with self.assertRaises(UserError), self.cr.savepoint():
copy_move.date = fields.Date.from_string('2017-01-01')
def test_misc_draft_reconciled_entries_1(self):
draft_moves = self.env['account.move'].create([
{
'move_type': 'entry',
'line_ids': [
(0, None, {
'name': 'move 1 receivable line',
'account_id': self.company_data['default_account_receivable'].id,
'debit': 1000.0,
'credit': 0.0,
}),
(0, None, {
'name': 'move 1 counterpart line',
'account_id': self.company_data['default_account_expense'].id,
'debit': 0.0,
'credit': 1000.0,
}),
]
},
{
'move_type': 'entry',
'line_ids': [
(0, None, {
'name': 'move 2 receivable line',
'account_id': self.company_data['default_account_receivable'].id,
'debit': 0.0,
'credit': 2000.0,
}),
(0, None, {
'name': 'move 2 counterpart line',
'account_id': self.company_data['default_account_expense'].id,
'debit': 2000.0,
'credit': 0.0,
}),
]
},
])
# lines[0] = 'move 2 receivable line'
# lines[1] = 'move 1 counterpart line'
# lines[2] = 'move 1 receivable line'
# lines[3] = 'move 2 counterpart line'
draft_moves.action_post()
lines = draft_moves.mapped('line_ids').sorted('balance')
(lines[0] + lines[2]).reconcile()
# You can't write something impacting the reconciliation on an already reconciled line.
with self.assertRaises(UserError), self.cr.savepoint():
draft_moves[0].write({
'line_ids': [
(1, lines[1].id, {'credit': lines[1].credit + 100.0}),
(1, lines[2].id, {'debit': lines[2].debit + 100.0}),
]
})
# The write must not raise anything because the rounding of the monetary field should ignore such tiny amount.
draft_moves[0].write({
'line_ids': [
(1, lines[1].id, {'credit': lines[1].credit + 0.0000001}),
(1, lines[2].id, {'debit': lines[2].debit + 0.0000001}),
]
})
# You can't unlink an already reconciled line.
with self.assertRaises(UserError), self.cr.savepoint():
draft_moves.unlink()
def test_misc_always_balanced_move(self):
''' Ensure there is no way to make '''
# You can't remove a journal item making the journal entry unbalanced.
with self.assertRaises(UserError), self.cr.savepoint():
self.test_move.line_ids[0].unlink()
# Same check using write instead of unlink.
with self.assertRaises(UserError), self.cr.savepoint():
balance = self.test_move.line_ids[0].balance + 5
self.test_move.line_ids[0].write({
'debit': balance if balance > 0.0 else 0.0,
'credit': -balance if balance < 0.0 else 0.0,
})
# You can remove journal items if the related journal entry is still balanced.
self.test_move.line_ids.unlink()
def test_add_followers_on_post(self):
# Add some existing partners, some from another company
company = self.env['res.company'].create({'name': 'Oopo'})
company.flush()
existing_partners = self.env['res.partner'].create([{
'name': 'Jean',
'company_id': company.id,
},{
'name': 'Paulus',
}])
self.test_move.message_subscribe(existing_partners.ids)
user = new_test_user(self.env, login='jag', groups='account.group_account_invoice')
move = self.test_move.with_user(user)
partner = self.env['res.partner'].create({'name': 'Belouga'})
move.partner_id = partner
move.action_post()
self.assertEqual(move.message_partner_ids, self.env.user.partner_id | existing_partners | partner)
def test_misc_move_onchange(self):
''' Test the behavior on onchanges for account.move having 'entry' as type. '''
move_form = Form(self.env['account.move'])
# Rate 1:3
move_form.date = fields.Date.from_string('2016-01-01')
# New line that should get 400.0 as debit.
with move_form.line_ids.new() as line_form:
line_form.name = 'debit_line'
line_form.account_id = self.company_data['default_account_revenue']
line_form.currency_id = self.currency_data['currency']
line_form.amount_currency = 1200.0
# New line that should get 400.0 as credit.
with move_form.line_ids.new() as line_form:
line_form.name = 'credit_line'
line_form.account_id = self.company_data['default_account_revenue']
line_form.currency_id = self.currency_data['currency']
line_form.amount_currency = -1200.0
move = move_form.save()
self.assertRecordValues(
move.line_ids.sorted('debit'),
[
{
'currency_id': self.currency_data['currency'].id,
'amount_currency': -1200.0,
'debit': 0.0,
'credit': 400.0,
},
{
'currency_id': self.currency_data['currency'].id,
'amount_currency': 1200.0,
'debit': 400.0,
'credit': 0.0,
},
],
)
# === Change the date to change the currency conversion's rate ===
with Form(move) as move_form:
move_form.date = fields.Date.from_string('2017-01-01')
self.assertRecordValues(
move.line_ids.sorted('debit'),
[
{
'currency_id': self.currency_data['currency'].id,
'amount_currency': -1200.0,
'debit': 0.0,
'credit': 600.0,
},
{
'currency_id': self.currency_data['currency'].id,
'amount_currency': 1200.0,
'debit': 600.0,
'credit': 0.0,
},
],
)
def test_included_tax(self):
'''
Test an account.move.line is created automatically when adding a tax.
This test uses the following scenario:
- Create manually a debit line of 1000 having an included tax.
- Assume a line containing the tax amount is created automatically.
- Create manually a credit line to balance the two previous lines.
- Save the move.
included tax = 20%
Name | Debit | Credit | Tax_ids | Tax_line_id's name
-----------------------|-----------|-----------|---------------|-------------------
debit_line_1 | 1000 | | tax |
included_tax_line | 200 | | | included_tax_line
credit_line_1 | | 1200 | |
'''
self.included_percent_tax = self.env['account.tax'].create({
'name': 'included_tax_line',
'amount_type': 'percent',
'amount': 20,
'price_include': True,
'include_base_amount': False,
})
self.account = self.company_data['default_account_revenue']
move_form = Form(self.env['account.move'].with_context(default_move_type='entry'))
# Create a new account.move.line with debit amount.
with move_form.line_ids.new() as debit_line:
debit_line.name = 'debit_line_1'
debit_line.account_id = self.account
debit_line.debit = 1000
debit_line.tax_ids.clear()
debit_line.tax_ids.add(self.included_percent_tax)
self.assertTrue(debit_line.recompute_tax_line)
# Create a third account.move.line with credit amount.
with move_form.line_ids.new() as credit_line:
credit_line.name = 'credit_line_1'
credit_line.account_id = self.account
credit_line.credit = 1200
move = move_form.save()
self.assertRecordValues(move.line_ids, [
{'name': 'debit_line_1', 'debit': 1000.0, 'credit': 0.0, 'tax_ids': [self.included_percent_tax.id], 'tax_line_id': False},
{'name': 'included_tax_line', 'debit': 200.0, 'credit': 0.0, 'tax_ids': [], 'tax_line_id': self.included_percent_tax.id},
{'name': 'credit_line_1', 'debit': 0.0, 'credit': 1200.0, 'tax_ids': [], 'tax_line_id': False},
])
def test_misc_prevent_unlink_posted_items(self):
# You cannot remove journal items if the related journal entry is posted.
self.test_move.action_post()
with self.assertRaises(UserError), self.cr.savepoint():
self.test_move.line_ids.unlink()
# You can remove journal items if the related journal entry is draft.
self.test_move.button_draft()
self.test_move.line_ids.unlink()
|