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/>.
#
##############################################################################
2010-07-15 10:40:15 +00:00
2010-06-11 14:44:32 +00:00
import time
2010-07-15 10:40:15 +00:00
from lxml import etree
2010-06-11 14:44:32 +00:00
from osv import fields , osv
from tools . translate import _
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
2012-01-25 09:29:32 +00:00
def onchange_chart_id ( self , cr , uid , ids , chart_account_id = False , context = None ) :
2012-08-20 10:59:42 +00:00
res = { }
2012-01-25 09:29:32 +00:00
if chart_account_id :
company_id = self . pool . get ( ' account.account ' ) . browse ( cr , uid , chart_account_id , context = context ) . company_id . id
2012-08-20 10:59:42 +00:00
res [ ' value ' ] = { ' company_id ' : company_id }
return res
2012-01-25 09:29:32 +00:00
2010-06-11 14:44:32 +00:00
_columns = {
2011-04-15 10:09:01 +00:00
' chart_account_id ' : fields . many2one ( ' account.account ' , ' Chart of Account ' , help = ' Select Charts of Accounts ' , required = True , domain = [ ( ' parent_id ' , ' = ' , False ) ] ) ,
2012-01-25 09:29:32 +00:00
' company_id ' : fields . related ( ' chart_account_id ' , ' company_id ' , type = ' many2one ' , relation = ' res.company ' , string = ' Company ' , readonly = True ) ,
2011-04-15 10:09:01 +00:00
' fiscalyear_id ' : fields . many2one ( ' account.fiscalyear ' , ' Fiscal Year ' , help = ' Keep empty for all open fiscal year ' ) ,
2010-08-14 12:12:31 +00:00
' filter ' : fields . selection ( [ ( ' filter_no ' , ' No Filters ' ) , ( ' filter_date ' , ' Date ' ) , ( ' filter_period ' , ' Periods ' ) ] , " Filter by " , required = True ) ,
2011-04-15 10:09:01 +00:00
' period_from ' : fields . many2one ( ' account.period ' , ' Start Period ' ) ,
' period_to ' : fields . many2one ( ' account.period ' , ' End Period ' ) ,
2011-09-26 17:18:37 +00:00
' journal_ids ' : fields . many2many ( ' account.journal ' , string = ' Journals ' , required = True ) ,
2010-06-25 11:58:36 +00:00
' date_from ' : fields . date ( " Start Date " ) ,
' date_to ' : fields . date ( " End Date " ) ,
2010-12-10 10:31:12 +00:00
' target_move ' : fields . selection ( [ ( ' posted ' , ' All Posted Entries ' ) ,
( ' all ' , ' All Entries ' ) ,
] , ' Target Moves ' , required = True ) ,
2010-10-04 09:43:20 +00:00
}
2010-06-11 14:44:32 +00:00
2012-01-25 09:29:32 +00:00
def _check_company_id ( self , cr , uid , ids , context = None ) :
for wiz in self . browse ( cr , uid , ids , context = context ) :
company_id = wiz . company_id . id
2012-01-26 18:53:56 +00:00
if wiz . fiscalyear_id and company_id != wiz . fiscalyear_id . company_id . id :
2012-01-25 09:29:32 +00:00
return False
if wiz . period_from and company_id != wiz . period_from . company_id . id :
return False
if wiz . period_to and company_id != wiz . period_to . company_id . id :
return False
return True
_constraints = [
( _check_company_id , ' The fiscalyear, periods or chart of account chosen have to belong to the same company. ' , [ ' chart_account_id ' , ' fiscalyear_id ' , ' period_from ' , ' period_to ' ] ) ,
]
2010-07-15 10:40:15 +00:00
def fields_view_get ( self , cr , uid , view_id = None , view_type = ' form ' , context = None , toolbar = False , submenu = False ) :
2012-06-05 10:50:33 +00:00
if context is None : context = { }
2010-07-15 10:40:15 +00:00
res = super ( account_common_report , self ) . fields_view_get ( cr , uid , view_id = view_id , view_type = view_type , context = context , toolbar = toolbar , submenu = False )
if context . get ( ' active_model ' , False ) == ' account.account ' and view_id :
doc = etree . XML ( res [ ' arch ' ] )
nodes = doc . xpath ( " //field[@name= ' chart_account_id ' ] " )
for node in nodes :
node . set ( ' readonly ' , ' 1 ' )
node . set ( ' help ' , ' If you print the report from Account list/form view it will not consider Charts of account ' )
res [ ' arch ' ] = etree . tostring ( doc )
return res
2010-08-03 07:29:43 +00:00
def onchange_filter ( self , cr , uid , ids , filter = ' filter_no ' , fiscalyear_id = False , context = None ) :
2011-05-06 06:27:58 +00:00
res = { ' value ' : { } }
2010-07-12 06:13:10 +00:00
if filter == ' filter_no ' :
2010-07-15 11:40:44 +00:00
res [ ' value ' ] = { ' period_from ' : False , ' period_to ' : False , ' date_from ' : False , ' date_to ' : False }
if filter == ' filter_date ' :
res [ ' value ' ] = { ' period_from ' : False , ' period_to ' : False , ' date_from ' : time . strftime ( ' % Y-01-01 ' ) , ' date_to ' : time . strftime ( ' % Y- % m- %d ' ) }
2010-08-03 07:29:43 +00:00
if filter == ' filter_period ' and fiscalyear_id :
2010-08-03 07:22:53 +00:00
start_period = end_period = False
2010-09-07 14:33:15 +00:00
cr . execute ( '''
2010-10-01 11:22:06 +00:00
SELECT * FROM ( SELECT p . id
FROM account_period p
LEFT JOIN account_fiscalyear f ON ( p . fiscalyear_id = f . id )
WHERE f . id = % s
2011-09-20 05:26:46 +00:00
AND p . special = false
2011-05-04 14:52:33 +00:00
ORDER BY p . date_start ASC , p . special ASC
2010-09-07 14:33:15 +00:00
LIMIT 1 ) AS period_start
2011-10-18 13:33:15 +00:00
UNION ALL
2010-09-07 14:33:15 +00:00
SELECT * FROM ( SELECT p . id
FROM account_period p
LEFT JOIN account_fiscalyear f ON ( p . fiscalyear_id = f . id )
WHERE f . id = % s
AND p . date_start < NOW ( )
2011-09-20 05:26:46 +00:00
AND p . special = false
2010-09-07 14:33:15 +00:00
ORDER BY p . date_stop DESC
LIMIT 1 ) AS period_stop ''' , (fiscalyear_id, fiscalyear_id))
2010-08-03 07:22:53 +00:00
periods = [ i [ 0 ] for i in cr . fetchall ( ) ]
2010-09-07 14:33:15 +00:00
if periods and len ( periods ) > 1 :
2010-08-03 07:22:53 +00:00
start_period = periods [ 0 ]
end_period = periods [ 1 ]
res [ ' value ' ] = { ' period_from ' : start_period , ' period_to ' : end_period , ' date_from ' : False , ' date_to ' : False }
2010-07-20 10:05:04 +00:00
return res
2010-07-12 11:00:39 +00:00
2010-06-11 15:49:23 +00:00
def _get_account ( self , cr , uid , context = None ) :
2012-04-16 08:12:56 +00:00
user = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context = context )
accounts = self . pool . get ( ' account.account ' ) . search ( cr , uid , [ ( ' parent_id ' , ' = ' , False ) , ( ' company_id ' , ' = ' , user . company_id . id ) ] , limit = 1 )
2010-07-20 10:05:04 +00:00
return accounts and accounts [ 0 ] or False
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 ' )
2012-11-12 16:47:33 +00:00
company_id = False
2012-11-09 12:43:09 +00:00
ids = context . get ( ' active_ids ' , [ ] )
2012-11-12 16:47:33 +00:00
if ids :
company_id = self . browse ( cr , uid , ids [ 0 ] , context = context ) . company_id . id
fiscalyears = self . pool . get ( ' account.fiscalyear ' ) . search ( cr , uid , [ ( ' date_start ' , ' < ' , now ) , ( ' date_stop ' , ' > ' , now ) , ( ' company_id ' , ' = ' , company_id ) ] , limit = 1 )
2010-07-20 10:05:04 +00:00
return fiscalyears and fiscalyears [ 0 ] or False
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-10-13 09:45:27 +00:00
' fiscalyear_id ' : _get_fiscalyear ,
2012-03-13 12:51:19 +00:00
' company_id ' : lambda self , cr , uid , c : self . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' account.common.report ' , context = c ) ,
2010-06-11 15:49:23 +00:00
' journal_ids ' : _get_all_journal ,
' filter ' : ' filter_no ' ,
2010-06-24 10:11:45 +00:00
' chart_account_id ' : _get_account ,
2010-12-10 10:31:12 +00:00
' target_move ' : ' posted ' ,
2010-08-03 04:25:54 +00:00
}
2010-06-11 14:44:32 +00:00
2010-07-15 23:36:56 +00:00
def _build_contexts ( self , cr , uid , ids , data , context = None ) :
2010-06-25 11:58:36 +00:00
if context is None :
context = { }
2010-06-14 10:59:28 +00:00
result = { }
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 ' ] :
2012-07-25 07:33:57 +00:00
raise osv . except_osv ( _ ( ' Error! ' ) , _ ( ' Select a starting and an ending period. ' ) )
2010-10-01 15:24:03 +00:00
result [ ' period_from ' ] = data [ ' form ' ] [ ' period_from ' ]
result [ ' period_to ' ] = data [ ' form ' ] [ ' period_to ' ]
2010-10-01 11:32:41 +00:00
return result
2010-06-14 10:59:28 +00:00
2010-10-01 11:32:41 +00:00
def _print_report ( self , cr , uid , ids , data , context = None ) :
2012-08-06 15:44:10 +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 ' )
2011-02-04 12:58:09 +00:00
data [ ' form ' ] = self . read ( cr , uid , ids , [ ' date_from ' , ' date_to ' , ' fiscalyear_id ' , ' journal_ids ' , ' period_from ' , ' period_to ' , ' filter ' , ' chart_account_id ' , ' target_move ' ] , context = context ) [ 0 ]
2011-02-15 09:20:57 +00:00
for field in [ ' fiscalyear_id ' , ' chart_account_id ' , ' period_from ' , ' period_to ' ] :
2011-02-04 12:58:09 +00:00
if isinstance ( data [ ' form ' ] [ field ] , tuple ) :
data [ ' form ' ] [ field ] = data [ ' form ' ] [ field ] [ 0 ]
2010-10-01 11:32:41 +00:00
used_context = self . _build_contexts ( cr , uid , ids , data , context = context )
2010-07-15 23:36:56 +00:00
data [ ' form ' ] [ ' periods ' ] = used_context . get ( ' periods ' , False ) and used_context [ ' periods ' ] or [ ]
2010-10-01 08:12:02 +00:00
data [ ' form ' ] [ ' used_context ' ] = used_context
2010-10-01 11:32:41 +00:00
return self . _print_report ( cr , uid , ids , data , context = context )
2010-07-12 05:42:20 +00:00
2010-06-11 14:44:32 +00:00
account_common_report ( )
2010-08-14 12:12:31 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: