V2.0: Refonte système bancaire - DB SQLite, RIB FR, TVA auto, multi-banques
- Remplace JSON par SQLite (aiosqlite) pour atomicité et intégrité - Nouveau format RIB français avec checksum modulo 97 - Chaque banque a son propre rôle Discord (isolation par serveur) - TVA automatique Particulier -> Entreprise, redirection au Fisc - Institutions gouvernementales avec droit d'audit optionnel - Commandes: /gouv, /banque, /audit, /mes-ribs - Supprime: 16 anciens fichiers (entreprises, FBM, URSAFF, etc.) - Reset complet des données, repart de zéro
This commit is contained in:
parent
2e38d05152
commit
c9ad155bf6
38 changed files with 3689 additions and 5106 deletions
82
utils/db.py
Normal file
82
utils/db.py
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
import aiosqlite
|
||||
import os
|
||||
|
||||
DB_PATH = os.path.join(os.path.dirname(__file__), "..", "data", "bank_v2.db")
|
||||
|
||||
SCHEMA = """
|
||||
CREATE TABLE IF NOT EXISTS banks (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
guild_id INTEGER NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
role_id INTEGER NOT NULL,
|
||||
establishment_code INTEGER NOT NULL,
|
||||
vault_balance REAL DEFAULT 0.0,
|
||||
clients_total_balance REAL DEFAULT 0.0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(guild_id, name),
|
||||
UNIQUE(guild_id, establishment_code)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS accounts (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
guild_id INTEGER NOT NULL,
|
||||
bank_id INTEGER NOT NULL,
|
||||
user_id INTEGER NOT NULL,
|
||||
rib TEXT NOT NULL UNIQUE,
|
||||
account_type TEXT NOT NULL CHECK(account_type IN ('particulier', 'entreprise')),
|
||||
solde REAL DEFAULT 0.0,
|
||||
tva_rate REAL DEFAULT 0.0,
|
||||
password TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (bank_id) REFERENCES banks(id) ON DELETE CASCADE,
|
||||
UNIQUE(guild_id, bank_id, user_id, account_type)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS institutions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
guild_id INTEGER NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
role_id INTEGER,
|
||||
can_audit_all INTEGER DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(guild_id, name)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS fisc_config (
|
||||
guild_id INTEGER PRIMARY KEY,
|
||||
institution_id INTEGER,
|
||||
FOREIGN KEY (institution_id) REFERENCES institutions(id) ON DELETE SET NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS transactions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
guild_id INTEGER NOT NULL,
|
||||
emetteur_rib TEXT,
|
||||
destinataire_rib TEXT,
|
||||
emetteur_id INTEGER,
|
||||
destinataire_id INTEGER,
|
||||
montant REAL NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
metadata TEXT
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_accounts_guild_user ON accounts(guild_id, user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_accounts_rib ON accounts(rib);
|
||||
CREATE INDEX IF NOT EXISTS idx_accounts_bank ON accounts(guild_id, bank_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_transactions_guild ON transactions(guild_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_transactions_ribs ON transactions(emetteur_rib, destinataire_rib);
|
||||
CREATE INDEX IF NOT EXISTS idx_banks_guild ON banks(guild_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_institutions_guild ON institutions(guild_id);
|
||||
"""
|
||||
|
||||
|
||||
async def get_db() -> aiosqlite.Connection:
|
||||
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
||||
db = await aiosqlite.connect(DB_PATH)
|
||||
db.row_factory = aiosqlite.Row
|
||||
await db.execute("PRAGMA journal_mode=WAL")
|
||||
await db.execute("PRAGMA foreign_keys=ON")
|
||||
await db.executescript(SCHEMA)
|
||||
await db.commit()
|
||||
return db
|
||||
Loading…
Add table
Add a link
Reference in a new issue