Python program to automatically generate simple text invoices with customer data, products, quantities, subtotal, VAT, and total saved as a formatted text file.
import datetime
class Prodotto:
def __init__(self, descrizione, prezzo_unitario):
self.descrizione = descrizione
self.prezzo_unitario = prezzo_unitario
class Fattura:
def __init__(self, numero, cliente, indirizzo, partita_iva, prodotti_quantita, iva_percentuale=22):
self.numero = numero
self.cliente = cliente
self.indirizzo = indirizzo
self.partita_iva = partita_iva
self.prodotti_quantita = prodotti_quantita # dict {Prodotto: quantita}
self.iva_percentuale = iva_percentuale
self.data = datetime.date.today()
def calcola_subtotale(self):
subtotale = 0
for prodotto, quantita in self.prodotti_quantita.items():
subtotale += prodotto.prezzo_unitario * quantita
return subtotale
def calcola_iva(self):
return self.calcola_subtotale() * self.iva_percentuale / 100
def calcola_totale(self):
return self.calcola_subtotale() + self.calcola_iva()
def genera_testo_fattura(self):
righe = []
righe.append(f"Invoice no. {self.numero}")
righe.append(f"Date: {self.data.strftime('%d/%m/%Y')}")
righe.append("")
righe.append(f"Customer: {self.cliente}")
righe.append(f"Address: {self.indirizzo}")
righe.append(f"VAT Number: {self.partita_iva}")
righe.append("")
righe.append("Description Quantity Unit Price Total")
righe.append("------------------------------------------------------------")
for prodotto, quantita in self.prodotti_quantita.items():
totale_prodotto = prodotto.prezzo_unitario * quantita
# Aligned formatting with spaces
righe.append(f"{prodotto.descrizione:<20} {quantita:>8} {prodotto.prezzo_unitario:>14.2f} {totale_prodotto:>10.2f}")
righe.append("------------------------------------------------------------")
subtotale = self.calcola_subtotale()
iva = self.calcola_iva()
totale = self.calcola_totale()
righe.append(f"Subtotal: {subtotale:.2f} EUR")
righe.append(f"VAT ({self.iva_percentuale}%): {iva:.2f} EUR")
righe.append(f"Total: {totale:.2f} EUR")
return "\n".join(righe)
def salva_fattura(self):
nome_file = f"fattura_{self.numero}.txt"
testo = self.genera_testo_fattura()
with open(nome_file, "w", encoding="utf-8") as f:
f.write(testo)
return nome_file
def main():
# Customer data
cliente = "Mario Rossi"
indirizzo = "Via Roma 10, 00100 Roma"
partita_iva = "IT12345678901"
# Available products
prodotto1 = Prodotto("Blue Pen", 1.20)
prodotto2 = Prodotto("A4 Notebook", 2.50)
prodotto3 = Prodotto("30cm Ruler", 1.00)
# Quantities purchased
prodotti_quantita = {
prodotto1: 10,
prodotto2: 5,
prodotto3: 2
}
# Invoice number (could be incremental)
numero_fattura = 1
fattura = Fattura(numero_fattura, cliente, indirizzo, partita_iva, prodotti_quantita)
nome_file = fattura.salva_fattura()
print(f"Invoice saved in file: {nome_file}")
if __name__ == "__main__":
main()Python 3.x
DEPENDENCIES: no external libraries, only standard libraries (datetime)
CONFIG: the program saves the invoice file named 'fattura_<number>.txt' in the execution folder
INPUT: customer data (name, address, VAT number), list of products (description, unit price), quantities purchased
OUTPUT: text file with formatted invoice