Analytic chart of Accounts: according to context and period

bzr revid: jvo@tinyerp.com-20090408143550-dj2zhfz3sc6c77o5
This commit is contained in:
Jay (Open ERP) 2009-04-08 20:05:50 +05:30
parent 5609c037ce
commit ca068d9e3b
6 changed files with 140 additions and 16 deletions

View File

@ -48,6 +48,21 @@ class account_analytic_line(osv.osv):
'date': lambda *a: time.strftime('%Y-%m-%d'),
}
_order = 'date'
def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None):
if context is None:
context = {}
if context.get('from_date',False):
args.append(['date', '>=',context['from_date']])
if context.get('to_date',False):
args.append(['date','<=',context['to_date']])
return super(account_analytic_line, self).search(cr, uid, args, offset, limit,
order, context=context)
def _check_company(self, cr, uid, ids):
lines = self.browse(cr, uid, ids)
for l in lines:

View File

@ -36,8 +36,15 @@ class account_analytic_account(osv.osv):
def _credit_calc(self, cr, uid, ids, name, arg, context={}):
acc_set = ",".join(map(str, ids))
cr.execute("SELECT a.id, COALESCE(SUM(l.amount),0) FROM account_analytic_account a LEFT JOIN account_analytic_line l ON (a.id=l.account_id) WHERE l.amount<0 and a.id IN (%s) GROUP BY a.id" % acc_set)
r= dict(cr.fetchall())
where_date = ''
if context.get('from_date',False):
where_date += " AND l.date >= '" + context['from_date'] + "'"
if context.get('to_date',False):
where_date += " AND l.date <= '" + context['to_date'] + "'"
cr.execute("SELECT a.id, COALESCE(SUM(l.amount),0) FROM account_analytic_account a LEFT JOIN account_analytic_line l ON (a.id=l.account_id %s) WHERE l.amount<0 and a.id IN (%s) GROUP BY a.id" % (where_date,acc_set))
r = dict(cr.fetchall())
for i in ids:
r.setdefault(i,0.0)
return r
@ -45,18 +52,30 @@ class account_analytic_account(osv.osv):
def _debit_calc(self, cr, uid, ids, name, arg, context={}):
acc_set = ",".join(map(str, ids))
cr.execute("SELECT a.id, COALESCE(SUM(l.amount),0) FROM account_analytic_account a LEFT JOIN account_analytic_line l ON (a.id=l.account_id) WHERE l.amount>0 and a.id IN (%s) GROUP BY a.id" % acc_set)
where_date = ''
if context.get('from_date',False):
where_date += " AND l.date >= '" + context['from_date'] + "'"
if context.get('to_date',False):
where_date += " AND l.date <= '" + context['to_date'] + "'"
cr.execute("SELECT a.id, COALESCE(SUM(l.amount),0) FROM account_analytic_account a LEFT JOIN account_analytic_line l ON (a.id=l.account_id %s) WHERE l.amount>0 and a.id IN (%s) GROUP BY a.id" % (where_date,acc_set))
r= dict(cr.fetchall())
for i in ids:
r.setdefault(i,0.0)
return r
def _balance_calc(self, cr, uid, ids, name, arg, context={}):
ids2 = self.search(cr, uid, [('parent_id', 'child_of', ids)])
acc_set = ",".join(map(str, ids2))
cr.execute("SELECT a.id, COALESCE(SUM(l.amount),0) FROM account_analytic_account a LEFT JOIN account_analytic_line l ON (a.id=l.account_id) WHERE a.id IN (%s) GROUP BY a.id" % acc_set)
where_date = ''
if context.get('from_date',False):
where_date += " AND l.date >= '" + context['from_date'] + "'"
if context.get('to_date',False):
where_date += " AND l.date <= '" + context['to_date'] + "'"
cr.execute("SELECT a.id, COALESCE(SUM(l.amount),0) FROM account_analytic_account a LEFT JOIN account_analytic_line l ON (a.id=l.account_id %s) WHERE a.id IN (%s) GROUP BY a.id" % (where_date,acc_set))
res = {}
for account_id, sum in cr.fetchall():
res[account_id] = sum
@ -86,9 +105,16 @@ class account_analytic_account(osv.osv):
#XXX must convert into one uom
ids2 = self.search(cr, uid, [('parent_id', 'child_of', ids)])
acc_set = ",".join(map(str, ids2))
where_date = ''
if context.get('from_date',False):
where_date += " AND l.date >= '" + context['from_date'] + "'"
if context.get('to_date',False):
where_date += " AND l.date <= '" + context['to_date'] + "'"
cr.execute('SELECT a.id, COALESCE(SUM(l.unit_amount), 0) \
FROM account_analytic_account a \
LEFT JOIN account_analytic_line l ON (a.id = l.account_id) \
LEFT JOIN account_analytic_line l ON (a.id = l.account_id ' + where_date + ') \
WHERE a.id IN ('+acc_set+') GROUP BY a.id')
res = {}
for account_id, sum in cr.fetchall():
@ -168,7 +194,7 @@ class account_analytic_account(osv.osv):
_order = 'parent_id desc,code'
_constraints = [
(check_recursion, 'Error! You can not create recursive account.', ['parent_id'])
(check_recursion, 'Error! You can not create recursive analytic accounts.', ['parent_id'])
]
def create(self, cr, uid, vals, context=None):

