Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,66 +3,107 @@ import pandas as pd
|
|
| 3 |
from tinydb import TinyDB, Query
|
| 4 |
from datetime import datetime
|
| 5 |
|
| 6 |
-
# Initialize
|
| 7 |
db = TinyDB("db.json")
|
| 8 |
buyers_table = db.table("buyers")
|
| 9 |
payments_table = db.table("payments")
|
| 10 |
|
| 11 |
-
# Functions
|
| 12 |
-
def add_buyer(name, total_due):
|
| 13 |
-
buyers_table.insert({
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def record_payment(buyer_name, amount):
|
| 17 |
buyer = Query()
|
| 18 |
old_paid = buyers_table.get(buyer.name == buyer_name)["paid"]
|
| 19 |
-
buyers_table.update({
|
|
|
|
|
|
|
|
|
|
| 20 |
payments_table.insert({"buyer": buyer_name, "amount": amount, "date": str(datetime.now())})
|
| 21 |
-
return "Payment recorded!"
|
| 22 |
|
| 23 |
def dashboard():
|
| 24 |
buyers = pd.DataFrame(buyers_table.all())
|
| 25 |
if buyers.empty:
|
| 26 |
-
return "No buyers yet."
|
| 27 |
buyers["remaining"] = buyers["total_due"] - buyers["paid"]
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def generate_receipt(buyer_name, amount):
|
| 32 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
# Gradio
|
| 35 |
with gr.Blocks() as demo:
|
| 36 |
-
gr.Markdown("##
|
| 37 |
-
|
| 38 |
with gr.Tab("Add Buyer"):
|
| 39 |
name_input = gr.Textbox(label="Buyer Name")
|
| 40 |
total_input = gr.Number(label="Total Due")
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
add_btn.
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
with gr.Tab("Record Payment"):
|
| 46 |
-
buyers_list = [b["name"] for b in buyers_table.all()]
|
| 47 |
-
if not buyers_list:
|
| 48 |
-
buyers_list = ["No buyers yet"]
|
| 49 |
buyer_select = gr.Dropdown(choices=buyers_list, label="Select Buyer")
|
| 50 |
amount_input = gr.Number(label="Payment Amount")
|
| 51 |
-
pay_btn = gr.Button("Submit Payment")
|
| 52 |
-
|
| 53 |
-
pay_btn.click(record_payment, inputs=[buyer_select, amount_input], outputs=
|
| 54 |
-
|
| 55 |
with gr.Tab("Dashboard"):
|
| 56 |
-
dash_btn = gr.Button("Refresh Dashboard")
|
| 57 |
-
dash_table = gr.Dataframe()
|
| 58 |
dash_text = gr.Textbox()
|
| 59 |
-
|
| 60 |
-
|
|
|
|
| 61 |
with gr.Tab("Receipt"):
|
| 62 |
rec_buyer = gr.Textbox(label="Buyer Name")
|
| 63 |
rec_amount = gr.Number(label="Amount Paid")
|
| 64 |
-
rec_btn = gr.Button("Generate Receipt")
|
| 65 |
rec_output = gr.Textbox()
|
| 66 |
rec_btn.click(generate_receipt, inputs=[rec_buyer, rec_amount], outputs=rec_output)
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
| 3 |
from tinydb import TinyDB, Query
|
| 4 |
from datetime import datetime
|
| 5 |
|
| 6 |
+
# Initialize database
|
| 7 |
db = TinyDB("db.json")
|
| 8 |
buyers_table = db.table("buyers")
|
| 9 |
payments_table = db.table("payments")
|
| 10 |
|
| 11 |
+
# --- Functions ---
|
| 12 |
+
def add_buyer(name, total_due, guarantor1, guarantor2):
|
| 13 |
+
buyers_table.insert({
|
| 14 |
+
"name": name,
|
| 15 |
+
"total_due": total_due,
|
| 16 |
+
"paid": 0.0,
|
| 17 |
+
"guarantor1": guarantor1,
|
| 18 |
+
"guarantor2": guarantor2,
|
| 19 |
+
"last_payment": None
|
| 20 |
+
})
|
| 21 |
+
return f"β
Buyer '{name}' added successfully!"
|
| 22 |
|
| 23 |
def record_payment(buyer_name, amount):
|
| 24 |
buyer = Query()
|
| 25 |
old_paid = buyers_table.get(buyer.name == buyer_name)["paid"]
|
| 26 |
+
buyers_table.update({
|
| 27 |
+
"paid": old_paid + amount,
|
| 28 |
+
"last_payment": str(datetime.now())
|
| 29 |
+
}, buyer.name == buyer_name)
|
| 30 |
payments_table.insert({"buyer": buyer_name, "amount": amount, "date": str(datetime.now())})
|
| 31 |
+
return f"π° Payment of {amount} recorded for {buyer_name}!"
|
| 32 |
|
| 33 |
def dashboard():
|
| 34 |
buyers = pd.DataFrame(buyers_table.all())
|
| 35 |
if buyers.empty:
|
| 36 |
+
return "No buyers yet.", None
|
| 37 |
buyers["remaining"] = buyers["total_due"] - buyers["paid"]
|
| 38 |
+
|
| 39 |
+
# Alert for monthly pending payments
|
| 40 |
+
alerts = []
|
| 41 |
+
now_month = datetime.now().month
|
| 42 |
+
for _, row in buyers.iterrows():
|
| 43 |
+
if row["last_payment"] is None:
|
| 44 |
+
alerts.append(f"{row['name']} has not paid yet this month!")
|
| 45 |
+
else:
|
| 46 |
+
last_month = datetime.fromisoformat(row["last_payment"]).month
|
| 47 |
+
if last_month < now_month:
|
| 48 |
+
alerts.append(f"{row['name']} missed payment this month!")
|
| 49 |
+
|
| 50 |
+
# Build interactive table with Plotly
|
| 51 |
+
import plotly.graph_objects as go
|
| 52 |
+
fig = go.Figure(data=[go.Table(
|
| 53 |
+
header=dict(values=["Buyer","Guarantor1","Guarantor2","Total Due","Paid","Remaining"],
|
| 54 |
+
fill_color='paleturquoise',
|
| 55 |
+
align='center'),
|
| 56 |
+
cells=dict(values=[
|
| 57 |
+
buyers["name"], buyers["guarantor1"], buyers["guarantor2"],
|
| 58 |
+
buyers["total_due"], buyers["paid"], buyers["remaining"]
|
| 59 |
+
],
|
| 60 |
+
fill_color='lavender',
|
| 61 |
+
align='center'))
|
| 62 |
+
])
|
| 63 |
+
|
| 64 |
+
return "\n".join(alerts) if alerts else "β
All payments up-to-date", fig
|
| 65 |
|
| 66 |
def generate_receipt(buyer_name, amount):
|
| 67 |
+
return f"""
|
| 68 |
+
π§Ύ Receipt
|
| 69 |
+
Buyer: {buyer_name}
|
| 70 |
+
Amount Paid: {amount}
|
| 71 |
+
Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
| 72 |
+
"""
|
| 73 |
|
| 74 |
+
# --- Gradio Interface ---
|
| 75 |
with gr.Blocks() as demo:
|
| 76 |
+
gr.Markdown("## π³ Installment Management Dashboard (Enhanced)")
|
| 77 |
+
|
| 78 |
with gr.Tab("Add Buyer"):
|
| 79 |
name_input = gr.Textbox(label="Buyer Name")
|
| 80 |
total_input = gr.Number(label="Total Due")
|
| 81 |
+
guarantor1_input = gr.Textbox(label="Guarantor 1 Name")
|
| 82 |
+
guarantor2_input = gr.Textbox(label="Guarantor 2 Name")
|
| 83 |
+
add_btn = gr.Button("β Add Buyer", elem_id="animated-btn")
|
| 84 |
+
add_output = gr.Textbox()
|
| 85 |
+
add_btn.click(add_buyer, inputs=[name_input, total_input, guarantor1_input, guarantor2_input], outputs=add_output)
|
| 86 |
+
|
| 87 |
with gr.Tab("Record Payment"):
|
| 88 |
+
buyers_list = [b["name"] for b in buyers_table.all()] or ["No buyers yet"]
|
|
|
|
|
|
|
| 89 |
buyer_select = gr.Dropdown(choices=buyers_list, label="Select Buyer")
|
| 90 |
amount_input = gr.Number(label="Payment Amount")
|
| 91 |
+
pay_btn = gr.Button("π΅ Submit Payment", elem_id="animated-btn")
|
| 92 |
+
pay_output = gr.Textbox()
|
| 93 |
+
pay_btn.click(record_payment, inputs=[buyer_select, amount_input], outputs=pay_output)
|
| 94 |
+
|
| 95 |
with gr.Tab("Dashboard"):
|
| 96 |
+
dash_btn = gr.Button("π Refresh Dashboard", elem_id="animated-btn")
|
|
|
|
| 97 |
dash_text = gr.Textbox()
|
| 98 |
+
dash_table = gr.Plot()
|
| 99 |
+
dash_btn.click(dashboard, outputs=[dash_text, dash_table])
|
| 100 |
+
|
| 101 |
with gr.Tab("Receipt"):
|
| 102 |
rec_buyer = gr.Textbox(label="Buyer Name")
|
| 103 |
rec_amount = gr.Number(label="Amount Paid")
|
| 104 |
+
rec_btn = gr.Button("π§Ύ Generate Receipt", elem_id="animated-btn")
|
| 105 |
rec_output = gr.Textbox()
|
| 106 |
rec_btn.click(generate_receipt, inputs=[rec_buyer, rec_amount], outputs=rec_output)
|
| 107 |
|
| 108 |
+
# --- CSS Animation for Buttons ---
|
| 109 |
+
demo.launch(share=True, server_name="0.0.0.0", server_port=7860, inbrowser=False)
|