File size: 5,152 Bytes
201dcc1
 
 
cf9d036
ef6bf75
9ff0c52
201dcc1
25ef9d4
 
201dcc1
25ef9d4
 
 
9ff0c52
25ef9d4
9ff0c52
25ef9d4
9ff0c52
25ef9d4
9ff0c52
 
 
 
25ef9d4
ac5e180
25ef9d4
 
ef6bf75
 
25ef9d4
ef6bf75
 
 
9ff0c52
 
 
caaf712
25ef9d4
 
 
 
 
 
 
 
 
 
ef6bf75
25ef9d4
 
 
 
 
 
 
caaf712
6539c9c
 
 
 
 
 
10eb027
 
ef6bf75
201dcc1
10eb027
 
 
 
25ef9d4
10eb027
 
 
 
5b1e7be
10eb027
25ef9d4
 
 
10eb027
25ef9d4
4094352
6539c9c
25ef9d4
 
 
 
10eb027
25ef9d4
201dcc1
25ef9d4
 
 
4094352
ef6bf75
10eb027
 
ef6bf75
4d05d75
10eb027
caaf712
a18fe7e
201dcc1
 
 
 
25ef9d4
cf9d036
25ef9d4
201dcc1
 
 
 
ef6bf75
201dcc1
25ef9d4
 
 
 
201dcc1
 
 
a18fe7e
201dcc1
 
25ef9d4
4d05d75
ef6bf75
4d05d75
ef6bf75
201dcc1
 
 
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
import gradio as gr
import pandas as pd
from fpdf import FPDF
from datetime import datetime
import tempfile
import os

# Initialize an empty DataFrame to store all records of invoices (acting as our ledger)
ledger = pd.DataFrame(columns=["Head", "Company Name", "SP/Station", "Invoice No", "Date", "Amount"])

# Function to add a new invoice under a specific head
def add_invoice(head, company_name, sp_station, invoice_no, date, amount):
    global ledger
    try:
        # Add the new record to the ledger
        new_row = {
            "Head": head,
            "Company Name": company_name,
            "SP/Station": sp_station,
            "Invoice No": invoice_no,
            "Date": date,
            "Amount": float(amount)
        }
        ledger = pd.concat([ledger, pd.DataFrame([new_row])], ignore_index=True)
        
        # Filter the ledger for the selected head
        filtered_ledger = ledger[ledger["Head"] == head]
        
        # Return filtered ledger and the sum of amounts for this head
        total_amount = filtered_ledger["Amount"].sum()
        
        # Return only the relevant rows and total to speed up processing
        return filtered_ledger.to_dict('records'), f"Total for {head}: {total_amount}"

    except Exception as e:
        return pd.DataFrame(), "Error during adding invoice"

# Function to search and display all previous records by head
def search_by_head(head):
    global ledger
    try:
        # Filter the ledger for the selected head
        filtered_ledger = ledger[ledger["Head"] == head]

        # Calculate the total for the selected head
        total_amount = filtered_ledger["Amount"].sum()

        return filtered_ledger.to_dict('records'), f"Total for {head}: {total_amount}"

    except Exception as e:
        return pd.DataFrame(), "Error during search by head"

# Function to generate a PDF report for a specific head
def generate_ledger_pdf(head):
    global ledger
    try:
        # Filter records by head
        filtered_ledger = ledger[ledger["Head"] == head]

        if filtered_ledger.empty:
            return None

        # Use a temporary directory to save the file
        with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
            pdf_file_path = tmp_file.name

            # Create a PDF
            pdf = FPDF()
            pdf.add_page()
            pdf.set_font("Arial", 'B', 16)
            pdf.cell(200, 10, txt=f"Ledger Report for {head}", 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)

            # Table headers
            pdf.cell(40, 10, txt="Company Name", border=1, align="C")
            pdf.cell(40, 10, txt="SP/Station", border=1, align="C")
            pdf.cell(30, 10, txt="Invoice No", border=1, align="C")
            pdf.cell(30, 10, txt="Date", border=1, align="C")
            pdf.cell(50, 10, txt="Amount", border=1, ln=True, align="C")

            # Add table rows
            for _, row in filtered_ledger.iterrows():
                pdf.cell(40, 10, txt=str(row['Company Name']), border=1, align="C")
                pdf.cell(40, 10, txt=str(row['SP/Station']), border=1, align="C")
                pdf.cell(30, 10, txt=str(row['Invoice No']), border=1, align="C")
                pdf.cell(30, 10, txt=str(row['Date']), border=1, align="C")
                pdf.cell(50, 10, txt=str(row['Amount']), border=1, ln=True, align="C")

            total_amount = filtered_ledger["Amount"].sum()
            pdf.cell(140, 10, txt="Total Amount", border=1, align="R")
            pdf.cell(50, 10, txt=str(total_amount), border=1, ln=True, align="C")

            # Save the PDF
            pdf.output(pdf_file_path)

            # Return the path of the generated PDF
            return pdf_file_path

    except Exception as e:
        return None

# Gradio interface
with gr.Blocks() as app:
    with gr.Row():
        head = gr.Textbox(label="Head")
        company_name = gr.Textbox(label="Company Name")
        sp_station = gr.Textbox(label="SP/Station")
        invoice_no = gr.Textbox(label="Invoice No")
        date = gr.Textbox(label="Date")
        amount = gr.Number(label="Amount")
        add_button = gr.Button("Add Invoice")

    with gr.Row():
        search_head = gr.Textbox(label="Search by Head")
        search_button = gr.Button("Search Ledger")
        ledger_records = gr.DataFrame(label="Ledger Records")
        total_display = gr.Textbox(label="Total Amount", interactive=False)
    
    with gr.Row():
        generate_pdf_button = gr.Button("Generate PDF")
        pdf_output = gr.File(label="Download PDF")

    # Define button functionalities
    add_button.click(add_invoice, [head, company_name, sp_station, invoice_no, date, amount], [ledger_records, total_display])
    
    search_button.click(search_by_head, [search_head], [ledger_records, total_display])

    generate_pdf_button.click(generate_ledger_pdf, [search_head], [pdf_output])

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