View File

@ -94,8 +94,11 @@
action="action_account_analytic_account_tree2"
id="account_analytic_def_chart"
parent="account_analytic_def_account"/>
<menuitem action="action_account_analytic_account_tree2" id="account_analytic_chart" parent="account.menu_finance_charts"/>
<!-- <menuitem action="action_account_analytic_account_tree2" id="account_analytic_chart" parent="account.menu_finance_charts"/>-->
<wizard id="wizard_analytic_account_chart" menu="False" model="account.analytic.account" name="account.analytic.account.chart" string="Analytic Chart of Accounts"/>
<menuitem icon="STOCK_INDENT" action="wizard_analytic_account_chart" id="menu_action_analytic_account_tree2" parent="account.menu_finance_charts" type="wizard"/>
<record id="analytic_account_form" model="ir.actions.act_window">
<field name="name">New Analytic Account</field>
<field name="res_model">account.analytic.account</field>

View File

@ -27,6 +27,7 @@ import wizard_account_analytic_cost_ledger_report
import wizard_account_analytic_cost_ledger_for_journal_report
import wizard_account_analytic_analytic_check
import wizard_account_analytic_line
import wizard_analytic_account_chart
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -26,15 +26,15 @@ import time
from tools.translate import _
def _action_open_window(self, cr, uid, data, context):
domain=[]
from_date= data['form']['from_date']
to_date=data['form']['to_date']
domain = []
from_date = data['form']['from_date']
to_date = data['form']['to_date']
if from_date and to_date:
domain=[('date','>=',from_date),('date','<=',to_date)]
domain = [('date','>=',from_date),('date','<=',to_date)]
elif from_date:
domain=[('date','>=',from_date)]
domain = [('date','>=',from_date)]
elif to_date:
domain=[('date','<=',to_date)]
domain = [('date','<=',to_date)]
return {
'name': _('Analytic Entries'),
'view_type': 'form',

View File

@ -0,0 +1,79 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
# $Id$
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import wizard
import pooler
class wizard_analytic_account_chart(wizard.interface):
_account_chart_arch = '''<?xml version="1.0"?>
<form string="Analytic Account Charts">
<separator string="Select the Period for Analysis" colspan="4"/>
<field name="from_date"/>
<newline/>
<field name="to_date"/>
<newline/>
<label string="(Keep empty to open the current situation)" align="0.0" colspan="3"/>
</form>'''
_account_chart_fields = {
'from_date': {
'string': 'From',
'type': 'date',
},
'to_date': {
'string': 'To',
'type': 'date',
},
}
def _analytic_account_chart_open_window(self, cr, uid, data, context):
mod_obj = pooler.get_pool(cr.dbname).get('ir.model.data')
act_obj = pooler.get_pool(cr.dbname).get('ir.actions.act_window')
result = mod_obj._get_id(cr, uid, 'account', 'action_account_analytic_account_tree2')
id = mod_obj.read(cr, uid, [result], ['res_id'])[0]['res_id']
result = act_obj.read(cr, uid, [id], context=context)[0]
result_context = {}
if data['form']['from_date']:
result_context.update({'from_date' : data['form']['from_date']})
if data['form']['to_date']:
result_context.update({'to_date' : data['form']['to_date']})
result['context'] = str(result_context)
return result
states = {
'init': {
'actions': [],
'result': {'type': 'form', 'arch':_account_chart_arch, 'fields':_account_chart_fields, 'state': [('end', 'Cancel'), ('open', 'Open Charts')]}
},
'open': {
'actions': [],
'result': {'type': 'action', 'action':_analytic_account_chart_open_window, 'state':'end'}
}
}
wizard_analytic_account_chart('account.analytic.account.chart')
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: