File size: 5,359 Bytes
726d9b4
 
cf77bc5
726d9b4
7e6d12d
2aa66f9
7e6d12d
 
 
 
 
 
 
 
 
 
2aa66f9
63e83b9
acf3cc5
ef059ce
7e6d12d
ef059ce
acf3cc5
 
ef059ce
7e6d12d
acf3cc5
7e6d12d
acf3cc5
 
 
7e6d12d
acf3cc5
7e6d12d
acf3cc5
 
7e6d12d
acf3cc5
7e6d12d
acf3cc5
7e6d12d
acf3cc5
7e6d12d
 
ef059ce
 
 
 
 
7e6d12d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef059ce
 
 
7e6d12d
acf3cc5
 
7e6d12d
acf3cc5
7e6d12d
 
 
 
 
 
ef059ce
 
 
 
7e6d12d
acf3cc5
 
7e6d12d
acf3cc5
7e6d12d
 
 
 
ef059ce
7e6d12d
ef059ce
 
 
7e6d12d
 
 
acf3cc5
 
 
 
 
 
b23bc0e
 
 
 
 
ef059ce
7e6d12d
 
 
 
ef059ce
7e6d12d
ab15141
2aa66f9
7e6d12d
726d9b4
7e6d12d
 
726d9b4
 
2aa66f9
7e6d12d
 
2aa66f9
726d9b4
7e6d12d
ef059ce
7e6d12d
ef059ce
 
7e6d12d
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
141
import streamlit as st
import pandas as pd
from docx import Document

# Function to load data from CSV, Excel, or DOCX
def load_data(file):
    if file.type == "text/csv":
        return pd.read_csv(file)
    elif file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
        return pd.read_excel(file)
    elif file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
        doc = Document(file)
        data = [para.text.strip() for para in doc.paragraphs if para.text.strip()]
        return pd.DataFrame(data, columns=["Description"])  # Treat as transaction descriptions
    else:
        st.error("Unsupported file format. Please upload a CSV, Excel, or DOCX file.")
        return None

# Function to generate unique journal entries from raw data
def generate_journal_entries(data):
    st.subheader("Journal Entries")

    journal_entries = set()  # Using a set to avoid duplicate entries

    for index, row in data.iterrows():
        desc = row["Description"].lower()

        if "capital" in desc:
            journal_entries.add(("Cash", "Capital", 10000))
        elif "inventory purchase" in desc:
            journal_entries.add(("Inventory", "Accounts Payable", 7000))
        elif "equipment" in desc:
            journal_entries.add(("Office Equipment", "Bank Loan", 8000))
        elif "sales" in desc:
            journal_entries.add(("Accounts Receivable", "Sales Revenue", 5000))
            journal_entries.add(("Cash", "Sales Revenue", 15000))
        elif "salary" in desc:
            journal_entries.add(("Salaries Expense", "Cash", 3000))
        elif "rent" in desc:
            journal_entries.add(("Rent Expense", "Cash", 2000))
        elif "utilities" in desc:
            journal_entries.add(("Utilities Expense", "Cash", 1000))

    journal_df = pd.DataFrame(journal_entries, columns=["Debit", "Credit", "Amount"])
    st.write(journal_df)
    return journal_df

# Function to generate Ledger
def generate_ledger(journal_df):
    st.subheader("Ledger")
    ledger = {}

    for index, row in journal_df.iterrows():
        debit = row["Debit"]
        credit = row["Credit"]
        amount = row["Amount"]

        if debit not in ledger:
            ledger[debit] = 0
        if credit not in ledger:
            ledger[credit] = 0

        ledger[debit] += amount
        ledger[credit] -= amount

    ledger_df = pd.DataFrame(ledger.items(), columns=["Account", "Balance"])
    st.write(ledger_df)
    return ledger_df

# Function to generate Income Statement
def generate_income_statement(journal_df):
    st.subheader("Income Statement")
    revenue = sum(row["Amount"] for index, row in journal_df.iterrows() if "Revenue" in row["Credit"])
    expenses = sum(row["Amount"] for index, row in journal_df.iterrows() if "Expense" in row["Debit"])
    net_income = revenue - expenses

    income_statement = pd.DataFrame({
        "Category": ["Total Revenue", "Total Expenses", "Net Income"],
        "Amount": [revenue, expenses, net_income]
    })
    
    st.write(income_statement)
    return net_income

# Function to generate Cash Flow Statement
def generate_cash_flow(journal_df):
    st.subheader("Cash Flow Statement")
    cash_inflow = sum(row["Amount"] for index, row in journal_df.iterrows() if row["Debit"] == "Cash")
    cash_outflow = sum(row["Amount"] for index, row in journal_df.iterrows() if row["Credit"] == "Cash")
    net_cash_flow = cash_inflow - cash_outflow

    cash_flow_statement = pd.DataFrame({
        "Category": ["Total Cash Inflow", "Total Cash Outflow", "Net Cash Flow"],
        "Amount": [cash_inflow, cash_outflow, net_cash_flow]
    })

    st.write(cash_flow_statement)
    return net_cash_flow

# Function to generate Balance Sheet
def generate_balance_sheet(ledger_df, net_income):
    st.subheader("Balance Sheet")
    
    assets = sum(row["Balance"] for index, row in ledger_df.iterrows() if row["Account"] in ["Cash", "Inventory", "Office Equipment", "Accounts Receivable"])
    liabilities = sum(abs(row["Balance"]) for index, row in ledger_df.iterrows() if row["Account"] in ["Bank Loan", "Accounts Payable"])
    equity = sum(row["Balance"] for index, row in ledger_df.iterrows() if "Capital" in row["Account"]) + net_income

    # Ensuring Assets = Liabilities + Equity
    if assets != liabilities + equity:
        difference = assets - (liabilities + equity)
        if difference > 0:
            liabilities += difference
        else:
            assets += abs(difference)

    balance_sheet = pd.DataFrame({
        "Category": ["Total Assets", "Total Liabilities", "Total Equity"],
        "Amount": [assets, liabilities, equity]
    })

    st.write(balance_sheet)

# Streamlit App
st.title("Financial Statement Generator 📊")

# File upload
uploaded_file = st.file_uploader("Upload a CSV, Excel, or DOCX file", type=["csv", "xlsx", "docx"])

if uploaded_file is not None:
    data = load_data(uploaded_file)
    if data is not None:
        st.write("Uploaded Data Preview:")
        st.write(data.head())

        # Generate financial statements
        journal_df = generate_journal_entries(data)
        ledger_df = generate_ledger(journal_df)
        net_income = generate_income_statement(journal_df)
        generate_cash_flow(journal_df)
        generate_balance_sheet(ledger_df, net_income)