BSDX08	; IHS/OIT/HMW - WINDOWS SCHEDULING RPCS ; 9/15/10 8:21pm
	;;1.4;BSDX;;Sep 07, 2010
	;
	;
APPDELD(BSDXY,BSDXAPTID,BSDXTYP,BSDXCR,BSDXNOT)	;EP
	;Entry point for debugging
	;
	;D DEBUG^%Serenji("APPDEL^BSDX08(.BSDXY,BSDXAPTID,BSDXTYP,BSDXCR,BSDXNOT)")
	Q
	;
APPDEL(BSDXY,BSDXAPTID,BSDXTYP,BSDXCR,BSDXNOT)	       ;EP
	;Called by BSDX CANCEL APPOINTMENT
	;Cancels appointment
	;BSDXAPTID is entry number in BSDX APPOINTMENT file
	;BSDXTYP is C for clinic-cancelled and PC for patient cancelled
	;BSDXCR is pointer to CANCELLATION REASON File (409.2)
	;BSDXNOT is user note
	;Returns error code in recordset field ERRORID
	;
	;
	N BSDXNOD,BSDXPATID,BSDXSTART,DIK,DA,BSDXID,BSDXI,BSDXZ,BSDXERR
	N BSDXLOC,BSDXLEN,BSDXSCIEN
	N BSDXNOEV
	S BSDXNOEV=1 ;Don't execute BSDX CANCEL APPOINTMENT protocol
	;
	D ^XBKVAR S X="ETRAP^BSDX08",@^%ZOSF("TRAP")
	S BSDXI=0
	K ^BSDXTMP($J)
	S BSDXY="^BSDXTMP("_$J_")"
	S ^BSDXTMP($J,BSDXI)="T00020ERRORID"_$C(30)
	S BSDXI=BSDXI+1
	TSTART
	I '+BSDXAPTID D ERR(BSDXI,"BSDX08: Invalid Appointment ID") Q
	I '$D(^BSDXAPPT(BSDXAPTID,0)) D ERR(BSDXI,"BSDX08: Invalid Appointment ID") Q
	;
	;Delete APPOINTMENT entries
	S BSDXNOD=^BSDXAPPT(BSDXAPTID,0)
	S BSDXPATID=$P(BSDXNOD,U,5)
	S BSDXSTART=$P(BSDXNOD,U)
	;
	;Lock BSDX node
	L +^BSDXAPPT(BSDXPATID):5 I '$T D ERR(BSDXI+1,"Another user is working with this patient's record.  Please try again later") TROLLBACK  Q
	;
	D BSDXCAN(BSDXAPTID)
	;
	S BSDXSC1=$P(BSDXNOD,U,7) ;RESOURCEID
	I BSDXSC1]"",$D(^BSDXRES(BSDXSC1,0)) D  I +$G(BSDXZ) S BSDXERR=BSDXERR_$P(BSDXZ,U,2) D ERR(BSDXI,BSDXERR) Q
	. S BSDXNOD=^BSDXRES(BSDXSC1,0)
	. S BSDXLOC=$P(BSDXNOD,U,4) ;HOSPITAL LOCATION
	. Q:'+BSDXLOC
	. S BSDXSCIEN=$$SCIEN^BSDXAPI(BSDXPATID,BSDXLOC,BSDXSTART) I BSDXSCIEN="" D  I 'BSDXZ Q  ;Q:BSDXZ
	. . S BSDXERR="BSDX08: Unable to find associated RPMS appointment for this patient. "
	. . S BSDXZ=1
	. . I '$D(^BSDXRES(BSDXSC1,20)) S BSDXZ=0 Q
	. . N BSDX1
	. . S BSDX1=0
	. . F  S BSDX1=$O(^BSDXRES(BSDXSC1,20,BSDX1)) Q:'+BSDX1  Q:BSDXZ=0  D
	. . . Q:'$D(^BSDXRES(BSDXSC1,20,BSDX1,0))
	. . . S BSDXLOC=$P(^BSDXRES(BSDXSC1,20,BSDX1,0),U)
	. . . S BSDXSCIEN=$$SCIEN^BSDXAPI(BSDXPATID,BSDXLOC,BSDXSTART) I +BSDXSCIEN S BSDXZ=0 Q
	. S BSDXERR="BSDX08: CANCEL^BSDXAPI Returned "
	. I BSDXLOC']"" S BSDXZ="0^Unable to find associated RPMS appointment for this patient." Q
	. I '$D(^SC(BSDXLOC,0)) S BSDXZ="0^Unable to find associated RPMS appointment for this patient." Q
	. S BSDXNOD=$G(^SC(BSDXLOC,"S",BSDXSTART,1,BSDXSCIEN,0))
	. I BSDXNOD="" S BSDXZ="0^Unable to find associated RPMS appointment for this patient." Q
	. S BSDXLEN=$P(BSDXNOD,U,2)
	. D APCAN(.BSDXZ,BSDXLOC,BSDXPATID,BSDXSTART)
	. Q:+$G(BSDXZ)
	. D AVUPDT(BSDXLOC,BSDXSTART,BSDXLEN)
	. ;L
	;
	TCOMMIT
	L -^BSDXAPPT(BSDXPATID)
	S BSDXI=BSDXI+1
	S ^BSDXTMP($J,BSDXI)=""_$C(30)
	S BSDXI=BSDXI+1
	S ^BSDXTMP($J,BSDXI)=$C(31)
	Q
	;
AVUPDT(BSDXSCD,BSDXSTART,BSDXLEN)	;Update RPMS Clinic availability
	;See SDCNP0
	S (SD,S)=BSDXSTART
	S I=BSDXSCD
	Q:'$D(^SC(I,"ST",SD\1,1))
	S SL=^SC(I,"SL"),X=$P(SL,U,3),STARTDAY=$S($L(X):X,1:8),SB=STARTDAY-1/100,X=$P(SL,U,6),HSI=$S(X:X,1:4),SI=$S(X="":4,X<3:4,X:X,1:4),STR="#@!$* XXWVUTSRQPONMLKJIHGFEDCBA0123456789jklmnopqrstuvwxyz",SDDIF=$S(HSI<3:8/HSI,1:2) K Y
	S SL=BSDXLEN
	S S=^SC(I,"ST",SD\1,1),Y=SD#1-SB*100,ST=Y#1*SI\.6+(Y\1*SI),SS=SL*HSI/60
	I Y'<1 F I=ST+ST:SDDIF S Y=$E(STR,$F(STR,$E(S,I+1))) Q:Y=""  S S=$E(S,1,I)_Y_$E(S,I+2,999),SS=SS-1 Q:SS'>0
	S ^SC(BSDXSCD,"ST",SD\1,1)=S
	Q
	;
APCAN(BSDXZ,BSDXLOC,BSDXDFN,BSDXSD)	        ;
	;Cancel appointment for patient BSDXDFN in clinic BSDXSC1
	;at time BSDXSD
	N BSDXC,%H
	S BSDXC("PAT")=BSDXPATID
	S BSDXC("CLN")=BSDXLOC
	S BSDXC("TYP")=BSDXTYP
	S BSDXC("ADT")=BSDXSD
	S %H=$H D YMD^%DTC
	S BSDXC("CDT")=X+%
	S BSDXC("NOT")=BSDXNOT
	S:'+$G(BSDXCR) BSDXCR=11 ;Other
	S BSDXC("CR")=BSDXCR
	S BSDXC("USR")=DUZ
	;
	S BSDXZ=$$CANCEL^BSDXAPI(.BSDXC)
	Q
	;
BSDXCAN(BSDXAPTID)	;
	;Cancel BSDX APPOINTMENT entry
	N %DT,X,BSDXDATE,Y,BSDXIENS,BSDXFDA,BSDXMSG
	S %DT="XT",X="NOW" D ^%DT ; X ^DD("DD")
	S BSDXDATE=Y
	S BSDXIENS=BSDXAPTID_","
	S BSDXFDA(9002018.4,BSDXIENS,.12)=BSDXDATE
	K BSDXMSG
	D FILE^DIE("","BSDXFDA","BSDXMSG")
	Q
	;
CANEVT(BSDXPAT,BSDXSTART,BSDXSC)	;EP Called by BSDX CANCEL APPOINTMENT event
	;when appointments cancelled via PIMS interface.
	;Propagates cancellation to BSDXAPPT and raises refresh event to running GUI clients
	N BSDXFOUND,BSDXRES
	Q:+$G(BSDXNOEV)
	Q:'+$G(BSDXSC)
	S BSDXFOUND=0
	I $D(^BSDXRES("ALOC",BSDXSC)) S BSDXRES=$O(^BSDXRES("ALOC",BSDXSC,0)) S BSDXFOUND=$$CANEVT1(BSDXRES,BSDXSTART,BSDXPAT)
	I BSDXFOUND D CANEVT3(BSDXRES) Q
	I $D(^BXDXRES("ASSOC",BSDXSC)) S BSDXRES=$O(^BSDXRES("ASSOC",BSDXSC,0)) S BSDXFOUND=$$CANEVT1(BSDXRES,BSDXSTART,BSDXPAT)
	I BSDXFOUND D CANEVT3(BSDXRES)
	Q
	;
CANEVT1(BSDXRES,BSDXSTART,BSDXPAT)	;
	;Get appointment id in BSDXAPT
	;If found, call BSDXCAN(BSDXAPPT) and return 1
	;else return 0
	N BSDXFOUND,BSDXAPPT
	S BSDXFOUND=0
	Q:'+BSDXRES BSDXFOUND
	Q:'$D(^BSDXAPPT("ARSRC",BSDXRES,BSDXSTART)) BSDXFOUND
	S BSDXAPPT=0 F  S BSDXAPPT=$O(^BSDXAPPT("ARSRC",BSDXRES,BSDXSTART,BSDXAPPT)) Q:'+BSDXAPPT  D  Q:BSDXFOUND
	. 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)
	Q BSDXFOUND
	;
CANEVT3(BSDXRES)	;
	;Call RaiseEvent to notify GUI clients
	;
	N BSDXRESN
	S BSDXRESN=$G(^BSDXRES(BSDXRES,0))
	Q:BSDXRESN=""
	S BSDXRESN=$P(BSDXRESN,"^")
	;D EVENT^BSDX23("SCHEDULE-"_BSDXRESN,"","","")
	D EVENT^BMXMEVN("BSDX SCHEDULE",BSDXRESN)
	Q
	;
ERR(BSDXI,BSDXERR)	;Error processing
	S BSDXI=BSDXI+1
	S BSDXERR=$TR(BSDXERR,"^","~")
	TROLLBACK
	S ^BSDXTMP($J,BSDXI)=BSDXERR_$C(30)
	S BSDXI=BSDXI+1
	S ^BSDXTMP($J,BSDXI)=$C(31)
	L
	Q
	;
ETRAP	;EP Error trap entry
	D ^%ZTER
	I '$D(BSDXI) N BSDXI S BSDXI=999999
	S BSDXI=BSDXI+1
	D ERR(BSDXI,"BSDX08 Error: "_$G(%ZTERROR))
	Q
