Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
db.py
CHANGED
|
@@ -1,215 +1,245 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from contextlib import contextmanager
|
| 3 |
-
from typing import Any, Dict, List, Optional, Sequence
|
| 4 |
-
|
| 5 |
-
import psycopg2
|
| 6 |
-
from psycopg2.extras import RealDictCursor
|
| 7 |
-
|
| 8 |
-
DATABASE_URL = os.environ.get("NEON_DATABASE_URL")
|
| 9 |
-
|
| 10 |
-
if not DATABASE_URL:
|
| 11 |
-
raise RuntimeError(
|
| 12 |
-
"Brak zmiennej NEON_DATABASE_URL. Ustaw sekret w Hugging Face lub "
|
| 13 |
-
"ustaw zmienną środowiskową lokalnie."
|
| 14 |
-
)
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
@contextmanager
|
| 19 |
-
def db_conn():
|
| 20 |
-
conn = psycopg2.connect(DATABASE_URL, cursor_factory=RealDictCursor)
|
| 21 |
-
try:
|
| 22 |
-
yield conn
|
| 23 |
-
conn.commit()
|
| 24 |
-
except Exception:
|
| 25 |
-
conn.rollback()
|
| 26 |
-
raise
|
| 27 |
-
finally:
|
| 28 |
-
conn.close()
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
def fetch_one(query: str, params: Sequence[Any]) -> Optional[Dict[str, Any]]:
|
| 32 |
-
with db_conn() as conn, conn.cursor() as cur:
|
| 33 |
-
cur.execute(query, params)
|
| 34 |
-
return cur.fetchone()
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
def fetch_all(query: str, params: Sequence[Any] = ()) -> List[Dict[str, Any]]:
|
| 38 |
-
with db_conn() as conn, conn.cursor() as cur:
|
| 39 |
-
cur.execute(query, params)
|
| 40 |
-
return cur.fetchall()
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
def execute(query: str, params: Sequence[Any]) -> None:
|
| 44 |
-
with db_conn() as conn, conn.cursor() as cur:
|
| 45 |
-
cur.execute(query, params)
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
def create_account(login: str, email: str, password_hash: str) -> int:
|
| 49 |
-
with db_conn() as conn, conn.cursor() as cur:
|
| 50 |
-
cur.execute(
|
| 51 |
-
"""
|
| 52 |
-
INSERT INTO accounts (login, password_hash)
|
| 53 |
-
VALUES (%s, %s)
|
| 54 |
-
RETURNING id
|
| 55 |
-
""",
|
| 56 |
-
(login, password_hash),
|
| 57 |
-
)
|
| 58 |
-
account_id = cur.fetchone()["id"]
|
| 59 |
-
cur.execute(
|
| 60 |
-
"""
|
| 61 |
-
INSERT INTO business_profiles (account_id, company_name, owner_name,
|
| 62 |
-
address_line, postal_code, city, tax_id, bank_account)
|
| 63 |
-
VALUES (%s, '', '', '', '', '', '', '')
|
| 64 |
-
""",
|
| 65 |
-
(account_id,),
|
| 66 |
-
)
|
| 67 |
-
return account_id
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
def update_business(account_id: int, data: Dict[str, str]) -> None:
|
| 71 |
-
execute(
|
| 72 |
-
"""
|
| 73 |
-
UPDATE business_profiles
|
| 74 |
-
SET company_name = %s,
|
| 75 |
-
owner_name = %s,
|
| 76 |
-
address_line = %s,
|
| 77 |
-
postal_code = %s,
|
| 78 |
-
city = %s,
|
| 79 |
-
tax_id = %s,
|
| 80 |
-
bank_account = %s
|
| 81 |
-
WHERE account_id = %s
|
| 82 |
-
""",
|
| 83 |
-
(
|
| 84 |
-
data["company_name"],
|
| 85 |
-
data["owner_name"],
|
| 86 |
-
data["address_line"],
|
| 87 |
-
data["postal_code"],
|
| 88 |
-
data["city"],
|
| 89 |
-
data["tax_id"],
|
| 90 |
-
data["bank_account"],
|
| 91 |
-
account_id,
|
| 92 |
-
),
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
)
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from contextlib import contextmanager
|
| 3 |
+
from typing import Any, Dict, List, Optional, Sequence
|
| 4 |
+
|
| 5 |
+
import psycopg2
|
| 6 |
+
from psycopg2.extras import RealDictCursor
|
| 7 |
+
|
| 8 |
+
DATABASE_URL = os.environ.get("NEON_DATABASE_URL")
|
| 9 |
+
|
| 10 |
+
if not DATABASE_URL:
|
| 11 |
+
raise RuntimeError(
|
| 12 |
+
"Brak zmiennej NEON_DATABASE_URL. Ustaw sekret w Hugging Face lub "
|
| 13 |
+
"ustaw zmienną środowiskową lokalnie."
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@contextmanager
|
| 19 |
+
def db_conn():
|
| 20 |
+
conn = psycopg2.connect(DATABASE_URL, cursor_factory=RealDictCursor)
|
| 21 |
+
try:
|
| 22 |
+
yield conn
|
| 23 |
+
conn.commit()
|
| 24 |
+
except Exception:
|
| 25 |
+
conn.rollback()
|
| 26 |
+
raise
|
| 27 |
+
finally:
|
| 28 |
+
conn.close()
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def fetch_one(query: str, params: Sequence[Any]) -> Optional[Dict[str, Any]]:
|
| 32 |
+
with db_conn() as conn, conn.cursor() as cur:
|
| 33 |
+
cur.execute(query, params)
|
| 34 |
+
return cur.fetchone()
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def fetch_all(query: str, params: Sequence[Any] = ()) -> List[Dict[str, Any]]:
|
| 38 |
+
with db_conn() as conn, conn.cursor() as cur:
|
| 39 |
+
cur.execute(query, params)
|
| 40 |
+
return cur.fetchall()
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def execute(query: str, params: Sequence[Any]) -> None:
|
| 44 |
+
with db_conn() as conn, conn.cursor() as cur:
|
| 45 |
+
cur.execute(query, params)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def create_account(login: str, email: str, password_hash: str) -> int:
|
| 49 |
+
with db_conn() as conn, conn.cursor() as cur:
|
| 50 |
+
cur.execute(
|
| 51 |
+
"""
|
| 52 |
+
INSERT INTO accounts (login, password_hash)
|
| 53 |
+
VALUES (%s, %s)
|
| 54 |
+
RETURNING id
|
| 55 |
+
""",
|
| 56 |
+
(login, password_hash),
|
| 57 |
+
)
|
| 58 |
+
account_id = cur.fetchone()["id"]
|
| 59 |
+
cur.execute(
|
| 60 |
+
"""
|
| 61 |
+
INSERT INTO business_profiles (account_id, company_name, owner_name,
|
| 62 |
+
address_line, postal_code, city, tax_id, bank_account)
|
| 63 |
+
VALUES (%s, '', '', '', '', '', '', '')
|
| 64 |
+
""",
|
| 65 |
+
(account_id,),
|
| 66 |
+
)
|
| 67 |
+
return account_id
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def update_business(account_id: int, data: Dict[str, str]) -> None:
|
| 71 |
+
execute(
|
| 72 |
+
"""
|
| 73 |
+
UPDATE business_profiles
|
| 74 |
+
SET company_name = %s,
|
| 75 |
+
owner_name = %s,
|
| 76 |
+
address_line = %s,
|
| 77 |
+
postal_code = %s,
|
| 78 |
+
city = %s,
|
| 79 |
+
tax_id = %s,
|
| 80 |
+
bank_account = %s
|
| 81 |
+
WHERE account_id = %s
|
| 82 |
+
""",
|
| 83 |
+
(
|
| 84 |
+
data["company_name"],
|
| 85 |
+
data["owner_name"],
|
| 86 |
+
data["address_line"],
|
| 87 |
+
data["postal_code"],
|
| 88 |
+
data["city"],
|
| 89 |
+
data["tax_id"],
|
| 90 |
+
data["bank_account"],
|
| 91 |
+
account_id,
|
| 92 |
+
),
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def fetch_business_logo(account_id: int) -> Optional[Dict[str, Optional[str]]]:
|
| 98 |
+
row = fetch_one(
|
| 99 |
+
"""
|
| 100 |
+
SELECT logo_mime_type, logo_data_base64
|
| 101 |
+
FROM business_profiles
|
| 102 |
+
WHERE account_id = %s
|
| 103 |
+
""",
|
| 104 |
+
(account_id,),
|
| 105 |
+
)
|
| 106 |
+
if not row:
|
| 107 |
+
return None
|
| 108 |
+
mime_type = row.get("logo_mime_type")
|
| 109 |
+
data_base64 = row.get("logo_data_base64")
|
| 110 |
+
if not mime_type or not data_base64:
|
| 111 |
+
return None
|
| 112 |
+
return {"mime_type": mime_type, "data": data_base64}
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def update_business_logo(account_id: int, mime: Optional[str], data_base64: Optional[str]) -> None:
|
| 116 |
+
execute(
|
| 117 |
+
"""
|
| 118 |
+
UPDATE business_profiles
|
| 119 |
+
SET logo_mime_type = %s,
|
| 120 |
+
logo_data_base64 = %s
|
| 121 |
+
WHERE account_id = %s
|
| 122 |
+
""",
|
| 123 |
+
(mime, data_base64, account_id),
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
def upsert_client(account_id: int, payload: Dict[str, str]) -> int:
|
| 127 |
+
row = fetch_one(
|
| 128 |
+
"""
|
| 129 |
+
SELECT id FROM clients
|
| 130 |
+
WHERE account_id = %s AND tax_id = %s
|
| 131 |
+
""",
|
| 132 |
+
(account_id, payload["tax_id"]),
|
| 133 |
+
)
|
| 134 |
+
if row:
|
| 135 |
+
client_id = row["id"]
|
| 136 |
+
execute(
|
| 137 |
+
"""
|
| 138 |
+
UPDATE clients
|
| 139 |
+
SET name = %s,
|
| 140 |
+
address_line = %s,
|
| 141 |
+
postal_code = %s,
|
| 142 |
+
city = %s,
|
| 143 |
+
phone = %s
|
| 144 |
+
WHERE id = %s
|
| 145 |
+
""",
|
| 146 |
+
(
|
| 147 |
+
payload["name"],
|
| 148 |
+
payload["address_line"],
|
| 149 |
+
payload["postal_code"],
|
| 150 |
+
payload["city"],
|
| 151 |
+
payload.get("phone"),
|
| 152 |
+
client_id,
|
| 153 |
+
),
|
| 154 |
+
)
|
| 155 |
+
return client_id
|
| 156 |
+
|
| 157 |
+
with db_conn() as conn, conn.cursor() as cur:
|
| 158 |
+
cur.execute(
|
| 159 |
+
"""
|
| 160 |
+
INSERT INTO clients (account_id, name, address_line, postal_code, city, tax_id, phone)
|
| 161 |
+
VALUES (%s, %s, %s, %s, %s, %s, %s)
|
| 162 |
+
RETURNING id
|
| 163 |
+
""",
|
| 164 |
+
(
|
| 165 |
+
account_id,
|
| 166 |
+
payload["name"],
|
| 167 |
+
payload["address_line"],
|
| 168 |
+
payload["postal_code"],
|
| 169 |
+
payload["city"],
|
| 170 |
+
payload["tax_id"],
|
| 171 |
+
payload.get("phone"),
|
| 172 |
+
),
|
| 173 |
+
)
|
| 174 |
+
return cur.fetchone()["id"]
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
def insert_invoice(account_id: int, client_id: int, invoice: Dict[str, Any]) -> int:
|
| 178 |
+
with db_conn() as conn, conn.cursor() as cur:
|
| 179 |
+
cur.execute(
|
| 180 |
+
"""
|
| 181 |
+
INSERT INTO invoices (account_id, client_id, invoice_number, issued_at,
|
| 182 |
+
sale_date, payment_term_days, exemption_note,
|
| 183 |
+
total_net, total_vat, total_gross)
|
| 184 |
+
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
| 185 |
+
RETURNING id
|
| 186 |
+
""",
|
| 187 |
+
(
|
| 188 |
+
account_id,
|
| 189 |
+
client_id,
|
| 190 |
+
invoice["invoice_id"],
|
| 191 |
+
invoice["issued_at"],
|
| 192 |
+
invoice["sale_date"],
|
| 193 |
+
invoice.get("payment_term", 14),
|
| 194 |
+
invoice.get("exemption_note"),
|
| 195 |
+
invoice["totals"]["net"],
|
| 196 |
+
invoice["totals"]["vat"],
|
| 197 |
+
invoice["totals"]["gross"],
|
| 198 |
+
),
|
| 199 |
+
)
|
| 200 |
+
invoice_id = cur.fetchone()["id"]
|
| 201 |
+
|
| 202 |
+
cur.executemany(
|
| 203 |
+
"""
|
| 204 |
+
INSERT INTO invoice_items (invoice_id, line_no, name, quantity, unit,
|
| 205 |
+
vat_code, vat_label, unit_price_net,
|
| 206 |
+
unit_price_gross, net_total, vat_amount, gross_total)
|
| 207 |
+
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
| 208 |
+
""",
|
| 209 |
+
[
|
| 210 |
+
(
|
| 211 |
+
invoice_id,
|
| 212 |
+
idx + 1,
|
| 213 |
+
item["name"],
|
| 214 |
+
item["quantity"],
|
| 215 |
+
item.get("unit"),
|
| 216 |
+
item.get("vat_code"),
|
| 217 |
+
item.get("vat_label"),
|
| 218 |
+
item["unit_price_net"],
|
| 219 |
+
item["unit_price_gross"],
|
| 220 |
+
item["net_total"],
|
| 221 |
+
item["vat_amount"],
|
| 222 |
+
item["gross_total"],
|
| 223 |
+
)
|
| 224 |
+
for idx, item in enumerate(invoice["items"])
|
| 225 |
+
],
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
cur.executemany(
|
| 229 |
+
"""
|
| 230 |
+
INSERT INTO invoice_vat_summary (invoice_id, vat_label, net_total, vat_total, gross_total)
|
| 231 |
+
VALUES (%s, %s, %s, %s, %s)
|
| 232 |
+
""",
|
| 233 |
+
[
|
| 234 |
+
(
|
| 235 |
+
invoice_id,
|
| 236 |
+
row["vat_label"],
|
| 237 |
+
row["net_total"],
|
| 238 |
+
row["vat_total"],
|
| 239 |
+
row["gross_total"],
|
| 240 |
+
)
|
| 241 |
+
for row in invoice["summary"]
|
| 242 |
+
],
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
return invoice_id
|
server.py
CHANGED
|
@@ -1,672 +1,711 @@
|
|
| 1 |
-
import base64
|
| 2 |
-
import binascii
|
| 3 |
-
import hashlib
|
| 4 |
-
import json
|
| 5 |
-
import os
|
| 6 |
-
import re
|
| 7 |
-
import uuid
|
| 8 |
-
from datetime import date, datetime, timedelta
|
| 9 |
-
from decimal import Decimal, ROUND_HALF_UP, getcontext
|
| 10 |
-
from pathlib import Path
|
| 11 |
-
from typing import Any, Dict, List, Optional, Tuple
|
| 12 |
-
|
| 13 |
-
from flask import Flask, jsonify, request, send_from_directory
|
| 14 |
-
|
| 15 |
-
from db import (
|
| 16 |
-
create_account,
|
| 17 |
-
fetch_all,
|
| 18 |
-
fetch_one,
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
"
|
| 38 |
-
"
|
| 39 |
-
"
|
| 40 |
-
"
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
"
|
| 53 |
-
"
|
| 54 |
-
"
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
if not
|
| 86 |
-
raise ValueError("
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
tmp_path.
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
if
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
if
|
| 183 |
-
return jsonify({"error": "
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
"
|
| 202 |
-
"
|
| 203 |
-
"
|
| 204 |
-
"
|
| 205 |
-
"
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
"
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
return jsonify({"business":
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
"
|
| 293 |
-
"
|
| 294 |
-
"
|
| 295 |
-
"
|
| 296 |
-
"
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
if
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
{
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
"
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
"
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
"
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import binascii
|
| 3 |
+
import hashlib
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
import re
|
| 7 |
+
import uuid
|
| 8 |
+
from datetime import date, datetime, timedelta
|
| 9 |
+
from decimal import Decimal, ROUND_HALF_UP, getcontext
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
from typing import Any, Dict, List, Optional, Tuple
|
| 12 |
+
|
| 13 |
+
from flask import Flask, jsonify, request, send_from_directory
|
| 14 |
+
|
| 15 |
+
from db import (
|
| 16 |
+
create_account,
|
| 17 |
+
fetch_all,
|
| 18 |
+
fetch_one,
|
| 19 |
+
fetch_business_logo,
|
| 20 |
+
insert_invoice,
|
| 21 |
+
update_business,
|
| 22 |
+
update_business_logo,
|
| 23 |
+
upsert_client,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
APP_ROOT = Path(__file__).parent.resolve()
|
| 27 |
+
DATA_DIR = Path(os.environ.get("DATA_DIR", APP_ROOT))
|
| 28 |
+
DATA_FILE = DATA_DIR / "web_invoice_store.json"
|
| 29 |
+
INVOICE_HISTORY_LIMIT = 200
|
| 30 |
+
MAX_LOGO_SIZE = 512 * 1024 # 512 KB
|
| 31 |
+
TOKEN_TTL = timedelta(hours=12)
|
| 32 |
+
ALLOWED_LOGO_MIME_TYPES = {"image/png", "image/jpeg"}
|
| 33 |
+
|
| 34 |
+
DATABASE_AVAILABLE = bool(os.environ.get("NEON_DATABASE_URL"))
|
| 35 |
+
|
| 36 |
+
VAT_RATES: Dict[str, Optional[Decimal]] = {
|
| 37 |
+
"23": Decimal("0.23"),
|
| 38 |
+
"8": Decimal("0.08"),
|
| 39 |
+
"5": Decimal("0.05"),
|
| 40 |
+
"0": Decimal("0.00"),
|
| 41 |
+
"ZW": None,
|
| 42 |
+
"NP": None,
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
DEFAULT_UNIT = "szt."
|
| 46 |
+
ALLOWED_UNITS = {"szt.", "godz."}
|
| 47 |
+
PASSWORD_MIN_LENGTH = 4
|
| 48 |
+
|
| 49 |
+
SESSION_TOKENS: Dict[str, Dict[str, Any]] = {}
|
| 50 |
+
|
| 51 |
+
ALLOWED_STATIC = {
|
| 52 |
+
"index.html",
|
| 53 |
+
"styles.css",
|
| 54 |
+
"main.js",
|
| 55 |
+
"favicon.ico",
|
| 56 |
+
"Roboto-VariableFont_wdth,wght.ttf",
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
app = Flask(__name__, static_folder=str(APP_ROOT), static_url_path="")
|
| 61 |
+
|
| 62 |
+
getcontext().prec = 10
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def _quantize(value: Decimal) -> Decimal:
|
| 66 |
+
return value.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def _decimal(value: Any) -> Decimal:
|
| 70 |
+
try:
|
| 71 |
+
return Decimal(str(value))
|
| 72 |
+
except Exception as error: # pragma: no cover - defensive
|
| 73 |
+
raise ValueError(f"Niepoprawna wartosc liczby: {value}") from error
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def hash_password(password: str) -> str:
|
| 77 |
+
return hashlib.sha256(password.encode("utf-8")).hexdigest()
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
EMAIL_PATTERN = re.compile(r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$")
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def normalize_email(raw_email: str) -> Tuple[str, str]:
|
| 84 |
+
display_email = (raw_email or "").strip()
|
| 85 |
+
if not display_email:
|
| 86 |
+
raise ValueError("Email nie moze byc pusty.")
|
| 87 |
+
if not EMAIL_PATTERN.fullmatch(display_email):
|
| 88 |
+
raise ValueError("Podaj poprawny adres email.")
|
| 89 |
+
return display_email.lower(), display_email
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def sanitize_filename(filename: Optional[str]) -> str:
|
| 93 |
+
if not filename:
|
| 94 |
+
return "logo"
|
| 95 |
+
name = str(filename).split("/")[-1].split("\\")[-1]
|
| 96 |
+
sanitized = re.sub(r"[^A-Za-z0-9._-]", "_", name).strip("._")
|
| 97 |
+
return sanitized or "logo"
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def find_account_identifier(accounts: Dict[str, Any], identifier: str) -> Tuple[Optional[str], Optional[Dict[str, Any]]]:
|
| 101 |
+
key = (identifier or "").strip().lower()
|
| 102 |
+
if not key:
|
| 103 |
+
return None, None
|
| 104 |
+
account = accounts.get(key)
|
| 105 |
+
if account:
|
| 106 |
+
return key, account
|
| 107 |
+
for login_key, candidate in accounts.items():
|
| 108 |
+
candidate_login = (candidate.get("login") or "").strip().lower()
|
| 109 |
+
candidate_email = (candidate.get("email") or "").strip().lower()
|
| 110 |
+
if key in {candidate_login, candidate_email}:
|
| 111 |
+
return login_key, candidate
|
| 112 |
+
return None, None
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def load_store() -> Dict[str, Any]:
|
| 116 |
+
if not DATA_FILE.exists():
|
| 117 |
+
return {"accounts": {}}
|
| 118 |
+
try:
|
| 119 |
+
with DATA_FILE.open("r", encoding="utf-8") as handle:
|
| 120 |
+
data = json.load(handle)
|
| 121 |
+
except json.JSONDecodeError:
|
| 122 |
+
raise ValueError("Plik z danymi jest uszkodzony.")
|
| 123 |
+
return data
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def save_store(data: Dict[str, Any]) -> None:
|
| 127 |
+
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
| 128 |
+
tmp_path = DATA_FILE.with_suffix(".tmp")
|
| 129 |
+
with tmp_path.open("w", encoding="utf-8") as handle:
|
| 130 |
+
json.dump(data, handle, ensure_ascii=False, indent=2)
|
| 131 |
+
tmp_path.replace(DATA_FILE)
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def get_account(data: Dict[str, Any], login_key: str) -> Dict[str, Any]:
|
| 135 |
+
accounts = data.get("accounts") or {}
|
| 136 |
+
account = accounts.get(login_key)
|
| 137 |
+
if not account:
|
| 138 |
+
raise KeyError("Nie znaleziono konta.")
|
| 139 |
+
return account
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def get_account_row(login_key: str) -> Dict[str, Any]:
|
| 143 |
+
row = fetch_one("SELECT id, login FROM accounts WHERE login = %s", (login_key,))
|
| 144 |
+
if not row:
|
| 145 |
+
raise KeyError("Nie znaleziono konta.")
|
| 146 |
+
return row
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
def require_auth() -> str:
|
| 150 |
+
auth_header = request.headers.get("Authorization")
|
| 151 |
+
if not auth_header or not auth_header.startswith("Bearer "):
|
| 152 |
+
raise PermissionError("Brak tokenu.")
|
| 153 |
+
token = auth_header.split(" ", 1)[1].strip()
|
| 154 |
+
session = SESSION_TOKENS.get(token)
|
| 155 |
+
if not session:
|
| 156 |
+
raise PermissionError("Nieprawidlowy token.")
|
| 157 |
+
if session["expires_at"] < datetime.utcnow():
|
| 158 |
+
SESSION_TOKENS.pop(token, None)
|
| 159 |
+
raise PermissionError("Token wygasl.")
|
| 160 |
+
return session["login_key"]
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
@app.route("/")
|
| 164 |
+
def index() -> Any:
|
| 165 |
+
return send_from_directory(app.static_folder, "index.html")
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
@app.route("/<path:filename>")
|
| 169 |
+
def static_files(filename: str) -> Any:
|
| 170 |
+
if filename not in ALLOWED_STATIC:
|
| 171 |
+
return jsonify({"error": "Nie ma takiego zasobu."}), 404
|
| 172 |
+
return send_from_directory(app.static_folder, filename)
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
@app.route("/api/register", methods=["POST"])
|
| 176 |
+
def api_register() -> Any:
|
| 177 |
+
payload = request.get_json(force=True)
|
| 178 |
+
email = payload.get("email")
|
| 179 |
+
password = payload.get("password")
|
| 180 |
+
confirm = payload.get("confirm_password")
|
| 181 |
+
|
| 182 |
+
if password != confirm:
|
| 183 |
+
return jsonify({"error": "Hasla musza byc identyczne."}), 400
|
| 184 |
+
if len(password or "") < PASSWORD_MIN_LENGTH:
|
| 185 |
+
return jsonify({"error": "Haslo jest za krotkie."}), 400
|
| 186 |
+
|
| 187 |
+
login_key, display_email = normalize_email(email)
|
| 188 |
+
password_hash = hash_password(password)
|
| 189 |
+
|
| 190 |
+
if DATABASE_AVAILABLE:
|
| 191 |
+
if fetch_one("SELECT 1 FROM accounts WHERE login = %s", (login_key,)):
|
| 192 |
+
return jsonify({"error": "Konto o podanym emailu juz istnieje."}), 400
|
| 193 |
+
create_account(login_key, display_email, password_hash)
|
| 194 |
+
return jsonify({"message": "Konto zostalo utworzone."})
|
| 195 |
+
|
| 196 |
+
data = load_store()
|
| 197 |
+
if login_key in data["accounts"]:
|
| 198 |
+
return jsonify({"error": "Konto o podanym emailu juz istnieje."}), 400
|
| 199 |
+
|
| 200 |
+
data["accounts"][login_key] = {
|
| 201 |
+
"login": login_key,
|
| 202 |
+
"email": display_email,
|
| 203 |
+
"password_hash": password_hash,
|
| 204 |
+
"business": None,
|
| 205 |
+
"invoices": [],
|
| 206 |
+
"logo": None,
|
| 207 |
+
"created_at": datetime.utcnow().isoformat(timespec="seconds"),
|
| 208 |
+
}
|
| 209 |
+
save_store(data)
|
| 210 |
+
return jsonify({"message": "Konto zostalo utworzone."})
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
@app.route("/api/login", methods=["POST"])
|
| 214 |
+
def api_login() -> Any:
|
| 215 |
+
payload = request.get_json(force=True)
|
| 216 |
+
identifier = payload.get("identifier") or payload.get("email")
|
| 217 |
+
password = payload.get("password")
|
| 218 |
+
if not identifier or not password:
|
| 219 |
+
return jsonify({"error": "Podaj email/login i haslo."}), 400
|
| 220 |
+
|
| 221 |
+
login_key, _ = normalize_email(identifier)
|
| 222 |
+
|
| 223 |
+
if DATABASE_AVAILABLE:
|
| 224 |
+
row = fetch_one(
|
| 225 |
+
"SELECT id, password_hash FROM accounts WHERE login = %s",
|
| 226 |
+
(login_key,),
|
| 227 |
+
)
|
| 228 |
+
if not row or row["password_hash"] != hash_password(password):
|
| 229 |
+
return jsonify({"error": "Niepoprawne dane logowania."}), 401
|
| 230 |
+
token = uuid.uuid4().hex
|
| 231 |
+
SESSION_TOKENS[token] = {
|
| 232 |
+
"login_key": login_key,
|
| 233 |
+
"account_id": row["id"],
|
| 234 |
+
"expires_at": datetime.utcnow() + TOKEN_TTL,
|
| 235 |
+
}
|
| 236 |
+
return jsonify({"token": token, "login": login_key})
|
| 237 |
+
|
| 238 |
+
data = load_store()
|
| 239 |
+
accounts = data.get("accounts") or {}
|
| 240 |
+
login_key, account = find_account_identifier(accounts, login_key)
|
| 241 |
+
if not account or account.get("password_hash") != hash_password(password):
|
| 242 |
+
return jsonify({"error": "Niepoprawne dane logowania."}), 401
|
| 243 |
+
token = uuid.uuid4().hex
|
| 244 |
+
SESSION_TOKENS[token] = {
|
| 245 |
+
"login_key": login_key,
|
| 246 |
+
"expires_at": datetime.utcnow() + TOKEN_TTL,
|
| 247 |
+
}
|
| 248 |
+
return jsonify({"token": token, "login": account.get("login", login_key)})
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
@app.route("/api/logout", methods=["POST"])
|
| 252 |
+
def api_logout() -> Any:
|
| 253 |
+
token = request.headers.get("Authorization", "").replace("Bearer ", "")
|
| 254 |
+
SESSION_TOKENS.pop(token, None)
|
| 255 |
+
return jsonify({"message": "Wylogowano."})
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
@app.route("/api/business", methods=["GET", "POST"])
|
| 259 |
+
def api_business() -> Any:
|
| 260 |
+
try:
|
| 261 |
+
login_key = require_auth()
|
| 262 |
+
except PermissionError:
|
| 263 |
+
return jsonify({"error": "Brak autoryzacji."}), 401
|
| 264 |
+
|
| 265 |
+
data = load_store()
|
| 266 |
+
account = data.get("accounts", {}).get(login_key)
|
| 267 |
+
account_row = None
|
| 268 |
+
if DATABASE_AVAILABLE:
|
| 269 |
+
try:
|
| 270 |
+
account_row = get_account_row(login_key)
|
| 271 |
+
except KeyError:
|
| 272 |
+
return jsonify({"error": "Nie znaleziono konta."}), 404
|
| 273 |
+
|
| 274 |
+
if request.method == "GET":
|
| 275 |
+
if DATABASE_AVAILABLE:
|
| 276 |
+
profile = fetch_one(
|
| 277 |
+
"""
|
| 278 |
+
SELECT company_name, owner_name, address_line, postal_code,
|
| 279 |
+
city, tax_id, bank_account
|
| 280 |
+
FROM business_profiles
|
| 281 |
+
WHERE account_id = %s
|
| 282 |
+
""",
|
| 283 |
+
(account_row["id"],),
|
| 284 |
+
)
|
| 285 |
+
return jsonify({"business": profile})
|
| 286 |
+
if not account:
|
| 287 |
+
return jsonify({"business": None})
|
| 288 |
+
return jsonify({"business": account.get("business")})
|
| 289 |
+
|
| 290 |
+
payload = request.get_json(force=True)
|
| 291 |
+
required_fields = [
|
| 292 |
+
"company_name",
|
| 293 |
+
"owner_name",
|
| 294 |
+
"address_line",
|
| 295 |
+
"postal_code",
|
| 296 |
+
"city",
|
| 297 |
+
"tax_id",
|
| 298 |
+
"bank_account",
|
| 299 |
+
]
|
| 300 |
+
for field in required_fields:
|
| 301 |
+
if not (payload.get(field) or "").strip():
|
| 302 |
+
return jsonify({"error": f"Pole {field} jest wymagane."}), 400
|
| 303 |
+
|
| 304 |
+
if DATABASE_AVAILABLE:
|
| 305 |
+
update_business(account_row["id"], payload)
|
| 306 |
+
return jsonify({"message": "Dane sprzedawcy zostaly zaktualizowane."})
|
| 307 |
+
|
| 308 |
+
if not account:
|
| 309 |
+
return jsonify({"error": "Nie znaleziono konta."}), 404
|
| 310 |
+
account["business"] = payload
|
| 311 |
+
save_store(data)
|
| 312 |
+
return jsonify({"message": "Dane sprzedawcy zostaly zaktualizowane."})
|
| 313 |
+
|
| 314 |
+
|
| 315 |
+
@app.route("/api/logo", methods=["GET", "POST", "DELETE"])
|
| 316 |
+
def api_logo() -> Any:
|
| 317 |
+
try:
|
| 318 |
+
login_key = require_auth()
|
| 319 |
+
except PermissionError:
|
| 320 |
+
return jsonify({"error": "Brak autoryzacji."}), 401
|
| 321 |
+
|
| 322 |
+
account_row = None
|
| 323 |
+
account = None
|
| 324 |
+
data = None
|
| 325 |
+
|
| 326 |
+
if DATABASE_AVAILABLE:
|
| 327 |
+
try:
|
| 328 |
+
account_row = get_account_row(login_key)
|
| 329 |
+
except KeyError:
|
| 330 |
+
return jsonify({"error": "Nie znaleziono konta."}), 404
|
| 331 |
+
else:
|
| 332 |
+
data = load_store()
|
| 333 |
+
try:
|
| 334 |
+
account = get_account(data, login_key)
|
| 335 |
+
except KeyError:
|
| 336 |
+
return jsonify({"error": "Nie znaleziono konta."}), 404
|
| 337 |
+
|
| 338 |
+
if request.method == "GET":
|
| 339 |
+
if DATABASE_AVAILABLE:
|
| 340 |
+
logo_row = fetch_business_logo(account_row["id"])
|
| 341 |
+
if not logo_row:
|
| 342 |
+
return jsonify({"logo": None})
|
| 343 |
+
mime_type = logo_row["mime_type"]
|
| 344 |
+
encoded = logo_row["data"]
|
| 345 |
+
data_url = f"data:{mime_type};base64,{encoded}" if mime_type and encoded else None
|
| 346 |
+
return jsonify(
|
| 347 |
+
{
|
| 348 |
+
"logo": {
|
| 349 |
+
"filename": None,
|
| 350 |
+
"mime_type": mime_type,
|
| 351 |
+
"data": encoded,
|
| 352 |
+
"data_url": data_url,
|
| 353 |
+
"uploaded_at": None,
|
| 354 |
+
}
|
| 355 |
+
}
|
| 356 |
+
)
|
| 357 |
+
logo = account.get("logo") if account else None
|
| 358 |
+
if not logo:
|
| 359 |
+
return jsonify({"logo": None})
|
| 360 |
+
encoded = logo.get("data")
|
| 361 |
+
mime_type = logo.get("mime_type")
|
| 362 |
+
data_url = None
|
| 363 |
+
if encoded and mime_type:
|
| 364 |
+
data_url = f"data:{mime_type};base64,{encoded}"
|
| 365 |
+
return jsonify(
|
| 366 |
+
{
|
| 367 |
+
"logo": {
|
| 368 |
+
"filename": logo.get("filename"),
|
| 369 |
+
"mime_type": mime_type,
|
| 370 |
+
"data": encoded,
|
| 371 |
+
"data_url": data_url,
|
| 372 |
+
"uploaded_at": logo.get("uploaded_at"),
|
| 373 |
+
}
|
| 374 |
+
}
|
| 375 |
+
)
|
| 376 |
+
|
| 377 |
+
if request.method == "DELETE":
|
| 378 |
+
if DATABASE_AVAILABLE:
|
| 379 |
+
update_business_logo(account_row["id"], None, None)
|
| 380 |
+
return jsonify({"message": "Logo zostalo usuniete."})
|
| 381 |
+
if not account or data is None:
|
| 382 |
+
return jsonify({"error": "Nie znaleziono konta."}), 404
|
| 383 |
+
account["logo"] = None
|
| 384 |
+
save_store(data)
|
| 385 |
+
return jsonify({"message": "Logo zostalo usuniete."})
|
| 386 |
+
|
| 387 |
+
payload = request.get_json(force=True)
|
| 388 |
+
raw_content = (payload.get("content") or payload.get("data") or "").strip()
|
| 389 |
+
if not raw_content:
|
| 390 |
+
return jsonify({"error": "Brak danych logo."}), 400
|
| 391 |
+
|
| 392 |
+
provided_mime = (payload.get("mime_type") or "").strip()
|
| 393 |
+
filename = sanitize_filename(payload.get("filename"))
|
| 394 |
+
|
| 395 |
+
if raw_content.startswith("data:"):
|
| 396 |
+
try:
|
| 397 |
+
header, encoded_content = raw_content.split(",", 1)
|
| 398 |
+
except ValueError:
|
| 399 |
+
return jsonify({"error": "Niepoprawny format danych logo."}), 400
|
| 400 |
+
header = header.strip()
|
| 401 |
+
if ";base64" not in header:
|
| 402 |
+
return jsonify({"error": "Niepoprawny format danych logo (oczekiwano base64)."}), 400
|
| 403 |
+
mime_type = header.split(";")[0].replace("data:", "", 1) or provided_mime
|
| 404 |
+
base64_content = encoded_content.strip()
|
| 405 |
+
else:
|
| 406 |
+
mime_type = provided_mime
|
| 407 |
+
base64_content = raw_content
|
| 408 |
+
|
| 409 |
+
mime_type = (mime_type or "").lower()
|
| 410 |
+
if mime_type not in ALLOWED_LOGO_MIME_TYPES:
|
| 411 |
+
return jsonify({"error": "Dozwolone formaty logo to PNG lub JPG."}), 400
|
| 412 |
+
|
| 413 |
+
try:
|
| 414 |
+
logo_bytes = base64.b64decode(base64_content, validate=True)
|
| 415 |
+
except (ValueError, binascii.Error):
|
| 416 |
+
return jsonify({"error": "Nie udalo sie odczytac danych logo (base64)."}), 400
|
| 417 |
+
|
| 418 |
+
if len(logo_bytes) > MAX_LOGO_SIZE:
|
| 419 |
+
return jsonify({"error": f"Logo jest zbyt duze (maksymalnie {MAX_LOGO_SIZE // 1024} KB)."}), 400
|
| 420 |
+
|
| 421 |
+
encoded_logo = base64.b64encode(logo_bytes).decode("ascii")
|
| 422 |
+
stored_logo = {
|
| 423 |
+
"filename": filename,
|
| 424 |
+
"mime_type": mime_type,
|
| 425 |
+
"data": encoded_logo,
|
| 426 |
+
"uploaded_at": datetime.utcnow().isoformat(timespec="seconds"),
|
| 427 |
+
}
|
| 428 |
+
|
| 429 |
+
if DATABASE_AVAILABLE:
|
| 430 |
+
update_business_logo(account_row["id"], stored_logo["mime_type"], stored_logo["data"])
|
| 431 |
+
return jsonify({"logo": stored_logo})
|
| 432 |
+
|
| 433 |
+
account["logo"] = stored_logo
|
| 434 |
+
save_store(data)
|
| 435 |
+
return jsonify({"logo": stored_logo})
|
| 436 |
+
|
| 437 |
+
def normalize_phone(phone: Optional[str]) -> Optional[str]:
|
| 438 |
+
if not phone:
|
| 439 |
+
return None
|
| 440 |
+
digits = re.sub(r"[^\d+]", "", phone)
|
| 441 |
+
return digits or None
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
def validate_client(payload: Dict[str, Any]) -> Dict[str, str]:
|
| 445 |
+
client = {
|
| 446 |
+
"name": (payload.get("clientName") or "").strip(),
|
| 447 |
+
"tax_id": (payload.get("clientTaxId") or "").strip(),
|
| 448 |
+
"address_line": (payload.get("clientAddress") or "").strip(),
|
| 449 |
+
"postal_code": (payload.get("clientPostalCode") or "").strip(),
|
| 450 |
+
"city": (payload.get("clientCity") or "").strip(),
|
| 451 |
+
"phone": normalize_phone(payload.get("clientPhone")),
|
| 452 |
+
}
|
| 453 |
+
return client
|
| 454 |
+
|
| 455 |
+
|
| 456 |
+
def build_invoice(payload: Dict[str, Any], business: Dict[str, Any], client: Dict[str, str]) -> Dict[str, Any]:
|
| 457 |
+
now = datetime.now()
|
| 458 |
+
invoice_id = f"FV-{now.strftime('%Y%m%d-%H%M%S')}"
|
| 459 |
+
issued_at = now.strftime("%Y-%m-%d %H:%M")
|
| 460 |
+
sale_date = payload.get("saleDate") or date.today().isoformat()
|
| 461 |
+
payment_term = int(payload.get("paymentTerm") or 14)
|
| 462 |
+
items = payload.get("items") or []
|
| 463 |
+
|
| 464 |
+
normalized_items: List[Dict[str, Any]] = []
|
| 465 |
+
for item in items:
|
| 466 |
+
name = (item.get("name") or "").strip()
|
| 467 |
+
if not name:
|
| 468 |
+
raise ValueError("Nazwa pozycji nie moze byc pusta.")
|
| 469 |
+
quantity = _quantize(_decimal(item.get("quantity") or "0"))
|
| 470 |
+
if quantity <= Decimal("0"):
|
| 471 |
+
raise ValueError("Ilosc musi byc dodatnia.")
|
| 472 |
+
unit = item.get("unit") or DEFAULT_UNIT
|
| 473 |
+
vat_code = str(item.get("vat") or "23")
|
| 474 |
+
if vat_code not in VAT_RATES:
|
| 475 |
+
raise ValueError("Niepoprawna stawka VAT.")
|
| 476 |
+
unit_price_gross = _quantize(_decimal(item.get("unitPrice") or "0"))
|
| 477 |
+
if unit_price_gross <= Decimal("0"):
|
| 478 |
+
raise ValueError("Cena musi byc dodatnia.")
|
| 479 |
+
vat_rate = VAT_RATES[vat_code]
|
| 480 |
+
if vat_rate is None:
|
| 481 |
+
unit_price_net = unit_price_gross
|
| 482 |
+
vat_amount = Decimal("0.00")
|
| 483 |
+
else:
|
| 484 |
+
unit_price_net = _quantize(unit_price_gross / (Decimal("1.0") + vat_rate))
|
| 485 |
+
vat_amount = _quantize(unit_price_gross - unit_price_net)
|
| 486 |
+
net_total = _quantize(unit_price_net * quantity)
|
| 487 |
+
vat_total = _quantize(vat_amount * quantity)
|
| 488 |
+
gross_total = _quantize(unit_price_gross * quantity)
|
| 489 |
+
normalized_items.append(
|
| 490 |
+
{
|
| 491 |
+
"name": name,
|
| 492 |
+
"quantity": str(quantity),
|
| 493 |
+
"unit": unit,
|
| 494 |
+
"vat_code": vat_code,
|
| 495 |
+
"vat_label": item.get("vatLabel") or vat_code,
|
| 496 |
+
"unit_price_net": str(unit_price_net),
|
| 497 |
+
"unit_price_gross": str(unit_price_gross),
|
| 498 |
+
"net_total": str(net_total),
|
| 499 |
+
"vat_amount": str(vat_amount),
|
| 500 |
+
"gross_total": str(gross_total),
|
| 501 |
+
}
|
| 502 |
+
)
|
| 503 |
+
|
| 504 |
+
totals = {"net": Decimal("0"), "vat": Decimal("0"), "gross": Decimal("0")}
|
| 505 |
+
summary: Dict[str, Dict[str, Decimal]] = {}
|
| 506 |
+
for item in normalized_items:
|
| 507 |
+
totals["net"] += Decimal(item["net_total"])
|
| 508 |
+
totals["vat"] += Decimal(item["vat_amount"])
|
| 509 |
+
totals["gross"] += Decimal(item["gross_total"])
|
| 510 |
+
label = item["vat_label"]
|
| 511 |
+
summary.setdefault(label, {"net_total": Decimal("0"), "vat_total": Decimal("0"), "gross_total": Decimal("0")})
|
| 512 |
+
summary[label]["net_total"] += Decimal(item["net_total"])
|
| 513 |
+
summary[label]["vat_total"] += Decimal(item["vat_amount"])
|
| 514 |
+
summary[label]["gross_total"] += Decimal(item["gross_total"])
|
| 515 |
+
|
| 516 |
+
totals = {key: str(_quantize(value)) for key, value in totals.items()}
|
| 517 |
+
summary_list = [
|
| 518 |
+
{
|
| 519 |
+
"vat_label": label,
|
| 520 |
+
"net_total": str(_quantize(values["net_total"])),
|
| 521 |
+
"vat_total": str(_quantize(values["vat_total"])),
|
| 522 |
+
"gross_total": str(_quantize(values["gross_total"])),
|
| 523 |
+
}
|
| 524 |
+
for label, values in summary.items()
|
| 525 |
+
]
|
| 526 |
+
|
| 527 |
+
exemption_note = payload.get("exemptionNote", "").strip()
|
| 528 |
+
|
| 529 |
+
return {
|
| 530 |
+
"invoice_id": invoice_id,
|
| 531 |
+
"issued_at": issued_at,
|
| 532 |
+
"sale_date": sale_date,
|
| 533 |
+
"payment_term": payment_term,
|
| 534 |
+
"items": normalized_items,
|
| 535 |
+
"summary": summary_list,
|
| 536 |
+
"totals": totals,
|
| 537 |
+
"client": client,
|
| 538 |
+
"business": business,
|
| 539 |
+
"exemption_note": exemption_note,
|
| 540 |
+
}
|
| 541 |
+
|
| 542 |
+
|
| 543 |
+
@app.route("/api/invoices", methods=["GET", "POST"])
|
| 544 |
+
def api_invoices() -> Any:
|
| 545 |
+
try:
|
| 546 |
+
login_key = require_auth()
|
| 547 |
+
except PermissionError:
|
| 548 |
+
return jsonify({"error": "Brak autoryzacji."}), 401
|
| 549 |
+
|
| 550 |
+
if request.method == "GET":
|
| 551 |
+
if DATABASE_AVAILABLE:
|
| 552 |
+
try:
|
| 553 |
+
account_row = get_account_row(login_key)
|
| 554 |
+
except KeyError:
|
| 555 |
+
return jsonify({"error": "Nie znaleziono konta."}), 404
|
| 556 |
+
rows = fetch_all(
|
| 557 |
+
"""
|
| 558 |
+
SELECT invoice_number AS invoice_id,
|
| 559 |
+
to_char(issued_at, 'YYYY-MM-DD HH24:MI') AS issued_at,
|
| 560 |
+
sale_date,
|
| 561 |
+
total_gross
|
| 562 |
+
FROM invoices
|
| 563 |
+
WHERE account_id = %s
|
| 564 |
+
ORDER BY issued_at DESC
|
| 565 |
+
LIMIT %s
|
| 566 |
+
""",
|
| 567 |
+
(account_row["id"], INVOICE_HISTORY_LIMIT),
|
| 568 |
+
)
|
| 569 |
+
return jsonify({"invoices": rows})
|
| 570 |
+
|
| 571 |
+
data = load_store()
|
| 572 |
+
try:
|
| 573 |
+
account = get_account(data, login_key)
|
| 574 |
+
except KeyError:
|
| 575 |
+
return jsonify({"error": "Nie znaleziono konta."}), 404
|
| 576 |
+
invoices = account.get("invoices", [])[:INVOICE_HISTORY_LIMIT]
|
| 577 |
+
return jsonify({"invoices": invoices})
|
| 578 |
+
|
| 579 |
+
payload = request.get_json(force=True)
|
| 580 |
+
data = load_store()
|
| 581 |
+
try:
|
| 582 |
+
account = get_account(data, login_key)
|
| 583 |
+
except KeyError:
|
| 584 |
+
return jsonify({"error": "Nie znaleziono konta."}), 404
|
| 585 |
+
|
| 586 |
+
business = account.get("business")
|
| 587 |
+
if not business:
|
| 588 |
+
return jsonify({"error": "Ustaw dane sprzedawcy przed dodaniem faktury."}), 400
|
| 589 |
+
|
| 590 |
+
client = validate_client(payload)
|
| 591 |
+
try:
|
| 592 |
+
invoice = build_invoice(payload, business, client)
|
| 593 |
+
except ValueError as error:
|
| 594 |
+
return jsonify({"error": str(error)}), 400
|
| 595 |
+
|
| 596 |
+
if DATABASE_AVAILABLE:
|
| 597 |
+
account_row = get_account_row(login_key)
|
| 598 |
+
client_id = upsert_client(
|
| 599 |
+
account_row["id"],
|
| 600 |
+
{
|
| 601 |
+
"name": client["name"],
|
| 602 |
+
"address_line": client["address_line"],
|
| 603 |
+
"postal_code": client["postal_code"],
|
| 604 |
+
"city": client["city"],
|
| 605 |
+
"tax_id": client["tax_id"],
|
| 606 |
+
"phone": client.get("phone"),
|
| 607 |
+
},
|
| 608 |
+
)
|
| 609 |
+
insert_invoice(account_row["id"], client_id, invoice)
|
| 610 |
+
return jsonify({"message": "Faktura zostala zapisana."})
|
| 611 |
+
|
| 612 |
+
invoices = account.setdefault("invoices", [])
|
| 613 |
+
invoices.insert(0, invoice)
|
| 614 |
+
account["invoices"] = invoices[:INVOICE_HISTORY_LIMIT]
|
| 615 |
+
save_store(data)
|
| 616 |
+
return jsonify({"message": "Faktura zostala zapisana."})
|
| 617 |
+
|
| 618 |
+
|
| 619 |
+
@app.route("/api/invoices/summary", methods=["GET"])
|
| 620 |
+
def api_invoice_summary() -> Any:
|
| 621 |
+
try:
|
| 622 |
+
login_key = require_auth()
|
| 623 |
+
except PermissionError:
|
| 624 |
+
return jsonify({"error": "Brak autoryzacji."}), 401
|
| 625 |
+
|
| 626 |
+
now = datetime.utcnow()
|
| 627 |
+
last_month_start = now - timedelta(days=30)
|
| 628 |
+
quarter_first_month = ((now.month - 1) // 3) * 3 + 1
|
| 629 |
+
quarter_start = now.replace(month=quarter_first_month, day=1, hour=0, minute=0, second=0, microsecond=0)
|
| 630 |
+
year_start = now.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0)
|
| 631 |
+
|
| 632 |
+
def aggregate_from_rows(rows: List[Dict[str, Any]], start: datetime) -> Dict[str, Any]:
|
| 633 |
+
count = 0
|
| 634 |
+
gross_total = Decimal("0.00")
|
| 635 |
+
for row in rows:
|
| 636 |
+
issued_dt = row["issued_at"]
|
| 637 |
+
if issued_dt < start:
|
| 638 |
+
continue
|
| 639 |
+
count += 1
|
| 640 |
+
gross_total += Decimal(row["total_gross"])
|
| 641 |
+
return {"count": count, "gross_total": str(_quantize(gross_total))}
|
| 642 |
+
|
| 643 |
+
if DATABASE_AVAILABLE:
|
| 644 |
+
try:
|
| 645 |
+
account_row = get_account_row(login_key)
|
| 646 |
+
except KeyError:
|
| 647 |
+
return jsonify({"error": "Nie znaleziono konta."}), 404
|
| 648 |
+
rows = fetch_all(
|
| 649 |
+
"""
|
| 650 |
+
SELECT issued_at, total_gross
|
| 651 |
+
FROM invoices
|
| 652 |
+
WHERE account_id = %s
|
| 653 |
+
""",
|
| 654 |
+
(account_row["id"],),
|
| 655 |
+
)
|
| 656 |
+
# rows zwraca datetime, ale upewniamy się, że są w Python datetime
|
| 657 |
+
parsed = [
|
| 658 |
+
{
|
| 659 |
+
"issued_at": row["issued_at"],
|
| 660 |
+
"total_gross": row["total_gross"],
|
| 661 |
+
}
|
| 662 |
+
for row in rows
|
| 663 |
+
]
|
| 664 |
+
summary = {
|
| 665 |
+
"last_month": aggregate_from_rows(parsed, last_month_start),
|
| 666 |
+
"quarter": aggregate_from_rows(parsed, quarter_start),
|
| 667 |
+
"year": aggregate_from_rows(parsed, year_start),
|
| 668 |
+
}
|
| 669 |
+
return jsonify({"summary": summary})
|
| 670 |
+
|
| 671 |
+
data = load_store()
|
| 672 |
+
try:
|
| 673 |
+
account = get_account(data, login_key)
|
| 674 |
+
except KeyError:
|
| 675 |
+
return jsonify({"error": "Nie znaleziono konta."}), 404
|
| 676 |
+
invoices = account.get("invoices", [])
|
| 677 |
+
|
| 678 |
+
def parse_issued_at(value: Optional[str]) -> Optional[datetime]:
|
| 679 |
+
if not value:
|
| 680 |
+
return None
|
| 681 |
+
try:
|
| 682 |
+
return datetime.strptime(value, "%Y-%m-%d %H:%M")
|
| 683 |
+
except ValueError:
|
| 684 |
+
return None
|
| 685 |
+
|
| 686 |
+
def aggregate(start: datetime) -> Dict[str, Any]:
|
| 687 |
+
count = 0
|
| 688 |
+
gross_total = Decimal("0.00")
|
| 689 |
+
for invoice in invoices:
|
| 690 |
+
issued_dt = parse_issued_at(invoice.get("issued_at"))
|
| 691 |
+
if issued_dt is None or issued_dt < start:
|
| 692 |
+
continue
|
| 693 |
+
count += 1
|
| 694 |
+
gross_value = invoice.get("totals", {}).get("gross", "0")
|
| 695 |
+
try:
|
| 696 |
+
gross_total += _decimal(gross_value)
|
| 697 |
+
except ValueError:
|
| 698 |
+
continue
|
| 699 |
+
return {"count": count, "gross_total": str(_quantize(gross_total))}
|
| 700 |
+
|
| 701 |
+
summary = {
|
| 702 |
+
"last_month": aggregate(last_month_start),
|
| 703 |
+
"quarter": aggregate(quarter_start),
|
| 704 |
+
"year": aggregate(year_start),
|
| 705 |
+
}
|
| 706 |
+
return jsonify({"summary": summary})
|
| 707 |
+
|
| 708 |
+
|
| 709 |
+
if __name__ == "__main__":
|
| 710 |
+
port = int(os.environ.get("PORT", "5000"))
|
| 711 |
+
app.run(host="0.0.0.0", port=port, debug=True)
|