[IMP] Give the possibility to parse and import multiple statments

This commit is contained in:
Laurent Mignon (Acsone)
2014-05-05 21:36:31 +02:00
parent 6c84e21644
commit 0960cf0d03
3 changed files with 38 additions and 21 deletions

View File

@@ -54,6 +54,7 @@ class BankStatementImportParser(object):
self.balance_end = None self.balance_end = None
self.statement_name = None self.statement_name = None
self.statement_date = None self.statement_date = None
self.support_multi_statements = False
@classmethod @classmethod
def parser_for(cls, parser_name): def parser_for(cls, parser_name):
@@ -163,10 +164,16 @@ class BankStatementImportParser(object):
raise Exception(_('No buffer file given.')) raise Exception(_('No buffer file given.'))
self._format(*args, **kwargs) self._format(*args, **kwargs)
self._pre(*args, **kwargs) self._pre(*args, **kwargs)
if self.support_multi_statements:
while self._parse(*args, **kwargs):
self._validate(*args, **kwargs)
self._post(*args, **kwargs)
yield self.result_row_list
else:
self._parse(*args, **kwargs) self._parse(*args, **kwargs)
self._validate(*args, **kwargs) self._validate(*args, **kwargs)
self._post(*args, **kwargs) self._post(*args, **kwargs)
return self.result_row_list yield self.result_row_list
def itersubclasses(cls, _seen=None): def itersubclasses(cls, _seen=None):

View File

@@ -138,20 +138,8 @@ class AccountStatementProfil(Model):
vals.update(parser.get_st_vals()) vals.update(parser.get_st_vals())
return vals return vals
def statement_import(self, cr, uid, ids, profile_id, file_stream, ftype="csv", context=None): def multi_statement_import(self, cr, uid, ids, profile_id, file_stream,
""" ftype="csv", context=None):
Create a bank statement with the given profile and parser. It will fullfill the bank statement
with the values of the file providen, but will not complete data (like finding the partner, or
the right account). This will be done in a second step with the completion rules.
:param int/long profile_id: ID of the profile used to import the file
:param filebuffer file_stream: binary of the providen file
:param char: ftype represent the file exstension (csv by default)
:return: ID of the created account.bank.statemênt
"""
statement_obj = self.pool.get('account.bank.statement')
statement_line_obj = self.pool.get('account.bank.statement.line')
attachment_obj = self.pool.get('ir.attachment')
prof_obj = self.pool.get("account.statement.profile") prof_obj = self.pool.get("account.statement.profile")
if not profile_id: if not profile_id:
raise osv.except_osv(_("No Profile!"), raise osv.except_osv(_("No Profile!"),
@@ -159,7 +147,29 @@ class AccountStatementProfil(Model):
prof = prof_obj.browse(cr, uid, profile_id, context=context) prof = prof_obj.browse(cr, uid, profile_id, context=context)
parser = new_bank_statement_parser(prof.import_type, ftype=ftype) parser = new_bank_statement_parser(prof.import_type, ftype=ftype)
result_row_list = parser.parse(file_stream) res = []
for result_row_list in parser.parse(file_stream):
statement_id = self._statement_import(cr, uid, ids, prof, parser, file_stream, ftype=ftype, context=context)
res.append(statement_id)
return res
def _statement_import(self, cr, uid, ids, prof, parser, file_stream, ftype="csv", context=None):
"""
Create a bank statement with the given profile and parser. It will fullfill the bank statement
with the values of the file providen, but will not complete data (like finding the partner, or
the right account). This will be done in a second step with the completion rules.
:param profile : The profile used to import the file
:param parser: the parser
:param filebuffer file_stream: binary of the providen file
:param char: ftype represent the file exstension (csv by default)
:return: ID of the created account.bank.statemênt
"""
statement_obj = self.pool.get('account.bank.statement')
statement_line_obj = self.pool.get('account.bank.statement.line')
attachment_obj = self.pool.get('ir.attachment')
result_row_list = parser.result_row_list
# Check all key are present in account.bank.statement.line!! # Check all key are present in account.bank.statement.line!!
if not result_row_list: if not result_row_list:
raise osv.except_osv(_("Nothing to import"), raise osv.except_osv(_("Nothing to import"),

View File

@@ -99,7 +99,7 @@ class CreditPartnerStatementImporter(orm.TransientModel):
ftype = self._check_extension(importer.file_name) ftype = self._check_extension(importer.file_name)
context['file_name'] = importer.file_name context['file_name'] = importer.file_name
sid = self.pool.get( sid = self.pool.get(
'account.statement.profile').statement_import( 'account.statement.profile').multi_statement_import(
cr, cr,
uid, uid,
False, False,
@@ -112,5 +112,5 @@ class CreditPartnerStatementImporter(orm.TransientModel):
action_obj = self.pool.get('ir.actions.act_window') action_obj = self.pool.get('ir.actions.act_window')
action_id = model_obj.get_object_reference(cr, uid, 'account', 'action_bank_statement_tree')[1] action_id = model_obj.get_object_reference(cr, uid, 'account', 'action_bank_statement_tree')[1]
res = action_obj.read(cr, uid, action_id) res = action_obj.read(cr, uid, action_id)
res['domain'] = res['domain'][:-1] + ",('id', '=', %d)]" % sid res['domain'] = res['domain'][:-1] + ",('id', 'in', %s)]" % sid
return res return res