0% found this document useful (0 votes)
4 views5 pages

Settings Functions

The document outlines a PL/SQL function that generates reservation slots for middle office personnel based on specific settings and availability. It retrieves relevant data from multiple tables, checks availability for each day of the week, and inserts reservation slots into the database if conditions are met. If no slots can be created, it returns a failure message indicating the reason.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Settings Functions

The document outlines a PL/SQL function that generates reservation slots for middle office personnel based on specific settings and availability. It retrieves relevant data from multiple tables, checks availability for each day of the week, and inserts reservation slots into the database if conditions are met. If no slots can be created, it returns a failure message indicating the reason.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

CREATE OR REPLACE FUNCTION generate_reservation_slots(uuid IN VARCHAR2,from_time

varchar2)
RETURN VARCHAR2
IS

settingDetails "settings"%rowtype;
middleOfficeUuid middle_office_type;
slots_days day_table_type;
slotsCount slot_table_type;
counter NUMBER := 0;
formattedDate VARCHAR2(10);
durations VARCHAR2(100);
slotDuration VARCHAR2(100);
slotDurationPeriod varchar2(100);
fromTime varchar2(50);
fromTime1 varchar2(50);
fromHours varchar2(50);
fromMinutes varchar2(50);
slotFromTime VARCHAR2(50);
slotToTime NUMBER := 0;
slotToTimeDuration VARCHAR2(50);
toTime VARCHAR2(50);

startDate DATE;
endDate DATE;

loopDate DATE;
loopVar DATE;
slots_booked slots_table_type;
begin

--selecting settings data for particular uuid:

select *
into settingDetails
from "settings"
where "uuid" = uuid;
dbms_output.put_line(settingDetails."uuid");

--selecting all middlepersons uuid's:

select uuid_list("uuid")
bulk collect into middleOfficeUuid
from "middle_office_person";
--selecting all the days of the week:

select day_type(slot_availability) bulk collect into slots_days from get_days();

-- Check if there are middle office persons UUIDs

IF middleOfficeUuid.COUNT != 0 THEN

FOR i IN 1..middleOfficeUuid.COUNT
LOOP
startDate := TRUNC(SYSDATE);
endDate := TRUNC(SYSDATE) + 6;
loopVar := startDate;
loopDate := startDate;
dbms_output.put_line(middleOfficeUuid(i).uuid);
dbms_output.put_line(TO_CHAR(loopVar, 'D'));
WHILE loopVar <= endDate
LOOP

IF TO_CHAR(loopVar, 'D') >= 2 AND TO_CHAR(loopVar, 'D') <= 6 THEN

IF INSTR(settingDetails."slot_availability", slots_days(TO_CHAR(loopVar, 'D')-


1).slot_availability) > 0 THEN
formattedDate := TO_CHAR(loopVar, 'MM/DD/YYYY');
dbms_output.put_line(slots_days(TO_CHAR(loopVar, 'D')-1).slot_availability);
dbms_output.put_line(formattedDate);

--Retriving particular obs slot count

select slot_object(count("id")) bulk collect into slotsCount from "reservation_slot" slot


where trunc(slot."date") = trunc(to_date('05/30/2023','MM/DD/YYYY')) and
slot."uuid" = middleOfficeUuid(i).uuid;

dbms_output.put_line(slotsCount(1).obCount);

if slotsCount(1).obCount < settingDetails."slots_per_day" then


counter := counter + 1;
durations := settingDetails."duration_each_slot";
dbms_output.put_line(durations);
slotDuration := SUBSTR(durations, 1, INSTR(durations, ' ') - 1);
dbms_output.put_line(slotDuration);
slotDurationPeriod := SUBSTR(slotDuration,1, INSTR(slotDuration,' ') -1);
dbms_output.put_line(slotDurationPeriod);
fromTime := from_time;

for i in 1..settingDetails."slots_per_day" loop


fromTime1 := REGEXP_SUBSTR(fromTime, '[0-9.]+', 1, 1);
dbms_output.put_line(fromTime1);
fromHours := TO_NUMBER(SUBSTR(fromTime1, 1, 2));
dbms_output.put_line(fromHours);
fromMinutes := TO_NUMBER(SUBSTR(fromTime1, 4));
dbms_output.put_line(fromMinutes);

