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

CREATE TEMP TABLE w_dbsetup AS
    SELECT oid,      name,      texte,      select_cmd,      code       FROM compta.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_sections_analytiques', 'Sections analytiques', 'SELECT oid, code, texte, '''' as complement, CASE WHEN oid in (SELECT oid FROM compta.p_oids WHERE code_table = ''sections_analytiques_c'')  THEN ''O'' ELSE ''N'' END as utilise FROM compta.t_sections_analytiques WHERE oid > 0 ORDER BY code', 'ANA'),
(1, 't_sections_analytiques_c', 'Sections analytiques Clinique', 'SELECT oid, code, texte, '''' as complement, ''O'' as utilise FROM compta.t_sections_analytiques WHERE oid > 0 AND oid in (SELECT oid FROM compta.p_oids WHERE code_table = ''sections_analytiques_c'')  ORDER BY code', 'ANAC'),
(1, 't_comptes', 'Comptes', 'SELECT t_comptes.oid, t_comptes.numero as code, t_comptes.texte, t_types_compta.texte as complement, CASE WHEN t_comptes.oid in (SELECT oid FROM compta.p_oids WHERE code_table IN (''comptes_c'', ''comptes_h'', ''comptes_extra_c'', ''comptes_extra_h'')) THEN  ''O'' ELSE ''N'' END as utilise FROM compta.t_comptes, compta.t_types_compta WHERE t_comptes.oid > 0 AND t_comptes.type_compta_id = t_types_compta.oid
AND t_comptes.oid in (SELECT oid FROM compta.p_oids WHERE code_table IN (''comptes_c'', ''comptes_h'', ''comptes_extra_c'', ''comptes_extra_h''))  ORDER BY code', 'CPT'),
(1, 't_comptes_c', 'Comptes Clinique', 'SELECT t_comptes.oid, t_comptes.numero as code, t_comptes.texte, t_types_compta.texte as complement, ''O'' as utilise FROM compta.t_comptes, compta.t_types_compta WHERE t_comptes.oid > 0 AND t_comptes.type_compta_id = t_types_compta.oid AND t_comptes.oid in (SELECT oid FROM compta.p_oids WHERE code_table = ''comptes_c'')  ORDER BY code', 'CPTC'),
(1, 't_comptes_h', 'Comptes Honoraires', 'SELECT t_comptes.oid, t_comptes.numero as code, t_comptes.texte, t_types_compta.texte as complement, ''O'' as utilise FROM compta.t_comptes, compta.t_types_compta WHERE t_comptes.oid > 0 AND t_comptes.type_compta_id = t_types_compta.oid AND t_comptes.oid in (SELECT oid FROM compta.p_oids WHERE code_table = ''comptes_h'')  ORDER BY code', 'CPTH'),
(1, 't_activites_cout_patient_shs', 'Cout patient SHS. Activités', 'SELECT t_activites_cout_patient_shs.oid, t_activites_cout_patient_shs.code, t_activites_cout_patient_shs.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM compta.t_activites_cout_patient_shs LEFT JOIN compta.p_oids ON (t_activites_cout_patient_shs.oid = p_oids.oid AND code_table = ''activites_cout_patient_shs'')
WHERE t_activites_cout_patient_shs.oid > 0 
ORDER BY t_activites_cout_patient_shs.code', 'CSA'),
(1, 't_elements_cout_patient_shs', 'Cout patient SHS. Eléments de cout', 'SELECT t_elements_cout_patient_shs.oid, t_elements_cout_patient_shs.code, t_elements_cout_patient_shs.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM compta.t_elements_cout_patient_shs LEFT JOIN compta.p_oids ON (t_elements_cout_patient_shs.oid = p_oids.oid AND code_table = ''etages_cout_patient_shs'')
WHERE t_elements_cout_patient_shs.oid > 0 
ORDER BY t_elements_cout_patient_shs.code', 'CSC'),
(1, 't_etages_cout_patient_shs', 'Cout patient SHS. Etages', 'SELECT t_etages_cout_patient_shs.oid, t_etages_cout_patient_shs.code, t_etages_cout_patient_shs.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM compta.t_etages_cout_patient_shs LEFT JOIN compta.p_oids ON (t_etages_cout_patient_shs.oid = p_oids.oid AND code_table = ''etages_cout_patient_shs'')
WHERE t_etages_cout_patient_shs.oid > 0 
ORDER BY t_etages_cout_patient_shs.code', 'CSE'),
(1, 't_services_facturation_cout_patient_shs', 'Cout patient SHS. Services', 'SELECT t_services_facturation_cout_patient_shs.oid, t_services_facturation_cout_patient_shs.code, t_services_facturation_cout_patient_shs.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM compta.t_services_facturation_cout_patient_shs LEFT JOIN compta.p_oids ON (t_services_facturation_cout_patient_shs.oid = p_oids.oid AND code_table = ''services_facturation_cout_patient_shs'')
WHERE t_services_facturation_cout_patient_shs.oid > 0 
ORDER BY t_services_facturation_cout_patient_shs.code', 'CSF'),
(1, 't_ghm_cout_patient_shs', 'Cout patient SHS. GHM', 'SELECT t_ghm_cout_patient_shs.oid, t_ghm_cout_patient_shs.code, t_ghm_cout_patient_shs.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM compta.t_ghm_cout_patient_shs LEFT JOIN compta.p_oids ON (t_ghm_cout_patient_shs.oid = p_oids.oid AND code_table = ''ghm_cout_patient_shs'')
WHERE t_ghm_cout_patient_shs.oid > 0 
ORDER BY t_ghm_cout_patient_shs.code', 'CSG'),
(1, 't_ghs_cout_patient_shs', 'Cout patient SHS. GHS', 'SELECT t_ghs_cout_patient_shs.oid, t_ghs_cout_patient_shs.code, t_ghs_cout_patient_shs.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM compta.t_ghs_cout_patient_shs LEFT JOIN compta.p_oids ON (t_ghs_cout_patient_shs.oid = p_oids.oid AND code_table = ''ghs_cout_patient_shs'')
WHERE t_ghs_cout_patient_shs.oid > 0 
ORDER BY t_ghs_cout_patient_shs.code', 'CSH'),
(1, 't_medecins_cout_patient_shs', 'Cout patient SHS. Médecins', 'SELECT t_medecins_cout_patient_shs.oid, t_medecins_cout_patient_shs.code, t_medecins_cout_patient_shs.nom || '' '' || t_medecins_cout_patient_shs.prenom AS texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM compta.t_medecins_cout_patient_shs LEFT JOIN compta.p_oids ON (t_medecins_cout_patient_shs.oid = p_oids.oid AND code_table = ''medecins_cout_patient_shs'')
WHERE t_medecins_cout_patient_shs.oid > 0 
ORDER BY t_medecins_cout_patient_shs.code', 'CSM'),
(1, 't_unites_oeuvres_shs', 'Cout patient SHS. Unités d''oeuvre', 'SELECT t_unites_oeuvre_shs.oid, t_unites_oeuvre_shs.code, t_unites_oeuvre_shs.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM compta.t_unites_oeuvre_shs LEFT JOIN compta.p_oids ON (t_unites_oeuvre_shs.oid = p_oids.oid AND code_table = ''etages_cout_patient_shs'')
WHERE t_unites_oeuvre_shs.oid > 0 
ORDER BY t_unites_oeuvre_shs.code', 'CSU'),
(1, 't_journaux', 'Journaux', 'SELECT t_journaux.oid, t_journaux.code, t_journaux.texte, subview.clinique_honoraire as complement, ''O'' as utilise FROM compta.t_journaux,
(SELECT oid, trim(MAX(CASE WHEN code_table = ''journaux_c'' THEN ''CLINIQUE'' ELSE '''' END) || '' '' || MAX(CASE WHEN code_table = ''journaux_h'' THEN ''HONORAIRE'' ELSE '''' END)) as clinique_honoraire FROM compta.p_oids WHERE code_table IN (''journaux_c'', ''journaux_h'') GROUP BY oid
) subview
WHERE t_journaux.oid > 0 AND t_journaux.oid = subview.oid
ORDER BY code', 'JRN'),
(1, 't_journaux_c', 'Journaux Clinique', 'SELECT oid, code, texte, '''' as complement, ''O'' as utilise FROM compta.t_journaux WHERE oid > 0 AND oid in (SELECT oid FROM compta.p_oids WHERE code_table = ''journaux_c'')  ORDER BY code', 'JRNC'),
(1, 't_journaux_h', 'Journaux Honoraires', 'SELECT oid, code, texte, '''' as complement, ''O'' as utilise FROM compta.t_journaux WHERE oid > 0 AND oid in (SELECT oid FROM compta.p_oids WHERE code_table = ''journaux_h'')  ORDER BY code', 'JRNH'),
(1, 't_partenaires', 'Partenaires', 'SELECT oid, code, texte, subview.comptes as complement, CASE WHEN oid in (SELECT oid FROM compta.p_oids WHERE code_table IN (''partenaires_c'', ''partenaires_h'')) THEN ''O'' ELSE ''N'' END as utilise FROM compta.t_partenaires LEFT JOIN (SELECT partenaire_id, base.cti_group_concat(DISTINCT compte_numero || '' '' ||  substr(compte_texte, 1 , 15)) as comptes FROM compta.v_historique_ecritures_total_1 GROUP BY partenaire_id) subview ON (subview.partenaire_id = t_partenaires.oid) WHERE oid > 0  ORDER BY code', 'PAR'),
(1, 't_partenaires_c', 'Partenaires Clinique', 'SELECT oid, code, texte, subview.comptes as complement, ''O'' as utilise FROM compta.t_partenaires LEFT JOIN (SELECT partenaire_id, base.cti_group_concat(DISTINCT compte_numero || '' '' ||  substr(compte_texte, 1 , 15)) as comptes FROM compta.v_historique_ecritures_total_1 GROUP BY partenaire_id) subview ON (subview.partenaire_id = t_partenaires.oid) WHERE oid > 0 AND oid in (SELECT oid FROM compta.p_oids WHERE code_table = ''partenaires_c'')  ORDER BY code', 'PARC'),
(1, 't_partenaires_h', 'Partenaires Honoraires', 'SELECT oid, code, texte, subview.comptes as complement, ''O'' as utilise FROM compta.t_partenaires LEFT JOIN (SELECT partenaire_id, base.cti_group_concat(DISTINCT compte_numero || '' '' ||  substr(compte_texte, 1 , 15)) as comptes FROM compta.v_historique_ecritures_total_1 GROUP BY partenaire_id) subview ON (subview.partenaire_id = t_partenaires.oid) WHERE oid > 0 AND oid in (SELECT oid FROM compta.p_oids WHERE code_table = ''partenaires_h'')  ORDER BY code', 'PARH'),
(1, 't_sites', 'Sites', 'SELECT oid, code, texte, '''' as complement, CASE WHEN oid in (SELECT oid FROM compta.p_oids WHERE code_table IN (''sites_c'', ''t_sites_h'')) THEN ''O'' ELSE ''N'' END as utilise FROM compta.t_sites WHERE oid > 0   ORDER BY code', 'SIT'),
(1, 't_sites_c', 'Sites Clinique', 'SELECT oid, code, texte, '''' as complement, ''O'' as utilise FROM compta.t_sites WHERE oid > 0 AND oid in (SELECT oid FROM compta.p_oids WHERE code_table = ''sites_c'')  ORDER BY code', 'SITC'),
(1, 't_sites_h', 'Sites Honoraires', 'SELECT oid, code, texte, '''' as complement, ''O'' as utilise FROM compta.t_sites WHERE oid > 0 AND oid in (SELECT oid FROM compta.p_oids WHERE code_table = ''sites_h'')  ORDER BY code', 'SITH'),
(1, 't_types_compta', 'Types comptabilité', 'SELECT oid, code, texte, '''' as complement, CASE WHEN oid in (SELECT oid FROM compta.p_oids WHERE code_table In (''types_compta_c'', ''types_compta_extra_c'', ''types_compta_h'', ''types_compta_extra_h'')) THEN ''O'' ELSE ''N'' END as utilise FROM compta.t_types_compta WHERE oid > 0   ORDER BY code', 'TYP'),
(1, 't_types_compta_c', 'Types comptabilité Clinique', 'SELECT oid, code, texte, '''' as complement, ''O'' as utilise FROM compta.t_types_compta WHERE oid > 0 AND oid in (SELECT oid FROM compta.p_oids WHERE code_table In (''types_compta_c'', ''types_compta_extra_c''))  ORDER BY code', 'TYPC'),
(1, 't_types_compta_h', 'Types comptabilité Honoraires', 'SELECT oid, code, texte, '''' as complement, ''O'' as utilise FROM compta.t_types_compta WHERE oid > 0 AND oid in (SELECT oid FROM compta.p_oids WHERE code_table In (''types_compta_h'', ''types_compta_extra_h''))  ORDER BY code', 'TYPH')
;

-- 3/3 : Màj de la table iCTI
UPDATE compta.t_listes_tables SET         code = w_dbsetup.code,        texte = w_dbsetup.texte,         select_cmd = w_dbsetup.select_cmd       FROM w_dbsetup       WHERE t_listes_tables.name = w_dbsetup.name;        INSERT INTO compta.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 compta.t_listes_tables);              INSERT INTO compta.t_listes_tables(code, name, texte, select_cmd)        SELECT code, name, texte, select_cmd FROM w_dbsetup WHERE oid <> 0 AND name NOT IN (SELECT name FROM compta.t_listes_tables);