Files
tryton/modules/account_statement_aeb43/statement.py
T

103 lines
3.8 KiB
Python
Raw Normal View History

2018-06-29 16:08:42 +02:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from io import StringIO
from stdnum.es.ccc import calc_check_digits, to_iban
from csb43 import csb43
2018-12-29 14:20:29 +01:00
from trytond.i18n import gettext
2018-06-29 16:08:42 +02:00
from trytond.pool import Pool, PoolMeta
2018-12-29 14:20:29 +01:00
from trytond.modules.account_statement.exceptions import ImportStatementError
2018-06-29 16:08:42 +02:00
class ImportStatementStart(metaclass=PoolMeta):
__name__ = 'account.statement.import.start'
@classmethod
def __setup__(cls):
super(ImportStatementStart, cls).__setup__()
aeb43 = ('aeb43', 'AEB Norm 43')
cls.file_format.selection.append(aeb43)
class ImportStatement(metaclass=PoolMeta):
__name__ = 'account.statement.import'
2019-04-16 13:25:58 +02:00
def parse_aeb43(self, encoding='iso-8859-1'):
2018-06-29 16:08:42 +02:00
file_ = self.start.file_
if not isinstance(file_, str):
file_ = file_.decode(encoding)
aeb43 = csb43.File(StringIO(file_), strict=False)
for account in aeb43.accounts:
statement = self.aeb43_statement(account)
origins = []
for transaction in account.transactions:
origins.extend(self.aeb43_origin(statement, transaction))
statement.origins = origins
yield statement
def aeb43_statement(self, account):
pool = Pool()
Statement = pool.get('account.statement')
Journal = pool.get('account.statement.journal')
# Compute IBAN code from spanish format
bank_code = account.bankCode
branch_code = account.branchCode
account_number = account.accountNumber
check_digits = calc_check_digits(
bank_code + branch_code + '00' + account_number)
bank_account = to_iban(
bank_code + branch_code + check_digits + account_number)
journal = Journal.get_by_bank_account(self.start.company, bank_account)
if not journal:
2018-12-29 14:20:29 +01:00
raise ImportStatementError(
gettext('account_statement.msg_import_no_journal',
account=bank_account))
2018-06-29 16:08:42 +02:00
statement = Statement()
statement.name = '%(account)s@(%(start_date)s/%(end_date)s)' % {
'account': bank_account,
'start_date': account.initialDate,
'end_date': account.finalDate,
}
statement.company = self.start.company
statement.journal = journal
statement.start_balance = account.initialBalance
statement.end_balance = account.abstract.balance
statement.number_of_lines = (
account.abstract.incomeEntries + account.abstract.expenseEntries)
statement.total_amount = (
account.abstract.income - account.abstract.expense)
return statement
def aeb43_origin(self, statement, transaction):
pool = Pool()
Origin = pool.get('account.statement.origin')
origin = Origin()
try:
origin.date = transaction.valueDate
except ValueError:
origin.date = transaction.transactionDate
2018-06-29 16:08:42 +02:00
origin.amount = transaction.amount
descriptions = []
for item in transaction.optionalItems:
if item.item1:
descriptions.append(item.item1)
if item.item2:
descriptions.append(item.item2)
origin.description = ''.join(descriptions)
2019-02-06 22:34:13 +01:00
origin.information = self.aeb43_information(statement, transaction)
2018-06-29 16:08:42 +02:00
return [origin]
2019-02-06 22:34:13 +01:00
def aeb43_information(self, statement, transaction):
2018-06-29 16:08:42 +02:00
return {
'aeb43_operation_date': transaction.transactionDate,
'aeb43_record_type': transaction.sharedItem,
'aeb43_document_number': transaction.documentNumber,
'aeb43_first_reference': transaction.reference1,
'aeb43_second_reference': transaction.reference2,
}