2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2006-12-07 13:41:40 +00:00
##############################################################################
2009-11-30 10:24:22 +00:00
#
2009-10-14 11:15:34 +00:00
# OpenERP, Open Source Management Solution
2010-01-12 09:18:39 +00:00
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
2008-06-16 11:00:21 +00:00
#
2008-11-03 19:18:56 +00:00
# This program is free software: you can redistribute it and/or modify
2009-10-14 11:15:34 +00:00
# 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.
2006-12-07 13:41:40 +00:00
#
2008-11-03 19:18:56 +00:00
# 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
2009-10-14 11:15:34 +00:00
# GNU Affero General Public License for more details.
2006-12-07 13:41:40 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
2009-11-30 10:24:22 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2006-12-07 13:41:40 +00:00
#
##############################################################################
import pooler
import time
from report import report_sxw
class account_analytic_cost_ledger ( report_sxw . rml_parse ) :
2008-07-22 15:11:28 +00:00
def __init__ ( self , cr , uid , name , context ) :
2009-10-09 11:49:00 +00:00
super ( account_analytic_cost_ledger , self ) . __init__ ( cr , uid , name , context = context )
2008-07-22 15:11:28 +00:00
self . localcontext . update ( {
' time ' : time ,
' lines_g ' : self . _lines_g ,
' lines_a ' : self . _lines_a ,
' account_sum_debit ' : self . _account_sum_debit ,
' account_sum_credit ' : self . _account_sum_credit ,
' account_sum_balance ' : self . _account_sum_balance ,
' sum_debit ' : self . _sum_debit ,
' sum_credit ' : self . _sum_credit ,
' sum_balance ' : self . _sum_balance ,
} )
2007-07-19 13:36:04 +00:00
2011-11-28 13:01:57 +00:00
def _get_children ( self , account_id ) :
result = [ ]
def _get_rec ( account_id ) :
analytic_obj = self . pool . get ( ' account.analytic.account ' )
analytic_search_ids = analytic_obj . search ( self . cr , self . uid , [ ( ' id ' , ' = ' , account_id ) ] )
analytic_datas = analytic_obj . browse ( self . cr , self . uid , analytic_search_ids )
result . append ( account_id )
for account in analytic_datas :
for child in account . child_ids :
result . append ( child . id )
2011-11-30 06:15:27 +00:00
_get_rec ( child . id )
2011-11-28 13:01:57 +00:00
return result
child_ids = _get_rec ( account_id )
return child_ids
2008-07-22 15:11:28 +00:00
def _lines_g ( self , account_id , date1 , date2 ) :
2011-11-28 13:01:57 +00:00
chid_ids = self . _get_children ( account_id )
2008-07-22 15:11:28 +00:00
self . cr . execute ( " SELECT sum(aal.amount) AS balance, aa.code AS code, aa.name AS name, aa.id AS id \
FROM account_account AS aa , account_analytic_line AS aal \
2011-11-28 13:01:57 +00:00
WHERE ( aal . account_id IN % s ) AND ( aal . date > = % s ) AND ( aal . date < = % s ) AND ( aal . general_account_id = aa . id ) AND aa . active \
GROUP BY aa . code , aa . name , aa . id ORDER BY aa . code " , (tuple(chid_ids), date1, date2))
2008-07-22 15:11:28 +00:00
res = self . cr . dictfetchall ( )
2006-12-07 13:41:40 +00:00
2008-07-22 15:11:28 +00:00
for r in res :
if r [ ' balance ' ] > 0 :
r [ ' debit ' ] = r [ ' balance ' ]
r [ ' credit ' ] = 0.0
elif r [ ' balance ' ] < 0 :
r [ ' debit ' ] = 0.0
r [ ' credit ' ] = - r [ ' balance ' ]
else :
r [ ' debit ' ] = 0.0
r [ ' credit ' ] = 0.0
return res
2007-07-17 08:28:19 +00:00
2008-07-22 15:11:28 +00:00
def _lines_a ( self , general_account_id , account_id , date1 , date2 ) :
2011-11-28 13:01:57 +00:00
chid_ids = self . _get_children ( account_id )
2008-07-22 15:11:28 +00:00
self . cr . execute ( " SELECT aal.name AS name, aal.code AS code, aal.amount AS balance, aal.date AS date, aaj.code AS cj FROM account_analytic_line AS aal, account_analytic_journal AS aaj \
2011-11-28 13:01:57 +00:00
WHERE ( aal . general_account_id = % s ) AND ( aal . account_id IN % s ) AND ( aal . date > = % s ) AND ( aal . date < = % s ) \
2008-07-22 15:11:28 +00:00
AND ( aal . journal_id = aaj . id ) \
2011-11-28 13:01:57 +00:00
ORDER BY aal . date , aaj . code , aal . code " , (general_account_id, tuple(chid_ids), date1, date2))
2008-07-22 15:11:28 +00:00
res = self . cr . dictfetchall ( )
2006-12-07 13:41:40 +00:00
2008-07-22 15:11:28 +00:00
for r in res :
if r [ ' balance ' ] > 0 :
r [ ' debit ' ] = r [ ' balance ' ]
r [ ' credit ' ] = 0.0
elif r [ ' balance ' ] < 0 :
r [ ' debit ' ] = 0.0
r [ ' credit ' ] = - r [ ' balance ' ]
else :
r [ ' debit ' ] = 0.0
r [ ' credit ' ] = 0.0
return res
2006-12-07 13:41:40 +00:00
2008-07-22 15:11:28 +00:00
def _account_sum_debit ( self , account_id , date1 , date2 ) :
2011-11-28 13:01:57 +00:00
chid_ids = self . _get_children ( account_id )
self . cr . execute ( " SELECT sum(amount) FROM account_analytic_line WHERE account_id IN %s AND date>= %s AND date<= %s AND amount>0 " , ( tuple ( chid_ids ) , date1 , date2 ) )
2008-07-22 15:11:28 +00:00
return self . cr . fetchone ( ) [ 0 ] or 0.0
2006-12-07 13:41:40 +00:00
2008-07-22 15:11:28 +00:00
def _account_sum_credit ( self , account_id , date1 , date2 ) :
2011-11-28 13:01:57 +00:00
chid_ids = self . _get_children ( account_id )
self . cr . execute ( " SELECT -sum(amount) FROM account_analytic_line WHERE account_id IN %s AND date>= %s AND date<= %s AND amount<0 " , ( tuple ( chid_ids ) , date1 , date2 ) )
2008-07-22 15:11:28 +00:00
return self . cr . fetchone ( ) [ 0 ] or 0.0
2007-07-17 08:28:19 +00:00
2008-07-22 15:11:28 +00:00
def _account_sum_balance ( self , account_id , date1 , date2 ) :
2009-11-30 10:24:22 +00:00
debit = self . _account_sum_debit ( account_id , date1 , date2 )
2008-07-22 15:11:28 +00:00
credit = self . _account_sum_credit ( account_id , date1 , date2 )
return ( debit - credit )
2007-07-17 08:28:19 +00:00
2008-07-22 15:11:28 +00:00
def _sum_debit ( self , accounts , date1 , date2 ) :
ids = map ( lambda x : x . id , accounts )
2011-11-28 13:01:57 +00:00
chid_ids = self . _get_children ( ids [ 0 ] )
2011-11-30 06:15:27 +00:00
if not chid_ids :
2008-07-22 15:11:28 +00:00
return 0.0
2011-11-28 13:01:57 +00:00
self . cr . execute ( " SELECT sum(amount) FROM account_analytic_line WHERE account_id IN %s AND date>= %s AND date<= %s AND amount>0 " , ( tuple ( chid_ids ) , date1 , date2 , ) )
2008-07-22 15:11:28 +00:00
return self . cr . fetchone ( ) [ 0 ] or 0.0
2007-07-17 08:28:19 +00:00
2008-07-22 15:11:28 +00:00
def _sum_credit ( self , accounts , date1 , date2 ) :
ids = map ( lambda x : x . id , accounts )
2011-11-28 13:01:57 +00:00
chid_ids = self . _get_children ( ids [ 0 ] )
2011-11-30 06:15:27 +00:00
if not chid_ids :
2008-07-22 15:11:28 +00:00
return 0.0
2011-11-28 13:01:57 +00:00
self . cr . execute ( " SELECT -sum(amount) FROM account_analytic_line WHERE account_id IN %s AND date>= %s AND date<= %s AND amount<0 " , ( tuple ( chid_ids ) , date1 , date2 , ) )
2008-07-22 15:11:28 +00:00
return self . cr . fetchone ( ) [ 0 ] or 0.0
2006-12-07 13:41:40 +00:00
2008-07-22 15:11:28 +00:00
def _sum_balance ( self , accounts , date1 , date2 ) :
debit = self . _sum_debit ( accounts , date1 , date2 ) or 0.0
credit = self . _sum_credit ( accounts , date1 , date2 ) or 0.0
return ( debit - credit )
2006-12-07 13:41:40 +00:00
2010-08-24 12:37:11 +00:00
report_sxw . report_sxw ( ' report.account.analytic.account.cost_ledger ' , ' account.analytic.account ' , ' addons/account/project/report/cost_ledger.rml ' , parser = account_analytic_cost_ledger , header = " internal " )
2006-12-07 13:41:40 +00:00
2008-07-23 14:41:47 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: