2010-06-11 14:44:32 +00:00
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import time
from osv import fields , osv
from tools . translate import _
import tools
class account_common_report ( osv . osv_memory ) :
_name = " account.common.report "
2010-06-25 11:58:36 +00:00
_description = " Account Common Report "
2010-06-11 14:44:32 +00:00
_columns = {
2010-06-25 11:58:36 +00:00
' chart_account_id ' : fields . many2one ( ' account.account ' , ' Chart of account ' , help = ' Select Charts of Accounts ' , required = True , domain = [ ( ' parent_id ' , ' = ' , False ) ] ) ,
2010-06-11 14:44:32 +00:00
' fiscalyear_id ' : fields . many2one ( ' account.fiscalyear ' , ' Fiscal year ' , help = ' Keep empty for all open fiscal year ' ) ,
2010-07-12 05:42:20 +00:00
' filter ' : fields . selection ( [ ( ' filter_no ' , ' No Filters ' ) , ( ' filter_date ' , ' Date ' ) , ( ' filter_period ' , ' Periods ' ) ] , " Filter by: " , required = True ) ,
2010-06-11 15:53:39 +00:00
' period_from ' : fields . many2one ( ' account.period ' , ' Start period ' ) ,
2010-06-11 14:44:32 +00:00
' period_to ' : fields . many2one ( ' account.period ' , ' End period ' ) ,
2010-06-24 13:28:50 +00:00
#not used. Do we really need it? 'period_ids': fields.many2many('account.period', 'ledger_period_rel', 'ledger_id', 'period_id', 'Periods'),
2010-06-11 14:44:32 +00:00
' journal_ids ' : fields . many2many ( ' account.journal ' , ' account_common_journal_rel ' , ' account_id ' , ' journal_id ' , ' Journals ' , required = True ) ,
2010-06-25 11:58:36 +00:00
' date_from ' : fields . date ( " Start Date " ) ,
' date_to ' : fields . date ( " End Date " ) ,
2010-06-11 14:44:32 +00:00
#'display_account': fields.selection([('bal_mouvement','With movements'), ('bal_all','All'), ('bal_solde','With balance is not equal to 0')],"Display accounts"),
#'amount_currency': fields.boolean("With Currency"),
#'company_id': fields.many2one('res.company', 'Company', required=True),
}
2010-06-24 13:28:50 +00:00
# def _get_company(self, cr, uid, context=None):
# user_obj = self.pool.get('res.users')
# company_obj = self.pool.get('res.company')
# if context is None:
# context = {}
# user = user_obj.browse(cr, uid, uid, context=context)
# if user.company_id:
# return user.company_id.id
# else:
# return company_obj.search(cr, uid, [('parent_id', '=', False)])[0]
2010-06-11 14:44:32 +00:00
2010-07-12 06:13:10 +00:00
def onchange_filter ( self , cr , uid , ids , filter = ' filter_no ' , context = None ) :
res = { }
if filter == ' filter_no ' :
res [ ' value ' ] = { ' period_from ' : False , ' period_to ' : False }
return res
return { }
2010-07-12 11:00:39 +00:00
def onchange_period_from ( self , cr , uid , ids , filter = ' filter_no ' , period_from = False , context = None ) :
period_obj = self . pool . get ( ' account.period ' )
res = { }
if filter == ' filter_period ' :
period_date_start = period_obj . read ( cr , uid , period_from , [ ' date_start ' ] ) [ ' date_start ' ]
res [ ' value ' ] = { ' date_from ' : period_date_start }
return res
return res
def onchange_period_to ( self , cr , uid , ids , filter = ' filter_no ' , period_to = False , context = None ) :
period_obj = self . pool . get ( ' account.period ' )
res = { }
if filter == ' filter_period ' :
period_date_to = period_obj . read ( cr , uid , period_to , [ ' date_stop ' ] ) [ ' date_stop ' ]
res [ ' value ' ] = { ' date_to ' : period_date_to }
return res
return res
2010-06-11 15:49:23 +00:00
def _get_account ( self , cr , uid , context = None ) :
2010-07-14 04:37:02 +00:00
accounts = self . pool . get ( ' account.account ' ) . search ( cr , uid , [ ] , limit = 1 )
if not accounts :
2010-06-11 15:49:23 +00:00
return False
2010-07-14 04:37:02 +00:00
return accounts [ 0 ]
2010-06-11 15:49:23 +00:00
2010-06-11 14:44:32 +00:00
def _get_fiscalyear ( self , cr , uid , context = None ) :
2010-06-11 15:49:23 +00:00
now = time . strftime ( ' % Y- % m- %d ' )
2010-07-14 04:37:02 +00:00
fiscalyears = self . pool . get ( ' account.fiscalyear ' ) . search ( cr , uid , [ ( ' date_start ' , ' < ' , now ) , ( ' date_stop ' , ' > ' , now ) ] , limit = 1 )
if not fiscalyears :
2010-06-11 15:49:23 +00:00
return False
2010-07-14 04:37:02 +00:00
return fiscalyears [ 0 ]
2010-06-11 15:49:23 +00:00
def _get_all_journal ( self , cr , uid , context = None ) :
return self . pool . get ( ' account.journal ' ) . search ( cr , uid , [ ] )
2010-06-11 14:44:32 +00:00
_defaults = {
2010-06-14 10:59:28 +00:00
' date_from ' : time . strftime ( ' % Y-01-01 ' ) ,
' date_to ' : time . strftime ( ' % Y- % m- %d ' ) ,
2010-06-11 15:49:23 +00:00
# 'company_id' : _get_company,
# 'display_account' : 'bal_all',
' fiscalyear_id ' : _get_fiscalyear ,
# 'amount_currency' : True,
' journal_ids ' : _get_all_journal ,
' filter ' : ' filter_no ' ,
2010-06-24 10:11:45 +00:00
' chart_account_id ' : _get_account ,
2010-06-11 14:44:32 +00:00
}
2010-06-25 11:58:36 +00:00
def _build_context ( self , cr , uid , ids , data , context = None ) :
if context is None :
context = { }
2010-06-14 10:59:28 +00:00
result = { }
2010-06-25 12:29:48 +00:00
period_obj = self . pool . get ( ' account.period ' )
2010-07-06 12:13:44 +00:00
result [ ' fiscalyear ' ] = ' fiscalyear_id ' in data [ ' form ' ] and data [ ' form ' ] [ ' fiscalyear_id ' ] or False
result [ ' journal_ids ' ] = ' journal_ids ' in data [ ' form ' ] and data [ ' form ' ] [ ' journal_ids ' ] or False
2010-07-12 13:27:38 +00:00
result [ ' chart_account_id ' ] = ' chart_account_id ' in data [ ' form ' ] and data [ ' form ' ] [ ' chart_account_id ' ] or False
2010-06-14 10:59:28 +00:00
if data [ ' form ' ] [ ' filter ' ] == ' filter_date ' :
result [ ' date_from ' ] = data [ ' form ' ] [ ' date_from ' ]
result [ ' date_to ' ] = data [ ' form ' ] [ ' date_to ' ]
elif data [ ' form ' ] [ ' filter ' ] == ' filter_period ' :
2010-06-25 12:23:07 +00:00
if not data [ ' form ' ] [ ' period_from ' ] or not data [ ' form ' ] [ ' period_to ' ] :
2010-06-25 11:22:16 +00:00
raise osv . except_osv ( _ ( ' Error ' ) , _ ( ' Select Start period and End period ' ) )
2010-06-25 12:23:07 +00:00
elif ( data [ ' form ' ] [ ' period_from ' ] > data [ ' form ' ] [ ' period_to ' ] ) :
raise osv . except_osv ( _ ( ' Error ' ) , _ ( ' Start period should be smaller then End period ' ) )
2010-06-24 14:45:51 +00:00
period_date_start = period_obj . read ( cr , uid , data [ ' form ' ] [ ' period_from ' ] , [ ' date_start ' ] ) [ ' date_start ' ]
period_date_stop = period_obj . read ( cr , uid , data [ ' form ' ] [ ' period_to ' ] , [ ' date_stop ' ] ) [ ' date_stop ' ]
2010-06-14 10:59:28 +00:00
cr . execute ( ' SELECT id FROM account_period WHERE date_start >= %s AND date_stop <= %s ' , ( period_date_start , period_date_stop ) )
2010-06-25 13:17:49 +00:00
result [ ' periods ' ] = map ( lambda x : x [ 0 ] , cr . fetchall ( ) )
2010-06-14 10:59:28 +00:00
return result
2010-06-25 11:58:36 +00:00
def _print_report ( self , cr , uid , ids , data , query_line , context = None ) :
2010-06-24 10:11:45 +00:00
raise ( _ ( ' Error ' ) , _ ( ' not implemented ' ) )
2010-06-11 14:44:32 +00:00
def check_report ( self , cr , uid , ids , context = None ) :
2010-06-25 11:58:36 +00:00
if context is None :
context = { }
2010-06-24 10:11:45 +00:00
data = { }
2010-06-25 11:58:36 +00:00
data [ ' ids ' ] = context . get ( ' active_ids ' , [ ] )
2010-06-24 10:11:45 +00:00
data [ ' model ' ] = context . get ( ' active_model ' , ' ir.ui.menu ' )
data [ ' form ' ] = self . read ( cr , uid , ids , [ ' date_from ' , ' date_to ' , ' fiscalyear_id ' , ' journal_ids ' , ' period_from ' , ' period_to ' , ' filter ' , ' chart_account_id ' ] ) [ 0 ]
2010-07-14 04:37:02 +00:00
if data [ ' form ' ] [ ' filter ' ] == ' filter_period ' : # FIX Me => on_change on period from and to is not working so did this ..but correct it!
2010-07-12 11:00:39 +00:00
start_date = self . onchange_period_from ( cr , uid , ids , data [ ' form ' ] [ ' filter ' ] , data [ ' form ' ] [ ' period_from ' ] )
end_date = self . onchange_period_to ( cr , uid , ids , data [ ' form ' ] [ ' filter ' ] , data [ ' form ' ] [ ' period_to ' ] )
data [ ' form ' ] [ ' date_from ' ] = start_date [ ' value ' ] [ ' date_from ' ]
data [ ' form ' ] [ ' date_to ' ] = end_date [ ' value ' ] [ ' date_to ' ]
2010-06-14 10:59:28 +00:00
used_context = self . _build_context ( cr , uid , ids , data , context )
2010-07-12 11:00:39 +00:00
query_line = self . pool . get ( ' account.move.line ' ) . _query_get ( cr , uid , obj = ' l ' , context = used_context )
2010-06-25 11:58:36 +00:00
if used_context . get ( ' periods ' , False ) :
data [ ' form ' ] [ ' periods ' ] = used_context [ ' periods ' ]
2010-06-25 13:17:49 +00:00
else :
data [ ' form ' ] [ ' periods ' ] = [ ]
2010-06-25 11:58:36 +00:00
return self . _print_report ( cr , uid , ids , data , query_line , context = context )
2010-07-12 05:42:20 +00:00
2010-06-11 14:44:32 +00:00
account_common_report ( )
2010-07-14 04:37:02 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: