please check this for errors, its not creating invoices? : import os
from PyQt6.QtWidgets import (
    QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton,
    QMessageBox, QComboBox, QListWidget, QSpinBox
)
from PyQt6.QtCore import pyqtSignal
from invoice import InvoiceProcessor
from data_handling import load_json, save_json
from datetime import datetime

CLIENTS_FILE = 'clients.json'
LINE_ITEMS_FILE = 'line_items.json'
TEMPLATES_FILE = 'templates.json'

DEFAULT_TEMPLATE = [
    {"text": "[Logo]", "x": 10, "y": 10},
    {"text": "From: [Your Company Address]", "x": 10, "y": 50},
    {"text": "Invoice Number: #INV001", "x": 530, "y": 10},
    {"text": "Invoice Date: YYYY-MM-DD", "x": 530, "y": 60},
    {"text": "Bill To: [Client Name]", "x": 10, "y": 120},
    {"text": "[Client Address]", "x": 10, "y": 160},
    {"text": "Line Items: Description - Qty - Price", "x": 10, "y": 200},
    {"text": "Subtotal: R0.00", "x": 540, "y": 450},
    {"text": "VAT: R0.00", "x": 540, "y": 490},
    {"text": "Total: R0.00", "x": 540, "y": 530},
    {"text": "Footer: Thank you for your business!", "x": 10, "y": 520}
]

