From 6efd9d53388e3337144ed587faa26a83380651d8 Mon Sep 17 00:00:00 2001 From: Thibault Francois Date: Sun, 9 Oct 2016 16:04:16 +0200 Subject: [PATCH] [ADD] crelan import module --- beesdoo_crelan_csv/__init__.py | 2 + beesdoo_crelan_csv/__openerp__.py | 22 ++++ beesdoo_crelan_csv/wizard/__init__.py | 6 + .../wizard/import_crelan_csv.py | 120 ++++++++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 beesdoo_crelan_csv/__init__.py create mode 100644 beesdoo_crelan_csv/__openerp__.py create mode 100644 beesdoo_crelan_csv/wizard/__init__.py create mode 100644 beesdoo_crelan_csv/wizard/import_crelan_csv.py diff --git a/beesdoo_crelan_csv/__init__.py b/beesdoo_crelan_csv/__init__.py new file mode 100644 index 0000000..56c29d9 --- /dev/null +++ b/beesdoo_crelan_csv/__init__.py @@ -0,0 +1,2 @@ +# -*- coding: utf-8 -*- +import wizard diff --git a/beesdoo_crelan_csv/__openerp__.py b/beesdoo_crelan_csv/__openerp__.py new file mode 100644 index 0000000..3a252a3 --- /dev/null +++ b/beesdoo_crelan_csv/__openerp__.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +{ + 'name': "Beescoop Coda Import module", + + 'summary': """ + Import Crelan CSV Wizard + """, + + 'description': """ + """, + + 'author': "Beescoop - Cellule IT", + 'website': "https://github.com/beescoop/Obeesdoo", + + 'category': 'Accounting & Finance', + 'version': '0.1', + + 'depends': ['account_bank_statement_import'], + + 'data': [ + ], +} diff --git a/beesdoo_crelan_csv/wizard/__init__.py b/beesdoo_crelan_csv/wizard/__init__.py new file mode 100644 index 0000000..bd91c43 --- /dev/null +++ b/beesdoo_crelan_csv/wizard/__init__.py @@ -0,0 +1,6 @@ +''' +Created on 09 octobre 2016 + +@author: Thibault Francois +''' +import import_crelan_csv \ No newline at end of file diff --git a/beesdoo_crelan_csv/wizard/import_crelan_csv.py b/beesdoo_crelan_csv/wizard/import_crelan_csv.py new file mode 100644 index 0000000..e89bfca --- /dev/null +++ b/beesdoo_crelan_csv/wizard/import_crelan_csv.py @@ -0,0 +1,120 @@ +# -*- coding: utf-8 -*- +''' +Created on 09 Octobre 2016 + +@author: Thibault Francois (thibault@françois.be) +''' + +from StringIO import StringIO +import csv +import datetime +from openerp import models, _ + +ACCOUNT = "Compte donneur d'ordre" +CURRENCY = "Devise" +DATE = "Date" +AMOUNT = "Montant" +COUNTERPART_NUMBER = "Compte contrepartie" +COUNTERPART_NAME = "Contrepartie" +COMMUNICATION = "Communication" +TRANSACTION_TYPE = "Type d'op\xc3\xa9ration" + +class CodaBankStatementImport(models.TransientModel): + _inherit = 'account.bank.statement.import' + + _date_format = "%d/%m/%Y" + + _decimal_sep = "." + _csv_delimiter = ";" + _csv_quote = '"' + + _header = ['Date', 'Montant', 'Devise', 'Contrepartie', 'Compte contrepartie', "Type d'op\xc3\xa9ration", 'Communication', "Compte donneur d'ordre"] + + + def _generate_note(self, move): + notes = [] + notes.append("%s: %s" % (_('Counter Party Name'), move[COUNTERPART_NAME])) + notes.append("%s: %s" % (_('Counter Party Account'), move[COUNTERPART_NUMBER])) + notes.append("%s: %s" % (_('Communication'), move[COMMUNICATION])) + return '\n'.join(notes) + + def _get_move_value(self, move, sequence): + move_data = { + 'name': move[TRANSACTION_TYPE] + ": " + move[COMMUNICATION], + 'note': self._generate_note(move), + 'date': self._to_iso_date(move[DATE]), + 'amount': float(move[AMOUNT]), + '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] + } + return move_data + + def _get_statement_data(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 + 'transactions' : [] + } + return statement_data + + def _get_acc_number(self, 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 + return acc_number + + return journal.bank_acc_number + + def _get_acc_balance(self, acc_number): + if not self.init_balance == None: + return self.init_balance + print "compute 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 + self.init_balance = 0.0 + else: + self.init_balance = float(journal.get_journal_dashboard_datas()['last_balance'][:-1].strip().replace(',', '')) + + return self.init_balance + + def _to_iso_date(self, orig_date): + date_obj = datetime.datetime.strptime(orig_date, self._date_format) + return date_obj.strftime('%Y-%m-%d') + + def _parse_file(self, data_file): + + try: + csv_file = StringIO(data_file) + data = csv.DictReader(csv_file, delimiter=self._csv_delimiter, quotechar=self._csv_quote) + if not data.fieldnames == self._header: + raise ValueError() + except ValueError: + return super(CodaBankStatementImport, self)._parse_file(data_file) + + currency_code = False + account_number = False + self.init_balance = None + begin_date = False + end_date = False + + transactions = [] + i = 1 + sum_transaction = 0 + for statement in data: + begin_date = begin_date or statement[DATE] + end_date = statement[DATE] + account_number = statement[ACCOUNT] + balance = self._get_acc_balance(account_number) + currency_code = statement[CURRENCY] + transactions.append(self._get_move_value(statement, i)) + sum_transaction += float(statement[AMOUNT]) + i += 1 + stmt = self._get_statement_data(balance, balance+ sum_transaction, begin_date, end_date) + stmt['transactions'] = transactions + return currency_code, self._get_acc_number(account_number), [stmt]