gpc-generator/gpc.py
2025-04-02 07:52:05 +02:00

78 lines
3.2 KiB
Python

from datetime import datetime
from enum import Enum
class RecordType(str, Enum):
HEADER = "074"
ITEM = "075"
class TransactionCode(int, Enum):
DEBET = 1 # 1 = položka debet
CREDIT = 2 # 2 = položka kredit
STORNO_DEBET = 4 # 4 = storno položky debet
STORNO_CREDIT = 5 # 5 = storno položky kredit
CURRENCIES_GPC = {
"AUD": "0036", "CAD": "0124", "CNY": "0156", "CZK": "0203", "DKK": "0208",
"EUR": "0978", "GBP": "0826", "HRK": "0191", "HUF": "0348", "CHF": "0756",
"JPY": "0392", "NOK": "0578", "PLN": "0985", "RON": "0946", "RUB": "0643",
"SEK": "0752", "TRY": "0949", "USD": "0840"
}
class BaseRecord:
def __init__(self, record_type: RecordType, account: int):
self.record_type = record_type
self.account = account
class Header(BaseRecord):
def __init__(self, account: int, account_name: str, old_date: datetime, old_balance: int,
old_sign: str, new_balance: int, new_sign: str, turnover_debet: int,
turnover_debet_sign: str, turnover_credit: int, turnover_credit_sign: str,
transaction_list_no: int, date: datetime):
super().__init__(RecordType.HEADER, account)
self.account_name = account_name[:20].ljust(20)
self.old_date = old_date.strftime("%d%m%y")
self.old_balance = old_balance
self.old_sign = old_sign
self.new_balance = new_balance
self.new_sign = new_sign
self.turnover_debet = turnover_debet
self.turnover_debet_sign = turnover_debet_sign
self.turnover_credit = turnover_credit
self.turnover_credit_sign = turnover_credit_sign
self.transaction_list_no = transaction_list_no
self.date = date.strftime("%d%m%y")
def to_string(self):
return (f"074{str(self.account)[:16].zfill(16)}{str(self.account_name)[:20].zfill(20)}"
f"{self.old_date}{self.old_balance:014}{self.old_sign}"
f"{self.new_balance:014}{self.new_sign}"
f"{int(self.turnover_debet):014}{self.turnover_debet_sign}"
f"{int(self.turnover_credit):014}{self.turnover_credit_sign}"
f"{self.transaction_list_no:03}{self.date}{' '.ljust(14)}\r\n")
class Data(BaseRecord):
def __init__(self, account: int, payer_account: int, no: int, balance: float, code: TransactionCode,
variable: int, constant_symbol: int, bank_code: int, specific_symbol: int,
client_name: str, currency: str, date: datetime):
super().__init__(RecordType.ITEM, account)
self.payer_account = payer_account
self.no = no
self.balance = abs(balance)
self.code = code
self.variable = variable
self.constant_symbol = constant_symbol
self.bank_code = bank_code
self.specific_symbol = specific_symbol
self.client_name = client_name
self.currency = currency
self.date = date.strftime("%d%m%y")
def to_string(self):
return (f"{int(self.record_type.value):03}{str(self.account)[:16].zfill(16)}{str(self.payer_account).rjust(16, '0')[:16]}"
f"{str(self.no)[:13].zfill(13)}{int(self.balance):012}{self.code.value:1}{self.variable:010}"
f"{self.constant_symbol:010}{self.specific_symbol:010}"
f"{'0'*6}{str(self.client_name)[:20].ljust(20)}{'0'}{self.currency}{self.date}\r\n")