Files
Moon-Household-Budget/assets.py
2026-06-06 14:22:57 -05:00

87 lines
2.3 KiB
Python

import json
import os
DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
ASSETS_FILE = os.path.join(DATA_DIR, "assets.json")
DEFAULT_CARS = [
{
"id": "kia-forte",
"type": "personal",
"year": 2012,
"make": "Kia",
"model": "Forte 5 SX",
"trim": "2.4L",
"color": "",
"owner": "Bonna",
"purchase_price": 0,
"purchase_date": "",
"estimated_value": 0,
"notes": "",
"gas_log": [],
"maintenance_log": [],
},
{
"id": "silverado",
"type": "personal",
"year": 2009,
"make": "Chevy",
"model": "Silverado LTZ",
"trim": "5.3L",
"color": "",
"owner": "Tony",
"purchase_price": 0,
"purchase_date": "",
"estimated_value": 0,
"notes": "",
"gas_log": [],
"maintenance_log": [],
},
]
ASSET_CATEGORIES = ["Investments", "Real Estate", "Equipment", "Other Valuables"]
def load_assets():
if os.path.exists(ASSETS_FILE):
with open(ASSETS_FILE) as f:
data = json.load(f)
# migrate old format if needed
if "other_assets" not in data:
data["other_assets"] = []
return data
save_assets({"personal": DEFAULT_CARS, "flips": [], "other_assets": []})
return {"personal": DEFAULT_CARS, "flips": [], "other_assets": []}
def save_assets(data):
with open(ASSETS_FILE, "w") as f:
json.dump(data, f, indent=2)
def car_display_name(car):
return f"{car['year']} {car['make']} {car['model']}"
def total_spent(car):
gas = sum(float(e.get("amount", 0)) for e in car.get("gas_log", []))
maintenance = sum(float(e.get("amount", 0)) for e in car.get("maintenance_log", []))
return round(gas + maintenance, 2)
def flip_profit(flip):
invested = float(flip.get("purchase_price", 0))
repairs = sum(float(e.get("amount", 0)) for e in flip.get("expense_log", []))
sale = float(flip.get("sale_price", 0))
total_in = invested + repairs
if sale > 0:
return round(sale - total_in, 2)
return None
def flip_total_invested(flip):
invested = float(flip.get("purchase_price", 0))
repairs = sum(float(e.get("amount", 0)) for e in flip.get("expense_log", []))
return round(invested + repairs, 2)