@ -3,74 +3,67 @@
# flake8: noqa
# flake8: noqa
import logging
import logging
import base64
import os
import StringIO
from openerp.osv import osv
from openerp.osv import osv
from openerp.tools.translate import _
from openerp.tools.translate import _
from openerp.exceptions import Warning
_logger = logging . getLogger ( __name__ )
_logger = logging . getLogger ( __name__ )
from openerp.addons.account_bank_statement_import import account_bank_statement_import as ibs
ibs . add_file_type ( ( ' ofx ' , ' OFX ' ) )
try :
try :
from ofxparse import OfxParser as ofxparser
from ofxparse import OfxParser as ofxparser
except ImportError :
except ImportError :
_logger . warning ( " OFX parser unavailable because the `ofxparse` Python library cannot be found. "
_logger . error ( " OFX parser unavailable because the `ofxparse` Python library cannot be found. "
" It can be downloaded and installed from `https://pypi.python.org/pypi/ofxparse`. " )
" It can be downloaded and installed from `https://pypi.python.org/pypi/ofxparse`. " )
ofxparser = None
ofxparser = None
class account_bank_statement_import ( osv . TransientModel ) :
class account_bank_statement_import ( osv . TransientModel ) :
_inherit = ' account.bank.statement.import '
_inherit = ' account.bank.statement.import '
def process_ofx ( self , cr , uid , data_file , journal_id = False , context = None ) :
""" Import a file in the .OFX format """
def _check_ofx ( self , cr , uid , file , context = None ) :
if ofxparser is None :
if ofxparser is None :
raise osv . except_osv ( _ ( " Error " ) , _ ( " OFX parser unavailable because the `ofxparse` Python library cannot be found. "
" It can be downloaded and installed from `https://pypi.python.org/pypi/ofxparse`. " ) )
return False
try :
try :
tempfile = open ( " temp.ofx " , " w+ " )
tempfile . write ( base64 . decodestring ( data_file ) )
tempfile . read ( )
pathname = os . path . dirname ( ' temp.ofx ' )
path = os . path . join ( os . path . abspath ( pathname ) , ' temp.ofx ' )
ofx = ofxparser . parse ( file ( path ) )
ofx = ofxparser . parse ( file )
except :
except :
raise osv . except_osv ( _ ( ' Import Error! ' ) , _ ( ' Please check OFX file format is proper or not. ' ) )
line_ids = [ ]
return False
return ofx
def _parse_file ( self , cr , uid , data_file , context = None ) :
ofx = self . _check_ofx ( cr , uid , StringIO . StringIO ( data_file ) , context = context )
if not ofx :
return super ( account_bank_statement_import , self ) . _parse_file ( cr , uid , data_file , context = context )
transactions = [ ]
total_amt = 0.00
total_amt = 0.00
try :
try :
for transaction in ofx . account . statement . transactions :
for transaction in ofx . account . statement . transactions :
bank_account_id , partner_id = self . _detect_partner ( cr , uid , transaction . payee , identifying_field = ' owner_name ' , context = context )
# Since ofxparse doesn't provide account numbers, we'll have to find res.partner and res.partner.bank here
# (normal behavious is to provide 'account_number', which the generic module uses to find partner/bank)
bank_account_id = partner_id = False
ids = self . pool . get ( ' res.partner.bank ' ) . search ( cr , uid , [ ( ' owner_name ' , ' = ' , transaction . payee ) ] , context = context )
if ids :
bank_account_id = bank_account_id = ids [ 0 ]
partner_id = self . pool . get ( ' res.partner.bank ' ) . browse ( cr , uid , bank_account_id , context = context ) . partner_id . id
vals_line = {
vals_line = {
' date ' : transaction . date ,
' date ' : transaction . date ,
' name ' : transaction . payee + ' : ' + transaction . memo ,
' name ' : transaction . payee + ( transaction . memo and ' : ' + transaction . memo or ' ' ) ,
' ref ' : transaction . id ,
' ref ' : transaction . id ,
' amount ' : transaction . amount ,
' amount ' : transaction . amount ,
' partner_id ' : partner_id ,
' unique_import_id ' : transaction . id ,
' bank_account_id ' : bank_account_id ,
' bank_account_id ' : bank_account_id ,
' partner_id ' : partner_id ,
}
}
total_amt + = float ( transaction . amount )
total_amt + = float ( transaction . amount )
line_ids . append ( ( 0 , 0 , vals_line ) )
transactions . append ( vals_line )
except Exception , e :
except Exception , e :
raise osv . except_osv ( _ ( ' Error! ' ) , _ ( " Following problem has been occurred while importing your file, Please verify the file is proper or not. \n \n %s " % e . message ) )
st_start_date = ofx . account . statement . start_date or False
st_end_date = ofx . account . statement . end_date or False
period_obj = self . pool . get ( ' account.period ' )
if st_end_date :
period_ids = period_obj . find ( cr , uid , st_end_date , context = context )
else :
period_ids = period_obj . find ( cr , uid , st_start_date , context = context )
raise Warning ( _ ( " The following problem occurred during import. The file might not be valid. \n \n %s " % e . message ) )
vals_bank_statement = {
vals_bank_statement = {
' name ' : ofx . account . routing_number ,
' name ' : ofx . account . routing_number ,
' balance_start ' : ofx . account . statement . balance ,
' balance_end_real ' : float ( ofx . account . statement . balance ) + total_amt ,
' period_id ' : period_ids and period_ids [ 0 ] or False ,
' journal_id ' : journal_id
' transactions ' : transactions ,
' balance_start ' : float ( ofx . account . statement . balance ) - total_amt ,
' balance_end_real ' : float ( ofx . account . statement . balance ) ,
}
}
vals_bank_statement . update ( { ' line_ids ' : line_ids } )
os . remove ( path )
return [ vals_bank_statement ]
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
return ofx . account . statement . currency , ofx . account . number , [ vals_bank_statement ]