200 lines
7.2 KiB
Python
200 lines
7.2 KiB
Python
import csv
|
|
import pandas as pd
|
|
import chardet
|
|
from datetime import datetime
|
|
from gpc import Data, Header, TransactionCode, CURRENCIES_GPC
|
|
from utils import extract_order_number, extract_numbers, parse_date
|
|
|
|
|
|
def load_bank_transactions(csv_file):
|
|
"""
|
|
Loads the bank transactions CSV file into a DataFrame and returns it.
|
|
|
|
:param csv_file: Path to the bank transactions CSV file.
|
|
:return: A pandas DataFrame containing the transactions.
|
|
"""
|
|
df = pd.read_csv(csv_file, delimiter=';', dtype=str)
|
|
|
|
# Ensure the required column exists
|
|
if 'Zpráva pro příjemce' not in df.columns:
|
|
raise ValueError("The CSV file does not contain the required column 'Zpráva pro příjemce'.")
|
|
|
|
return df
|
|
|
|
def search_bank_transaction(df, search_string):
|
|
"""
|
|
Searches for a given string in the 'Zpráva pro příjemce' column of the loaded DataFrame.
|
|
|
|
:param df: Pandas DataFrame containing bank transactions.
|
|
:param search_string: String to search for in the 'Zpráva pro příjemce' column.
|
|
:return: The first matching row as a dictionary or None if not found.
|
|
"""
|
|
matching_row = df[df['Zpráva pro příjemce'].str.contains(search_string, na=False, case=False)]
|
|
|
|
return matching_row.iloc[0].to_dict() if not matching_row.empty else None
|
|
|
|
|
|
def convert_csv_to_gpc(csv_file_path, gpc_file_path, account_number, currency, mapping):
|
|
gpc_lines = []
|
|
global transactions_df
|
|
|
|
|
|
if mapping['forced_encoding'] is not None:
|
|
detected_encoding = mapping['forced_encoding']
|
|
print(f"Forced encoding: {detected_encoding}")
|
|
else:
|
|
with open(csv_file_path, 'rb') as f:
|
|
rawdata = f.read(1024) # Read a small part of the file
|
|
result = chardet.detect(rawdata)
|
|
detected_encoding = result['encoding']
|
|
print(f"Detected encoding: {detected_encoding}")
|
|
|
|
|
|
with open(csv_file_path, mode='r', encoding=detected_encoding) as csv_file:
|
|
reader = csv.DictReader(csv_file, delimiter=mapping['delimiter']) if mapping['is_dict_mapping'] else csv.reader(csv_file, delimiter=mapping['delimiter'])
|
|
|
|
if not mapping['is_dict_mapping']:
|
|
next(reader)
|
|
|
|
total_payout = 0.0
|
|
first = True
|
|
clearing_id = ""
|
|
for row in reader:
|
|
|
|
if first:
|
|
clearing_id = row[mapping['CLEARING_ID']]
|
|
first = False
|
|
reference = extract_order_number(row[mapping['reference']])
|
|
transaction_id = extract_numbers(row[mapping['transaction_id']]) if mapping['use_transaction_id'] else reference
|
|
direction = row[mapping['direction']].lower() if mapping['direction'] is not None else None
|
|
source_name = row[mapping['source_name']].replace("Nazwa nadawcy: ", "")[:20].ljust(20) if mapping['use_source_name'] else ""
|
|
payer_account = extract_numbers(row[mapping['payer_account']].replace("Rachunek nadawcy: ", "").replace(" ", ""))[:16].ljust(16) if mapping['payer_number_exists'] else 0
|
|
if payer_account != 0:
|
|
if payer_account.strip() == "":
|
|
payer_account = 0
|
|
source_amount = float(row[mapping['source_amount']].replace(',', '.')) * 100 # Convert to cents
|
|
created_on = parse_date(row[mapping['created_on']])
|
|
|
|
# Determine transaction type
|
|
if(direction is None):
|
|
if source_amount > 0:
|
|
transaction_code = TransactionCode.CREDIT
|
|
else:
|
|
transaction_code = TransactionCode.DEBET
|
|
else:
|
|
if direction == "out":
|
|
transaction_code = TransactionCode.DEBET
|
|
else:
|
|
transaction_code = TransactionCode.CREDIT
|
|
|
|
# Convert currency
|
|
currency_code = CURRENCIES_GPC.get("CZK", "0000")
|
|
|
|
# Create GPC Data object
|
|
gpc_data = Data(
|
|
account=account_number,
|
|
payer_account=payer_account,
|
|
no=transaction_id,
|
|
balance=source_amount,
|
|
code=transaction_code,
|
|
variable=int(reference) if reference.isdigit() else 0,
|
|
constant_symbol=0,
|
|
bank_code=0,
|
|
specific_symbol=0,
|
|
client_name=source_name,
|
|
currency=currency_code,
|
|
date=created_on
|
|
)
|
|
|
|
gpc_lines.append(gpc_data.to_string())
|
|
total_payout += source_amount
|
|
|
|
# add fees
|
|
# fees_data = Data(
|
|
# account=account_number,
|
|
# payer_account=0,
|
|
# no=0,
|
|
# balance=total_fees,
|
|
# code=transaction_code,
|
|
# variable=0,
|
|
# constant_symbol=0,
|
|
# bank_code=0,
|
|
# specific_symbol=0,
|
|
# client_name="",
|
|
# currency=CURRENCIES_GPC.get("CZK", "0000"),
|
|
# date=created_on
|
|
# )
|
|
#
|
|
# gpc_lines.append(gpc_data.to_string())
|
|
|
|
corresponding_transaction = search_bank_transaction(transactions_df, clearing_id)
|
|
|
|
# vyuctovani row
|
|
payout_data = Data(
|
|
account=account_number,
|
|
payer_account=0,
|
|
no=0,
|
|
balance=total_payout,
|
|
code=TransactionCode.DEBET,
|
|
variable=666111222,
|
|
# variable=corresponding_transaction['Zpráva pro příjemce'].split(',')[-1].strip(),
|
|
constant_symbol=0,
|
|
bank_code=0,
|
|
specific_symbol=0,
|
|
client_name="",
|
|
currency=CURRENCIES_GPC.get("CZK", "0000"),
|
|
date=parse_date(corresponding_transaction['Datum'])
|
|
)
|
|
|
|
gpc_lines.append(payout_data.to_string())
|
|
|
|
# Create and add the header
|
|
header = Header(
|
|
account=account_number,
|
|
account_name=currency.ljust(20),
|
|
old_date=datetime.strptime("01-03-20", "%d-%m-%y"),
|
|
old_balance=0,
|
|
old_sign='+',
|
|
new_balance=0,
|
|
new_sign='+',
|
|
turnover_debet=total_payout,
|
|
turnover_debet_sign='0',
|
|
turnover_credit=total_payout,
|
|
turnover_credit_sign='0',
|
|
transaction_list_no=3,
|
|
date=datetime.now()
|
|
)
|
|
gpc_lines.insert(0, header.to_string())
|
|
|
|
with open(gpc_file_path, mode='w', encoding='utf-8') as gpc_file:
|
|
gpc_file.writelines(gpc_lines)
|
|
|
|
print(f"GPC file successfully created: {gpc_file_path}")
|
|
|
|
# Example mappings
|
|
mapping_paynl = {
|
|
'transaction_id': 'PAYMENT_SESSION_ID',
|
|
'reference': 'EXTRA_1',
|
|
'direction': None,
|
|
'source_name': 'CONSUMER_NAME',
|
|
'source_amount': 'TURNOVER_TOTAL',
|
|
'source_currency': '',
|
|
'payer_account': 'PAYMENT_SESSION_ID',
|
|
'created_on': 'TRANSACTION_DATE',
|
|
'CLEARING_ID': 'CLEARING_ID',
|
|
'fees': 'COSTS',
|
|
'delimiter': ";",
|
|
'is_dict_mapping': True,
|
|
'use_transaction_id': True,
|
|
'payer_number_exists': False,
|
|
'use_source_name': False,
|
|
'forced_encoding': None
|
|
}
|
|
|
|
|
|
transactions_df = load_bank_transactions("bank_statement.csv")
|
|
# Example usage:
|
|
convert_csv_to_gpc("Specification clearing 2024-05-10.csv", "avizo_paynl_test.gpc", account_number=2801379531, currency="EUR", mapping=mapping_paynl)
|
|
# convert_csv_to_gpc("pko_input.csv", "pko_output.gpc", account_number=95102013900000630206821286, currency="PLN", mapping=mapping_pko)
|
|
# convert_csv_to_gpc("sparkasse_input.csv", "sparkasse_output.gpc", account_number=95850503000221267034, currency="EUR", mapping=mapping_sparkasse)
|