-- Ajouts à appliquer sur polysco_professionnel_demo_db pour l'aligner sur polysco_db (7 tables + 1 colonne).
SET FOREIGN_KEY_CHECKS=0;

/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `n_depense_familles` (
  `dfa_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `dfa_parent_id` bigint(20) unsigned DEFAULT NULL,
  `dfa_nom` varchar(120) COLLATE utf8mb4_unicode_ci NOT NULL,
  `dfa_code` varchar(30) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `dfa_est_systeme` tinyint(1) NOT NULL DEFAULT '0',
  `dfa_est_actif` tinyint(1) NOT NULL DEFAULT '1',
  `dfa_ordre` smallint(5) unsigned NOT NULL DEFAULT '0',
  `created_at` timestamp NULL DEFAULT (now()),
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `created_by` bigint(20) unsigned DEFAULT NULL,
  PRIMARY KEY (`dfa_id`),
  UNIQUE KEY `n_depense_familles_dfa_code_unique` (`dfa_code`),
  KEY `n_depense_familles_created_by_foreign` (`created_by`),
  KEY `n_depense_familles_dfa_parent_id_dfa_ordre_index` (`dfa_parent_id`,`dfa_ordre`),
  CONSTRAINT `n_depense_familles_created_by_foreign` FOREIGN KEY (`created_by`) REFERENCES `n_utilisateurs` (`user_id`) ON DELETE SET NULL,
  CONSTRAINT `n_depense_familles_dfa_parent_id_foreign` FOREIGN KEY (`dfa_parent_id`) REFERENCES `n_depense_familles` (`dfa_id`) ON DELETE RESTRICT
) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `n_autres_recettes` (
  `ar_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `ar_date` date NOT NULL,
  `ar_montant` decimal(12,2) NOT NULL,
  `ar_libelle` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `ar_reference` varchar(80) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `ar_mode_id` bigint(20) unsigned DEFAULT NULL,
  `ar_centre_id` bigint(20) unsigned NOT NULL,
  `ar_as_id` bigint(20) unsigned NOT NULL,
  `created_at` timestamp NULL DEFAULT (now()),
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at` timestamp NULL DEFAULT NULL,
  `created_by` bigint(20) unsigned DEFAULT NULL,
  PRIMARY KEY (`ar_id`),
  KEY `n_autres_recettes_ar_mode_id_foreign` (`ar_mode_id`),
  KEY `n_autres_recettes_ar_as_id_foreign` (`ar_as_id`),
  KEY `n_autres_recettes_created_by_foreign` (`created_by`),
  KEY `n_autres_recettes_ar_centre_id_ar_date_index` (`ar_centre_id`,`ar_date`),
  CONSTRAINT `n_autres_recettes_ar_as_id_foreign` FOREIGN KEY (`ar_as_id`) REFERENCES `n_annees_scolaires` (`as_id`),
  CONSTRAINT `n_autres_recettes_ar_centre_id_foreign` FOREIGN KEY (`ar_centre_id`) REFERENCES `n_centres` (`ct_id`),
  CONSTRAINT `n_autres_recettes_ar_mode_id_foreign` FOREIGN KEY (`ar_mode_id`) REFERENCES `n_modes_paiement` (`mp_id`) ON DELETE SET NULL,
  CONSTRAINT `n_autres_recettes_created_by_foreign` FOREIGN KEY (`created_by`) REFERENCES `n_utilisateurs` (`user_id`) ON DELETE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `n_depenses` (
  `dep_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `dep_date` date NOT NULL,
  `dep_montant` decimal(12,2) NOT NULL,
  `dep_famille_id` bigint(20) unsigned NOT NULL,
  `dep_libelle` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `dep_beneficiaire` varchar(150) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `dep_mode_id` bigint(20) unsigned DEFAULT NULL,
  `dep_reference` varchar(80) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `dep_observation` text COLLATE utf8mb4_unicode_ci,
  `dep_centre_id` bigint(20) unsigned NOT NULL,
  `dep_as_id` bigint(20) unsigned NOT NULL,
  `dep_source` enum('manuelle','vacation') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'manuelle',
  `dep_vp_id` bigint(20) unsigned DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT (now()),
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at` timestamp NULL DEFAULT NULL,
  `created_by` bigint(20) unsigned DEFAULT NULL,
  `updated_by` bigint(20) unsigned DEFAULT NULL,
  PRIMARY KEY (`dep_id`),
  UNIQUE KEY `n_depenses_dep_vp_id_unique` (`dep_vp_id`),
  KEY `n_depenses_dep_famille_id_foreign` (`dep_famille_id`),
  KEY `n_depenses_dep_mode_id_foreign` (`dep_mode_id`),
  KEY `n_depenses_created_by_foreign` (`created_by`),
  KEY `n_depenses_updated_by_foreign` (`updated_by`),
  KEY `n_depenses_dep_centre_id_dep_date_index` (`dep_centre_id`,`dep_date`),
  KEY `n_depenses_dep_as_id_dep_famille_id_index` (`dep_as_id`,`dep_famille_id`),
  CONSTRAINT `n_depenses_created_by_foreign` FOREIGN KEY (`created_by`) REFERENCES `n_utilisateurs` (`user_id`) ON DELETE SET NULL,
  CONSTRAINT `n_depenses_dep_as_id_foreign` FOREIGN KEY (`dep_as_id`) REFERENCES `n_annees_scolaires` (`as_id`),
  CONSTRAINT `n_depenses_dep_centre_id_foreign` FOREIGN KEY (`dep_centre_id`) REFERENCES `n_centres` (`ct_id`),
  CONSTRAINT `n_depenses_dep_famille_id_foreign` FOREIGN KEY (`dep_famille_id`) REFERENCES `n_depense_familles` (`dfa_id`),
  CONSTRAINT `n_depenses_dep_mode_id_foreign` FOREIGN KEY (`dep_mode_id`) REFERENCES `n_modes_paiement` (`mp_id`) ON DELETE SET NULL,
  CONSTRAINT `n_depenses_updated_by_foreign` FOREIGN KEY (`updated_by`) REFERENCES `n_utilisateurs` (`user_id`) ON DELETE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=34 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `n_calendrier_periode_classes` (
  `cpc_per_id` bigint(20) unsigned NOT NULL,
  `cpc_classe_id` bigint(20) unsigned NOT NULL,
  `created_at` timestamp NULL DEFAULT (now()),
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `created_by` bigint(20) unsigned DEFAULT NULL,
  PRIMARY KEY (`cpc_per_id`,`cpc_classe_id`),
  KEY `n_calendrier_periode_classes_cpc_classe_id_foreign` (`cpc_classe_id`),
  KEY `idx_created_by` (`created_by`),
  CONSTRAINT `n_calendrier_periode_classes_cpc_classe_id_foreign` FOREIGN KEY (`cpc_classe_id`) REFERENCES `n_classes` (`classe_id`) ON DELETE CASCADE,
  CONSTRAINT `n_calendrier_periode_classes_cpc_per_id_foreign` FOREIGN KEY (`cpc_per_id`) REFERENCES `n_calendrier_periodes` (`per_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `n_examens_publications` (
  `epub_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `epub_classe_id` bigint(20) unsigned NOT NULL,
  `epub_as_id` bigint(20) unsigned NOT NULL,
  `epub_session_id` bigint(20) unsigned NOT NULL,
  `epub_type_edt_id` bigint(20) unsigned NOT NULL,
  `epub_publie_le` timestamp NOT NULL,
  `epub_publie_par` bigint(20) unsigned DEFAULT NULL,
  `epub_nb_notifies` int(10) unsigned NOT NULL DEFAULT '0',
  `created_at` timestamp NULL DEFAULT (now()),
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `created_by` bigint(20) unsigned DEFAULT NULL,
  PRIMARY KEY (`epub_id`),
  UNIQUE KEY `uq_examens_publication` (`epub_classe_id`,`epub_as_id`,`epub_session_id`,`epub_type_edt_id`),
  KEY `n_examens_publications_epub_as_id_foreign` (`epub_as_id`),
  KEY `n_examens_publications_epub_session_id_foreign` (`epub_session_id`),
  KEY `n_examens_publications_epub_type_edt_id_foreign` (`epub_type_edt_id`),
  KEY `n_examens_publications_epub_publie_par_foreign` (`epub_publie_par`),
  KEY `idx_created_by` (`created_by`),
  CONSTRAINT `n_examens_publications_epub_as_id_foreign` FOREIGN KEY (`epub_as_id`) REFERENCES `n_annees_scolaires` (`as_id`),
  CONSTRAINT `n_examens_publications_epub_classe_id_foreign` FOREIGN KEY (`epub_classe_id`) REFERENCES `n_classes` (`classe_id`),
  CONSTRAINT `n_examens_publications_epub_publie_par_foreign` FOREIGN KEY (`epub_publie_par`) REFERENCES `n_utilisateurs` (`user_id`),
  CONSTRAINT `n_examens_publications_epub_session_id_foreign` FOREIGN KEY (`epub_session_id`) REFERENCES `n_session` (`sess_id`),
  CONSTRAINT `n_examens_publications_epub_type_edt_id_foreign` FOREIGN KEY (`epub_type_edt_id`) REFERENCES `n_type_edt` (`edtype_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `n_seance_surveillants` (
  `ss_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `ss_se_id` bigint(20) unsigned NOT NULL,
  `ss_prof_id` bigint(20) unsigned DEFAULT NULL,
  `ss_user_id` bigint(20) unsigned DEFAULT NULL,
  `ss_role` enum('responsable','surveillant') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'surveillant',
  `created_at` timestamp NULL DEFAULT (now()),
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `created_by` bigint(20) unsigned DEFAULT NULL,
  PRIMARY KEY (`ss_id`),
  KEY `n_seance_surveillants_ss_se_id_foreign` (`ss_se_id`),
  KEY `n_seance_surveillants_ss_prof_id_ss_se_id_index` (`ss_prof_id`,`ss_se_id`),
  KEY `n_seance_surveillants_ss_user_id_ss_se_id_index` (`ss_user_id`,`ss_se_id`),
  KEY `idx_created_by` (`created_by`),
  CONSTRAINT `n_seance_surveillants_ss_prof_id_foreign` FOREIGN KEY (`ss_prof_id`) REFERENCES `n_enseignants` (`prof_id`),
  CONSTRAINT `n_seance_surveillants_ss_se_id_foreign` FOREIGN KEY (`ss_se_id`) REFERENCES `n_seances` (`se_id`) ON DELETE CASCADE,
  CONSTRAINT `n_seance_surveillants_ss_user_id_foreign` FOREIGN KEY (`ss_user_id`) REFERENCES `n_utilisateurs` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=228 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `n_stage_jours` (
  `stj_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `stj_stg_id` bigint(20) unsigned NOT NULL,
  `stj_date` date NOT NULL,
  `stj_statut` enum('present','absent','justifie','ferie','non_travaille') COLLATE utf8mb4_unicode_ci NOT NULL,
  `stj_heures` decimal(4,2) DEFAULT NULL,
  `stj_commentaire` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT (now()),
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `created_by` bigint(20) unsigned DEFAULT NULL,
  PRIMARY KEY (`stj_id`),
  UNIQUE KEY `n_stage_jours_stj_stg_id_stj_date_unique` (`stj_stg_id`,`stj_date`),
  KEY `n_stage_jours_created_by_foreign` (`created_by`),
  CONSTRAINT `n_stage_jours_created_by_foreign` FOREIGN KEY (`created_by`) REFERENCES `n_utilisateurs` (`user_id`) ON DELETE SET NULL,
  CONSTRAINT `n_stage_jours_stj_stg_id_foreign` FOREIGN KEY (`stj_stg_id`) REFERENCES `n_stages` (`stg_id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

ALTER TABLE `n_calendrier_periodes` ADD COLUMN `per_jours` varchar(20) NULL AFTER `per_date_fin`;

SET FOREIGN_KEY_CHECKS=1;
