File size: 5,453 Bytes
a6ebfad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, render_template, request, redirect, url_for, send_file, flash
import sqlite3
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
import io
from datetime import datetime

app = Flask(__name__)
app.secret_key = 'your_secret_key'  # Needed for flashing messages

def init_db():
    conn = sqlite3.connect('pycash.db')
    c = conn.cursor()
    c.execute('''

        CREATE TABLE IF NOT EXISTS transactions (

            id INTEGER PRIMARY KEY AUTOINCREMENT,

            type TEXT,

            amount REAL,

            recipient TEXT,

            date TEXT,

            memo TEXT

        )

    ''')
    c.execute('''

        CREATE TABLE IF NOT EXISTS balance (

            id INTEGER PRIMARY KEY,

            total REAL

        )

    ''')
    c.execute('SELECT COUNT(*) FROM balance')
    if c.fetchone()[0] == 0:
        c.execute('INSERT INTO balance (id, total) VALUES (1, 0)')
    conn.commit()
    conn.close()

init_db()

def update_balance(amount, operation):
    conn = sqlite3.connect('pycash.db')
    c = conn.cursor()
    c.execute('SELECT total FROM balance WHERE id = 1')
    current_balance = c.fetchone()[0]
    new_balance = current_balance + amount if operation == 'deposit' else current_balance - amount
    c.execute('UPDATE balance SET total = ? WHERE id = 1', (new_balance,))
    conn.commit()
    conn.close()

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/add_payment', methods=['GET', 'POST'])
def add_payment():
    if request.method == 'POST':
        amount = float(request.form['dollars'])
        payment_to = request.form['paymentTo']
        date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        memo = request.form.get('memo', '')

        conn = sqlite3.connect('pycash.db')
        c = conn.cursor()
        c.execute('SELECT total FROM balance WHERE id = 1')
        current_balance = c.fetchone()[0]

        if amount > current_balance:
            flash('Insufficient funds!')
            return redirect(url_for('add_payment'))
        
        c.execute('''

            INSERT INTO transactions (type, amount, recipient, date, memo)

            VALUES (?, ?, ?, ?, ?)

        ''', ('Payment', amount, payment_to, date, memo))
        conn.commit()
        conn.close()

        update_balance(amount, 'payment')

        return redirect(url_for('index'))
    return render_template('add_payment.html')

@app.route('/add_deposit', methods=['GET', 'POST'])
def add_deposit():
    if request.method == 'POST':
        amount = float(request.form['dollars'])
        date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        memo = request.form.get('memo', '')

        conn = sqlite3.connect('pycash.db')
        c = conn.cursor()
        c.execute('''

            INSERT INTO transactions (type, amount, recipient, date, memo)

            VALUES (?, ?, ?, ?, ?)

        ''', ('Deposit', amount, '', date, memo))
        conn.commit()
        conn.close()

        update_balance(amount, 'deposit')

        return redirect(url_for('index'))
    return render_template('add_deposit.html')

@app.route('/view_finances', methods=['GET'])
def view_finances():
    conn = sqlite3.connect('pycash.db')
    c = conn.cursor()
    c.execute('SELECT * FROM transactions')
    transactions = c.fetchall()
    c.execute('SELECT total FROM balance WHERE id = 1')
    balance = c.fetchone()[0]
    conn.close()
    return render_template('view_finances.html', transactions=transactions, balance=balance)

@app.route('/generate_pdf')
def generate_pdf():
    conn = sqlite3.connect('pycash.db')
    c = conn.cursor()
    c.execute('SELECT * FROM transactions')
    transactions = c.fetchall()
    c.execute('SELECT total FROM balance WHERE id = 1')
    balance = c.fetchone()[0]
    conn.close()

    pdf_buffer = io.BytesIO()
    pdf = canvas.Canvas(pdf_buffer, pagesize=letter)
    width, height = letter

    pdf.setFont("Helvetica-Bold", 16)
    pdf.drawString(30, height - 30, "Transaction Statement")

    pdf.setFont("Helvetica-Bold", 12)
    pdf.drawString(30, height - 60, "Type")
    pdf.drawString(120, height - 60, "Amount")
    pdf.drawString(210, height - 60, "Date")
    pdf.drawString(300, height - 60, "From")

    y = height - 80
    pdf.setFont("Helvetica", 12)
    for transaction in transactions:
        transaction_type, amount, recipient, date, memo = transaction[1], transaction[2], transaction[3], transaction[4], transaction[5]
        pdf.drawString(30, y, transaction_type)
        pdf.drawString(120, y, f"₹{amount:.2f}")
        pdf.drawString(210, y, date)
        pdf.drawString(300, y, From)
        y -= 20

    pdf.setFont("Helvetica-Bold", 14)
    pdf.drawString(30, y - 20, f"Current Balance: ₹{balance:.2f}")

    pdf.save()
    pdf_buffer.seek(0)

    return send_file(pdf_buffer, as_attachment=True, download_name='statement.pdf', mimetype='application/pdf')

@app.route('/clear_transactions', methods=['POST'])
def clear_transactions():
    conn = sqlite3.connect('pycash.db')
    c = conn.cursor()
    c.execute('DELETE FROM transactions')
    c.execute('UPDATE balance SET total = 0 WHERE id = 1')
    conn.commit()
    conn.close()
    return redirect(url_for('index'))

if __name__ == '__main__':
    app.run(debug=True)