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

CREATE TEMP TABLE w_dbsetup AS
    SELECT
    t_dashboard_widget.oid, 
    t_chart.code as chart_code,
    t_dashboard.code as dashboard_code,
    t_dashboard_widget.deprecated
		FROM rh.t_dashboard_widget
		LEFT JOIN rh.t_chart ON t_chart.oid = chart_id
		LEFT JOIN rh.t_dashboard ON t_dashboard.oid = dashboard_id
		ORDER BY 1 LIMIT 0
;

-- 2/3 : Peuplement de la table temporaire
INSERT INTO w_dbsetup(
    oid,
    chart_code,
    dashboard_code,
    deprecated
)
VALUES
(1, 'CTI_STATTIME', 'CTI_EGAPRO', 't'),
(1, 'CTI_TYPECNT', 'CTI_EGAPRO', 'f'),
(1, 'CTI_STAT', 'CTI_EGAPRO', 'f'),
(1, 'CTI_EFFANC', 'CTI_EGAPRO', 'f'),
(1, 'CTI_ENTSORCAR', 'CTI_EGAPRO', 'f'),
(1, 'CTI_MSCSPMAP', 'CTI_EGAPRO', 'f'),
(1, 'CTI_MSAGE', 'CTI_EGAPRO', 'f'),
(1, 'CTI_EFFGEN', 'CTI_EGAPRO', 'f'),
(1, 'CTI_EFFAGE', 'CTI_EGAPRO', 'f'),
(1, 'CTI_EFFSTAT', 'CTI_EGAPRO', 'f'),
(1, 'CTI_MSCSP', 'CTI_EGAPRO', 'f')
;

-- 3/3 : Màj de la table iCTI
TRUNCATE rh.t_dashboard_widget;

INSERT INTO rh.t_dashboard_widget(dashboard_id, chart_id, deprecated)
	SELECT
		t_dashboard.oid,
		t_chart.oid,
		w_dbsetup.deprecated
	FROM w_dbsetup
	LEFT JOIN rh.t_chart ON t_chart.code = chart_code
	LEFT JOIN rh.t_dashboard ON t_dashboard.code = dashboard_code
;