mirror of
https://github.com/OCA/account-reconcile.git
synced 2025-01-20 12:27:39 +02:00
[FIX] Bug fixing and first running version
(lp:c2c-financial-addons/6.1 rev 24.1.19)
This commit is contained in:
@@ -24,14 +24,17 @@ import datetime
|
||||
import netsvc
|
||||
logger = netsvc.Logger()
|
||||
from openerp.osv.orm import Model, fields
|
||||
from openerp.osv import fields, osv
|
||||
# from account_statement_base_import.parser.file_parser import FileParser
|
||||
from parser import new_bank_statement_parser
|
||||
import sys
|
||||
import traceback
|
||||
|
||||
class AccountStatementProfil(Model):
|
||||
_inherit = "account.statement.profil"
|
||||
|
||||
|
||||
def get_type_selection(self, cr, uid, context=None):
|
||||
def get_import_type_selection(self, cr, uid, context=None):
|
||||
"""
|
||||
Has to be inherited to add parser
|
||||
"""
|
||||
@@ -50,10 +53,12 @@ class AccountStatementProfil(Model):
|
||||
}
|
||||
|
||||
def write_logs_after_import(self, cr, uid, ids, statement_id, num_lines, context):
|
||||
if type(ids) is int:
|
||||
ids = [ids]
|
||||
for id in ids:
|
||||
log = self.read(cr, uid, id, ['rec_log'], context=context)['rec_log']
|
||||
log_line = log and log.split("\n") or []
|
||||
import_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||||
import_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||||
log_line[0:0] = [import_date + ' : '
|
||||
+ _("Bank Statement ID %s have been imported with %s lines ") %(statement_id, num_lines)]
|
||||
log = "\n".join(log_line)
|
||||
@@ -62,6 +67,69 @@ class AccountStatementProfil(Model):
|
||||
"Bank Statement ID %s have been imported with %s lines "%(statement_id, num_lines))
|
||||
return True
|
||||
|
||||
def create_global_commission_line(self, cr, uid, commission_global_amount,
|
||||
result_row_list, profile, statement_id, context):
|
||||
"""Create the global commission line if there is one. The global commission is computed by
|
||||
summing the commission column of each row. Feel free to override the methode to compute
|
||||
your own commission line from the result_row_list.
|
||||
:params: cr: The DB Cursor
|
||||
:params: uid: int/long ID of the current user in the system
|
||||
:params: commission_global_amount: float
|
||||
:params: result_row_list: [{'key':value}]
|
||||
:params: profile: browserecord of account.statement.profile
|
||||
:params: statement_id : int/long of the current importing statement ID
|
||||
:params: context: global context
|
||||
return: an ID of the statement line that represent the global commission
|
||||
for the statement.
|
||||
"""
|
||||
res = False
|
||||
if commission_global_amount:
|
||||
partner_id = profile.partner_id and profile.partner_id.id or False
|
||||
commission_account_id = profile.commission_account_id and profile.commission_account_id.id or False
|
||||
commission_analytic_id = profile.commission_analytic_id and profile.commission_analytic_id.id or False
|
||||
statement_line_obj = self.pool.get('account.bank.statement.line')
|
||||
comm_values = {
|
||||
'name': 'IN '+ _('Commission line'),
|
||||
'date': datetime.datetime.now().date(),
|
||||
'amount': commission_global_amount,
|
||||
'partner_id': partner_id,
|
||||
'type': 'general',
|
||||
'statement_id': statement_id,
|
||||
'account_id': commission_account_id,
|
||||
'ref': 'commission',
|
||||
'analytic_account_id': commission_analytic_id,
|
||||
# !! We set the already_completed so auto-completion will not update those values !
|
||||
'already_completed': True,
|
||||
}
|
||||
res = statement_line_obj.create(cr, uid,
|
||||
comm_values,
|
||||
context=context)
|
||||
return res
|
||||
|
||||
def prepare_statetement_lines_vals(self, cursor, uid, parser_line,
|
||||
account_payable, account_receivable, statement_id, context):
|
||||
"""Hook to change the values of a line. Overide it to compute or change
|
||||
the account or any other values (e.g. if a line is the global commission)"""
|
||||
values = {
|
||||
'name': parser_line.get('label', parser_line.get('ref','/')),
|
||||
'date': parser_line.get('date', datetime.datetime.now().date()),
|
||||
'amount': parser_line['amount'],
|
||||
'ref': parser_line['ref'],
|
||||
'type': 'customer',
|
||||
'statement_id': statement_id,
|
||||
'label': parser_line.get('label',''),
|
||||
'commission_amount': parser_line.get('commission_amount', 0.0),
|
||||
#'account_id': journal.default_debit_account_id
|
||||
}
|
||||
values['account_id'] = statement_obj.get_account_for_counterpart(
|
||||
cursor,
|
||||
uid,
|
||||
parser_line['amount'],
|
||||
account_receivable,
|
||||
account_payable
|
||||
)
|
||||
return values
|
||||
|
||||
def statement_import(self, cursor, 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
|
||||
@@ -76,69 +144,34 @@ class AccountStatementProfil(Model):
|
||||
raise osv.except_osv(
|
||||
_("No Profile !"),
|
||||
_("You must provide a valid profile to import a bank statement !"))
|
||||
else:
|
||||
prof = prof_obj.browse(cursor,uid,profile_id,context)
|
||||
partner_id = prof.partner_id and prof.partner_id.id or False
|
||||
commission_account_id = prof.commission_account_id and prof.commission_account_id.id or False
|
||||
commission_analytic_id = prof.commission_analytic_id and prof.commission_analytic_id.id or False
|
||||
|
||||
parser = new_bank_statement_parser(parse_name=prof.import_type, ftype=ftype)
|
||||
parser = new_bank_statement_parser(prof.import_type, ftype=ftype)
|
||||
result_row_list = parser.parse(file_stream)
|
||||
|
||||
# Check all key are present in account.bank.statement.line !!
|
||||
parsed_cols = self.result_row_list[0].keys()
|
||||
parsed_cols = result_row_list[0].keys()
|
||||
for col in parsed_cols:
|
||||
if col not in statement_line_obj.__columns__:
|
||||
if col not in statement_line_obj._columns:
|
||||
raise osv.except_osv(
|
||||
_("Missing column !"),
|
||||
_("Column %s you try to import is not present in the bank statement line !") %(col))
|
||||
|
||||
statement_id = statement_obj.create(cursor,uid,{'profile_id':prof.id,},context)
|
||||
account_receivable, account_payable = self.get_default_pay_receiv_accounts(cursor, uid, context)
|
||||
account_receivable, account_payable = statement_obj.get_default_pay_receiv_accounts(cursor, uid, context)
|
||||
commission_global_amount = 0.0
|
||||
try:
|
||||
# Record every line in the bank statement and compute the global commission
|
||||
# based on the commission_amount column
|
||||
for line in result_row_list:
|
||||
line_partner_id = False
|
||||
line_to_reconcile = False
|
||||
|
||||
commission_global_amount += line.get('commission_amount', 0.0)
|
||||
values = {
|
||||
'name': line.get('label', line.get('ref','/')),
|
||||
'date': line.get('date', datetime.datetime.now().date()),
|
||||
'amount': line['amount'],
|
||||
'ref': line['ref'],
|
||||
'type': 'customer',
|
||||
'statement_id': statement_id,
|
||||
#'account_id': journal.default_debit_account_id
|
||||
}
|
||||
values['account_id'] = self.get_account_for_counterpart(
|
||||
cursor,
|
||||
uid,
|
||||
line['amount'],
|
||||
account_receivable,
|
||||
account_payable
|
||||
)
|
||||
values = self.prepare_statetement_lines_vals(cursor, uid, line, account_payable,
|
||||
account_receivable, statement_id, context)
|
||||
# we finally create the line in system
|
||||
statement_line_obj.create(cursor, uid, values, context=context)
|
||||
|
||||
# we create commission line
|
||||
if commission_global_amount:
|
||||
comm_values = {
|
||||
'name': 'IN '+ _('Commission line'),
|
||||
'date': datetime.datetime.now().date(),
|
||||
'amount': commission_global_amount,
|
||||
'partner_id': partner_id,
|
||||
'type': 'general',
|
||||
'statement_id': statement_id,
|
||||
'account_id': commission_account_id,
|
||||
'ref': 'commission',
|
||||
'analytic_account_id': commission_analytic_id
|
||||
}
|
||||
statement_line_obj.create(cursor, uid,
|
||||
comm_values,
|
||||
context=context)
|
||||
self.create_global_commission_line(cursor, uid, commission_global_amount,
|
||||
result_row_list, prof, statement_id, context)
|
||||
|
||||
attachment_obj.create(
|
||||
cursor,
|
||||
@@ -158,16 +191,17 @@ class AccountStatementProfil(Model):
|
||||
self.button_auto_completion(cursor, uid, statement_id, context)
|
||||
|
||||
# Write the needed log infos on profile
|
||||
self.write_logs_after_import(self, cr, uid, prof.id, statement_id,
|
||||
self.write_logs_after_import(cursor, uid, prof.id, statement_id,
|
||||
len(result_row_list), context)
|
||||
|
||||
except Exception, exc:
|
||||
logger.notifyChannel("Statement import",
|
||||
netsvc.LOG_ERROR,
|
||||
_("Statement can not be created %s") %(exc,))
|
||||
|
||||
statement_obj.unlink(cursor, uid, [statement_id])
|
||||
raise exc
|
||||
error_type, error_value, trbk = sys.exc_info()
|
||||
st = "Error: %s\nDescription: %s\nTraceback:" % (error_type.__name__, error_value)
|
||||
st += ''.join(traceback.format_tb(trbk, 30))
|
||||
raise osv.except_osv(
|
||||
_("Statement import error"),
|
||||
_("The statement cannot be created : %s") %(st))
|
||||
return statement_id
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user