|
@ -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 csv |
|
|
import datetime |
|
|
import datetime |
|
|
import md5 |
|
|
|
|
|
from openerp import models, _ |
|
|
|
|
|
|
|
|
import hashlib |
|
|
|
|
|
from odoo import models, _ |
|
|
|
|
|
|
|
|
ACCOUNT = "Compte donneur d'ordre" |
|
|
ACCOUNT = "Compte donneur d'ordre" |
|
|
CURRENCY = "Devise" |
|
|
CURRENCY = "Devise" |
|
@ -18,7 +11,7 @@ AMOUNT = "Montant" |
|
|
COUNTERPART_NUMBER = "Compte contrepartie" |
|
|
COUNTERPART_NUMBER = "Compte contrepartie" |
|
|
COUNTERPART_NAME = "Contrepartie" |
|
|
COUNTERPART_NAME = "Contrepartie" |
|
|
COMMUNICATION = "Communication" |
|
|
COMMUNICATION = "Communication" |
|
|
TRANSACTION_TYPE = "Type d'op\xc3\xa9ration" |
|
|
|
|
|
|
|
|
TRANSACTION_TYPE = "Type d'opération" |
|
|
|
|
|
|
|
|
class CodaBankStatementImport(models.TransientModel): |
|
|
class CodaBankStatementImport(models.TransientModel): |
|
|
_inherit = 'account.bank.statement.import' |
|
|
_inherit = 'account.bank.statement.import' |
|
@ -29,7 +22,8 @@ class CodaBankStatementImport(models.TransientModel): |
|
|
_csv_delimiter = ";" |
|
|
_csv_delimiter = ";" |
|
|
_csv_quote = '"' |
|
|
_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): |
|
|
def _generate_note_crelan(self, move): |
|
@ -45,11 +39,12 @@ class CodaBankStatementImport(models.TransientModel): |
|
|
'note': self._generate_note_crelan(move), |
|
|
'note': self._generate_note_crelan(move), |
|
|
'date': self._to_iso_date(move[DATE]), |
|
|
'date': self._to_iso_date(move[DATE]), |
|
|
'amount': float(move[AMOUNT]), |
|
|
'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], |
|
|
'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 |
|
|
return move_data |
|
|
|
|
|
|
|
@ -57,8 +52,8 @@ class CodaBankStatementImport(models.TransientModel): |
|
|
statement_data = { |
|
|
statement_data = { |
|
|
'name': _("Bank Statement from %s to %s") % (begin_date, end_date), |
|
|
'name': _("Bank Statement from %s to %s") % (begin_date, end_date), |
|
|
'date': self._to_iso_date(end_date), |
|
|
'date': self._to_iso_date(end_date), |
|
|
'balance_start': balance_start, #ok |
|
|
|
|
|
'balance_end_real' : balance_end, #ok |
|
|
|
|
|
|
|
|
'balance_start': balance_start, # Ok |
|
|
|
|
|
'balance_end_real' : balance_end, # Ok |
|
|
'transactions' : [] |
|
|
'transactions' : [] |
|
|
} |
|
|
} |
|
|
return statement_data |
|
|
return statement_data |
|
@ -66,7 +61,7 @@ class CodaBankStatementImport(models.TransientModel): |
|
|
def _get_acc_number_crelan(self, acc_number): |
|
|
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)]) |
|
|
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 acc_number |
|
|
|
|
|
|
|
|
return journal.bank_acc_number |
|
|
return journal.bank_acc_number |
|
@ -76,13 +71,14 @@ class CodaBankStatementImport(models.TransientModel): |
|
|
return self.init_balance |
|
|
return self.init_balance |
|
|
|
|
|
|
|
|
journal = self.env['account.journal'].search([('bank_acc_number', '=like', '%' + 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 |
|
|
|
|
|
|
|
|
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 |
|
|
self.init_balance = 0.0 |
|
|
else: |
|
|
else: |
|
|
lang = self._context.get('lang', 'en_US') |
|
|
lang = self._context.get('lang', 'en_US') |
|
|
l = self.env['res.lang'].search([('code', '=', lang)]) |
|
|
l = self.env['res.lang'].search([('code', '=', lang)]) |
|
|
balance = journal.get_journal_dashboard_datas()['last_balance'][:-1] |
|
|
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 |
|
|
return self.init_balance |
|
|
|
|
|
|
|
|
def _to_iso_date(self, orig_date): |
|
|
def _to_iso_date(self, orig_date): |
|
@ -92,7 +88,7 @@ class CodaBankStatementImport(models.TransientModel): |
|
|
def _parse_file(self, data_file): |
|
|
def _parse_file(self, data_file): |
|
|
|
|
|
|
|
|
try: |
|
|
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) |
|
|
data = csv.DictReader(csv_file, delimiter=self._csv_delimiter, quotechar=self._csv_quote) |
|
|
if not data.fieldnames == self._header: |
|
|
if not data.fieldnames == self._header: |
|
|
raise ValueError() |
|
|
raise ValueError() |
|
|