File size: 4,928 Bytes
201dcc1
 
 
cf9d036
3198196
201dcc1
caaf712
cf9d036
201dcc1
caaf712
cf9d036
201dcc1
 
cf9d036
 
 
 
 
 
 
201dcc1
 
caaf712
 
cf9d036
caaf712
 
 
 
 
 
 
4094352
 
caaf712
5b1e7be
4094352
caaf712
 
4094352
 
 
caaf712
 
4094352
201dcc1
5b1e7be
 
4094352
caaf712
 
 
 
 
201dcc1
4094352
caaf712
5b1e7be
 
caaf712
 
 
 
 
 
4094352
 
caaf712
 
 
201dcc1
5b1e7be
 
4094352
caaf712
4094352
3198196
 
4094352
4d05d75
3198196
5b1e7be
4094352
caaf712
 
4094352
 
201dcc1
 
 
cf9d036
201dcc1
cf9d036
 
201dcc1
 
 
 
 
 
 
 
 
 
4d05d75
201dcc1
 
4d05d75
201dcc1
 
cf9d036
4d05d75
 
 
 
 
 
 
 
 
201dcc1
 
 
04dac16
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
import gradio as gr
import pandas as pd
from fpdf import FPDF
from datetime import datetime
import os  # To check file existence

# Initialize an empty DataFrame to store all invoice records
invoice_data = pd.DataFrame(columns=["Company Name", "Name", "Invoice No", "Date", "Amount"])

# Function to add new invoice entry and display records by company name
def add_invoice(company_name, name, invoice_no, date, amount):
    global invoice_data
    # Add the new record to the DataFrame
    new_row = {
        "Company Name": company_name,
        "Name": name,
        "Invoice No": invoice_no,
        "Date": date,
        "Amount": float(amount)
    }
    invoice_data = pd.concat([invoice_data, pd.DataFrame([new_row])], ignore_index=True)

    # Filter records by company name
    filtered_data = invoice_data[invoice_data["Company Name"] == company_name]
    
    # Calculate the total amount for that company
    total_amount = filtered_data["Amount"].sum()
    return filtered_data, f"Total Amount for {company_name}: {total_amount}"

# Function to generate and save the filtered invoice data to a PDF
def generate_pdf(company_name):
    global invoice_data
    pdf_file_path = "/content/invoice_records.pdf"  # Define the file path

    try:
        print(f"Generating PDF for company: {company_name}")
        # Create PDF
        pdf = FPDF()
        pdf.add_page()
        pdf.set_font("Arial", 'B', 16)
        pdf.cell(200, 10, txt=f"Invoice Report for {company_name}", ln=True, align="C")
        pdf.set_font("Arial", size=12)
        current_date = datetime.now().strftime("%d/%m/%Y")
        pdf.cell(200, 10, txt=f"Report Date: {current_date}", ln=True, align="C")
        pdf.ln(10)

        # Log the generation process
        print("Adding table headers to the PDF")
        # Table headers
        pdf.cell(50, 10, txt="Company Name", border=1, align="C")
        pdf.cell(40, 10, txt="Name", border=1, align="C")
        pdf.cell(40, 10, txt="Invoice No", border=1, align="C")
        pdf.cell(30, 10, txt="Date", border=1, align="C")
        pdf.cell(30, 10, txt="Amount", border=1, ln=True, align="C")

        # Add filtered data for the company
        filtered_data = invoice_data[invoice_data["Company Name"] == company_name]
        print(f"Number of rows for {company_name}: {len(filtered_data)}")

        for index, row in filtered_data.iterrows():
            pdf.cell(50, 10, txt=str(row['Company Name']), border=1, align="C")
            pdf.cell(40, 10, txt=str(row['Name']), border=1, align="C")
            pdf.cell(40, 10, txt=str(row['Invoice No']), border=1, align="C")
            pdf.cell(30, 10, txt=str(row['Date']), border=1, align="C")
            pdf.cell(30, 10, txt=str(row['Amount']), border=1, ln=True, align="C")

        # Total amount
        total_amount = filtered_data["Amount"].sum()
        pdf.cell(160, 10, txt="Total Amount", border=1, align="R")
        pdf.cell(30, 10, txt=str(total_amount), border=1, ln=True, align="C")

        # Log that we're saving the PDF
        print(f"Saving PDF to {pdf_file_path}")
        # Save PDF file
        pdf.output(pdf_file_path)

        # Check if file exists
        if os.path.exists(pdf_file_path):
            print(f"PDF successfully created at {pdf_file_path}")
            return pdf_file_path
        else:
            print("Error: PDF file not found.")
            return "Error: PDF not generated."
    
    except Exception as e:
        print(f"Error during PDF generation: {e}")
        return "Error: PDF generation failed."

# Gradio interface
with gr.Blocks() as app:
    # Section for adding new invoices with Company Name and Name fields
    with gr.Row():
        company_name = gr.Textbox(label="Company Name")
        name = gr.Textbox(label="Name")
        invoice_no = gr.Textbox(label="Invoice No")
        date = gr.Textbox(label="Date")
        amount = gr.Number(label="Amount")
        add_button = gr.Button("Add Invoice")
    
    # Section for displaying records and total amount
    with gr.Row():
        invoice_records = gr.DataFrame(label="Invoice Records")
        total_amount = gr.Textbox(label="Total Amount", interactive=False)
    
    # Section for generating and displaying PDF link
    with gr.Row():
        generate_pdf_button = gr.Button("Generate PDF")
        pdf_link = gr.HTML()  # To manually display the download link

    # Define button functionalities
    add_button.click(add_invoice, [company_name, name, invoice_no, date, amount], [invoice_records, total_amount])
    
    def show_pdf_link(company_name):
        pdf_path = generate_pdf(company_name)
        if pdf_path != "Error: PDF generation failed.":
            return f'<a href="{pdf_path}" target="_blank">Download PDF</a>'
        else:
            return "Error generating PDF."

    generate_pdf_button.click(show_pdf_link, [company_name], pdf_link)

# Launch the app
app.launch(share=True)