Migration to python-sql

review1001002
This commit is contained in:
Cédric Krier committed 2013-09-06 22:33:59 +02:00
1 parent 987e170823
commit 41b820671b
5 files changed
+67 -56

No files matched your search

+1
View File
@@ -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/)
+55 -40
View File
@@ -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():
+2 -2
View File
@@ -26,7 +26,7 @@ this repository contains the full copyright notices and license terms. -->
<record model="ir.action.act_window" id="act_account_tree">
<field name="name">Analytic Accounts</field>
<field name="res_model">analytic_account.account</field>
<field name="domain">[('parent', '=', False)]</field>
<field name="domain">[('parent', '=', None)]</field>
</record>
<record model="ir.action.act_window.view" id="act_account_tree_view1">
<field name="sequence" eval="10"/>
@@ -69,7 +69,7 @@ this repository contains the full copyright notices and license terms. -->
<record model="ir.action.act_window" id="act_account_tree2">
<field name="name">Analytic Accounts</field>
<field name="res_model">analytic_account.account</field>
<field name="domain">[('parent', '=', False)]</field>
<field name="domain">[('parent', '=', None)]</field>
</record>
<record model="ir.action.act_window.view" id="act_account_tree2_view1">
<field name="sequence" eval="10"/>
+8 -13
View File
@@ -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):
+1 -1
View File
@@ -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' %