-- ========================================
-- FIX: Ajouter des valeurs par défaut à la table factures
-- ========================================
-- La table factures a beaucoup de champs NOT NULL sans valeur par défaut
-- Cela cause des erreurs SQL lors de la création de factures
-- ========================================

SET sql_mode = '';

-- Modifier les champs NOT NULL pour avoir des valeurs par défaut
ALTER TABLE `factures` MODIFY COLUMN `created_at` VARCHAR(10) DEFAULT '';
ALTER TABLE `factures` MODIFY COLUMN `updated_at` VARCHAR(50) DEFAULT '';
ALTER TABLE `factures` MODIFY COLUMN `number_bdc` VARCHAR(255) DEFAULT '';
ALTER TABLE `factures` MODIFY COLUMN `nb_reglements` INT DEFAULT 1;
ALTER TABLE `factures` MODIFY COLUMN `montant_tva` VARCHAR(11) DEFAULT '0';
ALTER TABLE `factures` MODIFY COLUMN `devise` VARCHAR(10) DEFAULT 'MAD';
ALTER TABLE `factures` MODIFY COLUMN `caller_id` INT DEFAULT 0;
ALTER TABLE `factures` MODIFY COLUMN `facture_du_jour` INT DEFAULT 1;
ALTER TABLE `factures` MODIFY COLUMN `date_facture_du_jour` VARCHAR(50) DEFAULT '';
ALTER TABLE `factures` MODIFY COLUMN `upload_url` VARCHAR(100) DEFAULT '';
ALTER TABLE `factures` MODIFY COLUMN `bdc_id` INT DEFAULT 0;
ALTER TABLE `factures` MODIFY COLUMN `detail_reglements` VARCHAR(500) DEFAULT '';
ALTER TABLE `factures` MODIFY COLUMN `debut_payment_date` VARCHAR(255) DEFAULT '';
ALTER TABLE `factures` MODIFY COLUMN `quantite` VARCHAR(10) DEFAULT '0';
ALTER TABLE `factures` MODIFY COLUMN `commercial_id` INT DEFAULT 0;
ALTER TABLE `factures` MODIFY COLUMN `paiement_recu` VARCHAR(255) DEFAULT 'Non';
ALTER TABLE `factures` MODIFY COLUMN `transition_old_new` VARCHAR(20) DEFAULT '';
ALTER TABLE `factures` MODIFY COLUMN `tva_declare` VARCHAR(50) DEFAULT '';
ALTER TABLE `factures` MODIFY COLUMN `manager_id` INT DEFAULT 0;
ALTER TABLE `factures` MODIFY COLUMN `date_derniere_mensualite` VARCHAR(50) DEFAULT '';
ALTER TABLE `factures` MODIFY COLUMN `ufer` VARCHAR(20) DEFAULT 'non';
ALTER TABLE `factures` MODIFY COLUMN `date_envoi_facture` VARCHAR(50) DEFAULT '';

-- Nettoyer les données existantes
UPDATE `factures` SET `created_at` = '' WHERE `created_at` IS NULL;
UPDATE `factures` SET `updated_at` = '' WHERE `updated_at` IS NULL;
UPDATE `factures` SET `number_bdc` = '' WHERE `number_bdc` IS NULL;
UPDATE `factures` SET `nb_reglements` = 1 WHERE `nb_reglements` IS NULL;
UPDATE `factures` SET `montant_tva` = '0' WHERE `montant_tva` IS NULL;
UPDATE `factures` SET `devise` = 'MAD' WHERE `devise` IS NULL;
UPDATE `factures` SET `caller_id` = 0 WHERE `caller_id` IS NULL;
UPDATE `factures` SET `facture_du_jour` = 1 WHERE `facture_du_jour` IS NULL;
UPDATE `factures` SET `date_facture_du_jour` = '' WHERE `date_facture_du_jour` IS NULL;
UPDATE `factures` SET `upload_url` = '' WHERE `upload_url` IS NULL;
UPDATE `factures` SET `bdc_id` = 0 WHERE `bdc_id` IS NULL;
UPDATE `factures` SET `detail_reglements` = '' WHERE `detail_reglements` IS NULL;
UPDATE `factures` SET `debut_payment_date` = '' WHERE `debut_payment_date` IS NULL;
UPDATE `factures` SET `quantite` = '0' WHERE `quantite` IS NULL;
UPDATE `factures` SET `commercial_id` = 0 WHERE `commercial_id` IS NULL;
UPDATE `factures` SET `paiement_recu` = 'Non' WHERE `paiement_recu` IS NULL;
UPDATE `factures` SET `transition_old_new` = '' WHERE `transition_old_new` IS NULL;
UPDATE `factures` SET `tva_declare` = '' WHERE `tva_declare` IS NULL;
UPDATE `factures` SET `manager_id` = 0 WHERE `manager_id` IS NULL;
UPDATE `factures` SET `date_derniere_mensualite` = '' WHERE `date_derniere_mensualite` IS NULL;
UPDATE `factures` SET `ufer` = 'non' WHERE `ufer` IS NULL;
UPDATE `factures` SET `date_envoi_facture` = '' WHERE `date_envoi_facture` IS NULL;

SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

-- ========================================
-- VÉRIFICATION
-- ========================================
-- SHOW COLUMNS FROM factures;
-- ========================================

