66 lines
2.1 KiB
Python
66 lines
2.1 KiB
Python
import os, uuid
|
|
from datetime import datetime
|
|
from flask import Blueprint, request, jsonify, send_file, render_template, current_app
|
|
from werkzeug.utils import secure_filename
|
|
|
|
from common import load_gl_dump
|
|
from .logic import build_gl_only_report
|
|
|
|
donor_report_gl_bp = Blueprint("donor_report_gl", __name__, url_prefix="/donor-report-gl")
|
|
|
|
|
|
@donor_report_gl_bp.route("/")
|
|
def index():
|
|
return render_template("donor_report_gl/index.html")
|
|
|
|
|
|
@donor_report_gl_bp.route("/api/process", methods=["POST"])
|
|
def api_process():
|
|
f = request.files.get("gl_file")
|
|
if not f or not f.filename:
|
|
return jsonify({"error": "No GL Dump uploaded"}), 400
|
|
|
|
path = os.path.join(
|
|
current_app.config["UPLOAD_FOLDER"],
|
|
f"{uuid.uuid4()}_{secure_filename(f.filename)}",
|
|
)
|
|
f.save(path)
|
|
try:
|
|
gl_df = load_gl_dump(path)
|
|
finally:
|
|
os.remove(path)
|
|
|
|
if gl_df is None:
|
|
return jsonify({
|
|
"error": f"Could not recognize {f.filename} as a GL Dump export "
|
|
f"(no row with both \"Journal\" and \"Ledger Account\" found).",
|
|
}), 400
|
|
|
|
fname = f"Donor_Revenue_Report_GL_{datetime.today().strftime('%Y-%m-%d')}.xlsx"
|
|
fpath = os.path.join(current_app.config["OUTPUT_FOLDER"], fname)
|
|
donor_count, total_revenue, total_expense = build_gl_only_report(gl_df, fpath)
|
|
|
|
if donor_count == 0:
|
|
return jsonify({
|
|
"error": "No revenue or expense activity found in this GL Dump for the "
|
|
"donor report's account ranges.",
|
|
}), 400
|
|
|
|
return jsonify({
|
|
"status": "ok",
|
|
"filename": fname,
|
|
"donors": donor_count,
|
|
"total_revenue": total_revenue,
|
|
"total_expense": total_expense,
|
|
"surplus_deficit": round(total_revenue - total_expense, 2),
|
|
}), 200
|
|
|
|
|
|
@donor_report_gl_bp.route("/api/download/<filename>")
|
|
def api_download(filename):
|
|
safe = secure_filename(filename)
|
|
fpath = os.path.join(current_app.config["OUTPUT_FOLDER"], safe)
|
|
if not os.path.exists(fpath):
|
|
return jsonify({"error": "File not found"}), 404
|
|
return send_file(fpath, as_attachment=True)
|