Repository navigation
Expand file tree
/
Copy pathadd_trans.py
More file actions
140 lines (122 loc) Β· 6.44 KB
/
Copy pathadd_trans.py
File metadata and controls
140 lines (122 loc) Β· 6.44 KB
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
136
137
138
139
140
import streamlit as st
import pandas as pd
from sqlalchemy import text
from auth import logout, check_login
from database import conn
from config import SHOPS, NJC, TECH_CAFE, KRISHNA_KRRIPA, AMUL
def add_transaction_page(cookies):
check_login()
user = st.session_state.current_user
col1, col2 = st.columns([3, 1])
with col1:
st.title("πΈ Add Transaction")
st.write("Record a new lending or borrowing activity")
with col2:
if st.button("β¬
οΈ Back to Dashboard", use_container_width=True):
st.session_state.page = "dashboard"
st.rerun()
if st.button("πͺ Logout", use_container_width=True):
logout(cookies)
st.divider()
friends_query = f"""
SELECT
CASE
WHEN user1 = '{user}' THEN user2
ELSE user1
END as friend_name
FROM friends
WHERE user1 = '{user}' OR user2 = '{user}'
"""
users = conn.query(friends_query, ttl=0)['friend_name'].to_list()
with st.container(border=True):
st.markdown("### π Transaction Details")
transactee = st.selectbox("Select the person you want to transact with", users, placeholder="π§βπ€βπ§ Select Friend", index=None)
amount = 0
description = ""
food_item = ""
transaction = st.selectbox("Enter transaction / shop category", SHOPS, index=None, placeholder="πͺ Select Category")
if transaction == "NJC":
food_item = st.selectbox("Enter food item", list(NJC.keys()))
amount = NJC[food_item]
if amount == 0:
amount = st.number_input("Enter amount", min_value=1.00, max_value=10000.00, value=1.00, step=0.5)
elif transaction == "Tech Cafe":
food_item = st.selectbox("Enter food item", list(TECH_CAFE.keys()))
amount = TECH_CAFE[food_item]
if amount == 0:
amount = st.number_input("Enter amount", min_value=1.00, max_value=10000.00, value=1.00, step=0.5)
elif transaction == "Krishna Krripa":
food_item = st.selectbox("Enter food item", list(KRISHNA_KRRIPA.keys()))
amount = KRISHNA_KRRIPA[food_item]
if amount == 0:
amount = st.number_input("Enter amount", min_value=1.00, max_value=10000.00, value=1.00, step=0.5)
elif transaction == "Amul":
food_item = st.selectbox("Enter food item", list(AMUL.keys()))
amount = AMUL[food_item]
if amount == 0:
amount = st.number_input("Enter amount", min_value=9.00, max_value=10000.00, value=9.00, step=0.5)
elif transaction in ["BlinkIT", "Auto/Rentals", "Outside", "Others"]:
amount = st.number_input("Enter amount", min_value=1.00, max_value=10000.00, value=1.00, step=0.01)
description = st.text_input("Enter description / note", value="", placeholder="e.g. Movie tickets")
if transaction and transactee:
if st.button("β Save Transaction", type="primary"):
st.session_state.changes = amount
with conn.session as s:
query = '''INSERT INTO transactions (date_time, lender, borrower, shop_category, item, amount, description, status)
VALUES (CURRENT_TIMESTAMP, '{lender}', '{borrower}', '{shop}', '{item}', {amount}, '{description}', 'active')'''
s.execute(text(query.format(
lender=st.session_state.current_user,
borrower=transactee,
shop=transaction,
item=food_item,
amount=amount,
description=description
)))
s.commit()
st.success(f"Transaction recorded successfully! π")
st.markdown(f"""
<div style="padding:15px; border-radius:10px; border:1px solid #ddd;">
<h3>π Latest Transaction Details</h3>
<h4>π€ {transactee}</h4>
<p><b>Shop:</b> {transaction}</p>
{"<p><b>Item:</b> " + food_item + "</p>" if food_item else ""}
<p><b>Amount:</b> <span style="color:green; font-size:18px;">βΉ {amount}</span></p>
{"<p><b>Note:</b> " + description + "</p>" if description else ""}
</div>
""", unsafe_allow_html=True)
if transactee:
st.markdown(f"### π History with {transactee}")
query = f"""
SELECT date_time, lender, borrower, shop_category, item, amount, description, status
FROM transactions
WHERE (lender = '{user}' AND borrower = '{transactee}')
OR (lender = '{transactee}' AND borrower = '{user}')
ORDER BY date_time DESC
"""
df = conn.query(query, ttl=0)
if not df.empty:
lended = df[df['lender'] == user]['amount'].sum()
borrowed = df[df['borrower'] == user]['amount'].sum()
net_balance = lended - borrowed
row = st.container(horizontal=True)
with row:
st.metric("Total Lended π’", f":green[βΉ{lended:.2f}]", delta = st.session_state.changes, )
st.metric("Total Borrowed π΄", f":red[βΉ{borrowed:.2f}]")
st.metric("Net Balance βοΈ", f"βΉ{net_balance:.2f}", delta = st.session_state.changes)
st.dataframe(
df,
use_container_width=True,
hide_index=True,
column_config={
"date_time": st.column_config.DatetimeColumn("Date & Time β°", format="DD MMM YY, HH:mm"),
"lender": st.column_config.TextColumn("Lender πΌ"),
"borrower": st.column_config.TextColumn("Borrower π½"),
"shop_category": st.column_config.TextColumn("Shop πͺ"),
"item": st.column_config.TextColumn("Item ποΈ"),
"amount": st.column_config.NumberColumn("Amount πΈ", format="βΉ %.2f"),
"description": st.column_config.TextColumn("Note ποΈ"),
"status": st.column_config.TextColumn("Status π¦")
}
)
else:
st.info("No transaction history found with this person yet.")