-- 1/3 : Création de la table temporaire
DROP TABLE IF EXISTS w_dbsetup
;

CREATE TEMP TABLE w_dbsetup AS
    SELECT
        CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
    	name,
    	texte,
    	select_cmd,
    	code 
    FROM activite.t_listes_tables
    ORDER BY code
     LIMIT 0
;

-- 2/3 : Peuplement de la table temporaire
INSERT INTO w_dbsetup(
    oid,
    name,
    texte,
    select_cmd,
    code
)
VALUES
(1, 't_actes', 'Actes', '(SELECT t_actes_c.oid, t_actes_c.code, t_actes_c.texte, ccam_regroupement_code_1 || CASE WHEN t_actes_c.classant = ''Y'' THEN '' (classant)'' ELSE '''' END  as complement, ''O'' as utilise 
FROM activite.t_actes_c
WHERE t_actes_c.oid > 0 AND nomenclature IN ( ''CCAM'', ''NGAP'')  AND t_actes_c.oid in (SELECT oid FROM activite.p_oids WHERE code_table = ''acte_h'')  ) 
UNION 
(SELECT t_actes_c.oid, t_actes_c.code, t_actes_c.texte, ccam_regroupement_code_1 || CASE WHEN t_actes_c.classant = ''Y'' THEN '' (classant)'' ELSE '''' END as complement, ''N'' as utilise 
FROM activite.t_actes_c
WHERE t_actes_c.oid > 0 AND nomenclature  IN ( ''CCAM'', ''NGAP'')  AND t_actes_c.oid not in (SELECT oid FROM activite.p_oids WHERE code_table = ''acte_h'')  ) 
ORDER BY code', 'ACTE'),
(1, 't_activites', '[DICT.ACTI.ACTIVITE#3]
', '(SELECT oid, code, texte, ''''::text as complement, ''O'' as utilise FROM activite.t_activites WHERE oid > 0 AND oid in (SELECT oid FROM activite.p_oids WHERE code_table = ''activite'') UNION SELECT oid, code, texte, ''''::text as complement, ''N'' as utilise FROM activite.t_activites WHERE oid > 0 AND oid not in (SELECT oid FROM activite.p_oids WHERE code_table = ''activite'') )ORDER BY code', 'ACTIVITE'),
(1, 't_ages', 'Ages', 'SELECT oid, code, texte, ''''::text as complement, ''O''::text as utilise FROM base.t_ages ORDER BY code', 'AGE'),
(1, 't_budget_cle', 'Clés budgétaires', 'SELECT oid, code AS code, texte AS texte, 
''''::text as complement, 
''O'' as utilise 
FROM activite.t_budget_cle
WHERE oid > 0 ', 'BUDGETCLE'),
(1, 't_codes_postaux', 'Codes postaux', '(
SELECT t_codes_postaux.oid, t_codes_postaux.code, t_codes_postaux.texte, '''' as complement, ''O'' as utilise 
FROM base.t_codes_postaux
WHERE 
	t_codes_postaux.oid > 0 
	AND t_codes_postaux.oid in (SELECT oid FROM activite.p_oids WHERE code_table=''c_postaux'') 
UNION 
SELECT t_codes_postaux.oid, t_codes_postaux.code, t_codes_postaux.texte, '''' as complement, ''N'' as utilise 
FROM base.t_codes_postaux
WHERE 
	t_codes_postaux.oid > 0 
	AND t_codes_postaux.oid not in (SELECT oid FROM activite.p_oids WHERE code_table=''c_postaux'')
) 
ORDER BY code', 'CODEPOST'),
(1, 't_compte', 'Comptes produit', 'SELECT t_compte.oid AS oid, code AS code, texte AS texte, 
''''::text as complement, 
(CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END)::text as utilise 
FROM activite.t_compte 
LEFT JOIN activite.p_oids ON code_table=''compte'' AND t_compte.oid = p_oids.oid
WHERE t_compte.oid > 0 ', 'COMPTE'),
(1, 't_durees_sejour', 'Durées de séjour', 'SELECT oid, code, texte, ''''::text as complement, ''O''::text as utilise FROM base.t_durees_sejour ORDER BY code', 'DURSEJ'),
(1, 't_etages', '[DICT.ACTI.ETAGE#31]', '(SELECT oid, code, texte, ''''::text as complement, ''O'' as utilise FROM activite.t_etages WHERE oid in (SELECT oid FROM activite.p_oids WHERE code_table = ''etage'') UNION SELECT oid, code, texte, ''''::text as complement, ''N'' as utilise FROM activite.t_etages WHERE oid not in (SELECT oid FROM activite.p_oids WHERE code_table = ''etage'') )ORDER BY code', 'ETAGE'),
(1, 't_etat_sejour', 'Etat de séjour', 'SELECT oid, code, texte, ''''::text as complement, ''O''::text as utilise FROM activite.t_etat_sejour ORDER BY code', 'ETATSEJOUR'),
(1, 't_expert_controles', 'Controles Expert', 'SELECT oid, code, texte, ''''::text as complement, ''O''::text as utilise 
FROM activite.t_expert_controle WHERE oid > 0 
ORDER BY code', 'EXPERTCTL'),
(1, 't_filiere_soin', 'Filières de soin', 'SELECT oid, code, texte, ''''::text as complement, 
				CASE WHEN oid in (SELECT oid FROM activite.p_oids WHERE code_table IN (''filiere_soin'')) THEN ''O'' ELSE ''N'' END as utilise 
				FROM activite.t_filiere_soin
				ORDER BY 2', 'FILIERESOIN'),
(1, 't_finess', 'Finess', 'SELECT oid, code, texte, ''''::text as complement, ''O'' as utilise FROM activite.t_finess_c WHERE oid > 0 ORDER BY code, texte', 'FINESS'),
(1, 't_forme_activite', 'Formes d''activité', 'SELECT oid, code, texte, ''''::text as complement, 
				CASE WHEN oid in (SELECT oid FROM activite.p_oids WHERE code_table IN (''forme_activite'')) THEN ''O'' ELSE ''N'' END as utilise 
				FROM activite.t_forme_activite
				ORDER BY 2', 'FORMEACTIVITE'),
(1, 't_ghm_c', 'GHM', 'SELECT t_ghm_c.ghm_id AS oid, ghm_code AS code, ghm_texte AS texte, 
cas_text as complement,
(CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END)::text as utilise 
FROM activite.t_ghm_c 
LEFT JOIN activite.p_oids ON code_table=''ghm'' AND t_ghm_c.ghm_id = p_oids.oid
WHERE t_ghm_c.ghm_id > 0 ', 'GHM'),
(1, 't_ghs', 'GHS', '(SELECT oid, code, texte, ''''::text as complement, ''O'' as utilise FROM base.t_ghs WHERE oid > 0 AND oid in (SELECT oid FROM activite.p_oids WHERE code_table = ''ghs'') UNION SELECT oid, code, texte, ''''::text as complement, ''N'' as utilise FROM base.t_ghs WHERE oid > 0 AND code > 0 AND oid not in (SELECT oid FROM activite.p_oids WHERE code_table = ''ghs'') ) ORDER BY code', 'GHS'),
(1, 't_gme_c', 'GME', 'SELECT t_gme_c.gme_id AS oid, gme_code AS code, gme_texte AS texte,''''::text as complement ,
(CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END)::text as utilise 
FROM activite.t_gme_c 
LEFT JOIN activite.p_oids ON code_table=''gme'' AND t_gme_c.gme_id = p_oids.oid
WHERE t_gme_c.gme_id > 0', 'GME'),
(1, 't_lits', 'Lits', '(
SELECT t_lits.oid, t_etages.code || ''-'' || t_lits.code AS code, t_etages.texte || '' lit '' || t_lits.texte AS texte, ''''::text as complement, ''O'' as utilise FROM activite.t_lits JOIN activite.t_etages ON etage_id = t_etages.oid WHERE t_lits.oid > 0 AND t_lits.oid in (SELECT oid FROM activite.p_oids WHERE code_table = ''lit'') 

UNION 

SELECT t_lits.oid, t_etages.code || ''-'' || t_lits.code AS code, t_etages.texte || '' lit '' || t_lits.texte AS texte, ''''::text as complement, ''N'' as utilise FROM activite.t_lits JOIN activite.t_etages ON etage_id = t_etages.oid WHERE t_lits.oid > 0 AND t_lits.oid NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = ''lit'') 

) ORDER BY code', 'LIT'),
(1, 't_medecins_administratifs', 'Médecins', 'SELECT t_medecins_administratifs.oid
			,t_medecins_administratifs.code
			,t_medecins_administratifs.nom || '' '' || t_medecins_administratifs.prenom AS texte
			,COALESCE(t_specialites_medecin.code || '' '' || t_specialites_medecin.texte,'''')::text AS complement
			,CASE WHEN t_medecins_administratifs.oid IN (SELECT p_oids.oid FROM activite.p_oids WHERE  code_table IN (''medecin_s'',''medecin_x'') )  THEN ''O'' ELSE ''N'' END AS utilise
		FROM activite.t_medecins_administratifs
		LEFT JOIN base.t_medecins ON t_medecins_administratifs.medecin_id = t_medecins.oid
		LEFT JOIN base.t_specialites_medecin ON t_medecins.specialite_id = t_specialites_medecin.oid
		ORDER BY t_medecins_administratifs.code', 'MEDECIN'),
(1, 't_medecins_traitants_administratifs', 'Médecins traitants', 'SELECT t_medecins_traitants_administratifs.oid
			,t_medecins_traitants_administratifs.code
			,t_medecins_traitants_administratifs.nom || '' '' || t_medecins_traitants_administratifs.prenom AS texte
			,COALESCE(t_medecins_traitants_administratifs.code || '' '' || t_medecins_traitants_administratifs.no_rpps,'''')::text AS complement
			,CASE WHEN t_medecins_traitants_administratifs.oid IN (SELECT p_oids.oid FROM activite.p_oids WHERE  code_table IN (''medecin_t'') )  THEN ''O'' ELSE ''N'' END AS utilise
		FROM activite.t_medecins_traitants_administratifs
		LEFT JOIN base.t_medecins ON t_medecins_traitants_administratifs.medecin_id = t_medecins.oid
		LEFT JOIN base.t_specialites_medecin ON t_medecins.specialite_id = t_specialites_medecin.oid
		ORDER BY t_medecins_traitants_administratifs.code', 'MEDECIN_TRAITANT'),
(1, 't_prestations', 'Prestations Cliniques', '(SELECT oid, code, texte, norme as complement, ''O'' as utilise FROM activite.t_prestations WHERE oid in (SELECT oid FROM activite.p_oids WHERE code_table = ''prestation_c'') UNION SELECT oid, code, texte, norme as complement, ''N'' as utilise FROM activite.t_prestations WHERE oid not in (SELECT oid FROM activite.p_oids WHERE code_table = ''prestation_c'') ) ORDER BY code', 'PRESTA_C'),
(1, 't_prestations', 'Prestations Honoraires', '(SELECT oid, code, texte, norme as complement, ''O'' as utilise FROM activite.t_prestations WHERE oid > 0 AND clinique_honoraire IN (''H'', '''') AND oid in (SELECT oid FROM activite.p_oids WHERE code_table = ''prestation_h'') UNION SELECT oid, code, texte, norme as complement, ''N'' as utilise FROM activite.t_prestations WHERE oid > 0 AND clinique_honoraire IN (''H'', '''') AND oid not in (SELECT oid FROM activite.p_oids WHERE code_table = ''prestation_h'') )ORDER BY code', 'PRESTA_H'),
(1, 't_rejets_noemie', 'Rejets NOEMIE', 'SELECT oid, code, texte, ''''::text as complement, ''O'' as utilise FROM activite.t_rejets_noemie WHERE oid > 0 ORDER BY code, texte', 'REJETNOEMI'),
(1, 't_rubriques_facturation', 'Rubriques de facturation', '(SELECT oid, code, texte, ''''::text as complement, ''O'' as utilise FROM activite.t_rubriques_facturation WHERE oid > 0 AND oid in (SELECT oid FROM activite.p_oids WHERE code_table = ''rubrique_c'') UNION SELECT oid, code, texte, ''''::text as complement, ''N'' as utilise FROM activite.t_rubriques_facturation WHERE oid > 0 AND oid not in (SELECT oid FROM activite.p_oids WHERE code_table = ''rubrique_c'') ) ORDER BY code', 'RUBRIQUE'),
(1, 't_services_facturation', '[DICT.ACTI.SERVICE#31]', '(SELECT oid, code, texte, ''''::text as complement, ''O'' as utilise FROM activite.t_services_facturation WHERE oid > 0 AND oid in (SELECT oid FROM activite.p_oids WHERE code_table = ''service'') UNION SELECT oid, code, texte, ''''::text as complement, ''N'' as utilise FROM activite.t_services_facturation WHERE oid > 0 AND oid not in (SELECT oid FROM activite.p_oids WHERE code_table = ''service'') )ORDER BY code', 'SERVICE_F'),
(1, 't_tiers_payant', 'Tiers payant', '(SELECT t_tiers_payant.oid, t_tiers_payant.code, t_tiers_payant.texte as texte, t_types_tiers_payant.texte as complement, ''O'' as utilise 
FROM activite.t_tiers_payant, activite.t_types_tiers_payant
WHERE t_tiers_payant.oid > 0 AND t_tiers_payant.type_tiers_payant = t_types_tiers_payant.code
	AND t_tiers_payant.oid in (SELECT oid FROM activite.p_oids  WHERE code_table = ''tiers_payant'' ) 
UNION 
SELECT t_tiers_payant.oid, t_tiers_payant.code, t_tiers_payant.texte as texte, t_types_tiers_payant.texte , ''N'' as utilise 
FROM activite.t_tiers_payant , activite.t_types_tiers_payant
WHERE t_tiers_payant.oid > 0 AND t_tiers_payant.type_tiers_payant = t_types_tiers_payant.code AND (t_tiers_payant.oid not in (SELECT oid FROM activite.p_oids WHERE code_table = ''tiers_payant''))) ORDER BY code', 'TIERSP'),
(1, 't_type_sejour', 'Types de séjour', 'SELECT oid, code, texte, ''''::text as complement, ''O''::text as utilise FROM activite.t_type_sejour ORDER BY code', 'TYPESEJOUR'),
(1, 't_unites_fonctionnelles', '[DICT.ACTI.UF#31]', '(SELECT oid, code, texte, ''''::text as complement, ''O'' as utilise FROM activite.t_unites_fonctionnelles WHERE oid > 0 AND oid in (SELECT oid FROM activite.p_oids WHERE code_table = ''unite_fonctionnelle'') UNION SELECT oid, code, texte, ''''::text as complement, ''N'' as utilise FROM activite.t_unites_fonctionnelles WHERE oid > 0 AND oid not in (SELECT oid FROM activite.p_oids WHERE code_table = ''unite_fonctionnelle'') ) ORDER BY code', 'UF'),
(1, 't_unites_medicales', '[DICT.ACTI.UM#31]', '(SELECT oid, code, texte, ''''::text as complement, ''O'' as utilise FROM activite.t_unites_medicales WHERE oid > 0 AND oid in (SELECT oid FROM activite.p_oids WHERE code_table = ''unite_medicale'') UNION SELECT oid, code, texte, ''''::text as complement, ''N'' as utilise FROM activite.t_unites_medicales WHERE oid > 0 AND oid not in (SELECT oid FROM activite.p_oids WHERE code_table = ''unite_medicale'') ) ORDER BY code', 'UM')
;

-- 3/3 : Màj de la table iCTI
UPDATE activite.t_listes_tables SET code = 'PRESTA_C_B' WHERE oid =
(SELECT MIN(oid) FROM activite.t_listes_tables WHERE code = 'PRESTA_C') 
AND (SELECT count(*) FROM activite.t_listes_tables WHERE code = 'PRESTA_C') > 1;

UPDATE activite.t_listes SET table_id = (SELECT oid FROM  activite.t_listes_tables WHERE code = 'PRESTA_C') 
WHERE table_id IN (SELECT oid FROM activite.t_listes_tables WHERE code = 'PRESTA_C_B');

DELETE FROM activite.t_listes_tables WHERE code = 'PRESTA_C_B';

UPDATE activite.t_listes_tables SET 
	code = w_dbsetup.code,
	texte = w_dbsetup.texte, 
	select_cmd = w_dbsetup.select_cmd,
	name = w_dbsetup.name
FROM w_dbsetup
WHERE t_listes_tables.code = w_dbsetup.code;

INSERT INTO activite.t_listes_tables(oid, code, name, texte, select_cmd)
	SELECT oid, code, name, texte, select_cmd FROM w_dbsetup WHERE oid = 0 AND oid NOT IN (SELECT oid FROM activite.t_listes_tables);

INSERT INTO activite.t_listes_tables(code, name, texte, select_cmd)
	SELECT code, name, texte, select_cmd FROM w_dbsetup WHERE oid <> 0 AND code NOT IN (SELECT code FROM activite.t_listes_tables);