Advertisement
Guest User

Untitled

a guest
May 14th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DROP TRIGGER IF EXISTS date_inscription_trigger ON reserve;
  2.  
  3. CREATE OR REPLACE FUNCTION date_inscription()
  4.   RETURNS trigger AS
  5. $BODY$
  6. DECLARE
  7. debut   timestamp;
  8. fin timestamp;
  9. s_id    int;
  10. local_deb time;
  11. local_fin time;
  12. BEGIN
  13. SELECT reserve.reserv_debut into debut FROM reserve WHERE locaux_id = NEW.locaux_id AND batiment_id = NEW.batiment_id;
  14. SELECT reserve.reserv_fin into fin FROM reserve WHERE locaux_id = NEW.locaux_id AND batiment_id = NEW.batiment_id;
  15. SELECT beneficie.status_id into s_id FROM beneficie WHERE cip = NEW.cip;
  16. SELECT locaux.horaire_depart into local_deb FROM locaux WHERE locaux_id = NEW.locaux_id AND batiment_id = NEW.batiment_id;
  17. SELECT locaux.horaire_fin into local_fin FROM locaux WHERE locaux_id = NEW.locaux_id AND batiment_id = NEW.batiment_id;
  18.  
  19. raise notice 'Value: %', local_deb;
  20. raise notice 'Value: %', debut::time;
  21. raise notice 'Value: %', local_fin;
  22. raise notice 'Value: %', fin::time;
  23.  
  24. IF ((NEW.reserv_debut > debut)
  25.     AND (NEW.reserv_debut > fin)
  26.     AND (NEW.reserv_fin > debut)
  27.     AND (NEW.reserv_fin > fin))
  28. OR ((NEW.reserv_debut < debut)
  29.     AND (NEW.reserv_debut < fin)
  30.     AND (NEW.reserv_fin < debut)
  31.     AND (NEW.reserv_fin < fin)) THEN
  32.    
  33.     IF(s_id = 2 OR s_id = 3) THEN
  34.         IF(local_deb < NEW.reserv_debut::time AND local_fin > NEW.reserv_fin::time) THEN
  35.             IF(to_seconds(debut) % 900 = 0 AND to_seconds(fin) % 900 = 0) THEN
  36.                 INSERT INTO journal(journal_id, cip, journal_description) VALUES (DEFAULT, NEW.cip, 'Creation de reservation');
  37.                 RETURN NEW;
  38.             ELSE
  39.                 RAISE EXCEPTION 'La reservation doit etre par tranches de 15 minutes';
  40.                 RETURN -1;
  41.             END IF;
  42.         ELSE
  43.             RAISE EXCEPTION 'Local est fermé pour cette période';
  44.             RETURN -1;
  45.         END IF;
  46.     ELSE
  47.         RAISE EXCEPTION 'User ne possede pas les permissions';
  48.         RETURN -1;
  49.     END IF;
  50. ELSE
  51. RAISE EXCEPTION 'Bloc dheure deja utilise';
  52. RETURN -1;
  53. END IF;
  54. END;
  55. $BODY$
  56. LANGUAGE 'plpgsql';
  57.  
  58.  
  59. CREATE TRIGGER date_inscription_trigger
  60.   BEFORE INSERT
  61.   ON reserve
  62.   FOR EACH ROW
  63.   EXECUTE PROCEDURE date_inscription();
  64.  
  65. CREATE OR REPLACE FUNCTION to_seconds(t timestamp)
  66.   RETURNS integer AS
  67. $BODY$
  68. DECLARE
  69.     hs INTEGER;
  70.     ms INTEGER;
  71.     s INTEGER;
  72. BEGIN
  73.     SELECT (EXTRACT( HOUR FROM  t::time) * 60*60) INTO hs;
  74.     SELECT (EXTRACT (MINUTES FROM t::time) * 60) INTO ms;
  75.     SELECT (EXTRACT (SECONDS from t::time)) INTO s;
  76.     SELECT (hs + ms + s) INTO s;
  77.     RETURN s;
  78. END;
  79. $BODY$
  80.   LANGUAGE 'plpgsql';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement