-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript
More file actions
130 lines (109 loc) Β· 4.71 KB
/
script
File metadata and controls
130 lines (109 loc) Β· 4.71 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
import streamlit as st
import pandas as pd
from decimal import Decimal, ROUND_HALF_UP
import io
# Utility functions
def round_decimal(value):
return Decimal(value).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
def parse_amount(x):
try:
return Decimal(str(x).replace(',', ''))
except:
return Decimal('0.00')
def reconcile_ledger(statement_df, ledger_df):
statement = statement_df.to_dict(orient='records')
ledger = ledger_df.to_dict(orient='records')
unmatched = []
# Step 1: Cancel out contra entries
for s in statement:
s_amt = parse_amount(s['amount'])
for l in ledger:
l_amt = parse_amount(l['amount'])
if s_amt == l_amt and s['type'] != l['type'] and not l.get('matched') and not s.get('matched'):
s['matched'] = l['matched'] = True
break
if not s.get('matched'):
unmatched.append(s)
for l in ledger:
if not l.get('matched'):
unmatched.append(l)
net_total = Decimal('0.00')
for entry in unmatched:
amt = parse_amount(entry['amount'])
if entry['type'] == 'debit':
net_total += amt
else:
net_total -= amt
net_total = round_decimal(net_total)
vat = round_decimal(net_total * Decimal('0.15'))
total_with_vat = round_decimal(net_total + vat)
discount = round_decimal(total_with_vat * Decimal('0.025'))
final_payment = round_decimal(total_with_vat - discount)
return {
"net_total": net_total,
"vat_15%": vat,
"total_with_vat": total_with_vat,
"discount_2.5%": discount,
"total_payable": final_payment,
"unmatched": pd.DataFrame(unmatched)
}
# Streamlit App UI
st.set_page_config(page_title="Statement-Ledger Reconciliation Tool", layout="wide")
st.title("π Statement-Ledger Reconciliation Tool")
st.markdown("""
Upload your **Statement** and **Ledger** CSV files for automatic reconciliation.
Ensure both files have columns: `id`, `amount`, `type` (`debit` or `credit`).
""")
statement_file = st.file_uploader("π Upload Statement CSV", type=["csv"])
ledger_file = st.file_uploader("π Upload Ledger CSV", type=["csv"])
if statement_file and ledger_file:
try:
statement_df = pd.read_csv(statement_file)
ledger_df = pd.read_csv(ledger_file)
required_cols = {'id', 'amount', 'type'}
if required_cols.issubset(statement_df.columns) and required_cols.issubset(ledger_df.columns):
result = reconcile_ledger(statement_df, ledger_df)
unmatched_df = result['unmatched']
st.markdown("---")
st.subheader("π Reconciliation Breakdown")
col1, col2 = st.columns(2)
with col1:
st.metric("Net Total", f"R {result['net_total']}")
st.metric("VAT (15%)", f"R {result['vat_15%']}")
with col2:
st.metric("Total Incl. VAT", f"R {result['total_with_vat']}")
st.metric("Less Discount (2.5%)", f"- R {result['discount_2.5%']}")
st.markdown("### β
**Total Amount Payable**")
st.success(f"π° R {result['total_payable']}")
st.markdown("---")
st.subheader("π Unmatched Transactions")
st.dataframe(unmatched_df, use_container_width=True)
# CSV Export
csv_buffer = io.StringIO()
unmatched_df.to_csv(csv_buffer, index=False)
st.download_button(
label="β¬οΈ Download Unmatched (CSV)",
data=csv_buffer.getvalue(),
file_name="unmatched_entries.csv",
mime="text/csv"
)
# Excel Export
excel_buffer = io.BytesIO()
with pd.ExcelWriter(excel_buffer, engine='xlsxwriter') as writer:
unmatched_df.to_excel(writer, index=False, sheet_name='Unmatched Entries')
worksheet = writer.sheets['Unmatched Entries']
for i, col in enumerate(unmatched_df.columns):
col_len = unmatched_df[col].astype(str).map(len).max()
worksheet.set_column(i, i, col_len + 5)
st.download_button(
label="β¬οΈ Download Unmatched (Excel)",
data=excel_buffer.getvalue(),
file_name="unmatched_entries.xlsx",
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
else:
st.error("β Missing required columns: 'id', 'amount', 'type' in one or both files.")
except Exception as e:
st.error(f"β An error occurred during processing:\n\n{e}")
else:
st.info("π Please upload both Statement and Ledger CSV files to begin.")