File size: 5,594 Bytes
9c4e7c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
86
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
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
import getpass
import hashlib
import json
import os
from datetime import datetime

DATA_FILE = "invoice_data.json"
INVOICE_FOLDER = "invoices"


def hash_password(password: str) -> str:
    return hashlib.sha256(password.encode("utf-8")).hexdigest()


def prompt_non_empty(prompt: str) -> str:
    while True:
        value = input(prompt).strip()
        if value:
            return value
        print("Pole nie moze byc puste.")


def prompt_positive_float(prompt: str) -> float:
    while True:
        raw_value = input(prompt).replace(",", ".").strip()
        try:
            value = float(raw_value)
        except ValueError:
            print("Wprowadz liczbe.")
            continue
        if value <= 0:
            print("Wartosc musi byc wieksza od zera.")
            continue
        return value


def load_data():
    if not os.path.exists(DATA_FILE):
        return None
    with open(DATA_FILE, "r", encoding="utf-8") as handle:
        return json.load(handle)


def save_data(data) -> None:
    with open(DATA_FILE, "w", encoding="utf-8") as handle:
        json.dump(data, handle, indent=2, ensure_ascii=False)


def run_setup():
    print("=== Konfiguracja konta przedsiebiorcy ===")
    business = {
        "company_name": prompt_non_empty("Nazwa firmy: "),
        "owner_name": prompt_non_empty("Imie i nazwisko wlasciciela: "),
        "address": prompt_non_empty("Adres: "),
        "tax_id": prompt_non_empty("NIP: "),
        "bank_account": prompt_non_empty("Numer konta bankowego: "),
    }

    print("\nUstaw haslo do logowania.")
    while True:
        password = getpass.getpass("Haslo: ")
        confirm = getpass.getpass("Powtorz haslo: ")
        if not password:
            print("Haslo nie moze byc puste.")
            continue
        if password != confirm:
            print("Hasla nie sa identyczne. Sprobuj ponownie.")
            continue
        break

    data = {
        "business": business,
        "password_hash": hash_password(password),
        "invoices": [],
    }
    save_data(data)
    print("\nDane zapisane. Uruchom aplikacje ponownie, aby zalogowac sie i wystawiac faktury.")


def authenticate(data) -> bool:
    for attempt in range(3):
        password = getpass.getpass("Haslo: ")
        if hash_password(password) == data["password_hash"]:
            return True
        print("Nieprawidlowe haslo.")
    return False


def prompt_client_details():
    answer = input("Dodac dane klienta? (T/N): ").strip().lower()
    if answer not in ("t", "tak"):
        return {}
    print("\n=== Dane klienta ===")
    client = {
        "name": prompt_non_empty("Nazwa / Imie i nazwisko: "),
        "address": prompt_non_empty("Adres: "),
        "tax_id": input("NIP (opcjonalnie): ").strip(),
    }
    return client


def format_invoice_text(invoice_id: str, business: dict, invoice: dict) -> str:
    lines = [
        f"Faktura: {invoice_id}",
        f"Data wystawienia: {invoice['issued_at']}",
        "",
        "=== Sprzedawca ===",
        f"Nazwa: {business['company_name']}",
        f"Wlasciciel: {business['owner_name']}",
        f"Adres: {business['address']}",
        f"NIP: {business['tax_id']}",
        f"Konto bankowe: {business['bank_account']}",
        "",
        "=== Nabywca ===",
    ]

    client = invoice.get("client", {})
    if client:
        lines.extend(
            [
                f"Nazwa: {client.get('name', '')}",
                f"Adres: {client.get('address', '')}",
                f"NIP: {client.get('tax_id', '') or '---'}",
            ]
        )
    else:
        lines.append("Brak danych klienta (pole opcjonalne).")

    lines.extend(
        [
            "",
            "=== Pozycja ===",
            f"Opis: {invoice['item_description']}",
            f"Ilosc: {invoice['quantity']}",
            f"Cena jednostkowa: {invoice['unit_price']:.2f} PLN",
            f"Wartosc netto: {invoice['net_total']:.2f} PLN",
        ]
    )
    return "\n".join(lines)


def create_invoice(data):
    print("\n=== Wystaw fakture ===")
    description = prompt_non_empty("Opis uslugi / towaru: ")
    quantity = prompt_positive_float("Ilosc: ")
    unit_price = prompt_positive_float("Cena jednostkowa (PLN): ")
    client = prompt_client_details()

    issued_at = datetime.now().strftime("%Y-%m-%d %H:%M")
    invoice_id = datetime.now().strftime("FV-%Y%m%d-%H%M%S")
    net_total = quantity * unit_price

    invoice = {
        "invoice_id": invoice_id,
        "issued_at": issued_at,
        "item_description": description,
        "quantity": quantity,
        "unit_price": unit_price,
        "net_total": net_total,
        "client": client,
    }

    os.makedirs(INVOICE_FOLDER, exist_ok=True)
    invoice_path = os.path.join(INVOICE_FOLDER, f"{invoice_id}.txt")
    with open(invoice_path, "w", encoding="utf-8") as handle:
        handle.write(format_invoice_text(invoice_id, data["business"], invoice))

    data["invoices"].append(invoice)
    save_data(data)

    print(f"\nFaktura zapisana do {invoice_path}")
    print(f"Suma do zaplaty: {net_total:.2f} PLN")


def main():
    data = load_data()
    if data is None:
        run_setup()
        return

    print("=== Logowanie ===")
    if not authenticate(data):
        print("Zbyt wiele nieudanych prob logowania. Zakonczono.")
        return

    create_invoice(data)


if __name__ == "__main__":
    main()