Home   Package List   Routine Alphabetical List   Global Alphabetical List   FileMan Files List   FileMan Sub-Files List   Package Component Lists   Package-Namespace Mapping  
Routine: APCHS9M3

APCHS9M3.m

Go to the documentation of this file.
APCHS9M3 ; IHS/CMI/LAB - MEDICATION TURN AROUND SUPPLEMENT ;
 ;;2.0;IHS PCC SUITE;**7**;MAY 14, 2009
 ;
  ;
EP ;EP - called from component
 Q:'$G(APCHSPAT)
 Q:'$D(^AUPNVMED("AC",APCHSPAT))
 I $E(IOST)="C",IO=IO(0) W !! S DIR("A")="CHRONIC MEDICATION RE-ORDER DOCUMENT (^ TO EXIT, RETURN TO CONTINUE)",DIR(0)="E" D ^DIR K DIR I $D(DIRUT) S APCHSQIT=1 Q  ;IHS/CMI/LAB fixed for slave printing
WR ;write out array
 K APCHQUIT
 S APCHPAGE=0,APCHQUIT=0
 D EP2  ;write out document
 I APCHQUIT S APCHSQIT=1
 D EOJ
 Q
 ;
EOJ ;
 K APCHCLN,APCHDATM,APCHDIEN,APCHIPRV,APCHMIEN,APCHNARC,APCHPAGE,APCHPROV,APCHQUIT,APCHRTN,APCHTXRF,APCHSBEG,APCHSCRN,APCHSDC,APCHSDLM,APCHSDOO,APCHSDTM,APCHSDYS,APCHSED,APCHSEXP
 K APCHSICL,APCHSIG,APCHSITE,APCHSIVD,APCHSM0,APCHSNAM,APCHSNRQ,APCHSP,APCHSQTY,APCHSREF,APCHSRFL,APCHSRX,APCHSSGY,APCHSTEX,APCHSTOB,APCHSTOP,APCHSTXT,APCHSUPI,APCHTC,APCHTILN,APCHTOB,APCHTOBN,APCHTQ,APCHTYPE,APCHVIEN,APCHX,APCHXRX,APCHY
 K DIR,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,V,X,Y,Z,W,X1,X2
 Q
 G:APCHPAGE=0 HEAD1
 K DIR I $E(IOST)="C",IO=IO(0) W ! S DIR(0)="EO" D ^DIR K DIR I Y=0!(Y="^")!($D(DTOUT)) S APCHQUIT=1 Q
HEAD1 ;
 S APCHPAGE=APCHPAGE+1
 W:$D(IOF) @IOF
 W !,APCHSHDR,!
 W !,"MEDICATION REORDER DOCUMENT                Date: "_$$FMTE^XLFDT(DT)_"   Page: "_APCHPAGE
 W !,"Patient: ",$P(^DPT(APCHSPAT,0),U),"  HRN: ",$$HRN^AUPNPAT(APCHSPAT,DUZ(2)),!
 Q
