Index: Scheduling/trunk/m/BSDX07.m
===================================================================
--- Scheduling/trunk/m/BSDX07.m	(revision 1472)
+++ Scheduling/trunk/m/BSDX07.m	(revision 1479)
@@ -1,3 +1,3 @@
-BSDX07	; VW/UJO/SMH - WINDOWS SCHEDULING RPCS  ; 7/5/12 12:57pm
+BSDX07	; VW/UJO/SMH - WINDOWS SCHEDULING RPCS  ; 7/9/12 4:02pm
 	;;1.7T1;BSDX;;Jul 06, 2012;Build 18
 	; Licensed under LGPL
@@ -32,5 +32,5 @@
 	Q
 	;
-APPADD(BSDXY,BSDXSTART,BSDXEND,BSDXPATID,BSDXRES,BSDXLEN,BSDXNOTE,BSDXATID,BSDXRADEXAM)	;EP
+APPADD(BSDXY,BSDXSTART,BSDXEND,BSDXPATID,BSDXRES,BSDXLEN,BSDXNOTE,BSDXATID,BSDXRADEXAM)	;Private EP
 	;
 	;Called by RPC: BSDX ADD NEW APPOINTMENT
@@ -58,5 +58,5 @@
 	; AppointmentID and ErrorNumber
 	;
-	; NB: Specifying BSDXLEN and BSDXEND is redundant. For future programmers
+	; TODO: Specifying BSDXLEN and BSDXEND is redundant. For future programmers
 	; to sort out. Needs changes on client.
 	;
@@ -66,9 +66,9 @@
 	; Deal with optional arguments
 	S BSDXRADEXAM=$G(BSDXRADEXAM)
-	   ;
+	;
 	; Return Array; set Return and clear array
 	S BSDXY=$NA(^BSDXTMP($J))
 	K ^BSDXTMP($J)
-	   ;
+	;
 	; $ET
 	N $ET S $ET="G ETRAP^BSDX07"
@@ -80,5 +80,5 @@
 	; It's not expected that the error will ever happen as no filing
 	; is supposed to take 5 seconds.
-	L +^BSDXAPPT(BSDXPATID):5 I '$T D ERR(BSDXI,"-1~Patient record is locked. Please contact technical support.") Q
+	L +^BSDXPAT(BSDXPATID):5 I '$T D ERR(BSDXI,"-1~Patient record is locked. Please contact technical support.") Q
 	;
 	; Header Node
@@ -92,6 +92,6 @@
 	N BSDXERR S BSDXERR=0
 	;
-	;;;test for error inside transaction. See if %ZTER works
-	I $G(BSDXDIE) S X=1/0
+	;;;test for error. See if %ZTER works
+	I $G(BSDXDIE) N X S X=1/0
 	;;;test
 	;
@@ -132,8 +132,8 @@
 	; Now, check if PIMS has any issues with us making the appt using MAKECK
 	N BSDXSCD S BSDXSCD=$P(BSDXRNOD,U,4)  ; Hosp Location IEN
-	N BSDXERR ; Variable to hold value of $$MAKE and $$MAKECK
+	N BSDXERR S BSDXERR=0 ; Variable to hold value of $$MAKE and $$MAKECK
 	N BSDXC ; Array to send to MAKE and MAKECK APIs
 	; Only if we have a valid Hosp Location
-	I +BSDXSCD,$D(^SC(BSDXSCD,0)) D  I +BSDXERR D ERR(BSDXI,"-10~BSDX07 Error: MAKECK^BSDXAPI returned error code: "_BSDXERR) Q  ; no need for roll back
+	I +BSDXSCD,$D(^SC(BSDXSCD,0)) D 
 	. S BSDXC("PAT")=BSDXPATID
 	. S BSDXC("CLN")=BSDXSCD
@@ -147,4 +147,5 @@
 	. S BSDXC("USR")=DUZ
 	. S BSDXERR=$$MAKECK^BSDXAPI(.BSDXC)
+	I BSDXERR D ERR(BSDXI,"-10~BSDX07 Error: MAKECK^BSDXAPI returned error code: "_BSDXERR) Q  ; no need for roll back
 	;
 	; Done with all checks, let's make appointment in BSDX APPOINTMENT
@@ -160,10 +161,14 @@
 	; Only if we have a valid Hosp Loc can we make an appointment in 2/44
 	; Use BSDXC array from before.
-	; NB: $$MAKE itself calls $$MAKECK to check again for being okay.
-	I +BSDXSCD,$D(^SC(BSDXSCD,0)) D  I +BSDXERR D ERR(BSDXI,"-10~BSDX07 Error: MAKE^BSDXAPI returned error code: "_BSDXERR),ROLLBACK(BSDXAPPTID,.BSDXC) Q
-	. S BSDXERR=$$MAKE^BSDXAPI(.BSDXC)
+	; FYI: $$MAKE itself calls $$MAKECK to check again for being okay.
+	; If an error happens here, we roll back both ^BSDXAPPT and 2/44 by deleting
+	N BSDXERR S BSDXERR=0 ; Variable to hold value of $$MAKE and $$MAKECK
+	I +BSDXSCD,$D(^SC(BSDXSCD,0)) S BSDXERR=$$MAKE^BSDXAPI(.BSDXC)
+	I BSDXERR D ERR(BSDXI,"-10~BSDX07 Error: MAKE^BSDXAPI returned error code: "_BSDXERR),ROLLBACK(BSDXAPPTID,.BSDXC) Q
+	;
+	; Unlock
+	L -^BSDXPAT(BSDXPATID)
 	;
 	;Return Recordset
