1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| create or replace function func_SaisieHeure (vUserId int4, t_deb_sem int4, t_fin_sem int4) returns void as $$
declare
referrer_id_tache RECORD;
referrer_timestamp record;
vDate timestamp with time zone;
vDay int4;
vRow int4;
-- curseur qui récupère tous les id des taches sur lesquelles la ressource à travailler
cId_tache cursor (id int4) is
select id_tache
from a_travaille
where id_tache in (SELECT id_tache
FROM a_travaille
WHERE id_ressource= id);
-- curseur récupérant des heures en fonction d'un timestamp, d'une tache et pour une semaine donnée
cT cursor (tm_deb_sem int4, tm_fin_sem int4, id_tache_param int4) is
select jour_travaille, id_type_champ_saisie
from a_travaille
where id_tache = id_tache_param
and atr.jour_travaille > tm_deb_sem
and atr.jour_travaille < tm_fin_sem;
begin
-- création de la table temporaire contenant les informations nécessaires pour la saisie des heures
drop table if exists tab_temp_welcome_hours;
create global temporary table tab_temp_welcome_hours as
SELECT t.id_tache, t.description_tache, typ.label_type_tache, p.nom_projet, t.date_deb_tache, t.date_fin_tache, t.duree_limite
FROM tache t
join type_tache typ on typ.id_type_tache = t.id_type_tache
join projet p on p.id_projet = t.id_projet
WHERE t.id_tache IN (SELECT id_tache
FROM a_travaille
WHERE id_ressource= vUserId);
-- ajout des colonnes correspondant aux jours
alter table tab_temp_welcome_hours
add column Lundi varchar(10);
alter table tab_temp_welcome_hours
add column Mardi varchar(10);
alter table tab_temp_welcome_hours
add column Mercredi varchar(10);
alter table tab_temp_welcome_hours
add column Jeudi varchar(10);
alter table tab_temp_welcome_hours
add column Vendredi varchar(10);
alter table tab_temp_welcome_hours
add column Samedi varchar(10);
for referrer_id_tache in cId_tache (vUserId) LOOP
for referrer_timestamp in cT (t_deb_sem, t_fin_sem, referrer_id_tache) LOOP
-- récupère le timestamp format Unix epoch pour le convertir en timestamp with time zone pour pouvoir utiliser la fonction extract
select to_timestamp(referrer_timestamp.jour_travaille)
into vDate;
-- extrait le nombre du jour de la date fourni en argument sachant que 0 est un dimanche et 6 un samedi
select extract(dow from timestamp ''||vDate||'')
into vDay;
if(vDay = 1) then
insert into tab_temp_welcome_hours (Lundi) values (referrer_timestamp.id_type_champ_saisie);
end if;
if(vDay = 2) then
insert into tab_temp_welcome_hours (Mardi) values (referrer_timestamp.id_type_champ_saisie);
end if;
if(vDay = 3) then
insert into tab_temp_welcome_hours (Mercredi) values (referrer_timestamp.id_type_champ_saisie);
end if;
if(vDay = 4) then
insert into tab_temp_welcome_hours (Jeudi) values (referrer_timestamp.id_type_champ_saisie);
end if;
if(vDay = 5) then
insert into tab_temp_welcome_hours (Vendredi) values (referrer_timestamp.id_type_champ_saisie);
end if;
if(vDay = 6) then
insert into tab_temp_welcome_hours (Samedi) values (referrer_timestamp.id_type_champ_saisie);
end if;
GET DIAGNOSTICS vRow = ROW_COUNT;
if vRow > 0 then
commit;
else
RAISE EXCEPTION 'problème au niveau des insert, vérifiez les tables, colonnes etc';
end if;
end loop;
end loop;
end;
$$ LANGUAGE plpgsql; |
Partager