EP2 ;PEP - PASS DFN get back array of patient care summary
 ;at this point you are stuck with ^TMP("APCHS",$J,"DCS"
 K ^TMP($J,"APCHMEDS")
 K ^TMP($J,"APCHCOUNT")
 K ^TMP($J,"APCHMEDSA")
 K ^TMP($J,"APCHMEDSG")
 S APCHSDLM=""
 I $G(APCHSTYP) S APCHSDLM=$P(^APCHSCTL(APCHSTYP,12,APCHSFOR,0),U,3)
 I APCHSDLM="" S APCHSDLM="1Y"
 I APCHSDLM?1N.N!(APCHSDLM?1N.N1"D") S APCHSDLS=+APCHSDLM_" day"
 S:APCHSDLM?1N.N1"M" APCHSDLS=+APCHSDLM_" month",APCHSDLM=+APCHSDLM*30
 S:APCHSDLM?1N.N1"Y" APCHSDLS=+APCHSDLM_" year",APCHSDLM=+APCHSDLM*365
 S APCHSDLM=+APCHSDLM
 S:+APCHSDLS>1 APCHSDLS=APCHSDLS_"s"
 K APCHSDLS
 I APCHSDLM'>0 S APCHSDLM=9999999
 E  S X1=DT,X2=-APCHSDLM D C^%DTC S APCHSDLM=9999999-X K X1,X2
 S APCHSMSD=$$FMTE^XLFDT(X),APCHSMSI=X
 D SETARRAY
 Q
SETARRAY ;set up array containing dm care summary
 D GETMEDS
 Q
GETMEDS ;
 ;gather up in ^TMP all meds in past year that are current or Chronic
 S APCHSTOP=$$FMADD^XLFDT(APCHSMSI,-1030)
 S APCHSTOP=9999999-APCHSTOP
 Q:'$D(^AUPNVMED("AC",APCHSPAT))  ;patient has no meds
 S APCHSIVD=0 F  S APCHSIVD=$O(^AUPNVMED("AA",APCHSPAT,APCHSIVD)) Q:APCHSIVD=""!(APCHSIVD>APCHSTOP)  D
 .S APCHMIEN=0 F  S APCHMIEN=$O(^AUPNVMED("AA",APCHSPAT,APCHSIVD,APCHMIEN)) Q:APCHMIEN'=+APCHMIEN  D
 ..S APCHSM0=^AUPNVMED(APCHMIEN,0)
 ..Q:$P(APCHSM0,U)=""
 ..Q:'$D(^PSDRUG($P(APCHSM0,U),0))
 ..S APCHDATM=9999999-$P(APCHSIVD,".")
 ..S APCHSDC=$P(APCHSM0,U,8)
 ..S APCHSDYS=$P(APCHSM0,U,7) S:APCHSDYS=0 APCHSDYS=30
 ..S APCHSNAM=$S($P(APCHSM0,U,4)]"":$P(APCHSM0,U,4),1:$P(^PSDRUG($P(APCHSM0,U),0),U))
 ..S APCHDIEN=$P(APCHSM0,U,1)
 ..S $P(^TMP($J,"APCHCOUNT",APCHSNAM,APCHDIEN),U)=$P($G(^TMP($J,"APCHCOUNT",APCHSNAM,APCHDIEN)),U)+1
 ..S X=$P(^TMP($J,"APCHCOUNT",APCHSNAM,APCHDIEN),U)
 ..I X<10 S $P(^TMP($J,"APCHCOUNT",APCHSNAM,APCHDIEN),U,(X+1))=$$DATE((9999999-APCHSIVD))
 ..I $$CHRONIC(APCHMIEN) D  Q
 ...Q:$D(^TMP($J,"APCHMEDSG","CHRONIC",APCHSNAM,APCHDIEN))  ;already have this one
 ...S ^TMP($J,"APCHMEDS","CHRONIC",APCHSIVD,APCHSNAM,APCHDIEN,APCHMIEN)="",^TMP($J,"APCHMEDSG","CHRONIC",APCHSNAM,APCHDIEN)=""
 ..;S X=$$FMDIFF^XLFDT(DT,APCHDATM) Q:X>60&(X>(2*APCHSDYS))
 ..Q:$D(^TMP($J,"APCHMEDSG","CURRENT",APCHSNAM,APCHDIEN))  ;already have this one
 ..S ^TMP($J,"APCHMEDS","CURRENT",APCHSIVD,APCHSNAM,APCHDIEN,APCHMIEN)="",^TMP($J,"APCHMEDSG","CURRENT",APCHSNAM,APCHDIEN)=""
 .Q
 I '$D(^TMP($J,"APCHMEDS","CURRENT")),'$D(^TMP($J,"APCHMEDS","CHRONIC")) Q  ;no meds to display
 ;NOW GET RID OF ALL DISCONTINUED MEDS BY .08 OF VMED OR BY STATUS IN FILE 52
 S I=0 F  S I=$O(^TMP($J,"APCHMEDS","CURRENT",I)) Q:I'=+I  S N="" F  S N=$O(^TMP($J,"APCHMEDS","CURRENT",I,N)) Q:N=""  S D=0 F  S D=$O(^TMP($J,"APCHMEDS","CURRENT",I,N,D)) Q:D'=+D  D
 .S M=0 F  S M=$O(^TMP($J,"APCHMEDS","CURRENT",I,N,D,M)) Q:M'=+M  I $$DC(M) K ^TMP($J,"APCHMEDS","CURRENT",I,N,D,M)
  S I=0 F  S I=$O(^TMP($J,"APCHMEDS","CHRONIC",I)) Q:I'=+I  S N="" F  S N=$O(^TMP($J,"APCHMEDS","CHRONIC",I,N)) Q:N=""  S D=0 F  S D=$O(^TMP($J,"APCHMEDS","CHRONIC",I,N,D)) Q:D'=+D  D
 .S M=0 F  S M=$O(^TMP($J,"APCHMEDS","CHRONIC",I,N,D,M)) Q:M'=+M  I $$DC(M) K ^TMP($J,"APCHMEDS","CHRONIC",I,N,D,M)
 ;S C=1,X=$O(^TMP($J,"APCHMEDS","CHRONIC",0)) D
 ;.I X="" S C=0 Q
 ;.S X=9999999-X I X<$$FMADD^XLFDT(DT,-366) S C=0
 ;S R=1,X=$O(^TMP($J,"APCHMEDS","CURRENT",0)) D
 ;.I X="" S R=0 Q
 ;.S X=9999999-X I X<$$FMADD^XLFDT(DT,-366) S R=0
 ;I 'C,'R Q  ;no meds in past year
 D HEADER
 ;K APCHSCRN S APCHTYPE="CURRENT" D MEDDSP  ;IHS/CMI/GRL suppress non-chronic meds
 S APCHSCRN=1 S APCHTYPE="CHRONIC" D MEDDSP
 I 'APCHQUIT W !!!,"DISPENSE UNTIL NEXT SCHEDULED EVALUATION: ________",!!,"SIGNATURE: ________________________________  DATE: ________________",!
 K ^TMP($J,"APCHMEDS")
 K ^TMP($J,"APCHCOUNT")
 K ^TMP($J,"APCHMEDSA")
 K ^TMP($J,"APCHMEDSG")
 Q
MEDDSP ;
 S X=""
 Q:'$D(^TMP($J,"APCHMEDS",APCHTYPE))  ;NO MEDS TO DISPLAY
 I APCHTYPE="CURRENT" S X="CURRENT MEDICATIONS - (TWICE DURATION OF RX - MINIMUM 60 DAYS)" D W(X,1)
 I APCHTYPE="CHRONIC" S X="LATEST OF EACH CHRONIC MEDICATION DISPENSED IN THE PAST YEAR" D W(X,1)
 ;S APCHSTOP=$$FMADD^XLFDT(DT,-366),APCHSTOP=9999999-APCHSTOP
 S APCHSIVD=0 F  S APCHSIVD=$O(^TMP($J,"APCHMEDS",APCHTYPE,APCHSIVD)) Q:APCHSIVD'=+APCHSIVD!(APCHSIVD>APCHSDLM)!(APCHQUIT)  D
 .S APCHSNAM="" F  S APCHSNAM=$O(^TMP($J,"APCHMEDS",APCHTYPE,APCHSIVD,APCHSNAM)) Q:APCHSNAM=""!(APCHQUIT)  D
 ..S APCHDIEN=0 F  S APCHDIEN=$O(^TMP($J,"APCHMEDS",APCHTYPE,APCHSIVD,APCHSNAM,APCHDIEN)) Q:APCHDIEN'=+APCHDIEN!(APCHQUIT)  D
 ...S APCHMIEN=0 S APCHMIEN=$O(^TMP($J,"APCHMEDS",APCHTYPE,APCHSIVD,APCHSNAM,APCHDIEN,APCHMIEN)) D MEDDSP1
 ...Q
 ..Q
 .Q
 Q
MEDDSP1 ;
 S APCHSM0=^AUPNVMED(APCHMIEN,0)
 S APCHSRX=$O(^PSRX("APCC",APCHMIEN,0)) I 'APCHSRX S APCHXRX=0
 S APCHRXRF=""
 I APCHSRX S APCHRXRF=$O(^PSRX("APCC",APCHMIEN,APCHSRX,"")) S:APCHRXRF="" APCHRXRF=0
 S (Y,APCHSDTM)=9999999-$P(APCHSIVD,".")
 ;S APCHSDTM=$$DATE(APCHSDTM)
 S APCHSDC=$P(APCHSM0,U,8)
 S APCHSDYS=$P(APCHSM0,U,7) S:APCHSDYS=0 APCHSDYS=30
 S APCHSNAM=$S($P(APCHSM0,U,4)]"":$P(APCHSM0,U,4),1:$P(^PSDRUG($P(APCHSM0,U),0),U))
 S APCHSQTY=$P(APCHSM0,U,6)
 S APCHSIG=$P(APCHSM0,U,5)
 S APCHVIEN=$P(APCHSM0,U,3)
 S APCHDIEN=+APCHSM0
 S X1=DT,X2=APCHSDTM D ^%DTC
 S APCHSEXP=""
 I X>APCHSDYS S X1=APCHSDTM,X2=APCHSDYS D C^%DTC S APCHSEXP="-- Ran out "_$$DATE(X)
 S APCHNARC=+$P(^PSDRUG(APCHDIEN,0),U,3)
 ;ADDED RETURNED TO STOCK FLAG FOR NEXT 4 LINES IHS/OKCAO/POC 12/19/97
 IF $P($G(^AUPNVMED(APCHMIEN,11)),U)="RETURNED TO STOCK" S APCHRTN="R"
 I APCHSDC,$G(APCHRTN)="R" S Y=APCHSDC X APCHSCVD S APCHSEXP="--Returned to Stock "_Y
 I APCHSDC,$G(APCHRTN)="" S Y=APCHSDC X APCHSCVD S APCHSEXP="-- D/C "_Y
 K APCHRTN
 D SIG S APCHSIG=APCHSSGY
 D REF
 I APCHSREF S APCHSREF=APCHSREF_$S(APCHSREF=1:" refill",1:" refills")_" left."
 S APCHSITE=$P(^AUPNVSIT(APCHVIEN,0),U,6)
 D PROV
 D CLN
 D SITE
 D WRITE
 Q
DC(V) ;is this d/c'ed
 I '$D(^AUPNVMED(V,0)) Q 0
 I $P(^AUPNVMED(V,0),U,8)]"" Q 1
 NEW P,S
 S P=$S($D(^PSRX("APCC",N)):$O(^(N,0)),1:0)
 I 'P Q 0
 S X=$P($G(^PSRX(P,0)),U,15)
 I X=12 Q 1
 I X=13 Q 1
 I X=14 Q 1
 I X=15 Q 1
 S X=$P($G(^PSRX(P,"STA")),U,1)
 I X=12 Q 1
 I X=13 Q 1
 I X=14 Q 1
 I X=15 Q 1
 Q 0
WRITE ;
 I $Y>(IOSL-10) D  Q:APCHQUIT
 .W !!!,"DISPENSE UNTIL NEXT SCHEDULED EVALUATION: ________"
 .W !!!,"SIGNATURE: ________________________________  DATE: ________________",!
 .D HEADER
 Q:APCHQUIT
 S X=$$DATE(APCHSDTM),$E(X,10)=APCHSNAM_"  "_APCHSEXP D W(X,1)
 S X="",$E(X,10)="QTY: "_APCHSQTY_"   ("_APCHSDYS_" days)   "_APCHPROV_$S(APCHCLN]"":" - "_APCHCLN,1:"") D W(X)
 S APCHSICL=10,APCHSNRQ="",APCHSTXT=APCHSIG D PRTTXT K APCHSICL,APCHSNRQ,APCHSP
 ;ISSUE HISTORY
 ;I APCHSRX,$D(^PSRX(APCHSRX,0)) S X="",$E(X,10)="Most recent issue date:  "_$$FMTE^XLFDT($P(^PSRX(APCHSRX,0),U,13),5) D W(X)
 I APCHSITE]"" S X="",$E(X,10)="Dispensed at: "_APCHSITE D W(X)
 S X=""
 S Y=$P(^TMP($J,"APCHCOUNT",APCHSNAM,APCHDIEN),U)
 I Y>1 S X="" D
 .S X="",$E(X,10)="Previously filled: " F I=3:1:5 S X=X_" "_$P(^TMP($J,"APCHCOUNT",APCHSNAM,APCHDIEN),U,I)
 I X]"" D W(X)
 I APCHSREF S X="",$E(X,10)=APCHSREF D W(X)
 S X=""
 I $G(APCHNARC)=2 S $E(X,10)="------- MUST REWRITE SCHEDULE 2 DRUGS -------" D W(X,1) Q
 I $G(APCHNARC)>2,$G(APCHNARC)<6 S $E(X,10)="RENEW __________ QTY __________ DC __________" D W(X,1) Q
 S $E(X,10)="RENEW __________  DC __________ " D W(X,1)
 Q
 ;
SITE ;DETERMINE IF OUTSIDE LOCATION INFO PRESENT
 S APCHSITE=""
 I $D(^AUPNVSIT(APCHVIEN,21))#2 S APCHSITE=$P(^(21),U) Q
 Q:$P(^AUPNVSIT(APCHVIEN,0),U,6)=""
 I $P(^AUPNVSIT(APCHVIEN,0),U,6)'=DUZ(2) S APCHSITE=$E($P(^DIC(4,$P(^AUPNVSIT(APCHVIEN,0),U,6),0),U),1,30)
 Q
 ;
SIG ;
 ;CONSTRUCT THE FULL TEXT FROM THE ENCODED SIG
 I $$VALI^XBDIQ1(9001015,APCHSTYP,3.5)="S" S APCHSSGY=APCHSIG Q
 S APCHSSGY="" F APCHSP=1:1:$L(APCHSIG," ") S X=$P(APCHSIG," ",APCHSP) I X]"" D
 . S Y=$O(^PS(51,"B",X,0)) I Y>0 S X=$P(^PS(51,Y,0),"^",2) I $D(^(9)) S Y=$P(APCHSIG," ",APCHSP-1),Y=$E(Y,$L(Y)) S:Y>1 X=$P(^(9),"^",1)
 . S APCHSSGY=APCHSSGY_X_" "
 Q
 ;
REF ;
 ;DETERMINE THE NUMBER OF REFILLS REMAINING
 I 'APCHSRX S APCHSREF=0 Q
 S APCHSRFL=$P(^PSRX(APCHSRX,0),U,9) S APCHSREF=0 F  S APCHSREF=$O(^PSRX(APCHSRX,1,APCHSREF)) Q:'APCHSREF  S APCHSRFL=APCHSRFL-1
 S APCHSREF=APCHSRFL
 Q
 ;
PROV ;GET THE PROVIDER FOR ORIGINAL RX OR REFILL
 S APCHPROV=""
 I 'APCHSRX S APCHPROV=$$VAL^XBDIQ1(9000010.14,1202,APCHMIEN) Q
 S APCHIPRV=$S(APCHRXRF=0:$P($G(^PSRX(APCHSRX,0)),"^",4),1:$P($G(^PSRX(APCHSRX,1,APCHRXRF,0)),"^",17))
 S APCHPROV=$S('APCHIPRV:"UNKNOWN PROVIDER",1:$P(^VA(200,APCHIPRV,0),"^"))
 Q
 ;
CLN ;GET CLINIC FOR V MEDICATION
 S APCHCLN=$E($$VAL^XBDIQ1(9000010,APCHVIEN,.08),1,10)
 Q
DATE(D) ;EP
 I D="" Q ""
 Q $E(D,4,5)_"/"_$E(D,6,7)_"/"_$E(D,2,3)
 ;
CHRONIC(N) ;
 I '$G(N) Q ""
 I '$D(^AUPNVMED(N)) Q ""
 NEW X,Y,P
 S P=$P(^AUPNVMED(N,0),U,2)
 S X=$S($D(^PSRX("APCC",N)):$O(^(N,0)),1:0)
 S Y=$S(+X:$D(^PS(55,P,"P","CP",X)),1:0)
 I 'Y Q ""
 Q 1
W(Y,F,C,T) ;set up array
 I '$G(F) S F=0
 I '$G(T) S T=0
 NEW %,X
 ;blank lines
 F F=1:1:F S X="" W !,X
 S X=Y
 I $G(C) S L=$L(Y),T=(80-L)/2 D  W !,X Q
 .F %=1:1:(T-1) S X=" "_X
 F %=1:1:T S X=" "_Y
 W !,X
 Q
PRTTXT ; GENERALIZED TEXT PRINTER
 S APCHTDLT=1,APCHTILN=80-APCHSICL-1
 F APCHTQ=0:0 S:APCHSNRQ]""&(($L(APCHSNRQ)+$L(APCHSTXT)+2)<255) APCHSTXT=$S(APCHSTXT]"":APCHSTXT_"; ",1:"")_APCHSNRQ,APCHSNRQ="" Q:APCHSTXT=""  D PRTTXT2
 K APCHTILN,APCHTDLT,APCHTF,APCHTC,APCHSTXT,APCHSDOO
 Q
PRTTXT2 D GETFRAG S X="",$E(X,APCHSICL)=APCHTF D W(X) S APCHSICL=APCHSICL+APCHTDLT,APCHTILN=APCHTILN-APCHTDLT,APCHTDLT=0
 Q
GETFRAG I $L(APCHSTXT)<APCHTILN S APCHTF=APCHSTXT,APCHSTXT="" Q
 F APCHTC=APCHTILN:-1:1 Q:$E(APCHSTXT,APCHTC)=" "
 S APCHTF=$E(APCHSTXT,1,APCHTC-1),APCHSTXT=$E(APCHSTXT,APCHTC+1,255)
 Q
 ;