File size: 6,260 Bytes
f47fe7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from fastapi import FastAPI, HTTPException, Depends
from pydantic import BaseModel
from typing import List, Optional
import mysql.connector
from datetime import datetime
import os

app = FastAPI()

# === MySQL Connection ===
def get_connection():
    return mysql.connector.connect(
        host=os.getenv("Host"),
        user=os.getenv("User"),
        password=os.getenv("Pass"),
        database=os.getenv("DB"),
        port=os.getenv("Port")
    )

# === Pydantic Models ===

class UserCreate(BaseModel):
    name: str
    email: str
    password_hash: str

class AccountCreate(BaseModel):
    account_name: str
    account_code: str
    account_type: str  # A, C, L, E, I
    description: Optional[str] = None

class JournalLine(BaseModel):
    account_id: int
    is_debit: bool
    amount: float

class JournalEntryCreate(BaseModel):
    entry_date: str
    description: Optional[str] = ""
    created_by: int
    lines: List[JournalLine]

# === Endpoints ===

@app.get("/")
def root():
    return {"message": "ERP Accounting API is running."}


# -- Users --

@app.post("/users/")
def create_user(user: UserCreate):
    conn = get_connection()
    cursor = conn.cursor()
    try:
        cursor.execute("""
            INSERT INTO users (name, email, password_hash)
            VALUES (%s, %s, %s)
        """, (user.name, user.email, user.password_hash))
        conn.commit()
        return {"message": "User created successfully."}
    except mysql.connector.Error as e:
        raise HTTPException(status_code=400, detail=str(e))
    finally:
        cursor.close()
        conn.close()

# -- Accounts --

@app.post("/accounts/")
def create_account(account: AccountCreate):
    conn = get_connection()
    cursor = conn.cursor()
    try:
        cursor.execute("""
            INSERT INTO accounts (account_name, account_code, account_type, description)
            VALUES (%s, %s, %s, %s)
        """, (account.account_name, account.account_code, account.account_type, account.description))
        conn.commit()
        return {"message": "Account created successfully."}
    except mysql.connector.Error as e:
        raise HTTPException(status_code=400, detail=str(e))
    finally:
        cursor.close()
        conn.close()

@app.get("/accounts/")
def list_accounts():
    conn = get_connection()
    cursor = conn.cursor(dictionary=True)
    try:
        cursor.execute("SELECT * FROM accounts")
        return cursor.fetchall()
    finally:
        cursor.close()
        conn.close()

# -- Journal Entries --

@app.post("/journal/")
def create_journal(entry: JournalEntryCreate):
    conn = get_connection()
    cursor = conn.cursor()
    try:
        cursor.execute("""
            INSERT INTO journal_entries (entry_date, description, created_by)
            VALUES (%s, %s, %s)
        """, (entry.entry_date, entry.description, entry.created_by))
        journal_id = cursor.lastrowid

        for line in entry.lines:
            cursor.execute("""
                INSERT INTO journal_lines (journal_id, account_id, is_debit, amount)
                VALUES (%s, %s, %s, %s)
            """, (journal_id, line.account_id, line.is_debit, line.amount))

            # Also insert into ledger
            cursor.execute("""
                INSERT INTO ledger (account_id, journal_id, entry_date, debit_amount, credit_amount)
                VALUES (%s, %s, %s, %s, %s)
            """, (
                line.account_id,
                journal_id,
                entry.entry_date,
                line.amount if line.is_debit else 0,
                line.amount if not line.is_debit else 0
            ))

        conn.commit()
        return {"message": "Journal entry created."}
    except mysql.connector.Error as e:
        conn.rollback()
        raise HTTPException(status_code=400, detail=str(e))
    finally:
        cursor.close()
        conn.close()

# -- Ledger View --

@app.get("/ledger/")
def get_ledger():
    conn = get_connection()
    cursor = conn.cursor(dictionary=True)
    try:
        cursor.execute("""
            SELECT l.entry_date, a.account_name, a.account_type,
                   l.debit_amount, l.credit_amount
            FROM ledger l
            JOIN accounts a ON l.account_id = a.account_id
            ORDER BY l.entry_date ASC
        """)
        return cursor.fetchall()
    finally:
        cursor.close()
        conn.close()

# -- Trial Balance --

@app.get("/trial-balance/")
def get_trial_balance():
    conn = get_connection()
    cursor = conn.cursor(dictionary=True)
    try:
        cursor.execute("""
            SELECT 
                a.account_name,
                a.account_type,
                SUM(l.debit_amount) AS total_debit,
                SUM(l.credit_amount) AS total_credit
            FROM ledger l
            JOIN accounts a ON l.account_id = a.account_id
            GROUP BY a.account_id
        """)
        return cursor.fetchall()
    finally:
        cursor.close()
        conn.close()

# -- Profit & Loss Statement --

@app.get("/report/pnl/")
def get_profit_and_loss():
    conn = get_connection()
    cursor = conn.cursor(dictionary=True)
    try:
        cursor.execute("""
            SELECT 
                a.account_name,
                SUM(l.debit_amount) AS total_debit,
                SUM(l.credit_amount) AS total_credit
            FROM ledger l
            JOIN accounts a ON l.account_id = a.account_id
            WHERE a.account_type IN ('I', 'E')
            GROUP BY a.account_id
        """)
        return cursor.fetchall()
    finally:
        cursor.close()
        conn.close()

# -- Balance Sheet --

@app.get("/report/balance-sheet/")
def get_balance_sheet():
    conn = get_connection()
    cursor = conn.cursor(dictionary=True)
    try:
        cursor.execute("""
            SELECT 
                a.account_name,
                a.account_type,
                SUM(l.debit_amount) AS total_debit,
                SUM(l.credit_amount) AS total_credit
            FROM ledger l
            JOIN accounts a ON l.account_id = a.account_id
            WHERE a.account_type IN ('A', 'L', 'C')
            GROUP BY a.account_id
        """)
        return cursor.fetchall()
    finally:
        cursor.close()
        conn.close()