BEHOVM5 ; IHS/MSC/MGH -Storing BMI values ;30-Oct-2014 08:56;du
;;1.1;BEH COMPONENTS;**001010**;Sep 18, 2007
; Logic from APCDBMI
BMICALC(APCDX) ;EP - called from input templates to calculate and store BMI
I '$G(APCDX) Q
D EN^XBNEW("CALCBMI1^BEHOVM5","APCDX")
K APCDX
Q
CALCBMI1 ;
;NEW A,B,C,D,E,P,V,VD,W,H,BMI,HD,ERR,APCLFDA,BIEN,X,Y,DA,DATA,VTBMI,VTBMIP
S A=$$GET1^DIQ(9000010.01,APCDX,.01)
S VTBMI=$$VTYPE^BEHOVM("BMI"),VTBMIP=$$VTYPE^BEHOVM("BMIP")
I A'="HT",A'="WT" Q ;only ht/wt
I A="WT" D CALCBMIW^BEHOVM5 Q
I A="HT" D CALCBMIH^BEHOVM5 Q
Q
CALCBMIW ;
S BMI=""
;weight was just entered so calculate and store a bmi and bmip
S W=$$GET1^DIQ(9000010.01,APCDX,.04) ;wt value is in W
Q:W=""
S DFN=$$GET1^DIQ(9000010.01,APCDX,.02,"I") ;patient dfn
Q:DFN=""
S V=$$GET1^DIQ(9000010.01,APCDX,.03,"I") ;visit ien is in V
Q:V=""
S VD=$$VD^APCLV(V) ;visit date
S AGE=$$AGE^AUPNPAT(DFN,VD) ;age of patient on visit date
S H=$$LASTHT^BEHOVM5(DFN,VD) ;get last height entered that is allowable for age (same day for <18, last 5 YRS for 19-49, last 2 years for 50 AND OVER)
I H="" Q ;no ht so don't calculate anything
S HD=$P(H,U,2)
S H=$P(H,U)
;calc bmi
S W=W*.45359,H=(H*.0254),H=(H*H),BMI=(W/H)
;
I $$HASVM(V,"BMI",BMI) G BMIP ;already has this bmi value on this visit so don't store it again, go do BMIP
D STORE^BEHOVM5(.DATA,VTBMI,BMI,V,HD)
;NOW STORE BMIP
D BMIP
Q
STORE(DATA,TYPE,VALUE,VIEN,WTDT) ;
;store BMI as v meas
N FDA,BIEN,ERR,TAKEN
S DATA=0
S TAKEN=$P($G(^AUPNVMSR(APCDX,12)),U),TAKEN=$$CVTDATE^BGOUTL(TAKEN)
I TAKEN="" S TAKEN=$P($G(^AUPNVSIT(VIEN,0)),U),TAKEN=$$CVTDATE^BGOUTL(TAKEN)
S FDA=$NA(FDA(9000010.01,"+1,"))
S @FDA@(.01)=TYPE
S @FDA@(.02)=DFN
S @FDA@(.03)=VIEN
S @FDA@(.04)=VALUE
S @FDA@(.07)=$$NOW^XLFDT
S @FDA@(1204)=DUZ
S @FDA@(1201)=TAKEN
S @FDA@(1216)=$$NOW^XLFDT
S @FDA@(1217)=DUZ
S @FDA@(1218)=$$NOW^XLFDT
S @FDA@(1219)=DUZ
D UPDATE^DIE(,"FDA","BIEN","ERR")
I $D(ERR) S DATA="-1^Unable to store BMI"
E S DATA=0
Q
CALCBMIH ;
;ht added, calculate bmi for this date and forward till find another ht
;first delete all bmis and bmips from this date/time forward
;table all visits from this date/time forward that have a WT or BMI or BMIP
;Quit when another HT is found
N VIEN,WTDT
S BMI=""
;HEIGHT was just entered so RE-calculate and store a bmi and bmip FROM THIS VISIT
;FORWARD UNTIL WE FIND ANOTHER HT
S HT=$$GET1^DIQ(9000010.01,APCDX,.04) ;wt value is in B
Q:HT=""
S DFN=$$GET1^DIQ(9000010.01,APCDX,.02,"I") ;patient dfn is in P
Q:DFN=""
S V=$$GET1^DIQ(9000010.01,APCDX,.03,"I") ;visit ien is in V
Q:V=""
S HD=$$VD^APCLV(V)
S AGE=$$AGE^AUPNPAT(DFN,$$VD^APCLV(V))
K APCDVAR
I AGE>18,AGE<50 S E=$$FMADD^XLFDT($$VD^APCLV(V),(5*365))
I AGE>49 S E=$$FMADD^XLFDT($$VD^APCLV(V),(2*365))
I AGE<19 S E=$$VD^APCLV(V)
D ALLV^APCLAPIU(DFN,$$VD^APCLV(V),E,"APCDVAR")
;REORDER BY DATE LOWEST TO HIGHEST
S APCDSTOP=""
S X=0 F S X=$O(APCDVAR(X)) Q:X'=+X D
.S N=$P(APCDVAR(X),U,5)
.S APCDVAR("LH",$$VDTM^APCLV(N),X)=APCDVAR(X)
S D=0 F S D=$O(APCDVAR("LH",D)) Q:D'=+D D
.S X=0 F S X=$O(APCDVAR("LH",D,X)) Q:X'=+X D
..S N=$P(APCDVAR("LH",D,X),U,5)
..I $$VDTM^APCLV(N)<$$VDTM^APCLV(V) K APCDVAR("LH",D,X) ;BEFORE MY VISIT, DON'T DEAL WITH IT
..I '$$HASAVM(N,"WT") K APCDVAR("LH",D,X) ;no wts so don't bother, can't calculate bmi
..I $$HASAVM(N,"HT"),N'=V S A=D,B=X D ;KILL OFF ALL REMAINING
...F S A=$O(APCDVAR("LH",A)) Q:A'=+A F S B=$O(APCDVAR("LH",A,B)) Q:B'=+B K APCDVAR("LH",A,B)
;now calculate bmi on this array of visits
S D=0 F S D=$O(APCDVAR("LH",D)) Q:D="" D
.S X=0 F S X=$O(APCDVAR("LH",D,X)) Q:X="" D
..S N=$P(APCDVAR(X),U,5) ;visit ien
..S APCDAGE=$$AGE^AUPNPAT(DFN,$$VD^APCLV(N))
..;delete all bmis and bmips
..F S APCDZ=$$HASVM(N,"BMI",BMI) Q:'APCDZ D FILEEIE(APCDZ)
..F S APCDZ=$$HASVM(N,"BMIP",BMI) Q:'APCDZ D FILEEIE(APCDZ)
..;NOW ADD NEW BMI/BMIP
..S APCDA=0 F S APCDA=$O(^AUPNVMSR("AD",N,APCDA)) Q:APCDA'=+APCDA D
...Q:$P($G(^AUPNVMSR(APCDA,2)),U,1)
...Q:$$VAL^XBDIQ1(9000010.01,APCDA,.01)'="WT"
...S W=$$VAL^XBDIQ1(9000010.01,APCDA,.04)
...S VIEN=$$GET1^DIQ(9000010.01,APCDA,.03,"I")
...Q:'+VIEN
...S WTDT=$$GET1^DIQ(9000010,VIEN,.01,"I")
...S H=$$LASTHT(DFN,$$VD^APCLV(N)) ;get last height entered that is allowable for age (same day for <18, last 5 YRS for 19-49, last 2 years for 50 AND OVER)
...I H="" Q ;no ht so don't calculate anything
...S HD=$P(H,U,2)
...S H=$P(H,U)
...;calc bmi
...N SAVAPCDX
...S SAVAPCDX=APCDX,APCDX=APCDA
...S W=W*.45359,H=(H*.0254),H=(H*H),BMI=(W/H)
...I '$$HASVM(V,"BMI",BMI) D STORE^BEHOVM5(.DATA,VTBMI,BMI,VIEN,WTDT)
...S APCDX=SAVAPCDX
...Q:$T(BMIPCT^BEHOVM2)="" ;no routine to calculate
...Q:APCDAGE<2
...Q:APCDAGE>18
...S BMIPCT=$$BMIPCT^BEHOVM2(BMI,DFN,$$VD^APCLV(N))
...I BMIPCT'>0 Q
...;store bmip
...I $$HASVM(N,"BMIP",BMIPCT) Q ;already has this bmiP value on this visit so don't store it again, Q
...;store BMIP as v meas
...S SAVAPCDX=APCDX,APCDX=APCDA
...D STORE^BEHOVM5(.DATA,VTBMIP,BMIPCT,VIEN,WTDT)
...S APCDX=SAVAPCDX
Q
LASTHT(P,VD) ;get last allowable ht for patient's age to calculate BMI
I '$G(P) Q ""
I '$G(VD) Q ""
NEW A,CD,VALUE,%
S VALUE=""
S A=$$AGE^AUPNPAT(P,VD) ;age of patient on visit date
I A<2 Q VALUE
I A>18,A<50 D Q VALUE ;get last ht in past 5 years
.S CD=$$FMADD^XLFDT(VD,-(5*365)) ;5 yrs
.S %=$$LASTITEM^APCLAPIU(P,"HT","MEASUREMENT",CD,VD,"A")
.Q:%=""
.S VALUE=$P(%,U,3)_U_$P(%,U,1) ;send back ht value^ht date
;NOW DO OVER 49
I A>49 D Q VALUE
.S CD=$$FMADD^XLFDT(VD,-(2*365))
.S %=$$LASTITEM^APCLAPIU(P,"HT","MEASUREMENT",CD,VD,"A")
.Q:%=""
.S VALUE=$P(%,U,3)_U_$P(%,U,1) ;send back ht value^ht date
;UNDER 19 MUST BE ON SAME DATE AS WT
S %=$$LASTITEM^APCLAPIU(P,"HT","MEASUREMENT",VD,VD,"A")
I %="" Q ""
S VALUE=$P(%,U,3)_U_$P(%,U,1) ;send back ht value^ht date
Q VALUE
;
HASVM(V,T,B) ;
NEW Y,G
S Y=0,G=0 F S Y=$O(^AUPNVMSR("AD",V,Y)) Q:Y'=+Y!(G) D
.Q:$$GET1^DIQ(9000010.01,Y,.01)'=T
.Q:$$GET1^DIQ(9000010.01,Y,.04)'=B
.Q:$P($G(^AUPNVMSR(Y,2)),U,1) ;EIE
.S G=Y
Q G
BMIP ;
Q:$T(BMIPCT^BEHOVM2)="" ;no routine to calculate
Q:AGE<2
Q:AGE>18
S BMIPCT=$$BMIPCT^BEHOVM2(BMI,DFN,VD)
I BMIPCT'>0 Q
;store bmip
I $$HASVM(V,"BMIP",BMIPCT) Q ;already has this bmiP value on this visit so don't store it again, Q
;store BMIP as v meas
D STORE^BEHOVM5(.DATA,VTBMIP,BMIPCT,V,HD)
Q
EIE(APCDX) ;EP - wt or ht entered in error, bmi eie
I '$G(APCDX) Q
D EN^XBNEW("EIE1^BEHOVM5","APCDX")
K APCDX
Q
EIE1 ;
;NEW A,B,C,D,E,P,V,VD,W,H,BMI,HD,ERR,APCLFDA,BIEN,X,Y,DA,VTBMI,VTBMIP
S VTBMI=$$VTYPE^BEHOVM("BMI"),VTBMIP=$$VTYPE^BEHOVM("BMIP")
S A=$$GET1^DIQ(9000010.01,APCDX,.01)
I A'="HT",A'="WT" Q ;only ht/wt
I A="WT" D EIEW Q
I A="HT" D EIEH Q
Q
EIEW ;WT ENTERED IN ERROR
;if no other wts deleted all bmi, bmip on this visit
;
S V=$$GET1^DIQ(9000010.01,APCDX,.03,"I") ;visit ien is in V
Q:V=""
I '$$HASAVM(V,"WT") D Q
.;find all bmi's and bmip's and mark them EIE
.S APCDY=0 F S APCDY=$O(^AUPNVMSR("AD",V,APCDY)) Q:APCDY'=+APCDY D
..Q:$P($G(^AUPNVMSR(APCDY,2)),U,1) ;ALREADY EIE
..S T=$$GET1^DIQ(9000010.01,APCDY,.01)
..I T'="BMI",T'="BMIP" Q
..;mark as EIE
..D FILEEIE(APCDY)
;WHAT IF THERE IS ALREADY A WT SO NEED TO DELETE THE CORRECT BMI SO FIND BMI/BMIP AND DELETE
S W=$$GET1^DIQ(9000010.01,APCDX,.04) ;wt value is in B
Q:W=""
S DFN=$$GET1^DIQ(9000010.01,APCDX,.02,"I") ;patient dfn is in P
Q:DFN=""
S V=$$GET1^DIQ(9000010.01,APCDX,.03,"I") ;visit ien is in V
Q:V=""
S VD=$$VD^APCLV(V) ;visit date
S AGE=$$AGE^AUPNPAT(DFN,VD) ;age of patient on visit date
S H=$$LASTHT(DFN,VD) ;get last height entered that is allowable for age (same day for <18, last 5 YRS for 19-49, last 2 years for 50 AND OVER)
I H="" Q ;no ht so don't calculate anything
S HD=$P(H,U,2)
S H=$P(H,U)
;calc bmi
S W=W*.45359,H=(H*.0254),H=(H*H),BMI=(W/H)
;find bmi with this value and mark as EIE
F S APCDZ=$$HASVM(V,"BMI",BMI) Q:'APCDZ D FILEEIE(APCDZ)
;now find bmip
Q:$T(BMIPCT^BEHOVM2)="" ;no routine to calculate
Q:AGE<2
Q:AGE>18
S BMIPCT=$$BMIPCT^BEHOVM2(BMI,DFN,VD)
I BMIPCT'>0 Q
F S APCDZ=$$HASVM(V,"BMIP",BMIPCT) Q:'APCDZ D FILEEIE(APCDZ)
Q
EIEH ;
;IF HT ENTERED IN ERROR, DELETE ALL BMIS AND BMIPS UNTIL FIND ANOTHER HT, MARK THEM ALL EIE
S DFN=$$GET1^DIQ(9000010.01,APCDX,.02,"I") ;patient dfn is in P
Q:DFN=""
S V=$$GET1^DIQ(9000010.01,APCDX,.03,"I") ;visit ien is in V
Q:V=""
S HD=$$VD^APCLV(V)
S AGE=$$AGE^AUPNPAT(DFN,$$VD^APCLV(V))
K APCDVAR
I AGE>18,AGE<50 S E=$$FMADD^XLFDT($$VD^APCLV(V),(5*365))
I AGE>49 S E=$$FMADD^XLFDT($$VD^APCLV(V),(2*365))
I AGE<19 S E=$$VD^APCLV(V)
D ALLV^APCLAPIU(DFN,$$VD^APCLV(V),E,"APCDVAR")
;REORDER BY DATE LOWEST TO HIGHEST
S APCDSTOP=""
S X=0 F S X=$O(APCDVAR(X)) Q:X'=+X D
.S N=$P(APCDVAR(X),U,5)
.S APCDVAR("LH",$$VDTM^APCLV(N),X)=APCDVAR(X)
S D=0 F S D=$O(APCDVAR("LH",D)) Q:D'=+D D
.S X=0 F S X=$O(APCDVAR("LH",D,X)) Q:X'=+X D
..S N=$P(APCDVAR("LH",D,X),U,5)
..I $$VDTM^APCLV(N)<$$VDTM^APCLV(V) K APCDVAR("LH",D,X) ;BEFORE MY VISIT, DON'T DEAL WITH IT
..I '$$HASAVM(N,"WT") K APCDVAR("LH",D,X) ;no wts so don't bother, can't calculate bmi
..I $$HASAVM(N,"HT"),N'=V S A=D,B=X D ;KILL OFF ALL REMAINING
...F S A=$O(APCDVAR("LH",A)) Q:A'=+A F S B=$O(APCDVAR("LH",A,B)) Q:B'=+B K APCDVAR("LH",A,B)
;nowDELETE bmi/BMIP on this array of visits
S D=0 F S D=$O(APCDVAR("LH",D)) Q:D="" D
.S X=0 F S X=$O(APCDVAR("LH",D,X)) Q:X="" D
..S N=$P(APCDVAR(X),U,5) ;visit ien
..S APCDAGE=$$AGE^AUPNPAT(DFN,$$VD^APCLV(N))
..;delete all bmis and bmips
..F S APCDZ=$$HASAVM(N,"BMI") Q:'APCDZ D FILEEIE(APCDZ)
..F S APCDZ=$$HASAVM(N,"BMIP") Q:'APCDZ D FILEEIE(APCDZ)
..;NOW ADD NEW BMI/BMIP WITH HT PREVIOUS TO THE ONE DELETED, IF WE CAN
..S APCDA=0 F S APCDA=$O(^AUPNVMSR("AD",N,APCDA)) Q:APCDA'=+APCDA D
...Q:$P($G(^AUPNVMSR(APCDA,2)),U,1)
...Q:$$VAL^XBDIQ1(9000010.01,APCDA,.01)'="WT"
...S W=$$VAL^XBDIQ1(9000010.01,APCDA,.04)
...;calc bmi
...S H=$$LASTHT(DFN,$$VD^APCLV(N)) ;get last height entered that is allowable for age (same day for <18, last 5 YRS for 19-49, last 2 years for 50 AND OVER)
...I H="" Q ;no ht so don't calculate anything
...S HD=$P(H,U,2)
...S H=$P(H,U)
...;calc bmi
...S W=W*.45359,H=(H*.0254),H=(H*H),BMI=(W/H)
...I '$$HASVM(V,"BMI",BMI) D STORE^BEHOVM5(.DATA,VTBMI,BMI,V,HD)
...Q:$T(BMIPCT^BEHOVM2)="" ;no routine to calculate
...Q:APCDAGE<2
...Q:APCDAGE>18
...S BMIPCT=$$BMIPCT^BEHOVM2(BMI,DFN,$$VD^APCLV(N))
...I BMIPCT'>0 Q
...;store bmip
...I $$HASVM(N,"BMIP",BMIPCT) Q ;already has this bmiP value on this visit so don't store it again, Q
...;store BMIP as v meas
...D STORE^BEHOVM5(.DATA,VTBMIP,BMIPCT,V,HD)
Q
FILEEIE(APCDY) ;
I '$G(APCDY) Q
I '$D(^AUPNVMSR(APCDY)) Q
NEW APCDIENS,APCDFDA,APCDERR,DA,DIK
S APCDIENS=APCDY_","
S APCDFDA(9000010.01,APCDIENS,2)=1
S APCDFDA(9000010.01,APCDIENS,3)=DUZ
;S APCDFDA(9000010.014,"+1,"_APCDIENS,.01)=$P(BEHDATA,"^",3)
D UPDATE^DIE("","APCDFDA","APCDIEN","APCDERR")
;NOW MERGE OVER THE RESONS FROM THE OTHER ENTRY
M ^AUPNVMSR(APCDY,2.1)=^AUPNVMSR(APCDX,2.1)
;REINDEX
S DA=APCDY,DIK="^AUPNVMSR(" D IX^DIK K DA,DIK
Q
;
HASAVM(V,T) ;
NEW Y,G
S Y=0,G=0 F S Y=$O(^AUPNVMSR("AD",V,Y)) Q:Y'=+Y!(G) D
.Q:$$GET1^DIQ(9000010.01,Y,.01)'=T
.Q:$P($G(^AUPNVMSR(Y,2)),U,1)&(Y'=APCDX) ;EIE
.S G=Y
Q G
BEHOVM5 ; IHS/MSC/MGH -Storing BMI values ;30-Oct-2014 08:56;du
+1 ;;1.1;BEH COMPONENTS;**001010**;Sep 18, 2007
+2 ; Logic from APCDBMI
BMICALC(APCDX) ;EP - called from input templates to calculate and store BMI
+1 IF '$GET(APCDX)
QUIT
+2 DO EN^XBNEW("CALCBMI1^BEHOVM5","APCDX")
+3 KILL APCDX
+4 QUIT
CALCBMI1 ;
+1 ;NEW A,B,C,D,E,P,V,VD,W,H,BMI,HD,ERR,APCLFDA,BIEN,X,Y,DA,DATA,VTBMI,VTBMIP
+2 SET A=$$GET1^DIQ(9000010.01,APCDX,.01)
+3 SET VTBMI=$$VTYPE^BEHOVM("BMI")
SET VTBMIP=$$VTYPE^BEHOVM("BMIP")
+4 ;only ht/wt
IF A'="HT"
IF A'="WT"
QUIT
+5 IF A="WT"
DO CALCBMIW^BEHOVM5
QUIT
+6 IF A="HT"
DO CALCBMIH^BEHOVM5
QUIT
+7 QUIT
CALCBMIW ;
+1 SET BMI=""
+2 ;weight was just entered so calculate and store a bmi and bmip
+3 ;wt value is in W
SET W=$$GET1^DIQ(9000010.01,APCDX,.04)
+4 IF W=""
QUIT
+5 ;patient dfn
SET DFN=$$GET1^DIQ(9000010.01,APCDX,.02,"I")
+6 IF DFN=""
QUIT
+7 ;visit ien is in V
SET V=$$GET1^DIQ(9000010.01,APCDX,.03,"I")
+8 IF V=""
QUIT
+9 ;visit date
SET VD=$$VD^APCLV(V)
+10 ;age of patient on visit date
SET AGE=$$AGE^AUPNPAT(DFN,VD)
+11 ;get last height entered that is allowable for age (same day for <18, last 5 YRS for 19-49, last 2 years for 50 AND OVER)
SET H=$$LASTHT^BEHOVM5(DFN,VD)
+12 ;no ht so don't calculate anything
IF H=""
QUIT
+13 SET HD=$PIECE(H,U,2)
+14 SET H=$PIECE(H,U)
+15 ;calc bmi
+16 SET W=W*.45359
SET H=(H*.0254)
SET H=(H*H)
SET BMI=(W/H)
+17 ;
+18 ;already has this bmi value on this visit so don't store it again, go do BMIP
IF $$HASVM(V,"BMI",BMI)
GOTO BMIP
+19 DO STORE^BEHOVM5(.DATA,VTBMI,BMI,V,HD)
+20 ;NOW STORE BMIP
+21 DO BMIP
+22 QUIT
STORE(DATA,TYPE,VALUE,VIEN,WTDT) ;
+1 ;store BMI as v meas
+2 NEW FDA,BIEN,ERR,TAKEN
+3 SET DATA=0
+4 SET TAKEN=$PIECE($GET(^AUPNVMSR(APCDX,12)),U)
SET TAKEN=$$CVTDATE^BGOUTL(TAKEN)
+5 IF TAKEN=""
SET TAKEN=$PIECE($GET(^AUPNVSIT(VIEN,0)),U)
SET TAKEN=$$CVTDATE^BGOUTL(TAKEN)
+6 SET FDA=$NAME(FDA(9000010.01,"+1,"))
+7 SET @FDA@(.01)=TYPE
+8 SET @FDA@(.02)=DFN
+9 SET @FDA@(.03)=VIEN
+10 SET @FDA@(.04)=VALUE
+11 SET @FDA@(.07)=$$NOW^XLFDT
+12 SET @FDA@(1204)=DUZ
+13 SET @FDA@(1201)=TAKEN
+14 SET @FDA@(1216)=$$NOW^XLFDT
+15 SET @FDA@(1217)=DUZ
+16 SET @FDA@(1218)=$$NOW^XLFDT
+17 SET @FDA@(1219)=DUZ
+18 DO UPDATE^DIE(,"FDA","BIEN","ERR")
+19 IF $DATA(ERR)
SET DATA="-1^Unable to store BMI"
+20 IF '$TEST
SET DATA=0
+21 QUIT
CALCBMIH ;
+1 ;ht added, calculate bmi for this date and forward till find another ht
+2 ;first delete all bmis and bmips from this date/time forward
+3 ;table all visits from this date/time forward that have a WT or BMI or BMIP
+4 ;Quit when another HT is found
+5 NEW VIEN,WTDT
+6 SET BMI=""
+7 ;HEIGHT was just entered so RE-calculate and store a bmi and bmip FROM THIS VISIT
+8 ;FORWARD UNTIL WE FIND ANOTHER HT
+9 ;wt value is in B
SET HT=$$GET1^DIQ(9000010.01,APCDX,.04)
+10 IF HT=""
QUIT
+11 ;patient dfn is in P
SET DFN=$$GET1^DIQ(9000010.01,APCDX,.02,"I")
+12 IF DFN=""
QUIT
+13 ;visit ien is in V
SET V=$$GET1^DIQ(9000010.01,APCDX,.03,"I")
+14 IF V=""
QUIT
+15 SET HD=$$VD^APCLV(V)
+16 SET AGE=$$AGE^AUPNPAT(DFN,$$VD^APCLV(V))
+17 KILL APCDVAR
+18 IF AGE>18
IF AGE<50
SET E=$$FMADD^XLFDT($$VD^APCLV(V),(5*365))
+19 IF AGE>49
SET E=$$FMADD^XLFDT($$VD^APCLV(V),(2*365))
+20 IF AGE<19
SET E=$$VD^APCLV(V)
+21 DO ALLV^APCLAPIU(DFN,$$VD^APCLV(V),E,"APCDVAR")
+22 ;REORDER BY DATE LOWEST TO HIGHEST
+23 SET APCDSTOP=""
+24 SET X=0
FOR
SET X=$ORDER(APCDVAR(X))
IF X'=+X
QUIT
Begin DoDot:1
+25 SET N=$PIECE(APCDVAR(X),U,5)
+26 SET APCDVAR("LH",$$VDTM^APCLV(N),X)=APCDVAR(X)
End DoDot:1
+27 SET D=0
FOR
SET D=$ORDER(APCDVAR("LH",D))
IF D'=+D
QUIT
Begin DoDot:1
+28 SET X=0
FOR
SET X=$ORDER(APCDVAR("LH",D,X))
IF X'=+X
QUIT
Begin DoDot:2
+29 SET N=$PIECE(APCDVAR("LH",D,X),U,5)
+30 ;BEFORE MY VISIT, DON'T DEAL WITH IT
IF $$VDTM^APCLV(N)<$$VDTM^APCLV(V)
KILL APCDVAR("LH",D,X)
+31 ;no wts so don't bother, can't calculate bmi
IF '$$HASAVM(N,"WT")
KILL APCDVAR("LH",D,X)
+32 ;KILL OFF ALL REMAINING
IF $$HASAVM(N,"HT")
IF N'=V
SET A=D
SET B=X
Begin DoDot:3
+33 FOR
SET A=$ORDER(APCDVAR("LH",A))
IF A'=+A
QUIT
FOR
SET B=$ORDER(APCDVAR("LH",A,B))
IF B'=+B
QUIT
KILL APCDVAR("LH",A,B)
End DoDot:3
End DoDot:2
End DoDot:1
+34 ;now calculate bmi on this array of visits
+35 SET D=0
FOR
SET D=$ORDER(APCDVAR("LH",D))
IF D=""
QUIT
Begin DoDot:1
+36 SET X=0
FOR
SET X=$ORDER(APCDVAR("LH",D,X))
IF X=""
QUIT
Begin DoDot:2
+37 ;visit ien
SET N=$PIECE(APCDVAR(X),U,5)
+38 SET APCDAGE=$$AGE^AUPNPAT(DFN,$$VD^APCLV(N))
+39 ;delete all bmis and bmips
+40 FOR
SET APCDZ=$$HASVM(N,"BMI",BMI)
IF 'APCDZ
QUIT
DO FILEEIE(APCDZ)
+41 FOR
SET APCDZ=$$HASVM(N,"BMIP",BMI)
IF 'APCDZ
QUIT
DO FILEEIE(APCDZ)
+42 ;NOW ADD NEW BMI/BMIP
+43 SET APCDA=0
FOR
SET APCDA=$ORDER(^AUPNVMSR("AD",N,APCDA))
IF APCDA'=+APCDA
QUIT
Begin DoDot:3
+44 IF $PIECE($GET(^AUPNVMSR(APCDA,2)),U,1)
QUIT
+45 IF $$VAL^XBDIQ1(9000010.01,APCDA,.01)'="WT"
QUIT
+46 SET W=$$VAL^XBDIQ1(9000010.01,APCDA,.04)
+47 SET VIEN=$$GET1^DIQ(9000010.01,APCDA,.03,"I")
+48 IF '+VIEN
QUIT
+49 SET WTDT=$$GET1^DIQ(9000010,VIEN,.01,"I")
+50 ;get last height entered that is allowable for age (same day for <18, last 5 YRS for 19-49, last 2 years for 50 AND OVER)
SET H=$$LASTHT(DFN,$$VD^APCLV(N))
+51 ;no ht so don't calculate anything
IF H=""
QUIT
+52 SET HD=$PIECE(H,U,2)
+53 SET H=$PIECE(H,U)
+54 ;calc bmi
+55 NEW SAVAPCDX
+56 SET SAVAPCDX=APCDX
SET APCDX=APCDA
+57 SET W=W*.45359
SET H=(H*.0254)
SET H=(H*H)
SET BMI=(W/H)
+58 IF '$$HASVM(V,"BMI",BMI)
DO STORE^BEHOVM5(.DATA,VTBMI,BMI,VIEN,WTDT)
+59 SET APCDX=SAVAPCDX
+60 ;no routine to calculate
IF $TEXT(BMIPCT^BEHOVM2)=""
QUIT
+61 IF APCDAGE<2
QUIT
+62 IF APCDAGE>18
QUIT
+63 SET BMIPCT=$$BMIPCT^BEHOVM2(BMI,DFN,$$VD^APCLV(N))
+64 IF BMIPCT'>0
QUIT
+65 ;store bmip
+66 ;already has this bmiP value on this visit so don't store it again, Q
IF $$HASVM(N,"BMIP",BMIPCT)
QUIT
+67 ;store BMIP as v meas
+68 SET SAVAPCDX=APCDX
SET APCDX=APCDA
+69 DO STORE^BEHOVM5(.DATA,VTBMIP,BMIPCT,VIEN,WTDT)
+70 SET APCDX=SAVAPCDX
End DoDot:3
End DoDot:2
End DoDot:1
+71 QUIT
LASTHT(P,VD) ;get last allowable ht for patient's age to calculate BMI
+1 IF '$GET(P)
QUIT ""
+2 IF '$GET(VD)
QUIT ""
+3 NEW A,CD,VALUE,%
+4 SET VALUE=""
+5 ;age of patient on visit date
SET A=$$AGE^AUPNPAT(P,VD)
+6 IF A<2
QUIT VALUE
+7 ;get last ht in past 5 years
IF A>18
IF A<50
Begin DoDot:1
+8 ;5 yrs
SET CD=$$FMADD^XLFDT(VD,-(5*365))
+9 SET %=$$LASTITEM^APCLAPIU(P,"HT","MEASUREMENT",CD,VD,"A")
+10 IF %=""
QUIT
+11 ;send back ht value^ht date
SET VALUE=$PIECE(%,U,3)_U_$PIECE(%,U,1)
End DoDot:1
QUIT VALUE
+12 ;NOW DO OVER 49
+13 IF A>49
Begin DoDot:1
+14 SET CD=$$FMADD^XLFDT(VD,-(2*365))
+15 SET %=$$LASTITEM^APCLAPIU(P,"HT","MEASUREMENT",CD,VD,"A")
+16 IF %=""
QUIT
+17 ;send back ht value^ht date
SET VALUE=$PIECE(%,U,3)_U_$PIECE(%,U,1)
End DoDot:1
QUIT VALUE
+18 ;UNDER 19 MUST BE ON SAME DATE AS WT
+19 SET %=$$LASTITEM^APCLAPIU(P,"HT","MEASUREMENT",VD,VD,"A")
+20 IF %=""
QUIT ""
+21 ;send back ht value^ht date
SET VALUE=$PIECE(%,U,3)_U_$PIECE(%,U,1)
+22 QUIT VALUE
+23 ;
HASVM(V,T,B) ;
+1 NEW Y,G
+2 SET Y=0
SET G=0
FOR
SET Y=$ORDER(^AUPNVMSR("AD",V,Y))
IF Y'=+Y!(G)
QUIT
Begin DoDot:1
+3 IF $$GET1^DIQ(9000010.01,Y,.01)'=T
QUIT
+4 IF $$GET1^DIQ(9000010.01,Y,.04)'=B
QUIT
+5 ;EIE
IF $PIECE($GET(^AUPNVMSR(Y,2)),U,1)
QUIT
+6 SET G=Y
End DoDot:1
+7 QUIT G
BMIP ;
+1 ;no routine to calculate
IF $TEXT(BMIPCT^BEHOVM2)=""
QUIT
+2 IF AGE<2
QUIT
+3 IF AGE>18
QUIT
+4 SET BMIPCT=$$BMIPCT^BEHOVM2(BMI,DFN,VD)
+5 IF BMIPCT'>0
QUIT
+6 ;store bmip
+7 ;already has this bmiP value on this visit so don't store it again, Q
IF $$HASVM(V,"BMIP",BMIPCT)
QUIT
+8 ;store BMIP as v meas
+9 DO STORE^BEHOVM5(.DATA,VTBMIP,BMIPCT,V,HD)
+10 QUIT
EIE(APCDX) ;EP - wt or ht entered in error, bmi eie
+1 IF '$GET(APCDX)
QUIT
+2 DO EN^XBNEW("EIE1^BEHOVM5","APCDX")
+3 KILL APCDX
+4 QUIT
EIE1 ;
+1 ;NEW A,B,C,D,E,P,V,VD,W,H,BMI,HD,ERR,APCLFDA,BIEN,X,Y,DA,VTBMI,VTBMIP
+2 SET VTBMI=$$VTYPE^BEHOVM("BMI")
SET VTBMIP=$$VTYPE^BEHOVM("BMIP")
+3 SET A=$$GET1^DIQ(9000010.01,APCDX,.01)
+4 ;only ht/wt
IF A'="HT"
IF A'="WT"
QUIT
+5 IF A="WT"
DO EIEW
QUIT
+6 IF A="HT"
DO EIEH
QUIT
+7 QUIT
EIEW ;WT ENTERED IN ERROR
+1 ;if no other wts deleted all bmi, bmip on this visit
+2 ;
+3 ;visit ien is in V
SET V=$$GET1^DIQ(9000010.01,APCDX,.03,"I")
+4 IF V=""
QUIT
+5 IF '$$HASAVM(V,"WT")
Begin DoDot:1
+6 ;find all bmi's and bmip's and mark them EIE
+7 SET APCDY=0
FOR
SET APCDY=$ORDER(^AUPNVMSR("AD",V,APCDY))
IF APCDY'=+APCDY
QUIT
Begin DoDot:2
+8 ;ALREADY EIE
IF $PIECE($GET(^AUPNVMSR(APCDY,2)),U,1)
QUIT
+9 SET T=$$GET1^DIQ(9000010.01,APCDY,.01)
+10 IF T'="BMI"
IF T'="BMIP"
QUIT
+11 ;mark as EIE
+12 DO FILEEIE(APCDY)
End DoDot:2
End DoDot:1
QUIT
+13 ;WHAT IF THERE IS ALREADY A WT SO NEED TO DELETE THE CORRECT BMI SO FIND BMI/BMIP AND DELETE
+14 ;wt value is in B
SET W=$$GET1^DIQ(9000010.01,APCDX,.04)
+15 IF W=""
QUIT
+16 ;patient dfn is in P
SET DFN=$$GET1^DIQ(9000010.01,APCDX,.02,"I")
+17 IF DFN=""
QUIT
+18 ;visit ien is in V
SET V=$$GET1^DIQ(9000010.01,APCDX,.03,"I")
+19 IF V=""
QUIT
+20 ;visit date
SET VD=$$VD^APCLV(V)
+21 ;age of patient on visit date
SET AGE=$$AGE^AUPNPAT(DFN,VD)
+22 ;get last height entered that is allowable for age (same day for <18, last 5 YRS for 19-49, last 2 years for 50 AND OVER)
SET H=$$LASTHT(DFN,VD)
+23 ;no ht so don't calculate anything
IF H=""
QUIT
+24 SET HD=$PIECE(H,U,2)
+25 SET H=$PIECE(H,U)
+26 ;calc bmi
+27 SET W=W*.45359
SET H=(H*.0254)
SET H=(H*H)
SET BMI=(W/H)
+28 ;find bmi with this value and mark as EIE
+29 FOR
SET APCDZ=$$HASVM(V,"BMI",BMI)
IF 'APCDZ
QUIT
DO FILEEIE(APCDZ)
+30 ;now find bmip
+31 ;no routine to calculate
IF $TEXT(BMIPCT^BEHOVM2)=""
QUIT
+32 IF AGE<2
QUIT
+33 IF AGE>18
QUIT
+34 SET BMIPCT=$$BMIPCT^BEHOVM2(BMI,DFN,VD)
+35 IF BMIPCT'>0
QUIT
+36 FOR
SET APCDZ=$$HASVM(V,"BMIP",BMIPCT)
IF 'APCDZ
QUIT
DO FILEEIE(APCDZ)
+37 QUIT
EIEH ;
+1 ;IF HT ENTERED IN ERROR, DELETE ALL BMIS AND BMIPS UNTIL FIND ANOTHER HT, MARK THEM ALL EIE
+2 ;patient dfn is in P
SET DFN=$$GET1^DIQ(9000010.01,APCDX,.02,"I")
+3 IF DFN=""
QUIT
+4 ;visit ien is in V
SET V=$$GET1^DIQ(9000010.01,APCDX,.03,"I")
+5 IF V=""
QUIT
+6 SET HD=$$VD^APCLV(V)
+7 SET AGE=$$AGE^AUPNPAT(DFN,$$VD^APCLV(V))
+8 KILL APCDVAR
+9 IF AGE>18
IF AGE<50
SET E=$$FMADD^XLFDT($$VD^APCLV(V),(5*365))
+10 IF AGE>49
SET E=$$FMADD^XLFDT($$VD^APCLV(V),(2*365))
+11 IF AGE<19
SET E=$$VD^APCLV(V)
+12 DO ALLV^APCLAPIU(DFN,$$VD^APCLV(V),E,"APCDVAR")
+13 ;REORDER BY DATE LOWEST TO HIGHEST
+14 SET APCDSTOP=""
+15 SET X=0
FOR
SET X=$ORDER(APCDVAR(X))
IF X'=+X
QUIT
Begin DoDot:1
+16 SET N=$PIECE(APCDVAR(X),U,5)
+17 SET APCDVAR("LH",$$VDTM^APCLV(N),X)=APCDVAR(X)
End DoDot:1
+18 SET D=0
FOR
SET D=$ORDER(APCDVAR("LH",D))
IF D'=+D
QUIT
Begin DoDot:1
+19 SET X=0
FOR
SET X=$ORDER(APCDVAR("LH",D,X))
IF X'=+X
QUIT
Begin DoDot:2
+20 SET N=$PIECE(APCDVAR("LH",D,X),U,5)
+21 ;BEFORE MY VISIT, DON'T DEAL WITH IT
IF $$VDTM^APCLV(N)<$$VDTM^APCLV(V)
KILL APCDVAR("LH",D,X)
+22 ;no wts so don't bother, can't calculate bmi
IF '$$HASAVM(N,"WT")
KILL APCDVAR("LH",D,X)
+23 ;KILL OFF ALL REMAINING
IF $$HASAVM(N,"HT")
IF N'=V
SET A=D
SET B=X
Begin DoDot:3
+24 FOR
SET A=$ORDER(APCDVAR("LH",A))
IF A'=+A
QUIT
FOR
SET B=$ORDER(APCDVAR("LH",A,B))
IF B'=+B
QUIT
KILL APCDVAR("LH",A,B)
End DoDot:3
End DoDot:2
End DoDot:1
+25 ;nowDELETE bmi/BMIP on this array of visits
+26 SET D=0
FOR
SET D=$ORDER(APCDVAR("LH",D))
IF D=""
QUIT
Begin DoDot:1
+27 SET X=0
FOR
SET X=$ORDER(APCDVAR("LH",D,X))
IF X=""
QUIT
Begin DoDot:2
+28 ;visit ien
SET N=$PIECE(APCDVAR(X),U,5)
+29 SET APCDAGE=$$AGE^AUPNPAT(DFN,$$VD^APCLV(N))
+30 ;delete all bmis and bmips
+31 FOR
SET APCDZ=$$HASAVM(N,"BMI")
IF 'APCDZ
QUIT
DO FILEEIE(APCDZ)
+32 FOR
SET APCDZ=$$HASAVM(N,"BMIP")
IF 'APCDZ
QUIT
DO FILEEIE(APCDZ)
+33 ;NOW ADD NEW BMI/BMIP WITH HT PREVIOUS TO THE ONE DELETED, IF WE CAN
+34 SET APCDA=0
FOR
SET APCDA=$ORDER(^AUPNVMSR("AD",N,APCDA))
IF APCDA'=+APCDA
QUIT
Begin DoDot:3
+35 IF $PIECE($GET(^AUPNVMSR(APCDA,2)),U,1)
QUIT
+36 IF $$VAL^XBDIQ1(9000010.01,APCDA,.01)'="WT"
QUIT
+37 SET W=$$VAL^XBDIQ1(9000010.01,APCDA,.04)
+38 ;calc bmi
+39 ;get last height entered that is allowable for age (same day for <18, last 5 YRS for 19-49, last 2 years for 50 AND OVER)
SET H=$$LASTHT(DFN,$$VD^APCLV(N))
+40 ;no ht so don't calculate anything
IF H=""
QUIT
+41 SET HD=$PIECE(H,U,2)
+42 SET H=$PIECE(H,U)
+43 ;calc bmi
+44 SET W=W*.45359
SET H=(H*.0254)
SET H=(H*H)
SET BMI=(W/H)
+45 IF '$$HASVM(V,"BMI",BMI)
DO STORE^BEHOVM5(.DATA,VTBMI,BMI,V,HD)
+46 ;no routine to calculate
IF $TEXT(BMIPCT^BEHOVM2)=""
QUIT
+47 IF APCDAGE<2
QUIT
+48 IF APCDAGE>18
QUIT
+49 SET BMIPCT=$$BMIPCT^BEHOVM2(BMI,DFN,$$VD^APCLV(N))
+50 IF BMIPCT'>0
QUIT
+51 ;store bmip
+52 ;already has this bmiP value on this visit so don't store it again, Q
IF $$HASVM(N,"BMIP",BMIPCT)
QUIT
+53 ;store BMIP as v meas
+54 DO STORE^BEHOVM5(.DATA,VTBMIP,BMIPCT,V,HD)
End DoDot:3
End DoDot:2
End DoDot:1
+55 QUIT
FILEEIE(APCDY) ;
+1 IF '$GET(APCDY)
QUIT
+2 IF '$DATA(^AUPNVMSR(APCDY))
QUIT
+3 NEW APCDIENS,APCDFDA,APCDERR,DA,DIK
+4 SET APCDIENS=APCDY_","
+5 SET APCDFDA(9000010.01,APCDIENS,2)=1
+6 SET APCDFDA(9000010.01,APCDIENS,3)=DUZ
+7 ;S APCDFDA(9000010.014,"+1,"_APCDIENS,.01)=$P(BEHDATA,"^",3)
+8 DO UPDATE^DIE("","APCDFDA","APCDIEN","APCDERR")
+9 ;NOW MERGE OVER THE RESONS FROM THE OTHER ENTRY
+10 MERGE ^AUPNVMSR(APCDY,2.1)=^AUPNVMSR(APCDX,2.1)
+11 ;REINDEX
+12 SET DA=APCDY
SET DIK="^AUPNVMSR("
DO IX^DIK
KILL DA,DIK
+13 QUIT
+14 ;
HASAVM(V,T) ;
+1 NEW Y,G
+2 SET Y=0
SET G=0
FOR
SET Y=$ORDER(^AUPNVMSR("AD",V,Y))
IF Y'=+Y!(G)
QUIT
Begin DoDot:1
+3 IF $$GET1^DIQ(9000010.01,Y,.01)'=T
QUIT
+4 ;EIE
IF $PIECE($GET(^AUPNVMSR(Y,2)),U,1)&(Y'=APCDX)
QUIT
+5 SET G=Y
End DoDot:1
+6 QUIT G