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
2014-04-02 16:40:53 +00:00
from openerp . osv import osv
2012-12-06 14:56:32 +00:00
from openerp . report import report_sxw
2008-08-24 14:45:43 +00:00
2014-04-02 16:40:53 +00:00
2008-08-24 14:45:43 +00:00
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 ) :
2012-12-15 16:59:29 +00:00
if not ref_ids : return [ ]
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 = { }
2015-11-03 20:38:53 +00:00
filters = [
' date >= %(date1)s ' ,
' date <= %(date2)s ' ,
]
params = {
' date1 ' : form [ ' date1 ' ] ,
' date2 ' : form [ ' date2 ' ]
}
2010-08-10 05:03:49 +00:00
if form [ ' journal_ids ' ] :
2015-11-03 20:38:53 +00:00
filters . append ( ' journal_id IN %(journal_ids)s ' )
params [ ' journal_ids ' ] = tuple ( form [ ' journal_ids ' ] )
2008-08-24 14:45:43 +00:00
else :
2015-11-03 20:38:53 +00:00
filters . append ( ' journal_id IS NOT NULL ' )
2008-08-24 14:45:43 +00:00
2015-11-03 20:38:53 +00:00
self . cr . execute (
" SELECT id FROM account_analytic_line WHERE " + ' AND ' . join ( filters ) ,
params
)
2008-08-24 14:45:43 +00:00
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 :
2015-11-03 20:38:53 +00:00
params [ ' selected_ids ' ] = tuple ( selected_ids )
filters = [
' aal.account_id = aaa.id ' ,
' aal.id IN %(selected_ids)s ' ,
' aal.date >= %(date1)s ' ,
' aal.date <= %(date2)s ' ,
]
if form [ ' journal_ids ' ] :
filters . append ( ' aal.journal_id in %(journal_ids)s ' )
else :
filters . append ( ' aal.journal_id IS NOT NULL ' )
self . cr . execute (
" SELECT SUM(aal.amount) AS amt, SUM(aal.unit_amount) AS qty "
" FROM account_analytic_line AS aal, account_analytic_account AS aaa "
" WHERE " + ' AND ' . join ( filters ) ,
params
)
2010-09-16 13:20:56 +00:00
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 ' ]
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
2015-11-03 20:38:53 +00:00
filters = [
' aal.account_id = aaa.id ' ,
' aal.date >= %(date1)s ' ,
' aal.date <= %(date2)s ' ,
]
params = {
' date1 ' : form [ ' date1 ' ] ,
' date2 ' : form [ ' date2 ' ] ,
}
2010-08-10 05:03:49 +00:00
if form [ ' journal_ids ' ] :
2015-11-03 20:38:53 +00:00
filters . append ( ' aal.journal_id IN %(journal_ids)s ' )
params [ ' journal_ids ' ] = tuple ( form [ ' journal_ids ' ] )
2008-08-24 14:45:43 +00:00
else :
2015-11-03 20:38:53 +00:00
filters . append ( ' aal.journal_id IS NOT NULL ' )
2008-08-24 14:45:43 +00:00
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 :
2015-11-03 20:38:53 +00:00
filters . append ( ' aal.id IN %(selected_ids)s ' )
params [ ' selected_ids ' ] = tuple ( selected_ids )
self . cr . execute (
" 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 "
" WHERE " + ' AND ' . join ( filters )
+ " GROUP BY aal.account_id,aaa.name,aaa.code ORDER BY aal.account_id "
)
2008-08-24 14:45:43 +00:00
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
2014-04-02 16:40:53 +00:00
class report_crossoveredanalyticplans ( osv . AbstractModel ) :
_name = ' report.account_analytic_plans.report_crossoveredanalyticplans '
_inherit = ' report.abstract_report '
_template = ' account_analytic_plans.report_crossoveredanalyticplans '
_wrapped_report_class = crossovered_analytic
2008-08-24 14:45:43 +00:00
2014-04-02 16:40:53 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: