File size: 4,500 Bytes
bc1c435
 
 
 
802eb89
bc1c435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
802eb89
bc1c435
 
802eb89
 
bc1c435
802eb89
 
 
 
bc1c435
802eb89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc1c435
 
 
802eb89
 
 
bc1c435
802eb89
 
 
 
bc1c435
 
 
802eb89
bc1c435
 
802eb89
bc1c435
 
802eb89
bc1c435
 
802eb89
bc1c435
 
802eb89
bc1c435
 
802eb89
bc1c435
802eb89
bc1c435
802eb89
 
bc1c435
 
 
802eb89
bc1c435
802eb89
 
bc1c435
 
 
 
802eb89
bc1c435
802eb89
 
bc1c435
 
 
 
 
 
 
802eb89
 
 
 
 
 
 
 
 
 
 
 
 
bc1c435
 
 
 
802eb89
bc1c435
 
 
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
import streamlit as st
import pandas as pd
from huggingface_hub import InferenceClient
import re
from datetime import datetime

# Initialize hosted inference client
client = InferenceClient(model="google/flan-t5-base")

# Simulated chart of accounts mapping
account_map = {
    "rent": "60001",
    "utilities": "60002",
    "capital": "30000",
    "cash": "10001",
    "bank": "10002",
    "sales": "40001",
    "supplies": "50001",
    "salary": "50002"
}

# Session state
if "gl_entries" not in st.session_state:
    st.session_state.gl_entries = []
if "company_name" not in st.session_state:
    st.session_state.company_name = "My Company"

# Streamlit UI
st.set_page_config(page_title="AI ERP App", layout="wide")
st.title(f"πŸ“˜ {st.session_state.company_name} Ledger - AI-Powered ERP")
prompt = st.text_input("πŸ“Œ Enter your accounting instruction:")

def extract_amount(prompt):
    match = re.search(r'\$?(\d{1,3}(,\d{3})*|\d+)(\.\d{1,2})?', prompt)
    return float(match.group().replace(',', '').replace('$', '')) if match else 0

def extract_date(prompt):
    date_match = re.search(r'(\d{1,2}[/-]\d{1,2}[/-]\d{2,4})|today', prompt.lower())
    if date_match:
        if "today" in date_match.group().lower():
            return datetime.today().strftime("%Y-%m-%d")
        try:
            return datetime.strptime(date_match.group(), "%d/%m/%Y").strftime("%Y-%m-%d")
        except:
            try:
                return datetime.strptime(date_match.group(), "%d-%m-%Y").strftime("%Y-%m-%d")
            except:
                return datetime.today().strftime("%Y-%m-%d")
    return datetime.today().strftime("%Y-%m-%d")

def handle_gl_entry(prompt):
    prompt_lower = prompt.lower()
    amount = extract_amount(prompt)
    date_str = extract_date(prompt)
    description = ""

    # Capture new company name
    company_match = re.search(r"company with a name of ([\w\s&.-]+)", prompt_lower)
    if company_match:
        st.session_state.company_name = company_match.group(1).strip().upper()

    if any(word in prompt_lower for word in ["invest", "capital", "start"]):
        description = "Owner Capital Contribution"
        debit_account, credit_account = "cash", "capital"
    elif "rent" in prompt_lower:
        description = "Rent Expense"
        debit_account, credit_account = "rent", "cash"
    elif "utilities" in prompt_lower:
        description = "Utilities Expense"
        debit_account, credit_account = "utilities", "cash"
    elif any(word in prompt_lower for word in ["sale", "revenue"]):
        description = "Sales Revenue"
        debit_account, credit_account = "cash", "sales"
    elif "supplies" in prompt_lower:
        description = "Supplies Purchase"
        debit_account, credit_account = "supplies", "cash"
    elif "salary" in prompt_lower or "payroll" in prompt_lower:
        description = "Salary Expense"
        debit_account, credit_account = "salary", "cash"
    else:
        return pd.DataFrame([{"Date": date_str, "Description": "Unrecognized Entry", "Account Code": "N/A", "Account Type": "N/A", "Debit": 0, "Credit": 0}])

    def format_code(name):
        return f"01-102-001-001-{account_map[name]}-000"

    entry = [
        {
            "Date": date_str,
            "Description": description,
            "Account Code": format_code(debit_account),
            "Account Type": debit_account.title(),
            "Debit": amount,
            "Credit": 0
        },
        {
            "Date": date_str,
            "Description": f"Offset for {description.lower()}",
            "Account Code": format_code(credit_account),
            "Account Type": credit_account.title(),
            "Debit": 0,
            "Credit": amount
        }
    ]
    st.session_state.gl_entries.extend(entry)
    return pd.DataFrame(entry)

# Buttons
col1, col2 = st.columns([1, 1])
with col1:
    st.download_button("πŸ“₯ Download All Entries (CSV)",
                       data=pd.DataFrame(st.session_state.gl_entries).to_csv(index=False),
                       file_name="gl_entries.csv",
                       mime="text/csv")
with col2:
    if st.button("πŸ—‘οΈ Delete All Records"):
        st.session_state.gl_entries = []
        st.success("βœ… All records have been deleted.")

# Handle input
if prompt:
    result = handle_gl_entry(prompt)
    st.dataframe(result)

# Display ledger
if st.session_state.gl_entries:
    st.subheader("πŸ“Š All Journal Entries")
    st.dataframe(pd.DataFrame(st.session_state.gl_entries))