SDES846PRE ;ALB/BWF - SD*5.3*846 Post Init Routine ; June 15, 2023
;;5.3;SCHEDULING;**846**;AUG 13, 1993;Build 12
;;Per VHA Directive 6402, this routine should not be modified
;
Q
EN ; Scheduling Consult Clean-up
D TASK
Q
;
TASK ;
D MES^XPDUTL("")
D MES^XPDUTL(" SD*5.3*846 Pre-Install to log orphaned encounter data")
D MES^XPDUTL(" for appointments that were cancelled by VAOS.")
D MES^XPDUTL("")
N ZTDESC,ZTRTN,ZTIO,ZTSK,X,ZTDTH,ZTSAVE
S ZTDESC="SD*5.3*846 Pre-Install Routine"
D NOW^%DTC S ZTDTH=X,ZTIO="",ZTRTN="ENCREP^SDES846PRE",ZTSAVE("*")="" D ^%ZTLOAD
I $D(ZTSK) D
. D MES^XPDUTL(" >>>Task "_ZTSK_" has been queued.")
. D MES^XPDUTL("")
I '$D(ZTSK) D
. D MES^XPDUTL(" UNABLE TO QUEUE THIS JOB.")
. D MES^XPDUTL(" Please contact the National Help Desk to report this issue.")
Q
;
ENCREP ;
N DFN,APPTDTTM,ENCOUNTER,CLINIC,IEN44,ENCFOUND,TOTCNT,CANRES,ENCLINKED,APPTIEN,RESOURCE,ENCLINKED
N CANREASON,APPTCLIN,ENCCLIN,TOTAPPTS,CANBY,CANDTTM,ORPHANENC,APPTENC,APPTCAN
K ^XTMP("SDES846PRE")
S ^XTMP("SDES846PRE",0)=$$FMADD^XLFDT(DT,30)_"^"_DT_"^SD*5.3*846 Pre-Install Orphaned Encounter Data report"
S (TOTCNT,TOTAPPTS)=0
S CANDTTM=3230509.99
F S CANDTTM=$O(^SDEC(409.84,"AD",CANDTTM)) Q:'CANDTTM D
.S APPTIEN=0 F S APPTIEN=$O(^SDEC(409.84,"AD",CANDTTM,APPTIEN)) Q:'APPTIEN D
..S APPTDTTM=$$GET1^DIQ(409.84,APPTIEN,.01,"I")
..S DFN=$$GET1^DIQ(409.84,APPTIEN,.05,"I")
..S CANBY=$$GET1^DIQ(409.84,APPTIEN,.121,"E")
..; only process encounters for appointments that have been CANCELLED by SDESOITEAS,SRV
..Q:CANBY'="SDESOITEAS,SRV"
..S RESOURCE=$$GET1^DIQ(409.84,APPTIEN,.07,"I") Q:'RESOURCE
..S CLINIC=$$GET1^DIQ(409.831,RESOURCE,.04,"I") Q:'CLINIC
..S TOTAPPTS=TOTAPPTS+1
..S (ENCLINKED,ENCFOUND)=0
..S ENCOUNTER=0 F S ENCOUNTER=$O(^SCE("C",DFN,ENCOUNTER)) Q:'ENCOUNTER!(ENCFOUND) D
...; must match date/time
...I $$GET1^DIQ(409.68,ENCOUNTER,.01,"I")'=APPTDTTM Q
...; encounter clinic must match the appointment clinic
...S ENCCLIN=$$GET1^DIQ(409.68,ENCOUNTER,.04,"I")
...I CLINIC'=ENCCLIN Q
...S APPTENC=$$GET1^DIQ(2.98,APPTDTTM_","_DFN_",",21,"I")
...S APPTCAN=$$GET1^DIQ(2.98,APPTDTTM_","_DFN_",",15,"I")
...; if the encounter is still on the appointment and this is not the correct encounter, quit
...I APPTENC'="",APPTENC'=ENCOUNTER Q
...; if the patient appointment is linked to the encounter and the appointment is not cancelled, quit
...I APPTENC'="",APPTENC=ENCOUNTER,APPTCAN="" Q
...; if there is an encounter on the appointment, it is not this encounter and the appointment is cancelled, set ENCLINKED/ENCFOUND and quit
...I APPTENC'="",APPTENC=ENCOUNTER,APPTCAN'="" S ENCFOUND=ENCOUNTER,ENCLINKED=1 Q
...; this means if the appointment is linked to the encounter and the appointment IS cancelled, we want to close this encounter
...S ENCFOUND=ENCOUNTER
..; if there is no encounter found for this cancelled or no-show appointment, quit
..Q:'ENCFOUND
..S TOTCNT=TOTCNT+1
..; get the appointment from file 44, if it cannot be found log it.
..S IEN44=$$SCIEN(DFN,CLINIC,APPTDTTM)
..I 'IEN44 D Q
...S ^XTMP("SDES846PRE",TOTCNT)=APPTDTTM_U_APPTIEN_U_DFN_U_ENCFOUND_U_CANBY_U_"0;Could not locate clinic appointment in the HOSPITAL LOCATION file (#44)."
...M ^XTMP("SDES846PRE",TOTCNT)=^SCE(ENCFOUND)
..; if checked-in log it as such
..I $$CI^SDECU2(DFN,CLINIC,APPTDTTM,IEN44) D Q
...S ^XTMP("SDES846PRE",TOTCNT)=APPTDTTM_U_APPTIEN_U_DFN_U_ENCFOUND_U_CANBY_U_"0;Appointment checked in."
...M ^XTMP("SDES846PRE",TOTCNT)=^SCE(ENCFOUND)
..S ^XTMP("SDES846PRE",TOTCNT)=APPTDTTM_U_APPTIEN_U_DFN_U_ENCFOUND_U_CANBY_U_1
..M ^XTMP("SDES846PRE",TOTCNT)=^SCE(ENCFOUND)
S ORPHANENC=$G(TOTCNT)
S TOTCNT=TOTCNT+1
S $P(^XTMP("SDES846PRE",TOTCNT),"-",80)=""
S TOTCNT=TOTCNT+1
S ^XTMP("SDES846PRE",TOTCNT)="TOTAL ORPHANED ENCOUNTERS: "_ORPHANENC
S TOTCNT=TOTCNT+1
S ^XTMP("SDES846PRE",TOTCNT)="TOTAL APPOINTMENTS SEARCHED: "_TOTAPPTS
Q
SCIEN(PAT,CLINIC,DATE) ;returns ien for appt in ^SC
N X,IEN
S X=0 F S X=$O(^SC(CLINIC,"S",DATE,1,X)) Q:'X Q:$G(IEN) D
.; only look at cancelled appts
.Q:$P($G(^SC(CLINIC,"S",DATE,1,X,0)),U,9)'="C"
.I +$G(^SC(CLINIC,"S",DATE,1,X,0))=PAT S IEN=X
Q $G(IEN)
--- Routine Detail --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HSDES846PRE 4273 printed Dec 13, 2024@02:55:10 Page 2
SDES846PRE ;ALB/BWF - SD*5.3*846 Post Init Routine ; June 15, 2023
+1 ;;5.3;SCHEDULING;**846**;AUG 13, 1993;Build 12
+2 ;;Per VHA Directive 6402, this routine should not be modified
+3 ;
+4 QUIT
EN ; Scheduling Consult Clean-up
+1 DO TASK
+2 QUIT
+3 ;
TASK ;
+1 DO MES^XPDUTL("")
+2 DO MES^XPDUTL(" SD*5.3*846 Pre-Install to log orphaned encounter data")
+3 DO MES^XPDUTL(" for appointments that were cancelled by VAOS.")
+4 DO MES^XPDUTL("")
+5 NEW ZTDESC,ZTRTN,ZTIO,ZTSK,X,ZTDTH,ZTSAVE
+6 SET ZTDESC="SD*5.3*846 Pre-Install Routine"
+7 DO NOW^%DTC
SET ZTDTH=X
SET ZTIO=""
SET ZTRTN="ENCREP^SDES846PRE"
SET ZTSAVE("*")=""
DO ^%ZTLOAD
+8 IF $DATA(ZTSK)
Begin DoDot:1
+9 DO MES^XPDUTL(" >>>Task "_ZTSK_" has been queued.")
+10 DO MES^XPDUTL("")
End DoDot:1
+11 IF '$DATA(ZTSK)
Begin DoDot:1
+12 DO MES^XPDUTL(" UNABLE TO QUEUE THIS JOB.")
+13 DO MES^XPDUTL(" Please contact the National Help Desk to report this issue.")
End DoDot:1
+14 QUIT
+15 ;
ENCREP ;
+1 NEW DFN,APPTDTTM,ENCOUNTER,CLINIC,IEN44,ENCFOUND,TOTCNT,CANRES,ENCLINKED,APPTIEN,RESOURCE,ENCLINKED
+2 NEW CANREASON,APPTCLIN,ENCCLIN,TOTAPPTS,CANBY,CANDTTM,ORPHANENC,APPTENC,APPTCAN
+3 KILL ^XTMP("SDES846PRE")
+4 SET ^XTMP("SDES846PRE",0)=$$FMADD^XLFDT(DT,30)_"^"_DT_"^SD*5.3*846 Pre-Install Orphaned Encounter Data report"
+5 SET (TOTCNT,TOTAPPTS)=0
+6 SET CANDTTM=3230509.99
+7 FOR
SET CANDTTM=$ORDER(^SDEC(409.84,"AD",CANDTTM))
if 'CANDTTM
QUIT
Begin DoDot:1
+8 SET APPTIEN=0
FOR
SET APPTIEN=$ORDER(^SDEC(409.84,"AD",CANDTTM,APPTIEN))
if 'APPTIEN
QUIT
Begin DoDot:2
+9 SET APPTDTTM=$$GET1^DIQ(409.84,APPTIEN,.01,"I")
+10 SET DFN=$$GET1^DIQ(409.84,APPTIEN,.05,"I")
+11 SET CANBY=$$GET1^DIQ(409.84,APPTIEN,.121,"E")
+12 ; only process encounters for appointments that have been CANCELLED by SDESOITEAS,SRV
+13 if CANBY'="SDESOITEAS,SRV"
QUIT
+14 SET RESOURCE=$$GET1^DIQ(409.84,APPTIEN,.07,"I")
if 'RESOURCE
QUIT
+15 SET CLINIC=$$GET1^DIQ(409.831,RESOURCE,.04,"I")
if 'CLINIC
QUIT
+16 SET TOTAPPTS=TOTAPPTS+1
+17 SET (ENCLINKED,ENCFOUND)=0
+18 SET ENCOUNTER=0
FOR
SET ENCOUNTER=$ORDER(^SCE("C",DFN,ENCOUNTER))
if 'ENCOUNTER!(ENCFOUND)
QUIT
Begin DoDot:3
+19 ; must match date/time
+20 IF $$GET1^DIQ(409.68,ENCOUNTER,.01,"I")'=APPTDTTM
QUIT
+21 ; encounter clinic must match the appointment clinic
+22 SET ENCCLIN=$$GET1^DIQ(409.68,ENCOUNTER,.04,"I")
+23 IF CLINIC'=ENCCLIN
QUIT
+24 SET APPTENC=$$GET1^DIQ(2.98,APPTDTTM_","_DFN_",",21,"I")
+25 SET APPTCAN=$$GET1^DIQ(2.98,APPTDTTM_","_DFN_",",15,"I")
+26 ; if the encounter is still on the appointment and this is not the correct encounter, quit
+27 IF APPTENC'=""
IF APPTENC'=ENCOUNTER
QUIT
+28 ; if the patient appointment is linked to the encounter and the appointment is not cancelled, quit
+29 IF APPTENC'=""
IF APPTENC=ENCOUNTER
IF APPTCAN=""
QUIT
+30 ; if there is an encounter on the appointment, it is not this encounter and the appointment is cancelled, set ENCLINKED/ENCFOUND and quit
+31 IF APPTENC'=""
IF APPTENC=ENCOUNTER
IF APPTCAN'=""
SET ENCFOUND=ENCOUNTER
SET ENCLINKED=1
QUIT
+32 ; this means if the appointment is linked to the encounter and the appointment IS cancelled, we want to close this encounter
+33 SET ENCFOUND=ENCOUNTER
End DoDot:3
+34 ; if there is no encounter found for this cancelled or no-show appointment, quit
+35 if 'ENCFOUND
QUIT
+36 SET TOTCNT=TOTCNT+1
+37 ; get the appointment from file 44, if it cannot be found log it.
+38 SET IEN44=$$SCIEN(DFN,CLINIC,APPTDTTM)
+39 IF 'IEN44
Begin DoDot:3
+40 SET ^XTMP("SDES846PRE",TOTCNT)=APPTDTTM_U_APPTIEN_U_DFN_U_ENCFOUND_U_CANBY_U_"0;Could not locate clinic appointment in the HOSPITAL LOCATION file (#44)."
+41 MERGE ^XTMP("SDES846PRE",TOTCNT)=^SCE(ENCFOUND)
End DoDot:3
QUIT
+42 ; if checked-in log it as such
+43 IF $$CI^SDECU2(DFN,CLINIC,APPTDTTM,IEN44)
Begin DoDot:3
+44 SET ^XTMP("SDES846PRE",TOTCNT)=APPTDTTM_U_APPTIEN_U_DFN_U_ENCFOUND_U_CANBY_U_"0;Appointment checked in."
+45 MERGE ^XTMP("SDES846PRE",TOTCNT)=^SCE(ENCFOUND)
End DoDot:3
QUIT
+46 SET ^XTMP("SDES846PRE",TOTCNT)=APPTDTTM_U_APPTIEN_U_DFN_U_ENCFOUND_U_CANBY_U_1
+47 MERGE ^XTMP("SDES846PRE",TOTCNT)=^SCE(ENCFOUND)
End DoDot:2
End DoDot:1
+48 SET ORPHANENC=$GET(TOTCNT)
+49 SET TOTCNT=TOTCNT+1
+50 SET $PIECE(^XTMP("SDES846PRE",TOTCNT),"-",80)=""
+51 SET TOTCNT=TOTCNT+1
+52 SET ^XTMP("SDES846PRE",TOTCNT)="TOTAL ORPHANED ENCOUNTERS: "_ORPHANENC
+53 SET TOTCNT=TOTCNT+1
+54 SET ^XTMP("SDES846PRE",TOTCNT)="TOTAL APPOINTMENTS SEARCHED: "_TOTAPPTS
+55 QUIT
SCIEN(PAT,CLINIC,DATE) ;returns ien for appt in ^SC
+1 NEW X,IEN
+2 SET X=0
FOR
SET X=$ORDER(^SC(CLINIC,"S",DATE,1,X))
if 'X
QUIT
if $GET(IEN)
QUIT
Begin DoDot:1
+3 ; only look at cancelled appts
+4 if $PIECE($GET(^SC(CLINIC,"S",DATE,1,X,0)),U,9)'="C"
QUIT
+5 IF +$GET(^SC(CLINIC,"S",DATE,1,X,0))=PAT
SET IEN=X
End DoDot:1
+6 QUIT $GET(IEN)