2010-07-15 23:36:56 +00:00
# -*- coding: utf-8 -*-
##############################################################################
2007-07-17 08:28:19 +00:00
#
2008-09-25 08:33:16 +00:00
# Copyright (c) 2005-2006 CamptoCamp
2010-07-15 23:36:56 +00:00
# Copyright (c) 2006-2010 OpenERP S.A
2007-07-17 08:28:19 +00:00
#
# 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
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2010-07-09 06:40:01 +00:00
#
##############################################################################
2007-07-17 08:28:19 +00:00
import time
2010-07-09 06:40:01 +00:00
2007-07-17 08:28:19 +00:00
from report import report_sxw
2010-07-14 12:54:18 +00:00
from common_report_header import common_report_header
2008-09-25 08:33:16 +00:00
import rml_parse
2008-05-19 12:42:45 +00:00
import pooler
2007-07-17 08:28:19 +00:00
2010-07-14 12:54:18 +00:00
class general_ledger ( rml_parse . rml_parse , common_report_header ) :
2009-06-09 23:24:24 +00:00
_name = ' report.account.general.ledger '
2010-07-09 07:19:33 +00:00
def set_context ( self , objects , data , ids , report_type = None ) :
2010-07-09 10:10:05 +00:00
new_ids = ids
2010-07-09 10:23:04 +00:00
self . query = data [ ' form ' ] [ ' query_line ' ]
2010-07-09 10:10:05 +00:00
if ( data [ ' model ' ] == ' ir.ui.menu ' ) :
new_ids = [ data [ ' form ' ] [ ' chart_account_id ' ] ]
2010-07-13 05:08:11 +00:00
self . sortby = data [ ' form ' ] [ ' sortby ' ]
2010-06-24 10:11:45 +00:00
objects = self . pool . get ( ' account.account ' ) . browse ( self . cr , self . uid , new_ids )
2010-07-09 10:23:04 +00:00
super ( general_ledger , self ) . set_context ( objects , data , new_ids , report_type = report_type )
2009-06-09 23:24:24 +00:00
2010-07-09 06:40:01 +00:00
def __init__ ( self , cr , uid , name , context = None ) :
if context is None :
context = { }
2009-10-09 11:49:00 +00:00
super ( general_ledger , self ) . __init__ ( cr , uid , name , context = context )
2009-06-09 23:24:24 +00:00
self . query = " "
self . tot_currency = 0.0
self . period_sql = " "
self . sold_accounts = { }
2010-07-13 05:08:11 +00:00
self . sortby = ' sort_date '
2009-06-09 23:24:24 +00:00
self . localcontext . update ( {
' time ' : time ,
' lines ' : self . lines ,
' sum_debit_account ' : self . _sum_debit_account ,
' sum_credit_account ' : self . _sum_credit_account ,
2010-07-15 23:36:56 +00:00
' sum_balance_account ' : self . _sum_balance_account ,
2009-06-09 23:24:24 +00:00
' get_children_accounts ' : self . get_children_accounts ,
2010-06-29 13:48:46 +00:00
' sum_currency_amount_account ' : self . _sum_currency_amount_account ,
2010-07-13 04:32:21 +00:00
' get_fiscalyear ' : self . _get_fiscalyear ,
' get_account ' : self . _get_account ,
2010-06-29 13:48:46 +00:00
' get_start_period ' : self . get_start_period ,
' get_end_period ' : self . get_end_period ,
2010-07-15 12:57:50 +00:00
' get_filter ' : self . _get_filter ,
2010-07-15 23:36:56 +00:00
' get_sortby ' : self . _get_sortby
2009-06-09 23:24:24 +00:00
} )
self . context = context
def get_children_accounts ( self , account , form ) :
res = [ ]
2010-07-15 23:36:56 +00:00
ids_acc = self . pool . get ( ' account.account ' ) . _get_children_and_consol ( self . cr , self . uid , account . id )
2010-06-24 10:11:45 +00:00
for child_account in self . pool . get ( ' account.account ' ) . browse ( self . cr , self . uid , ids_acc ) :
2010-07-15 23:36:56 +00:00
sql = """
SELECT count ( id )
FROM account_move_line l
WHERE % s AND l . account_id = % % s
""" % (self.query)
self . cr . execute ( sql , ( child_account . id , ) )
num_entry = self . cr . fetchone ( ) [ 0 ] or 0
sold_account = self . _sum_balance_account ( child_account , form )
2009-06-09 23:24:24 +00:00
self . sold_accounts [ child_account . id ] = sold_account
2010-07-15 23:36:56 +00:00
if form [ ' display_account ' ] == ' mouvement ' :
if child_account . type != ' view ' and num_entry < > 0 :
2009-06-09 23:24:24 +00:00
res . append ( child_account )
2010-07-15 23:36:56 +00:00
elif form [ ' display_account ' ] == ' balance ' :
if child_account . type != ' view ' and num_entry < > 0 :
2009-06-09 23:24:24 +00:00
if ( sold_account < > 0.0 ) :
res . append ( child_account )
else :
2010-07-15 23:36:56 +00:00
res . append ( child_account )
2009-06-09 23:24:24 +00:00
if not len ( res ) :
return [ account ]
return res
def lines ( self , account , form ) :
2010-05-20 14:43:10 +00:00
""" Return all the account_move_line of account with their account code counterparts """
2010-06-24 10:11:45 +00:00
# First compute all counterpart strings for every move_id where this account appear.
# Currently, the counterpart info is used only in landscape mode
2010-05-20 14:43:10 +00:00
sql = """
SELECT m1 . move_id ,
array_to_string ( ARRAY ( SELECT DISTINCT a . code FROM account_move_line m2 LEFT JOIN account_account a ON ( m2 . account_id = a . id ) WHERE m2 . move_id = m1 . move_id AND m2 . account_id < > % % s ) , ' , ' ) AS counterpart
2010-06-24 10:11:45 +00:00
FROM ( SELECT move_id FROM account_move_line l WHERE % s AND l . account_id = % % s GROUP BY move_id ) m1
2010-05-20 14:43:10 +00:00
""" % s elf.query
2010-06-24 10:11:45 +00:00
self . cr . execute ( sql , ( account . id , account . id ) )
2010-05-20 14:43:10 +00:00
counterpart_res = self . cr . dictfetchall ( )
counterpart_accounts = { }
for i in counterpart_res :
counterpart_accounts [ i [ ' move_id ' ] ] = i [ ' counterpart ' ]
del counterpart_res
# Then select all account_move_line of this account
2010-07-13 05:08:11 +00:00
if self . sortby == ' sort_journal_partner ' :
2010-06-24 10:11:45 +00:00
sql_sort = ' j.code, p.name '
2009-06-09 23:24:24 +00:00
else :
2010-06-24 10:11:45 +00:00
sql_sort = ' l.date '
2009-06-09 23:24:24 +00:00
sql = """
2010-07-15 23:36:56 +00:00
SELECT l . id as lid , l . date as ldate , j . code as lcode , l . amount_currency , l . ref as lref , l . name as lname , COALESCE ( l . debit , 0 ) AS debit , COALESCE ( l . credit , 0 ) AS credit , l . period_id as lperiod_id , l . partner_id as lpartner_id ,
m . name AS move_name , m . id AS mmove_id ,
2010-05-20 14:43:10 +00:00
c . code AS currency_code ,
i . id AS invoice_id , i . type AS invoice_type , i . number AS invoice_number ,
p . name AS partner_name
FROM account_move_line l
LEFT JOIN account_move m on ( l . move_id = m . id )
LEFT JOIN res_currency c on ( l . currency_id = c . id )
LEFT JOIN res_partner p on ( l . partner_id = p . id )
LEFT JOIN account_invoice i on ( m . id = i . move_id )
JOIN account_journal j on ( l . journal_id = j . id )
2010-06-24 10:11:45 +00:00
WHERE % s AND l . account_id = % % s ORDER by % s
2010-05-20 14:43:10 +00:00
""" % (self.query, sql_sort)
2010-06-24 10:11:45 +00:00
self . cr . execute ( sql , ( account . id , ) )
2010-07-15 23:36:56 +00:00
res_lines = self . cr . dictfetchall ( )
res_init = [ ]
if res_lines and form [ ' initial_balance ' ] :
#FIXME: replace the hardcoded date with the day before data[form][date_from]
#FIXME: replace the label of lname with a string translatable
sql = """
SELECT 0 AS lid , ' 2010-07-15 ' AS ldate , ' ' as lcode , COALESCE ( SUM ( l . amount_currency ) , 0.0 ) as amount_currency , ' ' as lref , ' Initial Balance ' as lname , COALESCE ( SUM ( l . debit ) , 0.0 ) AS debit , COALESCE ( SUM ( l . credit ) , 0.0 ) AS credit , ' ' AS lperiod_id , ' ' AS lpartner_id ,
' ' AS move_name , ' ' AS mmove_id ,
' ' AS currency_code ,
' ' AS invoice_id , ' ' AS invoice_type , ' ' AS invoice_number ,
' ' AS partner_name
FROM account_move_line l
LEFT JOIN account_move m on ( l . move_id = m . id )
LEFT JOIN res_currency c on ( l . currency_id = c . id )
LEFT JOIN res_partner p on ( l . partner_id = p . id )
LEFT JOIN account_invoice i on ( m . id = i . move_id )
JOIN account_journal j on ( l . journal_id = j . id )
WHERE % s AND l . account_id = % % s
""" % (form[ ' initial_bal_query ' ])
self . cr . execute ( sql , ( account . id , ) )
res_init = self . cr . dictfetchall ( )
res = res_init + res_lines
2010-05-20 14:43:10 +00:00
account_sum = 0.0
2009-06-09 23:24:24 +00:00
account_move_line_obj = pooler . get_pool ( self . cr . dbname ) . get ( ' account.move.line ' )
2010-05-20 14:43:10 +00:00
inv_types = { ' out_invoice ' : ' CI ' , ' in_invoice ' : ' SI ' , ' out_refund ' : ' OR ' , ' in_refund ' : ' SR ' , }
2009-06-09 23:24:24 +00:00
for l in res :
2010-05-20 14:43:10 +00:00
l [ ' move ' ] = l [ ' move_name ' ]
if l [ ' invoice_id ' ] :
2010-07-15 23:36:56 +00:00
l [ ' lref ' ] = ' %s : %s ' % ( inv_types [ l [ ' invoice_type ' ] ] , l [ ' invoice_number ' ] )
2010-05-20 14:43:10 +00:00
l [ ' partner ' ] = l [ ' partner_name ' ] or ' '
2010-07-15 23:36:56 +00:00
account_sum = l [ ' debit ' ] - l [ ' credit ' ]
2010-05-20 14:43:10 +00:00
l [ ' progress ' ] = account_sum
2010-07-15 23:36:56 +00:00
l [ ' line_corresp ' ] = l [ ' mmove_id ' ] == ' ' and ' ' or counterpart_accounts [ l [ ' mmove_id ' ] ]
# Modification of amount Currency
2010-05-20 14:43:10 +00:00
if l [ ' credit ' ] > 0 :
2009-06-09 23:24:24 +00:00
if l [ ' amount_currency ' ] != None :
l [ ' amount_currency ' ] = abs ( l [ ' amount_currency ' ] ) * - 1
if l [ ' amount_currency ' ] != None :
self . tot_currency = self . tot_currency + l [ ' amount_currency ' ]
return res
def _sum_debit_account ( self , account , form ) :
self . cr . execute ( " SELECT sum(debit) " \
" FROM account_move_line l " \
2010-05-20 09:20:05 +00:00
" WHERE l.account_id = %s AND %s " % ( account . id , self . query ) )
2009-06-09 23:24:24 +00:00
sum_debit = self . cr . fetchone ( ) [ 0 ] or 0.0
2010-07-15 23:36:56 +00:00
if form . get ( ' initial_balance ' , False ) :
self . cr . execute ( " SELECT sum(debit) " \
" FROM account_move_line l " \
" WHERE l.account_id = %s AND %s " % ( account . id , form [ ' initial_bal_query ' ] ) )
# Add initial balance to the result
sum_debit + = self . cr . fetchone ( ) [ 0 ] or 0.0
2009-06-09 23:24:24 +00:00
return sum_debit
def _sum_credit_account ( self , account , form ) :
self . cr . execute ( " SELECT sum(credit) " \
" FROM account_move_line l " \
2010-06-16 11:51:39 +00:00
" WHERE l.account_id = %s AND %s " % ( account . id , self . query ) )
2009-06-09 23:24:24 +00:00
sum_credit = self . cr . fetchone ( ) [ 0 ] or 0.0
2010-07-15 23:36:56 +00:00
if form . get ( ' initial_balance ' , False ) :
self . cr . execute ( " SELECT sum(credit) " \
" FROM account_move_line l " \
" WHERE l.account_id = %s AND %s " % ( account . id , form [ ' initial_bal_query ' ] ) )
# Add initial balance to the result
sum_credit + = self . cr . fetchone ( ) [ 0 ] or 0.0
2009-06-09 23:24:24 +00:00
return sum_credit
2010-07-15 23:36:56 +00:00
def _sum_balance_account ( self , account , form ) :
self . cr . execute ( " SELECT (sum(debit) - sum(credit)) as tot_balance " \
2009-06-09 23:24:24 +00:00
" FROM account_move_line l " \
2010-06-16 11:51:39 +00:00
" WHERE l.account_id = %s AND %s " % ( account . id , self . query ) )
2010-07-15 23:36:56 +00:00
sum_balance = self . cr . fetchone ( ) [ 0 ] or 0.0
if form . get ( ' initial_balance ' , False ) :
self . cr . execute ( " SELECT (sum(debit) - sum(credit)) as tot_balance " \
" FROM account_move_line l " \
" WHERE l.account_id = %s AND %s " % ( account . id , form [ ' initial_bal_query ' ] ) )
# Add initial balance to the result
sum_balance + = self . cr . fetchone ( ) [ 0 ] or 0.0
return sum_balance
2009-06-09 23:24:24 +00:00
def _set_get_account_currency_code ( self , account_id ) :
self . cr . execute ( " SELECT c.code as code " \
" FROM res_currency c,account_account as ac " \
2010-05-20 09:20:05 +00:00
" WHERE ac.id = %s AND ac.currency_id = c.id " % ( account_id ) )
2009-06-09 23:24:24 +00:00
result = self . cr . fetchone ( )
if result :
self . account_currency = result [ 0 ]
else :
self . account_currency = False
def _sum_currency_amount_account ( self , account , form ) :
2010-07-15 23:36:56 +00:00
#FIXME: not good not good at all. use the query_get. return the amount + curr code
2009-06-09 23:24:24 +00:00
self . _set_get_account_currency_code ( account . id )
self . cr . execute ( " SELECT sum(aml.amount_currency) FROM account_move_line as aml,res_currency as rc WHERE aml.currency_id = rc.id AND aml.account_id= %s " , ( account . id , ) )
total = self . cr . fetchone ( )
if self . account_currency :
return_field = str ( total [ 0 ] ) + self . account_currency
return return_field
else :
currency_total = self . tot_currency = 0.0
return currency_total
2010-07-09 06:40:01 +00:00
2010-07-15 23:36:56 +00:00
report_sxw . report_sxw ( ' report.account.general.ledger ' , ' account.account ' , ' addons/account/report/general_ledger.rml ' , parser = general_ledger , header = ' internal ' )
2010-07-15 13:48:51 +00:00
report_sxw . report_sxw ( ' report.account.general.ledger_landscape ' , ' account.account ' , ' addons/account/report/general_ledger_landscape.rml ' , parser = general_ledger , header = ' internal ' )
2010-05-20 09:05:34 +00:00
2009-03-06 22:18:24 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: