Home   Package List   Routine Alphabetical List   Global Alphabetical List   FileMan Files List   FileMan Sub-Files List   Package Component Lists   Package-Namespace Mapping  
Routine: SDESCCAVAIL

SDESCCAVAIL.m

Go to the documentation of this file.
SDESCCAVAIL ;ALB/KML,MGD,BWF,JDJ - VISTA SCHEDULING RPCS CANCEL CLINIC AVAILABILITY ; January 26,2024
 ;;5.3;Scheduling;**800,805,809,813,819,820,824,825,871**;Aug 13, 1993;Build 13
 ;;Per VHA Directive 6402, this routine should not be modified
 ;
 ;
 Q  ;No Direct Call
 ;
CANCLAVAIL(SDCLNJSON,SDCLNIEN,SDFULLPART,SDESBEGDTTM,SDESENDDTTM,SDCANREM,SDEAS) ;Called from RPC: SDES CANCEL CLINIC AVAILABILITY
 ; This RPC cancels Clinic availability within a given timeframe for a given clinic.
 ; Input:
 ;   SDCLNJSON   [required] - Success or Error message
 ;   SDCLNIEN    [required] - The Internal Entry Number (IEN) from the HOSPITAL LOCATION File #44
 ;   SDFULLPART  [required] - Full or partial day cancellation ('F' for full, 'P' for partial)
 ;   SDESBEGDTTM [required] - Start date/time in ISO8601 format (CCYY-MM-DDTHH:MM:SS-HH:MM)
 ;   SDESENDDTTM [required] - End Date/time in ISO8601 format (CCYY-MM-DDTHH:MM:SS-HH:MM)
 ;   SDCANREM    [optional] - Cancellation Remarks (must be 3-160 characters if passed)
 ;   SDEAS       [optional] - Enterprise Appointment Scheduling (EAS) Tracking Number associated to an appointment.
 ;
 N FMSDTTM,FMEDTTM,ERRORS,SINC,STARTOFDAY,SDCLNSREC,CANAPPTS
 S SDCLNIEN=$G(SDCLNIEN),SDFULLPART=$G(SDFULLPART),SDESBEGDTTM=$G(SDESBEGDTTM),SDESENDDTTM=$G(SDESENDDTTM),SDCANREM=$G(SDCANREM),SDEAS=$G(SDEAS)
 ; validate the dates first, since we need them to fully validate the clinic
 D VALIDATEFULLPART(.ERRORS,SDFULLPART)
 I $D(ERRORS) S ERRORS("CancelClinicAvailability",1)="" D BUILDJSON^SDESBUILDJSON(.SDCLNJSON,.ERRORS) Q
 S FMSDTTM=$$VALIDATEBEGDATE(.ERRORS,SDESBEGDTTM,SDCLNIEN)
 I $D(ERRORS) S ERRORS("CancelClinicAvailability",1)="" D BUILDJSON^SDESBUILDJSON(.SDCLNJSON,.ERRORS) Q
 S FMEDTTM=$$VALIDATEENDDATE(.ERRORS,SDESBEGDTTM,SDESENDDTTM,SDCLNIEN)
 I $D(ERRORS) S ERRORS("CancelClinicAvailability",1)="" D BUILDJSON^SDESBUILDJSON(.SDCLNJSON,.ERRORS) Q
 D VALIDATECANREM(.ERRORS,SDCANREM)
 I $D(ERRORS) S ERRORS("CancelClinicAvailability",1)="" D BUILDJSON^SDESBUILDJSON(.SDCLNJSON,.ERRORS) Q
 S SINC=""
 D VALIDATECLINIC(.ERRORS,SDCLNIEN,FMSDTTM,FMEDTTM,.SINC,.STARTOFDAY)
 D VALIDATEEAS(.ERRORS,SDEAS)
 I $D(ERRORS) D  Q
 .S ERRORS("CancelClinicAvailability",1)=""
 .D BUILDJSON^SDESBUILDJSON(.SDCLNJSON,.ERRORS)
 D CANCLNAVA(SDCLNIEN,SDFULLPART,FMSDTTM,FMEDTTM,SINC,STARTOFDAY,$G(SDCANREM),.DH,.CANAPPTS)
 S SDCLNSREC("CancelClinicAvailability",1)="Clinic availability has been successfully cancelled."
 I '$D(CANAPPTS) S SDCLNSREC("CancelClinicAvailability",1)=SDCLNSREC("CancelClinicAvailability",1)_" No appointments scheduled."
 M SDCLNSREC=CANAPPTS
 D BUILDJSON^SDESBUILDJSON(.SDCLNJSON,.SDCLNSREC)
 Q
 ;
VALIDATEFULLPART(ERRORS,FP) ;
 ; missing full or partial flag
 I $G(FP)="" D ERRLOG^SDESJSON(.ERRORS,245) Q  ; Missing full/partial day cancellation flag
 ; incorrect type
 I $G(FP)'="F"&($G(FP)'="P") D ERRLOG^SDESJSON(.ERRORS,246)  ;Invalid full/partial day cancellation flag
 Q
 ;
VALIDATEBEGDATE(ERRORS,BDATE,CLINIC) ;
 N FMSDATE
 I BDATE="" D ERRLOG^SDESJSON(.ERRORS,9) Q ""
 S FMSDATE=$$ISOTFM^SDAMUTDT(BDATE,CLINIC)
 I FMSDATE<1 D ERRLOG^SDESJSON(.ERRORS,11) Q ""
 Q FMSDATE
 ;
VALIDATEENDDATE(ERRORS,BDATE,EDATE,CLINIC) ;
 N FMSDATE,FMEDATE
 I EDATE="" D ERRLOG^SDESJSON(.ERRORS,10) Q ""
 S FMSDATE=$$ISOTFM^SDAMUTDT(BDATE,CLINIC)
 S FMEDATE=$$ISOTFM^SDAMUTDT(EDATE,CLINIC)
 I FMEDATE<1 D ERRLOG^SDESJSON(.ERRORS,12) Q ""
 ; Start date and end date must be the same
 I $P(FMEDATE,".")'=$P(FMSDATE,".") D ERRLOG^SDESJSON(.ERRORS,241) Q ""
 I FMEDATE<FMSDATE D ERRLOG^SDESJSON(.ERRORS,242) Q ""  ;"End time must be after Start Time."
 I FMEDATE=FMSDATE D ERRLOG^SDESJSON(.ERRORS,242) Q ""  ;"End time must be after Start Time."
 Q FMEDATE
 ;
VALIDATECANREM(ERRORS,REMARKS) ;
 I $L(REMARKS),$L(REMARKS)<3!($L(REMARKS)>160) D ERRLOG^SDESJSON(.ERRORS,255)
 Q
VALIDATEEAS(ERRORS,SDEAS) ;
 I $L(SDEAS) S SDEAS=$$EASVALIDATE^SDESUTIL($G(SDEAS))
 I $P($G(SDEAS),U)=-1 D ERRLOG^SDESJSON(.ERRORS,142)
 Q
VALIDATECLINIC(ERRORS,SDCLNIEN,FMSDTTM,FMEDTTM,SINC,STARTOFDAY) ;
 N FRTIME,TOTIME,DISPINCPERHR,HRCLNDISPBEG,HRCLNDISP,INACTIVATEDT,INACTIVATEDTEXT,REACTIVATEDT,REACTIVATEDTEXT,CANCELDATE,FDA,FDAIEN
 N X,MASTERPATTERN,MPATTERNSLOTS,DAYSCHED,DOW,REACTIVATEMSG,Y,FILENO
 I SDCLNIEN="" D ERRLOG^SDESJSON(.ERRORS,18) Q
 I '$D(^SC(SDCLNIEN,0)) D ERRLOG^SDESJSON(.ERRORS,19) Q
 S CANCELDATE=$P(FMSDTTM,".")
 S FRTIME=$P(FMSDTTM,".",2),TOTIME=$P(FMEDTTM,".",2)
 ; DISPINCPERHR - display increments per hour
 S DISPINCPERHR=$$GET1^DIQ(44,SDCLNIEN,1917,"I")
 ; SINC - DISPLAY INCREMENTS PER HOUR
 S SINC=$S(DISPINCPERHR="":4,DISPINCPERHR<3:4,DISPINCPERHR:DISPINCPERHR,1:4)
 ; HRCLNDISPBEG - hour clinic display begins
 S HRCLNDISPBEG=$$GET1^DIQ(44,SDCLNIEN,1914,"I")
 I HRCLNDISPBEG="" S HRCLNDISPBEG=8
 S STARTOFDAY=$S($L(HRCLNDISPBEG):HRCLNDISPBEG,1:8)
 ; Clinic inactive
 S INACTIVATEDT=$$GET1^DIQ(44,SDCLNIEN,2505,"I")
 S INACTIVATEDTEXT=$TR($$FMTE^XLFDT(INACTIVATEDT,"5DF")," ","0")
 S REACTIVATEDT=$$GET1^DIQ(44,SDCLNIEN,2506,"I")
 S REACTIVATEDTEXT=$TR($$FMTE^XLFDT(REACTIVATEDT,"5DF")," ","0")
 I REACTIVATEDT D
 .S REACTIVATEMSG=$S(REACTIVATEDT:" to "_REACTIVATEDTEXT,1:"")
 I INACTIVATEDT D
 .; if inactivate date is after than the cancel request date, quit
 .I INACTIVATEDT>$P(CANCELDATE,".") Q
 .; if there is a reactivation date and the reactivation date before the cancel date, quit
 .I REACTIVATEDT,REACTIVATEDT<$P(CANCELDATE,".") Q
 .D ERRLOG^SDESJSON(.ERRORS,52,"Clinic is inactive "_$S('REACTIVATEDT:"as of ",1:"from ")_INACTIVATEDTEXT_$G(REACTIVATEMSG))
 Q:$D(ERRORS)
 ;
 ; if the pattern does not exist for this date, create it from the template
 ; otherwise the user will be incorrectly told that the clinic does not meet on this day
 I '$D(^SC(SDCLNIEN,"ST",CANCELDATE,1)) D
 .S X=CANCELDATE
 .D DOW^SDM0 S DOW=Y
 .S MASTERPATTERN=+$O(^SC(SDCLNIEN,"T"_DOW,CANCELDATE))
 .S FILENO=$SELECT(DOW=0:44.06,DOW=1:44.07,DOW=2:44.08,DOW=3:44.09,DOW=4:44.008,DOW=5:44.009,DOW=6:44.0001,1:"")
 .S MPATTERNSLOTS=$$GET1^DIQ(FILENO,MASTERPATTERN_","_SDCLNIEN_",",1)
 ;.S FDA(44.005,"+1,"_SDCLNIEN_",",.01)=CANCELDATE
 ;.S FDA(44.005,"+1,"_SDCLNIEN_",",1)=$P("SU^MO^TU^WE^TH^FR^SA",U,DOW+1)_" "_$E(CANCELDATE,6,7)_$J("",SINC+SINC-6)_MPATTERNSLOTS
 ;.S FDAIEN(1)=CANCELDATE
 ;.D UPDATE^DIE(,"FDA","FDAIEN","FERR") K FDA,FDAIEN
 S DAYSCHED=$$GET1^DIQ(44.005,CANCELDATE_","_SDCLNIEN_",",1,"I")
 I DAYSCHED["CANCELLED" D ERRLOG^SDESJSON(.ERRORS,248) Q
 ; Clinic does not meet on that day
 I DAYSCHED'["[" D ERRLOG^SDESJSON(.ERRORS,249) Q
 ; future consideration - should we inform the user whether or not appointments were scheduled?
 ;I $O(^SC(SDCLNIEN,"S",CANCELDATE))\1-CANCELDATE D ERRLOG^SDESJSON(.ERRORS,52,"NO APPOINTMENTS SCHEDULED") Q
 ;I $O(^SC(SC,"S",SD))\1-SD W *7,!?5,"NO APPOINTMENTS SCHEDULED" S NOAP=1 G W
 ;
 ; Loss of workload credit
 I SDFULLPART="P",$$COED^SDESCCAVAIL2(SDCLNIEN,FMSDTTM,FMEDTTM,1) D ERRLOG^SDESJSON(.ERRORS,247) Q
 I SDFULLPART="F",$$COED^SDESCCAVAIL2(SDCLNIEN,$P(FMSDTTM,"."),$P(FMSDTTM,".")_.2359,1) D ERRLOG^SDESJSON(.ERRORS,247) Q
 ;
 N CANARY,OVERLAP,PCANDTTM,PCANCNT,PCANLOOP,FDAYSTRT
 D CHKPARTIALCAN(.CANARY,SDCLNIEN,CANCELDATE)
 I SDFULLPART="F" D  Q
 .S PCANCNT=0
 .S CANSTARTTIME=$$TC^SDESCCAVAIL2($P(FMSDTTM,".",2),$P(FMSDTTM,"."),STARTOFDAY,SINC)
 .S ST=$P(CANSTARTTIME,U,3)
 .S FR=$P(CANSTARTTIME,U,2)
 .I FR<0 D ERRLOG^SDESJSON(.ERRORS,52,$P(CANSTARTTIME,U,4))
 .I '$D(CANARY) Q
 .S PCANCNT=$O(ERRORS("Error",""),-1)+1
 .S ERRORS("Error",PCANCNT)=$G(CANARY("HDR"))
 .S PCANDTTM=0 F  S PCANDTTM=$O(CANARY(PCANDTTM)) Q:'PCANDTTM  D
 ..S PCANCNT=$G(PCANCNT)+1
 ..S ERRORS("Error",PCANCNT)=$G(CANARY(PCANDTTM,"MSG"))
 .S PCANCNT=$G(PCANCNT)+1
 .S ERRORS("Error",PCANCNT)="Full day cancellation cannot be used. Do you want to cancel another portion of the day?"
 S CANSTARTTIME=$$TC^SDESCCAVAIL2($P(FMSDTTM,".",2),$P(FMSDTTM,"."),STARTOFDAY,SINC)
 S ST=$P(CANSTARTTIME,U,3)
 S FR=$P(CANSTARTTIME,U,2)
 I FR<0 D ERRLOG^SDESJSON(.ERRORS,52,$P(CANSTARTTIME,U,4))
 S CANENDTIME=$$TC^SDESCCAVAIL2($P(FMEDTTM,".",2),$P(FMSDTTM,"."),STARTOFDAY,SINC)
 S TO=$P(CANENDTIME,U,2)
 I TO<0 D ERRLOG^SDESJSON(.ERRORS,52,$P(CANENDTIME,U,4))
 Q:$D(ERRORS)
 S OVERLAP=$$CHKOVERLAP^SDESCCAVAIL2(.CANARY,FR,TO)
 I OVERLAP D
 .D ERRLOG^SDESJSON(.ERRORS,250)
 .S PCANCNT=$O(ERRORS("Error",""),-1)+1
 .Q:'$D(CANARY)
 .S PCANCNT=PCANCNT+1
 .S ERRORS("Error",PCANCNT)=$G(CANARY("HDR"))
 .S PCANLOOP=0 F  S PCANLOOP=$O(CANARY(PCANLOOP)) Q:'PCANLOOP  D
 ..S PCANCNT=PCANCNT+1
 ..S ERRORS("Error",PCANCNT)=$G(CANARY(PCANLOOP,"MSG"))
 Q
CHKPARTIALCAN(CANARY,SDCLNIEN,CANDATE) ;
 N CHKDT,BEGCANDTTM,ENDCANDTTM,CANMSG,CANDTLOOP,X,MSG,APPTDT,SDCTO
 ; if the cancellation nodes exist, use them to build the current cancellations (from SDC2+3)
 I $D(^SC(SDCLNIEN,"SDCAN")),$P($O(^SC(SDCLNIEN,"SDCAN",CANDATE)),".")=CANDATE D  Q
 .S CHKDT=CANDATE
 .F  S CHKDT=$O(^SC(SDCLNIEN,"SDCAN",CHKDT)) Q:'CHKDT!($P(CHKDT,".")>CANDATE)  D
 ..S BEGCANDTTM=$$GET1^DIQ(44.05,CHKDT_","_SDCLNIEN_",",.01,"I")
 ..S ENDCANDTTM=$$GET1^DIQ(44.05,CHKDT_","_SDCLNIEN_",",1,"I")
 ..S CANMSG=$$GET1^DIQ(44.001,CHKDT_","_SDCLNIEN_",",1400,"E")
 ..S CANARY(CHKDT)=$P(BEGCANDTTM,".")_ENDCANDTTM/10000_$S($L(CANMSG):"   ("_$P(CANMSG,"(",2),1:"")
 .S CANARY("HDR")="Clinic already has the following cancellation(s) for that date: "
 .S CANDTLOOP=0
 .F  S CANDTLOOP=$O(CANARY(CANDTLOOP)) Q:'CANDTLOOP  D
 ..S X=CANDTLOOP
 ..D TMCHK(.X)
 ..S CANARY(CANDTLOOP,"MSG")="From: "_X_"   To: "
 ..S X=+(CANARY(CANDTLOOP))
 ..D TMCHK(.X)
 ..S CANARY(CANDTLOOP,"MSG")=CANARY(CANDTLOOP,"MSG")_X
 ..S CANARY(CANDTLOOP,"MSG")=CANARY(CANDTLOOP,"MSG")_$S($P(CANARY(CANDTLOOP),"(",2)]"":"  ("_$P(CANARY(CANDTLOOP),"(",2),1:"")
 Q
TMCHK(X) ;
 N %
 S X=$E($P(X,".",2)_"0000",1,4),%=X>1159 S:X>1259 X=X-1200 S X=X\100_":"_$E(X#100+100,2,3)_" "_$E("AP",%+1)_"M"
 Q
 ;
CANCLNAVA(SDCLNIEN,SDFULLPART,FMSDTTM,FMEDTTM,SI,STARTOFDAY,CANREMARKS,DH,CANAPPTS) ;
 N SDATA,SDCNHDL ; for evt dvr
 N CANDTTMBEGIN,CANENDTIME,CANENDDTTM,BLOCKLEN,CANCELDATE,NEWIEN,CANMES,CURAVAIL,CANSTARTTIME,Y,NEWAPPTDTTM,SDXX
 S CANDTTMBEGIN=FMSDTTM
 S CANSTARTTIME=$$TC^SDESCCAVAIL2($P(FMSDTTM,".",2),$P(FMSDTTM,"."),STARTOFDAY,SI)
 S ST=$P(CANSTARTTIME,U,3)
 S CANSTARTTIME=$P(CANSTARTTIME,U)
 S CANENDTIME=$$TC^SDESCCAVAIL2($P(FMEDTTM,".",2),$P(FMSDTTM,"."),STARTOFDAY,SI)
 S CANDTTMEND=$P(CANENDTIME,U,2)
 S SDXX=$P(CANENDTIME,U,3)
 S CANENDTIME=$P(CANENDTIME,U)
 S CANCELDATE=$P(FMSDTTM,".")
 ;
 I SDFULLPART="P" D PARTCAN(SDCLNIEN,CANDTTMBEGIN,CANDTTMEND,CANENDTIME,ST,SDXX,.DH,SI,.CANAPPTS)
 I SDFULLPART="F" D FULLCAN(SDCLNIEN,CANDTTMBEGIN,CANREMARKS,.DH,.CANAPPTS)
 Q
PARTCAN(SDCLNIEN,CANDTTMBEGIN,CANDTTMEND,CANENDTIME,ST,SDXX,DH,SI,CANAPPTS) ;
 N FDA,NEWIEN,NEWDTTMIEN,CANMES,CURAVAIL,Y,BLOCKLEN,X,CANCELDATE,P,NOAP
 S BLOCKLEN=CANDTTMEND-CANDTTMBEGIN
 S CANCELDATE=$P(CANDTTMBEGIN,".")
 S FDA(44.05,"+1,"_SDCLNIEN_",",.01)=CANDTTMBEGIN
 S FDA(44.05,"+1,"_SDCLNIEN_",",1)=CANENDTIME
 S NEWIEN(1)=CANDTTMBEGIN
 D UPDATE^DIE(,"FDA","NEWIEN") K FDA,NEWIEN
 ; NOAP seems to be set, but never used.
 S NOAP=$S($O(^SC(SDCLNIEN,"S",(CANDTTMBEGIN-.0001)))'>0:1,$O(^SC(SDCLNIEN,"S",(CANDTTMBEGIN-.0001)))>CANDTTMEND:1,1:0)
 I 'NOAP S NOAP=$S($O(^SC(SDCLNIEN,"S",+$O(^SC(SDCLNIEN,"S",(CANDTTMBEGIN-.0001))),0))="MES":1,1:0)
 I $D(^SC(SDCLNIEN,"S",CANDTTMBEGIN,0)) D
 .S FDA(44.001,CANDTTMBEGIN_","_SDCLNIEN_",",.01)=CANDTTMBEGIN
 .D FILE^DIE(,"FDA") K FDA
 I '$D(^SC(SDCLNIEN,"S",CANDTTMBEGIN,0)) D
 .S FDA(44.001,"+1,"_SDCLNIEN_",",.01)=CANDTTMBEGIN
 .S CANMES="CANCELLED UNTIL "_CANENDTIME_$S(CANREMARKS?.P:"",1:" ("_CANREMARKS_")")
 .S FDA(44.001,"+1,"_SDCLNIEN_",",1400)=CANMES
 .S NEWDTTMIEN(1)=CANDTTMBEGIN
 .D UPDATE^DIE(,"FDA","NEWDTTMIEN") K FDA,NEWDTTMIEN
 ; BELOW LINE OF CODE: ^SC(SC,"ST",SD,"CAN") does not have a field/DD - adding hard set of 'CAN' subscript below with new variables
 ; S ^SC(SC,"ST",SD,"CAN")=^SC(SC,"ST",SD,1)
 S ^SC(SDCLNIEN,"ST",CANCELDATE,"CAN")=$G(^SC(SDCLNIEN,"ST",CANCELDATE,1))
 S CURAVAIL=$$GET1^DIQ(44.005,CANCELDATE_","_SDCLNIEN_",",1,"E")
 S CURAVAIL=CURAVAIL_$J("",SDXX-$L(CURAVAIL))
 S Y=""
 I $G(BLOCKLEN)<100,$L(CURAVAIL)<77 S CURAVAIL=CURAVAIL_"    " ; pad 4 empty spaces needed for blocks < 60 minutes
 F X=0:2:SDXX D
 .S DH=$E(CURAVAIL,X+SI+SI)
 .S P=$S(X<ST:DH_$E(CURAVAIL,X+1+SI+SI),X=SDXX:$S(Y="[":Y,1:DH)_$E(CURAVAIL,X+1+SI+SI),1:$S(Y="["&(X=ST):"]",1:"X")_"X")
 .S Y=$S(DH="]":"",DH="[":DH,1:Y)
 .S CURAVAIL=$E(CURAVAIL,1,X-1+SI+SI)_P_$E(CURAVAIL,X+2+SI+SI,999)
 S:'$F(CURAVAIL,"[") I5=$F(CURAVAIL,"X"),CURAVAIL=$E(CURAVAIL,1,(I5-2))_"["_$E(CURAVAIL,I5,999)
 K I5
 S DH=0
 ; file updated clinic availability
 S FDA(44.005,CANCELDATE_","_SDCLNIEN_",",1)=CURAVAIL
 D FILE^DIE(,"FDA") K FDA
 D EN^SDTMPHLC(SDCLNIEN,CANDTTMBEGIN,CANDTTMEND,"P",CANREMARKS)
 D CANCELAPPTS(SDCLNIEN,CANDTTMBEGIN,CANDTTMEND,CANREMARKS,.DH,.CANAPPTS)
 Q
FULLCAN(SDCLNIEN,CANDTTMBEGIN,CANREMARKS,DH,CANAPPTS) ;
 N FR,TO,SD,CURAVAIL,FDA,DH
 S DH=0
 S SD=$P(CANDTTMBEGIN,".")
 S ^SC(SDCLNIEN,"ST",SD,"CAN")=$G(^SC(SDCLNIEN,"ST",SD,1))
 S CURAVAIL="   "_$E(SD,6,7)_"    **CANCELLED**"
 S FDA(44.005,SD_","_SDCLNIEN_",",1)=CURAVAIL
 D FILE^DIE(,"FDA") K FDA
 S FR=$P(CANDTTMBEGIN,"."),TO=$P(CANDTTMBEGIN,".")+.9
 D EN^SDTMPHLC(SDCLNIEN,SD,,"C","**CANCELLED**")
 D CANCELAPPTS(SDCLNIEN,FR,TO,CANREMARKS,.DH,.CANAPPTS)
 Q
 ;
CANCELAPPTS(SDCLNIEN,CANDTTMBEGIN,CANDTTMEND,CANREMARKS,DH,CANAPPTS) ;
 ;Cancel any appointments that are within the timeframe.
 N TDH,TMPD,DIE,DR,NODE,SDI
 N CANDTLOOP,APPTIEN,IENS,J,J2,DFN,SDCNHDL,SDATA,SDTIME,SDSC,SDTTM,SDPL,TDH,TMPD,APPTSTAT,FDA
 N CANCELREASONIEN,CANBY,CAPPTCNT,CAPPTIEN
 S SDTIME=$$NOW^XLFDT
 S CANCELREASONIEN=$$FIND1^DIC(409.2,,"B","CLINIC CANCELLED")
 S CANBY="C",CAPPTCNT=0
 S CANDTLOOP=CANDTTMBEGIN-.0001
 F  S CANDTLOOP=$O(^SC(SDCLNIEN,"S",CANDTLOOP)) Q:'CANDTLOOP!(CANDTLOOP>CANDTTMEND)  D
 .S APPTIEN=0 F  S APPTIEN=$O(^SC(SDCLNIEN,"S",CANDTLOOP,1,APPTIEN)) Q:'APPTIEN  D
 ..S IENS=APPTIEN_","_CANDTLOOP_","_SDCLNIEN_","
 ..I '$D(^SC(SDCLNIEN,"S",CANDTLOOP,1,APPTIEN,0)) I $D(^SC(SDCLNIEN,"S",CANDTLOOP,1,APPTIEN,"C")) S J=CANDTLOOP,J2=APPTIEN D DELETE^SDC1 K J,J2 Q  ;delete corrupt node
 ..S DFN=$$GET1^DIQ(44.003,IENS,.01,"I")
 ..I 'DFN S J=CANDTLOOP,J2=APPTIEN D DELETE^SDC1 K J,J2 Q  ;if DFN is missing delete record
 ..; do not process if already cancelled
 ..Q:$$GET1^DIQ(44.003,IENS,310,"I")="C"
 ..S SDCNHDL=$$HANDLE^SDAMEVT(1)
 ..D BEFORE^SDAMEVT(.SDATA,DFN,CANDTLOOP,SDCLNIEN,APPTIEN,SDCNHDL)
 ..I $$GET1^DIQ(2.98,CANDTLOOP_","_DFN_",",.01,"I")'=SDCLNIEN Q
 ..S SDSC=SDCLNIEN,SDTTM=CANDTLOOP,SDPL=APPTIEN,TDH=DH,TMPD=CANREMARKS
 ..D CANCEL^SDCNSLT
 ..S DH=TDH
 ..S APPTSTAT=$$GET1^DIQ(2.98,CANDTLOOP_","_DFN_",",.02,"I")
 ..; quit if cancelled
 ..I $D(^DPT(DFN,"S",CANDTLOOP,0)),APPTSTAT["C" Q
 ..S FDA(2.98,CANDTLOOP_","_DFN_",",3)="C"
 ..S FDA(2.98,CANDTLOOP_","_DFN_",",14)=DUZ
 ..S FDA(2.98,CANDTLOOP_","_DFN_",",15)=SDTIME
 ..S FDA(2.98,CANDTLOOP_","_DFN_",",17)=CANREMARKS
 ..D FILE^DIE(,"FDA") K FDA
 ..; clinic cancellation must be filed AFTER the patient cancellation or the ASDCN index will not fire (^SDCAN)
 ..S FDA(44.003,IENS,310)="C"
 ..D FILE^DIE(,"FDA") K FDA
 ..S DH=DH+1,TDH=DH,DH=TDH
 ..D PXRAYRSLTS(SDCLNIEN,CANDTLOOP,DFN,APPTIEN,.DH,SDCNHDL)
 ..S CAPPTIEN=$$APPTGET^SDECUTL(DFN,CANDTLOOP,SDCLNIEN)
 ..D SDEC^SDCNP0(DFN,CANDTLOOP,SDCLNIEN,"C","",$G(CANREMARKS),SDTIME,DUZ)   ;alb/sat 627
 ..; quit if the appointment did not get cancelled
 ..I $$GET1^DIQ(409.84,CAPPTIEN,.17,"I")'="C" Q
 ..S CAPPTCNT=CAPPTCNT+1
 ..S CANAPPTS("CancelledAppointment",CAPPTCNT,"AppointmentID")=CAPPTIEN
 ..; future - consider removing EVT tag and moving CANCEL^SDAMEVT into this loop
 ..;D CANCEL^SDAMEVT(.SDATA,DFN,CANDTLOOP,SDCLNIEN,APPTIEN,0,SDCNHDL) K SDATA,SDCNHDL
 Q
 ; prior X-RAY results to clinic/record tracking
PXRAYRSLTS(SDCLNIEN,CANDTTM,DFN,APPTIEN,DH,SDCNHDL) ;
 ; cannot change the following line since the file manager index doesn't support the filing of "N" as data on the index
 N SDV1,SDIV,SDH,SDTTM,SDSC,SDPL,SDRT,CHKDIV
 ; direct global set on this index - cannot be replaced with a file manager call
 I $D(^SC("ARAD",SDCLNIEN,CANDTTM,DFN)) S ^SC("ARAD",SDCLNIEN,CANDTTM,DFN)="N"
 S CHKDIV=$$GET1^DIQ(44,SDCLNIEN,3.5,"I")
 S SDIV=$S(CHKDIV]"":CHKDIV,1:" 1")
 S SDV1=$S(SDIV:SDIV,1:+$O(^DG(40.8,0)))
 ; no index for the following line to properly kill it with fileman
 I $D(^DPT("ASDPSD","C",SDIV,SDCLNIEN,CANDTTM,DFN)) K ^DPT("ASDPSD","C",SDIV,SDCLNIEN,CANDTTM,DFN)
 ; the following cancels record request from record tracking
 S SDH=DH,SDTTM=CANDTTM,SDSC=SDCLNIEN,SDPL=APPTIEN,SDRT="D" D RT^SDUTL
 S DH=SDH K SDH
 D CK1^SDESCCAVAIL2(CANDTTM,DFN,SDIV)
 D EVT^SDESCCAVAIL2(DFN,SDTTM,SDSC,SDPL,SDCNHDL)
 K SD1,SDIV,SDPL,SDRT,SDSC,SDTTM,SDX
 Q