2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2008-08-24 14:45:43 +00:00
##############################################################################
2010-09-01 10:36:32 +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-08-24 14:45:43 +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.
2008-08-24 14:45:43 +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.
2008-08-24 14:45:43 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
2010-09-01 10:36:32 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2008-08-24 14:45:43 +00:00
#
##############################################################################
import time
2010-07-02 14:09:05 +00:00
2008-08-24 14:45:43 +00:00
from report import report_sxw
class crossovered_analytic ( report_sxw . rml_parse ) :
def __init__ ( self , cr , uid , name , context ) :
2010-07-02 14:09:05 +00:00
super ( crossovered_analytic , self ) . __init__ ( cr , uid , name , context = context )
2008-08-24 14:45:43 +00:00
self . localcontext . update ( {
' time ' : time ,
' lines ' : self . _lines ,
2010-10-11 10:23:00 +00:00
' ref_lines ' : self . _ref_lines ,
' find_children ' : self . find_children ,
2008-08-24 14:45:43 +00:00
} )
2010-07-02 14:09:05 +00:00
self . base_amount = 0.00
2008-08-24 14:45:43 +00:00
2012-03-05 18:40:03 +00:00
def find_children ( self , ref_ids ) :
2008-08-24 14:45:43 +00:00
to_return_ids = [ ]
final_list = [ ]
parent_list = [ ]
set_list = [ ]
2010-10-11 11:25:50 +00:00
analytic_obj = self . pool . get ( ' account.analytic.account ' )
2008-08-24 14:45:43 +00:00
for id in ref_ids :
# to avoid duplicate entries
if id not in to_return_ids :
2010-10-11 11:25:50 +00:00
to_return_ids . append ( analytic_obj . search ( self . cr , self . uid , [ ( ' parent_id ' , ' child_of ' , [ id ] ) ] ) )
data_accnt = analytic_obj . browse ( self . cr , self . uid , to_return_ids [ 0 ] )
2008-08-24 14:45:43 +00:00
for data in data_accnt :
if data . parent_id and data . parent_id . id == ref_ids [ 0 ] :
parent_list . append ( data . id )
final_list . append ( ref_ids [ 0 ] )
set_list = self . set_account ( parent_list )
final_list . extend ( set_list )
return final_list #to_return_ids[0]
2012-03-05 18:40:03 +00:00
def set_account ( self , cats ) :
2008-08-24 14:45:43 +00:00
lst = [ ]
2010-10-11 11:25:50 +00:00
category = self . pool . get ( ' account.analytic.account ' ) . read ( self . cr , self . uid , cats )
2008-08-24 14:45:43 +00:00
for cat in category :
lst . append ( cat [ ' id ' ] )
if cat [ ' child_ids ' ] :
lst . extend ( self . set_account ( cat [ ' child_ids ' ] ) )
return lst
2012-03-05 18:40:03 +00:00
def _ref_lines ( self , form ) :
2008-08-24 14:45:43 +00:00
result = [ ]
res = { }
acc_pool = self . pool . get ( ' account.analytic.account ' )
line_pool = self . pool . get ( ' account.analytic.line ' )
self . dict_acc_ref = { }
2010-08-10 05:03:49 +00:00
if form [ ' journal_ids ' ] :
journal = " in ( " + ' , ' . join ( map ( lambda x : str ( x ) , form [ ' journal_ids ' ] ) ) + " ) "
2008-08-24 14:45:43 +00:00
else :
journal = ' is not null '
2010-10-11 11:25:50 +00:00
query_general = " SELECT id FROM account_analytic_line WHERE (journal_id " + journal + " ) AND date>= ' " + str ( form [ ' date1 ' ] ) + " ' " " AND date<= ' " + str ( form [ ' date2 ' ] ) + " ' "
2008-08-24 14:45:43 +00:00
self . cr . execute ( query_general )
2010-10-11 11:25:50 +00:00
l_ids = self . cr . fetchall ( )
2008-08-24 14:45:43 +00:00
line_ids = [ x [ 0 ] for x in l_ids ]
obj_line = line_pool . browse ( self . cr , self . uid , line_ids )
#this structure will be usefull for easily knowing the account_analytic_line that are related to the reference account. At this purpose, we save the move_id of analytic lines.
self . dict_acc_ref [ form [ ' ref ' ] ] = [ ]
2010-10-11 11:25:50 +00:00
children_list = acc_pool . search ( self . cr , self . uid , [ ( ' parent_id ' , ' child_of ' , [ form [ ' ref ' ] ] ) ] )
2008-08-24 14:45:43 +00:00
for obj in obj_line :
if obj . account_id . id in children_list :
if obj . move_id and obj . move_id . id not in self . dict_acc_ref [ form [ ' ref ' ] ] :
self . dict_acc_ref [ form [ ' ref ' ] ] . append ( obj . move_id . id )
2010-10-11 11:25:50 +00:00
res [ ' ref_name ' ] = acc_pool . name_get ( self . cr , self . uid , [ form [ ' ref ' ] ] ) [ 0 ] [ 1 ]
res [ ' ref_code ' ] = acc_pool . browse ( self . cr , self . uid , form [ ' ref ' ] ) . code
2008-08-24 14:45:43 +00:00
self . final_list = children_list
2010-10-11 11:25:50 +00:00
selected_ids = line_pool . search ( self . cr , self . uid , [ ( ' account_id ' , ' in ' , self . final_list ) ] )
2010-12-10 19:31:31 +00:00
res [ ' ref_qty ' ] = 0.0
res [ ' ref_amt ' ] = 0.0
self . base_amount = 0.0
2010-09-16 13:20:56 +00:00
if selected_ids :
2010-10-11 11:25:50 +00:00
query = " SELECT SUM(aal.amount) AS amt, SUM(aal.unit_amount) AS qty FROM account_analytic_line AS aal, account_analytic_account AS aaa \
WHERE aal . account_id = aaa . id AND aal . id IN ( " + ' , ' .join(map(str,selected_ids))+ " ) AND ( aal . journal_id " + journal + " ) AND aal . date > = ' " + str(form[ ' date1 ' ]) + " ' " " AND aal . date < = ' " + str(form[ ' date2 ' ]) + " ' "
2010-10-11 10:23:00 +00:00
2010-09-16 13:20:56 +00:00
self . cr . execute ( query )
info = self . cr . dictfetchall ( )
2010-10-11 11:25:50 +00:00
res [ ' ref_qty ' ] = info [ 0 ] [ ' qty ' ]
res [ ' ref_amt ' ] = info [ 0 ] [ ' amt ' ]
self . base_amount = info [ 0 ] [ ' amt ' ]
2010-12-10 19:31:31 +00:00
2008-08-24 14:45:43 +00:00
result . append ( res )
return result
2012-03-05 17:58:42 +00:00
def _lines ( self , form , ids = None ) :
if ids is None :
ids = { }
2008-08-24 14:45:43 +00:00
if not ids :
ids = self . ids
2010-08-10 05:03:49 +00:00
if form [ ' journal_ids ' ] :
journal = " in ( " + ' , ' . join ( map ( lambda x : str ( x ) , form [ ' journal_ids ' ] ) ) + " ) "
2008-08-24 14:45:43 +00:00
else :
journal = ' is not null '
acc_pool = self . pool . get ( ' account.analytic.account ' )
2010-10-11 11:25:50 +00:00
line_pool = self . pool . get ( ' account.analytic.line ' )
acc_id = [ ]
final = [ ]
self . list_ids = [ ]
2008-08-24 14:45:43 +00:00
self . final_list = self . find_children ( ids )
for acc_id in self . final_list :
2010-10-11 11:25:50 +00:00
selected_ids = line_pool . search ( self . cr , self . uid , [ ( ' account_id ' , ' = ' , acc_id ) , ( ' move_id ' , ' in ' , self . dict_acc_ref [ form [ ' ref ' ] ] ) ] )
2008-08-24 14:45:43 +00:00
if selected_ids :
2010-10-11 11:25:50 +00:00
query = " SELECT aaa.code AS code, SUM(aal.amount) AS amt, SUM(aal.unit_amount) AS qty, aaa.name AS acc_name, aal.account_id AS id FROM account_analytic_line AS aal, account_analytic_account AS aaa \
2008-08-24 14:45:43 +00:00
WHERE aal . account_id = aaa . id AND aal . id IN ( " + ' , ' .join(map(str,selected_ids))+ " ) AND ( aal . journal_id " + journal + " ) AND aal . date > = ' " + str(form[ ' date1 ' ]) + " ' " " AND aal . date < = ' " + str(form[ ' date2 ' ]) + " ' " " GROUP BY aal . account_id , aaa . name , aaa . code ORDER BY aal . account_id "
self . cr . execute ( query )
res = self . cr . dictfetchall ( )
if res :
for element in res :
2010-10-11 11:25:50 +00:00
if self . base_amount < > 0.00 :
element [ ' perc ' ] = ( element [ ' amt ' ] / self . base_amount ) * 100.00
2008-08-24 14:45:43 +00:00
else :
2010-10-11 11:25:50 +00:00
element [ ' perc ' ] = 0.00
2008-08-24 14:45:43 +00:00
else :
2010-10-11 11:25:50 +00:00
result = { }
res = [ ]
result [ ' id ' ] = acc_id
data_account = acc_pool . browse ( self . cr , self . uid , acc_id )
result [ ' acc_name ' ] = data_account . name
2008-08-24 14:45:43 +00:00
result [ ' code ' ] = data_account . code
2010-10-11 11:25:50 +00:00
result [ ' amt ' ] = result [ ' qty ' ] = result [ ' perc ' ] = 0.00
2008-08-24 14:45:43 +00:00
if not form [ ' empty_line ' ] :
res . append ( result )
else :
2010-07-02 14:09:05 +00:00
result = { }
res = [ ]
result [ ' id ' ] = acc_id
2010-10-11 11:25:50 +00:00
data_account = acc_pool . browse ( self . cr , self . uid , acc_id )
2010-07-02 14:09:05 +00:00
result [ ' acc_name ' ] = data_account . name
2008-08-24 14:45:43 +00:00
result [ ' code ' ] = data_account . code
2010-07-02 14:09:05 +00:00
result [ ' amt ' ] = result [ ' qty ' ] = result [ ' perc ' ] = 0.00
2008-08-24 14:45:43 +00:00
if not form [ ' empty_line ' ] :
res . append ( result )
for item in res :
2010-07-02 14:09:05 +00:00
obj_acc = acc_pool . name_get ( self . cr , self . uid , [ item [ ' id ' ] ] )
item [ ' acc_name ' ] = obj_acc [ 0 ] [ 1 ]
2008-08-24 14:45:43 +00:00
final . append ( item )
return final
2010-09-01 10:36:32 +00:00
report_sxw . report_sxw ( ' report.account.analytic.account.crossovered.analytic ' , ' account.analytic.account ' , ' addons/account_analytic_plans/report/crossovered_analytic.rml ' , parser = crossovered_analytic , header = ' internal ' )
2008-08-24 14:45:43 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: