Categories We Write About

Build a simple invoice tracker in Python

Here’s a simple Python program to track invoices. This basic tracker allows you to add new invoices with details like the invoice number, client, amount, and due date, and then list all invoices. It also allows marking invoices as paid.

python
import csv from datetime import datetime class InvoiceTracker: def __init__(self, filename="invoices.csv"): self.filename = filename # Check if the CSV file exists, if not, create it with headers try: with open(self.filename, mode='r', newline='') as file: pass # If file exists, just pass except FileNotFoundError: with open(self.filename, mode='w', newline='') as file: writer = csv.writer(file) writer.writerow(["Invoice Number", "Client", "Amount", "Due Date", "Paid"]) def add_invoice(self, invoice_number, client, amount, due_date): with open(self.filename, mode='a', newline='') as file: writer = csv.writer(file) writer.writerow([invoice_number, client, amount, due_date, "No"]) def mark_paid(self, invoice_number): invoices = self.read_invoices() updated_invoices = [] for invoice in invoices: if invoice[0] == invoice_number: invoice[4] = "Yes" updated_invoices.append(invoice) self.write_invoices(updated_invoices) def read_invoices(self): invoices = [] with open(self.filename, mode='r', newline='') as file: reader = csv.reader(file) next(reader) # Skip header for row in reader: invoices.append(row) return invoices def write_invoices(self, invoices): with open(self.filename, mode='w', newline='') as file: writer = csv.writer(file) writer.writerow(["Invoice Number", "Client", "Amount", "Due Date", "Paid"]) for invoice in invoices: writer.writerow(invoice) def list_invoices(self): invoices = self.read_invoices() for invoice in invoices: print(f"Invoice Number: {invoice[0]}, Client: {invoice[1]}, Amount: {invoice[2]}, Due Date: {invoice[3]}, Paid: {invoice[4]}") # Example usage: tracker = InvoiceTracker() # Add some invoices tracker.add_invoice("INV001", "Client A", 250.00, "2023-06-30") tracker.add_invoice("INV002", "Client B", 450.50, "2023-07-15") # List all invoices print("All Invoices:") tracker.list_invoices() # Mark an invoice as paid tracker.mark_paid("INV001") # List all invoices again to check the status print("nUpdated Invoices:") tracker.list_invoices()

Explanation:

  1. CSV File Handling: This script uses CSV files to store the invoice data (invoices.csv). If the file doesn’t exist, it will be created with headers.

  2. Functions:

    • add_invoice(): Adds a new invoice with the provided details.

    • mark_paid(): Marks an invoice as paid by updating the “Paid” field.

    • read_invoices(): Reads and returns a list of all invoices.

    • write_invoices(): Writes the updated invoices back to the CSV file.

    • list_invoices(): Prints all the invoices with their status.

You can modify the file path and add more complex features, such as sorting by due date, generating reports, etc. Let me know if you need further features or explanations!

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About