2009-10-14 11:15:34 +00:00
# -*- coding: utf-8 -*-
2006-12-07 13:41:40 +00:00
##############################################################################
2009-11-30 10:24:22 +00:00
#
2009-05-04 12:12:54 +00:00
# OpenERP, Open Source Management Solution
2010-01-12 09:18:39 +00:00
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
2006-12-07 13:41:40 +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 time
2008-09-17 12:57:57 +00:00
import re
2012-12-06 14:56:32 +00:00
from openerp . report import report_sxw
2010-07-14 12:54:18 +00:00
from common_report_header import common_report_header
2012-12-06 14:56:32 +00:00
from openerp . tools . translate import _
2006-12-07 13:41:40 +00:00
2010-10-26 09:35:41 +00:00
class third_party_ledger ( report_sxw . rml_parse , common_report_header ) :
2010-07-13 11:48:47 +00:00
2010-07-20 10:55:55 +00:00
def __init__ ( self , cr , uid , name , context = None ) :
super ( third_party_ledger , self ) . __init__ ( cr , uid , name , context = context )
2010-11-22 06:56:42 +00:00
self . init_bal_sum = 0.0
2010-07-29 11:58:05 +00:00
self . localcontext . update ( {
2010-07-20 10:55:55 +00:00
' time ' : time ,
' lines ' : self . lines ,
' sum_debit_partner ' : self . _sum_debit_partner ,
' sum_credit_partner ' : self . _sum_credit_partner ,
' get_currency ' : self . _get_currency ,
2010-07-20 09:48:53 +00:00
' get_start_period ' : self . get_start_period ,
' get_end_period ' : self . get_end_period ,
' get_account ' : self . _get_account ,
' get_filter ' : self . _get_filter ,
' get_start_date ' : self . _get_start_date ,
' get_end_date ' : self . _get_end_date ,
' get_fiscalyear ' : self . _get_fiscalyear ,
2010-07-20 10:55:55 +00:00
' get_journal ' : self . _get_journal ,
2010-07-29 12:02:16 +00:00
' get_partners ' : self . _get_partners ,
' get_intial_balance ' : self . _get_intial_balance ,
' display_initial_balance ' : self . _display_initial_balance ,
' display_currency ' : self . _display_currency ,
2010-10-04 11:31:09 +00:00
' get_target_move ' : self . _get_target_move ,
2010-07-29 12:02:16 +00:00
} )
2011-05-19 16:35:48 +00:00
def _get_filter ( self , data ) :
if data [ ' form ' ] [ ' filter ' ] == ' unreconciled ' :
return _ ( ' Unreconciled Entries ' )
return super ( third_party_ledger , self ) . _get_filter ( data )
2010-07-20 10:55:55 +00:00
def set_context ( self , objects , data , ids , report_type = None ) :
2010-10-01 10:26:34 +00:00
obj_move = self . pool . get ( ' account.move.line ' )
2010-10-14 12:27:54 +00:00
obj_partner = self . pool . get ( ' res.partner ' )
2010-10-01 10:42:19 +00:00
self . query = obj_move . _query_get ( self . cr , self . uid , obj = ' l ' , context = data [ ' form ' ] . get ( ' used_context ' , { } ) )
2010-10-01 12:06:00 +00:00
ctx2 = data [ ' form ' ] . get ( ' used_context ' , { } ) . copy ( )
2011-04-28 07:20:58 +00:00
self . initial_balance = data [ ' form ' ] . get ( ' initial_balance ' , True )
if self . initial_balance :
ctx2 . update ( { ' initial_bal ' : True } )
2010-10-01 12:06:00 +00:00
self . init_query = obj_move . _query_get ( self . cr , self . uid , obj = ' l ' , context = ctx2 )
2011-05-06 06:27:58 +00:00
self . reconcil = True
if data [ ' form ' ] [ ' filter ' ] == ' unreconciled ' :
self . reconcil = False
2010-08-02 09:18:45 +00:00
self . result_selection = data [ ' form ' ] . get ( ' result_selection ' , ' customer ' )
self . amount_currency = data [ ' form ' ] . get ( ' amount_currency ' , False )
2010-09-16 12:12:11 +00:00
self . target_move = data [ ' form ' ] . get ( ' target_move ' , ' all ' )
2010-07-20 10:55:55 +00:00
PARTNER_REQUEST = ' '
2010-09-16 12:12:11 +00:00
move_state = [ ' draft ' , ' posted ' ]
if self . target_move == ' posted ' :
move_state = [ ' posted ' ]
2010-08-02 09:14:34 +00:00
if self . result_selection == ' supplier ' :
2010-07-20 10:55:55 +00:00
self . ACCOUNT_TYPE = [ ' payable ' ]
2010-08-02 09:14:34 +00:00
elif self . result_selection == ' customer ' :
2010-07-20 10:55:55 +00:00
self . ACCOUNT_TYPE = [ ' receivable ' ]
else :
self . ACCOUNT_TYPE = [ ' payable ' , ' receivable ' ]
self . cr . execute (
" SELECT a.id " \
" FROM account_account a " \
" LEFT JOIN account_account_type t " \
" ON (a.type=t.code) " \
' WHERE a.type IN %s ' \
" AND a.active " , ( tuple ( self . ACCOUNT_TYPE ) , ) )
self . account_ids = [ a for ( a , ) in self . cr . fetchall ( ) ]
2013-01-23 11:32:53 +00:00
params = [ tuple ( move_state ) , tuple ( self . account_ids ) ]
#if we print from the partners, add a clause on active_ids
if ( data [ ' model ' ] == ' res.partner ' ) and ids :
PARTNER_REQUEST = " AND l.partner_id IN %s "
params + = [ tuple ( ids ) ]
2010-07-29 11:31:00 +00:00
self . cr . execute (
" SELECT DISTINCT l.partner_id " \
2010-09-16 12:12:11 +00:00
" FROM account_move_line AS l, account_account AS account, " \
" account_move AS am " \
2010-07-29 11:31:00 +00:00
" WHERE l.partner_id IS NOT NULL " \
" AND l.account_id = account.id " \
2010-09-16 12:12:11 +00:00
" AND am.id = l.move_id " \
" AND am.state IN %s "
2010-10-14 12:27:54 +00:00
# "AND " + self.query +" " \
2010-07-29 11:31:00 +00:00
" AND l.account_id IN %s " \
" " + PARTNER_REQUEST + " " \
2013-01-23 11:32:53 +00:00
" AND account.active " , params )
self . partner_ids = [ res [ ' partner_id ' ] for res in self . cr . dictfetchall ( ) ]
objects = obj_partner . browse ( self . cr , self . uid , self . partner_ids )
return super ( third_party_ledger , self ) . set_context ( objects , data , self . partner_ids , report_type )
2010-07-20 10:55:55 +00:00
2010-08-02 10:07:09 +00:00
def lines ( self , partner ) :
2010-09-16 12:12:11 +00:00
move_state = [ ' draft ' , ' posted ' ]
if self . target_move == ' posted ' :
move_state = [ ' posted ' ]
2010-07-20 10:55:55 +00:00
full_account = [ ]
2010-08-02 09:06:56 +00:00
if self . reconcil :
2010-07-20 10:55:55 +00:00
RECONCILE_TAG = " "
else :
RECONCILE_TAG = " AND l.reconcile_id IS NULL "
2010-07-29 10:09:23 +00:00
self . cr . execute (
2010-10-14 08:26:05 +00:00
" SELECT l.id, l.date, j.code, acc.code as a_code, acc.name as a_name, l.ref, m.name as move_name, l.name, l.debit, l.credit, l.amount_currency,l.currency_id, c.symbol AS currency_code " \
2010-07-29 10:09:23 +00:00
" FROM account_move_line l " \
" LEFT JOIN account_journal j " \
" ON (l.journal_id = j.id) " \
2010-09-16 13:44:07 +00:00
" LEFT JOIN account_account acc " \
" ON (l.account_id = acc.id) " \
" LEFT JOIN res_currency c ON (l.currency_id=c.id) " \
" LEFT JOIN account_move m ON (m.id=l.move_id) " \
2010-07-29 10:09:23 +00:00
" WHERE l.partner_id = %s " \
" AND l.account_id IN %s AND " + self . query + " " \
2010-09-16 12:12:11 +00:00
" AND m.state IN %s " \
2010-07-29 10:09:23 +00:00
" " + RECONCILE_TAG + " " \
2010-09-16 10:47:13 +00:00
" ORDER BY l.date " ,
2010-09-16 12:12:11 +00:00
( partner . id , tuple ( self . account_ids ) , tuple ( move_state ) ) )
2010-07-29 10:09:23 +00:00
res = self . cr . dictfetchall ( )
sum = 0.0
2010-11-18 08:27:59 +00:00
if self . initial_balance :
2010-11-22 07:19:21 +00:00
sum = self . init_bal_sum
2010-07-29 10:09:23 +00:00
for r in res :
2010-11-18 08:27:59 +00:00
sum + = r [ ' debit ' ] - r [ ' credit ' ]
2010-07-29 10:09:23 +00:00
r [ ' progress ' ] = sum
full_account . append ( r )
2010-07-20 10:55:55 +00:00
return full_account
2010-07-29 12:24:55 +00:00
def _get_intial_balance ( self , partner ) :
2010-09-16 12:12:11 +00:00
move_state = [ ' draft ' , ' posted ' ]
if self . target_move == ' posted ' :
move_state = [ ' posted ' ]
2010-10-29 10:32:11 +00:00
if self . reconcil :
RECONCILE_TAG = " "
else :
RECONCILE_TAG = " AND l.reconcile_id IS NULL "
2010-07-29 11:31:00 +00:00
self . cr . execute (
2010-07-30 07:19:00 +00:00
" SELECT COALESCE(SUM(l.debit),0.0), COALESCE(SUM(l.credit),0.0), COALESCE(sum(debit-credit), 0.0) " \
2010-09-16 12:12:11 +00:00
" FROM account_move_line AS l, " \
" account_move AS m "
2010-10-18 06:42:55 +00:00
" WHERE l.partner_id = %s " \
2010-09-16 12:12:11 +00:00
" AND m.id = l.move_id " \
" AND m.state IN %s "
2010-07-30 07:19:00 +00:00
" AND account_id IN %s " \
2010-10-29 10:32:11 +00:00
" " + RECONCILE_TAG + " " \
2010-07-30 07:19:00 +00:00
" AND " + self . init_query + " " ,
2010-09-16 12:12:11 +00:00
( partner . id , tuple ( move_state ) , tuple ( self . account_ids ) ) )
2010-11-18 08:27:59 +00:00
res = self . cr . fetchall ( )
self . init_bal_sum = res [ 0 ] [ 2 ]
return res
2010-07-29 11:31:00 +00:00
2010-08-02 10:07:09 +00:00
def _sum_debit_partner ( self , partner ) :
2010-09-16 12:12:11 +00:00
move_state = [ ' draft ' , ' posted ' ]
if self . target_move == ' posted ' :
move_state = [ ' posted ' ]
2010-07-20 10:55:55 +00:00
result_tmp = 0.0
2010-07-29 13:29:21 +00:00
result_init = 0.0
2010-10-08 12:49:22 +00:00
if self . reconcil :
2010-07-20 10:55:55 +00:00
RECONCILE_TAG = " "
else :
RECONCILE_TAG = " AND reconcile_id IS NULL "
2010-08-02 09:06:56 +00:00
if self . initial_balance :
2010-07-20 10:55:55 +00:00
self . cr . execute (
" SELECT sum(debit) " \
2010-09-16 12:12:11 +00:00
" FROM account_move_line AS l, " \
" account_move AS m "
2010-10-18 06:42:55 +00:00
" WHERE l.partner_id = %s " \
2010-09-16 12:12:11 +00:00
" AND m.id = l.move_id " \
" AND m.state IN %s "
2010-07-20 10:55:55 +00:00
" AND account_id IN %s " \
2010-10-05 09:46:15 +00:00
" " + RECONCILE_TAG + " " \
2010-07-30 05:24:44 +00:00
" AND " + self . init_query + " " ,
2010-10-05 09:46:15 +00:00
( partner . id , tuple ( move_state ) , tuple ( self . account_ids ) ) )
2010-07-20 10:55:55 +00:00
contemp = self . cr . fetchone ( )
if contemp != None :
2010-07-29 13:29:21 +00:00
result_init = contemp [ 0 ] or 0.0
2010-07-20 10:55:55 +00:00
else :
2010-07-29 13:29:21 +00:00
result_init = result_tmp + 0.0
2010-07-20 10:55:55 +00:00
2010-07-29 10:09:23 +00:00
self . cr . execute (
" SELECT sum(debit) " \
2010-09-16 12:12:11 +00:00
" FROM account_move_line AS l, " \
" account_move AS m "
2010-10-18 06:42:55 +00:00
" WHERE l.partner_id = %s " \
2010-09-16 12:12:11 +00:00
" AND m.id = l.move_id " \
" AND m.state IN %s "
2010-07-29 10:09:23 +00:00
" AND account_id IN %s " \
" " + RECONCILE_TAG + " " \
2010-10-08 12:49:22 +00:00
" AND " + self . query + " " ,
2010-10-05 09:46:15 +00:00
( partner . id , tuple ( move_state ) , tuple ( self . account_ids ) , ) )
2010-07-29 10:09:23 +00:00
contemp = self . cr . fetchone ( )
if contemp != None :
result_tmp = contemp [ 0 ] or 0.0
else :
result_tmp = result_tmp + 0.0
2010-10-05 09:46:15 +00:00
2010-07-29 13:29:21 +00:00
return result_tmp + result_init
2010-07-20 10:55:55 +00:00
2010-10-05 09:46:15 +00:00
def _sum_credit_partner ( self , partner ) :
2010-09-16 12:12:11 +00:00
move_state = [ ' draft ' , ' posted ' ]
if self . target_move == ' posted ' :
move_state = [ ' posted ' ]
2010-07-20 10:55:55 +00:00
result_tmp = 0.0
2010-07-29 13:29:21 +00:00
result_init = 0.0
2010-10-08 12:49:22 +00:00
if self . reconcil :
2010-07-20 10:55:55 +00:00
RECONCILE_TAG = " "
else :
RECONCILE_TAG = " AND reconcile_id IS NULL "
2010-08-02 09:06:56 +00:00
if self . initial_balance :
2010-07-20 10:55:55 +00:00
self . cr . execute (
" SELECT sum(credit) " \
2010-09-16 12:12:11 +00:00
" FROM account_move_line AS l, " \
" account_move AS m "
2010-10-18 06:42:55 +00:00
" WHERE l.partner_id = %s " \
2010-09-16 12:12:11 +00:00
" AND m.id = l.move_id " \
" AND m.state IN %s "
2010-07-20 10:55:55 +00:00
" AND account_id IN %s " \
2010-10-05 09:46:15 +00:00
" " + RECONCILE_TAG + " " \
2010-07-30 05:24:44 +00:00
" AND " + self . init_query + " " ,
2010-10-05 09:46:15 +00:00
( partner . id , tuple ( move_state ) , tuple ( self . account_ids ) ) )
2010-07-20 10:55:55 +00:00
contemp = self . cr . fetchone ( )
if contemp != None :
2010-07-29 13:29:21 +00:00
result_init = contemp [ 0 ] or 0.0
2010-07-20 10:55:55 +00:00
else :
2010-07-29 13:29:21 +00:00
result_init = result_tmp + 0.0
2010-07-20 10:55:55 +00:00
2010-07-29 10:09:23 +00:00
self . cr . execute (
" SELECT sum(credit) " \
2010-09-16 12:12:11 +00:00
" FROM account_move_line AS l, " \
" account_move AS m "
2010-10-18 06:42:55 +00:00
" WHERE l.partner_id= %s " \
2010-09-16 12:12:11 +00:00
" AND m.id = l.move_id " \
" AND m.state IN %s "
2010-07-29 10:09:23 +00:00
" AND account_id IN %s " \
" " + RECONCILE_TAG + " " \
2010-10-08 12:49:22 +00:00
" AND " + self . query + " " ,
2010-10-05 09:46:15 +00:00
( partner . id , tuple ( move_state ) , tuple ( self . account_ids ) , ) )
2010-07-29 10:09:23 +00:00
contemp = self . cr . fetchone ( )
if contemp != None :
result_tmp = contemp [ 0 ] or 0.0
else :
result_tmp = result_tmp + 0.0
2010-07-29 13:29:21 +00:00
return result_tmp + result_init
2010-10-05 09:46:15 +00:00
2010-08-02 10:07:09 +00:00
def _get_partners ( self ) :
2013-06-14 21:36:02 +00:00
# TODO: deprecated, to remove in trunk
2010-08-02 09:14:34 +00:00
if self . result_selection == ' customer ' :
2013-04-05 07:12:33 +00:00
return _ ( ' Receivable Accounts ' )
2010-08-02 09:14:34 +00:00
elif self . result_selection == ' supplier ' :
2013-04-05 07:12:33 +00:00
return _ ( ' Payable Accounts ' )
2010-08-02 09:14:34 +00:00
elif self . result_selection == ' customer_supplier ' :
2013-04-05 07:12:33 +00:00
return _ ( ' Receivable and Payable Accounts ' )
2010-07-20 11:42:50 +00:00
return ' '
2010-07-20 10:55:55 +00:00
def _sum_currency_amount_account ( self , account , form ) :
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-29 13:29:21 +00:00
2010-07-29 12:02:16 +00:00
def _display_initial_balance ( self , data ) :
2011-04-29 08:49:48 +00:00
if self . initial_balance :
return True
return False
2010-07-29 13:29:21 +00:00
2010-08-02 10:23:08 +00:00
def _display_currency ( self , data ) :
2011-04-29 08:49:48 +00:00
if self . amount_currency :
return True
return False
2010-07-29 12:24:55 +00:00
2008-09-17 12:57:57 +00:00
report_sxw . report_sxw ( ' report.account.third_party_ledger ' , ' res.partner ' ,
2010-08-02 12:27:00 +00:00
' addons/account/report/account_partner_ledger.rml ' , parser = third_party_ledger ,
2010-07-27 12:14:48 +00:00
header = ' internal ' )
2008-07-23 14:41:47 +00:00
2009-05-28 05:56:33 +00:00
report_sxw . report_sxw ( ' report.account.third_party_ledger_other ' , ' res.partner ' ,
2010-08-02 12:27:00 +00:00
' addons/account/report/account_partner_ledger_other.rml ' , parser = third_party_ledger ,
2010-07-27 12:14:48 +00:00
header = ' internal ' )
2009-05-28 05:56:33 +00:00
2010-11-22 07:19:21 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: