58 lines
2.2 KiB
Python
58 lines
2.2 KiB
Python
import json
|
|
import os
|
|
from datetime import datetime
|
|
from .logger import log_error
|
|
|
|
class BanqueManager:
|
|
def __init__(self):
|
|
|
|
self.file_path = "comptes_bancaires.json"
|
|
self.comptes = {}
|
|
self.load_data()
|
|
|
|
def load_data(self):
|
|
"""Charge les données bancaires depuis un fichier JSON."""
|
|
if os.path.exists(self.file_path):
|
|
try:
|
|
with open(self.file_path, "r", encoding="utf-8") as f:
|
|
data = f.read().strip()
|
|
self.comptes = json.loads(data) if data else {}
|
|
except json.JSONDecodeError:
|
|
log_error("Erreur de lecture dans comptes_bancaires.json, réinitialisation...")
|
|
self.comptes = {}
|
|
self.save_data()
|
|
|
|
def save_data(self):
|
|
"""Sauvegarde les comptes et transactions dans comptes_bancaires.json."""
|
|
with open(self.file_path, "w", encoding="utf-8") as f:
|
|
json.dump(self.comptes, f, indent=4, ensure_ascii=False)
|
|
|
|
def enregistrer_transaction(self, emetteur_id, destinataire_id, montant, type_transaction):
|
|
"""
|
|
Enregistre une transaction dans l'historique et recharge les données en temps réel.
|
|
"""
|
|
self.load_data()
|
|
|
|
transaction = {
|
|
"date": datetime.now().strftime("%d/%m/%Y %H:%M:%S"),
|
|
"type": type_transaction,
|
|
"montant": montant,
|
|
"emetteur": emetteur_id,
|
|
"destinataire": destinataire_id
|
|
}
|
|
|
|
if emetteur_id not in self.comptes:
|
|
self.comptes[emetteur_id] = {"solde": 0, "prets": [], "transactions": []}
|
|
elif "transactions" not in self.comptes[emetteur_id]:
|
|
self.comptes[emetteur_id]["transactions"] = []
|
|
|
|
self.comptes[emetteur_id]["transactions"].append(transaction)
|
|
|
|
if destinataire_id not in self.comptes:
|
|
self.comptes[destinataire_id] = {"solde": 0, "prets": [], "transactions": []}
|
|
elif "transactions" not in self.comptes[destinataire_id]:
|
|
self.comptes[destinataire_id]["transactions"] = []
|
|
|
|
self.comptes[destinataire_id]["transactions"].append(transaction)
|
|
|
|
self.save_data()
|