class InvoiceCreationTab(QWidget):
    # Define the signal for creating an invoice with 7 parameters
    create_invoice_signal = pyqtSignal(str, str, str, str, str, list, str)

    def __init__(self, parent, manage_invoices_tab=None):
        super().__init__(parent)
        self.parent = parent
        self.manage_invoices_tab = manage_invoices_tab
        self.clients = load_json(CLIENTS_FILE)
        self.line_items_data = load_json(LINE_ITEMS_FILE)

        # Ensure templates file exists, if not create with default template
        if not os.path.exists(TEMPLATES_FILE):
            save_json({"DefaultTemplate": DEFAULT_TEMPLATE}, TEMPLATES_FILE)

        self.templates = load_json(TEMPLATES_FILE)
        if not self.templates:
            QMessageBox.warning(self, "Load Error", "No templates found or error in loading templates.")

        self.line_items = []
        self.selected_template = None
        self.processor = InvoiceProcessor(self)
        self.init_ui()

    def init_ui(self):
        layout = QVBoxLayout()

        # Template Selection
        template_layout = QHBoxLayout()
        self.template_combo = QComboBox(self)
        self.load_templates()
        self.template_combo.currentIndexChanged.connect(self.change_template)
        template_layout.addWidget(QLabel("Select Template:"))
        template_layout.addWidget(self.template_combo)
        layout.addLayout(template_layout)

        # Client Selection
        client_layout = QHBoxLayout()
        self.client_combo = QComboBox(self)
        self.load_clients()
        client_layout.addWidget(QLabel("Select Client:"))
        client_layout.addWidget(self.client_combo)
        layout.addLayout(client_layout)

        # VAT Input (Optional)
        vat_layout = QHBoxLayout()
        self.vat_input = QLineEdit(self)
        self.vat_input.setPlaceholderText("VAT Number (Optional)")
        vat_layout.addWidget(QLabel("VAT Number:"))
        vat_layout.addWidget(self.vat_input)
        layout.addLayout(vat_layout)

        # Invoice Date Input (New Date Input)
        date_layout = QHBoxLayout()
        self.date_input = QLineEdit(self)
        self.date_input.setPlaceholderText("Invoice Date (YYYY-MM-DD)")
        date_layout.addWidget(QLabel("Invoice Date:"))
        date_layout.addWidget(self.date_input)
        layout.addLayout(date_layout)

        # Line Item Selection
        line_item_layout = QHBoxLayout()
        self.item_combo = QComboBox(self)
        self.load_line_items()
        self.quantity_input = QSpinBox(self)
        self.quantity_input.setMinimum(1)
        self.quantity_input.setValue(1)
        self.add_item_button = QPushButton("Add Item")
        self.add_item_button.clicked.connect(self.add_line_item)
        line_item_layout.addWidget(QLabel("Item:"))
        line_item_layout.addWidget(self.item_combo)
        line_item_layout.addWidget(QLabel("Quantity:"))
        line_item_layout.addWidget(self.quantity_input)
        line_item_layout.addWidget(self.add_item_button)
        layout.addLayout(line_item_layout)

        # Added Line Items List
        self.added_items_list = QListWidget()
        layout.addWidget(QLabel("Added Line Items:"))
        layout.addWidget(self.added_items_list)

        # Discounts and Taxes Input
        discounts_taxes_layout = QHBoxLayout()
        self.discount_input = QLineEdit(self)
        self.discount_input.setPlaceholderText("Discount (%)")
        self.tax_rate_input = QLineEdit(self)
        self.tax_rate_input.setPlaceholderText("Tax Rate (%)")
        discounts_taxes_layout.addWidget(QLabel("Discount (%):"))
        discounts_taxes_layout.addWidget(self.discount_input)
        discounts_taxes_layout.addWidget(QLabel("Tax Rate (%):"))
        discounts_taxes_layout.addWidget(self.tax_rate_input)
        layout.addLayout(discounts_taxes_layout)

        # Output Format Selection
        output_layout = QHBoxLayout()
        self.output_format_combo = QComboBox(self)
        self.output_format_combo.addItems(["PDF", "Excel"])
        output_layout.addWidget(QLabel("Output Format:"))
        output_layout.addWidget(self.output_format_combo)
        layout.addLayout(output_layout)

        # Create Invoice Button
        self.create_invoice_button = QPushButton("Create Invoice")
        self.create_invoice_button.clicked.connect(self.create_invoice)
        layout.addWidget(self.create_invoice_button)

        self.setLayout(layout)

    def load_templates(self):
        self.template_combo.clear()
        templates_list = list(self.templates.keys())
        if templates_list:
            self.template_combo.addItems(["Default"] + templates_list)
            self.selected_template = "Default"
        else:
            QMessageBox.warning(self, "Load Error", "No templates available.")

    def change_template(self, index):
        selected = self.template_combo.currentText()
        self.selected_template = selected if selected != "Default" else None

    def load_clients(self):
        self.client_combo.clear()
        self.client_combo.addItems(list(self.clients.keys()))

    def load_line_items(self):
        self.item_combo.clear()
        self.item_combo.addItems(list(self.line_items_data.keys()))

    def add_line_item(self):
        item_name = self.item_combo.currentText()
        quantity = self.quantity_input.value()
        if not item_name:
            QMessageBox.warning(self, "Selection Error", "Please select a line item.")
            return
        price = self.line_items_data[item_name]['price']
        total = price * quantity
        self.line_items.append({
            "item": item_name,
            "quantity": quantity,
            "price": price,
            "total": total
        })
        self.update_added_items_list()

    def update_added_items_list(self):
        self.added_items_list.clear()
        for idx, item in enumerate(self.line_items, 1):
            self.added_items_list.addItem(
                f"{idx}. {item['item']} - Qty: {item['quantity']} - Price: R{item['price']:.2f} - Total: R{item['total']:.2f}"
            )

    def create_invoice(self):
        client_name = self.client_combo.currentText()
        vat_number = self.vat_input.text().strip()
        discount = self.discount_input.text().strip()
        tax_rate = self.tax_rate_input.text().strip()

        # Ensure client and line items are selected
        if not client_name:
            QMessageBox.warning(self, "Input Error", "Please select a client.")
            return

        if not self.line_items:
            QMessageBox.warning(self, "Input Error", "Please add at least one line item.")
            return

        # Ensure date is in correct format (YYYY-MM-DD)
        invoice_date = self.date_input.text().strip()
        try:
            # Attempt to parse the invoice date (if not in correct format, an exception will be raised)
            datetime.strptime(invoice_date, '%Y-%m-%d')
        except ValueError:
            QMessageBox.warning(self, "Input Error", "Invalid date format. Please use YYYY-MM-DD.")
            return

        output_format = self.output_format_combo.currentText()

        # Emit signal with all invoice data (7 parameters)
        self.create_invoice_signal.emit(client_name, vat_number, invoice_date, discount, tax_rate, self.line_items, output_format)

        # Reset form after creating invoice
        self.line_items = []
        self.update_added_items_list()