http://codereview.tryton.org/8271002 analytic_account: Fix credit, debit and balance computation on SQLite
This commit is contained in:
parent
3978c4f421
commit
eb5a4412d6
|
@ -0,0 +1,174 @@
|
|||
Index: account.py
|
||||
===================================================================
|
||||
|
||||
--- a/modules/analytic_account/account.py
|
||||
+++ b/modules/analytic_account/account.py
|
||||
@@ -162,6 +162,9 @@
|
||||
id2currency = {}
|
||||
for account_id, sum, currency_id in cursor.fetchall():
|
||||
account_sum.setdefault(account_id, Decimal('0.0'))
|
||||
+ # SQLite uses float for SUM
|
||||
+ if not isinstance(sum, Decimal):
|
||||
+ sum = Decimal(str(sum))
|
||||
if currency_id != id2account[account_id].currency.id:
|
||||
currency = None
|
||||
if currency_id in id2currency:
|
||||
@@ -235,6 +238,9 @@
|
||||
|
||||
id2currency = {}
|
||||
for account_id, sum, currency_id in cursor.fetchall():
|
||||
+ # SQLite uses float for SUM
|
||||
+ if not isinstance(sum, Decimal):
|
||||
+ sum = Decimal(str(sum))
|
||||
if currency_id != id2account[account_id].currency.id:
|
||||
currency = None
|
||||
if currency_id in id2currency:
|
||||
|
||||
Index: tests/test_analytic_account.py
|
||||
===================================================================
|
||||
|
||||
--- a/modules/analytic_account/tests/test_analytic_account.py
|
||||
+++ b/modules/analytic_account/tests/test_analytic_account.py
|
||||
@@ -1,8 +1,11 @@
|
||||
#This file is part of Tryton. The COPYRIGHT file at the top level of
|
||||
#this repository contains the full copyright notices and license terms.
|
||||
import unittest
|
||||
+from decimal import Decimal
|
||||
import trytond.tests.test_tryton
|
||||
from trytond.tests.test_tryton import test_view, test_depends
|
||||
+from trytond.tests.test_tryton import POOL, DB_NAME, USER, CONTEXT
|
||||
+from trytond.transaction import Transaction
|
||||
|
||||
|
||||
class AnalyticAccountTestCase(unittest.TestCase):
|
||||
@@ -10,6 +13,11 @@
|
||||
|
||||
def setUp(self):
|
||||
trytond.tests.test_tryton.install_module('analytic_account')
|
||||
+ self.fiscalyear = POOL.get('account.fiscalyear')
|
||||
+ self.journal = POOL.get('account.journal')
|
||||
+ self.move = POOL.get('account.move')
|
||||
+ self.account = POOL.get('account.account')
|
||||
+ self.analytic_account = POOL.get('analytic_account.account')
|
||||
|
||||
def test0005views(self):
|
||||
'Test views'
|
||||
@@ -19,9 +27,117 @@
|
||||
'Test depends'
|
||||
test_depends()
|
||||
|
||||
+ def test0010account_debit_credit(self):
|
||||
+ 'Test account debit/credit'
|
||||
+ with Transaction().start(DB_NAME, USER,
|
||||
+ context=CONTEXT) as transaction:
|
||||
+ root, = self.analytic_account.create([{
|
||||
+ 'type': 'root',
|
||||
+ 'name': 'Root',
|
||||
+ }])
|
||||
+ analytic_account, = self.analytic_account.create([{
|
||||
+ 'type': 'normal',
|
||||
+ 'name': 'Analytic Account',
|
||||
+ 'parent': root.id,
|
||||
+ 'root': root.id,
|
||||
+ }])
|
||||
+ 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'),
|
||||
+ ])
|
||||
+
|
||||
+ first_account_line = {
|
||||
+ 'account': revenue.id,
|
||||
+ 'credit': Decimal(100),
|
||||
+ 'analytic_lines': [
|
||||
+ ('create', [{
|
||||
+ 'account': analytic_account.id,
|
||||
+ 'name': 'Analytic Line',
|
||||
+ 'credit': Decimal(100),
|
||||
+ 'debit': Decimal(0),
|
||||
+ 'journal': journal_revenue.id,
|
||||
+ 'date': period.start_date,
|
||||
+ }])
|
||||
+ ]}
|
||||
+ second_account_line = {
|
||||
+ 'account': expense.id,
|
||||
+ 'debit': Decimal(30),
|
||||
+ 'analytic_lines': [
|
||||
+ ('create', [{
|
||||
+ 'account': analytic_account.id,
|
||||
+ 'name': 'Analytic Line',
|
||||
+ 'debit': Decimal(30),
|
||||
+ 'credit': Decimal(0),
|
||||
+ 'journal': journal_expense.id,
|
||||
+ 'date': period.start_date,
|
||||
+ }])
|
||||
+ ]}
|
||||
+ # Create some moves
|
||||
+ vlist = [{
|
||||
+ 'period': period.id,
|
||||
+ 'journal': journal_revenue.id,
|
||||
+ 'date': period.start_date,
|
||||
+ 'lines': [
|
||||
+ ('create', [first_account_line, {
|
||||
+ 'account': receivable.id,
|
||||
+ 'debit': Decimal(100),
|
||||
+ }]),
|
||||
+ ],
|
||||
+ }, {
|
||||
+ 'period': period.id,
|
||||
+ 'journal': journal_expense.id,
|
||||
+ 'date': period.start_date,
|
||||
+ 'lines': [
|
||||
+ ('create', [second_account_line, {
|
||||
+ 'account': payable.id,
|
||||
+ 'credit': Decimal(30),
|
||||
+ }]),
|
||||
+ ],
|
||||
+ },
|
||||
+ ]
|
||||
+ self.move.create(vlist)
|
||||
+
|
||||
+ self.assertEqual((analytic_account.debit, analytic_account.credit),
|
||||
+ (Decimal(30), Decimal(100)))
|
||||
+ self.assertEqual(analytic_account.balance, Decimal(70))
|
||||
+
|
||||
+ with transaction.set_context(start_date=period.end_date):
|
||||
+ analytic_account = self.analytic_account(analytic_account.id)
|
||||
+ self.assertEqual((analytic_account.debit,
|
||||
+ analytic_account.credit),
|
||||
+ (Decimal(0), Decimal(0)))
|
||||
+ self.assertEqual(analytic_account.balance, Decimal(0))
|
||||
+
|
||||
+ with transaction.set_context(end_date=period.end_date):
|
||||
+ analytic_account = self.analytic_account(analytic_account.id)
|
||||
+ self.assertEqual((analytic_account.debit,
|
||||
+ analytic_account.credit),
|
||||
+ (Decimal(30), Decimal(100)))
|
||||
+ self.assertEqual(analytic_account.balance, Decimal(70))
|
||||
+
|
||||
|
||||
def suite():
|
||||
suite = trytond.tests.test_tryton.suite()
|
||||
+ from trytond.modules.account.tests import test_account
|
||||
+ for test in test_account.suite():
|
||||
+ if test not in suite:
|
||||
+ suite.addTest(test)
|
||||
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(
|
||||
AnalyticAccountTestCase))
|
||||
return suite
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
Index: line.xml
|
||||
===================================================================
|
||||
|
||||
--- a/modules/analytic_account/line.xml
|
||||
+++ b/modules/analytic_account/line.xml
|
||||
@@ -44,6 +44,15 @@
|
||||
<field name="perm_delete" eval="True"/>
|
||||
</record>
|
||||
|
||||
+ <record model="ir.rule.group" id="rule_group_line">
|
||||
+ <field name="model" search="[('model', '=', 'analytic_account.line')]"/>
|
||||
+ <field name="global_p" eval="True"/>
|
||||
+ </record>
|
||||
+ <record model="ir.rule" id="rule_line1">
|
||||
+ <field name="domain">[('account.company', 'in', [c.id for c in user.companies])]</field>
|
||||
+ <field name="rule_group" ref="rule_group_line"/>
|
||||
+ </record>
|
||||
+
|
||||
<record model="ir.action.wizard" id="act_open_account">
|
||||
<field name="name">Open Account</field>
|
||||
<field name="wiz_name">analytic_account.line.open_account</field>
|
||||
|
Loading…
Reference in New Issue