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

CREATE TEMP TABLE w_dbsetup AS
    SELECT
    	t_classes_sections.code AS section_code, 
    	t_prestations.code as to_code, 
    	t_listes_tables.code as table_code, 
    	t_classes.sequence as classe_sequence 
    FROM 
    	activite.t_classes_sections_elements, 
    	activite.t_classes_sections, 
    	activite.t_classes, 
    	activite.t_listes_tables, 
    	activite.t_prestations
    WHERE 
    	t_classes_sections_elements.section_id = t_classes_sections.oid
    	AND t_classes_sections.classe_id = t_classes.oid 
    	AND t_classes.is_cti = '1' 
    	AND table_id = t_listes_tables.oid
    	AND t_listes_tables.code = 'PRESTA_C'
    	AND t_classes_sections_elements.to_id = t_prestations.oid
    ORDER BY t_classes.sequence, t_classes_sections.code, t_prestations.code LIMIT 0
;

-- 2/3 : Peuplement de la table temporaire
INSERT INTO w_dbsetup(
    section_code,
    to_code,
    table_code,
    classe_sequence
)
VALUES
('S11', 'GHS', 'PRESTA_C', 9),
('S12', 'EXH', 'PRESTA_C', 9),
('S13', 'ANT', 'PRESTA_C', 9),
('S13', 'APE', 'PRESTA_C', 9),
('S13', 'ATU', 'PRESTA_C', 9),
('S13', 'D01', 'PRESTA_C', 9),
('S13', 'D02', 'PRESTA_C', 9),
('S13', 'D03', 'PRESTA_C', 9),
('S13', 'D04', 'PRESTA_C', 9),
('S13', 'D05', 'PRESTA_C', 9),
('S13', 'D06', 'PRESTA_C', 9),
('S13', 'D07', 'PRESTA_C', 9),
('S13', 'D08', 'PRESTA_C', 9),
('S13', 'D09', 'PRESTA_C', 9),
('S13', 'D10', 'PRESTA_C', 9),
('S13', 'D11', 'PRESTA_C', 9),
('S13', 'D12', 'PRESTA_C', 9),
('S13', 'D13', 'PRESTA_C', 9),
('S13', 'D14', 'PRESTA_C', 9),
('S13', 'D15', 'PRESTA_C', 9),
('S13', 'D16', 'PRESTA_C', 9),
('S13', 'D17', 'PRESTA_C', 9),
('S13', 'D18', 'PRESTA_C', 9),
('S13', 'D19', 'PRESTA_C', 9),
('S13', 'D20', 'PRESTA_C', 9),
('S13', 'D21', 'PRESTA_C', 9),
('S13', 'D22', 'PRESTA_C', 9),
('S13', 'D23', 'PRESTA_C', 9),
('S13', 'D24', 'PRESTA_C', 9),
('S13', 'DIP', 'PRESTA_C', 9),
('S13', 'DTP', 'PRESTA_C', 9),
('S13', 'ENT', 'PRESTA_C', 9),
('S13', 'FFM', 'PRESTA_C', 9),
('S13', 'FPU', 'PRESTA_C', 9),
('S13', 'NN1', 'PRESTA_C', 9),
('S13', 'NN2', 'PRESTA_C', 9),
('S13', 'NN3', 'PRESTA_C', 9),
('S13', 'PO1', 'PRESTA_C', 9),
('S13', 'PO2', 'PRESTA_C', 9),
('S13', 'PO3', 'PRESTA_C', 9),
('S13', 'PO4', 'PRESTA_C', 9),
('S13', 'PO5', 'PRESTA_C', 9),
('S13', 'PO6', 'PRESTA_C', 9),
('S13', 'PO7', 'PRESTA_C', 9),
('S13', 'PO8', 'PRESTA_C', 9),
('S13', 'PO9', 'PRESTA_C', 9),
('S13', 'RAP', 'PRESTA_C', 9),
('S13', 'REA', 'PRESTA_C', 9),
('S13', 'REP', 'PRESTA_C', 9),
('S13', 'SE1', 'PRESTA_C', 9),
('S13', 'SE2', 'PRESTA_C', 9),
('S13', 'SE3', 'PRESTA_C', 9),
('S13', 'SE4', 'PRESTA_C', 9),
('S13', 'SE5', 'PRESTA_C', 9),
('S13', 'SE6', 'PRESTA_C', 9),
('S13', 'SE7', 'PRESTA_C', 9),
('S13', 'SFC', 'PRESTA_C', 9),
('S13', 'SNS', 'PRESTA_C', 9),
('S13', 'SRA', 'PRESTA_C', 9),
('S13', 'SRC', 'PRESTA_C', 9),
('S13', 'SSC', 'PRESTA_C', 9),
('S13', 'STF', 'PRESTA_C', 9),
('S19', 'FJA', 'PRESTA_C', 9),
('S19', 'FJC', 'PRESTA_C', 9),
('S19', 'FJL', 'PRESTA_C', 9),
('S19', 'FJR', 'PRESTA_C', 9),
('S19', 'FR2', 'PRESTA_C', 9),
('S19', 'FR3', 'PRESTA_C', 9),
('S19', 'FRL', 'PRESTA_C', 9),
('S19', 'FSD', 'PRESTA_C', 9),
('S19', 'FTG', 'PRESTA_C', 9),
('S19', 'FTN', 'PRESTA_C', 9),
('S19', 'FTR', 'PRESTA_C', 9),
('S19', 'GHT', 'PRESTA_C', 9),
('S19', 'PHJ', 'PRESTA_C', 9),
('S19', 'PJ', 'PRESTA_C', 9),
('S19', 'PMH', 'PRESTA_C', 9),
('S19', 'PY0', 'PRESTA_C', 9),
('S19', 'PY1', 'PRESTA_C', 9),
('S19', 'PY2', 'PRESTA_C', 9),
('S19', 'PY3', 'PRESTA_C', 9),
('S19', 'PY4', 'PRESTA_C', 9),
('S19', 'PY5', 'PRESTA_C', 9),
('S19', 'PY6', 'PRESTA_C', 9),
('S19', 'PY7', 'PRESTA_C', 9),
('S19', 'PY8', 'PRESTA_C', 9),
('S19', 'PY9', 'PRESTA_C', 9),
('S19', 'VDE', 'PRESTA_C', 9),
('S2', 'ETI', 'PRESTA_C', 9),
('S2', 'GLU', 'PRESTA_C', 9),
('S2', 'LEN', 'PRESTA_C', 9),
('S2', 'LUN', 'PRESTA_C', 9),
('S2', 'MAC', 'PRESTA_C', 9),
('S2', 'MAD', 'PRESTA_C', 9),
('S2', 'MAR', 'PRESTA_C', 9),
('S2', 'OP1', 'PRESTA_C', 9),
('S2', 'OP2', 'PRESTA_C', 9),
('S2', 'OP3', 'PRESTA_C', 9),
('S2', 'OP4', 'PRESTA_C', 9),
('S2', 'OP5', 'PRESTA_C', 9),
('S2', 'OP6', 'PRESTA_C', 9),
('S2', 'OPM', 'PRESTA_C', 9),
('S2', 'OPT', 'PRESTA_C', 9),
('S2', 'ORP', 'PRESTA_C', 9),
('S2', 'P', 'PRESTA_C', 9),
('S2', 'PA', 'PRESTA_C', 9),
('S2', 'PAN', 'PRESTA_C', 9),
('S2', 'PAU', 'PRESTA_C', 9),
('S2', 'PEX', 'PRESTA_C', 9),
('S2', 'PII', 'PRESTA_C', 9),
('S2', 'PME', 'PRESTA_C', 9),
('S2', 'POC', 'PRESTA_C', 9),
('S2', 'VEH', 'PRESTA_C', 9),
('S2', 'VER', 'PRESTA_C', 9),
('S3', 'EMI', 'PRESTA_C', 9),
('S3', 'PH1', 'PRESTA_C', 9),
('S3', 'PH4', 'PRESTA_C', 9),
('S3', 'PH7', 'PRESTA_C', 9),
('S3', 'PH8', 'PRESTA_C', 9),
('S3', 'PHH', 'PRESTA_C', 9),
('S3', 'PHP', 'PRESTA_C', 9),
('S3', 'PHQ', 'PRESTA_C', 9),
('S3', 'PHS', 'PRESTA_C', 9),
('S3', 'PHU', 'PRESTA_C', 9),
('S3', 'PMR', 'PRESTA_C', 9)
;

-- 3/3 : Màj de la table iCTI
INSERT INTO activite.t_classes_sections_elements(section_id, to_id)     
SELECT 
	t_classes_sections.oid, 
	t_prestations.oid 
FROM 
	w_dbsetup, 
	activite.t_listes_tables, 
	activite.t_classes, 
	activite.t_classes_sections, 
	activite.t_prestations   
WHERE 
	w_dbsetup.table_code = t_listes_tables.code       
	AND t_classes.table_id = t_listes_tables.oid   
	AND t_classes_sections.classe_id = t_classes.oid   
	AND w_dbsetup.section_code = t_classes_sections.code
	AND w_dbsetup.classe_sequence = t_classes.sequence
	AND w_dbsetup.to_code = t_prestations.code
	AND t_classes_sections.oid::text || '-'::text || t_prestations.oid::text NOT IN (SELECT section_id::text || '-'::text || to_id::text FROM activite.t_classes_sections_elements);

SELECT * FROM activite.cti_update_schema_classes('PRESTA_C');