-	L -^BSDXAPPT(BSDXPATID)
 	S BSDXI=BSDXI+1
 	S ^BSDXTMP($J,BSDXI)=BSDXAPPTID_"^"_$C(30)
@@ -179,5 +184,5 @@
 	;Returns ien in BSDXAPPT or 0 if failed
 	;Create entry in BSDX APPOINTMENT
-	N BSDXAPPTID
+	N BSDXAPPTID,BSDXFDA
 	S BSDXFDA(9002018.4,"+1,",.01)=BSDXSTART
 	S BSDXFDA(9002018.4,"+1,",.02)=BSDXEND
@@ -208,5 +213,5 @@
 	;BSDXSCDA=IEN for ^SC(BSDXSC,"S",BSDXSTART,1,BSDXSCDA). Use to get Length & Note
 	;
-	N BSDXNOD,BSDXLEN,BSDXAPPTID,BSDXNODP,BSDXWKIN,BSDXRES
+	N BSDXNOD,BSDXLEN,BSDXAPPTID,BSDXNODP,BSDXWKIN,BSDXRES,BSDXNOTE,BSDXEND
 	Q:+$G(BSDXNOEV)
 	I $D(^BSDXRES("ALOC",BSDXSC)) S BSDXRES=$O(^BSDXRES("ALOC",BSDXSC,0))
@@ -243,6 +248,5 @@
 	; Appointment ID to remove from ^BSDXAPPT
 	; BSDXC array (see array format in $$MAKE^BSDXAPI)
-	; NB: I am not sure whether I want to do $G to protect against undefs?
-	; I send the variables to this EP from the Symbol Table in ETRAP
+	N %
 	D BSDXDEL^BSDX07(BSDXAPPTID)
 	S:$D(BSDXC) %=$$UNMAKE^BSDXAPI(.BSDXC) ; rtn value always 0
@@ -257,4 +261,8 @@
 	;
 ERR(BSDXI,BSDXERR)	 ;Error processing - different from error trap.
+	; Unlock first
+	L -^BSDXPAT(BSDXPATID)
+	; If last line is $C(31), we are done. No more errors to send to client.
+	I ^BSDXTMP($J,$O(^BSDXTMP($J," "),-1))=$C(31) QUIT
 	S BSDXI=BSDXI+1
 	S BSDXERR=$TR(BSDXERR,"^","~")
@@ -262,5 +270,4 @@
 	S BSDXI=BSDXI+1
 	S ^BSDXTMP($J,BSDXI)=$C(31)
-	L -^BSDXAPPT(BSDXPATID)
 	Q
 	;
@@ -268,6 +275,7 @@
 	N $ET S $ET="D ^%ZTER HALT"  ; Emergency Error Trap
 	D ^%ZTER
-	S $EC=""  ; Clear Error
+	;
 	I +$G(BSDXAPPTID) D ROLLBACK(BSDXAPPTID,.BSDXC) ; Rollback if BSDXAPPTID exists
+	;
 	; Log error message and send to client
 	I '$D(BSDXI) N BSDXI S BSDXI=0
Index: Scheduling/trunk/m/BSDX08.m
===================================================================
--- Scheduling/trunk/m/BSDX08.m	(revision 1472)
+++ Scheduling/trunk/m/BSDX08.m	(revision 1479)
@@ -1,3 +1,3 @@
-BSDX08	; VW/UJO/SMH - WINDOWS SCHEDULING RPCS ; 7/5/12 12:39pm
+BSDX08	; VW/UJO/SMH - WINDOWS SCHEDULING RPCS ; 7/9/12 4:22pm
 	;;1.7T1;BSDX;;Jul 06, 2012;Build 18
 	; 
@@ -37,5 +37,5 @@
 	Q
 	;
-APPDEL(BSDXY,BSDXAPTID,BSDXTYP,BSDXCR,BSDXNOT)	       ;EP
+APPDEL(BSDXY,BSDXAPTID,BSDXTYP,BSDXCR,BSDXNOT)	       ; Private EP
 	;Called by RPC: BSDX CANCEL APPOINTMENT
 	;Cancels existing appointment in BSDX APPOINTMENT and 44/2 subfiles
@@ -65,20 +65,20 @@
 	S ^BSDXTMP($J,BSDXI)="T00100ERRORID"_$C(30)
 	;
+	; Turn off SDAM APPT PROTOCOL BSDX Entries
+	N BSDXNOEV
+	S BSDXNOEV=1 ;Don't execute BSDX CANCEL APPOINTMENT protocol
+	;
+	;;;test for error inside transaction. See if %ZTER works
+	I $G(BSDXDIE1) N X S X=1/0
+	;
+	; Check appointment ID and whether it exists
+	I '+BSDXAPTID D ERR(BSDXI,"-2~BSDX08: Invalid Appointment ID") Q
+	I '$D(^BSDXAPPT(BSDXAPTID,0)) D ERR(BSDXI,"-3~BSDX08: Invalid Appointment ID") Q
+	; 
 	; Lock BSDX node, only to synchronize access to the globals.
 	; It's not expected that the error will ever happen as no filing
 	; is supposed to take 5 seconds.
-	L +^BSDXAPPT(BSDXAPTID):5 I '$T D ERR(BSDXI,"-1~BSDX08: Appt record is locked. Please contact technical support.") Q
-	;
-	; Turn off SDAM APPT PROTOCOL BSDX Entries
-	N BSDXNOEV
-	S BSDXNOEV=1 ;Don't execute BSDX CANCEL APPOINTMENT protocol
-	;
-	;;;test for error inside transaction. See if %ZTER works
-	I $G(BSDXDIE1) N X S X=1/0
-	;
-	; Check appointment ID and whether it exists
-	I '+BSDXAPTID D ERR(BSDXI,"-2~BSDX08: Invalid Appointment ID") Q
-	I '$D(^BSDXAPPT(BSDXAPTID,0)) D ERR(BSDXI,"-3~BSDX08: Invalid Appointment ID") Q
-	; 
+	L +^BSDXAPPT(BSDXAPTID):5 E  D ERR(BSDXI,"-1~BSDX08: Appt record is locked. Please contact technical support.") Q
+	;
 	; Start Processing:
 	; First, get data
@@ -124,13 +124,12 @@
 	; BSDXAPPT First; no need for rollback if error occured.
 	N BSDXERR S BSDXERR=$$BSDXCAN(BSDXAPTID)  ; Add a cancellation date in BSDX APPOINTMENT
-	I BSDXERR D ERR(BSDXI,"$$BSDXCAN failed (Fileman filing error): "_$P(BSDXERR,U,2)) QUIT
+	I BSDXERR D ERR(BSDXI,"-10~BSDX08: $$BSDXCAN failed (Fileman filing error): "_$P(BSDXERR,U,2)) QUIT
 	;
 	; Then PIMS: 
 	; cancel appt in "S" nodes in file 2 and 44, then update Legacy PIMS Availability
 	; If error happens, must rollback ^BSDXAPPT
-	I BSDXLOC D  QUIT:BSDXERR
-	. S BSDXERR=$$CANCEL^BSDXAPI(.BSDXC) ; Cancel through BSDXAPI
-	. ; Rollback BSDXAPPT if error occurs
-	. I BSDXERR D ERR(BSDXI,"-9^BSDX08: BSDXAPI returned an error: "_$P(BSDXERR,U,2)),ROLLBACK(BSDXAPTID)  QUIT
+	I BSDXLOC S BSDXERR=$$CANCEL^BSDXAPI(.BSDXC) ; Cancel through BSDXAPI
+	; Rollback BSDXAPPT if error occurs
+	I BSDXERR D ERR(BSDXI,"-9^BSDX08: BSDXAPI returned an error: "_$P(BSDXERR,U,2)),ROLLBACK(BSDXAPTID)  QUIT
 	;
 	L -^BSDXAPPT(BSDXAPTID)
@@ -186,5 +185,5 @@
 	. S BSDXNOD=$G(^BSDXAPPT(BSDXAPPT,0)) Q:BSDXNOD=""
 	. I $P(BSDXNOD,U,5)=BSDXPAT,$P(BSDXNOD,U,12)="" S BSDXFOUND=1 Q
-	I BSDXFOUND,+$G(BSDXAPPT) D BSDXCAN(BSDXAPPT)
+	I BSDXFOUND,+$G(BSDXAPPT) N % S %=$$BSDXCAN(BSDXAPPT) I % D ^%ZTER
 	Q BSDXFOUND
 	;
@@ -201,4 +200,6 @@
 	;
 ERR(BSDXI,BSDXERR)	;Error processing
+	; Unlock first
+	L:$D(BSDXAPTID) -^BSDXAPPT(BSDXAPTID)
 	; If last line is $C(31), we are done. No more errors to send to client.
 	I ^BSDXTMP($J,$O(^BSDXTMP($J," "),-1))=$C(31) QUIT
@@ -208,5 +209,4 @@
 	S BSDXI=BSDXI+1
 	S ^BSDXTMP($J,BSDXI)=$C(31)
-	L -^BSDXAPPT(BSDXAPTID)
 	QUIT
 	;
@@ -214,8 +214,10 @@
 	N $ET S $ET="D ^%ZTER HALT"  ; Emergency Error Trap
 	D ^%ZTER
+	;
 	; Roll back BSDXAPPT; 
 	; NB: What if a Mumps error happens inside fileman in BSDXAPI? 
 	; I have decided the M errors are out of scope for me to handle.
 	D:$G(BSDXAPTID) ROLLBACK(BSDXAPTID)
+	;
 	; Log error message and send to client
 	I '$D(BSDXI) N BSDXI S BSDXI=0
Index: Scheduling/trunk/m/BSDX25.m
===================================================================
--- Scheduling/trunk/m/BSDX25.m	(revision 1472)
+++ Scheduling/trunk/m/BSDX25.m	(revision 1479)
@@ -1,3 +1,3 @@
-BSDX25	; VEN/SMH - WINDOWS SCHEDULING RPCS ; 7/5/12 11:55am
+BSDX25	; VEN/SMH - WINDOWS SCHEDULING RPCS ; 7/9/12 5:00pm
 	;;1.7T1;BSDX;;Jul 06, 2012;Build 18
 	; Licensed under LGPL
@@ -41,4 +41,5 @@
 	; -2 -> Invalid Check-in Date
 	; -3 -> Cannot check-in due to Fileman Filer failure
+	; -4 -> Cannot lock ^BSDXAPPT(APPTID)
 	; -10 -> BSDXAPI error
 	; -100 -> Mumps Error
@@ -66,4 +67,9 @@
 	I '+BSDXAPPTID D ERR("-1~Invalid Appointment ID") QUIT
 	I '$D(^BSDXAPPT(BSDXAPPTID,0)) D ERR("-1~Invalid Appointment ID") QUIT
+	;
+	; Lock BSDX node, only to synchronize access to the globals.
+	; It's not expected that the error will ever happen as no filing
+	; is supposed to take 5 seconds.
+	L +^BSDXAPPT(BSDXAPPTID):5 E  D ERR("-4~Appt record is locked. Please contact technical support.") QUIT
 	;
 	; Remove Date formatting v.1.5. Client will send date as FM Date.
@@ -101,4 +107,5 @@
 	. D ERR(-10_"~"_$P(BSDXERR,U,2)) ; Send error message to client
 	;
+	L -^BSDXAPPT(BSDXAPPTID)
 	S BSDXI=BSDXI+1
 	S ^BSDXTMP($J,BSDXI)="0"_$C(30)
@@ -124,5 +131,5 @@
 	Q 0
 	;
-RMCI(BSDXY,BSDXAPPTID)	; EP - Remove Check-in from BSDX APPT and 2/44
+RMCI(BSDXY,BSDXAPPTID)	; Private EP - Remove Check-in from BSDX APPT and 2/44
 	; Called by RPC BSDX REMOVE CHECK-IN
 	; 
@@ -163,4 +170,8 @@
 	I '$D(^BSDXAPPT(BSDXAPPTID,0)) D ERR("-2~Invalid Appointment ID") QUIT
 	;
+	; Lock
+	; Timeout not expected to happen except in error conditions.
+	L +^BSDXAPPT(BSDXAPPTID):5 E  D ERR("-7~Appt record is locked. Please contact technical support.") QUIT
+	;
 	; Get appointment Data
 	N BSDXNOD S BSDXNOD=^BSDXAPPT(BSDXAPPTID,0)
@@ -199,5 +210,8 @@
 	. N % S %=$$BSDXCHK(BSDXAPPTID,BSDXCDT) ; No error checking here.
 	. D ERR("-5~"_$P(BSDXERR,U,2)) ; Send error message to client
-	; 
+	;
+	; Unlock
+	L -^BSDXAPPT(BSDXAPPTID)
+	;
 	; Return ADO recordset
 	S BSDXI=BSDXI+1
@@ -264,4 +278,6 @@
 	;
 ERR(BSDXERR)	;Error processing
+	; Unlock first
+	L:$D(BSDXAPPTID) -^BSDXAPPT(BSDXAPPTID)
 	; If last line is $C(31), we are done. No more errors to send to client.
 	I ^BSDXTMP($J,$O(^BSDXTMP($J," "),-1))=$C(31) QUIT
Index: Scheduling/trunk/m/BSDX26.m
===================================================================
--- Scheduling/trunk/m/BSDX26.m	(revision 1472)
+++ Scheduling/trunk/m/BSDX26.m	(revision 1479)
@@ -1,3 +1,3 @@
-BSDX26	 ; IHS/OIT/HMW - WINDOWS SCHEDULING RPCS ; 6/25/12 4:29pm
+BSDX26	 ; IHS/OIT/HMW - WINDOWS SCHEDULING RPCS ; 7/9/12 2:19pm
 	;;1.7T1;BSDX;;Jul 06, 2012;Build 18
 	; Licensed under LGPL
@@ -8,8 +8,13 @@
 	;
 	; Error Reference:
-	; -1: Appt ID is not a number
-	; -2: Appt IEN is not in ^BSDXAPPT
-	; -3: FM Failure to file WP field in ^BSDXAPPT
-	; -4: BSDXAPI reports failure to change note field in ^SC
+	; 1: Appt ID is not a number
+	; 2: Appt IEN is not in ^BSDXAPPT
+	; 3: FM Failure to file WP field in ^BSDXAPPT
+	; 4: BSDXAPI reports failure to change note field in ^SC
+	; 5: Failure to acquire lock on ^BSDXAPPT(APPTID)
+	; 100: Mumps Error
+	; 
+	; NB: Normally I use negative numbers for errors; this routine returns
+	;     -1 as a successful result! So I needed to use +ve numbers.
 	;
 EDITAPTD(BSDXY,BSDXAPTID,BSDXNOTE)	 ;EP
@@ -48,6 +53,11 @@
 	;
 	; Validate Appointment ID
