Spaces:
Sleeping
Sleeping
Upload server.py
Browse files
server.py
CHANGED
|
@@ -540,80 +540,102 @@ def build_invoice(payload: Dict[str, Any], business: Dict[str, Any], client: Dic
|
|
| 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 |
-
|
| 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 |
@app.route("/api/invoices/summary", methods=["GET"])
|
|
|
|
| 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 |
+
|
| 581 |
+
if DATABASE_AVAILABLE:
|
| 582 |
+
try:
|
| 583 |
+
account_row = get_account_row(login_key)
|
| 584 |
+
except KeyError:
|
| 585 |
+
return jsonify({"error": "Nie znaleziono konta."}), 404
|
| 586 |
+
business = fetch_one(
|
| 587 |
+
"""
|
| 588 |
+
SELECT company_name, owner_name, address_line, postal_code,
|
| 589 |
+
city, tax_id, bank_account
|
| 590 |
+
FROM business_profiles
|
| 591 |
+
WHERE account_id = %s
|
| 592 |
+
""",
|
| 593 |
+
(account_row["id"],),
|
| 594 |
+
)
|
| 595 |
+
if not business:
|
| 596 |
+
return jsonify({"error": "Ustaw dane sprzedawcy przed dodaniem faktury."}), 400
|
| 597 |
+
|
| 598 |
+
client = validate_client(payload)
|
| 599 |
+
try:
|
| 600 |
+
invoice = build_invoice(payload, business, client)
|
| 601 |
+
except ValueError as error:
|
| 602 |
+
return jsonify({"error": str(error)}), 400
|
| 603 |
+
|
| 604 |
+
client_id = upsert_client(
|
| 605 |
+
account_row["id"],
|
| 606 |
+
{
|
| 607 |
+
"name": client["name"],
|
| 608 |
+
"address_line": client["address_line"],
|
| 609 |
+
"postal_code": client["postal_code"],
|
| 610 |
+
"city": client["city"],
|
| 611 |
+
"tax_id": client["tax_id"],
|
| 612 |
+
"phone": client.get("phone"),
|
| 613 |
+
},
|
| 614 |
+
)
|
| 615 |
+
insert_invoice(account_row["id"], client_id, invoice)
|
| 616 |
+
return jsonify({"message": "Faktura zostala zapisana.", "invoice": invoice})
|
| 617 |
+
|
| 618 |
+
data = load_store()
|
| 619 |
+
try:
|
| 620 |
+
account = get_account(data, login_key)
|
| 621 |
+
except KeyError:
|
| 622 |
+
return jsonify({"error": "Nie znaleziono konta."}), 404
|
| 623 |
+
|
| 624 |
+
business = account.get("business")
|
| 625 |
+
if not business:
|
| 626 |
+
return jsonify({"error": "Ustaw dane sprzedawcy przed dodaniem faktury."}), 400
|
| 627 |
+
|
| 628 |
+
client = validate_client(payload)
|
| 629 |
+
try:
|
| 630 |
+
invoice = build_invoice(payload, business, client)
|
| 631 |
+
except ValueError as error:
|
| 632 |
+
return jsonify({"error": str(error)}), 400
|
| 633 |
+
|
| 634 |
+
invoices = account.setdefault("invoices", [])
|
| 635 |
+
invoices.insert(0, invoice)
|
| 636 |
+
account["invoices"] = invoices[:INVOICE_HISTORY_LIMIT]
|
| 637 |
+
save_store(data)
|
| 638 |
+
return jsonify({"message": "Faktura zostala zapisana.", "invoice": invoice})
|
| 639 |
|
| 640 |
|
| 641 |
@app.route("/api/invoices/summary", methods=["GET"])
|