-- ===================================================== -- Migration Stripe : is_striped → stripe_payment_id -- Date : Janvier 2025 -- Description : Refactoring pour simplifier la gestion des paiements Stripe -- ===================================================== -- 1. Modifier la table ope_pass -- ------------------------------ ALTER TABLE `ope_pass` DROP COLUMN IF EXISTS `chk_striped`; ALTER TABLE `ope_pass` ADD COLUMN `stripe_payment_id` VARCHAR(50) DEFAULT NULL COMMENT 'ID du PaymentIntent Stripe (pi_xxx)'; ALTER TABLE `ope_pass` ADD INDEX `idx_stripe_payment` (`stripe_payment_id`); -- 2. Modifier stripe_payment_history pour la rendre indépendante -- ---------------------------------------------------------------- -- Supprimer la clé étrangère vers stripe_payment_intents ALTER TABLE `stripe_payment_history` DROP FOREIGN KEY IF EXISTS `stripe_payment_history_ibfk_1`; -- Modifier la colonne pour stocker directement l'ID Stripe (totalement indépendante) ALTER TABLE `stripe_payment_history` DROP INDEX IF EXISTS `idx_fk_payment_intent`, CHANGE COLUMN `fk_payment_intent` `stripe_payment_intent_id` VARCHAR(255) DEFAULT NULL COMMENT 'ID du PaymentIntent Stripe', ADD INDEX `idx_stripe_payment_intent_id` (`stripe_payment_intent_id`); -- 3. Modifier stripe_refunds pour la rendre indépendante -- -------------------------------------------------------- ALTER TABLE `stripe_refunds` DROP FOREIGN KEY IF EXISTS `stripe_refunds_ibfk_1`; -- Modifier la colonne pour stocker directement l'ID Stripe (totalement indépendante) ALTER TABLE `stripe_refunds` DROP INDEX IF EXISTS `idx_fk_payment_intent`, CHANGE COLUMN `fk_payment_intent` `stripe_payment_intent_id` VARCHAR(255) NOT NULL COMMENT 'ID du PaymentIntent Stripe', ADD INDEX `idx_stripe_payment_intent_id` (`stripe_payment_intent_id`); -- 4. Supprimer la vue qui dépend de stripe_payment_intents -- ---------------------------------------------------------- DROP VIEW IF EXISTS `v_stripe_payment_stats`; -- 5. Supprimer la table stripe_payment_intents -- --------------------------------------------- DROP TABLE IF EXISTS `stripe_payment_intents`; -- 6. Créer une nouvelle vue basée sur ope_pass -- ---------------------------------------------- CREATE OR REPLACE VIEW `v_stripe_payment_stats` AS SELECT o.fk_entite, e.encrypted_name as entite_name, p.fk_user, CONCAT(u.first_name, ' ', u.sect_name) as user_name, COUNT(DISTINCT p.id) as total_ventes, COUNT(DISTINCT CASE WHEN p.stripe_payment_id IS NOT NULL THEN p.id END) as ventes_stripe, SUM(CASE WHEN p.stripe_payment_id IS NOT NULL THEN p.montant ELSE 0 END) as montant_stripe, SUM(CASE WHEN p.stripe_payment_id IS NULL THEN p.montant ELSE 0 END) as montant_autres, DATE(p.created_at) as date_vente FROM ope_pass p LEFT JOIN operations o ON p.fk_operation = o.id LEFT JOIN entites e ON o.fk_entite = e.id LEFT JOIN users u ON p.fk_user = u.id WHERE p.fk_type = 2 -- Type vente calendrier GROUP BY o.fk_entite, p.fk_user, DATE(p.created_at); -- 7. Vue pour les statistiques par entité uniquement -- ---------------------------------------------------- CREATE OR REPLACE VIEW `v_stripe_entite_stats` AS SELECT e.id as entite_id, e.encrypted_name as entite_name, sa.stripe_account_id, sa.charges_enabled, sa.payouts_enabled, COUNT(DISTINCT p.id) as total_passages, COUNT(DISTINCT CASE WHEN p.stripe_payment_id IS NOT NULL THEN p.id END) as passages_stripe, SUM(CASE WHEN p.stripe_payment_id IS NOT NULL THEN p.montant ELSE 0 END) as revenue_stripe, SUM(p.montant) as revenue_total FROM entites e LEFT JOIN stripe_accounts sa ON e.id = sa.fk_entite LEFT JOIN operations o ON e.id = o.fk_entite LEFT JOIN ope_pass p ON o.id = p.fk_operation GROUP BY e.id, e.encrypted_name, sa.stripe_account_id; -- 8. Fonction helper pour vérifier si un passage a un paiement Stripe -- --------------------------------------------------------------------- -- NOTE: Si vous exécutez en copier/coller, cette fonction est optionnelle -- Vous pouvez l'ignorer ou l'exécuter séparément avec DELIMITER -- ===================================================== -- FIN DE LA MIGRATION -- ===================================================== -- Tables supprimées : stripe_payment_intents -- Tables modifiées : ope_pass, stripe_payment_history, stripe_refunds -- Tables conservées : stripe_accounts, stripe_terminal_readers, etc. -- =====================================================