return: text
lang: plpgsql
src: |
  DECLARE	
  	result TEXT;
  BEGIN
  	
  	EXECUTE 'INSERT INTO activite.t_etages_historique(
  			etage_id, 
  			date, 
  			nb_lits_theoriques, 
  			nb_lits_cp_theoriques, 
  			nb_lits_ouverts, 
  			nb_lits_cp_ouverts, 
  			est_ouvert_1, 
  			est_ouvert_2, 
  			est_ouvert_3, 
  			est_ouvert_4, 
  			est_ouvert_5, 
  			est_ouvert_6, 
  			est_ouvert_7)
  		SELECT
  			oid, 
  			''20070101'', 
  			COALESCE(nb_lits,0), 
  			COALESCE(nb_cp,0), 
  			COALESCE(nb_lits,0), 
  			COALESCE(nb_cp,0), 
  			''1'',
  			''1'',
  			''1'',
  			''1'',
  			''1'',
  			''1'',
  			''1''
  		FROM activite.t_etages
  			LEFT JOIN activite.t_etages_historique ON t_etages_historique.etage_id = t_etages.oid AND t_etages_historique.date = ''20070101''
  		WHERE t_etages_historique.etage_id IS NULL';
  	EXECUTE 'UPDATE activite.t_etages_historique SET
  			date_fin = date(COALESCE((( 
  				SELECT min(t_etages_historique_f.date) AS min
  				FROM activite.t_etages_historique t_etages_historique_f
  				WHERE t_etages_historique_f.etage_id = t_etages_historique.etage_id AND 
  					t_etages_historique_f.date > t_etages_historique.date)) - interval ''1 day'', ''2099-12-31''::date)) ';
  	EXECUTE 'UPDATE activite.t_etages
  		SET nb_lits = t_etages_historique.nb_lits_theoriques
  		FROM activite.t_etages_historique
  		WHERE t_etages.oid =  t_etages_historique.etage_id AND
  			date_fin = ''20991231''';
  	EXECUTE 'UPDATE activite.t_etages_historique SET 
  			heberge_hospitalises = ''0'',
  			heberge_ambulatoires = ''0'',
  			heberge_externes = ''0'',
  			heberge_urgences = ''0'',
  			heberge_bebes = ''0''';
  	
  	EXECUTE 'UPDATE activite.t_etages_historique SET 
  			heberge_hospitalises = subview.heberge_hospitalises,
  			heberge_ambulatoires = subview.heberge_ambulatoires,
  			heberge_externes = subview.heberge_externes,
  			heberge_urgences = subview.heberge_urgences,
  			heberge_bebes = subview.heberge_bebes
  		FROM (
  			SELECT
  				t_lieux_c.etage_id,
  				t_etages_historique.date,
  				MAX(CASE WHEN est_jour_hospitalisation = ''1'' THEN ''1'' ELSE ''0'' END) AS heberge_hospitalises,
  				MAX(CASE WHEN nb_ambulatoires > 0 THEN ''1'' ELSE ''0'' END) AS heberge_ambulatoires,
  				MAX(CASE WHEN nb_externes > 0 THEN ''1'' ELSE ''0'' END) AS heberge_externes,
  				MAX(CASE WHEN nb_urgences > 0 THEN ''1'' ELSE ''0'' END) AS heberge_urgences,
  				MAX(CASE WHEN nb_bebes > 0 THEN ''1'' ELSE ''0'' END) AS heberge_bebes
  			FROM activite.p_mouvements_sejour
  				JOIN activite.t_lieux_c ON lieu_id = t_lieux_c.oid
  				JOIN activite.t_etages_historique ON t_lieux_c.etage_id = t_etages_historique.etage_id AND p_mouvements_sejour.date BETWEEN t_etages_historique.date AND t_etages_historique.date_fin
  			GROUP BY 1,2
  			ORDER BY 1,2
  		) subview
  		WHERE subview.etage_id = t_etages_historique.etage_id AND subview.date = t_etages_historique.date;';
  				
  	EXECUTE 'DROP TABLE IF EXISTS w_etages_places;
  		DROP TYPE IF EXISTS w_etages_places;';
  	EXECUTE 'CREATE TEMP TABLE w_etages_places AS
  		SELECT 
  			t_etages.oid AS etage_id,
  			p_calendrier.date,
  			t_etages_historique.nb_lits_theoriques,
  			t_etages_historique.nb_lits_ouverts,
  			0::numeric AS nb_lits_cp_theoriques, 
  			0::numeric AS nb_lits_cp_ouverts, 
  			CASE WHEN date_part(''DOW'',p_calendrier.date) = 1 THEN est_ouvert_1
  			WHEN date_part(''DOW'',p_calendrier.date) = 2 THEN est_ouvert_2
  			WHEN date_part(''DOW'',p_calendrier.date) = 3 THEN est_ouvert_3
  			WHEN date_part(''DOW'',p_calendrier.date) = 4 THEN est_ouvert_4
  			WHEN date_part(''DOW'',p_calendrier.date) = 5 THEN est_ouvert_5
  			WHEN date_part(''DOW'',p_calendrier.date) = 6 THEN est_ouvert_6
  			WHEN date_part(''DOW'',p_calendrier.date) = 0 THEN est_ouvert_7
  			ELSE ''0'' END AS est_ouvert,
  			heberge_hospitalises,
  			heberge_ambulatoires,
  			heberge_externes,
  			heberge_urgences,
  			heberge_bebes
  		FROM activite.t_etages 
  			JOIN activite.t_etages_historique ON t_etages.oid = t_etages_historique.etage_id
  			JOIN base.p_calendrier ON p_calendrier.date BETWEEN t_etages_historique.date AND t_etages_historique.date_fin AND p_calendrier.date BETWEEN ''20070101'' AND now() + interval ''1 month''
  		ORDER BY 1,2;';
  	EXECUTE 'INSERT INTO activite.t_etages_places(
  			etage_id, 
  			date, 
  			nb_lits_theoriques, 
  			nb_lits_ouverts, 
  			nb_lits_cp_theoriques, 
  			nb_lits_cp_ouverts, 
  			est_ouvert,
  			heberge_hospitalises,
  			heberge_ambulatoires,
  			heberge_externes,
  			heberge_urgences,
  			heberge_bebes)
  		SELECT 
  			w_etages_places.etage_id,
  			w_etages_places.date,
  			w_etages_places.nb_lits_theoriques,
  			w_etages_places.nb_lits_ouverts,
  			w_etages_places.nb_lits_cp_theoriques, 
  			w_etages_places.nb_lits_cp_ouverts, 
  			w_etages_places.est_ouvert,
  			w_etages_places.heberge_hospitalises,
  			w_etages_places.heberge_ambulatoires,
  			w_etages_places.heberge_externes,
  			w_etages_places.heberge_urgences,
  			w_etages_places.heberge_bebes
  		FROM w_etages_places
  			LEFT JOIN activite.t_etages_places t_etages_places_deja ON w_etages_places.etage_id = t_etages_places_deja.etage_id AND w_etages_places.date = t_etages_places_deja.date
  		WHERE t_etages_places_deja.etage_id IS NULL; ';
  	EXECUTE 'UPDATE activite.t_etages_places SET 
  			nb_lits_theoriques = w_etages_places.nb_lits_theoriques, 
  			nb_lits_ouverts = w_etages_places.nb_lits_ouverts, 
  			nb_lits_cp_theoriques = w_etages_places.nb_lits_cp_theoriques, 
  			nb_lits_cp_ouverts = w_etages_places.nb_lits_cp_ouverts, 
  			est_ouvert = w_etages_places.est_ouvert,
  			heberge_hospitalises = w_etages_places.heberge_hospitalises,
  			heberge_ambulatoires = w_etages_places.heberge_ambulatoires,
  			heberge_externes = w_etages_places.heberge_externes,
  			heberge_urgences = w_etages_places.heberge_urgences,
  			heberge_bebes = w_etages_places.heberge_bebes
  		FROM w_etages_places
  		WHERE w_etages_places.etage_id = t_etages_places.etage_id AND w_etages_places.date = t_etages_places.date AND
  			(
  			t_etages_places.nb_lits_theoriques IS DISTINCT FROM w_etages_places.nb_lits_theoriques OR 
  			t_etages_places.nb_lits_ouverts IS DISTINCT FROM w_etages_places.nb_lits_ouverts OR 
  			t_etages_places.nb_lits_cp_theoriques IS DISTINCT FROM w_etages_places.nb_lits_cp_theoriques OR 
  			t_etages_places.nb_lits_cp_ouverts IS DISTINCT FROM w_etages_places.nb_lits_cp_ouverts OR 
  			t_etages_places.est_ouvert IS DISTINCT FROM w_etages_places.est_ouvert OR
  			t_etages_places.heberge_hospitalises IS DISTINCT FROM w_etages_places.heberge_hospitalises OR 
  			t_etages_places.heberge_ambulatoires IS DISTINCT FROM w_etages_places.heberge_ambulatoires OR 
  			t_etages_places.heberge_externes IS DISTINCT FROM w_etages_places.heberge_externes OR 
  			t_etages_places.heberge_urgences IS DISTINCT FROM w_etages_places.heberge_urgences OR 
  			t_etages_places.heberge_bebes IS DISTINCT FROM w_etages_places.heberge_bebes
  			);
  			
  		';
  	EXECUTE 'DROP TABLE IF EXISTS w_etages_places;
  		DROP TYPE IF EXISTS w_etages_places;';
  	RETURN 'OK';
  END;