BGPMUG06 ; IHS/MSC/MMT - MI measure NQF0004 ;20-Aug-2011 14:56;DU
;;11.1;IHS CLINICAL REPORTING SYSTEM;**1**;JUN 27, 2011;Build 106
;Code to collect meaningful use report for Alcohol and Other Drug Dependence Treatment
ENTRY ;EP
N START,END,STRING,STRING2
N IEN,INV,VISIT,DATA,VDATE,VALUE,FIRST,VIEN
N CNT,NUM1,NUM2,DEN
S (NUM1,NUM2,DEN)=0
;Pts must be 13 or older
;No need to check further if no age match
Q:BGPAGEE<13
S DEN=$$DEN(DFN,BGPBDATE,BGPEDATE)
Q:'+DEN
;check to see if they are in the numerators
S NUM1=$$NUM1(DFN,DEN)
S:+NUM1 NUM2=$$NUM2(DFN,NUM1)
D TOTAL(DFN,DEN,NUM1,NUM2)
Q
TOTAL(DFN,DEN,NUM1,NUM2) ;See where this patient ends up
D:BGPAGEE<18 TOTSUB(1) ;ages 13-17
D:BGPAGEE>17 TOTSUB(2) ;ages 18+
D TOTSUB(3) ;all ages 13+
Q
TOTSUB(DENPOP) ;ADD Patient to a population's totals
N TOTAL,PTCNT,DENCT,NUM1CT,NUM2CT,NOTNUM
N DENSTR1,DENSTR2,NUMSTR1,NUMSTR2
S TOTAL=$G(^TMP("BGPMU0004",$J,BGPMUTF,DENPOP,"TOT"))
S DENCT=+$G(^TMP("BGPMU0004",$J,BGPMUTF,DENPOP,"DEN"))
S NUM1CT=+$G(^TMP("BGPMU0004",$J,BGPMUTF,DENPOP,"NUM",1))
S NUM2CT=+$G(^TMP("BGPMU0004",$J,BGPMUTF,DENPOP,"NUM",2))
S NOTNUM1=+$G(^TMP("BGPMU0004",$J,BGPMUTF,DENPOP,"NOT",1))
S NOTNUM2=+$G(^TMP("BGPMU0004",$J,BGPMUTF,DENPOP,"NOT",2))
S PTCNT=TOTAL
S PTCNT=PTCNT+1
S DENCT=DENCT+1 S ^TMP("BGPMU0004",$J,BGPMUTF,DENPOP,"DEN")=DENCT
S DENSTR1=$$DENSTR1(DEN)
S DENSTR2=DENSTR1
S NUMSTR1=$$NUMSTR1(NUM1)
S NUMSTR2=$$NUMSTR2(NUM1)
I +NUM1 D
.S NUM1CT=NUM1CT+1 S ^TMP("BGPMU0004",$J,BGPMUTF,DENPOP,"NUM",1)=NUM1CT
.I BGPMUTF="C" S ^TMP("BGPMU0004",$J,"PAT",BGPMUTF,DENPOP,"NUM",1,PTCNT)=DFN_U_DENSTR1_U_"M:"_NUMSTR1
I +NUM1=0 D
.S NOTNUM1=NOTNUM1+1 S ^TMP("BGPMU0004",$J,BGPMUTF,DENPOP,"NOT",1)=NOTNUM1
.I BGPMUTF="C" S ^TMP("BGPMU0004",$J,"PAT",BGPMUTF,DENPOP,"NOT",1,PTCNT)=DFN_U_DENSTR1_U_"NM:"_NUMSTR1
I +NUM2 D
.S NUM2CT=NUM2CT+1 S ^TMP("BGPMU0004",$J,BGPMUTF,DENPOP,"NUM",2)=NUM2CT
.I BGPMUTF="C" S ^TMP("BGPMU0004",$J,"PAT",BGPMUTF,DENPOP,"NUM",2,PTCNT)=DFN_U_DENSTR2_U_"M:"_NUMSTR2
I +NUM2=0 D
.S NOTNUM2=NOTNUM2+1 S ^TMP("BGPMU0004",$J,BGPMUTF,DENPOP,"NOT",2)=NOTNUM2
.I BGPMUTF="C" S ^TMP("BGPMU0004",$J,"PAT",BGPMUTF,DENPOP,"NOT",2,PTCNT)=DFN_U_DENSTR2_U_"NM:"_NUMSTR2
S ^TMP("BGPMU0004",$J,BGPMUTF,DENPOP,"TOT")=PTCNT
;Setup iCare array for patient only for the 3rd (all encompassing) population
S:DENPOP=3 BGPICARE("MU.EP.0004.1",BGPMUTF)=1_U_+NUM1_U_0_U_DENSTR1_U_NUMSTR1
S:DENPOP=3 BGPICARE("MU.EP.0004.2",BGPMUTF)=1_U_+NUM2_U_0_U_DENSTR2_U_NUMSTR2
Q
NUM1(DFN,DEN) ;Look for alcohol/drug treatment
;return: local VIEN array ptr ^ VISIT file ptr ^ VISIT DATE/TIME
N FOUND,VPTR,INITDATE,VSTRT
S FOUND=0,INITDATE=$$FMADD^XLFDT($P($P(DEN,U,2),"."),14)_".2359"
;quit early if DEN visit also qualifies as FIRST treatment (7th flag)
I $P($G(VIEN($P(DEN,U,3))),U,7) S FOUND=$P(DEN,U,3)_U_$P(VIEN($P(DEN,U,3)),U,1,2)_U_U_$P(DEN,U,3) Q FOUND
S VSTRT=$S($P(DEN,U)=2:CNT,1:($P(DEN,U,3)-1))
F VPTR=VSTRT:-1:1 D Q:FOUND'=0
.;make sure to avoid ED visits(3rd flag) and BH w/o Dx (6th flag)
.I '$P(VIEN(VPTR),U,3)&'$P(VIEN(VPTR),U,6) S FOUND=$S($P(VIEN(VPTR),U,2)>INITDATE:0,1:VPTR)_U_$P(VIEN(VPTR),U,1,2)_U_VPTR Q
Q FOUND
NUM2(DFN,NUM1) ;Look for 2nd & 3rd alcohol/drug treatment <= 30 days after first treatment (NUM1)
;return just a TRUE if met since not mets will still need to display encounters
N FOUND,VPTR,V1,FUP2DATE,TCNT,ST
K ST
S TCNT=0,FUP2DATE=$$FMADD^XLFDT($P($P(NUM1,U,3),"."),30)_".2359"
S FOUND=""
S V1=$P(NUM1,U)-1
F VPTR=V1:-1:1 D
.Q:$P(VIEN(VPTR),U,2)>FUP2DATE ;stop once we've gone past 30 days after FIRST
.;make sure to avoid ED visits
.S:'$P(VIEN(VPTR),U,3)&'$P(VIEN(VPTR),U,7) TCNT=TCNT+1
.I TCNT>1 S FOUND=1 Q
Q FOUND
DEN(DFN,BDATE,EDATE) ;CHECK IF PT IS IN DENOMINATOR
N SRCHSTRT,SRCHEND,START,END
N ALCDX,ALCPROC,EDENC,IPENC,IPAENC,BHENC,BHPENC
N FIRSTVST,INTVPRC,PREVDX,VPTR
S SRCHSTRT=$$FMADD^XLFDT(EDATE,-365),SRCHEND=$$FMADD^XLFDT(EDATE,-45)
S START=9999999-SRCHSTRT,END=9999999-SRCHEND
S START=START_".2359"
S CNT=0
S FIRST=END-0.1 F S FIRST=$O(^AUPNVSIT("AA",DFN,FIRST)) Q:FIRST=""!($P(FIRST,".",1)>START) D
.S IEN=0 F S IEN=$O(^AUPNVSIT("AA",DFN,FIRST,IEN)) Q:'+IEN D
..;Check provider, Only visits for chosen provider
..Q:'$$PRV^BGPMUUT1(IEN,BGPPROV)
..;Next check for Dx of AOD dependence
..S ALCDX=$$VSTPOV^BGPMUUT3(DFN,IEN,"BGPMU ALCOHOL DRUG DEP DX")
..S ALCPROC=$$VSTICD0^BGPMUUT3(DFN,IEN,"BGPMU ALCOHOL IP DETOX ICD0")
..S (EDENC,IPENC,IPAENC,BHENC,BHPENC)=""
..;check encounter codes
..S EDENC=$$VSTCPT^BGPMUUT1(DFN,IEN,"BGPMU ENCOUNTER ED CPT")
..I +EDENC&(+ALCDX) D VSTSTORE Q
..S IPAENC=$$VSTCPT^BGPMUUT1(DFN,IEN,"BGPMU ACUTE INPT ENC")
..I +IPAENC&(+ALCDX!+ALCPROC) D VSTSTORE Q
..S IPENC=$$VSTCPT^BGPMUUT1(DFN,IEN,"BGPMU NON-ACUTE INPT CPT")
..I +IPENC&(+ALCDX!+ALCPROC) D VSTSTORE Q
..S BHENC=$$VSTCPT^BGPMUUT1(DFN,IEN,"BGPMU ENC OUTPT BEHAVIOR HLTH")
..I +BHENC D VSTSTORE Q
..S BHPENC=$$VSTCPT^BGPMUUT1(DFN,IEN,"BGPMU ENC OUTPT BEHAVIOR POS")
..I +BHPENC&(+ALCDX) D VSTSTORE Q
;if no visits found, check entire search period for Intervention procedure, otherwise BEFORE earliest visit or Intervention
;set up earliest episode in either case
S FIRSTVST=0
I CNT=0 D
.S INTVPRC=$$LASTPRC^BGPMUUT2(DFN,SRCHSTRT,SRCHEND,"BGPMU ALCOHOL DETOX INTV ICD0")
.I +INTVPRC S:($P($G(^AUPNVPRC($P(INTVPRC,U,5),12)),U,4)=BGPPROV) FIRSTVST=2_U_$P(INTVPRC,U,3)_U_$P(INTVPRC,U,5) ;2 ^ Date ^ V PROC ptr
E D
.F VPTR=CNT:-1:1 I '$P(VIEN(VPTR),U,6) S FIRSTVST=$P(VIEN(VPTR),U,2) Q
.Q:FIRSTVST=0
.S INTVPRC=$$LASTPRC^BGPMUUT2(DFN,SRCHSTRT,FIRSTVST,"BGPMU ALCOHOL DETOX INTV ICD0")
.I +INTVPRC,(($P(INTVPRC,U,3)<FIRSTVST)&($P($G(^AUPNVPRC($P(INTVPRC,U,5),12)),U,4)=BGPPROV)) S FIRSTVST=2_U_$P(INTVPRC,U,3)_U_$P(INTVPRC,U,5)
.E S FIRSTVST=1_U_$P(VIEN(VPTR),U,2)_U_VPTR
I FIRSTVST=0 S DEN=0 Q DEN
;now that the first episode is established, check to make sure a previous Dx not present in the 60 days prior
S PREVDX=$$LASTDX^BGPMUUT2(DFN,$$FMADD^XLFDT($P(FIRSTVST,U,2),-60),$$FMADD^XLFDT($P(FIRSTVST,U,2),-2),"BGPMU ALCOHOL DRUG DEP DX")
I +PREVDX S DEN=0 Q DEN
S DEN=FIRSTVST
K ALCDX,ALCPROC,EDENC,IPENC,IPAENC,BHENC,BHPENC
Q DEN
VSTSTORE ;Store compliant visit into array
S CNT=CNT+1
S VDATE=$P($G(^AUPNVSIT(IEN,0)),U,1)
;3rd flag = ED encounters so they can be ignored for both NUM's
;6th flag = may count towards subsequent treatments (Num2) but NOT for the denominator or Num 1
;7th flag = may count toward Den and Num 1, but NOT Num2
S VIEN(CNT)=IEN_U_VDATE_U_+EDENC_U_$P(ALCDX,U,3)_U_$P(ALCPROC,U,3)_U_$S(+$G(BHENC):'ALCDX,1:0)_U_$S(+$G(IPAENC)!+$G(IPENC):(+ALCPROC),1:0)
;S VIEN(CNT)=IEN_U_VDATE_U_+EDENC_U_$P(ALCDX,U,3)_U_$P(ALCPROC,U,3)_U_$S(+$G(BHENC):'ALCDX,1:0)_U_$S(+$G(IPAENC)!+$G(IPENC):(+ALCPROC&'ALCDX),1:0)
S STRING(CNT)=$$DATE^BGPMUUTL(VDATE)
Q
DENSTR1(DEN) ;generate display string for denom criteria 1 - Dx present with IP/ED visit
N STRING,DXDT,PRCDT
S DXDT=$S($P(DEN,U)=1:$P(VIEN($P(DEN,U,3)),U,4),1:"")
S PRCDT=$S($P(DEN,U)=1:$P(VIEN($P(DEN,U,3)),U,5),1:"")
I $P(DEN,U)=2 D
.S STRING="DI:"_$$DATE^BGPMUUTL($P(DEN,U,2))
E D
.S STRING=$S(DXDT'="":"ADX:"_$$DATE^BGPMUUTL(DXDT),PRCDT'="":"ADI:"_$$DATE^BGPMUUTL(PRCDT),1:"")
.S STRING=STRING_";EN:"_$$DATE^BGPMUUTL($P(VIEN($P(DEN,U,3)),U,2))
Q STRING
NUMSTR1(NUM1) ;generate display string for NUM 1
Q $S($P(NUM1,U,3)'="":"FT:"_$$DATE^BGPMUUTL($P(NUM1,U,3)),1:"")
NUMSTR2(NUM1) ;generate display string for NUM 2
N STRING,V1,VPTR,COUNT
S STRING="",COUNT=0
S STRING=$S($P(NUM1,U,3)'="":"FT:"_$$DATE^BGPMUUTL($P(NUM1,U,3)),1:"")
S V1=$P(NUM1,U,4)-1
F VPTR=V1:-1:1 D Q:COUNT>1
.S STRING=STRING_$S(STRING'="":";",1:"")_"ST:"_$$DATE^BGPMUUTL($P(VIEN(VPTR),U,2))
.S COUNT=COUNT+1
.Q:COUNT>1
Q STRING
BGPMUG06 ; IHS/MSC/MMT - MI measure NQF0004 ;20-Aug-2011 14:56;DU
+1 ;;11.1;IHS CLINICAL REPORTING SYSTEM;**1**;JUN 27, 2011;Build 106
+2 ;Code to collect meaningful use report for Alcohol and Other Drug Dependence Treatment
ENTRY ;EP
+1 NEW START,END,STRING,STRING2
+2 NEW IEN,INV,VISIT,DATA,VDATE,VALUE,FIRST,VIEN
+3 NEW CNT,NUM1,NUM2,DEN
+4 SET (NUM1,NUM2,DEN)=0
+5 ;Pts must be 13 or older
+6 ;No need to check further if no age match
+7 IF BGPAGEE<13
QUIT
+8 SET DEN=$$DEN(DFN,BGPBDATE,BGPEDATE)
+9 IF '+DEN
QUIT
+10 ;check to see if they are in the numerators
+11 SET NUM1=$$NUM1(DFN,DEN)
+12 IF +NUM1
SET NUM2=$$NUM2(DFN,NUM1)
+13 DO TOTAL(DFN,DEN,NUM1,NUM2)
+14 QUIT
TOTAL(DFN,DEN,NUM1,NUM2) ;See where this patient ends up
+1 ;ages 13-17
IF BGPAGEE<18
DO TOTSUB(1)
+2 ;ages 18+
IF BGPAGEE>17
DO TOTSUB(2)
+3 ;all ages 13+
DO TOTSUB(3)
+4 QUIT
TOTSUB(DENPOP) ;ADD Patient to a population's totals
+1 NEW TOTAL,PTCNT,DENCT,NUM1CT,NUM2CT,NOTNUM
+2 NEW DENSTR1,DENSTR2,NUMSTR1,NUMSTR2
+3 SET TOTAL=$GET(^TMP("BGPMU0004",$JOB,BGPMUTF,DENPOP,"TOT"))
+4 SET DENCT=+$GET(^TMP("BGPMU0004",$JOB,BGPMUTF,DENPOP,"DEN"))
+5 SET NUM1CT=+$GET(^TMP("BGPMU0004",$JOB,BGPMUTF,DENPOP,"NUM",1))
+6 SET NUM2CT=+$GET(^TMP("BGPMU0004",$JOB,BGPMUTF,DENPOP,"NUM",2))
+7 SET NOTNUM1=+$GET(^TMP("BGPMU0004",$JOB,BGPMUTF,DENPOP,"NOT",1))
+8 SET NOTNUM2=+$GET(^TMP("BGPMU0004",$JOB,BGPMUTF,DENPOP,"NOT",2))
+9 SET PTCNT=TOTAL
+10 SET PTCNT=PTCNT+1
+11 SET DENCT=DENCT+1
SET ^TMP("BGPMU0004",$JOB,BGPMUTF,DENPOP,"DEN")=DENCT
+12 SET DENSTR1=$$DENSTR1(DEN)
+13 SET DENSTR2=DENSTR1
+14 SET NUMSTR1=$$NUMSTR1(NUM1)
+15 SET NUMSTR2=$$NUMSTR2(NUM1)
+16 IF +NUM1
Begin DoDot:1
+17 SET NUM1CT=NUM1CT+1
SET ^TMP("BGPMU0004",$JOB,BGPMUTF,DENPOP,"NUM",1)=NUM1CT
+18 IF BGPMUTF="C"
SET ^TMP("BGPMU0004",$JOB,"PAT",BGPMUTF,DENPOP,"NUM",1,PTCNT)=DFN_U_DENSTR1_U_"M:"_NUMSTR1
End DoDot:1
+19 IF +NUM1=0
Begin DoDot:1
+20 SET NOTNUM1=NOTNUM1+1
SET ^TMP("BGPMU0004",$JOB,BGPMUTF,DENPOP,"NOT",1)=NOTNUM1
+21 IF BGPMUTF="C"
SET ^TMP("BGPMU0004",$JOB,"PAT",BGPMUTF,DENPOP,"NOT",1,PTCNT)=DFN_U_DENSTR1_U_"NM:"_NUMSTR1
End DoDot:1
+22 IF +NUM2
Begin DoDot:1
+23 SET NUM2CT=NUM2CT+1
SET ^TMP("BGPMU0004",$JOB,BGPMUTF,DENPOP,"NUM",2)=NUM2CT
+24 IF BGPMUTF="C"
SET ^TMP("BGPMU0004",$JOB,"PAT",BGPMUTF,DENPOP,"NUM",2,PTCNT)=DFN_U_DENSTR2_U_"M:"_NUMSTR2
End DoDot:1
+25 IF +NUM2=0
Begin DoDot:1
+26 SET NOTNUM2=NOTNUM2+1
SET ^TMP("BGPMU0004",$JOB,BGPMUTF,DENPOP,"NOT",2)=NOTNUM2
+27 IF BGPMUTF="C"
SET ^TMP("BGPMU0004",$JOB,"PAT",BGPMUTF,DENPOP,"NOT",2,PTCNT)=DFN_U_DENSTR2_U_"NM:"_NUMSTR2
End DoDot:1
+28 SET ^TMP("BGPMU0004",$JOB,BGPMUTF,DENPOP,"TOT")=PTCNT
+29 ;Setup iCare array for patient only for the 3rd (all encompassing) population
+30 IF DENPOP=3
SET BGPICARE("MU.EP.0004.1",BGPMUTF)=1_U_+NUM1_U_0_U_DENSTR1_U_NUMSTR1
+31 IF DENPOP=3
SET BGPICARE("MU.EP.0004.2",BGPMUTF)=1_U_+NUM2_U_0_U_DENSTR2_U_NUMSTR2
+32 QUIT
NUM1(DFN,DEN) ;Look for alcohol/drug treatment
+1 ;return: local VIEN array ptr ^ VISIT file ptr ^ VISIT DATE/TIME
+2 NEW FOUND,VPTR,INITDATE,VSTRT
+3 SET FOUND=0
SET INITDATE=$$FMADD^XLFDT($PIECE($PIECE(DEN,U,2),"."),14)_".2359"
+4 ;quit early if DEN visit also qualifies as FIRST treatment (7th flag)
+5 IF $PIECE($GET(VIEN($PIECE(DEN,U,3))),U,7)
SET FOUND=$PIECE(DEN,U,3)_U_$PIECE(VIEN($PIECE(DEN,U,3)),U,1,2)_U_U_$PIECE(DEN,U,3)
QUIT FOUND
+6 SET VSTRT=$SELECT($PIECE(DEN,U)=2:CNT,1:($PIECE(DEN,U,3)-1))
+7 FOR VPTR=VSTRT:-1:1
Begin DoDot:1
+8 ;make sure to avoid ED visits(3rd flag) and BH w/o Dx (6th flag)
+9 IF '$PIECE(VIEN(VPTR),U,3)&'$PIECE(VIEN(VPTR),U,6)
SET FOUND=$SELECT($PIECE(VIEN(VPTR),U,2)>INITDATE:0,1:VPTR)_U_$PIECE(VIEN(VPTR),U,1,2)_U_VPTR
QUIT
End DoDot:1
IF FOUND'=0
QUIT
+10 QUIT FOUND
NUM2(DFN,NUM1) ;Look for 2nd & 3rd alcohol/drug treatment <= 30 days after first treatment (NUM1)
+1 ;return just a TRUE if met since not mets will still need to display encounters
+2 NEW FOUND,VPTR,V1,FUP2DATE,TCNT,ST
+3 KILL ST
+4 SET TCNT=0
SET FUP2DATE=$$FMADD^XLFDT($PIECE($PIECE(NUM1,U,3),"."),30)_".2359"
+5 SET FOUND=""
+6 SET V1=$PIECE(NUM1,U)-1
+7 FOR VPTR=V1:-1:1
Begin DoDot:1
+8 ;stop once we've gone past 30 days after FIRST
IF $PIECE(VIEN(VPTR),U,2)>FUP2DATE
QUIT
+9 ;make sure to avoid ED visits
+10 IF '$PIECE(VIEN(VPTR),U,3)&'$PIECE(VIEN(VPTR),U,7)
SET TCNT=TCNT+1
+11 IF TCNT>1
SET FOUND=1
QUIT
End DoDot:1
+12 QUIT FOUND
DEN(DFN,BDATE,EDATE) ;CHECK IF PT IS IN DENOMINATOR
+1 NEW SRCHSTRT,SRCHEND,START,END
+2 NEW ALCDX,ALCPROC,EDENC,IPENC,IPAENC,BHENC,BHPENC
+3 NEW FIRSTVST,INTVPRC,PREVDX,VPTR
+4 SET SRCHSTRT=$$FMADD^XLFDT(EDATE,-365)
SET SRCHEND=$$FMADD^XLFDT(EDATE,-45)
+5 SET START=9999999-SRCHSTRT
SET END=9999999-SRCHEND
+6 SET START=START_".2359"
+7 SET CNT=0
+8 SET FIRST=END-0.1
FOR
SET FIRST=$ORDER(^AUPNVSIT("AA",DFN,FIRST))
IF FIRST=""!($PIECE(FIRST,".",1)>START)
QUIT
Begin DoDot:1
+9 SET IEN=0
FOR
SET IEN=$ORDER(^AUPNVSIT("AA",DFN,FIRST,IEN))
IF '+IEN
QUIT
Begin DoDot:2
+10 ;Check provider, Only visits for chosen provider
+11 IF '$$PRV^BGPMUUT1(IEN,BGPPROV)
QUIT
+12 ;Next check for Dx of AOD dependence
+13 SET ALCDX=$$VSTPOV^BGPMUUT3(DFN,IEN,"BGPMU ALCOHOL DRUG DEP DX")
+14 SET ALCPROC=$$VSTICD0^BGPMUUT3(DFN,IEN,"BGPMU ALCOHOL IP DETOX ICD0")
+15 SET (EDENC,IPENC,IPAENC,BHENC,BHPENC)=""
+16 ;check encounter codes
+17 SET EDENC=$$VSTCPT^BGPMUUT1(DFN,IEN,"BGPMU ENCOUNTER ED CPT")
+18 IF +EDENC&(+ALCDX)
DO VSTSTORE
QUIT
+19 SET IPAENC=$$VSTCPT^BGPMUUT1(DFN,IEN,"BGPMU ACUTE INPT ENC")
+20 IF +IPAENC&(+ALCDX!+ALCPROC)
DO VSTSTORE
QUIT
+21 SET IPENC=$$VSTCPT^BGPMUUT1(DFN,IEN,"BGPMU NON-ACUTE INPT CPT")
+22 IF +IPENC&(+ALCDX!+ALCPROC)
DO VSTSTORE
QUIT
+23 SET BHENC=$$VSTCPT^BGPMUUT1(DFN,IEN,"BGPMU ENC OUTPT BEHAVIOR HLTH")
+24 IF +BHENC
DO VSTSTORE
QUIT
+25 SET BHPENC=$$VSTCPT^BGPMUUT1(DFN,IEN,"BGPMU ENC OUTPT BEHAVIOR POS")
+26 IF +BHPENC&(+ALCDX)
DO VSTSTORE
QUIT
End DoDot:2
End DoDot:1
+27 ;if no visits found, check entire search period for Intervention procedure, otherwise BEFORE earliest visit or Intervention
+28 ;set up earliest episode in either case
+29 SET FIRSTVST=0
+30 IF CNT=0
Begin DoDot:1
+31 SET INTVPRC=$$LASTPRC^BGPMUUT2(DFN,SRCHSTRT,SRCHEND,"BGPMU ALCOHOL DETOX INTV ICD0")
+32 ;2 ^ Date ^ V PROC ptr
IF +INTVPRC
IF ($PIECE($GET(^AUPNVPRC($PIECE(INTVPRC,U,5),12)),U,4)=BGPPROV)
SET FIRSTVST=2_U_$PIECE(INTVPRC,U,3)_U_$PIECE(INTVPRC,U,5)
End DoDot:1
+33 IF '$TEST
Begin DoDot:1
+34 FOR VPTR=CNT:-1:1
IF '$PIECE(VIEN(VPTR),U,6)
SET FIRSTVST=$PIECE(VIEN(VPTR),U,2)
QUIT
+35 IF FIRSTVST=0
QUIT
+36 SET INTVPRC=$$LASTPRC^BGPMUUT2(DFN,SRCHSTRT,FIRSTVST,"BGPMU ALCOHOL DETOX INTV ICD0")
+37 IF +INTVPRC
IF (($PIECE(INTVPRC,U,3)<FIRSTVST)&($PIECE($GET(^AUPNVPRC($PIECE(INTVPRC,U,5),12)),U,4)=BGPPROV))
SET FIRSTVST=2_U_$PIECE(INTVPRC,U,3)_U_$PIECE(INTVPRC,U,5)
+38 IF '$TEST
SET FIRSTVST=1_U_$PIECE(VIEN(VPTR),U,2)_U_VPTR
End DoDot:1
+39 IF FIRSTVST=0
SET DEN=0
QUIT DEN
+40 ;now that the first episode is established, check to make sure a previous Dx not present in the 60 days prior
+41 SET PREVDX=$$LASTDX^BGPMUUT2(DFN,$$FMADD^XLFDT($PIECE(FIRSTVST,U,2),-60),$$FMADD^XLFDT($PIECE(FIRSTVST,U,2),-2),"BGPMU ALCOHOL DRUG DEP DX")
+42 IF +PREVDX
SET DEN=0
QUIT DEN
+43 SET DEN=FIRSTVST
+44 KILL ALCDX,ALCPROC,EDENC,IPENC,IPAENC,BHENC,BHPENC
+45 QUIT DEN
VSTSTORE ;Store compliant visit into array
+1 SET CNT=CNT+1
+2 SET VDATE=$PIECE($GET(^AUPNVSIT(IEN,0)),U,1)
+3 ;3rd flag = ED encounters so they can be ignored for both NUM's
+4 ;6th flag = may count towards subsequent treatments (Num2) but NOT for the denominator or Num 1
+5 ;7th flag = may count toward Den and Num 1, but NOT Num2
+6 SET VIEN(CNT)=IEN_U_VDATE_U_+EDENC_U_$PIECE(ALCDX,U,3)_U_$PIECE(ALCPROC,U,3)_U_$SELECT(+$GET(BHENC):'ALCDX,1:0)_U_$SELECT(+$GET(IPAENC)!+$GET(IPENC):(+ALCPROC),1:0)
+7 ;S VIEN(CNT)=IEN_U_VDATE_U_+EDENC_U_$P(ALCDX,U,3)_U_$P(ALCPROC,U,3)_U_$S(+$G(BHENC):'ALCDX,1:0)_U_$S(+$G(IPAENC)!+$G(IPENC):(+ALCPROC&'ALCDX),1:0)
+8 SET STRING(CNT)=$$DATE^BGPMUUTL(VDATE)
+9 QUIT
DENSTR1(DEN) ;generate display string for denom criteria 1 - Dx present with IP/ED visit
+1 NEW STRING,DXDT,PRCDT
+2 SET DXDT=$SELECT($PIECE(DEN,U)=1:$PIECE(VIEN($PIECE(DEN,U,3)),U,4),1:"")
+3 SET PRCDT=$SELECT($PIECE(DEN,U)=1:$PIECE(VIEN($PIECE(DEN,U,3)),U,5),1:"")
+4 IF $PIECE(DEN,U)=2
Begin DoDot:1
+5 SET STRING="DI:"_$$DATE^BGPMUUTL($PIECE(DEN,U,2))
End DoDot:1
+6 IF '$TEST
Begin DoDot:1
+7 SET STRING=$SELECT(DXDT'="":"ADX:"_$$DATE^BGPMUUTL(DXDT),PRCDT'="":"ADI:"_$$DATE^BGPMUUTL(PRCDT),1:"")
+8 SET STRING=STRING_";EN:"_$$DATE^BGPMUUTL($PIECE(VIEN($PIECE(DEN,U,3)),U,2))
End DoDot:1
+9 QUIT STRING
NUMSTR1(NUM1) ;generate display string for NUM 1
+1 QUIT $SELECT($PIECE(NUM1,U,3)'="":"FT:"_$$DATE^BGPMUUTL($PIECE(NUM1,U,3)),1:"")
NUMSTR2(NUM1) ;generate display string for NUM 2
+1 NEW STRING,V1,VPTR,COUNT
+2 SET STRING=""
SET COUNT=0
+3 SET STRING=$SELECT($PIECE(NUM1,U,3)'="":"FT:"_$$DATE^BGPMUUTL($PIECE(NUM1,U,3)),1:"")
+4 SET V1=$PIECE(NUM1,U,4)-1
+5 FOR VPTR=V1:-1:1
Begin DoDot:1
+6 SET STRING=STRING_$SELECT(STRING'="":";",1:"")_"ST:"_$$DATE^BGPMUUTL($PIECE(VIEN(VPTR),U,2))
+7 SET COUNT=COUNT+1
+8 IF COUNT>1
QUIT
End DoDot:1
IF COUNT>1
QUIT
+9 QUIT STRING