|
|
@ -1,15 +1,8 @@ |
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
''' |
|
|
|
Created on 09 Octobre 2016 |
|
|
|
|
|
|
|
@author: Thibault Francois (thibault@françois.be) |
|
|
|
''' |
|
|
|
|
|
|
|
from StringIO import StringIO |
|
|
|
from io import StringIO |
|
|
|
import csv |
|
|
|
import datetime |
|
|
|
import md5 |
|
|
|
from openerp import models, _ |
|
|
|
import hashlib |
|
|
|
from odoo import models, _ |
|
|
|
|
|
|
|
ACCOUNT = "Compte donneur d'ordre" |
|
|
|
CURRENCY = "Devise" |
|
|
@ -18,7 +11,7 @@ AMOUNT = "Montant" |
|
|
|
COUNTERPART_NUMBER = "Compte contrepartie" |
|
|
|
COUNTERPART_NAME = "Contrepartie" |
|
|
|
COMMUNICATION = "Communication" |
|
|
|
TRANSACTION_TYPE = "Type d'op\xc3\xa9ration" |
|
|
|
TRANSACTION_TYPE = "Type d'opération" |
|
|
|
|
|
|
|
class CodaBankStatementImport(models.TransientModel): |
|
|
|
_inherit = 'account.bank.statement.import' |
|
|
@ -29,7 +22,8 @@ class CodaBankStatementImport(models.TransientModel): |
|
|
|
_csv_delimiter = ";" |
|
|
|
_csv_quote = '"' |
|
|
|
|
|
|
|
_header = ['Date', 'Montant', 'Devise', 'Contrepartie', 'Compte contrepartie', "Type d'op\xc3\xa9ration", 'Communication', "Compte donneur d'ordre"] |
|
|
|
_header = ['Date', 'Montant', 'Devise', 'Contrepartie', 'Compte contrepartie', "Type d'opération", |
|
|
|
'Communication', "Compte donneur d'ordre"] |
|
|
|
|
|
|
|
|
|
|
|
def _generate_note_crelan(self, move): |
|
|
@ -45,28 +39,29 @@ class CodaBankStatementImport(models.TransientModel): |
|
|
|
'note': self._generate_note_crelan(move), |
|
|
|
'date': self._to_iso_date(move[DATE]), |
|
|
|
'amount': float(move[AMOUNT]), |
|
|
|
'account_number': move[COUNTERPART_NUMBER], #ok |
|
|
|
'partner_name': move[COUNTERPART_NAME], #ok |
|
|
|
'account_number': move[COUNTERPART_NUMBER], # Ok |
|
|
|
'partner_name': move[COUNTERPART_NAME], # Ok |
|
|
|
'ref': move[DATE] + '-' + move[AMOUNT] + '-' + move[COUNTERPART_NUMBER] + '-' + move[COUNTERPART_NAME], |
|
|
|
'sequence': sequence, #ok |
|
|
|
'unique_import_id' : move[DATE] + '-' + move[AMOUNT] + '-' + move[COUNTERPART_NUMBER] + '-' + move[COUNTERPART_NAME] + '-' + md5.new(move[COMMUNICATION]).hexdigest() |
|
|
|
'sequence': sequence, # Ok |
|
|
|
'unique_import_id': move[DATE] + '-' + move[AMOUNT] + '-' + move[COUNTERPART_NUMBER] + '-' + |
|
|
|
move[COUNTERPART_NAME] + '-' + hashlib.new('md5', move[COMMUNICATION].encode()).hexdigest() |
|
|
|
} |
|
|
|
return move_data |
|
|
|
|
|
|
|
def _get_statement_data_crelan(self, balance_start, balance_end, begin_date, end_date): |
|
|
|
statement_data = { |
|
|
|
'name' : _("Bank Statement from %s to %s") % (begin_date, end_date), |
|
|
|
'date' : self._to_iso_date(end_date), |
|
|
|
'balance_start': balance_start, #ok |
|
|
|
'balance_end_real' : balance_end, #ok |
|
|
|
'name': _("Bank Statement from %s to %s") % (begin_date, end_date), |
|
|
|
'date': self._to_iso_date(end_date), |
|
|
|
'balance_start': balance_start, # Ok |
|
|
|
'balance_end_real' : balance_end, # Ok |
|
|
|
'transactions' : [] |
|
|
|
} |
|
|
|
return statement_data |
|
|
|
|
|
|
|
def _get_acc_number_crelan(self, acc_number): |
|
|
|
#Check if we match the exact acc_number or the end of an acc number |
|
|
|
# Check if we match the exact acc_number or the end of an acc number |
|
|
|
journal = self.env['account.journal'].search([('bank_acc_number', '=like', '%' + acc_number)]) |
|
|
|
if not journal or len(journal) > 1: #if not found or ambiguious |
|
|
|
if not journal or len(journal) > 1: # If not found or ambiguious |
|
|
|
return acc_number |
|
|
|
|
|
|
|
return journal.bank_acc_number |
|
|
@ -76,13 +71,14 @@ class CodaBankStatementImport(models.TransientModel): |
|
|
|
return self.init_balance |
|
|
|
|
|
|
|
journal = self.env['account.journal'].search([('bank_acc_number', '=like', '%' + acc_number)]) |
|
|
|
if not journal or len(journal) > 1: #if not found or ambiguious |
|
|
|
currency = journal.currency_id or journal.company_id.currency_id |
|
|
|
if not journal or len(journal) > 1: # If not found or ambiguious |
|
|
|
self.init_balance = 0.0 |
|
|
|
else: |
|
|
|
lang = self._context.get('lang', 'en_US') |
|
|
|
l = self.env['res.lang'].search([('code', '=', lang)]) |
|
|
|
balance = journal.get_journal_dashboard_datas()['last_balance'][:-1] |
|
|
|
self.init_balance = float(balance.strip().replace(l.thousands_sep, '').replace(l.decimal_point, '.')) |
|
|
|
self.init_balance = float(balance.replace(currency.symbol, '').strip().replace(l.thousands_sep, '').replace(l.decimal_point, '.')) |
|
|
|
return self.init_balance |
|
|
|
|
|
|
|
def _to_iso_date(self, orig_date): |
|
|
@ -92,7 +88,7 @@ class CodaBankStatementImport(models.TransientModel): |
|
|
|
def _parse_file(self, data_file): |
|
|
|
|
|
|
|
try: |
|
|
|
csv_file = StringIO(data_file) |
|
|
|
csv_file = StringIO(data_file.decode()) |
|
|
|
data = csv.DictReader(csv_file, delimiter=self._csv_delimiter, quotechar=self._csv_quote) |
|
|
|
if not data.fieldnames == self._header: |
|
|
|
raise ValueError() |
|
|
@ -117,6 +113,6 @@ class CodaBankStatementImport(models.TransientModel): |
|
|
|
transactions.append(self._get_move_value_crelan(statement, i)) |
|
|
|
sum_transaction += float(statement[AMOUNT]) |
|
|
|
i += 1 |
|
|
|
stmt = self._get_statement_data_crelan(balance, balance+ sum_transaction, begin_date, end_date) |
|
|
|
stmt = self._get_statement_data_crelan(balance, balance + sum_transaction, begin_date, end_date) |
|
|
|
stmt['transactions'] = transactions |
|
|
|
return currency_code, self._get_acc_number_crelan(account_number), [stmt] |