SDEC07 ;ALB/SAT,PC,KML,MGD,LAB,TJB/BLB,TJB - ADD NEW APPOINTMENT ;Jun 23,2023
;;5.3;Scheduling;**627,642,651,658,665,669,671,672,701,686,740,694,785,788,790,799,801,805,816,819,842,843,847,851,869,875,877**;Aug 13, 1993;Build 14
;;Per VHA Directive 6402, this routine should not be modified
;
; Reference to ^GMR(123 is supported by IA #4837
Q
APPADD(SDECY,SDECSTART,SDECEND,DFN,SDECRES,SDECLEN,SDECNOTE,SDECATID,SDECCR,SDMRTC,SDDDT,SDREQBY,SDLAB,PROVIEN,SDID,SDAPTYP,SDSVCP,SDSVCPR,SDCL,SDEKG,SDXRAY,APPTYPE,EESTAT,OVB,SDPARENT,SDEL,EAS) ;
N SDAPPTYP,CKATYP
N SDECERR,SDECIEN,SDECDEP,SDECI,SDECJ,SDECAPPTI,SDECDJ,SDECRESD,SDECRNOD,SDECC,SDECERR,SDECWKIN,ITYP,REQIEN
N SDECNOEV,SDECDEV,SDECDERR,SDECTMP,SAVESTRT,SDAREQ0,XQOPT
N %DT,X,Y,DGQUIET,OBM,RET
N SDOE,DONE
;Don't execute SDEC ADD APPOINTMENT protocol
S SDECNOEV=1
K ^TMP("SDEC07",$J)
S SDECERR=0
S SDECI=0
S SDECY="^TMP(""SDEC07"","_$J_")"
S ^TMP("SDEC07",$J,SDECI)="I00020APPOINTMENTID^T00020ERRORID"_$C(30)
S SDECI=SDECI+1
S ITYP="SDEC07 Error: Invalid Appointment Type"
S CKATYP=$P($G(SDAPTYP),"|",1)
I ("^A^C^R^"'[("^"_CKATYP_"^")) D ERR(SDECI+1,ITYP,,0) Q
S REQIEN=+$P($G(SDAPTYP),"|",2)
I 'REQIEN D ERR(SDECI+1,ITYP,,0) Q
I CKATYP="E" I '$D(^SDWL(409.3,REQIEN,0)) D ERR(SDECI+1,ITYP,,0) Q
I CKATYP="R" I '$D(^SD(403.5,REQIEN,0)) D ERR(SDECI+1,ITYP,,0) Q
I CKATYP="C" I '$D(^GMR(123,REQIEN,0)) D ERR(SDECI+1,ITYP,,0) Q
I CKATYP="A" I '$D(^SDEC(409.85,REQIEN,0)) D ERR(SDECI+1,ITYP,,0) Q
I CKATYP="A" S SDAREQ0=$G(^SDEC(409.85,REQIEN,0)) I $P(SDAREQ0,U,5)="RTC",$P(SDAREQ0,U,17)="C" D ERR(SDECI+1,"SDEC07 Error: This RTC request has been closed.",,0) Q
; MGH save date/time for consult request
S SAVESTRT=SDECSTART
S ITYP="SDEC07 Error: The patient on the appointment request and the subsequent appointment must match."
I CKATYP="R" I DFN'=$$GET1^DIQ(403.5,REQIEN,.01,"I") D ERR(SDECI+1,ITYP,,0) Q
I CKATYP="C" I DFN'=$$GET1^DIQ(123,REQIEN,.02,"I") D ERR(SDECI+1,ITYP,,0) Q
I CKATYP="A" I DFN'=$$GET1^DIQ(409.85,REQIEN,.01,"I") D ERR(SDECI+1,ITYP,,0) Q
;
S SDECSTART=$$NETTOFM^SDECDATE(SDECSTART,"Y")
I SDECSTART=-1 D ERR(SDECI+1,"SDEC07 Error: Invalid Start Time",,0) Q
S SDECEND=$$NETTOFM^SDECDATE(SDECEND,"Y")
I SDECEND=-1 D ERR(SDECI+1,"SDEC07 Error: Invalid End Time",,0) Q
I $L(SDECEND,".")=1 D ERR(SDECI+1,"SDEC07 Error: Invalid End Time",,0) Q
I SDECSTART>SDECEND S SDECTMP=SDECEND,SDECEND=SDECSTART,SDECSTART=SDECTMP
S DFN=$G(DFN)
I DFN="" D ERR(SDECI+1,"SDEC07: Patient ID required.",,0) Q
I '$D(^DPT(DFN,0)) D ERR(SDECI+1,"SDEC07 Error: Invalid Patient ID",,0) Q
L +^DPT(DFN):3 I '$T D ERR(SDECI+1,"Patient is being edited. Try again later.",,0) Q
D OP^XQCHK
N POP
S POP=0
S:(CKATYP="A")&($P($G(XQOPT),U)'="SD RECEIVE OR") POP=$$ORDERLOCKCHECK^SDEC07C($P(SDAREQ0,"^",5),$P(SDAPTYP,"|",2),.SDECI,DFN)
Q:POP
; Reject if another appointment scheduled at same time.
I $D(^DPT(DFN,"S",SDECSTART)),$P(^(SDECSTART,0),U,2)'="C",$P(^(0),U,2)'="PC" D ERR(SDECI+1,"Appointment in "_$P(^SC($P(^(0),U,1),0),U,1)_" already scheduled at the same time.",DFN,1) Q ;SD,740
;Validate Resource
S SDECERR=0 K SDECRESD
S SDECRES=$G(SDECRES) I SDECRES="" D ERR(SDECI+1,"SDEC07 Error: Invalid Resource ID",DFN,1) Q
I +SDECRES,'$D(^SDEC(409.831,SDECRES,0)) D ERR(SDECI+1,"SDEC07 Error: Invalid Resource ID",DFN,1) Q
I '+SDECRES,'$D(^SDEC(409.831,"B",SDECRES)) D ERR(SDECI+1,"SDEC07 Error: Invalid Resource ID",DFN,1) Q
S SDECRESD=$S(+SDECRES:+SDECRES,1:$O(^SDEC(409.831,"B",SDECRES,0)))
S SDECRNOD=$G(^SDEC(409.831,SDECRESD,0))
I SDECRNOD="" D ERR(SDECI+1,"SDEC07 Error: Unable to add appointment -- invalid Resource entry.",DFN,1) Q
N PTR44,MAXDAYS S PTR44=$P(SDECRNOD,"^",4),MAXDAYS=390
I +PTR44,$D(^SC(PTR44,"SDP")) S MAXDAYS=$P(^("SDP"),"^",2) S:MAXDAYS="" MAXDAYS=390
I SDECSTART>$$FMADD^XLFDT($$NOW^XLFDT(),MAXDAYS) D ERR(SDECI+1,"Appointment date too far in the future",DFN,1) Q
S SDECWKIN=0
S SDECATID=$G(SDECATID)
I SDECATID="WALKIN" S SDECWKIN=1
I SDECATID'?.N&(SDECATID'="WALKIN") S SDECATID=""
S SDECLEN=$G(SDECLEN)
;validate MTRC flag (optional)
S SDMRTC=$$UP^XLFSTR($G(SDMRTC))
S SDMRTC=$S(SDMRTC="TRUE":1,1:0)
;validate desired date of appt (optional)
S SDDDT=$G(SDDDT)
I SDDDT'="" S %DT="" S X=$P(SDDDT,"@",1) D ^%DT S SDDDT=Y I Y=-1 S SDDDT=""
I SDDDT="",SDECATID'="WALKIN" S SDDDT=$P(SDECSTART,".",1)
;validate requested by
S SDREQBY=$$UP^XLFSTR($G(SDREQBY))
I SDREQBY'="" S SDREQBY=$S(SDREQBY="PROVIDER":1,SDREQBY="PATIENT":2,1:0)
;validate lab date/time (optional)
S SDLAB=$G(SDLAB)
I SDLAB'="" S %DT="T" S X=SDLAB D ^%DT S SDLAB=Y I Y=-1 S SDLAB=""
;validate EKG date/time (optional)
S SDEKG=$G(SDEKG)
I SDEKG'="" S %DT="T" S X=SDEKG D ^%DT S SDEKG=Y I Y=-1 S SDEKG=""
;validate XRAY date/time (optional)
S SDXRAY=$G(SDXRAY)
I SDXRAY'="" S %DT="T" S X=SDXRAY D ^%DT S SDXRAY=Y I Y=-1 S SDXRAY=""
S SDID=$G(SDID)
;validate clini101
S SDCL=$G(SDCL)
I SDCL'="" I '$D(^SC(SDCL,0)) S SDCL=""
;clinic ID ;support for single HOSPITAL LOCATION in SDEC RESOURCE
I SDCL="" S SDCL=$$GET1^DIQ(409.831,SDECRESD_",",.04,"I")
S OVB=+$G(OVB)
I 'OVB S OBM=$$OBM1^SDEC57(SDCL,SDECSTART,SDMRTC,,+SDECWKIN) I OBM'="",+OBM'=1 S SDECAPPTID=0 D ERR(SDECI+1,"OBM"_OBM,DFN,1) Q ;alb/sat 658 check if overbook ;alb/sat 665 clear SDECAPPTID ;SD,740
;validate appt request type (required)
I SDCL="" D
.S:$P(SDAPTYP,"|",1)="E" SDCL=$$GET1^DIQ(409.3,$P(SDAPTYP,"|",2)_",",13.2,"I")
.S:$P(SDAPTYP,"|",1)="R" SDCL=$$GET1^DIQ(403.5,$P(SDAPTYP,"|",2)_",",4.5,"I")
.;ICR 4837 ICR states 'Zero node read into variable'
.S:$P(SDAPTYP,"|",1)="C" SDCL=$P($G(^GMR(123,+$P(SDAPTYP,"|",2),0)),U,4)
.S:$P(SDAPTYP,"|",1)="A" SDCL=$$GET1^DIQ(409.85,$P(SDAPTYP,"|",2)_",",8,"I")
I SDCL="" D ERR(SDECI+1,"SDEC07 Error: Invalid clinic ID.",DFN,1) Q
I $$INACTIVE^SDEC32(SDCL) D ERR(SDECI+1,"SDEC07 Error: "_$$GET1^DIQ(44,SDCL_",",.01)_" is an inactive clinic.",DFN,1) Q ;SD,740
;Reject if consult is not active or pending. SD*5.3*686
I $P(SDAPTYP,"|",1)="C" N CNSLTSTS,NOTOK S CNSLTSTS=$P($G(^GMR(123,+$P(SDAPTYP,"|",2),0)),U,12),NOTOK=0 D Q:NOTOK ;
. I CNSLTSTS'=5,CNSLTSTS'=6 D ERR(SDECI+1,"Consult status is not PENDING or ACTIVE. It cannot be scheduled.",DFN,1) S NOTOK=1 Q ;SD,740
;
;validate provider
S DONE=0
I $P(SDAPTYP,"|",1)'="R" D
. I $G(PROVIEN),'$D(^VA(200,+$G(PROVIEN),0)) D ERR(SDECI+1,"SDEC07 Error: Invalid provider ID.",DFN,1) S DONE=1
. I $G(PROVIEN)="" S PROVIEN=$$GETPROVIDER^SDESCREATEAPPT(SDCL,$P(SDAPTYP,"|",1),$P(SDAPTYP,"|",2))
I $P(SDAPTYP,"|",1)="R" D
. I $G(PROVIEN),'$$GET1^DIQ(403.54,PROVIEN,.01,"I") D ERR(SDECI+1,"SDEC07 Error: Invalid recall provider ID.",DFN,1) S DONE=1
Q:DONE
;
;validate service connected
S SDSVCPR=$G(SDSVCPR)
I SDSVCPR'="" S:(+SDSVCPR<0)!(+SDSVCPR>100) SDSVCPR=""
S SDSVCP=$G(SDSVCP)
S SDSVCP=$S(SDSVCP=0:0,SDSVCP="NO":0,SDSVCP=1:1,SDSVCP="YES":1,1:"")
;validate note ;alb/sat 658 - only use 1st 150 characters
S SDECNOTE=$G(SDECNOTE) S:SDECNOTE'="" SDECNOTE=$TR($E(SDECNOTE,1,150),"^"," ")
;validate APPTYPE
S APPTYPE=$G(APPTYPE) I APPTYPE'="",'$D(^SD(409.1,+APPTYPE,0)) S APPTYPE=""
;validate Patient Status (EESTAT)
S EESTAT=$G(EESTAT)
I EESTAT="" D
.I $P(SDAPTYP,"|",1)="E" S EESTAT=$$GET1^DIQ(409.3,$P(SDAPTYP,"|",2)_",",27,"I")
.I $P(SDAPTYP,"|",1)="A" S EESTAT=$$GET1^DIQ(409.3,$P(SDAPTYP,"|",2)_",",.02,"I")
S EESTAT=$S(EESTAT="N":"N",EESTAT="NEW":"N",EESTAT="E":"E",EESTAT="ESTABLISHED":"E",1:"")
;validate OVB (overbook)
S OVB=+$G(OVB)
I 'OVB D
.D OVBOOK^SDEC07A(.RET,SDCL,SDECSTART,SDECRES)
D
.S SDAPPTYP=+APPTYPE
.I 'SDAPPTYP D
..I $P(SDAPTYP,"|",1)="E" S SDAPPTYP=$$GET1^DIQ(409.3,+$P(SDAPTYP,"|",2)_",",8.7,"I")
..I $P(SDAPTYP,"|",1)="A" S SDAPPTYP=$$GET1^DIQ(409.85,+$P(SDAPTYP,"|",2)_",",8.7,"I")
..I $P(SDAPTYP,"|",1)="C",+APPTYPE S SDAPPTYP=+APPTYPE
.S:'SDAPPTYP SDAPPTYP=$O(^SD(409.1,"B","REGULAR",0))
S EAS=$TR($G(EAS),"^"," ")
I $L(EAS) S EAS=$$EASVALIDATE^SDESUTIL(EAS)
I EAS=-1 D ERR(SDECI+1,"SDEC07 Error: Invalid EAS Tracking Number",,0) Q
S SDECAPPTID=$$SDECADD(SDECSTART,SDECEND,DFN,SDECRESD,SDECATID,SDDDT,SDID,SDAPTYP,PROVIEN,SDCL,SDECNOTE,SAVESTRT,SDECRES,SDAPPTYP,EESTAT,1,+SDECLEN,EAS) ;alb/sat 665 add SDECLEN
I 'SDECAPPTID D ERR(SDECI+1,"SDEC07 Error: Unable to add appointment to SDEC APPOINTMENT file.",DFN,2) Q ;SD,740
;Lock SDEC node
L +^SDEC(409.84,SDECAPPTID):5 I '$T D ERR(SDECI+1,"Another user is working with this patient's record. Please try again later",DFN,1) Q ; Fixed Lock *790
;Save the appt
; call chart request
S SDECDEV=""
I SDECATID="WALKIN",$G(SDECCR),$G(SDECDEV)'="" S DGQUIET=1 D WISD^SDECRT(DFN,$P(SDECSTART,"."),"",SDECDEV)
I SDECNOTE]"" D SDECWP(SDECAPPTID,SDECNOTE)
I +SDCL,$D(^SC(SDCL,0)) D I +SDECERR D ERR(SDECI+1,$P(SDECERR,U,2),DFN,2)
. S SDECC("PAT")=DFN
. S SDECC("CLN")=SDCL
. S SDECC("TYP")=$S(+SDECWKIN:4,SDAPPTYP=1:1,1:3)
. S SDECC("COL")=$S(SDAPPTYP=7:1,1:"")
. S SDECC("APT")=SDAPPTYP
. S SDECC("ADT")=SDECSTART
. S SDECC("LEN")=SDECLEN
. S SDECC("OI")=$E($G(SDECNOTE),1,150)
. S SDECC("OI")=$TR(SDECC("OI"),";"," ")
. S SDECC("OI")=$$STRIP(SDECC("OI"))
. S SDECC("RES")=SDECRESD
. S SDECC("USR")=DUZ
. S SDECC("MTR")=$G(SDMRTC)
. S SDECC("DDT")=SDDDT
. S SDECC("REQ")=SDREQBY
. S SDECC("LAB")=SDLAB
. S SDECC("XRA")=SDXRAY
. S SDECC("EKG")=SDEKG
. S SDECC("OVB")=$S(OVB=1:1,1:0)
. S SDECC("ELG")=SDEL
. S:$P(SDAPTYP,"|",1)="C" SDECC("CON")=$P(SDAPTYP,"|",2)
. S SDECERR=$$MAKE^SDEC07B(.SDECC)
. Q:SDECERR
. D AVUPDT^SDEC07C(SDCL,SDECSTART,SDECLEN)
. Q
I $P(SDAPTYP,"|",1)="E" D EWL^SDEC07A($P(SDAPTYP,"|",2),SDECSTART,SDCL,SDSVCPR,SDSVCP,,SDAPPTYP)
I $P(SDAPTYP,"|",1)="A" D
.D UPDATE^SDECAR2($P(SDAPTYP,"|",2),SDECSTART,SDCL,SDSVCPR,SDSVCP,,SDAPPTYP,EAS) ;alb/sat 658 do not pass note
.I $G(SDMRTC),$G(SDPARENT) D AR433^SDECAR2(SDPARENT,SDECAPPTID_"~"_$P(SDAPTYP,"|",2))
.D:$G(SDPARENT) AR438^SDECAR2($P(SDAPTYP,"|",2),SDPARENT)
N SDT S SDT=SDECSTART
I $$NOW^XLFDT>SDT,$$NEW^SDPCE(SDT) D
.N SDCOED
.S SDOE=$$GETAPT^SDVSIT2(DFN,SDT,SDCL)
L -^SDEC(409.84,SDECAPPTID) ; Fixed Lock *790
L -^DPT(DFN)
S SDECI=SDECI+1
S ^TMP("SDEC07",$J,SDECI)=SDECAPPTID_"^"_$G(SDECDERR)_$C(30)
S SDECI=SDECI+1
S ^TMP("SDEC07",$J,SDECI)=$C(31)
Q
;
;ADD SDEC APPT ENTRY
SDECADD(SDECSTART,SDECEND,DFN,SDECRESD,SDECATID,SDDDT,SDID,SDAPTYP,PROVIEN,SDCL,SDECNOTE,SAVESTRT,SDECRES,SDAPPTYP,EESTAT,SDF,SDECLEN,EAS) ;alb/sat 665 add SDECLEN
N SDIEN,SDECAPPTID,SDECIEN,SDECMSG,SL,X,SDDFN,FDA,ERR,REQIEN
S SDECSTART=$G(SDECSTART)
S SAVESTRT=$G(SAVESTRT),SDECRES=$G(SDECRES) ;MGH save date/time for consult request
S DFN=$G(DFN),SDDFN=DFN
S SDECRESD=$G(SDECRESD)
S SDECATID=$G(SDECATID)
S SDDDT=$G(SDDDT)
S SDID=$G(SDID)
S SDAPTYP=$G(SDAPTYP)
S SDAPPTYP=$G(SDAPPTYP)
S PROVIEN=$G(PROVIEN)
S SDCL=$G(SDCL)
S SDECEND=$G(SDECEND)
I $P(SDAPTYP,"|",1)="R" D
.S PROVIEN=$$GET1^DIQ(403.54,PROVIEN,.01,"I")
S SDECLEN=$G(SDECLEN)
I SDECLEN="",SDECEND="" S SDECLEN=+$G(^SC(SDCL,"SL")) S:'+SDECLEN SDECLEN=30 S SDECEND=$$FMADD^XLFDT(SDECSTART,,,+SDECLEN) ;no length or end date/time
I SDECLEN="",SDECEND'="" S SDECLEN=$$FMDIFF^XLFDT(SDECEND,SDECSTART,2)\60 ;no length
I SDECLEN'="",SDECEND="" S SDECEND=$$FMADD^XLFDT(SDECSTART,,,+SDECLEN) ;no end date/time
S SDECNOTE=$G(SDECNOTE)
S SDF=$G(SDF,0)
S EAS=$G(EAS)
I PROVIEN="" S PROVIEN=$$GET1^DIQ(44,SDCL_",",16,"I")
S SDIEN=$$APPTGET^SDECUTL(DFN,SDECSTART,SDCL)
S SDIEN=$S(SDIEN'="":SDIEN_",",1:"+1,") K FDA
S FDA(409.84,SDIEN,.01)=SDECSTART
S FDA(409.84,SDIEN,.02)=SDECEND
S FDA(409.84,SDIEN,.03)="@"
S FDA(409.84,SDIEN,.04)="@"
S FDA(409.84,SDIEN,.05)=DFN
S FDA(409.84,SDIEN,.06)=$S(+SDAPPTYP:SDAPPTYP,1:"@")
S FDA(409.84,SDIEN,.07)=SDECRESD
S FDA(409.84,SDIEN,.08)=$G(DUZ)
S FDA(409.84,SDIEN,.09)=$$NOW^XLFDT()
S FDA(409.84,SDIEN,.1)="@"
S FDA(409.84,SDIEN,.101)="@"
S FDA(409.84,SDIEN,.102)="@"
S FDA(409.84,SDIEN,.11)="@"
S FDA(409.84,SDIEN,.12)="@"
S FDA(409.84,SDIEN,.121)="@"
S FDA(409.84,SDIEN,.122)="@"
S FDA(409.84,SDIEN,.13)=$S(SDECATID="WALKIN":"y",1:"@")
S FDA(409.84,SDIEN,.14)="@"
S FDA(409.84,SDIEN,.16)=$S(PROVIEN'="":PROVIEN,1:"@")
S FDA(409.84,SDIEN,.17)="@"
S FDA(409.84,SDIEN,.18)=$S($G(SDECLEN)'="":SDECLEN,1:"@")
S FDA(409.84,SDIEN,.2)=SDDDT
S FDA(409.84,SDIEN,.21)=$S($G(SDID)'="":SDID,1:"@")
S FDA(409.84,SDIEN,.22)=$S(SDAPTYP'="":$P(SDAPTYP,"|",2)_";"_$S($P(SDAPTYP,"|",1)="E":"SDWL(409.3,",$P(SDAPTYP,"|",1)="C":"GMR(123,",$P(SDAPTYP,"|",1)="R":"SD(403.5,",$P(SDAPTYP,"|",1)="A":"SDEC(409.85,",1:""),1:"@")
S FDA(409.84,SDIEN,.23)=$S($G(EESTAT)'="":EESTAT,1:"@")
S:EAS'="" FDA(409.84,SDIEN,100)=EAS
K SDECIEN,SDECMSG
D UPDATE^DIE("","FDA","SDECIEN","SDECMSG")
I $P(SDAPTYP,"|",1)="A"!($P(SDAPTYP,"|",1)="R") D
.S REQIEN=$P(SDAPTYP,"|",2)
.I $$LASTPIDCHECK^SDECAR2(REQIEN,SDDDT) D
..K FDA S FDA(409.854,"+1,"_REQIEN_",",.01)=$$NOW^XLFDT
..S FDA(409.854,"+1,"_REQIEN_",",1)=SDDDT
..S FDA(409.854,"+1,"_REQIEN_",",2)=$$GET1^DIQ(200,DUZ,.01,"E")
..D UPDATE^DIE(,"FDA",,"ERR") K FDA
S SDECAPPTID=$S(SDIEN'="+1,":+SDIEN,1:+$G(SDECIEN(1)))
K SDECMSG
I SDECNOTE="" D WP^DIE(409.84,$S(+$G(SDECAPPTID):SDECAPPTID_",",1:SDIEN_","),1,"","@","SDECMSG")
I SDECNOTE'="" N ARR D WP^SDECUTL(.ARR,SDECNOTE) D WP^DIE(409.84,$S(+$G(SDECAPPTID):SDECAPPTID_",",1:SDIEN_","),1,"","ARR","SDECMSG")
I SDECAPPTID'="" D
.I $P(SDAPTYP,"|",1)="C",SDF D
..D CONSULTPID^SDEC07PID(SDAPTYP,DFN,PROVIEN,SAVESTRT,SDDDT,SDDFN,SDECNOTE,SDECRES)
.;
.I $P(SDAPTYP,"|",1)="R" D ; VSE-863 ;6/9/2021
.. N SDCOMM,SDRET,SDRIEN1,SDRRFTR
.. S SDRIEN1=$P(SDAPTYP,"|",2)
.. S SDCOMM=$$GET1^DIQ(403.5,SDRIEN1,2.5)
.. S SDRRFTR="APPT SCHEDULED" ; "7"
.. D RECDSET^SDEC52A(.SDRET,SDRIEN1,SDRRFTR,SDCOMM)
.;
I $G(SDECAPPTID) D SETMISSIONELIG^SDESMISSIONELG(SDECAPPTID)
Q SDECAPPTID
;
CONSPIDCHECK(SDRIEN1,SDDDT) ;
N CHIEN,CHSIEN,OLDPID
S CHIEN=$O(^SDEC(409.87,"B",SDRIEN1,0))
S CHSIEN=$O(^SDEC(409.87,CHIEN,1,9999999),-1)
S OLDPID=$$GET1^DIQ(409.871,CHSIEN_","_CHIEN_",",1,"I")
I OLDPID'=$G(SDDDT) Q 1
Q 0
SDECWP(SDECAPPTID,SDECNOTE) ;
;Add WP field
I SDECNOTE]"" S SDECNOTE(.5)=SDECNOTE,SDECNOTE=""
I $D(SDECNOTE(0)) S SDECNOTE(.5)=SDECNOTE(0) K SDECNOTE(0)
I $D(SDECNOTE(.5)) D
. D WP^DIE(409.84,SDECAPPTID_",",1,"","SDECNOTE","SDECMSG")
Q
;
ADDEVT(DFN,SDECSTART,SDECSC,SDCLA) ;EP
;Called by SDEC ADD APPOINTMENT protocol
;SDECSC=IEN of clinic in ^SC
;SDCLA=IEN for ^SC(SDECSC,"S",SDECSTART,1,SDCLA). Use to get Length & Note
;
N SDECNOD,SDECLEN,SDECAPPTID,SDECNODP,SDECWKIN,SDECRES
Q:+$G(SDECNOEV)
I $D(^SDEC(409.831,"ALOC",SDECSC)) S SDECRES=$O(^SDEC(409.831,"ALOC",SDECSC,0))
Q:'+$G(SDECRES)
S SDECNOD=$G(^SC(SDECSC,"S",SDECSTART,1,SDCLA,0))
Q:SDECNOD=""
S SDECNODP=$G(^DPT(DFN,"S",SDECSTART,0))
S SDECWKIN=""
;Purpose of Visit field of DPT Appointment subfile
S:$P(SDECNODP,U,7)=4 SDECWKIN="WALKIN"
S SDECLEN=$P(SDECNOD,U,2)
Q:'+SDECLEN
S SDECEND=$$FMADD^XLFDT(SDECSTART,0,0,SDECLEN,0)
S SDECAPPTID=$$SDECADD(SDECSTART,SDECEND,DFN,SDECRES,SDECWKIN,,,,,SDECSC,,,,,,1,+SDECLEN) ;alb/sat 665 add SDECLEN
Q:'+SDECAPPTID
S SDECNOTE=$P(SDECNOD,U,4)
I SDECNOTE]"" D SDECWP(SDECAPPTID,SDECNOTE)
D ADDEVT3(SDECRES)
Q
;
ADDEVT3(SDECRES) ;
;Call RaiseEvent to notify GUI clients
Q
;
STRIP(SDECZ) ;Replace control characters with spaces
N SDECI
F SDECI=1:1:$L(SDECZ) I (32>$A($E(SDECZ,SDECI))) S SDECZ=$E(SDECZ,1,SDECI-1)_" "_$E(SDECZ,SDECI+1,999)
Q SDECZ
;
ERR(SDECI,SDECERR,DFN,LOCK) ;Error processing SD,740
S DFN=$G(DFN)
S SDECI=SDECI+1
S SDECERR=$TR(SDECERR,"^","~")
S ^TMP("SDEC07",$J,SDECI)=$G(SDECAPPTID,0)_"^"_SDECERR_$C(30)
S SDECI=SDECI+1
S ^TMP("SDEC07",$J,SDECI)=$C(31)
I $G(LOCK)=1 L -^DPT(DFN)
I $G(LOCK)=2 L -^DPT(DFN)
I +$G(SDECAPPTID) L -^SDEC(409.84,SDECAPPTID)
Q
;
ETRAP ;EP Error trap entry
D ^%ZTER
I '$D(SDECI) N SDECI S SDECI=999999
S SDECI=SDECI+1
D ERR(SDECI,"SDEC07 Error")
Q
ERROR ;
D ERR1("Error")
Q
;
ERR1(SDECERR) ;Error processing
S SDECI=SDECI+1
S ^TMP("SDEC07",$J,SDECI)=SDECERR_$C(30)
S SDECI=SDECI+1
S ^TMP("SDEC07",$J,SDECI)=$C(31)
Q
--- Routine Detail --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HSDEC07 16332 printed Oct 16, 2024@18:50:31 Page 2
SDEC07 ;ALB/SAT,PC,KML,MGD,LAB,TJB/BLB,TJB - ADD NEW APPOINTMENT ;Jun 23,2023
+1 ;;5.3;Scheduling;**627,642,651,658,665,669,671,672,701,686,740,694,785,788,790,799,801,805,816,819,842,843,847,851,869,875,877**;Aug 13, 1993;Build 14
+2 ;;Per VHA Directive 6402, this routine should not be modified
+3 ;
+4 ; Reference to ^GMR(123 is supported by IA #4837
+5 QUIT
APPADD(SDECY,SDECSTART,SDECEND,DFN,SDECRES,SDECLEN,SDECNOTE,SDECATID,SDECCR,SDMRTC,SDDDT,SDREQBY,SDLAB,PROVIEN,SDID,SDAPTYP,SDSVCP,SDSVCPR,SDCL,SDEKG,SDXRAY,APPTYPE,EESTAT,OVB,SDPARENT,SDEL,EAS) ;
+1 NEW SDAPPTYP,CKATYP
+2 NEW SDECERR,SDECIEN,SDECDEP,SDECI,SDECJ,SDECAPPTI,SDECDJ,SDECRESD,SDECRNOD,SDECC,SDECERR,SDECWKIN,ITYP,REQIEN
+3 NEW SDECNOEV,SDECDEV,SDECDERR,SDECTMP,SAVESTRT,SDAREQ0,XQOPT
+4 NEW %DT,X,Y,DGQUIET,OBM,RET
+5 NEW SDOE,DONE
+6 ;Don't execute SDEC ADD APPOINTMENT protocol
+7 SET SDECNOEV=1
+8 KILL ^TMP("SDEC07",$JOB)
+9 SET SDECERR=0
+10 SET SDECI=0
+11 SET SDECY="^TMP(""SDEC07"","_$JOB_")"
+12 SET ^TMP("SDEC07",$JOB,SDECI)="I00020APPOINTMENTID^T00020ERRORID"_$CHAR(30)
+13 SET SDECI=SDECI+1
+14 SET ITYP="SDEC07 Error: Invalid Appointment Type"
+15 SET CKATYP=$PIECE($GET(SDAPTYP),"|",1)
+16 IF ("^A^C^R^"'[("^"_CKATYP_"^"))
DO ERR(SDECI+1,ITYP,,0)
QUIT
+17 SET REQIEN=+$PIECE($GET(SDAPTYP),"|",2)
+18 IF 'REQIEN
DO ERR(SDECI+1,ITYP,,0)
QUIT
+19 IF CKATYP="E"
IF '$DATA(^SDWL(409.3,REQIEN,0))
DO ERR(SDECI+1,ITYP,,0)
QUIT
+20 IF CKATYP="R"
IF '$DATA(^SD(403.5,REQIEN,0))
DO ERR(SDECI+1,ITYP,,0)
QUIT
+21 IF CKATYP="C"
IF '$DATA(^GMR(123,REQIEN,0))
DO ERR(SDECI+1,ITYP,,0)
QUIT
+22 IF CKATYP="A"
IF '$DATA(^SDEC(409.85,REQIEN,0))
DO ERR(SDECI+1,ITYP,,0)
QUIT
+23 IF CKATYP="A"
SET SDAREQ0=$GET(^SDEC(409.85,REQIEN,0))
IF $PIECE(SDAREQ0,U,5)="RTC"
IF $PIECE(SDAREQ0,U,17)="C"
DO ERR(SDECI+1,"SDEC07 Error: This RTC request has been closed.",,0)
QUIT
+24 ; MGH save date/time for consult request
+25 SET SAVESTRT=SDECSTART
+26 SET ITYP="SDEC07 Error: The patient on the appointment request and the subsequent appointment must match."
+27 IF CKATYP="R"
IF DFN'=$$GET1^DIQ(403.5,REQIEN,.01,"I")
DO ERR(SDECI+1,ITYP,,0)
QUIT
+28 IF CKATYP="C"
IF DFN'=$$GET1^DIQ(123,REQIEN,.02,"I")
DO ERR(SDECI+1,ITYP,,0)
QUIT
+29 IF CKATYP="A"
IF DFN'=$$GET1^DIQ(409.85,REQIEN,.01,"I")
DO ERR(SDECI+1,ITYP,,0)
QUIT
+30 ;
+31 SET SDECSTART=$$NETTOFM^SDECDATE(SDECSTART,"Y")
+32 IF SDECSTART=-1
DO ERR(SDECI+1,"SDEC07 Error: Invalid Start Time",,0)
QUIT
+33 SET SDECEND=$$NETTOFM^SDECDATE(SDECEND,"Y")
+34 IF SDECEND=-1
DO ERR(SDECI+1,"SDEC07 Error: Invalid End Time",,0)
QUIT
+35 IF $LENGTH(SDECEND,".")=1
DO ERR(SDECI+1,"SDEC07 Error: Invalid End Time",,0)
QUIT
+36 IF SDECSTART>SDECEND
SET SDECTMP=SDECEND
SET SDECEND=SDECSTART
SET SDECSTART=SDECTMP
+37 SET DFN=$GET(DFN)
+38 IF DFN=""
DO ERR(SDECI+1,"SDEC07: Patient ID required.",,0)
QUIT
+39 IF '$DATA(^DPT(DFN,0))
DO ERR(SDECI+1,"SDEC07 Error: Invalid Patient ID",,0)
QUIT
+40 LOCK +^DPT(DFN):3
IF '$TEST
DO ERR(SDECI+1,"Patient is being edited. Try again later.",,0)
QUIT
+41 DO OP^XQCHK
+42 NEW POP
+43 SET POP=0
+44 if (CKATYP="A")&($PIECE($GET(XQOPT),U)'="SD RECEIVE OR")
SET POP=$$ORDERLOCKCHECK^SDEC07C($PIECE(SDAREQ0,"^",5),$PIECE(SDAPTYP,"|",2),.SDECI,DFN)
+45 if POP
QUIT
+46 ; Reject if another appointment scheduled at same time.
+47 ;SD,740
IF $DATA(^DPT(DFN,"S",SDECSTART))
IF $PIECE(^(SDECSTART,0),U,2)'="C"
IF $PIECE(^(0),U,2)'="PC"
DO ERR(SDECI+1,"Appointment in "_$PIECE(^SC($PIECE(^(0),U,1),0),U,1)_" already scheduled at the same time.",DFN,1)
QUIT
+48 ;Validate Resource
+49 SET SDECERR=0
KILL SDECRESD
+50 SET SDECRES=$GET(SDECRES)
IF SDECRES=""
DO ERR(SDECI+1,"SDEC07 Error: Invalid Resource ID",DFN,1)
QUIT
+51 IF +SDECRES
IF '$DATA(^SDEC(409.831,SDECRES,0))
DO ERR(SDECI+1,"SDEC07 Error: Invalid Resource ID",DFN,1)
QUIT
+52 IF '+SDECRES
IF '$DATA(^SDEC(409.831,"B",SDECRES))
DO ERR(SDECI+1,"SDEC07 Error: Invalid Resource ID",DFN,1)
QUIT
+53 SET SDECRESD=$SELECT(+SDECRES:+SDECRES,1:$ORDER(^SDEC(409.831,"B",SDECRES,0)))
+54 SET SDECRNOD=$GET(^SDEC(409.831,SDECRESD,0))
+55 IF SDECRNOD=""
DO ERR(SDECI+1,"SDEC07 Error: Unable to add appointment -- invalid Resource entry.",DFN,1)
QUIT
+56 NEW PTR44,MAXDAYS
SET PTR44=$PIECE(SDECRNOD,"^",4)
SET MAXDAYS=390
+57 IF +PTR44
IF $DATA(^SC(PTR44,"SDP"))
SET MAXDAYS=$PIECE(^("SDP"),"^",2)
if MAXDAYS=""
SET MAXDAYS=390
+58 IF SDECSTART>$$FMADD^XLFDT($$NOW^XLFDT(),MAXDAYS)
DO ERR(SDECI+1,"Appointment date too far in the future",DFN,1)
QUIT
+59 SET SDECWKIN=0
+60 SET SDECATID=$GET(SDECATID)
+61 IF SDECATID="WALKIN"
SET SDECWKIN=1
+62 IF SDECATID'?.N&(SDECATID'="WALKIN")
SET SDECATID=""
+63 SET SDECLEN=$GET(SDECLEN)
+64 ;validate MTRC flag (optional)
+65 SET SDMRTC=$$UP^XLFSTR($GET(SDMRTC))
+66 SET SDMRTC=$SELECT(SDMRTC="TRUE":1,1:0)
+67 ;validate desired date of appt (optional)
+68 SET SDDDT=$GET(SDDDT)
+69 IF SDDDT'=""
SET %DT=""
SET X=$PIECE(SDDDT,"@",1)
DO ^%DT
SET SDDDT=Y
IF Y=-1
SET SDDDT=""
+70 IF SDDDT=""
IF SDECATID'="WALKIN"
SET SDDDT=$PIECE(SDECSTART,".",1)
+71 ;validate requested by
+72 SET SDREQBY=$$UP^XLFSTR($GET(SDREQBY))
+73 IF SDREQBY'=""
SET SDREQBY=$SELECT(SDREQBY="PROVIDER":1,SDREQBY="PATIENT":2,1:0)
+74 ;validate lab date/time (optional)
+75 SET SDLAB=$GET(SDLAB)
+76 IF SDLAB'=""
SET %DT="T"
SET X=SDLAB
DO ^%DT
SET SDLAB=Y
IF Y=-1
SET SDLAB=""
+77 ;validate EKG date/time (optional)
+78 SET SDEKG=$GET(SDEKG)
+79 IF SDEKG'=""
SET %DT="T"
SET X=SDEKG
DO ^%DT
SET SDEKG=Y
IF Y=-1
SET SDEKG=""
+80 ;validate XRAY date/time (optional)
+81 SET SDXRAY=$GET(SDXRAY)
+82 IF SDXRAY'=""
SET %DT="T"
SET X=SDXRAY
DO ^%DT
SET SDXRAY=Y
IF Y=-1
SET SDXRAY=""
+83 SET SDID=$GET(SDID)
+84 ;validate clini101
+85 SET SDCL=$GET(SDCL)
+86 IF SDCL'=""
IF '$DATA(^SC(SDCL,0))
SET SDCL=""
+87 ;clinic ID ;support for single HOSPITAL LOCATION in SDEC RESOURCE
+88 IF SDCL=""
SET SDCL=$$GET1^DIQ(409.831,SDECRESD_",",.04,"I")
+89 SET OVB=+$GET(OVB)
+90 ;alb/sat 658 check if overbook ;alb/sat 665 clear SDECAPPTID ;SD,740
IF 'OVB
SET OBM=$$OBM1^SDEC57(SDCL,SDECSTART,SDMRTC,,+SDECWKIN)
IF OBM'=""
IF +OBM'=1
SET SDECAPPTID=0
DO ERR(SDECI+1,"OBM"_OBM,DFN,1)
QUIT
+91 ;validate appt request type (required)
+92 IF SDCL=""
Begin DoDot:1
+93 if $PIECE(SDAPTYP,"|",1)="E"
SET SDCL=$$GET1^DIQ(409.3,$PIECE(SDAPTYP,"|",2)_",",13.2,"I")
+94 if $PIECE(SDAPTYP,"|",1)="R"
SET SDCL=$$GET1^DIQ(403.5,$PIECE(SDAPTYP,"|",2)_",",4.5,"I")
+95 ;ICR 4837 ICR states 'Zero node read into variable'
+96 if $PIECE(SDAPTYP,"|",1)="C"
SET SDCL=$PIECE($GET(^GMR(123,+$PIECE(SDAPTYP,"|",2),0)),U,4)
+97 if $PIECE(SDAPTYP,"|",1)="A"
SET SDCL=$$GET1^DIQ(409.85,$PIECE(SDAPTYP,"|",2)_",",8,"I")
End DoDot:1
+98 IF SDCL=""
DO ERR(SDECI+1,"SDEC07 Error: Invalid clinic ID.",DFN,1)
QUIT
+99 ;SD,740
IF $$INACTIVE^SDEC32(SDCL)
DO ERR(SDECI+1,"SDEC07 Error: "_$$GET1^DIQ(44,SDCL_",",.01)_" is an inactive clinic.",DFN,1)
QUIT
+100 ;Reject if consult is not active or pending. SD*5.3*686
+101 ;
IF $PIECE(SDAPTYP,"|",1)="C"
NEW CNSLTSTS,NOTOK
SET CNSLTSTS=$PIECE($GET(^GMR(123,+$PIECE(SDAPTYP,"|",2),0)),U,12)
SET NOTOK=0
Begin DoDot:1
+102 ;SD,740
IF CNSLTSTS'=5
IF CNSLTSTS'=6
DO ERR(SDECI+1,"Consult status is not PENDING or ACTIVE. It cannot be scheduled.",DFN,1)
SET NOTOK=1
QUIT
End DoDot:1
if NOTOK
QUIT
+103 ;
+104 ;validate provider
+105 SET DONE=0
+106 IF $PIECE(SDAPTYP,"|",1)'="R"
Begin DoDot:1
+107 IF $GET(PROVIEN)
IF '$DATA(^VA(200,+$GET(PROVIEN),0))
DO ERR(SDECI+1,"SDEC07 Error: Invalid provider ID.",DFN,1)
SET DONE=1
+108 IF $GET(PROVIEN)=""
SET PROVIEN=$$GETPROVIDER^SDESCREATEAPPT(SDCL,$PIECE(SDAPTYP,"|",1),$PIECE(SDAPTYP,"|",2))
End DoDot:1
+109 IF $PIECE(SDAPTYP,"|",1)="R"
Begin DoDot:1
+110 IF $GET(PROVIEN)
IF '$$GET1^DIQ(403.54,PROVIEN,.01,"I")
DO ERR(SDECI+1,"SDEC07 Error: Invalid recall provider ID.",DFN,1)
SET DONE=1
End DoDot:1
+111 if DONE
QUIT
+112 ;
+113 ;validate service connected
+114 SET SDSVCPR=$GET(SDSVCPR)
+115 IF SDSVCPR'=""
if (+SDSVCPR<0)!(+SDSVCPR>100)
SET SDSVCPR=""
+116 SET SDSVCP=$GET(SDSVCP)
+117 SET SDSVCP=$SELECT(SDSVCP=0:0,SDSVCP="NO":0,SDSVCP=1:1,SDSVCP="YES":1,1:"")
+118 ;validate note ;alb/sat 658 - only use 1st 150 characters
+119 SET SDECNOTE=$GET(SDECNOTE)
if SDECNOTE'=""
SET SDECNOTE=$TRANSLATE($EXTRACT(SDECNOTE,1,150),"^"," ")
+120 ;validate APPTYPE
+121 SET APPTYPE=$GET(APPTYPE)
IF APPTYPE'=""
IF '$DATA(^SD(409.1,+APPTYPE,0))
SET APPTYPE=""
+122 ;validate Patient Status (EESTAT)
+123 SET EESTAT=$GET(EESTAT)
+124 IF EESTAT=""
Begin DoDot:1
+125 IF $PIECE(SDAPTYP,"|",1)="E"
SET EESTAT=$$GET1^DIQ(409.3,$PIECE(SDAPTYP,"|",2)_",",27,"I")
+126 IF $PIECE(SDAPTYP,"|",1)="A"
SET EESTAT=$$GET1^DIQ(409.3,$PIECE(SDAPTYP,"|",2)_",",.02,"I")
End DoDot:1
+127 SET EESTAT=$SELECT(EESTAT="N":"N",EESTAT="NEW":"N",EESTAT="E":"E",EESTAT="ESTABLISHED":"E",1:"")
+128 ;validate OVB (overbook)
+129 SET OVB=+$GET(OVB)
+130 IF 'OVB
Begin DoDot:1
+131 DO OVBOOK^SDEC07A(.RET,SDCL,SDECSTART,SDECRES)
End DoDot:1
+132 Begin DoDot:1
+133 SET SDAPPTYP=+APPTYPE
+134 IF 'SDAPPTYP
Begin DoDot:2
+135 IF $PIECE(SDAPTYP,"|",1)="E"
SET SDAPPTYP=$$GET1^DIQ(409.3,+$PIECE(SDAPTYP,"|",2)_",",8.7,"I")
+136 IF $PIECE(SDAPTYP,"|",1)="A"
SET SDAPPTYP=$$GET1^DIQ(409.85,+$PIECE(SDAPTYP,"|",2)_",",8.7,"I")
+137 IF $PIECE(SDAPTYP,"|",1)="C"
IF +APPTYPE
SET SDAPPTYP=+APPTYPE
End DoDot:2
+138 if 'SDAPPTYP
SET SDAPPTYP=$ORDER(^SD(409.1,"B","REGULAR",0))
End DoDot:1
+139 SET EAS=$TRANSLATE($GET(EAS),"^"," ")
+140 IF $LENGTH(EAS)
SET EAS=$$EASVALIDATE^SDESUTIL(EAS)
+141 IF EAS=-1
DO ERR(SDECI+1,"SDEC07 Error: Invalid EAS Tracking Number",,0)
QUIT
+142 ;alb/sat 665 add SDECLEN
SET SDECAPPTID=$$SDECADD(SDECSTART,SDECEND,DFN,SDECRESD,SDECATID,SDDDT,SDID,SDAPTYP,PROVIEN,SDCL,SDECNOTE,SAVESTRT,SDECRES,SDAPPTYP,EESTAT,1,+SDECLEN,EAS)
+143 ;SD,740
IF 'SDECAPPTID
DO ERR(SDECI+1,"SDEC07 Error: Unable to add appointment to SDEC APPOINTMENT file.",DFN,2)
QUIT
+144 ;Lock SDEC node
+145 ; Fixed Lock *790
LOCK +^SDEC(409.84,SDECAPPTID):5
IF '$TEST
DO ERR(SDECI+1,"Another user is working with this patient's record. Please try again later",DFN,1)
QUIT
+146 ;Save the appt
+147 ; call chart request
+148 SET SDECDEV=""
+149 IF SDECATID="WALKIN"
IF $GET(SDECCR)
IF $GET(SDECDEV)'=""
SET DGQUIET=1
DO WISD^SDECRT(DFN,$PIECE(SDECSTART,"."),"",SDECDEV)
+150 IF SDECNOTE]""
DO SDECWP(SDECAPPTID,SDECNOTE)
+151 IF +SDCL
IF $DATA(^SC(SDCL,0))
Begin DoDot:1
+152 SET SDECC("PAT")=DFN
+153 SET SDECC("CLN")=SDCL
+154 SET SDECC("TYP")=$SELECT(+SDECWKIN:4,SDAPPTYP=1:1,1:3)
+155 SET SDECC("COL")=$SELECT(SDAPPTYP=7:1,1:"")
+156 SET SDECC("APT")=SDAPPTYP
+157 SET SDECC("ADT")=SDECSTART
+158 SET SDECC("LEN")=SDECLEN
+159 SET SDECC("OI")=$EXTRACT($GET(SDECNOTE),1,150)
+160 SET SDECC("OI")=$TRANSLATE(SDECC("OI"),";"," ")
+161 SET SDECC("OI")=$$STRIP(SDECC("OI"))
+162 SET SDECC("RES")=SDECRESD
+163 SET SDECC("USR")=DUZ
+164 SET SDECC("MTR")=$GET(SDMRTC)
+165 SET SDECC("DDT")=SDDDT
+166 SET SDECC("REQ")=SDREQBY
+167 SET SDECC("LAB")=SDLAB
+168 SET SDECC("XRA")=SDXRAY
+169 SET SDECC("EKG")=SDEKG
+170 SET SDECC("OVB")=$SELECT(OVB=1:1,1:0)
+171 SET SDECC("ELG")=SDEL
+172 if $PIECE(SDAPTYP,"|",1)="C"
SET SDECC("CON")=$PIECE(SDAPTYP,"|",2)
+173 SET SDECERR=$$MAKE^SDEC07B(.SDECC)
+174 if SDECERR
QUIT
+175 DO AVUPDT^SDEC07C(SDCL,SDECSTART,SDECLEN)
+176 QUIT
End DoDot:1
IF +SDECERR
DO ERR(SDECI+1,$PIECE(SDECERR,U,2),DFN,2)
+177 IF $PIECE(SDAPTYP,"|",1)="E"
DO EWL^SDEC07A($PIECE(SDAPTYP,"|",2),SDECSTART,SDCL,SDSVCPR,SDSVCP,,SDAPPTYP)
+178 IF $PIECE(SDAPTYP,"|",1)="A"
Begin DoDot:1
+179 ;alb/sat 658 do not pass note
DO UPDATE^SDECAR2($PIECE(SDAPTYP,"|",2),SDECSTART,SDCL,SDSVCPR,SDSVCP,,SDAPPTYP,EAS)
+180 IF $GET(SDMRTC)
IF $GET(SDPARENT)
DO AR433^SDECAR2(SDPARENT,SDECAPPTID_"~"_$PIECE(SDAPTYP,"|",2))
+181 if $GET(SDPARENT)
DO AR438^SDECAR2($PIECE(SDAPTYP,"|",2),SDPARENT)
End DoDot:1
+182 NEW SDT
SET SDT=SDECSTART
+183 IF $$NOW^XLFDT>SDT
IF $$NEW^SDPCE(SDT)
Begin DoDot:1
+184 NEW SDCOED
+185 SET SDOE=$$GETAPT^SDVSIT2(DFN,SDT,SDCL)
End DoDot:1
+186 ; Fixed Lock *790
LOCK -^SDEC(409.84,SDECAPPTID)
+187 LOCK -^DPT(DFN)
+188 SET SDECI=SDECI+1
+189 SET ^TMP("SDEC07",$JOB,SDECI)=SDECAPPTID_"^"_$GET(SDECDERR)_$CHAR(30)
+190 SET SDECI=SDECI+1
+191 SET ^TMP("SDEC07",$JOB,SDECI)=$CHAR(31)
+192 QUIT
+193 ;
+194 ;ADD SDEC APPT ENTRY
SDECADD(SDECSTART,SDECEND,DFN,SDECRESD,SDECATID,SDDDT,SDID,SDAPTYP,PROVIEN,SDCL,SDECNOTE,SAVESTRT,SDECRES,SDAPPTYP,EESTAT,SDF,SDECLEN,EAS) ;alb/sat 665 add SDECLEN
+1 NEW SDIEN,SDECAPPTID,SDECIEN,SDECMSG,SL,X,SDDFN,FDA,ERR,REQIEN
+2 SET SDECSTART=$GET(SDECSTART)
+3 ;MGH save date/time for consult request
SET SAVESTRT=$GET(SAVESTRT)
SET SDECRES=$GET(SDECRES)
+4 SET DFN=$GET(DFN)
SET SDDFN=DFN
+5 SET SDECRESD=$GET(SDECRESD)
+6 SET SDECATID=$GET(SDECATID)
+7 SET SDDDT=$GET(SDDDT)
+8 SET SDID=$GET(SDID)
+9 SET SDAPTYP=$GET(SDAPTYP)
+10 SET SDAPPTYP=$GET(SDAPPTYP)
+11 SET PROVIEN=$GET(PROVIEN)
+12 SET SDCL=$GET(SDCL)
+13 SET SDECEND=$GET(SDECEND)
+14 IF $PIECE(SDAPTYP,"|",1)="R"
Begin DoDot:1
+15 SET PROVIEN=$$GET1^DIQ(403.54,PROVIEN,.01,"I")
End DoDot:1
+16 SET SDECLEN=$GET(SDECLEN)
+17 ;no length or end date/time
IF SDECLEN=""
IF SDECEND=""
SET SDECLEN=+$GET(^SC(SDCL,"SL"))
if '+SDECLEN
SET SDECLEN=30
SET SDECEND=$$FMADD^XLFDT(SDECSTART,,,+SDECLEN)
+18 ;no length
IF SDECLEN=""
IF SDECEND'=""
SET SDECLEN=$$FMDIFF^XLFDT(SDECEND,SDECSTART,2)\60
+19 ;no end date/time
IF SDECLEN'=""
IF SDECEND=""
SET SDECEND=$$FMADD^XLFDT(SDECSTART,,,+SDECLEN)
+20 SET SDECNOTE=$GET(SDECNOTE)
+21 SET SDF=$GET(SDF,0)
+22 SET EAS=$GET(EAS)
+23 IF PROVIEN=""
SET PROVIEN=$$GET1^DIQ(44,SDCL_",",16,"I")
+24 SET SDIEN=$$APPTGET^SDECUTL(DFN,SDECSTART,SDCL)
+25 SET SDIEN=$SELECT(SDIEN'="":SDIEN_",",1:"+1,")
KILL FDA
+26 SET FDA(409.84,SDIEN,.01)=SDECSTART
+27 SET FDA(409.84,SDIEN,.02)=SDECEND
+28 SET FDA(409.84,SDIEN,.03)="@"
+29 SET FDA(409.84,SDIEN,.04)="@"
+30 SET FDA(409.84,SDIEN,.05)=DFN
+31 SET FDA(409.84,SDIEN,.06)=$SELECT(+SDAPPTYP:SDAPPTYP,1:"@")
+32 SET FDA(409.84,SDIEN,.07)=SDECRESD
+33 SET FDA(409.84,SDIEN,.08)=$GET(DUZ)
+34 SET FDA(409.84,SDIEN,.09)=$$NOW^XLFDT()
+35 SET FDA(409.84,SDIEN,.1)="@"
+36 SET FDA(409.84,SDIEN,.101)="@"
+37 SET FDA(409.84,SDIEN,.102)="@"
+38 SET FDA(409.84,SDIEN,.11)="@"
+39 SET FDA(409.84,SDIEN,.12)="@"
+40 SET FDA(409.84,SDIEN,.121)="@"
+41 SET FDA(409.84,SDIEN,.122)="@"
+42 SET FDA(409.84,SDIEN,.13)=$SELECT(SDECATID="WALKIN":"y",1:"@")
+43 SET FDA(409.84,SDIEN,.14)="@"
+44 SET FDA(409.84,SDIEN,.16)=$SELECT(PROVIEN'="":PROVIEN,1:"@")
+45 SET FDA(409.84,SDIEN,.17)="@"
+46 SET FDA(409.84,SDIEN,.18)=$SELECT($GET(SDECLEN)'="":SDECLEN,1:"@")
+47 SET FDA(409.84,SDIEN,.2)=SDDDT
+48 SET FDA(409.84,SDIEN,.21)=$SELECT($GET(SDID)'="":SDID,1:"@")
+49 SET FDA(409.84,SDIEN,.22)=$SELECT(SDAPTYP'="":$PIECE(SDAPTYP,"|",2)_";"_$SELECT($PIECE(SDAPTYP,"|",1)="E":"SDWL(409.3,",$PIECE(SDAPTYP,"|",1)="C":"GMR(123,",$PIECE(SDAPTYP,"|",1)="R":"SD(403.5,",$PIECE(SDAPTYP,"|",1)="A":"SDEC(409.85,",1:""),1:
"@")
+50 SET FDA(409.84,SDIEN,.23)=$SELECT($GET(EESTAT)'="":EESTAT,1:"@")
+51 if EAS'=""
SET FDA(409.84,SDIEN,100)=EAS
+52 KILL SDECIEN,SDECMSG
+53 DO UPDATE^DIE("","FDA","SDECIEN","SDECMSG")
+54 IF $PIECE(SDAPTYP,"|",1)="A"!($PIECE(SDAPTYP,"|",1)="R")
Begin DoDot:1
+55 SET REQIEN=$PIECE(SDAPTYP,"|",2)
+56 IF $$LASTPIDCHECK^SDECAR2(REQIEN,SDDDT)
Begin DoDot:2
+57 KILL FDA
SET FDA(409.854,"+1,"_REQIEN_",",.01)=$$NOW^XLFDT
+58 SET FDA(409.854,"+1,"_REQIEN_",",1)=SDDDT
+59 SET FDA(409.854,"+1,"_REQIEN_",",2)=$$GET1^DIQ(200,DUZ,.01,"E")
+60 DO UPDATE^DIE(,"FDA",,"ERR")
KILL FDA
End DoDot:2
End DoDot:1
+61 SET SDECAPPTID=$SELECT(SDIEN'="+1,":+SDIEN,1:+$GET(SDECIEN(1)))
+62 KILL SDECMSG
+63 IF SDECNOTE=""
DO WP^DIE(409.84,$SELECT(+$GET(SDECAPPTID):SDECAPPTID_",",1:SDIEN_","),1,"","@","SDECMSG")
+64 IF SDECNOTE'=""
NEW ARR
DO WP^SDECUTL(.ARR,SDECNOTE)
DO WP^DIE(409.84,$SELECT(+$GET(SDECAPPTID):SDECAPPTID_",",1:SDIEN_","),1,"","ARR","SDECMSG")
+65 IF SDECAPPTID'=""
Begin DoDot:1
+66 IF $PIECE(SDAPTYP,"|",1)="C"
IF SDF
Begin DoDot:2
+67 DO CONSULTPID^SDEC07PID(SDAPTYP,DFN,PROVIEN,SAVESTRT,SDDDT,SDDFN,SDECNOTE,SDECRES)
End DoDot:2
+68 ;
+69 ; VSE-863 ;6/9/2021
IF $PIECE(SDAPTYP,"|",1)="R"
Begin DoDot:2
+70 NEW SDCOMM,SDRET,SDRIEN1,SDRRFTR
+71 SET SDRIEN1=$PIECE(SDAPTYP,"|",2)
+72 SET SDCOMM=$$GET1^DIQ(403.5,SDRIEN1,2.5)
+73 ; "7"
SET SDRRFTR="APPT SCHEDULED"
+74 DO RECDSET^SDEC52A(.SDRET,SDRIEN1,SDRRFTR,SDCOMM)
End DoDot:2
+75 ;
End DoDot:1
+76 IF $GET(SDECAPPTID)
DO SETMISSIONELIG^SDESMISSIONELG(SDECAPPTID)
+77 QUIT SDECAPPTID
+78 ;
CONSPIDCHECK(SDRIEN1,SDDDT) ;
+1 NEW CHIEN,CHSIEN,OLDPID
+2 SET CHIEN=$ORDER(^SDEC(409.87,"B",SDRIEN1,0))
+3 SET CHSIEN=$ORDER(^SDEC(409.87,CHIEN,1,9999999),-1)
+4 SET OLDPID=$$GET1^DIQ(409.871,CHSIEN_","_CHIEN_",",1,"I")
+5 IF OLDPID'=$GET(SDDDT)
QUIT 1
+6 QUIT 0
SDECWP(SDECAPPTID,SDECNOTE) ;
+1 ;Add WP field
+2 IF SDECNOTE]""
SET SDECNOTE(.5)=SDECNOTE
SET SDECNOTE=""
+3 IF $DATA(SDECNOTE(0))
SET SDECNOTE(.5)=SDECNOTE(0)
KILL SDECNOTE(0)
+4 IF $DATA(SDECNOTE(.5))
Begin DoDot:1
+5 DO WP^DIE(409.84,SDECAPPTID_",",1,"","SDECNOTE","SDECMSG")
End DoDot:1
+6 QUIT
+7 ;
ADDEVT(DFN,SDECSTART,SDECSC,SDCLA) ;EP
+1 ;Called by SDEC ADD APPOINTMENT protocol
+2 ;SDECSC=IEN of clinic in ^SC
+3 ;SDCLA=IEN for ^SC(SDECSC,"S",SDECSTART,1,SDCLA). Use to get Length & Note
+4 ;
+5 NEW SDECNOD,SDECLEN,SDECAPPTID,SDECNODP,SDECWKIN,SDECRES
+6 if +$GET(SDECNOEV)
QUIT
+7 IF $DATA(^SDEC(409.831,"ALOC",SDECSC))
SET SDECRES=$ORDER(^SDEC(409.831,"ALOC",SDECSC,0))
+8 if '+$GET(SDECRES)
QUIT
+9 SET SDECNOD=$GET(^SC(SDECSC,"S",SDECSTART,1,SDCLA,0))
+10 if SDECNOD=""
QUIT
+11 SET SDECNODP=$GET(^DPT(DFN,"S",SDECSTART,0))
+12 SET SDECWKIN=""
+13 ;Purpose of Visit field of DPT Appointment subfile
+14 if $PIECE(SDECNODP,U,7)=4
SET SDECWKIN="WALKIN"
+15 SET SDECLEN=$PIECE(SDECNOD,U,2)
+16 if '+SDECLEN
QUIT
+17 SET SDECEND=$$FMADD^XLFDT(SDECSTART,0,0,SDECLEN,0)
+18 ;alb/sat 665 add SDECLEN
SET SDECAPPTID=$$SDECADD(SDECSTART,SDECEND,DFN,SDECRES,SDECWKIN,,,,,SDECSC,,,,,,1,+SDECLEN)
+19 if '+SDECAPPTID
QUIT
+20 SET SDECNOTE=$PIECE(SDECNOD,U,4)
+21 IF SDECNOTE]""
DO SDECWP(SDECAPPTID,SDECNOTE)
+22 DO ADDEVT3(SDECRES)
+23 QUIT
+24 ;
ADDEVT3(SDECRES) ;
+1 ;Call RaiseEvent to notify GUI clients
+2 QUIT
+3 ;
STRIP(SDECZ) ;Replace control characters with spaces
+1 NEW SDECI
+2 FOR SDECI=1:1:$LENGTH(SDECZ)
IF (32>$ASCII($EXTRACT(SDECZ,SDECI)))
SET SDECZ=$EXTRACT(SDECZ,1,SDECI-1)_" "_$EXTRACT(SDECZ,SDECI+1,999)
+3 QUIT SDECZ
+4 ;
ERR(SDECI,SDECERR,DFN,LOCK) ;Error processing SD,740
+1 SET DFN=$GET(DFN)
+2 SET SDECI=SDECI+1
+3 SET SDECERR=$TRANSLATE(SDECERR,"^","~")
+4 SET ^TMP("SDEC07",$JOB,SDECI)=$GET(SDECAPPTID,0)_"^"_SDECERR_$CHAR(30)
+5 SET SDECI=SDECI+1
+6 SET ^TMP("SDEC07",$JOB,SDECI)=$CHAR(31)
+7 IF $GET(LOCK)=1
LOCK -^DPT(DFN)
+8 IF $GET(LOCK)=2
LOCK -^DPT(DFN)
+9 IF +$GET(SDECAPPTID)
LOCK -^SDEC(409.84,SDECAPPTID)
+10 QUIT
+11 ;
ETRAP ;EP Error trap entry
+1 DO ^%ZTER
+2 IF '$DATA(SDECI)
NEW SDECI
SET SDECI=999999
+3 SET SDECI=SDECI+1
+4 DO ERR(SDECI,"SDEC07 Error")
+5 QUIT
ERROR ;
+1 DO ERR1("Error")
+2 QUIT
+3 ;
ERR1(SDECERR) ;Error processing
+1 SET SDECI=SDECI+1
+2 SET ^TMP("SDEC07",$JOB,SDECI)=SDECERR_$CHAR(30)
+3 SET SDECI=SDECI+1
+4 SET ^TMP("SDEC07",$JOB,SDECI)=$CHAR(31)
+5 QUIT