-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
59 lines (46 loc) · 1.65 KB
/
model.py
File metadata and controls
59 lines (46 loc) · 1.65 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
import json
import os
from datetime import datetime
DATA_FILE = "data.json"
class FinanaceModel:
def __init__(self):
self.transactions = []
self.budgets = {}
self.load()
# Transactions
def add_transaction(self, type_, amount, category, description, date = None, recurrence = None):
if date == None:
date = datetime.today().strftime("%Y-%m-%d")
self.transactions.append({
"type": type_,
"amount": float(amount),
"category": category,
"description": description,
"date": date,
"recurrence": recurrence
})
self.save()
# Amounts
def total_income(self):
return sum(t["amount"] for t in self.transactions if t["type"] == "Income")
def total_expense(self):
return sum(t["amount"] for t in self.transactions if t["type"] == "Expense")
def balance(self):
return self.total_income() - self.total_expense()
# Budget
def set_budget(self, category, amount):
self.budgets[category] = amount
self.save()
def save(self):
data = {
"transactions": self.transactions,
"budgets": self.budgets
}
with open(DATA_FILE, "w") as f:
json.dump(data, f)
def load(self):
if os.path.exists(DATA_FILE):
with open(DATA_FILE, "r") as f:
data = json.load(f)
self.transactions = data["transactions"]
self.budgets = data["budgets"]