-- 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_forme_activite.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_forme_activite
    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 = 'FORMEACTIVITE'
    	AND t_classes_sections_elements.to_id = t_forme_activite.oid
    ORDER BY t_classes.sequence, t_classes_sections.code, t_forme_activite.code LIMIT 0
;

-- 2/3 : Peuplement de la table temporaire
INSERT INTO w_dbsetup(
    section_code,
    to_code,
    table_code,
    classe_sequence
)
VALUES
('1', '111', 'FORMEACTIVITE', 1),
('1', '112', 'FORMEACTIVITE', 1),
('1', '113', 'FORMEACTIVITE', 1),
('1', '114', 'FORMEACTIVITE', 1),
('1', '117', 'FORMEACTIVITE', 1),
('1', '118', 'FORMEACTIVITE', 1),
('1', '119', 'FORMEACTIVITE', 1),
('1', '121', 'FORMEACTIVITE', 1),
('1', '122', 'FORMEACTIVITE', 1),
('1', '123', 'FORMEACTIVITE', 1),
('1', '124', 'FORMEACTIVITE', 1),
('1', '127', 'FORMEACTIVITE', 1),
('1', '128', 'FORMEACTIVITE', 1),
('1', '129', 'FORMEACTIVITE', 1),
('1', '131', 'FORMEACTIVITE', 1),
('1', '132', 'FORMEACTIVITE', 1),
('1', '133', 'FORMEACTIVITE', 1),
('1', '139', 'FORMEACTIVITE', 1),
('2', '211', 'FORMEACTIVITE', 1),
('2', '212', 'FORMEACTIVITE', 1),
('2', '217', 'FORMEACTIVITE', 1),
('2', '218', 'FORMEACTIVITE', 1),
('2', '219', 'FORMEACTIVITE', 1),
('2', '221', 'FORMEACTIVITE', 1),
('2', '222', 'FORMEACTIVITE', 1),
('2', '227', 'FORMEACTIVITE', 1),
('2', '228', 'FORMEACTIVITE', 1),
('2', '229', 'FORMEACTIVITE', 1),
('3', '311', 'FORMEACTIVITE', 1),
('3', '317', 'FORMEACTIVITE', 1),
('3', '318', 'FORMEACTIVITE', 1),
('3', '319', 'FORMEACTIVITE', 1),
('3', '321', 'FORMEACTIVITE', 1),
('3', '327', 'FORMEACTIVITE', 1),
('3', '328', 'FORMEACTIVITE', 1),
('3', '329', 'FORMEACTIVITE', 1),
('4', '401', 'FORMEACTIVITE', 1),
('5', '501', 'FORMEACTIVITE', 1),
('5', '502', 'FORMEACTIVITE', 1),
('5', '503', 'FORMEACTIVITE', 1),
('7', '701', 'FORMEACTIVITE', 1),
('7', '702', 'FORMEACTIVITE', 1),
('7', '709', 'FORMEACTIVITE', 1),
('9', '***', 'FORMEACTIVITE', 1),
('9', '999', 'FORMEACTIVITE', 1),
('11', '111', 'FORMEACTIVITE', 2),
('11', '121', 'FORMEACTIVITE', 2),
('12', '112', 'FORMEACTIVITE', 2),
('12', '122', 'FORMEACTIVITE', 2),
('13', '113', 'FORMEACTIVITE', 2),
('13', '123', 'FORMEACTIVITE', 2),
('14', '114', 'FORMEACTIVITE', 2),
('14', '124', 'FORMEACTIVITE', 2),
('15', '131', 'FORMEACTIVITE', 2),
('15', '132', 'FORMEACTIVITE', 2),
('15', '133', 'FORMEACTIVITE', 2),
('15', '139', 'FORMEACTIVITE', 2),
('19', '117', 'FORMEACTIVITE', 2),
('19', '118', 'FORMEACTIVITE', 2),
('19', '119', 'FORMEACTIVITE', 2),
('19', '127', 'FORMEACTIVITE', 2),
('19', '128', 'FORMEACTIVITE', 2),
('19', '129', 'FORMEACTIVITE', 2),
('20', '211', 'FORMEACTIVITE', 2),
('20', '212', 'FORMEACTIVITE', 2),
('20', '217', 'FORMEACTIVITE', 2),
('20', '218', 'FORMEACTIVITE', 2),
('20', '219', 'FORMEACTIVITE', 2),
('20', '221', 'FORMEACTIVITE', 2),
('20', '222', 'FORMEACTIVITE', 2),
('20', '227', 'FORMEACTIVITE', 2),
('20', '228', 'FORMEACTIVITE', 2),
('20', '229', 'FORMEACTIVITE', 2),
('30', '311', 'FORMEACTIVITE', 2),
('30', '317', 'FORMEACTIVITE', 2),
('30', '318', 'FORMEACTIVITE', 2),
('30', '319', 'FORMEACTIVITE', 2),
('30', '321', 'FORMEACTIVITE', 2),
('30', '327', 'FORMEACTIVITE', 2),
('30', '328', 'FORMEACTIVITE', 2),
('30', '329', 'FORMEACTIVITE', 2),
('40', '401', 'FORMEACTIVITE', 2),
('50', '501', 'FORMEACTIVITE', 2),
('50', '502', 'FORMEACTIVITE', 2),
('50', '503', 'FORMEACTIVITE', 2),
('70', '701', 'FORMEACTIVITE', 2),
('70', '702', 'FORMEACTIVITE', 2),
('70', '709', 'FORMEACTIVITE', 2),
('99', '***', 'FORMEACTIVITE', 2),
('99', '999', 'FORMEACTIVITE', 2),
('11', '121', 'FORMEACTIVITE', 3),
('11', '122', 'FORMEACTIVITE', 3),
('11', '123', 'FORMEACTIVITE', 3),
('11', '124', 'FORMEACTIVITE', 3),
('11', '127', 'FORMEACTIVITE', 3),
('11', '128', 'FORMEACTIVITE', 3),
('11', '129', 'FORMEACTIVITE', 3),
('12', '111', 'FORMEACTIVITE', 3),
('12', '112', 'FORMEACTIVITE', 3),
('12', '113', 'FORMEACTIVITE', 3),
('12', '114', 'FORMEACTIVITE', 3),
('12', '117', 'FORMEACTIVITE', 3),
('12', '118', 'FORMEACTIVITE', 3),
('12', '119', 'FORMEACTIVITE', 3),
('2', '211', 'FORMEACTIVITE', 3),
('2', '212', 'FORMEACTIVITE', 3),
('2', '217', 'FORMEACTIVITE', 3),
('2', '218', 'FORMEACTIVITE', 3),
('2', '219', 'FORMEACTIVITE', 3),
('2', '221', 'FORMEACTIVITE', 3),
('2', '222', 'FORMEACTIVITE', 3),
('2', '227', 'FORMEACTIVITE', 3),
('2', '228', 'FORMEACTIVITE', 3),
('2', '229', 'FORMEACTIVITE', 3),
('3', '311', 'FORMEACTIVITE', 3),
('3', '317', 'FORMEACTIVITE', 3),
('3', '318', 'FORMEACTIVITE', 3),
('3', '319', 'FORMEACTIVITE', 3),
('3', '321', 'FORMEACTIVITE', 3),
('3', '327', 'FORMEACTIVITE', 3),
('3', '328', 'FORMEACTIVITE', 3),
('3', '329', 'FORMEACTIVITE', 3),
('4', '401', 'FORMEACTIVITE', 3),
('9', '***', 'FORMEACTIVITE', 3),
('9', '131', 'FORMEACTIVITE', 3),
('9', '132', 'FORMEACTIVITE', 3),
('9', '133', 'FORMEACTIVITE', 3),
('9', '139', 'FORMEACTIVITE', 3),
('9', '501', 'FORMEACTIVITE', 3),
('9', '502', 'FORMEACTIVITE', 3),
('9', '503', 'FORMEACTIVITE', 3),
('9', '701', 'FORMEACTIVITE', 3),
('9', '702', 'FORMEACTIVITE', 3),
('9', '709', 'FORMEACTIVITE', 3),
('9', '999', 'FORMEACTIVITE', 3)
;

-- 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_forme_activite.oid 
FROM 
	w_dbsetup, 
	activite.t_listes_tables, 
	activite.t_classes, 
	activite.t_classes_sections, 
	activite.t_forme_activite   
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_forme_activite.code
	AND t_classes_sections.oid::text || '-'::text || t_forme_activite.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('FORMEACTIVITE');