====== pears.NetOwnerDiarySet ======
Generated schema reference. Regenerate this page from the SQL unload; keep hand-maintained business notes in the narrative namespace.
===== Original SQL =====
create procedure "pears"."NetOwnerDiarySet"( in "pWebUserID" char(20),in "pDiaryID" char(20) default null,in "pAction" char(1),in "pEventType" char(1) default null,in "pStaffID" char(20) default null,in "pPersonID" char(20) default null,in "pEmploymentID" char(20) default null,in "pStartDate" char(20) default null,in "pEndDate" char(20) default null,in "pStartTime" char(20) default null,in "pEndTime" char(20) default null,in "pDescription" char(100) default null,in "pNotes" long varchar default null,in "pPriority" char(1) default null,in "pMinutesBefore" integer default null )
result( "pResult" char(250) )
begin
// IQXWeb
declare "dEndDateTime" "datetime";
declare "dStartDateTime" "datetime";
if "pAction" not in( 'N','U','D' ) then
select '3:~Invalid pAction';
return
end if;
if "pEventType" not in( 'A','R' ) and "pAction" = 'U' then
select '6:~Invalid Event Type';
return
end if;
if "isnull"("pDiaryID",'') = '' and "pAction" in( 'U','D' ) then -- must have a DiaryID for update or delete
select '2:~pDiaryID required';
return
end if;
if "pAction" <> 'D' then
if "isnull"("pStartDate",'') <> '' then
set "dStartDateTime" = "iqxnetstringtodate"("pstartdate")+"iqxnetstringtotime"("isnull"("pstarttime",'00:00'));
if "dStartDateTime" is null then
select '107:~Invalid start date or time';
return
end if end if;
if "isnull"("pEndDate",'') <> '' then
set "dEndDateTime" = "iqxnetstringtodate"("penddate")+"iqxnetstringtotime"("isnull"("pendtime",'00:00'));
if "dEndDateTime" is null then
select '107:~Invalid end date or time';
return
end if end if;
if "dEndDateTime" < "dStartDateTime" then
select '108:~Finish time appears to be before Start time';
return
end if;
if "pAction" = 'N' and "dEndDateTime" is null then
set "dEndDateTime" = "dStartDateTime"
end if end if;
case "pAction"
when 'N' then
if "dStartDateTime" is null then
select '104:~pStartDate required';
return
end if;
if "trim"("isnull"("pDescription",'')) = '' then
select '105:~pDescription required';
return
end if;
if "pMinutesBefore" > 30240 then
select '106:~Minutes Before cannot exceed 30240 minutes (or 21 days)';
return
end if;
if "isnull"("pStaffID",'') = '' then
set "pStaffID" = (select "staffid" from "iqxnetuser" where "iqxnetuserid" = "pWebUserID")
end if;
insert into "diary"( "diaryid","staffid","personid","employmentid","diaryfrom","diaryto","description","notes","priority","PopupMinutes" ) values
( "isnull"("pDiaryID","uniquekey"('x')),"pStaffID","pPersonID","pEmploymentID","dStartDateTime","dEndDateTime","pDescription","pNotes","pPriority","pMinutesBefore" ) ;
select '0:~Success';
return
when 'U' then -- Update record
if "pEventType" = 'R' then
set "dEndDateTime" = "dStartDateTime"
end if;
update "diary" set "staffid" = "isnull"("pStaffID","staffid"),"personid" = "isnull"("pPersonID","personid"),"employmentid" = "isnull"("pEmploymentID","employmentid"),"diaryfrom" = "isnull"("dStartDateTime","diaryfrom"),"diaryto" = "isnull"("dEndDateTime","diaryto"),"description" = "isnull"("pDescription","description"),"notes" = "isnull"("pNotes","notes"),"priority" = "isnull"("pPriority","priority"),"durationtype" = null,"PopupMinutes" = "pMinutesBefore","PopupTime" = "dateadd"("minute",0-"pMinutesBefore","DiaryFrom") where "DiaryID" = "pDiaryID";
-- Note setting durationtype=null to prevent the trigger mucking up the diaryto based on previous stored duration
select '0:~Success';
return
when 'D' then -- delete record
delete from "diary" where "diaryid" = "pDiaryID";
select '0:~Success';
return
end case;
select '1:~Error' -- if here then no action has been specified
end /* TEST
call NetTestSetup('');
delete from diary where staffid='test';
select * from NetOwnerDiarySet(pWebUserID='test.owner', pAction='N', pStartDate='25/12/2001', pStartTime='09:00', pDescription='TEST991');
expect pResult=0:*;
select * from diary where staffid='TEST' and Description='TEST991';
expect diaryfrom=25/12/2001 09:00*, durationtype=R;
select * from NetOwnerDiarySet(pWebUserID='test.owner', pDiaryID='TEST992', pAction='N', pStartDate='24/12/2001', pEndDate='25/12/2001', pDescription='TEST992');
expect pResult=0:*;
select * from diary where DiaryID='TEST992';
expect diaryfrom=24/12/2001*, durationtype=D;
call NetOwnerDiarySet(pWebUserID='test.owner', pDiaryID='TEST992', pAction='U', pNotes='TestNotes', pStartDate='23/12/2001', pStartTime='09:15');
select * from diary where DiaryID='TEST992';
expect notes=TestNotes,diaryfrom=23/12/2001 09:15*,diaryto=25/12/2001;
call NetOwnerDiarySet(pWebUserID='test.owner', pDiaryID='TEST992', pAction='U', pNotes='TestNotes', pStartDate='23/12/2001', pStartTime='09:00',pMinutesBefore='30');
select notes,replace(diaryfrom,' ','!') as DF,diaryto,popupminutes,replace(popuptime,' ','!') as PT from diary where DiaryID='TEST992';
expect notes=TestNotes,DF=2001-12-23!09:00:00.000000,diaryto=25/12/2001,popupminutes=30,PT=2001-12-23!08:30:00.000000;
call NetOwnerDiarySet(pWebUserID='test.owner', pDiaryID='TEST992', pAction='D');
select notes,replace(diaryfrom,' ','!') as DF,diaryto,popupminutes,replace(popuptime,' ','!') as PT from diary where DiaryID='TEST992';
expect EOF;
select * from NetOwnerDiarySet(pWebUserID='test.owner', pAction='Z', pStartDate='25/12/2001', pStartTime='09:00', pDescription='TEST991');
expect pResult=3:*;
select * from NetOwnerDiarySet(pWebUserID='test.owner', pAction='N', pStartDate=null, pStartTime='09:00', pDescription='TEST991');
expect pResult=4:*;
select * from NetOwnerDiarySet(pWebUserID='test.owner', pAction='N', pStartDate='25/12/2001', pStartTime='09:00');
expect pResult=5:*;
select * from NetOwnerDiarySet(pWebUserID='test.owner', pAction='N', pStartDate='25/12/2001', pStartTime='09:00',pEndDate='24/12/2001',pEndTime='08:00');
expect pResult=8:*;
select * from NetOwnerDiarySet(pWebUserID='test.owner', pAction='D');
expect pResult=2:*;
delete from diary where staffid='test';
*/
/* DOC
2016-07-26 PC Provide new/update/delete facility for NetOwnerDiary
2016-09-07 GJ When editing a REMINDER set the end datetime to the same as the start date time
2016-09-27 PC modified to accept dates with slash
2016-09-30 PC MS Modify logic to rationalise validation
2016-11-10 PC Modify date validation to cover validation removed from web page
2017-10-30 GJ There is now a 21 day limit on the MinutesBefore popup to match what IQX.exe does
2018-05-14 ET Removed the bit setting 'who entered' to staff ID as this was causing it to be set to the wrong staff ID, not the staff ID of the person who actually entered it.
Incoming:
pAction - N for New, U for Update, D for Delete
pDiaryID - PK of line to update or delete - required for update and delete but not new
pStaffID - staff member to make appointment with - defaults to logged in user
pPersonID - candidate to make appointment with - optional
pEmploymentID - contact to make appointment with - optional
pStartDate and pStartTime - start date and time for appointment or reminder - date required time optional
pEndDate and pEndTime - end date and time for appointment - if not supplied it is a reminder
pDescription - title of diary entry - required
pNotes - notes for diary entry - optional
pPriority - priority level for diary entry - optional
Outgoing:
pResult - success or error message
Tests:
create a reminder
create an appointment
change the appointment notes
delete the appointment
check for error reports on invalid action, corrupt date, missing description and missing pDiaryID
*/
go
COMMENT TO PRESERVE FORMAT ON PROCEDURE "pears"."NetOwnerDiarySet" IS
{create PROCEDURE pears."NetOwnerDiarySet"(in pWebUserID char(20),in pDiaryID char(20) default null,in pAction char(1),in pEventType char(1) default null,in pStaffID char(20) default null,in pPersonID char(20) default null,in pEmploymentID char(20) default null,in pStartDate char(20) default null,in pEndDate char(20) default null,pStartTime char(20) default null,pEndTime char(20) default null,in pDescription char(100) default null,in pNotes long varchar default null,in pPriority char(1) default null,pMinutesBefore integer default null)
result( "pResult" char(250) )
BEGIN
// IQXWeb
declare dEndDateTime datetime;
declare dStartDateTime datetime;
if not pAction in ('N','U','D') then
select '3:~Invalid pAction';
return;
end if;
if not pEventType in ('A','R') and pAction='U' then
select '6:~Invalid Event Type';
return;
end if;
if isnull(pDiaryID,'')='' and pAction in ('U','D') then -- must have a DiaryID for update or delete
select '2:~pDiaryID required';
return;
end if;
if pAction !='D' then
if isnull(pStartDate,'') <>'' then
set dStartDateTime=iqxnetstringtodate(pstartdate)+iqxnetstringtotime(isnull(pstarttime,'00:00'));
if dStartDateTime is null then
select '107:~Invalid start date or time';
return;
end if;
end if;
if isnull(pEndDate,'') <>'' then
set dEndDateTime=iqxnetstringtodate(penddate)+iqxnetstringtotime(isnull(pendtime,'00:00'));
if dEndDateTime is null then
select '107:~Invalid end date or time';
return;
end if;
end if;
if dEndDateTime < dStartDateTime then
select '108:~Finish time appears to be before Start time';
return;
end if;
if pAction='N' and dEndDateTime is null then
set dEndDateTime=dStartDateTime
end if;
end if;
case pAction
when 'N' then
if dStartDateTime is null then
select '104:~pStartDate required';
return;
end if;
if trim(isnull(pDescription,''))='' then
select '105:~pDescription required';
return;
end if;
if pMinutesBefore > 30240 then
select '106:~Minutes Before cannot exceed 30240 minutes (or 21 days)';
return;
end if;
if isnull(pStaffID,'')='' then
set pStaffID=(select staffid from iqxnetuser where iqxnetuserid=pWebUserID);
end if;
insert into diary (diaryid,staffid,personid,employmentid,diaryfrom,diaryto,description,notes,priority,PopupMinutes)
values (isnull(pDiaryID,uniquekey('x')),pStaffID,pPersonID,pEmploymentID,dStartDateTime,dEndDateTime,pDescription,pNotes,pPriority,pMinutesBefore);
select '0:~Success';
return;
when 'U' then -- Update record
if pEventType='R' then
set dEndDateTime=dStartDateTime
end if;
update diary set staffid=isnull(pStaffID,staffid), personid=isnull(pPersonID,personid), employmentid=isnull(pEmploymentID,employmentid), diaryfrom=isnull(dStartDateTime,diaryfrom), diaryto=isnull(dEndDateTime,diaryto), description=isnull(pDescription,description), notes=isnull(pNotes,notes),priority=isnull(pPriority,priority), durationtype=null,PopupMinutes=pMinutesBefore,PopupTime=dateadd(minute,0-pMinutesBefore,DiaryFrom) where DiaryID=pDiaryID;
-- Note setting durationtype=null to prevent the trigger mucking up the diaryto based on previous stored duration
select '0:~Success';
return
when 'D' then -- delete record
delete from diary where diaryid=pDiaryID;
select '0:~Success';
return
end case;
select '1:~Error'; -- if here then no action has been specified
END
/* TEST
call NetTestSetup('');
delete from diary where staffid='test';
select * from NetOwnerDiarySet(pWebUserID='test.owner', pAction='N', pStartDate='25/12/2001', pStartTime='09:00', pDescription='TEST991');
expect pResult=0:*;
select * from diary where staffid='TEST' and Description='TEST991';
expect diaryfrom=25/12/2001 09:00*, durationtype=R;
select * from NetOwnerDiarySet(pWebUserID='test.owner', pDiaryID='TEST992', pAction='N', pStartDate='24/12/2001', pEndDate='25/12/2001', pDescription='TEST992');
expect pResult=0:*;
select * from diary where DiaryID='TEST992';
expect diaryfrom=24/12/2001*, durationtype=D;
call NetOwnerDiarySet(pWebUserID='test.owner', pDiaryID='TEST992', pAction='U', pNotes='TestNotes', pStartDate='23/12/2001', pStartTime='09:15');
select * from diary where DiaryID='TEST992';
expect notes=TestNotes,diaryfrom=23/12/2001 09:15*,diaryto=25/12/2001;
call NetOwnerDiarySet(pWebUserID='test.owner', pDiaryID='TEST992', pAction='U', pNotes='TestNotes', pStartDate='23/12/2001', pStartTime='09:00',pMinutesBefore='30');
select notes,replace(diaryfrom,' ','!') as DF,diaryto,popupminutes,replace(popuptime,' ','!') as PT from diary where DiaryID='TEST992';
expect notes=TestNotes,DF=2001-12-23!09:00:00.000000,diaryto=25/12/2001,popupminutes=30,PT=2001-12-23!08:30:00.000000;
call NetOwnerDiarySet(pWebUserID='test.owner', pDiaryID='TEST992', pAction='D');
select notes,replace(diaryfrom,' ','!') as DF,diaryto,popupminutes,replace(popuptime,' ','!') as PT from diary where DiaryID='TEST992';
expect EOF;
select * from NetOwnerDiarySet(pWebUserID='test.owner', pAction='Z', pStartDate='25/12/2001', pStartTime='09:00', pDescription='TEST991');
expect pResult=3:*;
select * from NetOwnerDiarySet(pWebUserID='test.owner', pAction='N', pStartDate=null, pStartTime='09:00', pDescription='TEST991');
expect pResult=4:*;
select * from NetOwnerDiarySet(pWebUserID='test.owner', pAction='N', pStartDate='25/12/2001', pStartTime='09:00');
expect pResult=5:*;
select * from NetOwnerDiarySet(pWebUserID='test.owner', pAction='N', pStartDate='25/12/2001', pStartTime='09:00',pEndDate='24/12/2001',pEndTime='08:00');
expect pResult=8:*;
select * from NetOwnerDiarySet(pWebUserID='test.owner', pAction='D');
expect pResult=2:*;
delete from diary where staffid='test';
*/
/* DOC
2016-07-26 PC Provide new/update/delete facility for NetOwnerDiary
2016-09-07 GJ When editing a REMINDER set the end datetime to the same as the start date time
2016-09-27 PC modified to accept dates with slash
2016-09-30 PC MS Modify logic to rationalise validation
2016-11-10 PC Modify date validation to cover validation removed from web page
2017-10-30 GJ There is now a 21 day limit on the MinutesBefore popup to match what IQX.exe does
2018-05-14 ET Removed the bit setting 'who entered' to staff ID as this was causing it to be set to the wrong staff ID, not the staff ID of the person who actually entered it.
Incoming:
pAction - N for New, U for Update, D for Delete
pDiaryID - PK of line to update or delete - required for update and delete but not new
pStaffID - staff member to make appointment with - defaults to logged in user
pPersonID - candidate to make appointment with - optional
pEmploymentID - contact to make appointment with - optional
pStartDate and pStartTime - start date and time for appointment or reminder - date required time optional
pEndDate and pEndTime - end date and time for appointment - if not supplied it is a reminder
pDescription - title of diary entry - required
pNotes - notes for diary entry - optional
pPriority - priority level for diary entry - optional
Outgoing:
pResult - success or error message
Tests:
create a reminder
create an appointment
change the appointment notes
delete the appointment
check for error reports on invalid action, corrupt date, missing description and missing pDiaryID
*/
}