yaltik_odoo_custom/account_bank_statement_impo.../wizard/account_bank_statement_impo...

103 lines
4.1 KiB
Python

# -*- coding: utf-8 -*-
# Copyright 2018-2020 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=';')
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
@api.model
def _get_coop_transaction(self, account_number, line):
""" Prepare transaction line """
# CCoop format change : old was YY, now YYYY
if len(line[0]) <= 8:
parsed_date = time.strptime(line[0], '%d/%m/%y')
else:
parsed_date = time.strptime(line[0], '%d/%m/%Y')
return {'name': line[2] or u'/',
'date': time.strftime('%Y-%m-%d', parsed_date),
'amount': str2float(line[3] or line[4]),
'unique_import_id': line[1],
'note': line[5],
'ref': line[1]}
@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()
# Change in CSV produced from CCoop, now empty last cell
balance_start = data[-1][-1]
balance_end_real = data[3][-1]
if balance_start == '':
balance_start = data[-1][-2]
if balance_end_real == '':
balance_end_real = data[3][-2]
bank_statement_data = {
'date': time.strftime(
'%Y-%m-%d', time.strptime(data[0][3].split(' : ')[1], '%d/%m/%Y')),
'balance_start': str2float(balance_start),
'balance_end_real': str2float(balance_end_real)
}
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 """
data = self._check_load_ccoop(data_file)
if not data:
return super(AccountBankStatementImport, self)._parse_file(data_file)
currency, account_number, bank_statement_data = self._get_ccop_processed(data)
return currency, account_number, [bank_statement_data]