2018-09-12 15:29:15 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 2018 Fabien Bourgeois <fabien@yaltik.com>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
""" Bank statement import : CSV from Credit Coop """
|
|
|
|
|
|
|
|
import time
|
|
|
|
from mimetypes import guess_type
|
|
|
|
import csv
|
|
|
|
from odoo import models, api, _
|
|
|
|
from odoo.exceptions import ValidationError
|
|
|
|
|
|
|
|
def str2float(string):
|
|
|
|
""" Transform CSV string to Python float """
|
|
|
|
return float(string.replace(',', '.'))
|
|
|
|
|
|
|
|
|
|
|
|
class AccountBankStatementImport(models.TransientModel):
|
|
|
|
""" Bank statement import : CSV from Credit Coop """
|
|
|
|
_inherit = 'account.bank.statement.import'
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def _load_ccop_csv(self, data_file):
|
|
|
|
""" Parses and load CSV from CCoop """
|
|
|
|
csv_data = csv.reader(data_file.split('\n'), delimiter=';')
|
2018-09-12 17:25:05 +02:00
|
|
|
return [[cell.decode('iso-8859-15').encode('utf8') for cell in line]
|
|
|
|
for line in csv_data]
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def _check_load_ccoop(self, data_file):
|
|
|
|
""" Check filetype and CCoop before loading data """
|
|
|
|
file_type = guess_type(self.filename)[0]
|
|
|
|
if file_type != 'text/csv':
|
|
|
|
return False
|
|
|
|
data = self._load_ccop_csv(data_file)
|
|
|
|
if not data[0][0].startswith('Code de la banque : '):
|
|
|
|
return False
|
|
|
|
return data
|
2018-09-12 15:29:15 +02:00
|
|
|
|
|
|
|
@api.model
|
|
|
|
def _get_coop_transaction(self, account_number, line):
|
|
|
|
""" Prepare transaction line """
|
2018-09-21 16:25:02 +02:00
|
|
|
return {'name': line[2] or u'/',
|
2018-09-12 15:29:15 +02:00
|
|
|
'date': time.strftime('%Y-%m-%d',
|
|
|
|
time.strptime(line[0], '%d/%m/%y')),
|
|
|
|
'amount': str2float(line[3] or line[4]),
|
|
|
|
'unique_import_id': line[1],
|
|
|
|
'note': line[5],
|
2018-09-21 16:25:02 +02:00
|
|
|
'ref': line[1]}
|
2018-09-12 15:29:15 +02:00
|
|
|
|
|
|
|
@api.model
|
|
|
|
def _get_ccop_processed(self, data):
|
|
|
|
""" Prepare and return CCoop transaction """
|
|
|
|
currency = data[1][2].split(' : ')[1]
|
|
|
|
account_number = data[1][0].split(' : ')[1]
|
|
|
|
if not len(data[-1]): # If last line is empty, remove it
|
|
|
|
data.pop()
|
|
|
|
bank_statement_data = {
|
|
|
|
'date': time.strftime(
|
|
|
|
'%Y-%m-%d', time.strptime(data[0][3].split(' : ')[1], '%d/%m/%Y')),
|
|
|
|
'balance_start': str2float(data[-1][-1]),
|
|
|
|
'balance_end_real': str2float(data[3][-1])
|
|
|
|
}
|
|
|
|
bank_statement_data['name'] = '%s - %s' % (account_number,
|
|
|
|
bank_statement_data['date'])
|
|
|
|
bank_statement_data['transactions'] = [
|
|
|
|
self._get_coop_transaction(account_number, l) for l in data[5:-1]
|
|
|
|
]
|
|
|
|
return currency, account_number, bank_statement_data
|
|
|
|
|
|
|
|
|
|
|
|
def _parse_file(self, data_file):
|
|
|
|
""" Implements parse_file from parent, returning the required triplet """
|
2018-09-12 17:25:05 +02:00
|
|
|
data = self._check_load_ccoop(data_file)
|
|
|
|
if not data:
|
|
|
|
return super(AccountBankStatementImport, self)._parse_file(data_file)
|
2018-09-12 15:29:15 +02:00
|
|
|
currency, account_number, bank_statement_data = self._get_ccop_processed(data)
|
|
|
|
return currency, account_number, [bank_statement_data]
|