-	I '+BSDXAPTID D ERR(BSDXI,"-1~BSDX26: Invalid Appointment ID") QUIT
-	I '$D(^BSDXAPPT(BSDXAPTID,0)) D ERR(BSDXI,"-2~BSDX26: Invalid Appointment ID") QUIT
+	I '+BSDXAPTID D ERR(BSDXI,"1~BSDX26: Invalid Appointment ID") QUIT
+	I '$D(^BSDXAPPT(BSDXAPTID,0)) D ERR(BSDXI,"2~BSDX26: Invalid Appointment ID") QUIT
+	;
+	; Lock BSDX node, only to synchronize access to the globals.
+	; It's not expected that the error will ever happen as no filing
+	; is supposed to take 5 seconds.
+	L +^BSDXAPPT(BSDXAPTID):5 E  D ERR(BSDXI,"5~BSDX08: Appt record is locked. Please contact technical support.") QUIT
 	;
 	; Put the WP in decendant fields from the root to file as a WP field
@@ -65,5 +75,5 @@
 	;
 	; Error handling. No need for rollback since nothing else changed.
-	I $D(BSDXMSG) D ERR(BSDXI,"-3~BSDX26: Fileman failure to file data into 9002018.4") QUIT
+	I $D(BSDXMSG) D ERR(BSDXI,"3~BSDX26: Fileman failure to file data into 9002018.4") QUIT
 	;
 	; Now file in file 44:
@@ -73,10 +83,11 @@
 	N BSDXRES S BSDXRES=0 ; Result
 	; Update Note only if we have a linked hospital location.
-	I HLIEN S BSDXRES=$$UPDATENT^BSDXAPI(PTIEN,HLIEN,DATE,BSDXNOTE(.5))
+	I HLIEN S BSDXRES=$$UPDATENT^BSDXAPI1(PTIEN,HLIEN,DATE,BSDXNOTE(.5))
 	; If we get an error (denoted by -1 in BSDXRES), return error to client
 	; AND restore the original note
-	I BSDXRES<0 D ERR(BSDXI,"-4~BSDX26: BSDXAPI reports an error: "_BSDXRES),ROLLBACK(BSDXAPTID) QUIT
+	I BSDXRES<0 D ERR(BSDXI,"4~BSDX26: BSDXAPI reports an error: "_BSDXRES),ROLLBACK(BSDXAPTID) QUIT
 	;
 	;Return Recordset indicating success
+	L -^BSDXAPPT(BSDXAPTID)
 	S BSDXI=BSDXI+1
 	S ^BSDXTMP($J,BSDXI)="-1"_$C(30)
@@ -93,4 +104,8 @@
 	;
 ERR(BSDXI,BSDXERR)	 ;Error processing
+	; Unlock first
+	L:$D(BSDXAPTID) -^BSDXAPPT(BSDXAPTID)
+	; If last line is $C(31), we are done. No more errors to send to client.
+	I ^BSDXTMP($J,$O(^BSDXTMP($J," "),-1))=$C(31) QUIT
 	S BSDXI=BSDXI+1
 	S BSDXERR=$TR(BSDXERR,"^","~")
@@ -103,6 +118,6 @@
 	N $ET S $ET="D ^%ZTER HALT" ; Emergency Error Trap
 	D ^%ZTER
-	S $EC=""
+	;
 	I '$D(BSDXI) N BSDXI S BSDXI=0
-	D ERR(BSDXI,"-100~BSDX26 Error: "_$G(%ZTERZE))
+	D ERR(BSDXI,"100~BSDX26 Error: "_$G(%ZTERZE))
 	QUIT
Index: Scheduling/trunk/m/BSDX29.m
===================================================================
--- Scheduling/trunk/m/BSDX29.m	(revision 1472)
+++ Scheduling/trunk/m/BSDX29.m	(revision 1479)
@@ -1,3 +1,3 @@
-BSDX29	; IHS/OIT/HMW - WINDOWS SCHEDULING RPCS ; 6/22/12 1:46pm
+BSDX29	; IHS/OIT/HMW - WINDOWS SCHEDULING RPCS ; 7/9/12 11:50am
 	;;1.7T1;BSDX;;Jul 06, 2012;Build 18
 	; Licensed under LGPL
@@ -102,5 +102,4 @@
 	N $ET S $ET="D ^%ZTER HALT" ; Emergency Error Trap
 	D ^%ZTER
-	S $EC="" ; Clear Error
 	QUIT
 	;
@@ -153,4 +152,6 @@
 	;
 ERR(BSDXI,BSDXCNT,BSDXERR)	;Error processing
+	; If last line is $C(31), we are done. No more errors to send to client.
+	I ^BSDXTMP($J,$O(^BSDXTMP($J," "),-1))=$C(31) QUIT
 	S BSDXI=BSDXI+1
 	S BSDXERR=$TR(BSDXERR,"^","~")
Index: Scheduling/trunk/m/BSDX31.m
===================================================================
--- Scheduling/trunk/m/BSDX31.m	(revision 1472)
+++ Scheduling/trunk/m/BSDX31.m	(revision 1479)
@@ -1,3 +1,3 @@
-BSDX31	 ; IHS/OIT/HMW - WINDOWS SCHEDULING RPCS ; 6/27/12 4:57pm
+BSDX31	 ; IHS/OIT/HMW - WINDOWS SCHEDULING RPCS ; 7/9/12 12:57pm
 	;;1.7T1;BSDX;;Jul 06, 2012;Build 18
 	; Licensed under LGPL
