diff --git a/INSTALL b/INSTALL
index 80b3192b1b..cbfa29a982 100644
--- a/INSTALL
+++ b/INSTALL
@@ -5,6 +5,7 @@ Prerequisites
-------------
* Python 2.6 or later (http://www.python.org/)
+ * python-sql (http://code.google.com/p/python-sql/)
* trytond (http://www.tryton.org/)
* trytond_company (http://www.tryton.org/)
* trytond_party (http://www.tryton.org/)
diff --git a/account.py b/account.py
index a1e0afb4c6..3fad3bc505 100644
--- a/account.py
+++ b/account.py
@@ -2,6 +2,10 @@
#this repository contains the full copyright notices and license terms.
from decimal import Decimal
import copy
+from sql import Column
+from sql.aggregate import Sum
+from sql.conditionals import Coalesce
+
from trytond.model import ModelView, ModelSQL, fields
from trytond.wizard import Wizard, StateView, StateAction, Button
from trytond.pyson import Eval, PYSONEncoder
@@ -115,9 +119,18 @@ class Account(ModelSQL, ModelView):
@classmethod
def get_balance(cls, accounts, name):
res = {}
- Line = Pool().get('analytic_account.line')
- Currency = Pool().get('currency.currency')
+ pool = Pool()
+ Line = pool.get('analytic_account.line')
+ MoveLine = pool.get('account.move.line')
+ Account = pool.get('account.account')
+ Company = pool.get('company.company')
+ Currency = pool.get('currency.currency')
cursor = Transaction().cursor
+ table = cls.__table__()
+ line = Line.__table__()
+ move_line = MoveLine.__table__()
+ a_account = Account.__table__()
+ company = Company.__table__()
ids = [a.id for a in accounts]
childs = cls.search([('parent', 'child_of', ids)])
@@ -128,25 +141,22 @@ class Account(ModelSQL, ModelView):
for account in all_accounts:
id2account[account.id] = account
- line_query = Line.query_get()
- cursor.execute('SELECT a.id, '
- 'SUM((COALESCE(l.debit, 0) - COALESCE(l.credit, 0))), '
- 'c.currency '
- 'FROM analytic_account_account a '
- 'LEFT JOIN analytic_account_line l '
- 'ON (a.id = l.account) '
- 'LEFT JOIN account_move_line ml '
- 'ON (ml.id = l.move_line) '
- 'LEFT JOIN account_account aa '
- 'ON (aa.id = ml.account) '
- 'LEFT JOIN company_company c '
- 'ON (c.id = aa.company) '
- 'WHERE a.type != \'view\' '
- 'AND a.id IN (' +
- ','.join(('%s',) * len(all_ids)) + ') '
- 'AND ' + line_query + ' '
- 'AND a.active '
- 'GROUP BY a.id, c.currency', all_ids)
+ line_query = Line.query_get(line)
+ cursor.execute(*table.join(line, 'LEFT',
+ condition=table.id == line.account
+ ).join(move_line, 'LEFT',
+ condition=move_line.id == line.move_line
+ ).join(a_account, 'LEFT',
+ condition=a_account.id == move_line.account
+ ).join(company, 'LEFT',
+ condition=company.id == a_account.company
+ ).select(table.id,
+ Sum(Coalesce(line.debit, 0) - Coalesce(line.credit, 0)),
+ company.currency,
+ where=(table.type != 'view')
+ & table.id.in_(all_ids)
+ & table.active & line_query,
+ group_by=(table.id, company.currency)))
account_sum = {}
id2currency = {}
for account_id, sum, currency_id in cursor.fetchall():
@@ -185,8 +195,16 @@ class Account(ModelSQL, ModelView):
res = {}
pool = Pool()
Line = pool.get('analytic_account.line')
+ MoveLine = pool.get('account.move.line')
+ Account = pool.get('account.account')
+ Company = pool.get('company.company')
Currency = pool.get('currency.currency')
cursor = Transaction().cursor
+ table = cls.__table__()
+ line = Line.__table__()
+ move_line = MoveLine.__table__()
+ a_account = Account.__table__()
+ company = Company.__table__()
if name not in ('credit', 'debit'):
raise Exception('Bad argument')
@@ -197,25 +215,22 @@ class Account(ModelSQL, ModelView):
res[account.id] = Decimal('0.0')
id2account[account.id] = account
- line_query = Line.query_get()
- cursor.execute('SELECT a.id, '
- 'SUM(COALESCE(l.' + name + ', 0)), '
- 'c.currency '
- 'FROM analytic_account_account a '
- 'LEFT JOIN analytic_account_line l '
- 'ON (a.id = l.account) '
- 'LEFT JOIN account_move_line ml '
- 'ON (ml.id = l.move_line) '
- 'LEFT JOIN account_account aa '
- 'ON (aa.id = ml.account) '
- 'LEFT JOIN company_company c '
- 'ON (c.id = aa.company) '
- 'WHERE a.type != \'view\' '
- 'AND a.id IN (' +
- ','.join(('%s',) * len(ids)) + ') '
- 'AND ' + line_query + ' '
- 'AND a.active '
- 'GROUP BY a.id, c.currency', ids)
+ line_query = Line.query_get(line)
+ cursor.execute(*table.join(line, 'LEFT',
+ condition=table.id == line.account
+ ).join(move_line, 'LEFT',
+ condition=move_line.id == line.move_line
+ ).join(a_account, 'LEFT',
+ condition=a_account.id == move_line.account
+ ).join(company, 'LEFT',
+ condition=company.id == a_account.company
+ ).select(table.id,
+ Sum(Coalesce(Column(line, name), 0)),
+ company.currency,
+ where=(table.type != 'view')
+ & table.id.in_(ids)
+ & table.active & line_query,
+ group_by=(table.id, company.currency)))
id2currency = {}
for account_id, sum, currency_id in cursor.fetchall():
diff --git a/account.xml b/account.xml
index 04c21bf511..f042d0a315 100644
--- a/account.xml
+++ b/account.xml
@@ -26,7 +26,7 @@ this repository contains the full copyright notices and license terms. -->
Analytic Accounts
analytic_account.account
- [('parent', '=', False)]
+ [('parent', '=', None)]
@@ -69,7 +69,7 @@ this repository contains the full copyright notices and license terms. -->
Analytic Accounts
analytic_account.account
- [('parent', '=', False)]
+ [('parent', '=', None)]
diff --git a/line.py b/line.py
index 123500cdb5..722bd0e2e5 100644
--- a/line.py
+++ b/line.py
@@ -4,7 +4,7 @@ import time
from decimal import Decimal
from trytond.model import ModelView, ModelSQL, fields
from trytond.wizard import Wizard, StateAction
-from trytond.backend import TableHandler
+from trytond import backend
from trytond.pyson import Eval, PYSONEncoder
from trytond.transaction import Transaction
from trytond.pool import Pool
@@ -53,6 +53,7 @@ class Line(ModelSQL, ModelView):
@classmethod
def __register__(cls, module_name):
+ TableHandler = backend.get('TableHandler')
super(Line, cls).__register__(module_name)
cursor = Transaction().cursor
table = TableHandler(cursor, cls, module_name)
@@ -87,23 +88,17 @@ class Line(ModelSQL, ModelView):
return 2
@staticmethod
- def query_get(obj='l'):
+ def query_get(table):
'''
Return SQL clause for analytic line depending of the context.
- obj is the SQL alias of the analytic_account_line in the query.
+ table is the SQL instance of the analytic_account_line table.
'''
- res = obj + '.active'
+ clause = table.active
if Transaction().context.get('start_date'):
- # Check start_date
- time.strptime(str(Transaction().context['start_date']), '%Y-%m-%d')
- res += (' AND ' + obj + '.date >= date(\''
- + str(Transaction().context['start_date']) + '\')')
+ clause &= table.date >= Transaction().context['start_date']
if Transaction().context.get('end_date'):
- # Check end_date
- time.strptime(str(Transaction().context['end_date']), '%Y-%m-%d')
- res += (' AND ' + obj + '.date <= date(\''
- + str(Transaction().context['end_date']) + '\')')
- return res
+ clause &= table.date <= Transaction().context['end_date']
+ return clause
@classmethod
def validate(cls, lines):
diff --git a/setup.py b/setup.py
index b3149cbb82..b5e96384ce 100644
--- a/setup.py
+++ b/setup.py
@@ -21,7 +21,7 @@ major_version, minor_version, _ = info.get('version', '0.0.1').split('.', 2)
major_version = int(major_version)
minor_version = int(minor_version)
-requires = []
+requires = ['python-sql']
for dep in info.get('depends', []):
if not re.match(r'(ir|res|webdav)(\W|$)', dep):
requires.append('trytond_%s >= %s.%s, < %s.%s' %