Files
tryton/tests/test_account.py
T

481 lines
20 KiB
Python
Raw Normal View History

2014-12-02 12:54:29 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
2008-08-27 11:50:53 +02:00
import unittest
2013-01-26 15:12:02 +01:00
import doctest
2011-11-29 11:45:03 +01:00
import datetime
from decimal import Decimal
2014-01-31 22:30:34 +01:00
from dateutil.relativedelta import relativedelta
2009-01-28 12:36:22 +01:00
import trytond.tests.test_tryton
from trytond.tests.test_tryton import test_view, test_depends, test_menu_action
2014-09-04 08:44:32 +02:00
from trytond.tests.test_tryton import doctest_setup, doctest_teardown
from trytond.tests.test_tryton import POOL, DB_NAME, USER, CONTEXT
from trytond.transaction import Transaction
2008-08-27 11:50:53 +02:00
class AccountTestCase(unittest.TestCase):
2014-01-12 19:59:16 +01:00
'Test Account module'
2008-08-27 11:50:53 +02:00
def setUp(self):
2009-01-28 12:36:22 +01:00
trytond.tests.test_tryton.install_module('account')
self.account_template = POOL.get('account.account.template')
2013-03-10 19:01:35 +01:00
self.tax_code_template = POOL.get('account.tax.code.template')
self.tax_template = POOL.get('account.tax.code.template')
self.account = POOL.get('account.account')
2012-01-02 17:54:33 +01:00
self.account_create_chart = POOL.get(
'account.create_chart', type='wizard')
self.company = POOL.get('company.company')
self.user = POOL.get('res.user')
2011-11-29 11:45:03 +01:00
self.fiscalyear = POOL.get('account.fiscalyear')
self.sequence = POOL.get('ir.sequence')
self.move = POOL.get('account.move')
self.journal = POOL.get('account.journal')
self.account_type = POOL.get('account.account.type')
2013-09-16 11:33:24 +02:00
self.period = POOL.get('account.period')
self.balance_non_deferral = POOL.get(
'account.fiscalyear.balance_non_deferral', type='wizard')
2014-01-31 22:30:34 +01:00
self.tax = POOL.get('account.tax')
self.party = POOL.get('party.party')
2008-08-27 11:50:53 +02:00
2009-06-27 10:56:00 +02:00
def test0005views(self):
2014-01-12 19:59:16 +01:00
'Test views'
2010-08-27 12:17:53 +02:00
test_view('account')
2009-06-27 10:56:00 +02:00
2011-08-04 16:57:14 +02:00
def test0006depends(self):
2014-01-12 19:59:16 +01:00
'Test depends'
2011-08-04 16:57:14 +02:00
test_depends()
def test0007menu_actions(self):
'Test menu actions'
test_menu_action('account')
def test0010account_chart(self):
'Test creation of minimal chart of accounts'
with Transaction().start(DB_NAME, USER,
context=CONTEXT) as transaction:
2012-09-24 22:17:29 +02:00
account_template, = self.account_template.search([
('parent', '=', None),
])
2013-03-10 19:01:35 +01:00
tax_account, = self.account_template.search([
('name', '=', 'Main Tax'),
])
with Transaction().set_user(0):
tax_code = self.tax_code_template()
tax_code.name = 'Tax Code'
tax_code.account = account_template
tax_code.save()
base_code = self.tax_code_template()
base_code.name = 'Base Code'
base_code.account = account_template
base_code.save()
tax = self.tax_template()
tax.name = tax.description = '20% VAT'
tax.type = 'percentage'
2013-06-26 22:59:55 +02:00
tax.rate = Decimal('0.2')
2013-03-10 19:01:35 +01:00
tax.account = account_template
tax.invoice_account = tax_account
tax.credit_note_account = tax_account
tax.invoice_base_code = base_code
tax.invoice_base_sign = Decimal(1)
tax.invoice_tax_code = tax_code
tax.invoice_tax_sign = Decimal(1)
tax.credit_note_base_code = base_code
tax.credit_note_base_sign = Decimal(-1)
tax.credit_note_tax_code = tax_code
tax.credit_note_tax_sign = Decimal(-1)
tax.save()
2014-01-16 22:09:59 +01:00
company, = self.company.search([
('rec_name', '=', 'Dunder Mifflin'),
])
2012-09-24 22:17:29 +02:00
self.user.write([self.user(USER)], {
'main_company': company.id,
'company': company.id,
})
2011-11-29 11:45:03 +01:00
CONTEXT.update(self.user.get_preferences(context_only=True))
2012-01-02 17:54:33 +01:00
session_id, _, _ = self.account_create_chart.create()
2012-09-24 22:17:29 +02:00
create_chart = self.account_create_chart(session_id)
create_chart.account.account_template = account_template
create_chart.account.company = company
create_chart.transition_create_account()
receivable, = self.account.search([
('kind', '=', 'receivable'),
2012-09-24 22:17:29 +02:00
('company', '=', company.id),
])
2012-09-24 22:17:29 +02:00
payable, = self.account.search([
('kind', '=', 'payable'),
2012-09-24 22:17:29 +02:00
('company', '=', company.id),
])
2012-09-24 22:17:29 +02:00
create_chart.properties.company = company
create_chart.properties.account_receivable = receivable
create_chart.properties.account_payable = payable
create_chart.transition_create_properties()
transaction.cursor.commit()
2011-11-29 11:45:03 +01:00
def test0020fiscalyear(self):
2014-01-12 19:59:16 +01:00
'Test fiscalyear'
2011-11-29 11:45:03 +01:00
with Transaction().start(DB_NAME, USER,
context=CONTEXT) as transaction:
today = datetime.date.today()
2014-01-16 22:09:59 +01:00
company, = self.company.search([
('rec_name', '=', 'Dunder Mifflin'),
])
2013-01-06 22:18:13 +01:00
sequence, = self.sequence.create([{
'name': '%s' % today.year,
'code': 'account.move',
'company': company.id,
}])
fiscalyear, = self.fiscalyear.create([{
'name': '%s' % today.year,
'start_date': today.replace(month=1, day=1),
'end_date': today.replace(month=12, day=31),
'company': company.id,
'post_move_sequence': sequence.id,
}])
2012-09-24 22:17:29 +02:00
self.fiscalyear.create_period([fiscalyear])
2011-11-29 11:45:03 +01:00
self.assertEqual(len(fiscalyear.periods), 12)
transaction.cursor.commit()
def test0030account_debit_credit(self):
2014-01-12 19:59:16 +01:00
'Test account debit/credit'
with Transaction().start(DB_NAME, USER,
context=CONTEXT) as transaction:
party = self.party(name='Party')
party.save()
fiscalyear, = self.fiscalyear.search([])
period = fiscalyear.periods[0]
journal_revenue, = self.journal.search([
('code', '=', 'REV'),
])
journal_expense, = self.journal.search([
('code', '=', 'EXP'),
])
revenue, = self.account.search([
('kind', '=', 'revenue'),
])
receivable, = self.account.search([
('kind', '=', 'receivable'),
])
expense, = self.account.search([
('kind', '=', 'expense'),
])
payable, = self.account.search([
('kind', '=', 'payable'),
])
# Create some moves
2013-01-06 22:18:13 +01:00
vlist = [
{
'period': period.id,
'journal': journal_revenue.id,
'date': period.start_date,
'lines': [
2013-01-06 22:18:13 +01:00
('create', [{
'account': revenue.id,
'credit': Decimal(100),
}, {
'account': receivable.id,
'debit': Decimal(100),
'party': party.id,
2013-01-06 22:18:13 +01:00
}]),
],
},
{
'period': period.id,
'journal': journal_expense.id,
'date': period.start_date,
'lines': [
2013-01-06 22:18:13 +01:00
('create', [{
'account': expense.id,
'debit': Decimal(30),
}, {
'account': payable.id,
'credit': Decimal(30),
'party': party.id,
2013-01-06 22:18:13 +01:00
}]),
],
},
]
2013-01-06 22:18:13 +01:00
self.move.create(vlist)
# Test debit/credit
self.assertEqual((revenue.debit, revenue.credit),
(Decimal(0), Decimal(100)))
self.assertEqual(revenue.balance, Decimal(-100))
# Use next fiscalyear
2013-01-06 22:18:13 +01:00
next_sequence, = self.sequence.create([{
'name': 'Next Year',
'code': 'account.move',
'company': fiscalyear.company.id,
}])
next_fiscalyear, = self.fiscalyear.copy([fiscalyear],
default={
'start_date': fiscalyear.end_date + datetime.timedelta(1),
'end_date': fiscalyear.end_date + datetime.timedelta(360),
'post_move_sequence': next_sequence.id,
'periods': None,
})
self.fiscalyear.create_period([next_fiscalyear])
# Test debit/credit for next year
with Transaction().set_context(fiscalyear=next_fiscalyear.id):
revenue = self.account(revenue.id)
self.assertEqual((revenue.debit, revenue.credit),
(Decimal(0), Decimal(0)))
self.assertEqual(revenue.balance, Decimal(-100))
# Test debit/credit cumulate for next year
with Transaction().set_context(fiscalyear=next_fiscalyear.id,
cumulate=True):
revenue = self.account(revenue.id)
self.assertEqual((revenue.debit, revenue.credit),
(Decimal(0), Decimal(100)))
self.assertEqual(revenue.balance, Decimal(-100))
2013-09-16 11:33:24 +02:00
# Balance non-deferral
journal_sequence, = self.sequence.search([
('code', '=', 'account.journal'),
])
2013-01-06 22:18:13 +01:00
journal_closing, = self.journal.create([{
'name': 'Closing',
'code': 'CLO',
'type': 'situation',
'sequence': journal_sequence.id,
}])
2013-09-16 11:33:24 +02:00
period_closing, = self.period.create([{
'name': 'Closing',
'start_date': fiscalyear.end_date,
'end_date': fiscalyear.end_date,
'fiscalyear': fiscalyear.id,
'type': 'adjustment',
}])
type_equity, = self.account_type.search([
('name', '=', 'Equity'),
])
2013-01-06 22:18:13 +01:00
account_pl, = self.account.create([{
'name': 'P&L',
'type': type_equity.id,
'deferral': True,
'parent': revenue.parent.id,
'kind': 'other',
}])
2013-09-16 11:33:24 +02:00
session_id = self.balance_non_deferral.create()[0]
balance_non_deferral = self.balance_non_deferral(session_id)
balance_non_deferral.start.fiscalyear = fiscalyear
balance_non_deferral.start.journal = journal_closing
balance_non_deferral.start.period = period_closing
balance_non_deferral.start.credit_account = account_pl
balance_non_deferral.start.debit_account = account_pl
balance_non_deferral._execute('balance')
moves = self.move.search([
('state', '=', 'draft'),
('period.fiscalyear', '=', fiscalyear.id),
])
self.move.post(moves)
2013-09-16 11:33:24 +02:00
# Close fiscalyear
self.fiscalyear.close([fiscalyear])
# Check deferral
self.assertEqual(revenue.deferrals, ())
deferral_receivable, = receivable.deferrals
self.assertEqual(
(deferral_receivable.debit, deferral_receivable.credit),
(Decimal(100), Decimal(0)))
self.assertEqual(deferral_receivable.fiscalyear, fiscalyear)
# Test debit/credit
with Transaction().set_context(fiscalyear=fiscalyear.id):
revenue = self.account(revenue.id)
self.assertEqual((revenue.debit, revenue.credit),
(Decimal(100), Decimal(100)))
self.assertEqual(revenue.balance, Decimal(0))
receivable = self.account(receivable.id)
self.assertEqual((receivable.debit, receivable.credit),
(Decimal(100), Decimal(0)))
self.assertEqual(receivable.balance, Decimal(100))
# Test debit/credit for next year
with Transaction().set_context(fiscalyear=next_fiscalyear.id):
revenue = self.account(revenue.id)
self.assertEqual((revenue.debit, revenue.credit),
(Decimal(0), Decimal(0)))
self.assertEqual(revenue.balance, Decimal(0))
receivable = self.account(receivable.id)
self.assertEqual((receivable.debit, receivable.credit),
(Decimal(0), Decimal(0)))
self.assertEqual(receivable.balance, Decimal(100))
# Test debit/credit cumulate for next year
with Transaction().set_context(fiscalyear=next_fiscalyear.id,
cumulate=True):
revenue = self.account(revenue.id)
self.assertEqual((revenue.debit, revenue.credit),
(Decimal(0), Decimal(0)))
self.assertEqual(revenue.balance, Decimal(0))
receivable = self.account(receivable.id)
self.assertEqual((receivable.debit, receivable.credit),
(Decimal(100), Decimal(0)))
self.assertEqual(receivable.balance, Decimal(100))
transaction.cursor.rollback()
2014-01-31 22:30:34 +01:00
def test0040tax_compute(self):
2015-01-15 23:40:42 +01:00
'Test tax compute/reverse_compute'
2014-01-31 22:30:34 +01:00
with Transaction().start(DB_NAME, USER, context=CONTEXT):
today = datetime.date.today()
tax_account, = self.account.search([
('name', '=', 'Main Tax'),
])
tax = self.tax()
tax.name = tax.description = 'Test'
tax.type = 'none'
tax.invoice_account = tax_account
tax.credit_note_account = tax_account
child1 = self.tax()
child1.name = child1.description = 'Child 1'
child1.type = 'percentage'
child1.rate = Decimal('0.2')
child1.invoice_account = tax_account
child1.credit_note_account = tax_account
child1.save()
child2 = self.tax()
child2.name = child2.description = 'Child 1'
child2.type = 'fixed'
child2.amount = Decimal('10')
child2.invoice_account = tax_account
child2.credit_note_account = tax_account
child2.save()
tax.childs = [child1, child2]
tax.save()
self.assertEqual(self.tax.compute([tax], Decimal('100'), 2),
[{
'base': Decimal('200'),
'amount': Decimal('40.0'),
'tax': child1,
}, {
'base': Decimal('200'),
'amount': Decimal('20'),
'tax': child2,
}])
2015-01-15 23:40:42 +01:00
self.assertEqual(
self.tax.reverse_compute(Decimal('130'), [tax]),
Decimal('100'))
2014-01-31 22:30:34 +01:00
child1.end_date = today + relativedelta(days=5)
child1.save()
self.assertEqual(self.tax.compute([tax], Decimal('100'), 2, today),
[{
'base': Decimal('200'),
'amount': Decimal('40.0'),
'tax': child1,
}, {
'base': Decimal('200'),
'amount': Decimal('20'),
'tax': child2,
}])
2015-01-15 23:40:42 +01:00
self.assertEqual(
self.tax.reverse_compute(Decimal('130'), [tax], today),
Decimal('100'))
2014-01-31 22:30:34 +01:00
child1.start_date = today + relativedelta(days=1)
child1.save()
self.assertEqual(self.tax.compute([tax], Decimal('100'), 2, today),
[{
'base': Decimal('200'),
'amount': Decimal('20'),
'tax': child2,
}])
2015-01-15 23:40:42 +01:00
self.assertEqual(
self.tax.reverse_compute(Decimal('110'), [tax], today),
Decimal('100'))
2014-01-31 22:30:34 +01:00
self.assertEqual(self.tax.compute([tax], Decimal('100'), 2,
today + relativedelta(days=1)), [{
'base': Decimal('200'),
'amount': Decimal('40.0'),
'tax': child1,
}, {
'base': Decimal('200'),
'amount': Decimal('20'),
'tax': child2,
}])
2015-01-15 23:40:42 +01:00
self.assertEqual(
self.tax.reverse_compute(
Decimal('130'), [tax], today + relativedelta(days=1)),
Decimal('100'))
2014-01-31 22:30:34 +01:00
self.assertEqual(self.tax.compute([tax], Decimal('100'), 2,
today + relativedelta(days=5)), [{
'base': Decimal('200'),
'amount': Decimal('40.0'),
'tax': child1,
}, {
'base': Decimal('200'),
'amount': Decimal('20'),
'tax': child2,
}])
2015-01-15 23:40:42 +01:00
self.assertEqual(
self.tax.reverse_compute(Decimal('130'), [tax],
today + relativedelta(days=5)),
Decimal('100'))
2014-01-31 22:30:34 +01:00
self.assertEqual(self.tax.compute([tax], Decimal('100'), 2,
today + relativedelta(days=6)), [{
'base': Decimal('200'),
'amount': Decimal('20'),
'tax': child2,
}])
2015-01-15 23:40:42 +01:00
self.assertEqual(
self.tax.reverse_compute(Decimal('110'), [tax],
today + relativedelta(days=6)),
Decimal('100'))
2014-01-31 22:30:34 +01:00
child1.end_date = None
child1.save()
self.assertEqual(self.tax.compute([tax], Decimal('100'), 2,
today + relativedelta(days=6)), [{
'base': Decimal('200'),
'amount': Decimal('40.0'),
'tax': child1,
}, {
'base': Decimal('200'),
'amount': Decimal('20'),
'tax': child2,
}])
2015-01-15 23:40:42 +01:00
self.assertEqual(
self.tax.reverse_compute(Decimal('130'), [tax],
today + relativedelta(days=6)),
Decimal('100'))
2014-01-31 22:30:34 +01:00
2012-04-03 20:06:15 +02:00
2008-08-27 11:50:53 +02:00
def suite():
2010-03-04 14:03:54 +01:00
suite = trytond.tests.test_tryton.suite()
from trytond.modules.company.tests import test_company
for test in test_company.suite():
if test not in suite:
suite.addTest(test)
2010-03-04 14:03:54 +01:00
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(
AccountTestCase))
2013-01-26 15:12:02 +01:00
suite.addTests(doctest.DocFileSuite(
'scenario_account_reconciliation.rst',
2014-09-04 08:44:32 +02:00
setUp=doctest_setup, tearDown=doctest_teardown, encoding='utf-8',
2013-01-26 15:12:02 +01:00
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
2014-05-09 23:32:17 +02:00
suite.addTests(doctest.DocFileSuite(
'scenario_move_cancel.rst',
2014-09-04 08:44:32 +02:00
setUp=doctest_setup, tearDown=doctest_teardown, encoding='utf-8',
2014-05-09 23:32:17 +02:00
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
2010-03-04 14:03:54 +01:00
return suite