72 lines
2.8 KiB
Python
72 lines
2.8 KiB
Python
import pandas as pd
|
|
import csv
|
|
import sys
|
|
import os
|
|
import io
|
|
import zipfile
|
|
import shutil
|
|
from aviza.helpers import write_output_csv_to_zip
|
|
from utils import parse_date, extract_order_number
|
|
|
|
|
|
|
|
def extract_and_process_zip_przelewy_report_single(zip_file_path, output_file):
|
|
all_transformed_data = []
|
|
|
|
# Create a temporary folder for extraction
|
|
base_dir = os.path.dirname(zip_file_path)
|
|
extract_folder = os.path.join(base_dir, "extracted_temp")
|
|
os.makedirs(extract_folder, exist_ok=True)
|
|
|
|
# Extract the provided outer zip file
|
|
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
|
|
zip_ref.extractall(extract_folder)
|
|
|
|
# Check if a top-level "Transactions" folder exists
|
|
if os.path.exists(extract_folder):
|
|
# Look for a CSV file inside the Transactions folder
|
|
csv_filename = next(
|
|
(f for f in os.listdir(extract_folder)
|
|
if f.endswith(".csv")),
|
|
None
|
|
)
|
|
if csv_filename:
|
|
csv_path = os.path.join(extract_folder, csv_filename)
|
|
transformed_data = transform_csv(csv_path)
|
|
all_transformed_data.append(transformed_data)
|
|
|
|
# Clean up the outer extraction folder
|
|
shutil.rmtree(extract_folder)
|
|
print(f"Processed and cleaned up: {zip_file_path}")
|
|
|
|
# Return a ZIP archive (in memory) containing all the output CSV files.
|
|
return write_output_csv_to_zip(output_file, all_transformed_data)
|
|
|
|
def transform_csv(input_file):
|
|
df = pd.read_csv(input_file, delimiter=",", dtype=str, skiprows=3)
|
|
df.iloc[:, 11] = pd.to_numeric(df.iloc[:, 11], errors='coerce').fillna(0)
|
|
df["Commission_cumsum"] = df["Commission"].astype(float).cumsum().round(2)
|
|
df["Amount_cumsum"] = df["Amount"].astype(float).cumsum().round(2)
|
|
transformed_data = []
|
|
total_rows = len(df)
|
|
|
|
for index, row in df.iloc[:-1].iterrows():
|
|
amount = row.iloc[11]
|
|
typ_operace = "t" if amount >= 0 else "c"
|
|
orders_id = extract_order_number(row['Title'])
|
|
transformed_row = [
|
|
typ_operace, parse_date(row.iloc[12]).strftime("%Y-%m-%d"), row['Transaction text id'], amount, row['Balance after operation'], "TRUE", orders_id,
|
|
f"Dobirka za FA s VS {orders_id}", "", "", "", "", row['Client email'], row['Client email'], "", row["Commission"], "PLN"
|
|
]
|
|
transformed_data.append(transformed_row)
|
|
|
|
progress = (index + 1) / total_rows * 100
|
|
sys.stdout.write(f"\rProcessing: {progress:.2f}%")
|
|
sys.stdout.flush()
|
|
|
|
last_row = df.iloc[-1]
|
|
final_row = ["w", parse_date(last_row.iloc[12]).strftime("%Y-%m-%d"), last_row['Transaction text id'], last_row['Amount without commission'], 0, "TRUE", "", "Vyrovnání zůstatku", "95102013900000630206821286", "", "", "", "", "", "NEWLINE BREAK", "", "PLN"]
|
|
transformed_data.append(final_row)
|
|
|
|
return transformed_data
|