Vicy_Publique/commandes/virement_entreprise.py
2026-08-16 19:37:15 +02:00

149 lines
5.7 KiB
Python

import discord
from discord import app_commands
from discord.ext import commands
import json
from datetime import datetime
class VirementEntreprise(commands.Cog):
def __init__(self, bot):
self.bot = bot
banque_choices = [
app_commands.Choice(name="Victoria's Credit Bank", value="Victoria's Credit Bank"),
app_commands.Choice(name="NGC Bank", value="NGC Bank"),
]
@app_commands.command(name="virement_entreprise", description="Effectuer un virement depuis une entreprise vers un RIB.")
@app_commands.choices(banque=banque_choices)
async def virement_entreprise(
self,
interaction: discord.Interaction,
rib_entreprise: str,
montant: float,
mot_de_passe: str,
rib_destinataire: str,
banque: app_commands.Choice[str]
):
banque_nom = banque.value
# Chargement des données
try:
with open("comptes_entreprises.json", "r", encoding="utf-8") as f:
comptes_entreprises = json.load(f)
with open("comptes_bancaires.json", "r", encoding="utf-8") as f:
comptes_bancaires = json.load(f)
except Exception:
await interaction.response.send_message("❌ Erreur de lecture des données bancaires.", ephemeral=True)
return
# 🔐 Vérification de l'émetteur
entreprise_id = None
for nom_entreprise, infos in comptes_entreprises.get(banque_nom, {}).items():
if infos.get("rib") == rib_entreprise:
if infos.get("mdp") != mot_de_passe:
await interaction.response.send_message("❌ Mot de passe incorrect.", ephemeral=True)
return
entreprise_id = nom_entreprise
break
if not entreprise_id:
await interaction.response.send_message("❌ Aucun compte entreprise trouvé avec ce RIB.", ephemeral=True)
return
compte_emetteur = comptes_entreprises[banque_nom][entreprise_id]
if compte_emetteur["solde"] < montant:
await interaction.response.send_message("❌ Solde insuffisant.", ephemeral=True)
return
# 🔎 Recherche du destinataire
destinataire_data = None
destinataire_type = None
destinataire_nom = None
banque_dest = None
for banque_cible, comptes in comptes_entreprises.items():
for nom, infos in comptes.items():
if infos.get("rib") == rib_destinataire:
destinataire_data = infos
destinataire_type = "entreprise"
destinataire_nom = nom
banque_dest = banque_cible
break
if destinataire_data:
break
if not destinataire_data:
for banque_cible, comptes in comptes_bancaires.items():
for uid, infos in comptes.items():
if infos.get("rib") == rib_destinataire:
destinataire_data = infos
destinataire_type = "particulier"
destinataire_nom = uid
banque_dest = banque_cible
break
if destinataire_data:
break
if not destinataire_data:
await interaction.response.send_message("❌ RIB destinataire introuvable.", ephemeral=True)
return
# 📉 Débit du compte entreprise
compte_emetteur["solde"] -= montant
now = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
compte_emetteur.setdefault("transactions", []).append({
"date": now,
"type": "Virement",
"source": f"Vers RIB {rib_destinataire}",
"montant": -montant
})
# 📈 Crédit du destinataire (avec TVA si entreprise)
montant_net = montant
tva_envoyee = 0
if destinataire_type == "entreprise":
tva = destinataire_data.get("tva", 0)
tva_envoyee = round(montant * tva / 100, 2)
montant_net = montant - tva_envoyee
# Crédit TVA à l'État
if "Gouvernement" not in comptes_entreprises:
comptes_entreprises["Gouvernement"] = {}
if "GOUV" not in comptes_entreprises["Gouvernement"]:
comptes_entreprises["Gouvernement"]["GOUV"] = {
"rib": "00000 00000 00000000000 00",
"solde": 0.0,
"transactions": []
}
comptes_entreprises["Gouvernement"]["GOUV"]["solde"] += tva_envoyee
comptes_entreprises["Gouvernement"]["GOUV"]["transactions"].append({
"date": now,
"type": "TVA reçue",
"source": entreprise_id,
"montant": tva_envoyee
})
destinataire_data["solde"] += montant_net
destinataire_data.setdefault("transactions", []).append({
"date": now,
"type": "Virement reçu",
"source": entreprise_id,
"montant": montant_net
})
# Sauvegardes
with open("comptes_entreprises.json", "w", encoding="utf-8") as f:
json.dump(comptes_entreprises, f, ensure_ascii=False, indent=4)
with open("comptes_bancaires.json", "w", encoding="utf-8") as f:
json.dump(comptes_bancaires, f, ensure_ascii=False, indent=4)
await interaction.response.send_message(
f"✅ Virement effectué avec succès.{' TVA de ' + str(tva_envoyee) + '€ envoyée au Gouvernement.' if tva_envoyee else ''}",
ephemeral=True
)
async def setup(bot):
await bot.add_cog(VirementEntreprise(bot))