Antoni09 commited on
Commit
b2d6f55
·
verified ·
1 Parent(s): b85875e

Upload server.py

Browse files
Files changed (1) hide show
  1. server.py +96 -74
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
- 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"])
 
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"])