@@ -20,4 +20,5 @@
 	; -5: Filing of No-show in ^DPT failed (BSDXAPI error)
 	; -6: Invalid Resource ID
+	; -7: Lock not acquired on ^BSDXAPPT(BSDXAPTID)
 	; -100: M Error
 	;
@@ -70,4 +71,9 @@
 	I '$D(^BSDXAPPT(BSDXAPTID,0)) D ERR(-2,"BSDX31: Invalid Appointment ID") Q
 	;
+	; Lock BSDX node, only to synchronize access to the globals.
+	; It's not expected that the error will ever happen as no filing
+	; is supposed to take 5 seconds.
+	L +^BSDXAPPT(BSDXAPTID):5 E  D ERR(-7,"BSDX31: Appt record is locked. Please contact technical support.") Q
+	;
 	; Noshow value check - Must be 1 or 0
 	S BSDXNS=+BSDXNS
@@ -113,4 +119,7 @@
 	. D ERR(-5,"BSDX31: "_$P(BSDXERR,U,2))
 	. N % S %=$$BSDXNOS(BSDXAPTID,'BSDXNS) ; no error checking for filer
+	;
+	; Unlock
+	L -^BSDXAPPT(BSDXAPTID)
 	;
 	; Return data in ADO.net table
@@ -177,4 +186,6 @@
 	;
 ERR(BSDXERID,ERRTXT)	   ;Error processing
+	; Unlock first
+	L:$D(BSDXAPTID) -^BSDXAPPT(BSDXAPTID)
 	; If last line is $C(31), we are done. No more errors to send to client.
 	I ^BSDXTMP($J,$O(^BSDXTMP($J," "),-1))=$C(31) QUIT
@@ -189,5 +200,5 @@
 	N $ET S $ET="D ^%ZTER HALT"  ; Emergency Error Trap
 	D ^%ZTER
-	S $EC="" ; Clear Error
+	;
 	I $G(BSDXAPTID),$D(BSDXNS) N % S %=$$BSDXNOS(BSDXAPTID,'BSDXNS) ; Reverse No-Show status (whatever it was)
 	; Send to client
Index: Scheduling/trunk/m/BSDXAPI.m
===================================================================
--- Scheduling/trunk/m/BSDXAPI.m	(revision 1472)
+++ Scheduling/trunk/m/BSDXAPI.m	(revision 1479)
@@ -1,3 +1,3 @@
-BSDXAPI	; IHS/LJF,HMW,MAW & VEN/SMH - SCHEDULING APIs ; 7/6/12 10:24am
+BSDXAPI	; IHS/LJF,HMW,MAW & VEN/SMH - SCHEDULING APIs ; 7/9/12 4:00pm
 	;;1.7T1;BSDX;;Jul 06, 2012;Build 18
 	; Licensed under LGPL  
@@ -155,4 +155,10 @@
 	; If previous data exists, which caused an error, it's destroyed.
 	; NB: ^DIK stops for nobody
+	; TODO: If Patient Appointment previously existed as cancelled, it's removed.
+	; How can I tell if one previously existed when data is in an intermediate
+	; State? Can I restore it if the other file failed? Restoration can cause
+	; another error. If I restore the global, there will be cross-references
+	; missing (ASDCN specifically).
+	;
 	; Input: Same array as $$MAKE
 	; Output: Always 0
@@ -422,22 +428,2 @@
 	Q $S(X=3:"SCHED",X=4:"WALK-IN",1:"??")
 	;
-UPDATENT(PAT,CLINIC,DATE,NOTE)	; PEP; Update Note in ^SC for patient's appointment @ DATE
-	; PAT = DFN
-	; CLINIC = SC IEN
-	; DATE = FM Date/Time of Appointment
-	;
-	; Returns:
-	; 0 if okay
-	; -1 if failure
-	;
-	; ERROR SIMULATION
-	I $G(BSDXSIMERR1) QUIT "-1~Simulated Error"
-	;
-	N SCIEN S SCIEN=$$SCIEN(PAT,CLINIC,DATE) ; ien of appt in ^SC
-	I SCIEN<1 QUIT 0    ; Appt cancelled; cancelled appts rm'ed from file 44
-	N BSDXIENS S BSDXIENS=SCIEN_","_DATE_","_CLINIC_","
-	N BSDXFDA S BSDXFDA(44.003,BSDXIENS,3)=$E(NOTE,1,150)
-	N BSDXERR
-	D FILE^DIE("","BSDXFDA","BSDXERR")
-	I $D(BSDXERR) QUIT "-1~Can't file for Pat "_PAT_" in Clinic "_CLINIC_" at "_DATE_". Fileman reported an error: "_BSDXERR("DIERR",1,"TEXT",1)
-	QUIT 0
Index: Scheduling/trunk/m/BSDXAPI1.m
===================================================================
--- Scheduling/trunk/m/BSDXAPI1.m	(revision 1472)
+++ Scheduling/trunk/m/BSDXAPI1.m	(revision 1479)
@@ -1,3 +1,3 @@
-BSDXAPI1	; VEN/SMH - SCHEDULING APIs - Continued!!! ; 7/6/12 10:23am
+BSDXAPI1	; VEN/SMH - SCHEDULING APIs - Continued!!! ; 7/9/12 2:22pm
 	;;1.7T1;BSDX;;Jul 06, 2012;Build 18
 	; Licensed under LGPL  
@@ -49,4 +49,7 @@
 	; belong to PIMS, not to the Scheduling GUI. $$MAKE and $$CANCEL now
 	; call the EPs here.
+	; Cancel and Remove-Check-in now check to see if the patient is checked-out
+	; If the patient is checked out, then we fail to cancel/no-show.
+	; UPDATENOTE was renamed to UPDATENT and moved to BSDXAPI1.
 	;
 NOSHOW(PAT,CLINIC,DATE,NSFLAG)	; $$ PEP; No-show Patient at appt date (new in v1.7)
Index: Scheduling/trunk/m/BSDXUT1.m
===================================================================
--- Scheduling/trunk/m/BSDXUT1.m	(revision 1472)
+++ Scheduling/trunk/m/BSDXUT1.m	(revision 1479)
@@ -1,3 +1,3 @@
-BSDXUT1	; VEN/SMH - Unit Tests for Scheduling GUI - cont. ; 7/3/12 12:28pm
+BSDXUT1	; VEN/SMH - Unit Tests for Scheduling GUI - cont. ; 7/9/12 12:31pm
 	;;1.7T1;BSDX;;Jul 06, 2012;Build 18
 	;
@@ -266,15 +266,15 @@
 	N NOTE S NOTE="Nothing important"
 	D EDITAPT^BSDX26(.ZZZ,"BLAHBLAH",NOTE)
-	I +^BSDXTMP($J,1)'=-1 W "ERROR IN -1",!
+	I +^BSDXTMP($J,1)'=1 W "ERROR IN -1",!
 	;
 	; Test 3: Test Error -2
 	; -2 --> ApptID not in ^BSDXAPPT
 	D EDITAPT^BSDX26(.ZZZ,298734322,NOTE)
-	I +^BSDXTMP($J,1)'=-2 W "ERROR IN -2",!
+	I +^BSDXTMP($J,1)'=2 W "ERROR IN -2",!
 	;
 	; Test 4: M Error
 	N BSDXDIE S BSDXDIE=1
 	D EDITAPT^BSDX26(.ZZZ,188,NOTE)
-	I +^BSDXTMP($J,1)'=-100 W "ERROR IN -100",!
+	I +^BSDXTMP($J,1)'=100 W "ERROR IN -100",!
 	K BSDXDIE
 	; Test 5: Trestart -- retired in v1.7
@@ -334,5 +334,5 @@
 	N NOTE S NOTE="New Note "_%H
 	D EDITAPT^BSDX26(.ZZZ,APPID,NOTE)
-	I +^BSDXTMP($J,1)'=-4 W "Simulated error not triggered",!
+	I +^BSDXTMP($J,1)'=4 W "Simulated error not triggered",!
 	I ^BSDXAPPT(APPID,1,1,0)'=ORIGNOTE W "ERROR 3",!
 	I $P(^SC(HLIEN,"S",APPTTIME,1,1,0),U,4)'=ORIGNOTE W "ERROR 4",!
Index: Scheduling/trunk/m/BSDXUT2.m
===================================================================
--- Scheduling/trunk/m/BSDXUT2.m	(revision 1472)
+++ Scheduling/trunk/m/BSDXUT2.m	(revision 1479)
@@ -1,7 +1,7 @@
-BSDXUT2	; VEN/SMH - Unit Tests for Scheduling GUI - cont. ; 7/5/12 11:39am
+BSDXUT2	; VEN/SMH - Unit Tests for Scheduling GUI - cont. ; 7/9/12 3:18pm
 	;;1.7T1;BSDX;;Jul 06, 2012;Build 18
 	;
 EN	; Run all unit tests in this routine
-	D UT25
+	D UT25,PIMS
 	QUIT
 	;
@@ -177,15 +177,33 @@
 	IF +^BSDXTMP($J,1)'=-100 WRITE "ERROR IN Etest 7",!
 	K BSDXDIE
-	;
-	; Tests for running PIMS by itself.
+	QUIT
+	;
+PIMS ; Tests for running PIMS by itself.
+	N $ET S $ET="W ""An Error Occured. Breaking."",! BREAK"
+	N RESNAM S RESNAM="UTCLINIC"
+	N HLRESIENS ; holds output of UTCR^BSDXUT - HL IEN^Resource IEN
+	D
+	. N $ET S $ET="D ^%ZTER B"
+	. S HLRESIENS=$$UTCR^BSDXUT(RESNAM)
+	. I HLRESIENS<0 S $EC=",U1," ; not supposed to happen - hard crash if so
+	;
+	N HLIEN,RESIEN
+	S HLIEN=$P(HLRESIENS,U)
+	S RESIEN=$P(HLRESIENS,U,2)
+	;
+	;
 	N APPTTIME S APPTTIME=$$TIMEHL^BSDXUT(HLIEN) ; appt time
 	N DFN S DFN=2
+	;
+	; TEST $$MAKE1^BSDXAPI
 	N % S %=$$MAKE1^BSDXAPI(DFN,HLIEN,3,APPTTIME,15,"Sam Test Appt"_DFN)
 	I % W "Error in $$MAKE1^BSDXAPI for TIME "_APPTTIME_" for DFN "_DFN,!,%,!
 	I '$D(^BSDXAPPT("APAT",DFN,APPTTIME)) W "No BSDX Appointment Created",!
-	;TODO: Index doesn't include resource.
-	N APPTID S APPTID=$O(^(APPTTIME,""))
+	N RESID S RESID=$O(^(APPTTIME,""))
+	N APPTID S APPTID=$O(^(RESID,""))
 	I 'APPTID W "Can't get appointment",!
 	IF $P(^BSDXAPPT(APPTID,0),U,3) WRITE "ERROR IN CHECKIN 3",!
+	;
+	; TEST CHECKIN1 AND RMCI ^BSDXAPI[1]
 	N % S %=$$CHECKIN1^BSDXAPI(DFN,HLIEN,APPTTIME) ; Checkin via PIMS
 	I % W "Error in Checking in via BSDXAPI",!
@@ -200,3 +218,48 @@
 	IF '+$G(^SC(HLIEN,"S",APPTTIME,1,1,"C")) WRITE "ERROR IN CHECKIN 14",!
 	IF '$P(^BSDXAPPT(APPTID,0),U,3) WRITE "ERROR IN CHECKIN 15",!
+	;
+	; TEST CANCEL1^BSDXAPI
+	N APPTTIME S APPTTIME=$$TIMEHL^BSDXUT(HLIEN) ; appt time
+	N DFN S DFN=2
+	N % S %=$$MAKE1^BSDXAPI(DFN,HLIEN,3,APPTTIME,15,"Sam Test Appt"_DFN)
+	I % W "Error in $$MAKE1^BSDXAPI for TIME "_APPTTIME_" for DFN "_DFN,!,%,!
+	I '$D(^BSDXAPPT("APAT",DFN,APPTTIME)) W "No BSDX Appointment Created",!
+	N RESID S RESID=$O(^(APPTTIME,""))
+	N APPTID S APPTID=$O(^(RESID,""))
+	I 'APPTID W "Can't get appointment",!
+	N % S %=$$CANCEL1^BSDXAPI(DFN,HLIEN,"PC",APPTTIME,1,"Afraid of Baby Foxes")
+	I % W "Error cancelling via $$CANCEL1^BSDXAPI",!
+	I ^BSDXAPPT(APPTID,0) ; Change $R
+	I '$P(^(0),U,12) W "No cancel date found in BSDXAPPT",!
+	; Make same appointment again!
+	; NB: Index APAT will have two identical entries, one for the cancelled
+	; appointment, and one for the new one. I won't check it for that reason.
+	N % S %=$$MAKE1^BSDXAPI(DFN,HLIEN,3,APPTTIME,15,"Sam Test Appt"_DFN)
+	I % W "Error in $$MAKE1^BSDXAPI for TIME "_APPTTIME_" for DFN "_DFN,!,%,!
+	;
+	; TEST NOSHOW^BSDXAPI1
+	N APPTTIME S APPTTIME=$$TIMEHL^BSDXUT(HLIEN) ; appt time
+	N DFN S DFN=3
+	N % S %=$$MAKE1^BSDXAPI(DFN,HLIEN,3,APPTTIME,15,"Sam Test Appt"_DFN)
+	I % W "Error in $$MAKE1^BSDXAPI for TIME "_APPTTIME_" for DFN "_DFN,!,%,!
+	I '$D(^BSDXAPPT("APAT",DFN,APPTTIME)) W "No BSDX Appointment Created",!
+	N RESID S RESID=$O(^(APPTTIME,""))
+	N APPTID S APPTID=$O(^(RESID,""))
+	I 'APPTID W "Can't get appointment",!
+	; No show via PIMS
+	N % S %=$$NOSHOW^BSDXAPI1(DFN,HLIEN,APPTTIME,1)
+	I % W "Error no-showing via $$NOSHOW^BSDXAPI1",!
+	I ^BSDXAPPT(APPTID,0) ; Change $R
+	I '$P(^(0),U,10) W "No-show not present in ^BSDXAPPT",!
+	; un-noshow via PIMS
+	N % S %=$$NOSHOW^BSDXAPI1(DFN,HLIEN,APPTTIME,0)
+	I % W "Error no-showing via $$NOSHOW^BSDXAPI1",!
+	I ^BSDXAPPT(APPTID,0) ; Change $R
+	I $P(^(0),U,10) W "No-show present in ^BSDXAPPT when it shouldn't",!
+	;
+	; NB: UPDATENT^BSDXAPI is updates the note. Right now, we don't have any
+	; way to update the note from BSDXAPI back to ^BSDXAPPT as the protocol
+	; file is currently not involved. Right now I can't even find the code 
+	; that lets you change an appointment note in PIMS.
+	;
 	QUIT