IF LENGTH(fromTime1) = 4 THEN
fromMinutes := fromMinutes * 10;
dbms_output.put_line(fromMinutes);
END IF;

IF loopVar = startDate THEN


IF fromHours < EXTRACT(HOUR FROM current_timestamp) AND fromMinutes <
EXTRACT(MINUTE FROM current_timestamp) THEN
fromHours := EXTRACT(HOUR FROM current_timestamp);
fromMinutes := EXTRACT(MINUTE FROM current_timestamp);
ELSIF fromHours = EXTRACT(HOUR FROM current_timestamp) AND
fromMinutes < EXTRACT(MINUTE FROM current_timestamp) THEN
fromMinutes := EXTRACT(MINUTE FROM current_timestamp);
ELSIF fromHours < EXTRACT(HOUR FROM current_timestamp) THEN
fromHours := EXTRACT(HOUR FROM current_timestamp);
END IF;

slotFromTime := TO_CHAR(fromHours, 'FM00') || '.' || TO_CHAR(fromMinutes,


'FM00');

IF TO_NUMBER(SUBSTR(slotDurationPeriod, 1, INSTR(slotDurationPeriod, ':') - 1))


= 0 THEN
slotToTime := (fromHours * 60) + fromMinutes +
TO_NUMBER(SUBSTR(slotDurationPeriod, INSTR(slotDurationPeriod, ':') + 1));
ELSE
slotToTime := (fromHours * 60) + fromMinutes +
TO_NUMBER(SUBSTR(slotDurationPeriod, INSTR(slotDurationPeriod, ':') + 1)) +
(TO_NUMBER(SUBSTR(slotDurationPeriod, 1, INSTR(slotDurationPeriod, ':') - 1)) * 60);
END IF;
ELSE
IF TO_NUMBER(SUBSTR(slotDurationPeriod, 1, INSTR(slotDurationPeriod, ':') - 1))
= 0 THEN
slotToTime := (fromHours * 60) + fromMinutes +
TO_NUMBER(SUBSTR(slotDurationPeriod, INSTR(slotDurationPeriod, ':') + 1));
ELSE
slotToTime := (fromHours * 60) + fromMinutes +
TO_NUMBER(SUBSTR(slotDurationPeriod, INSTR(slotDurationPeriod, ':') + 1)) +
(TO_NUMBER(SUBSTR(slotDurationPeriod, 1, INSTR(slotDurationPeriod, ':') - 1)) * 60);
END IF;
END IF;

IF slotToTime != 0 THEN
toTime := TO_CHAR(TRUNC(slotToTime / 60), 'FM00') || '.' ||
TO_CHAR(MOD(slotToTime, 60), 'FM00');
END IF;

slotToTimeDuration:= toTime;
--inserting the records --
INSERT INTO "reservation_slot"(
"id",
"date",
"from_time",
"to_time",
"allowed_clients",
"client_id",
"status",
"uuid",
"created_by",
"updated_by",
"created_at",
"updated_at",
"zoom_id"
)
VALUES (SYS_GUID(),TO_DATE('10/23/2023',
'MM/DD/YYYY'),'10.30','11.30',1,null,'Open','324-7785765143-12421','235235-
23523523','23523523523---235',
current_timestamp,current_timestamp,'2152525');

COMMIT;

end loop;
end if;
END IF;
end IF;
loopDate := loopDate + 1;
loopVar := loopDate;
end loop;
end loop;
IF counter = 0 THEN
RETURN '{"status":"FAIL","message":"All OBS slots for this week are already created"}';
END IF;
-- Insert reservation slots into the database
-- FOR i IN 1..1 loop
-- INSERT INTO "reservation_slot"(
-- "id",
-- "date",
-- "from_time",
-- "to_time",
-- "allowed_clients",
-- "client_id",
-- "status",
-- "uuid",
-- "created_by",
-- "updated_by",
-- "created_at",
-- "updated_at",
-- "zoom_id"
-- )
-- VALUES (SYS_GUID(),TO_DATE('10/23/2023',
'MM/DD/YYYY'),'10.30','11.30',1,null,'Open','324-7785765143-12421','235235-
23523523','23523523523---235',
-- current_timestamp,current_timestamp,'2152525');
-- end loop;
COMMIT;
else
RETURN '{"status":"FAIL","message":"Middle Office Person details not found"}';
end if;

end;
/

You might also like