Commit 66352319 authored by cosine's avatar cosine

- WIP - trying to add "collisions" to cosine resource-management report

parent b3a2b77a
......@@ -369,6 +369,7 @@ BEGIN
v_date := row.start_date;
WHILE (v_date <= row.end_date) LOOP
v := v_work_days[v_date - p_start_date];
IF v is NULL THEN exit; END IF;
v := v - v_perc;
if v < 0.0 THEN v := 0.0; END IF;
v_work_days[v_date - p_start_date] := v;
......
......@@ -42,6 +42,7 @@ BEGIN
v_date := row.start_date;
WHILE (v_date <= row.end_date) LOOP
v := v_work_days[v_date - p_start_date];
IF v is null THEN exit; END IF;
v := v - v_perc;
if v < 0.0 THEN v := 0.0; END IF;
v_work_days[v_date - p_start_date] := v;
......@@ -54,3 +55,73 @@ END;$body$ language 'plpgsql';
-- select im_resource_mgmt_work_days(624, '2019-12-23', '2019-12-30');
-- select im_resource_mgmt_work_days(463, '2018-12-01'::date, '2019-01-01');
-- Returns a real[] for each day between start and end
-- with 100 for a full absence (vacation, bank holiday, ...any)
create or replace function im_resource_mgmt_absence_days (integer, date, date)
returns float[] as $body$
DECLARE
p_user_id alias for $1;
p_start_date alias for $2;
p_end_date alias for $3;
v_weekday integer;
v_date date;
v_work_days float[];
v_date_difference integer;
v_perc float;
v float;
row record;
BEGIN
-- Initiate the result array with zeros
v_date := p_start_date;
WHILE (v_date <= p_end_date) LOOP
v_work_days[v_date - p_start_date] := 0;
v_date := v_date + 1;
END LOOP;
-- Apply absences
FOR row IN
select a.*
from im_user_absences a
where (a.owner_id = p_user_id OR a.group_id = p_user_id OR a.group_id in (select group_id from group_distinct_member_map where member_id = p_user_id)) and
a.end_date::date >= p_start_date and a.start_date::date <= p_end_date and
-- a.absence_type_id in (select * from im_sub_categories(5005)) and -- only bank holidays
a.absence_status_id not in (select * from im_sub_categories(16002) union select * from im_sub_categories(16006)) -- exclude deleted and rejected
LOOP
v_date_difference = 1 + row.end_date::date - row.start_date::date;
v_perc = 100.0 * row.duration_days / v_date_difference;
RAISE NOTICE 'im_resource_mgmt_work_days(%,%,%): Absence %', p_user_id, p_start_date, p_end_date, row.absence_name;
v_date := row.start_date;
WHILE (v_date <= row.end_date) LOOP
v := v_work_days[v_date - p_start_date];
IF v is null THEN exit; END IF;
v := v + v_perc;
if v > 100.0 THEN v := 100.0; END IF;
v_work_days[v_date - p_start_date] := v;
v_date := v_date + 1;
END LOOP;
END LOOP;
-- Set weekends to zero
WHILE (v_date <= p_end_date) LOOP
v_weekday := to_char(v_date, 'D');
IF v_weekday = 1 OR v_weekday = 7 THEN
v_work_days[v_date - p_start_date] := 0;
END IF;
v_date := v_date + 1;
END LOOP;
return v_work_days;
END;$body$ language 'plpgsql';
select im_resource_mgmt_work_days(55815, '2020-01-01', '2020-01-31');
select im_resource_mgmt_work_days_cosine(55815, '2020-01-01', '2020-01-31');
select im_resource_mgmt_absence_days(55815, '2020-01-01', '2020-01-31');
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment