PXUTLVST ;ISL/DEE,PKR - Looks up the visit to see if there is already one ;12/01/2024
;;1.0;PCE PATIENT CARE ENCOUNTER;**238,248**;Aug 12, 1996;Build 6
;
Q
;
FINDVISIT(DFN,VDT,HLOC,SVC,DSS,INS,TYPE,ENCTYPE,CSVC,VISITLIST) ;Use the "AA" index
;to search for existing visits. Requires Patient (DFN) and Visit Date and Time (VDT). Hospital Location
;is required unless SVC="E". Institution (INS), Type, Encounter Type (ENCTYPE), DSS ID (DSS), and
;Service Category (SVC) are optional. If they are passed as "", then they are not
;included in the visit matching.
;
;Credit Stop visits are not returned as a match because they are created and managed by
;Scheduling.
;
;DATA2PCE can change the input value of Service Category depending on if the patient was an
;inpatient or outpatient on the date of the encounter. It can also change the Service Category
;to "T" if the name of the Clinic Stop contains "TELE".
;If the Stop Code is in file #150.1, the Service Category will be changed to X or D, see SVC^PXKCO.
;It can also be changed to A when EN^PXKCO is called from the protocol event SDAM APPOINTMENT EVENTS.
;This event is fired as part of SDAM PCE EVENT (D EN^SDPCE) which in turn is run directly from
;EVENT^PXKMASC.
;If there is a possibility the input value of SVC may not be correct, pass CSVC=1 to bypass Service
;Category matching except when SVC="E" is passed.
;
S VDT=$$VFMDATE^PXDATE(VDT,"ST")
I (+DFN'>0)!('VDT) S VISITLIST(0)=-1 Q
I (SVC'="E"),(+HLOC'>0) S VISITLIST(0)=-1 Q
I (+HLOC>0),('$D(^SC(HLOC))) S VISITLIST(0)=-1 Q
;
N AMCLIST,APPTVST,COSVC,DATE,DECLIST,DSST,DSSTP,ENCTYP,HOSPLOC,INP,INVDT,IPM,NFOUND
N SERVCAT,STATUS,SVCMATCH,TEMP,TESTSVC,TIME,VISITIEN,VISITS,VLIST,VSIT
;
;Get the visit for the appointment if there is one.
S APPTVST=$S(SVC="E":0,1:+$$APPT2VST^PXUTL1(DFN,VDT,HLOC))
I APPTVST>0 S VISITLIST(0)=1,VISITLIST(1)=APPTVST,VISITLIST(1,"A")="" Q
;
S VSIT("VDT")=VDT
S VSIT("PAT")=DFN
S VSIT("LOC")=HLOC
S VSIT("INS")=$G(INS)
S VSIT("DSS")=$G(DSS)
S VSIT("TYPE")=$G(TYPE)
S VSIT("ENCTYPE")=$G(ENCTYPE)
S VSIT("SVC")=$G(SVC)
;
S (NFOUND,VISITLIST(0))=0
;Build a list of visits based on matching VDT, DFN, and HLOC.
S DATE=$P(VDT,".",1),TIME=$P(VDT,".",2)
S INVDT=(9999999-DATE)
I TIME'="" S INVDT=INVDT_"."_TIME
S VISITIEN=0
F S VISITIEN=+$O(^AUPNVSIT("AA",DFN,INVDT,VISITIEN)) Q:VISITIEN=0 D
. I $D(VISITS(VISITIEN)) Q
. S ENCTYP=$P($G(^AUPNVSIT(VISITIEN,150)),U,3)
.;Ignore Credit Stop encounters.
. I ENCTYP="C" Q
. S TEMP=^AUPNVSIT(VISITIEN,0)
. S SERVCAT=$P(TEMP,U,7)
.;Service Category is required, if it is NULL, it is a corrupted entry.
. I SERVCAT="" Q
.;If SVC=E, match Service Category, otherwise ignore it.
. I (VSIT("SVC")="E"),(SERVCAT'="E") Q
. I (VSIT("SVC")'="E"),(SERVCAT="E") Q
. S HOSPLOC=$P(TEMP,U,22)
. I (VSIT("LOC")'=""),(VSIT("LOC")'=HOSPLOC) Q
. I HOSPLOC="" S HOSPLOC=0
. S VISITS(VISITIEN)=""
. S NFOUND=NFOUND+1,VISITLIST(NFOUND)=VISITIEN
S VISITLIST(0)=NFOUND
;If there are no matches or an exact match the search is done.
I NFOUND<2 Q
;
;When there are multiple matches, use additional criteria
;to try to find a unique match.
;
;Allow for service category being changed depending on the patient's
;status (inpatient/outpatient) and the stop code.
S TESTSVC=""
I ($G(CSVC)=1),(VSIT("SVC")'="E") D
.;Use this special check for inpatient status.
. S IPM=$$IP^VSITCK1(VDT,DFN)
. S INP=$S(IPM>0:1,1:0)
. I +VSIT("DSS")>0 S DSSTP=VSIT("DSS")
. I (VSIT("DSS")=""),(+VSIT("LOC")>0) S DSSTP=+$P(^SC(VSIT("LOC"),0),U,7)
. I DSSTP>0 S TESTSVC=$$SVC^PXKCO(SVC,DSSTP,INP,VSIT("LOC"))
;
S NFOUND=0,VISITIEN=""
F S VISITIEN=$O(VISITS(VISITIEN)) Q:VISITIEN="" D
.;Allow for checked out encounters having their service category changed.
.;If the hospital location's stop code contains "TELE" the service category will always be T.
.;It is not changed on check out.
. S COSVC=""
. S STATUS=$S(VSIT("SVC")="E":0,TESTSVC="T":0,1:+$$STATUS^SDPCE(VISITIEN))
. I STATUS=2 D
.. I INP=0 S COSVC=$S(VSIT("SVC")="X":"X",1:"A")
.. I INP=1 S COSVC=$S(VSIT("SVC")="X":"D",1:"I")
.;
. S TEMP=$G(^AUPNVSIT(VISITIEN,0))
. S SVCMATCH=0
. I $G(CSVC)=1 D
.. I (VSIT("SVC")'=""),(VSIT("SVC")=$P(TEMP,U,7)) S SVCMATCH=1
.. I (SVCMATCH=0),(TESTSVC'=""),(TESTSVC=$P(TEMP,U,7)) S SVCMATCH=1
.. I (SVCMATCH=0),(COSVC'=""),(COSVC=$P(TEMP,U,7)) S SVCMATCH=1
. I SVCMATCH=0 Q
.;
. I (VSIT("TYPE")'=""),(VSIT("TYPE")'=$P(TEMP,U,3)) Q
. I (VSIT("DSS")'=""),(VSIT("DSS")'=$P(TEMP,U,8)) Q
. I (VSIT("INS")'=""),(VSIT("INS")'=$P(TEMP,U,6)) Q
. I VSIT("ENCTYPE")'="" D Q:VSIT("ENCTYPE")'=ENCTYP
.. S ENCTYP=($P($G(^AUPNVSIT(VISITIEN,150)),U,3))
. S NFOUND=NFOUND+1,AMCLIST(NFOUND)=VISITIEN
S AMCLIST(0)=NFOUND
;If there is an exact match the search is done.
I AMCLIST(0)=1 D Q
. K VISITLIST
. S VISITLIST(0)=AMCLIST(0),VISITLIST(1)=AMCLIST(1)
;
I (AMCLIST(0)>0),(AMCLIST(0)<VISITLIST(0)) F IND=0:1:AMCLIST(0) S DECLIST(IND)=AMCLIST(IND)
E F IND=0:1:VISITLIST(0) S DECLIST(IND)=VISITLIST(IND)
;
;Process multiple matches.
N DECOUNT,KEEPIEN,KEEPIND,NREFS,NREFSD,NVISITS,TEMPLIST
F IND=1:1:DECLIST(0) D
. S VISITIEN=DECLIST(IND)
. S DECOUNT=+$P(^AUPNVSIT(VISITIEN,0),U,9)
. S NREFSD(DECOUNT,IND)=VISITIEN
;Order the list based on Dependent Entry Count; largest is first. Remove
;visits with no references.
K VISITLIST
S NREFS="",NVISITS=0
F S NREFS=+$O(NREFSD(NREFS),-1) Q:NREFS=0 D
. S IND=""
. F S IND=$O(NREFSD(NREFS,IND)) Q:IND="" D
.. S NVISITS=NVISITS+1,VISITLIST(NVISITS)=DECLIST(IND)
I NVISITS>0 S VISITLIST(0)=NVISITS Q
;
;If no visits have references return the first one.
S KEEPIND=$O(NREFSD(0,""))
S KEEPIEN=DECLIST(KEEPIND)
K VISITLIST
S VISITLIST(0)=1,VISITLIST(1)=KEEPIEN
Q
;
;=========
LOOKVSIT(PAT,VDT,LOC,DSS,INS,TYP) ;Calls visit tracking to see if there is
;already a visit.
;Get the visit for the appointment is there is one
N APPTVST
S APPTVST=$$APPT2VST^PXUTL1(PAT,VDT,LOC)
I APPTVST>0 Q APPTVST
;
N VSIT,VSITPKG
S VSIT("IEN")=""
S VSIT("VDT")=VDT
S VSIT("PAT")=PAT
S VSIT("LOC")=LOC
S VSIT("INS")=$G(INS)
S VSIT("TYP")=$G(TYP)
S VSIT("DSS")=$G(DSS)
I VSIT("DSS")="",$P($G(^SC(+VSIT("LOC"),0)),"^",7)>0 S VSIT("DSS")=$P(^SC(+VSIT("LOC"),0),"^",7)
S VSITPKG="PX"
S VSIT(0)="D0EM"
;
;CALL FOR VSIT
D ^VSIT
I '$D(VSIT("IEN"))#2 Q -1
Q $P(VSIT("IEN"),"^",1)
;
--- Routine Detail --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HPXUTLVST 6534 printed Aug 26, 2025@22:47:25 Page 2
PXUTLVST ;ISL/DEE,PKR - Looks up the visit to see if there is already one ;12/01/2024
+1 ;;1.0;PCE PATIENT CARE ENCOUNTER;**238,248**;Aug 12, 1996;Build 6
+2 ;
+3 QUIT
+4 ;
FINDVISIT(DFN,VDT,HLOC,SVC,DSS,INS,TYPE,ENCTYPE,CSVC,VISITLIST) ;Use the "AA" index
+1 ;to search for existing visits. Requires Patient (DFN) and Visit Date and Time (VDT). Hospital Location
+2 ;is required unless SVC="E". Institution (INS), Type, Encounter Type (ENCTYPE), DSS ID (DSS), and
+3 ;Service Category (SVC) are optional. If they are passed as "", then they are not
+4 ;included in the visit matching.
+5 ;
+6 ;Credit Stop visits are not returned as a match because they are created and managed by
+7 ;Scheduling.
+8 ;
+9 ;DATA2PCE can change the input value of Service Category depending on if the patient was an
+10 ;inpatient or outpatient on the date of the encounter. It can also change the Service Category
+11 ;to "T" if the name of the Clinic Stop contains "TELE".
+12 ;If the Stop Code is in file #150.1, the Service Category will be changed to X or D, see SVC^PXKCO.
+13 ;It can also be changed to A when EN^PXKCO is called from the protocol event SDAM APPOINTMENT EVENTS.
+14 ;This event is fired as part of SDAM PCE EVENT (D EN^SDPCE) which in turn is run directly from
+15 ;EVENT^PXKMASC.
+16 ;If there is a possibility the input value of SVC may not be correct, pass CSVC=1 to bypass Service
+17 ;Category matching except when SVC="E" is passed.
+18 ;
+19 SET VDT=$$VFMDATE^PXDATE(VDT,"ST")
+20 IF (+DFN'>0)!('VDT)
SET VISITLIST(0)=-1
QUIT
+21 IF (SVC'="E")
IF (+HLOC'>0)
SET VISITLIST(0)=-1
QUIT
+22 IF (+HLOC>0)
IF ('$DATA(^SC(HLOC)))
SET VISITLIST(0)=-1
QUIT
+23 ;
+24 NEW AMCLIST,APPTVST,COSVC,DATE,DECLIST,DSST,DSSTP,ENCTYP,HOSPLOC,INP,INVDT,IPM,NFOUND
+25 NEW SERVCAT,STATUS,SVCMATCH,TEMP,TESTSVC,TIME,VISITIEN,VISITS,VLIST,VSIT
+26 ;
+27 ;Get the visit for the appointment if there is one.
+28 SET APPTVST=$SELECT(SVC="E":0,1:+$$APPT2VST^PXUTL1(DFN,VDT,HLOC))
+29 IF APPTVST>0
SET VISITLIST(0)=1
SET VISITLIST(1)=APPTVST
SET VISITLIST(1,"A")=""
QUIT
+30 ;
+31 SET VSIT("VDT")=VDT
+32 SET VSIT("PAT")=DFN
+33 SET VSIT("LOC")=HLOC
+34 SET VSIT("INS")=$GET(INS)
+35 SET VSIT("DSS")=$GET(DSS)
+36 SET VSIT("TYPE")=$GET(TYPE)
+37 SET VSIT("ENCTYPE")=$GET(ENCTYPE)
+38 SET VSIT("SVC")=$GET(SVC)
+39 ;
+40 SET (NFOUND,VISITLIST(0))=0
+41 ;Build a list of visits based on matching VDT, DFN, and HLOC.
+42 SET DATE=$PIECE(VDT,".",1)
SET TIME=$PIECE(VDT,".",2)
+43 SET INVDT=(9999999-DATE)
+44 IF TIME'=""
SET INVDT=INVDT_"."_TIME
+45 SET VISITIEN=0
+46 FOR
SET VISITIEN=+$ORDER(^AUPNVSIT("AA",DFN,INVDT,VISITIEN))
if VISITIEN=0
QUIT
Begin DoDot:1
+47 IF $DATA(VISITS(VISITIEN))
QUIT
+48 SET ENCTYP=$PIECE($GET(^AUPNVSIT(VISITIEN,150)),U,3)
+49 ;Ignore Credit Stop encounters.
+50 IF ENCTYP="C"
QUIT
+51 SET TEMP=^AUPNVSIT(VISITIEN,0)
+52 SET SERVCAT=$PIECE(TEMP,U,7)
+53 ;Service Category is required, if it is NULL, it is a corrupted entry.
+54 IF SERVCAT=""
QUIT
+55 ;If SVC=E, match Service Category, otherwise ignore it.
+56 IF (VSIT("SVC")="E")
IF (SERVCAT'="E")
QUIT
+57 IF (VSIT("SVC")'="E")
IF (SERVCAT="E")
QUIT
+58 SET HOSPLOC=$PIECE(TEMP,U,22)
+59 IF (VSIT("LOC")'="")
IF (VSIT("LOC")'=HOSPLOC)
QUIT
+60 IF HOSPLOC=""
SET HOSPLOC=0
+61 SET VISITS(VISITIEN)=""
+62 SET NFOUND=NFOUND+1
SET VISITLIST(NFOUND)=VISITIEN
End DoDot:1
+63 SET VISITLIST(0)=NFOUND
+64 ;If there are no matches or an exact match the search is done.
+65 IF NFOUND<2
QUIT
+66 ;
+67 ;When there are multiple matches, use additional criteria
+68 ;to try to find a unique match.
+69 ;
+70 ;Allow for service category being changed depending on the patient's
+71 ;status (inpatient/outpatient) and the stop code.
+72 SET TESTSVC=""
+73 IF ($GET(CSVC)=1)
IF (VSIT("SVC")'="E")
Begin DoDot:1
+74 ;Use this special check for inpatient status.
+75 SET IPM=$$IP^VSITCK1(VDT,DFN)
+76 SET INP=$SELECT(IPM>0:1,1:0)
+77 IF +VSIT("DSS")>0
SET DSSTP=VSIT("DSS")
+78 IF (VSIT("DSS")="")
IF (+VSIT("LOC")>0)
SET DSSTP=+$PIECE(^SC(VSIT("LOC"),0),U,7)
+79 IF DSSTP>0
SET TESTSVC=$$SVC^PXKCO(SVC,DSSTP,INP,VSIT("LOC"))
End DoDot:1
+80 ;
+81 SET NFOUND=0
SET VISITIEN=""
+82 FOR
SET VISITIEN=$ORDER(VISITS(VISITIEN))
if VISITIEN=""
QUIT
Begin DoDot:1
+83 ;Allow for checked out encounters having their service category changed.
+84 ;If the hospital location's stop code contains "TELE" the service category will always be T.
+85 ;It is not changed on check out.
+86 SET COSVC=""
+87 SET STATUS=$SELECT(VSIT("SVC")="E":0,TESTSVC="T":0,1:+$$STATUS^SDPCE(VISITIEN))
+88 IF STATUS=2
Begin DoDot:2
+89 IF INP=0
SET COSVC=$SELECT(VSIT("SVC")="X":"X",1:"A")
+90 IF INP=1
SET COSVC=$SELECT(VSIT("SVC")="X":"D",1:"I")
End DoDot:2
+91 ;
+92 SET TEMP=$GET(^AUPNVSIT(VISITIEN,0))
+93 SET SVCMATCH=0
+94 IF $GET(CSVC)=1
Begin DoDot:2
+95 IF (VSIT("SVC")'="")
IF (VSIT("SVC")=$PIECE(TEMP,U,7))
SET SVCMATCH=1
+96 IF (SVCMATCH=0)
IF (TESTSVC'="")
IF (TESTSVC=$PIECE(TEMP,U,7))
SET SVCMATCH=1
+97 IF (SVCMATCH=0)
IF (COSVC'="")
IF (COSVC=$PIECE(TEMP,U,7))
SET SVCMATCH=1
End DoDot:2
+98 IF SVCMATCH=0
QUIT
+99 ;
+100 IF (VSIT("TYPE")'="")
IF (VSIT("TYPE")'=$PIECE(TEMP,U,3))
QUIT
+101 IF (VSIT("DSS")'="")
IF (VSIT("DSS")'=$PIECE(TEMP,U,8))
QUIT
+102 IF (VSIT("INS")'="")
IF (VSIT("INS")'=$PIECE(TEMP,U,6))
QUIT
+103 IF VSIT("ENCTYPE")'=""
Begin DoDot:2
+104 SET ENCTYP=($PIECE($GET(^AUPNVSIT(VISITIEN,150)),U,3))
End DoDot:2
if VSIT("ENCTYPE")'=ENCTYP
QUIT
+105 SET NFOUND=NFOUND+1
SET AMCLIST(NFOUND)=VISITIEN
End DoDot:1
+106 SET AMCLIST(0)=NFOUND
+107 ;If there is an exact match the search is done.
+108 IF AMCLIST(0)=1
Begin DoDot:1
+109 KILL VISITLIST
+110 SET VISITLIST(0)=AMCLIST(0)
SET VISITLIST(1)=AMCLIST(1)
End DoDot:1
QUIT
+111 ;
+112 IF (AMCLIST(0)>0)
IF (AMCLIST(0)<VISITLIST(0))
FOR IND=0:1:AMCLIST(0)
SET DECLIST(IND)=AMCLIST(IND)
+113 IF '$TEST
FOR IND=0:1:VISITLIST(0)
SET DECLIST(IND)=VISITLIST(IND)
+114 ;
+115 ;Process multiple matches.
+116 NEW DECOUNT,KEEPIEN,KEEPIND,NREFS,NREFSD,NVISITS,TEMPLIST
+117 FOR IND=1:1:DECLIST(0)
Begin DoDot:1
+118 SET VISITIEN=DECLIST(IND)
+119 SET DECOUNT=+$PIECE(^AUPNVSIT(VISITIEN,0),U,9)
+120 SET NREFSD(DECOUNT,IND)=VISITIEN
End DoDot:1
+121 ;Order the list based on Dependent Entry Count; largest is first. Remove
+122 ;visits with no references.
+123 KILL VISITLIST
+124 SET NREFS=""
SET NVISITS=0
+125 FOR
SET NREFS=+$ORDER(NREFSD(NREFS),-1)
if NREFS=0
QUIT
Begin DoDot:1
+126 SET IND=""
+127 FOR
SET IND=$ORDER(NREFSD(NREFS,IND))
if IND=""
QUIT
Begin DoDot:2
+128 SET NVISITS=NVISITS+1
SET VISITLIST(NVISITS)=DECLIST(IND)
End DoDot:2
End DoDot:1
+129 IF NVISITS>0
SET VISITLIST(0)=NVISITS
QUIT
+130 ;
+131 ;If no visits have references return the first one.
+132 SET KEEPIND=$ORDER(NREFSD(0,""))
+133 SET KEEPIEN=DECLIST(KEEPIND)
+134 KILL VISITLIST
+135 SET VISITLIST(0)=1
SET VISITLIST(1)=KEEPIEN
+136 QUIT
+137 ;
+138 ;=========
LOOKVSIT(PAT,VDT,LOC,DSS,INS,TYP) ;Calls visit tracking to see if there is
+1 ;already a visit.
+2 ;Get the visit for the appointment is there is one
+3 NEW APPTVST
+4 SET APPTVST=$$APPT2VST^PXUTL1(PAT,VDT,LOC)
+5 IF APPTVST>0
QUIT APPTVST
+6 ;
+7 NEW VSIT,VSITPKG
+8 SET VSIT("IEN")=""
+9 SET VSIT("VDT")=VDT
+10 SET VSIT("PAT")=PAT
+11 SET VSIT("LOC")=LOC
+12 SET VSIT("INS")=$GET(INS)
+13 SET VSIT("TYP")=$GET(TYP)
+14 SET VSIT("DSS")=$GET(DSS)
+15 IF VSIT("DSS")=""
IF $PIECE($GET(^SC(+VSIT("LOC"),0)),"^",7)>0
SET VSIT("DSS")=$PIECE(^SC(+VSIT("LOC"),0),"^",7)
+16 SET VSITPKG="PX"
+17 SET VSIT(0)="D0EM"
+18 ;
+19 ;CALL FOR VSIT
+20 DO ^VSIT
+21 IF '$DATA(VSIT("IEN"))#2
QUIT -1
+22 QUIT $PIECE(VSIT("IEN"),"^",1)
+23 ;