Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,37 +5,54 @@ from datetime import datetime
|
|
5 |
import tempfile # Use for temporary file handling
|
6 |
import os
|
7 |
|
8 |
-
# Initialize an empty DataFrame to store all
|
9 |
-
|
10 |
|
11 |
-
# Function to add new invoice
|
12 |
-
def add_invoice(company_name,
|
13 |
-
global
|
14 |
try:
|
15 |
-
# Add the new record to the
|
16 |
new_row = {
|
|
|
17 |
"Company Name": company_name,
|
18 |
-
"
|
19 |
"Invoice No": invoice_no,
|
20 |
"Date": date,
|
21 |
"Amount": float(amount)
|
22 |
}
|
23 |
-
|
24 |
|
25 |
-
# Filter
|
26 |
-
|
27 |
|
28 |
-
# Calculate the total
|
29 |
-
total_amount =
|
30 |
-
return
|
31 |
|
32 |
except Exception as e:
|
33 |
print(f"Error during adding invoice: {e}")
|
34 |
return pd.DataFrame(), "Error during adding invoice"
|
35 |
|
36 |
-
# Function to
|
37 |
-
def
|
38 |
-
global
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
try:
|
40 |
# Use a temporary directory to save the file
|
41 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
@@ -45,33 +62,33 @@ def generate_invoice_pdf(company_name):
|
|
45 |
pdf = FPDF()
|
46 |
pdf.add_page()
|
47 |
pdf.set_font("Arial", 'B', 16)
|
48 |
-
pdf.cell(200, 10, txt=f"
|
49 |
pdf.set_font("Arial", size=12)
|
50 |
current_date = datetime.now().strftime("%d/%m/%Y")
|
51 |
pdf.cell(200, 10, txt=f"Report Date: {current_date}", ln=True, align="C")
|
52 |
pdf.ln(10)
|
53 |
|
54 |
# Table headers
|
55 |
-
pdf.cell(
|
56 |
-
pdf.cell(40, 10, txt="
|
57 |
-
pdf.cell(
|
58 |
pdf.cell(30, 10, txt="Date", border=1, align="C")
|
59 |
-
pdf.cell(
|
60 |
|
61 |
-
# Filter records by
|
62 |
-
|
63 |
|
64 |
-
for _, row in
|
65 |
-
pdf.cell(
|
66 |
-
pdf.cell(40, 10, txt=str(row['
|
67 |
-
pdf.cell(
|
68 |
pdf.cell(30, 10, txt=str(row['Date']), border=1, align="C")
|
69 |
-
pdf.cell(
|
70 |
|
71 |
-
# Total amount
|
72 |
-
total_amount =
|
73 |
-
pdf.cell(
|
74 |
-
pdf.cell(
|
75 |
|
76 |
# Save PDF file
|
77 |
pdf.output(pdf_file_path)
|
@@ -85,19 +102,22 @@ def generate_invoice_pdf(company_name):
|
|
85 |
|
86 |
# Gradio interface
|
87 |
with gr.Blocks() as app:
|
88 |
-
# Section for adding new invoices with
|
89 |
with gr.Row():
|
|
|
90 |
company_name = gr.Textbox(label="Company Name")
|
91 |
-
|
92 |
invoice_no = gr.Textbox(label="Invoice No")
|
93 |
date = gr.Textbox(label="Date")
|
94 |
amount = gr.Number(label="Amount")
|
95 |
add_button = gr.Button("Add Invoice")
|
96 |
|
97 |
-
# Section for displaying
|
98 |
with gr.Row():
|
99 |
-
|
100 |
-
|
|
|
|
|
101 |
|
102 |
# Section for generating and displaying PDF link
|
103 |
with gr.Row():
|
@@ -105,16 +125,22 @@ with gr.Blocks() as app:
|
|
105 |
pdf_output = gr.File(label="Download PDF")
|
106 |
|
107 |
# Define button functionalities
|
108 |
-
add_button.click(add_invoice, [company_name,
|
109 |
|
110 |
-
def
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
if pdf_path:
|
113 |
return pdf_path
|
114 |
else:
|
115 |
return None
|
116 |
|
117 |
-
generate_pdf_button.click(generate_pdf, [
|
118 |
|
119 |
# Launch the app
|
120 |
app.launch(share=True)
|
|
|
5 |
import tempfile # Use for temporary file handling
|
6 |
import os
|
7 |
|
8 |
+
# Initialize an empty DataFrame to store all records of invoices (acting as our ledger)
|
9 |
+
ledger = pd.DataFrame(columns=["Head", "Company Name", "SP/Station", "Invoice No", "Date", "Amount"])
|
10 |
|
11 |
+
# Function to add a new invoice under a specific head
|
12 |
+
def add_invoice(head, company_name, sp_station, invoice_no, date, amount):
|
13 |
+
global ledger
|
14 |
try:
|
15 |
+
# Add the new record to the ledger
|
16 |
new_row = {
|
17 |
+
"Head": head,
|
18 |
"Company Name": company_name,
|
19 |
+
"SP/Station": sp_station,
|
20 |
"Invoice No": invoice_no,
|
21 |
"Date": date,
|
22 |
"Amount": float(amount)
|
23 |
}
|
24 |
+
ledger = pd.concat([ledger, pd.DataFrame([new_row])], ignore_index=True)
|
25 |
|
26 |
+
# Filter the ledger for the selected head
|
27 |
+
filtered_ledger = ledger[ledger["Head"] == head]
|
28 |
|
29 |
+
# Calculate the total for the selected head
|
30 |
+
total_amount = filtered_ledger["Amount"].sum()
|
31 |
+
return filtered_ledger, f"Total for {head}: {total_amount}"
|
32 |
|
33 |
except Exception as e:
|
34 |
print(f"Error during adding invoice: {e}")
|
35 |
return pd.DataFrame(), "Error during adding invoice"
|
36 |
|
37 |
+
# Function to search and display all previous records by head
|
38 |
+
def search_by_head(head):
|
39 |
+
global ledger
|
40 |
+
try:
|
41 |
+
# Filter the ledger for the selected head
|
42 |
+
filtered_ledger = ledger[ledger["Head"] == head]
|
43 |
+
|
44 |
+
# Calculate the total for the selected head
|
45 |
+
total_amount = filtered_ledger["Amount"].sum()
|
46 |
+
|
47 |
+
return filtered_ledger, f"Total for {head}: {total_amount}"
|
48 |
+
|
49 |
+
except Exception as e:
|
50 |
+
print(f"Error during search by head: {e}")
|
51 |
+
return pd.DataFrame(), "Error during search by head"
|
52 |
+
|
53 |
+
# Function to generate a PDF report for a specific head
|
54 |
+
def generate_ledger_pdf(head):
|
55 |
+
global ledger
|
56 |
try:
|
57 |
# Use a temporary directory to save the file
|
58 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
|
|
62 |
pdf = FPDF()
|
63 |
pdf.add_page()
|
64 |
pdf.set_font("Arial", 'B', 16)
|
65 |
+
pdf.cell(200, 10, txt=f"Ledger Report for {head}", ln=True, align="C")
|
66 |
pdf.set_font("Arial", size=12)
|
67 |
current_date = datetime.now().strftime("%d/%m/%Y")
|
68 |
pdf.cell(200, 10, txt=f"Report Date: {current_date}", ln=True, align="C")
|
69 |
pdf.ln(10)
|
70 |
|
71 |
# Table headers
|
72 |
+
pdf.cell(40, 10, txt="Company Name", border=1, align="C")
|
73 |
+
pdf.cell(40, 10, txt="SP/Station", border=1, align="C")
|
74 |
+
pdf.cell(30, 10, txt="Invoice No", border=1, align="C")
|
75 |
pdf.cell(30, 10, txt="Date", border=1, align="C")
|
76 |
+
pdf.cell(50, 10, txt="Amount", border=1, ln=True, align="C")
|
77 |
|
78 |
+
# Filter records by head
|
79 |
+
filtered_ledger = ledger[ledger["Head"] == head]
|
80 |
|
81 |
+
for _, row in filtered_ledger.iterrows():
|
82 |
+
pdf.cell(40, 10, txt=str(row['Company Name']), border=1, align="C")
|
83 |
+
pdf.cell(40, 10, txt=str(row['SP/Station']), border=1, align="C")
|
84 |
+
pdf.cell(30, 10, txt=str(row['Invoice No']), border=1, align="C")
|
85 |
pdf.cell(30, 10, txt=str(row['Date']), border=1, align="C")
|
86 |
+
pdf.cell(50, 10, txt=str(row['Amount']), border=1, ln=True, align="C")
|
87 |
|
88 |
+
# Total amount for the head
|
89 |
+
total_amount = filtered_ledger["Amount"].sum()
|
90 |
+
pdf.cell(140, 10, txt="Total Amount", border=1, align="R")
|
91 |
+
pdf.cell(50, 10, txt=str(total_amount), border=1, ln=True, align="C")
|
92 |
|
93 |
# Save PDF file
|
94 |
pdf.output(pdf_file_path)
|
|
|
102 |
|
103 |
# Gradio interface
|
104 |
with gr.Blocks() as app:
|
105 |
+
# Section for adding new invoices with Head and Company Name fields
|
106 |
with gr.Row():
|
107 |
+
head = gr.Textbox(label="Head")
|
108 |
company_name = gr.Textbox(label="Company Name")
|
109 |
+
sp_station = gr.Textbox(label="SP/Station")
|
110 |
invoice_no = gr.Textbox(label="Invoice No")
|
111 |
date = gr.Textbox(label="Date")
|
112 |
amount = gr.Number(label="Amount")
|
113 |
add_button = gr.Button("Add Invoice")
|
114 |
|
115 |
+
# Section for searching and displaying ledger by head
|
116 |
with gr.Row():
|
117 |
+
search_head = gr.Textbox(label="Search by Head")
|
118 |
+
search_button = gr.Button("Search Ledger")
|
119 |
+
ledger_records = gr.DataFrame(label="Ledger Records")
|
120 |
+
total_display = gr.Textbox(label="Total Amount", interactive=False)
|
121 |
|
122 |
# Section for generating and displaying PDF link
|
123 |
with gr.Row():
|
|
|
125 |
pdf_output = gr.File(label="Download PDF")
|
126 |
|
127 |
# Define button functionalities
|
128 |
+
add_button.click(add_invoice, [head, company_name, sp_station, invoice_no, date, amount], [ledger_records, total_display])
|
129 |
|
130 |
+
def search_ledger(head):
|
131 |
+
filtered_ledger, total = search_by_head(head)
|
132 |
+
return filtered_ledger, total
|
133 |
+
|
134 |
+
search_button.click(search_ledger, [search_head], [ledger_records, total_display])
|
135 |
+
|
136 |
+
def generate_pdf(head):
|
137 |
+
pdf_path = generate_ledger_pdf(head)
|
138 |
if pdf_path:
|
139 |
return pdf_path
|
140 |
else:
|
141 |
return None
|
142 |
|
143 |
+
generate_pdf_button.click(generate_pdf, [search_head], [pdf_output])
|
144 |
|
145 |
# Launch the app
|
146 |
app.launch(share=True)
|