Spaces:
Sleeping
Sleeping
Upload server.py
Browse files
server.py
CHANGED
|
@@ -872,92 +872,85 @@ def api_invoice_detail(invoice_id: str) -> Any:
|
|
| 872 |
|
| 873 |
@app.route("/api/invoices/summary", methods=["GET"])
|
| 874 |
def api_invoice_summary() -> Any:
|
| 875 |
-
try:
|
| 876 |
-
login_key = require_auth()
|
| 877 |
-
except PermissionError:
|
| 878 |
-
return jsonify({"error": "Brak autoryzacji."}), 401
|
| 879 |
-
|
| 880 |
-
now = datetime.utcnow()
|
| 881 |
-
last_month_start = now - timedelta(days=30)
|
| 882 |
-
quarter_first_month = ((now.month - 1) // 3) * 3 + 1
|
| 883 |
-
quarter_start = now.replace(month=quarter_first_month, day=1, hour=0, minute=0, second=0, microsecond=0)
|
| 884 |
-
year_start = now.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0)
|
| 885 |
-
|
| 886 |
-
def
|
| 887 |
-
|
| 888 |
-
|
| 889 |
-
|
| 890 |
-
|
| 891 |
-
if
|
| 892 |
-
|
| 893 |
-
|
| 894 |
-
|
| 895 |
-
|
| 896 |
-
|
| 897 |
-
|
| 898 |
-
|
| 899 |
-
|
| 900 |
-
|
| 901 |
-
|
| 902 |
-
|
| 903 |
-
|
| 904 |
-
|
| 905 |
-
|
| 906 |
-
|
| 907 |
-
|
| 908 |
-
|
| 909 |
-
|
| 910 |
-
|
| 911 |
-
|
| 912 |
-
|
| 913 |
-
|
| 914 |
-
|
| 915 |
-
|
| 916 |
-
|
| 917 |
-
|
| 918 |
-
|
| 919 |
-
|
| 920 |
-
"
|
| 921 |
-
|
| 922 |
-
|
| 923 |
-
|
| 924 |
-
|
| 925 |
-
|
| 926 |
-
|
| 927 |
-
|
| 928 |
-
|
| 929 |
-
|
| 930 |
-
|
| 931 |
-
|
| 932 |
-
|
| 933 |
-
|
| 934 |
-
|
| 935 |
-
|
| 936 |
-
|
| 937 |
-
|
| 938 |
-
|
| 939 |
-
|
| 940 |
-
|
| 941 |
-
|
| 942 |
-
|
| 943 |
-
|
| 944 |
-
|
| 945 |
-
|
| 946 |
-
|
| 947 |
-
|
| 948 |
-
|
| 949 |
-
|
| 950 |
-
|
| 951 |
-
|
| 952 |
-
|
| 953 |
-
|
| 954 |
-
|
| 955 |
-
summary = {
|
| 956 |
-
"last_month": aggregate(last_month_start),
|
| 957 |
-
"quarter": aggregate(quarter_start),
|
| 958 |
-
"year": aggregate(year_start),
|
| 959 |
-
}
|
| 960 |
-
return jsonify({"summary": summary})
|
| 961 |
|
| 962 |
|
| 963 |
if __name__ == "__main__":
|
|
|
|
| 872 |
|
| 873 |
@app.route("/api/invoices/summary", methods=["GET"])
|
| 874 |
def api_invoice_summary() -> Any:
|
| 875 |
+
try:
|
| 876 |
+
login_key = require_auth()
|
| 877 |
+
except PermissionError:
|
| 878 |
+
return jsonify({"error": "Brak autoryzacji."}), 401
|
| 879 |
+
|
| 880 |
+
now = datetime.utcnow()
|
| 881 |
+
last_month_start = now - timedelta(days=30)
|
| 882 |
+
quarter_first_month = ((now.month - 1) // 3) * 3 + 1
|
| 883 |
+
quarter_start = now.replace(month=quarter_first_month, day=1, hour=0, minute=0, second=0, microsecond=0)
|
| 884 |
+
year_start = now.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0)
|
| 885 |
+
|
| 886 |
+
def normalize_issued_at(value: Any) -> Optional[datetime]:
|
| 887 |
+
if isinstance(value, datetime):
|
| 888 |
+
return value
|
| 889 |
+
if isinstance(value, str):
|
| 890 |
+
candidate = value.strip()
|
| 891 |
+
if not candidate:
|
| 892 |
+
return None
|
| 893 |
+
for fmt in ("%Y-%m-%d %H:%M", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%d"):
|
| 894 |
+
try:
|
| 895 |
+
return datetime.strptime(candidate, fmt)
|
| 896 |
+
except ValueError:
|
| 897 |
+
continue
|
| 898 |
+
return None
|
| 899 |
+
|
| 900 |
+
def aggregate_from_rows(rows: List[Dict[str, Any]], start: datetime) -> Dict[str, Any]:
|
| 901 |
+
count = 0
|
| 902 |
+
gross_total = Decimal("0.00")
|
| 903 |
+
for row in rows:
|
| 904 |
+
issued_dt = normalize_issued_at(row.get("issued_at"))
|
| 905 |
+
if issued_dt is None or issued_dt < start:
|
| 906 |
+
continue
|
| 907 |
+
try:
|
| 908 |
+
gross_total += _decimal(row.get("total_gross") or "0")
|
| 909 |
+
except ValueError:
|
| 910 |
+
continue
|
| 911 |
+
count += 1
|
| 912 |
+
return {"count": count, "gross_total": str(_quantize(gross_total))}
|
| 913 |
+
|
| 914 |
+
if DATABASE_AVAILABLE:
|
| 915 |
+
try:
|
| 916 |
+
account_row = get_account_row(login_key)
|
| 917 |
+
except KeyError:
|
| 918 |
+
return jsonify({"error": "Nie znaleziono konta."}), 404
|
| 919 |
+
rows = fetch_all(
|
| 920 |
+
"""
|
| 921 |
+
SELECT issued_at, total_gross
|
| 922 |
+
FROM invoices
|
| 923 |
+
WHERE account_id = %s
|
| 924 |
+
""",
|
| 925 |
+
(account_row["id"],),
|
| 926 |
+
)
|
| 927 |
+
summary = {
|
| 928 |
+
"last_month": aggregate_from_rows(rows, last_month_start),
|
| 929 |
+
"quarter": aggregate_from_rows(rows, quarter_start),
|
| 930 |
+
"year": aggregate_from_rows(rows, year_start),
|
| 931 |
+
}
|
| 932 |
+
return jsonify({"summary": summary})
|
| 933 |
+
|
| 934 |
+
data = load_store()
|
| 935 |
+
try:
|
| 936 |
+
account = get_account(data, login_key)
|
| 937 |
+
except KeyError:
|
| 938 |
+
return jsonify({"error": "Nie znaleziono konta."}), 404
|
| 939 |
+
invoices = account.get("invoices", [])
|
| 940 |
+
rows = [
|
| 941 |
+
{
|
| 942 |
+
"issued_at": invoice.get("issued_at"),
|
| 943 |
+
"total_gross": (invoice.get("totals") or {}).get("gross", "0"),
|
| 944 |
+
}
|
| 945 |
+
for invoice in invoices
|
| 946 |
+
]
|
| 947 |
+
|
| 948 |
+
summary = {
|
| 949 |
+
"last_month": aggregate_from_rows(rows, last_month_start),
|
| 950 |
+
"quarter": aggregate_from_rows(rows, quarter_start),
|
| 951 |
+
"year": aggregate_from_rows(rows, year_start),
|
| 952 |
+
}
|
| 953 |
+
return jsonify({"summary": summary})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 954 |
|
| 955 |
|
| 956 |
if __name__ == "__main__":
|