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
from osv import fields , osv
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-09-14 17:53:07 +00:00
def create ( self , cr , uid , vals , context = None ) :
2010-09-14 18:13:52 +00:00
if ' line_ids ' in vals :
2012-04-17 14:53:22 +00:00
for idx , line in enumerate ( vals [ ' line_ids ' ] ) :
line [ 2 ] [ ' sequence ' ] = idx + 1
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 )
2010-10-20 10:12:02 +00:00
account_bank_statement_line_obj = self . pool . get ( ' account.bank.statement.line ' )
2010-09-14 17:53:07 +00:00
for statement in self . browse ( cr , uid , ids , context ) :
2012-04-17 14:53:22 +00:00
for idx , line in enumerate ( statement . line_ids ) :
account_bank_statement_line_obj . write ( cr , uid , [ line . id ] , { ' sequence ' : idx + 1 } , context = context )
2010-09-14 17:53:07 +00:00
return res
2010-11-19 13:48:01 +00:00
def _default_journal_id ( self , cr , uid , context = None ) :
2010-11-23 11:31:52 +00:00
if context is None :
context = { }
2010-07-30 11:12:06 +00:00
journal_pool = self . pool . get ( ' account.journal ' )
journal_type = context . get ( ' journal_type ' , False )
2012-01-24 15:27:47 +00:00
company_id = self . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' account.bank.statement ' , context = context )
2010-07-30 11:12:06 +00:00
if journal_type :
2012-01-24 15:27:47 +00:00
ids = journal_pool . search ( cr , uid , [ ( ' type ' , ' = ' , journal_type ) , ( ' company_id ' , ' = ' , company_id ) ] )
2010-07-30 11:12:06 +00:00
if ids :
2012-04-17 14:53:22 +00:00
return ids [ 0 ]
return False
2008-07-22 15:11:28 +00:00
def _end_balance ( self , cursor , user , ids , name , attr , context = None ) :
res = { }
2012-05-12 22:37:43 +00:00
for statement in self . browse ( cursor , user , ids , context = context ) :
2008-07-22 15:11:28 +00:00
res [ statement . id ] = statement . balance_start
2012-05-12 22:37:43 +00:00
for line in statement . line_ids :
res [ statement . id ] + = line . amount
2008-07-22 15:11:28 +00:00
return res
2010-11-19 13:48:01 +00:00
def _get_period ( self , cr , uid , context = None ) :
2008-07-22 15:11:28 +00:00
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
2011-09-25 21:00:20 +00:00
def _get_statement ( self , cr , uid , ids , context = None ) :
result = { }
for line in self . pool . get ( ' account.bank.statement.line ' ) . browse ( cr , uid , ids , context = context ) :
result [ line . statement_id . id ] = True
return result . keys ( )
2010-11-04 04:54:38 +00:00
_order = " date desc, id desc "
2008-07-22 15:11:28 +00:00
_name = " account.bank.statement "
_description = " Bank Statement "
2012-05-07 12:07:07 +00:00
_inherit = [ ' mail.thread ' ]
2008-07-22 15:11:28 +00:00
_columns = {
2012-07-16 22:17:06 +00:00
' name ' : fields . char ( ' Reference ' , size = 64 , required = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } , readonly = 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 ' ) , # readonly for account_cash_statement
' date ' : fields . date ( ' Date ' , required = True , states = { ' confirm ' : [ ( ' readonly ' , True ) ] } , select = 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 ' ) ,
2011-09-17 00:50:07 +00:00
states = { ' confirm ' : [ ( ' readonly ' , True ) ] } ) ,
2011-10-12 11:36:55 +00:00
' balance_end ' : fields . function ( _end_balance ,
2011-09-25 21:00:20 +00:00
store = {
2012-05-12 22:37:43 +00:00
' account.bank.statement ' : ( lambda self , cr , uid , ids , c = { } : ids , [ ' line_ids ' , ' move_line_ids ' , ' balance_start ' ] , 10 ) ,
2011-09-25 21:00:20 +00:00
' account.bank.statement.line ' : ( _get_statement , [ ' amount ' ] , 10 ) ,
} ,
2011-09-25 21:54:57 +00:00
string = " Computed Balance " , help = ' Balance as calculated based on Starting Balance and transaction lines ' ) ,
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 ) ] } ) ,
2011-09-22 06:14:19 +00:00
' state ' : fields . selection ( [ ( ' draft ' , ' New ' ) ,
2011-09-17 00:50:07 +00:00
( ' open ' , ' Open ' ) , # used by cash statements
( ' confirm ' , ' Closed ' ) ] ,
2012-05-04 11:57:48 +00:00
' Status ' , required = True , readonly = " 1 " ,
2012-10-12 12:45:13 +00:00
help = ' When new statement is created the status will be \' Draft \' . \n '
' And after getting confirmation from the bank it will be in \' Confirmed \' status. ' ) ,
2011-07-01 23:41:24 +00:00
' currency ' : fields . function ( _currency , string = ' Currency ' ,
2008-07-22 15:11:28 +00:00
type = ' many2one ' , relation = ' res.currency ' ) ,
2010-11-26 15:14:46 +00:00
' account_id ' : fields . related ( ' journal_id ' , ' default_debit_account_id ' , type = ' many2one ' , relation = ' account.account ' , string = ' Account used in this journal ' , readonly = True , help = ' used in statement reconciliation domain, but shouldn \' t be used elswhere. ' ) ,
2008-07-22 15:11:28 +00:00
}
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 ' : " / " ,
2012-02-13 18:07:41 +00:00
' date ' : fields . date . context_today ,
2010-09-23 06:38:30 +00:00
' state ' : ' draft ' ,
2008-07-22 15:11:28 +00:00
' journal_id ' : _default_journal_id ,
' period_id ' : _get_period ,
2011-04-07 15:06:57 +00:00
' company_id ' : lambda self , cr , uid , c : self . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' account.bank.statement ' , context = c ) ,
2008-07-22 15:11:28 +00:00
}
2012-01-25 09:29:32 +00:00
def _check_company_id ( self , cr , uid , ids , context = None ) :
for statement in self . browse ( cr , uid , ids , context = context ) :
if statement . company_id . id != statement . period_id . company_id . id :
return False
return True
_constraints = [
( _check_company_id , ' The journal and period chosen have to belong to the same company. ' , [ ' journal_id ' , ' period_id ' ] ) ,
]
2012-01-24 15:27:47 +00:00
def onchange_date ( self , cr , uid , ids , date , company_id , context = None ) :
2010-07-26 14:41:32 +00:00
"""
2012-01-24 15:27:47 +00:00
Find the correct period to use for the given date and company_id , return it and set it in the context
2010-07-26 14:41:32 +00:00
"""
res = { }
period_pool = self . pool . get ( ' account.period ' )
2010-09-23 06:38:30 +00:00
if context is None :
context = { }
2012-01-24 15:27:47 +00:00
ctx = context . copy ( )
ctx . update ( { ' company_id ' : company_id } )
pids = period_pool . find ( cr , uid , dt = date , context = ctx )
2010-07-26 14:41:32 +00:00
if pids :
2012-01-24 15:27:47 +00:00
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
2012-06-21 10:40:18 +00:00
def _prepare_move ( self , cr , uid , st_line , st_line_number , context = None ) :
""" Prepare the dict of values to create the move from a
statement line . This method may be overridden to implement custom
move generation ( making sure to call super ( ) to establish
a clean extension chain ) .
: param browse_record st_line : account . bank . statement . line record to
create the move from .
: param char st_line_number : will be used as the name of the generated account move
: return : dict of value to create ( ) the account . move
"""
2012-07-25 08:25:39 +00:00
return {
2012-06-21 10:40:18 +00:00
' journal_id ' : st_line . statement_id . journal_id . id ,
' period_id ' : st_line . statement_id . period_id . id ,
' date ' : st_line . date ,
' name ' : st_line_number ,
' ref ' : st_line . ref ,
}
2012-07-25 08:25:39 +00:00
2012-08-07 11:06:16 +00:00
def _prepare_bank_move_line ( self , cr , uid , st_line , move_id , amount , company_currency_id ,
2012-06-21 10:40:18 +00:00
context = None ) :
""" Compute the args to build the dict of values to create the bank move line from a
2012-08-07 11:06:16 +00:00
statement line by calling the _prepare_move_line_vals . This method may be
overridden to implement custom move generation ( making sure to call super ( ) to
2012-06-21 10:40:18 +00:00
establish a clean extension chain ) .
: param browse_record st_line : account . bank . statement . line record to
create the move from .
: param int / long move_id : ID of the account . move to link the move line
: param float amount : amount of the move line
: param int / long company_currency_id : ID of currency of the concerned company
: return : dict of value to create ( ) the bank account . move . line
"""
anl_id = st_line . analytic_account_id and st_line . analytic_account_id . id or False
debit = ( ( amount < 0 ) and - amount ) or 0.0
credit = ( ( amount > 0 ) and amount ) or 0.0
cur_id = False
amt_cur = False
if st_line . statement_id . currency . id < > company_currency_id :
2012-07-25 08:25:39 +00:00
cur_id = st_line . statement_id . currency . id
2012-06-21 10:40:18 +00:00
if st_line . account_id and st_line . account_id . currency_id and st_line . account_id . currency_id . id < > company_currency_id :
cur_id = st_line . account_id . currency_id . id
2012-07-25 08:25:39 +00:00
if cur_id :
res_currency_obj = self . pool . get ( ' res.currency ' )
amt_cur = - res_currency_obj . compute ( cr , uid , company_currency_id , cur_id , amount , context = context )
2012-08-07 11:06:16 +00:00
res = self . _prepare_move_line_vals ( cr , uid , st_line , move_id , debit , credit ,
2012-07-25 08:25:39 +00:00
amount_currency = amt_cur , currency_id = cur_id , analytic_id = anl_id , context = context )
2012-06-21 10:40:18 +00:00
return res
def _get_counter_part_account ( sefl , cr , uid , st_line , context = None ) :
2012-08-07 11:06:16 +00:00
""" Retrieve the account to use in the counterpart move.
2012-06-21 10:40:18 +00:00
This method may be overridden to implement custom move generation ( making sure to
call super ( ) to establish a clean extension chain ) .
: param browse_record st_line : account . bank . statement . line record to
create the move from .
: return : int / long of the account . account to use as counterpart
"""
if st_line . amount > = 0 :
2012-07-25 08:25:39 +00:00
return st_line . statement_id . journal_id . default_credit_account_id . id
return st_line . statement_id . journal_id . default_debit_account_id . id
2012-06-21 10:40:18 +00:00
def _get_counter_part_partner ( sefl , cr , uid , st_line , context = None ) :
2012-08-07 11:06:16 +00:00
""" Retrieve the partner to use in the counterpart move.
2012-06-21 10:40:18 +00:00
This method may be overridden to implement custom move generation ( making sure to
call super ( ) to establish a clean extension chain ) .
: param browse_record st_line : account . bank . statement . line record to
create the move from .
: return : int / long of the res . partner to use as counterpart
"""
2012-07-25 08:25:39 +00:00
return st_line . partner_id and st_line . partner_id . id or False
2012-06-21 10:40:18 +00:00
2012-08-07 11:06:16 +00:00
def _prepare_counterpart_move_line ( self , cr , uid , st_line , move_id , amount , company_currency_id ,
2012-06-21 10:40:18 +00:00
context = None ) :
""" Compute the args to build the dict of values to create the counter part move line from a
2012-08-07 11:06:16 +00:00
statement line by calling the _prepare_move_line_vals . This method may be
overridden to implement custom move generation ( making sure to call super ( ) to
2012-06-21 10:40:18 +00:00
establish a clean extension chain ) .
: param browse_record st_line : account . bank . statement . line record to
create the move from .
: param int / long move_id : ID of the account . move to link the move line
: param float amount : amount of the move line
: param int / long account_id : ID of account to use as counter part
: param int / long company_currency_id : ID of currency of the concerned company
: return : dict of value to create ( ) the bank account . move . line
"""
account_id = self . _get_counter_part_account ( cr , uid , st_line , context = context )
partner_id = self . _get_counter_part_partner ( cr , uid , st_line , context = context )
debit = ( ( amount > 0 ) and amount ) or 0.0
credit = ( ( amount < 0 ) and - amount ) or 0.0
cur_id = False
amt_cur = False
if st_line . statement_id . currency . id < > company_currency_id :
amt_cur = st_line . amount
cur_id = st_line . statement_id . currency . id
2012-08-07 11:06:16 +00:00
return self . _prepare_move_line_vals ( cr , uid , st_line , move_id , debit , credit ,
2012-06-21 10:40:18 +00:00
amount_currency = amt_cur , currency_id = cur_id , account_id = account_id ,
partner_id = partner_id , context = context )
2012-07-25 08:25:39 +00:00
2012-06-21 10:40:18 +00:00
def _prepare_move_line_vals ( self , cr , uid , st_line , move_id , debit , credit , currency_id = False ,
2012-08-07 11:06:16 +00:00
amount_currency = False , account_id = False , analytic_id = False ,
2012-06-21 10:40:18 +00:00
partner_id = False , context = None ) :
""" Prepare the dict of values to create the move line from a
statement line . All non - mandatory args will replace the default computed one .
This method may be overridden to implement custom move generation ( making sure to
call super ( ) to establish a clean extension chain ) .
: param browse_record st_line : account . bank . statement . line record to
create the move from .
: param int / long move_id : ID of the account . move to link the move line
: param float debit : debit amount of the move line
: param float credit : credit amount of the move line
: param int / long currency_id : ID of currency of the move line to create
: param float amount_currency : amount of the debit / credit expressed in the currency_id
: param int / long account_id : ID of the account to use in the move line if different
from the statement line account ID
: param int / long analytic_id : ID of analytic account to put on the move line
: param int / long partner_id : ID of the partner to put on the move line
: return : dict of value to create ( ) the account . move . line
"""
2012-07-25 08:25:39 +00:00
acc_id = account_id or st_line . account_id . id
cur_id = currency_id or st_line . statement_id . currency . id
par_id = partner_id or ( ( ( st_line . partner_id ) and st_line . partner_id . id ) or False )
return {
2012-06-21 10:40:18 +00:00
' name ' : st_line . name ,
' date ' : st_line . date ,
' ref ' : st_line . ref ,
' move_id ' : move_id ,
2012-09-17 15:05:48 +00:00
' partner_id ' : par_id ,
2012-06-21 10:40:18 +00:00
' account_id ' : acc_id ,
' credit ' : credit ,
' debit ' : debit ,
' statement_id ' : st_line . statement_id . id ,
' journal_id ' : st_line . statement_id . journal_id . id ,
' period_id ' : st_line . statement_id . period_id . id ,
' currency_id ' : cur_id ,
2012-07-25 08:25:39 +00:00
' amount_currency ' : amount_currency ,
' analytic_account_id ' : analytic_id ,
2012-06-21 10:40:18 +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 ) :
2012-07-25 08:25:39 +00:00
""" Create the account move from the statement line.
: param int / long st_line_id : ID of the account . bank . statement . line to create the move from .
2012-06-21 10:40:18 +00:00
: param int / long company_currency_id : ID of the res . currency of the company
: param char st_line_number : will be used as the name of the generated account move
: return : ID of the account . move created
"""
2010-11-23 11:31:52 +00:00
if context is None :
context = { }
2008-07-22 15:11:28 +00:00
res_currency_obj = self . pool . get ( ' res.currency ' )
account_move_obj = self . pool . get ( ' account.move ' )
account_move_line_obj = self . pool . get ( ' account.move.line ' )
2010-08-16 07:03:58 +00:00
account_bank_statement_line_obj = self . pool . get ( ' account.bank.statement.line ' )
2010-11-19 13:48:01 +00:00
st_line = account_bank_statement_line_obj . browse ( cr , uid , st_line_id , context = 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 } )
2010-09-29 08:03:34 +00:00
2012-06-21 10:40:18 +00:00
move_vals = self . _prepare_move ( cr , uid , st_line , st_line_number , context = context )
move_id = account_move_obj . create ( cr , uid , move_vals , context = context )
2010-09-17 14:30:45 +00:00
account_bank_statement_line_obj . write ( cr , uid , [ st_line . id ] , {
' move_ids ' : [ ( 4 , move_id , False ) ]
} )
torec = [ ]
acc_cur = ( ( st_line . amount < = 0 ) and st . journal_id . default_debit_account_id ) or st_line . account_id
2012-06-21 10:40:18 +00:00
2010-12-22 17:41:59 +00:00
context . update ( {
' res.currency.compute.account ' : acc_cur ,
} )
2010-09-17 14:30:45 +00:00
amount = res_currency_obj . compute ( cr , uid , st . currency . id ,
2010-12-22 17:41:59 +00:00
company_currency_id , st_line . amount , context = context )
2010-09-17 14:30:45 +00:00
2012-08-07 11:06:16 +00:00
bank_move_vals = self . _prepare_bank_move_line ( cr , uid , st_line , move_id , amount ,
2012-06-21 10:40:18 +00:00
company_currency_id , context = context )
2012-07-25 08:25:39 +00:00
move_line_id = account_move_line_obj . create ( cr , uid , bank_move_vals , context = context )
2010-09-17 14:30:45 +00:00
torec . append ( move_line_id )
2010-09-22 06:23:08 +00:00
2012-08-07 11:06:16 +00:00
counterpart_move_vals = self . _prepare_counterpart_move_line ( cr , uid , st_line , move_id ,
2012-06-21 10:40:18 +00:00
amount , company_currency_id , context = context )
2012-07-25 08:25:39 +00:00
account_move_line_obj . create ( cr , uid , counterpart_move_vals , context = context )
2010-09-17 14:30:45 +00:00
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 ' :
2012-08-07 11:31:37 +00:00
raise osv . except_osv ( _ ( ' Error! ' ) ,
2011-12-31 07:57:20 +00:00
_ ( ' Journal item " %s " is not valid. ' ) % line . name )
2010-09-17 14:30:45 +00:00
# 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 ) :
2010-10-26 16:16:52 +00:00
return st_number + ' / ' + str ( st_line . sequence )
2010-09-20 15:59:47 +00:00
2010-09-22 15:41:58 +00:00
def balance_check ( self , cr , uid , st_id , journal_type = ' bank ' , context = None ) :
2010-11-19 13:48:01 +00:00
st = self . browse ( cr , uid , st_id , context = context )
2012-05-12 22:37:43 +00:00
if not ( ( abs ( ( st . balance_end or 0.0 ) - st . balance_end_real ) < 0.0001 ) or ( abs ( ( st . balance_end or 0.0 ) - st . balance_end_real ) < 0.0001 ) ) :
2012-08-07 11:31:37 +00:00
raise osv . except_osv ( _ ( ' Error! ' ) ,
2011-03-09 04:58:22 +00:00
_ ( ' The statement balance is incorrect ! \n The expected balance ( %.2f ) is different than the computed one. ( %.2f ) ' ) % ( st . balance_end_real , st . balance_end ) )
2010-09-22 15:41:58 +00:00
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 ' ) :
2011-09-25 21:00:20 +00:00
return state in ( ' draft ' , ' open ' )
2010-09-22 15:41:58 +00:00
2010-09-17 14:30:45 +00:00
def button_confirm_bank ( self , cr , uid , ids , context = None ) :
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
2010-11-19 13:48:01 +00:00
for st in self . browse ( cr , uid , ids , context = 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 ) :
2012-08-07 11:06:16 +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 :
2012-03-22 11:39:59 +00:00
c = { ' fiscalyear_id ' : st . period_id . fiscalyear_id . id }
2010-09-17 14:30:45 +00:00
if st . journal_id . sequence_id :
2011-09-29 13:22:54 +00:00
st_number = obj_seq . next_by_id ( cr , uid , st . journal_id . sequence_id . id , context = c )
2010-09-17 14:30:45 +00:00
else :
2012-03-14 12:53:24 +00:00
st_number = obj_seq . next_by_code ( cr , uid , ' account.bank.statement ' , context = c )
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 ' :
2012-08-07 11:31:37 +00:00
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 :
2011-12-31 07:57:20 +00:00
raise osv . except_osv ( _ ( ' No Analytic Journal ! ' ) , _ ( " You have to assign 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-30 07:06:01 +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
2011-12-22 10:27:03 +00:00
self . write ( cr , uid , [ st . id ] , {
' name ' : st_number ,
' balance_end_real ' : st . balance_end
} , context = context )
2012-08-22 11:34:39 +00:00
self . message_post ( cr , uid , [ st . id ] , body = _ ( ' Statement %s confirmed, journal items were created. ' ) % ( st_number , ) , context = context )
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 = [ ]
2010-10-20 10:12:02 +00:00
account_move_obj = self . pool . get ( ' account.move ' )
2010-11-19 13:48:01 +00:00
for st in self . browse ( cr , uid , ids , context = context ) :
2008-07-22 15:11:28 +00:00
if st . state == ' draft ' :
continue
ids = [ ]
for line in st . line_ids :
ids + = [ x . id for x in line . move_ids ]
2010-10-20 10:12:02 +00:00
account_move_obj . unlink ( cr , uid , ids , context )
2008-07-22 15:11:28 +00:00
done . append ( st . id )
2010-09-23 06:38:30 +00:00
return self . write ( cr , uid , done , { ' state ' : ' draft ' } , context = context )
2012-08-07 11:06:16 +00:00
2012-04-26 12:30:17 +00:00
def _compute_balance_end_real ( self , cr , uid , journal_id , context = None ) :
2012-09-25 07:10:40 +00:00
res = False
if journal_id :
cr . execute ( ' SELECT balance_end_real \
FROM account_bank_statement \
WHERE journal_id = % s AND NOT state = % s \
ORDER BY date DESC , id DESC LIMIT 1 ' , (journal_id, ' draft ' ))
res = cr . fetchone ( )
2012-04-26 12:30:17 +00:00
return res and res [ 0 ] or 0.0
def onchange_journal_id ( self , cr , uid , statement_id , journal_id , context = None ) :
2012-09-05 10:31:36 +00:00
if not journal_id :
return { }
2012-04-26 12:30:17 +00:00
balance_start = self . _compute_balance_end_real ( cr , uid , journal_id , context = context )
2012-10-09 09:21:29 +00:00
journal_data = self . pool . get ( ' account.journal ' ) . read ( cr , uid , journal_id , [ ' company_id ' , ' currency ' ] , context = context )
2012-01-24 15:27:47 +00:00
company_id = journal_data [ ' company_id ' ]
2012-10-09 09:21:29 +00:00
currency_id = journal_data [ ' currency ' ] or self . pool . get ( ' res.company ' ) . browse ( cr , uid , company_id [ 0 ] , context = context ) . currency_id . id
return { ' value ' : { ' balance_start ' : balance_start , ' company_id ' : company_id , ' currency ' : currency_id } }
2007-09-05 14:02:52 +00:00
2010-02-04 11:47:04 +00:00
def unlink ( self , cr , uid , ids , context = None ) :
2010-12-13 06:43:09 +00:00
stat = self . read ( cr , uid , ids , [ ' state ' ] , context = context )
2010-02-04 11:47:04 +00:00
unlink_ids = [ ]
for t in stat :
if t [ ' state ' ] in ( ' draft ' ) :
unlink_ids . append ( t [ ' id ' ] )
else :
2012-08-07 11:06:16 +00:00
raise osv . except_osv ( _ ( ' Invalid Action! ' ) , _ ( ' In order to delete a bank statement, you must first cancel it to delete related journal items. ' ) )
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
2011-01-05 07:20:08 +00:00
def onchange_partner_id ( self , cr , uid , ids , partner_id , context = None ) :
obj_partner = self . pool . get ( ' res.partner ' )
if context is None :
context = { }
if not partner_id :
return { }
part = obj_partner . browse ( cr , uid , partner_id , context = context )
if not part . supplier and not part . customer :
2011-01-04 12:40:45 +00:00
type = ' general '
2011-01-05 07:20:08 +00:00
elif part . supplier and part . customer :
2011-01-04 12:40:45 +00:00
type = ' general '
else :
2011-01-05 07:20:08 +00:00
if part . supplier == True :
2011-01-04 12:40:45 +00:00
type = ' supplier '
2011-01-05 07:20:08 +00:00
if part . customer == True :
2011-01-04 12:40:45 +00:00
type = ' customer '
2011-01-05 07:20:08 +00:00
res_type = self . onchange_type ( cr , uid , ids , partner_id = partner_id , type = type , context = context )
if res_type [ ' value ' ] and res_type [ ' value ' ] . get ( ' account_id ' , False ) :
return { ' value ' : { ' type ' : type , ' account_id ' : res_type [ ' value ' ] [ ' account_id ' ] } }
2011-01-05 06:17:06 +00:00
return { ' value ' : { ' type ' : type } }
2011-01-04 12:40:45 +00:00
2010-10-26 16:16:52 +00:00
def onchange_type ( self , cr , uid , line_id , partner_id , type , context = None ) :
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-11-19 13:48:01 +00:00
line = self . browse ( cr , uid , line_id , context = context )
2010-05-11 08:51:06 +00:00
if not line or ( line and not line [ 0 ] . account_id ) :
2010-10-26 16:16:52 +00:00
part = obj_partner . browse ( cr , uid , 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
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 = {
2010-10-26 16:16:52 +00:00
' name ' : fields . char ( ' Communication ' , size = 64 , required = True ) ,
2008-07-22 15:11:28 +00:00
' date ' : fields . date ( ' Date ' , required = True ) ,
2010-12-09 09:34:48 +00:00
' amount ' : fields . float ( ' Amount ' , digits_compute = dp . get_precision ( ' Account ' ) ) ,
2008-07-22 15:11:28 +00:00
' 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 ' ) ,
2012-01-02 09:08:33 +00:00
' journal_id ' : fields . related ( ' statement_id ' , ' journal_id ' , type = ' many2one ' , relation = ' account.journal ' , string = ' Journal ' , store = True , readonly = True ) ,
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 ' ,
2011-05-25 06:13:53 +00:00
' account_bank_statement_line_move_rel ' , ' statement_line_id ' , ' move_id ' ,
2008-07-22 15:11:28 +00:00
' 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 ' ) ,
2011-12-09 06:03:08 +00:00
' sequence ' : fields . integer ( ' Sequence ' , select = True , 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 ' ) ,
2012-02-14 12:25:20 +00:00
' date ' : lambda self , cr , uid , context = { } : context . get ( ' date ' , fields . date . context_today ( self , cr , uid , context = context ) ) ,
2010-09-23 06:38:30 +00:00
' 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-11-26 15:14:46 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: