2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2007-01-04 09:45:50 +00:00
##############################################################################
2009-11-25 09:31:44 +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>).
2007-01-04 09:45:50 +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.
2007-01-04 09:45:50 +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.
2007-01-04 09:45:50 +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-25 09:31:44 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2007-01-04 09:45:50 +00:00
#
##############################################################################
2009-10-14 11:15:34 +00:00
2007-01-04 09:45:50 +00:00
import time
2010-08-14 05:37:43 +00:00
2007-01-04 09:45:50 +00:00
import netsvc
from osv import fields , osv
from tools . misc import currency
2008-07-08 08:13:12 +00:00
from tools . translate import _
2010-07-15 13:07:11 +00:00
import decimal_precision as dp
2007-01-04 09:45:50 +00:00
class account_bank_statement ( osv . osv ) :
2010-04-15 13:17:09 +00:00
2010-09-14 17:53:07 +00:00
def create ( self , cr , uid , vals , context = None ) :
seq = 0
2010-09-14 18:13:52 +00:00
if ' line_ids ' in vals :
for line in vals [ ' line_ids ' ] :
seq + = 1
line [ 2 ] [ ' sequence ' ] = seq
vals [ seq - 1 ] = line
2010-09-14 17:53:07 +00:00
return super ( account_bank_statement , self ) . create ( cr , uid , vals , context = context )
def write ( self , cr , uid , ids , vals , context = None ) :
res = super ( account_bank_statement , self ) . write ( cr , uid , ids , vals , context = context )
for statement in self . browse ( cr , uid , ids , context ) :
seq = 0
for line in statement . line_ids :
seq + = 1
if not line . sequence :
self . pool . get ( ' account.bank.statement.line ' ) . write ( cr , uid , [ line . id ] , { ' sequence ' : seq } , context = context )
return res
2010-04-15 13:17:09 +00:00
def button_import_invoice ( self , cr , uid , ids , context = None ) :
mod_obj = self . pool . get ( ' ir.model.data ' )
if context is None :
context = { }
2010-06-16 11:51:39 +00:00
model_data_ids = mod_obj . search ( cr , uid , [ ( ' model ' , ' = ' , ' ir.ui.view ' ) , ( ' name ' , ' = ' , ' view_account_statement_from_invoice ' ) ] , context = context )
2010-04-15 13:17:09 +00:00
resource_id = mod_obj . read ( cr , uid , model_data_ids , fields = [ ' res_id ' ] , context = context ) [ 0 ] [ ' res_id ' ]
context . update ( { ' statement_id ' : ids [ 0 ] } )
return {
' name ' : _ ( ' Import Invoice ' ) ,
' context ' : context ,
' view_type ' : ' form ' ,
' view_mode ' : ' tree,form ' ,
' res_model ' : ' account.statement.from.invoice ' ,
' views ' : [ ( resource_id , ' form ' ) ] ,
' type ' : ' ir.actions.act_window ' ,
' target ' : ' new ' ,
' nodestroy ' : True
2010-06-23 19:44:06 +00:00
}
2010-04-15 13:17:09 +00:00
2008-07-22 15:11:28 +00:00
def _default_journal_id ( self , cr , uid , context = { } ) :
2010-07-30 11:12:06 +00:00
journal_pool = self . pool . get ( ' account.journal ' )
journal_type = context . get ( ' journal_type ' , False )
journal_id = False
if journal_type :
ids = journal_pool . search ( cr , uid , [ ( ' type ' , ' = ' , journal_type ) ] )
if ids :
journal_id = ids [ 0 ]
2010-08-14 05:37:43 +00:00
return journal_id
2008-07-22 15:11:28 +00:00
def _default_balance_start ( self , cr , uid , context = { } ) :
2008-12-10 14:29:55 +00:00
cr . execute ( ' select id from account_bank_statement where journal_id= %s order by date desc limit 1 ' , ( 1 , ) )
2008-07-22 15:11:28 +00:00
res = cr . fetchone ( )
if res :
return self . browse ( cr , uid , [ res [ 0 ] ] , context ) [ 0 ] . balance_end
return 0.0
def _end_balance ( self , cursor , user , ids , name , attr , context = None ) :
res_currency_obj = self . pool . get ( ' res.currency ' )
res_users_obj = self . pool . get ( ' res.users ' )
res = { }
company_currency_id = res_users_obj . browse ( cursor , user , user ,
context = context ) . company_id . currency_id . id
statements = self . browse ( cursor , user , ids , context = context )
for statement in statements :
res [ statement . id ] = statement . balance_start
currency_id = statement . currency . id
for line in statement . move_line_ids :
if line . debit > 0 :
if line . account_id . id == \
statement . journal_id . default_debit_account_id . id :
res [ statement . id ] + = res_currency_obj . compute ( cursor ,
user , company_currency_id , currency_id ,
line . debit , context = context )
else :
if line . account_id . id == \
statement . journal_id . default_credit_account_id . id :
2008-12-07 13:06:32 +00:00
res [ statement . id ] - = res_currency_obj . compute ( cursor ,
2008-07-22 15:11:28 +00:00
user , company_currency_id , currency_id ,
line . credit , context = context )
if statement . state == ' draft ' :
for line in statement . line_ids :
res [ statement . id ] + = line . amount
for r in res :
res [ r ] = round ( res [ r ] , 2 )
return res
def _get_period ( self , cr , uid , context = { } ) :
periods = self . pool . get ( ' account.period ' ) . find ( cr , uid )
if periods :
return periods [ 0 ]
2010-08-30 06:28:31 +00:00
return False
2008-07-22 15:11:28 +00:00
def _currency ( self , cursor , user , ids , name , args , context = None ) :
res = { }
res_currency_obj = self . pool . get ( ' res.currency ' )
res_users_obj = self . pool . get ( ' res.users ' )
default_currency = res_users_obj . browse ( cursor , user ,
user , context = context ) . company_id . currency_id
for statement in self . browse ( cursor , user , ids , context = context ) :
currency = statement . journal_id . currency
if not currency :
currency = default_currency
res [ statement . id ] = currency . id
currency_names = { }
for currency_id , currency_name in res_currency_obj . name_get ( cursor ,
user , [ x for x in res . values ( ) ] , context = context ) :
currency_names [ currency_id ] = currency_name
for statement_id in res . keys ( ) :
currency_id = res [ statement_id ]
res [ statement_id ] = ( currency_id , currency_names [ currency_id ] )
return res
2010-08-14 05:37:43 +00:00
2008-07-22 15:11:28 +00:00
_order = " date desc "
_name = " account.bank.statement "
_description = " Bank Statement "
_columns = {
2010-08-16 07:03:58 +00:00
' name ' : fields . char ( ' Name ' , size = 64 , required = True , help = ' if you give the Name other then /, its created Accounting Entries Move will be with same name as statement name. This allows the statement entries to have the same references than the statement itself ' , states = { ' confirm ' : [ ( ' readonly ' , True ) ] } ) ,
2010-08-11 13:50:57 +00:00
' date ' : fields . date ( ' Date ' , required = True , states = { ' confirm ' : [ ( ' readonly ' , True ) ] } ) ,
2008-07-22 15:11:28 +00:00
' journal_id ' : fields . many2one ( ' account.journal ' , ' Journal ' , required = True ,
2010-09-27 06:15:38 +00:00
readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2008-07-22 15:11:28 +00:00
' period_id ' : fields . many2one ( ' account.period ' , ' Period ' , required = True ,
states = { ' confirm ' : [ ( ' readonly ' , True ) ] } ) ,
2010-07-15 13:07:11 +00:00
' balance_start ' : fields . float ( ' Starting Balance ' , digits_compute = dp . get_precision ( ' Account ' ) ,
2008-07-22 15:11:28 +00:00
states = { ' confirm ' : [ ( ' readonly ' , True ) ] } ) ,
2010-07-15 13:07:11 +00:00
' balance_end_real ' : fields . float ( ' Ending Balance ' , digits_compute = dp . get_precision ( ' Account ' ) ,
2008-07-22 15:11:28 +00:00
states = { ' confirm ' : [ ( ' readonly ' , True ) ] } ) ,
' balance_end ' : fields . function ( _end_balance , method = True , string = ' Balance ' ) ,
2010-09-14 17:53:07 +00:00
' company_id ' : fields . related ( ' journal_id ' , ' company_id ' , type = ' many2one ' , relation = ' res.company ' , string = ' Company ' , store = True , readonly = True ) ,
2008-07-22 15:11:28 +00:00
' line_ids ' : fields . one2many ( ' account.bank.statement.line ' ,
' statement_id ' , ' Statement lines ' ,
states = { ' confirm ' : [ ( ' readonly ' , True ) ] } ) ,
' move_line_ids ' : fields . one2many ( ' account.move.line ' , ' statement_id ' ,
' Entry lines ' , states = { ' confirm ' : [ ( ' readonly ' , True ) ] } ) ,
2009-09-24 10:46:21 +00:00
' state ' : fields . selection ( [ ( ' draft ' , ' Draft ' ) , ( ' confirm ' , ' Confirmed ' ) ] ,
2008-07-22 15:11:28 +00:00
' State ' , required = True ,
2009-11-23 13:52:38 +00:00
states = { ' confirm ' : [ ( ' readonly ' , True ) ] } , readonly = " 1 " ,
2009-11-25 10:03:47 +00:00
help = ' When new statement is created the state will be \' Draft \' . \
\n * And after getting confirmation from the bank it will be in \' Confirmed \' state. ' ) ,
2008-07-22 15:11:28 +00:00
' currency ' : fields . function ( _currency , method = True , string = ' Currency ' ,
type = ' many2one ' , relation = ' res.currency ' ) ,
}
2008-08-22 11:53:40 +00:00
2008-07-22 15:11:28 +00:00
_defaults = {
2010-09-23 06:38:30 +00:00
' name ' : " / " ,
' date ' : time . strftime ( ' % Y- % m- %d ' ) ,
' state ' : ' draft ' ,
2008-07-22 15:11:28 +00:00
' balance_start ' : _default_balance_start ,
' journal_id ' : _default_journal_id ,
' period_id ' : _get_period ,
}
2010-09-23 06:38:30 +00:00
def onchange_date ( self , cr , user , ids , date , context = None ) :
2010-07-26 14:41:32 +00:00
"""
Returns a dict that contains new values and context
@param cr : A database cursor
@param user : ID of the user currently logged in
@param date : latest value from user input for field date
@param args : other arguments
@param context : context arguments , like lang , time zone
@return : Returns a dict which contains new values , and context
"""
res = { }
period_pool = self . pool . get ( ' account.period ' )
2010-09-23 06:38:30 +00:00
if context is None :
context = { }
2010-07-26 14:41:32 +00:00
pids = period_pool . search ( cr , user , [ ( ' date_start ' , ' <= ' , date ) , ( ' date_stop ' , ' >= ' , date ) ] )
if pids :
res . update ( {
' period_id ' : pids [ 0 ]
} )
context . update ( {
' period_id ' : pids [ 0 ]
} )
2010-08-14 05:37:43 +00:00
2010-07-26 14:41:32 +00:00
return {
' value ' : res ,
' context ' : context ,
}
2010-08-11 14:09:35 +00:00
2010-09-23 06:38:30 +00:00
def button_dummy ( self , cr , uid , ids , context = None ) :
return self . write ( cr , uid , ids , { } , context = context )
2010-09-17 14:30:45 +00:00
2010-09-21 10:08:37 +00:00
def create_move_from_st_line ( self , cr , uid , st_line_id , company_currency_id , st_line_number , context = None ) :
2008-07-22 15:11:28 +00:00
res_currency_obj = self . pool . get ( ' res.currency ' )
res_users_obj = self . pool . get ( ' res.users ' )
account_move_obj = self . pool . get ( ' account.move ' )
account_move_line_obj = self . pool . get ( ' account.move.line ' )
2010-08-27 12:11:49 +00:00
account_analytic_line_obj = self . pool . get ( ' account.analytic.line ' )
2010-08-16 07:03:58 +00:00
account_bank_statement_line_obj = self . pool . get ( ' account.bank.statement.line ' )
2010-09-22 06:23:08 +00:00
2010-09-22 15:41:58 +00:00
st_line = account_bank_statement_line_obj . browse ( cr , uid , st_line_id , context )
2010-09-17 14:30:45 +00:00
st = st_line . statement_id
2010-09-22 06:23:08 +00:00
2010-09-17 14:30:45 +00:00
context . update ( { ' date ' : st_line . date } )
move_id = account_move_obj . create ( cr , uid , {
' journal_id ' : st . journal_id . id ,
' period_id ' : st . period_id . id ,
' date ' : st_line . date ,
2010-09-20 15:59:47 +00:00
' name ' : st_line_number ,
2010-09-17 14:30:45 +00:00
} , context = context )
account_bank_statement_line_obj . write ( cr , uid , [ st_line . id ] , {
' move_ids ' : [ ( 4 , move_id , False ) ]
} )
torec = [ ]
if st_line . amount > = 0 :
account_id = st . journal_id . default_credit_account_id . id
else :
account_id = st . journal_id . default_debit_account_id . id
acc_cur = ( ( st_line . amount < = 0 ) and st . journal_id . default_debit_account_id ) or st_line . account_id
amount = res_currency_obj . compute ( cr , uid , st . currency . id ,
company_currency_id , st_line . amount , context = context ,
account = acc_cur )
val = {
' name ' : st_line . name ,
' date ' : st_line . date ,
' ref ' : st_line . ref ,
' move_id ' : move_id ,
' partner_id ' : ( ( st_line . partner_id ) and st_line . partner_id . id ) or False ,
' account_id ' : ( st_line . account_id ) and st_line . account_id . id ,
' credit ' : ( ( amount > 0 ) and amount ) or 0.0 ,
' debit ' : ( ( amount < 0 ) and - amount ) or 0.0 ,
' statement_id ' : st . id ,
' journal_id ' : st . journal_id . id ,
' period_id ' : st . period_id . id ,
' currency_id ' : st . currency . id ,
' analytic_account_id ' : st_line . analytic_account_id and st_line . analytic_account_id . id or False
}
amount = res_currency_obj . compute ( cr , uid , st . currency . id ,
company_currency_id , st_line . amount , context = context ,
account = acc_cur )
if st . currency . id < > company_currency_id :
amount_cur = res_currency_obj . compute ( cr , uid , company_currency_id ,
st . currency . id , amount , context = context ,
account = acc_cur )
val [ ' amount_currency ' ] = - amount_cur
if st_line . account_id and st_line . account_id . currency_id and st_line . account_id . currency_id . id < > company_currency_id :
val [ ' currency_id ' ] = st_line . account_id . currency_id . id
if company_currency_id == st_line . account_id . currency_id . id :
amount_cur = st_line . amount
else :
amount_cur = res_currency_obj . compute ( cr , uid , company_currency_id ,
st_line . account_id . currency_id . id , amount , context = context ,
account = acc_cur )
val [ ' amount_currency ' ] = amount_cur
move_line_id = account_move_line_obj . create ( cr , uid , val , context = context )
torec . append ( move_line_id )
2010-09-22 06:23:08 +00:00
2010-09-17 14:30:45 +00:00
# Fill the secondary amount/currency
# if currency is not the same than the company
amount_currency = False
currency_id = False
if st . currency . id < > company_currency_id :
amount_currency = st_line . amount
currency_id = st . currency . id
account_move_line_obj . create ( cr , uid , {
' name ' : st_line . name ,
' date ' : st_line . date ,
' ref ' : st_line . ref ,
' move_id ' : move_id ,
' partner_id ' : ( ( st_line . partner_id ) and st_line . partner_id . id ) or False ,
' account_id ' : account_id ,
' credit ' : ( ( amount < 0 ) and - amount ) or 0.0 ,
' debit ' : ( ( amount > 0 ) and amount ) or 0.0 ,
' statement_id ' : st . id ,
' journal_id ' : st . journal_id . id ,
' period_id ' : st . period_id . id ,
' amount_currency ' : amount_currency ,
' currency_id ' : currency_id ,
} , context = context )
for line in account_move_line_obj . browse ( cr , uid , [ x . id for x in
account_move_obj . browse ( cr , uid , move_id ,
context = context ) . line_id ] ,
context = context ) :
if line . state < > ' valid ' :
raise osv . except_osv ( _ ( ' Error ! ' ) ,
_ ( ' Journal Item " %s " is not valid ' ) % line . name )
# Bank statements will not consider boolean on journal entry_posted
account_move_obj . post ( cr , uid , [ move_id ] , context = context )
return move_id
2010-09-20 15:59:47 +00:00
def get_next_st_line_number ( self , cr , uid , st_number , st_line , context = None ) :
return st_number + ' - ' + str ( st_line . sequence )
2010-09-22 15:41:58 +00:00
def balance_check ( self , cr , uid , st_id , journal_type = ' bank ' , context = None ) :
st = self . browse ( cr , uid , st_id , context )
if not ( abs ( ( st . balance_end or 0.0 ) - st . balance_end_real ) < 0.0001 ) :
raise osv . except_osv ( _ ( ' Error ! ' ) ,
_ ( ' The statement balance is incorrect ! \n ' ) +
_ ( ' The expected balance ( %.2f ) is different than the computed one. ( %.2f ) ' ) % ( st . balance_end_real , st . balance_end ) )
return True
def statement_close ( self , cr , uid , ids , journal_type = ' bank ' , context = None ) :
return self . write ( cr , uid , ids , { ' state ' : ' confirm ' } , context = context )
def check_status_condition ( self , cr , uid , state , journal_type = ' bank ' ) :
return state == ' draft '
2010-09-17 14:30:45 +00:00
def button_confirm_bank ( self , cr , uid , ids , context = None ) :
done = [ ]
2010-08-16 07:03:58 +00:00
obj_seq = self . pool . get ( ' ir.sequence ' )
if context is None :
context = { }
2008-07-22 15:11:28 +00:00
for st in self . browse ( cr , uid , ids , context ) :
2010-09-22 15:41:58 +00:00
j_type = st . journal_id . type
2010-09-17 14:30:45 +00:00
company_currency_id = st . journal_id . company_id . currency_id . id
2010-09-22 15:41:58 +00:00
if not self . check_status_condition ( cr , uid , st . state , journal_type = j_type ) :
2008-07-22 15:11:28 +00:00
continue
2010-09-23 06:38:30 +00:00
2010-09-22 15:41:58 +00:00
self . balance_check ( cr , uid , st . id , journal_type = j_type , context = context )
2008-07-22 15:11:28 +00:00
if ( not st . journal_id . default_credit_account_id ) \
or ( not st . journal_id . default_debit_account_id ) :
2010-01-05 10:39:47 +00:00
raise osv . except_osv ( _ ( ' Configuration Error ! ' ) ,
2008-07-22 15:11:28 +00:00
_ ( ' Please verify that an account is defined in the journal. ' ) )
2010-08-27 04:50:49 +00:00
if not st . name == ' / ' :
2010-09-20 15:59:47 +00:00
st_number = st . name
2010-08-27 04:50:49 +00:00
else :
2010-09-17 14:30:45 +00:00
if st . journal_id . sequence_id :
c = { ' fiscalyear_id ' : st . period_id . fiscalyear_id . id }
2010-09-20 15:59:47 +00:00
st_number = obj_seq . get_id ( cr , uid , st . journal_id . sequence_id . id , context = c )
2010-09-17 14:30:45 +00:00
else :
2010-09-20 15:59:47 +00:00
st_number = obj_seq . get ( cr , uid , ' account.bank.statement ' )
2010-08-27 04:50:49 +00:00
2008-07-22 15:11:28 +00:00
for line in st . move_line_ids :
if line . state < > ' valid ' :
raise osv . except_osv ( _ ( ' Error ! ' ) ,
2008-09-24 08:46:41 +00:00
_ ( ' The account entries lines are not in valid state. ' ) )
2010-09-17 14:30:45 +00:00
for st_line in st . line_ids :
if st_line . analytic_account_id :
2010-08-27 12:11:49 +00:00
if not st . journal_id . analytic_journal_id :
raise osv . except_osv ( _ ( ' No Analytic Journal ! ' ) , _ ( " You have to define an analytic journal on the ' %s ' journal! " ) % ( st . journal_id . name , ) )
2010-09-17 14:30:45 +00:00
if not st_line . amount :
2008-07-22 15:11:28 +00:00
continue
2010-09-20 15:59:47 +00:00
st_line_number = self . get_next_st_line_number ( cr , uid , st_number , st_line , context )
2010-09-21 10:08:37 +00:00
self . create_move_from_st_line ( cr , uid , st_line . id , company_currency_id , st_line_number , context )
2008-07-22 15:11:28 +00:00
2010-09-20 15:59:47 +00:00
self . write ( cr , uid , [ st . id ] , { ' name ' : st_number } , context = context )
self . log ( cr , uid , st . id , ' Statement %s is confirmed and entries are created. ' % st_number )
2008-07-22 15:11:28 +00:00
done . append ( st . id )
2010-09-22 15:41:58 +00:00
return self . write ( cr , uid , ids , { ' state ' : ' confirm ' } , context = context )
2008-07-22 15:11:28 +00:00
2010-09-23 06:38:30 +00:00
def button_cancel ( self , cr , uid , ids , context = None ) :
2008-07-22 15:11:28 +00:00
done = [ ]
for st in self . browse ( cr , uid , ids , context ) :
if st . state == ' draft ' :
continue
ids = [ ]
for line in st . line_ids :
ids + = [ x . id for x in line . move_ids ]
self . pool . get ( ' account.move ' ) . unlink ( cr , uid , ids , context )
done . append ( st . id )
2010-09-23 06:38:30 +00:00
return self . write ( cr , uid , done , { ' state ' : ' draft ' } , context = context )
2008-07-22 15:11:28 +00:00
def onchange_journal_id ( self , cursor , user , statement_id , journal_id , context = None ) :
account_journal_obj = self . pool . get ( ' account.journal ' )
res_users_obj = self . pool . get ( ' res.users ' )
cursor . execute ( ' SELECT balance_end_real \
FROM account_bank_statement \
2010-08-26 09:12:23 +00:00
WHERE journal_id = % s AND NOT state = % s \
ORDER BY date DESC , id DESC LIMIT 1 ' , (journal_id, ' draft ' ))
2008-07-22 15:11:28 +00:00
res = cursor . fetchone ( )
balance_start = res and res [ 0 ] or 0.0
2010-08-26 09:50:19 +00:00
return { ' value ' : { ' balance_start ' : balance_start } }
2007-09-05 14:02:52 +00:00
2010-02-04 11:47:04 +00:00
def unlink ( self , cr , uid , ids , context = None ) :
stat = self . read ( cr , uid , ids , [ ' state ' ] )
unlink_ids = [ ]
for t in stat :
if t [ ' state ' ] in ( ' draft ' ) :
unlink_ids . append ( t [ ' id ' ] )
else :
2010-09-27 12:38:06 +00:00
raise osv . except_osv ( _ ( ' Invalid action ! ' ) , _ ( ' Cannot delete bank statement(s) which are already confirmed ! ' ) )
2010-02-04 11:47:04 +00:00
osv . osv . unlink ( self , cr , uid , unlink_ids , context = context )
return True
2010-04-15 13:17:09 +00:00
2010-04-16 09:34:02 +00:00
def copy ( self , cr , uid , id , default = None , context = None ) :
if default is None :
default = { }
if context is None :
2010-04-16 14:37:24 +00:00
context = { }
2010-04-16 09:34:02 +00:00
default = default . copy ( )
default [ ' move_line_ids ' ] = [ ]
2010-09-23 06:38:30 +00:00
return super ( account_bank_statement , self ) . copy ( cr , uid , id , default , context = context )
2010-04-16 14:37:24 +00:00
2007-01-04 09:45:50 +00:00
account_bank_statement ( )
class account_bank_statement_line ( osv . osv ) :
2007-09-05 14:02:52 +00:00
2010-08-14 05:37:43 +00:00
def onchange_partner_id ( self , cursor , user , line_id , partner_id , type , currency_id , context = None ) :
2010-09-07 06:42:15 +00:00
res_users_obj = self . pool . get ( ' res.users ' )
res_currency_obj = self . pool . get ( ' res.currency ' )
2010-05-11 08:51:06 +00:00
res = { ' value ' : { } }
2010-08-14 05:37:43 +00:00
obj_partner = self . pool . get ( ' res.partner ' )
if context is None :
context = { }
2008-07-22 15:11:28 +00:00
if not partner_id :
2010-05-11 08:51:06 +00:00
return res
2010-09-07 06:06:58 +00:00
account_id = False
2010-05-11 08:51:06 +00:00
line = self . browse ( cursor , user , line_id )
if not line or ( line and not line [ 0 ] . account_id ) :
2010-08-14 05:37:43 +00:00
part = obj_partner . browse ( cursor , user , partner_id , context = context )
2010-05-11 08:51:06 +00:00
if type == ' supplier ' :
account_id = part . property_account_payable . id
else :
2010-05-19 12:49:23 +00:00
account_id = part . property_account_receivable . id
2010-05-11 08:51:06 +00:00
res [ ' value ' ] [ ' account_id ' ] = account_id
2010-08-27 11:13:30 +00:00
2010-09-07 09:17:38 +00:00
if account_id and ( not line or ( line and not line [ 0 ] . amount ) ) and not context . get ( ' amount ' , False ) :
2010-08-27 11:13:30 +00:00
company_currency_id = res_users_obj . browse ( cursor , user , user ,
context = context ) . company_id . currency_id . id
if not currency_id :
currency_id = company_currency_id
cursor . execute ( ' SELECT sum(debit-credit) \
FROM account_move_line \
WHERE ( reconcile_id is null ) \
AND partner_id = % s \
AND account_id = % s ' , (partner_id, account_id))
pgres = cursor . fetchone ( )
balance = pgres and pgres [ 0 ] or 0.0
balance = res_currency_obj . compute ( cursor , user , company_currency_id ,
currency_id , balance , context = context )
res [ ' value ' ] [ ' amount ' ] = balance
2010-05-11 08:51:06 +00:00
return res
2008-07-22 15:11:28 +00:00
2010-08-16 05:23:06 +00:00
_order = " statement_id desc, sequence "
2008-07-22 15:11:28 +00:00
_name = " account.bank.statement.line "
_description = " Bank Statement Line "
_columns = {
' name ' : fields . char ( ' Name ' , size = 64 , required = True ) ,
' date ' : fields . date ( ' Date ' , required = True ) ,
' amount ' : fields . float ( ' Amount ' ) ,
' type ' : fields . selection ( [
( ' supplier ' , ' Supplier ' ) ,
( ' customer ' , ' Customer ' ) ,
( ' general ' , ' General ' )
] , ' Type ' , required = True ) ,
' partner_id ' : fields . many2one ( ' res.partner ' , ' Partner ' ) ,
' account_id ' : fields . many2one ( ' account.account ' , ' Account ' ,
required = True ) ,
' statement_id ' : fields . many2one ( ' account.bank.statement ' , ' Statement ' ,
2009-12-01 09:57:30 +00:00
select = True , required = True , ondelete = ' cascade ' ) ,
2010-08-27 12:11:49 +00:00
' analytic_account_id ' : fields . many2one ( ' account.analytic.account ' , ' Analytic Account ' ) ,
2008-07-22 15:11:28 +00:00
' move_ids ' : fields . many2many ( ' account.move ' ,
' account_bank_statement_line_move_rel ' , ' move_id ' , ' statement_id ' ,
' Moves ' ) ,
2009-12-22 09:38:41 +00:00
' ref ' : fields . char ( ' Reference ' , size = 32 ) ,
2008-07-22 15:11:28 +00:00
' note ' : fields . text ( ' Notes ' ) ,
2010-05-19 12:49:23 +00:00
' sequence ' : fields . integer ( ' Sequence ' , help = " Gives the sequence order when displaying a list of bank statement lines. " ) ,
2010-09-16 13:26:14 +00:00
' company_id ' : fields . related ( ' statement_id ' , ' company_id ' , type = ' many2one ' , relation = ' res.company ' , string = ' Company ' , store = True , readonly = True ) ,
2008-07-22 15:11:28 +00:00
}
_defaults = {
' name ' : lambda self , cr , uid , context = { } : self . pool . get ( ' ir.sequence ' ) . get ( cr , uid , ' account.bank.statement.line ' ) ,
2010-09-23 06:38:30 +00:00
' date ' : time . strftime ( ' % Y- % m- %d ' ) ,
' type ' : ' general ' ,
2008-07-22 15:11:28 +00:00
}
2007-09-05 14:02:52 +00:00
2007-01-04 09:45:50 +00:00
account_bank_statement_line ( )
2010-09-06 16:54:21 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: