2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2009-01-22 13:00:17 +00:00
##############################################################################
#
# Copyright (c) 2008 JAILLET Simon - CrysaLEAD - www.crysalead.fr
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
2013-05-14 14:28:37 +00:00
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2009-01-22 13:00:17 +00:00
#
##############################################################################
import time
2012-12-06 14:56:32 +00:00
from openerp . report import report_sxw
2015-05-20 11:46:52 +00:00
from openerp . tools . safe_eval import safe_eval as eval
2009-01-22 13:00:17 +00:00
2014-04-22 12:11:05 +00:00
2009-01-22 13:00:17 +00:00
class base_report ( report_sxw . rml_parse ) :
2010-08-18 04:53:14 +00:00
def __init__ ( self , cr , uid , name , context = None ) :
2009-10-09 11:49:00 +00:00
super ( base_report , self ) . __init__ ( cr , uid , name , context = context )
2010-08-13 12:20:05 +00:00
self . localcontext . update ( {
2009-01-22 13:00:17 +00:00
' time ' : time ,
' _load ' : self . _load ,
' _get_variable ' : self . _get_variable ,
' _set_variable ' : self . _set_variable ,
} )
self . context = context
2010-08-18 04:53:14 +00:00
def _load ( self , name , form ) :
2010-10-28 13:03:49 +00:00
fiscalyear = self . pool . get ( ' account.fiscalyear ' ) . browse ( self . cr , self . uid , form [ ' fiscalyear_id ' ] )
period_ids = self . pool . get ( ' account.period ' ) . search ( self . cr , self . uid , [ ( ' fiscalyear_id ' , ' = ' , form [ ' fiscalyear_id ' ] ) ] )
2009-01-22 13:00:17 +00:00
2010-10-28 13:03:49 +00:00
if period_ids :
self . cr . execute ( " SELECT MIN(date_start) AS date_start, MAX(date_stop) AS date_stop FROM account_period WHERE id = ANY( %s ) " , ( period_ids , ) )
dates = self . cr . dictfetchall ( )
else :
dates = False
if dates :
self . _set_variable ( ' date_start ' , dates [ 0 ] [ ' date_start ' ] )
self . _set_variable ( ' date_stop ' , dates [ 0 ] [ ' date_stop ' ] )
2009-01-22 13:00:17 +00:00
2009-11-25 11:32:07 +00:00
self . cr . execute ( " SELECT l10n_fr_line.code,definition FROM l10n_fr_line LEFT JOIN l10n_fr_report ON l10n_fr_report.id=report_id WHERE l10n_fr_report.code= %s " , ( name , ) )
2010-08-18 04:53:14 +00:00
datas = self . cr . dictfetchall ( )
2009-11-25 11:32:07 +00:00
for line in datas :
2010-10-28 13:03:49 +00:00
self . _load_accounts ( form , line [ ' code ' ] , eval ( line [ ' definition ' ] ) , fiscalyear , period_ids )
2009-01-22 13:00:17 +00:00
2010-08-18 04:53:14 +00:00
def _set_variable ( self , variable , valeur ) :
self . localcontext . update ( { variable : valeur } )
2009-01-22 13:00:17 +00:00
2010-08-18 04:53:14 +00:00
def _get_variable ( self , variable ) :
2009-01-22 13:00:17 +00:00
return self . localcontext [ variable ]
2010-10-28 13:03:49 +00:00
def _load_accounts ( self , form , code , definition , fiscalyear , period_ids ) :
2010-08-18 04:53:14 +00:00
accounts = { }
2009-01-22 13:00:17 +00:00
for x in definition [ ' load ' ] :
2010-08-18 04:53:14 +00:00
p = x . split ( " : " )
accounts [ p [ 1 ] ] = [ p [ 0 ] , p [ 2 ] ]
sum = 0.0
if fiscalyear . state != ' done ' or not code . startswith ( ' bpcheck ' ) :
2010-10-28 13:03:49 +00:00
query_params = [ ]
2010-08-18 04:53:14 +00:00
query_cond = " ( "
2009-01-22 13:00:17 +00:00
for account in accounts :
2010-10-28 13:03:49 +00:00
query_cond + = " aa.code LIKE ' " + account + " %% ' OR "
2009-01-22 13:00:17 +00:00
query_cond = query_cond [ : - 4 ] + " ) "
2010-08-18 04:53:14 +00:00
if len ( definition [ ' except ' ] ) > 0 :
2009-01-22 13:00:17 +00:00
query_cond = query_cond + " and ( "
for account in definition [ ' except ' ] :
2010-10-28 13:03:49 +00:00
query_cond + = " aa.code NOT LIKE ' " + account + " %% ' AND "
2009-01-22 13:00:17 +00:00
query_cond = query_cond [ : - 5 ] + " ) "
2010-08-18 04:53:14 +00:00
closed_cond = " "
if fiscalyear . state == ' done ' :
2010-10-28 13:03:49 +00:00
closed_cond = " AND (aml.move_id NOT IN (SELECT account_move.id as move_id FROM account_move WHERE period_id = ANY( %s ) AND journal_id=(SELECT res_id FROM ir_model_data WHERE name= ' closing_journal ' AND module= ' l10n_fr ' )) OR (aa.type != ' income ' AND aa.type != ' expense ' )) "
query_params . append ( list ( period_ids ) )
2009-01-22 13:00:17 +00:00
2010-10-28 13:03:49 +00:00
query = " SELECT aa.code AS code, SUM(debit) as debit, SUM(credit) as credit " \
" FROM account_move_line aml LEFT JOIN account_account aa ON aa.id=aml.account_id " \
" WHERE " + query_cond + closed_cond + " AND aml.state= ' valid ' AND aml.period_id = ANY( %s ) GROUP BY code "
query_params . append ( list ( period_ids ) )
self . cr . execute ( query , query_params )
2009-01-22 13:00:17 +00:00
lines = self . cr . dictfetchall ( )
for line in lines :
for account in accounts :
if ( line [ " code " ] . startswith ( account ) ) :
operator = accounts [ account ] [ 0 ]
type = accounts [ account ] [ 1 ]
value = 0.0
2010-08-18 04:53:14 +00:00
if ( type == " S " ) :
2009-01-22 13:00:17 +00:00
value = line [ " debit " ] - line [ " credit " ]
2010-08-18 04:53:14 +00:00
elif ( type == " D " ) :
2009-01-22 13:00:17 +00:00
value = line [ " debit " ] - line [ " credit " ]
2012-11-29 08:12:48 +00:00
if ( abs ( value ) < 0.001 ) : value = 0.0
2010-08-18 04:53:14 +00:00
elif ( type == " C " ) :
2009-01-22 13:00:17 +00:00
value = line [ " credit " ] - line [ " debit " ]
2012-11-29 08:12:48 +00:00
if ( abs ( value ) < 0.001 ) : value = 0.0
2010-08-18 04:53:14 +00:00
if ( operator == ' + ' ) :
sum + = value
2009-01-22 13:00:17 +00:00
else :
2010-08-18 04:53:14 +00:00
sum - = value
2009-01-22 13:00:17 +00:00
break
self . _set_variable ( code , sum )
2012-03-05 18:40:03 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: