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

88 lines
3.4 KiB
Python

# -*- 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.multi
def import_file(self):
""" Check filetype before loading data """
self.ensure_one()
file_type = guess_type(self.filename)[0]
if file_type != 'text/csv':
raise ValidationError(_('Invalid filetype, expected CSV'))
return super(AccountBankStatementImport, self).import_file()
@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 [line for line in csv_data]
@api.model
def _get_coop_transaction(self, account_number, line):
""" Prepare transaction line """
return {'name': line[1],
'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],
'account_number': account_number,
'note': line[5],
'ref': line[2]}
@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 """
data = self._load_ccop_csv(data_file)
currency, account_number, bank_statement_data = self._get_ccop_processed(data)
return currency, account_number, [bank_statement_data]