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

PSOSIG.m

Go to the documentation of this file.
PSOSIG ;BIR/RTR-Utility to create SIG ;Feb 25, 2021@14:48
 ;;7.0;OUTPATIENT PHARMACY;**46,99,114,391,313,282,455,446,402,500,515,514,574,584,449**;DEC 1997;Build 2
 ;External reference to PS(51 supported by DBIA 2224
 ;External reference to PS(51.1 supported by DBIA 2225
 ;External reference to PSDRUG( supported by DBIA 221
 ;
EN(PSOSIGX) ;
 N VARIABLE
 Q
SCH ;*282 Preserve old functionality
 I $D(DTOUT)!($D(DUOUT)) Q
 I Y>0!($G(SCH)[" ") S SCHEX=$$SCHE(SCH)
 I Y'>0 W !,?2,"Free text '"_$G(SCH)_"' entered for schedule"
 Q
 ;
 ;SCHE is the new schedule expander
SCHE(SCH) ;
 ;SCH = Schedule entered
 ;Returns Expanded Schedule, or ""
 N SCHEX,SPCT
 I SCH[""""!($A(SCH)=45)!(SCH?.E1C.E)!($L(SCH," ")>$S(SCH["PRN":4,1:3))!($L(SCH)>20)!($L(SCH)<1) K SCH Q ""
 S SCH=$$UPPER(SCH)
 S SPCT=0 F I=1:1:$L(SCH) I $E(SCH,I)=" " S SPCT=SPCT+1
 S SCHEX=$$EXP(SCH) I SCHEX]"" Q SCHEX
 Q:SPCT=0 SCH
 Q $$SCHE($P(SCH," ",1,SPCT))_" "_$$SCHE($P(SCH," ",SPCT+1))
EXP(X) ; expand based on 51.1 and 51
 N PSIN,SCFLG,SCHEX
 S PSIN=0 F  S PSIN=$O(^PS(51.1,"APPSJ",X,PSIN)) Q:'PSIN!$G(SCFLG)  I $P(^PS(51.1,PSIN,0),"^",8)'="" S SCHEX=$P($G(^(0)),"^",8),SCFLG=1
 Q:$G(SCFLG) SCHEX
 S PSIN=0 F  S PSIN=$O(^PS(51.1,"D",X,PSIN)) Q:'PSIN!$G(SCFLG)  I $P(^PS(51.1,PSIN,0),"^",8)'="" S SCHEX=$P($G(^(0)),"^",8),SCFLG=1
 Q:$G(SCFLG) SCHEX
 S PSIN=0 F  S PSIN=$O(^PS(51,"B",X,PSIN)) Q:'PSIN!$G(SCLFL)  I PSIN,($P(^PS(51,PSIN,0),"^",4)<2)&($P($G(^PS(51,"A",X)),"^")'="") S SCHEX=$P(^(X),"^"),SCFLG=1
 Q:$G(SCFLG) SCHEX
 S PSIN=0 F  S PSIN=$O(^PS(51,"D",X,PSIN)) Q:'PSIN!$G(SCLFL)  I PSIN,($P(^PS(51,PSIN,0),"^",4)<2)&($P($G(^PS(51,"A",X)),"^")'="") S SCHEX=$P(^(X),"^"),SCFLG=1
 Q:$G(SCFLG) SCHEX
 I X[" " Q ""
 ; expand DOW ;p449
 N DOW,VAL,VAL1,STR,I,Y,XX,YY,TM
 S DOW="SUNDAY;MONDAY;TUESDAY;WEDNESDAY;THURSDAY;FRIDAY;SATURDAY",(STR,SCHEX)="",TM=$P(X,"@",2)
 F I=1:1:$L(X)+1 S VAL=$E(X,I) S:VAL?1AN STR=STR_VAL I VAL'?1AN  D  I VAL="@" Q
 .Q:STR=""
 .F J=1:1:7 S VAL1=$P(DOW,";",J),XX=0 I STR'="",(";"_VAL1)[(";"_STR) S YY=$P(SCHEX," ",$L(SCHEX," ")),SCHEX=$S(SCHEX="":"",DOW'[YY:SCHEX_" ",1:SCHEX_", ")_VAL1,STR="",XX=1 Q
 .I 'XX,SCHEX'="" S YY=$P(SCHEX," ",$L(SCHEX," ")),SCHEX=$S(SCHEX="":"",DOW'[YY:SCHEX_" ",1:SCHEX_" ")_STR,STR=""
 I SCHEX="" Q ""
 S XX=$L(SCHEX,", ") I XX>1 S STR=$P(SCHEX,", ",1,XX-1)_" AND "_$P(SCHEX,", ",XX),SCHEX=STR
 I SCHEX=X Q ""
 Q SCHEX_$S(TM'="":"@",1:"")_TM
 ;
QTY(PSOQX) ; PSOQX - Array containing Rx information
 N QDOSE,PSORXIEN,PSODSEDT,PSOOUTQT
 K PSOQX("QTY")
 S PSORXIEN=$S($G(PSOQX("IRXN")):+PSOQX("IRXN"),1:0)
 S PSODSEDT=$S(PSORXIEN&$D(PSOQX(52,PSORXIEN,8)):1,'PSORXIEN&($G(PSOQX("FLD"))=8):1,1:0)  ; DAYS SUPPLY field edited
 I 'PSORXIEN,$G(PSOQX("FLD"))=7 Q   ; QTY field being edite for Pending Order (do not ajust current QTY)
 S PSOOUTQT=1
QTYCP ;CPRS qty call comes through here
 N PSQQUIT,QTSH,PSQ,PSQMIN,PSQMINZ,PSOQRND,PSOLOWER,PSOLOWX,PSOLOWXL,PSOLOWST,PSORXDRG
 K PSOFRQ S PSQQUIT=0
 I '$G(PSOCPRQT) S QDOSE=0 F PSQ=0:0 S PSQ=$O(PSOQX("DOSE",PSQ)) Q:'PSQ  S QDOSE=PSQ S:'$G(PSOQX("DOSE ORDERED",PSQ)) PSQQUIT=1
 I '$G(PSOCPRQT) Q:PSQQUIT
 Q:'$G(PSOQX("DAYS SUPPLY"))
 Q:'$G(QDOSE)
 G:QDOSE>1 COMP
 Q:'$G(PSOQX("DOSE ORDERED",1))
 Q:'$G(PSOQX("DAYS SUPPLY"))&('$G(PSOQX("DURATION",1)))
 S PSOLOWER=0
 I $G(PSOQX("DURATION",1)) D
 .S PSOLOWX=$L(PSOQX("DURATION",1))
 .S PSOLOWXL=$S($E(PSOQX("DURATION",1),PSOLOWX)="M":1,$E(PSOQX("DURATION",1),PSOLOWX)="H":60,$E(PSOQX("DURATION",1),PSOLOWX)="S":.01666,$E(PSOQX("DURATION",1),PSOLOWX)="W":10080,$E(PSOQX("DURATION",1),PSOLOWX)="L":43200,1:1440)
 .S PSOLOWER=PSOLOWXL*(+$G(PSOQX("DURATION",1)))
 S PSOLOWX=0 I +$G(PSOQX("DAYS SUPPLY")) S PSOLOWX=1440*+$G(PSOQX("DAYS SUPPLY"))
 Q:'$G(PSOLOWER)&('$G(PSOLOWX))
 S QTSH=$G(PSOQX("SCHEDULE",1)) D QTS Q:PSQQUIT!('$G(PSOFRQ))
 S PSOLOWST=$S('$G(PSOLOWER):$G(PSOLOWX),'$G(PSOLOWX):$G(PSOLOWER),$G(PSOLOWER)>$G(PSOLOWX):$G(PSOLOWX),$G(PSOLOWX)>$G(PSOLOWER):$G(PSOLOWER),$G(PSOLOWX)=$G(PSOLOWER):$G(PSOLOWER),1:0)
 Q:'$G(PSOLOWST)
 S PSQMIN=+$G(PSOLOWST)
 S PSQMINZ=PSQMIN/PSOFRQ
 S PSOQRND=PSQMINZ*+$G(PSOQX("DOSE ORDERED",1)) Q:'PSOQRND
 ;If DAYS SUPPLY was edited by Pharmacy and previous quantity was calculated, don't calculate/update quantity automatically
 I $G(PSODSEDT),'$$UPDQTY(PSOQRND+.9999\1) Q
 ;If DISPENSE DRUG was edited in Pharmacy and SIG contains "PRN" on a CS Order, don't calculate/update quantity automatically
 S PSORXDRG=$S($G(PSORXIEN):$P($G(PSORXED("RX0")),"^",6),1:$P($G(OR0),"^",9))
 I $D(PSORXIEN),PSORXDRG,PSORXDRG'=+$G(PSODRUG("IEN")),$G(QTYHLD),$$CSDS^PSOSIGDS(+$G(PSODRUG("IEN"))),$$PRN() D  Q
 . W !!,"The Quantity (",$G(QTYHLD),") has not been changed."
 . W !,"Please review and update it if necessary.",!,$C(7)
 . N DIR S DIR(0)="E",DIR("A")="Press Return to Continue" D ^DIR W !
 D ROUND G QEND
 Q
COMP ;COMPLEX DOSE HERE
 N PSOQEXC,PSOQAND,PSOQTHEN,PSQL,PSQMNL,PSQMNLX,PSODUTOT,PSODUDIF,PSODUMIS,PSODUREP,PSQ1,PSODUX,PSODUXX,PSODSMIN
 ;PSODUTOT = TOTAL OF ALL DURATIONS
 ;PSODUDIF = DIFF. OF DURATION VS DAYS SUPPLY
 ;PSODUMIS = # OF SEQUENCES MISSING A DURATION IF >1 CAN'T DEFAULT
 ;PSODUREP = SEQUENCE # THAT IS MISSING DURATION
 ;PSODSMIN = MINUTES OF DAYS SUPPLY
 S (PSODUTOT,PSODUDIF,PSODUMIS,PSODUREP,PSODSMIN,PSOQAND,PSOQTHEN,PSOQEXC)=0
 F PSQL=1:1:QDOSE S:$G(PSOQX("CONJUNCTION",PSQL))["A" PSOQAND=1 S:$G(PSOQX("CONJUNCTION",PSQL))["T" PSOQTHEN=1 S:$G(PSOQX("CONJUNCTION",PSQL))["X" PSOQEXC=1
 I $G(PSOQEXC) G QEND
 I $G(PSOQAND) G COMP^PSOSIGCX
 F PSQ1=1:1:QDOSE D  Q:PSODUMIS>1
 .I '$G(PSOQX("DURATION",PSQ1)) S PSODUMIS=PSODUMIS+1,PSODUREP=PSQ1 Q
 .S PSODUX=$L(PSOQX("DURATION",PSQ1))
 .S PSODUXX=$S($E(PSOQX("DURATION",PSQ1),PSODUX)="M":1,$E(PSOQX("DURATION",PSQ1),PSODUX)="H":60,$E(PSOQX("DURATION",PSQ1),PSODUX)="S":.01666,$E(PSOQX("DURATION",PSQ1),PSODUX)="W":10080,$E(PSOQX("DURATION",PSQ1),PSODUX)="L":43200,1:1440)
 .S PSODUTOT=PSODUTOT+(PSODUXX*(+$G(PSOQX("DURATION",PSQ1))))
 G:PSODUMIS>1 QEND ; More than 1 sequence missing a duration
 I $G(PSODUMIS),'$G(PSOQX("DAYS SUPPLY")) G QEND ; missing 1 duration, no days supply
 S PSODSMIN=1440*+$G(PSOQX("DAYS SUPPLY"))
 I $G(PSODSMIN),$G(PSODSMIN)<$G(PSODUTOT) G QEND
 I '$G(PSODUMIS),$G(PSODSMIN),$G(PSODUTOT)>$G(PSODSMIN) G QEND ; no missing durations, but total durations are greater than days supply
 I $G(PSODUMIS),$G(PSODSMIN),$G(PSODSMIN)'>$G(PSODUTOT) G QEND ; 1 missing duration, and total of other durations are not less than days supply
 I '$G(PSODSMIN),$G(PSODUMIS) G QEND ; no days supply, missing a duration
 I $G(PSODUREP) S PSODUDIF=$G(PSODSMIN)-$G(PSODUTOT)
 F PSQ=1:1:QDOSE D  Q:$G(PSQQUIT)
 .I '$G(PSOQX("DOSE ORDERED",PSQ))!($G(PSOQX("SCHEDULE",PSQ))="") S PSQQUIT=1 Q
 .S QTSH=$G(PSOQX("SCHEDULE",PSQ)) D QTS S:'$G(PSOFRQ) PSQQUIT=1 Q:$G(PSQQUIT)
 .I $G(PSOQX("DURATION",PSQ)) S PSQMNL=$L(PSOQX("DURATION",PSQ)) D
 ..S PSQMNLX=$S($E(PSOQX("DURATION",PSQ),PSQMNL)="M":1,$E(PSOQX("DURATION",PSQ),PSQMNL)="H":60,$E(PSOQX("DURATION",PSQ),PSQMNL)="S":.01666,$E(PSOQX("DURATION",PSQ),PSQMNL)="W":10080,$E(PSOQX("DURATION",PSQ),PSQMNL)="L":43200,1:1440)
 ..S PSQMIN=PSQMNLX*(+$G(PSOQX("DURATION",PSQ)))
 .I '$G(PSOQX("DURATION",PSQ)) S PSQMIN=$G(PSODUDIF)
 .S PSQMINZ=PSQMIN/PSOFRQ
 .S PSOQRND=$S('$G(PSOQRND):PSQMINZ*+$G(PSOQX("DOSE ORDERED",PSQ)),1:$G(PSOQRND)+(PSQMINZ*+$G(PSOQX("DOSE ORDERED",PSQ))))
 I $G(PSQQUIT) G QEND
 ;If DAYS SUPPLY was edited by Pharmacy, don't re-calculate QTY automatically for Digitally Signed Pending Orders
 I $G(PSODSEDT),$G(QTYHLD),$G(PSOFDR),$P($G(OR0),"^",24) D  Q
 . W !!,"The Quantity (",QTYHLD,") has not been changed."
 . W !,"Please review and update it if necessary.",!,$C(7)
 . N DIR S DIR(0)="E",DIR("A")="Press Return to Continue" D ^DIR W !
 I $G(PSOQRND) D ROUND
 G QEND
QTS ;*282 Preserve Old Functionality
 S PSOFRQ=$$QTSCH(QTSH)
 I '$G(PSOFRQ) S PSQQUIT=1
 Q
 ;
QTSCH(QTSH) ;
 ; Return Frequency for Schedule QTSH
 ; Otherwise return ""
 N PSOFRQ,SPCT,FOUND
 Q:$G(QTSH)="" ""
 Q:$E(QTSH,1)=" " ""
 S QTSH=$$UPPER(QTSH)
 S SPCT=1,PSOFRQ=0 F I=1:1:$L(QTSH) I $E(QTSH,I)=" " S SPCT=SPCT+1
 F I=0:1:SPCT-1 S SCHTMP=$P(QTSH," ",1,SPCT-I) Q:PSOFRQ  D
 . F II=0:0 S II=$O(^PS(51.1,"APPSJ",SCHTMP,II)) Q:'II!PSOFRQ  S PSOFRQ=$P(^PS(51.1,II,0),"^",3)
 Q:PSOFRQ PSOFRQ
  F I=0:1:SPCT-1 S SCHTMP=$P(QTSH," ",1,SPCT-I) Q:PSOFRQ  D
 . F II=0:0 S II=$O(^PS(51,"B",SCHTMP,II)) Q:'II!PSOFRQ  I $P(^PS(51,II,0),"^",4)<2 S PSOFRQ=$P(^PS(51,II,0),"^",8)
 Q:PSOFRQ PSOFRQ
 Q ""
 ;
QEND ;
 ; PSOMTFLG variable indicates a Maintenance Rx (Titration/Maintenance)
 ; PSO*7.0*574 ;Defect 1155637
 K PSOFRQ
 I $G(PSOOUTQT),$G(QTYHLD),$G(PSOQX("QTY")),$G(QTYHLD)'=$G(PSOQX("QTY")) D  I '$G(PSOMTFLG) W ! N DIR S DIR(0)="E",DIR("A")="Press Return to Continue" D ^DIR W !
 .W:$P($G(^PSDRUG(PSODRUG("IEN"),"CLOZ1")),U)'="PSOCLO1" !!,"Quantity has been changed from "_QTYHLD_" to "_PSOQX("QTY")
 .I $G(PSONEW("FLD"))=8,$P($G(OR0),"^",24),$G(PSODRUG("IEN")),$D(^PSDRUG(+$G(PSODRUG("IEN")),0)) D
 ..I $P(^PSDRUG(PSODRUG("IEN"),0),"^",3)[2!($P(^PSDRUG(PSODRUG("IEN"),0),"^",3)["F") Q
 ..N ZRFA S ZRFA=$S($G(CLOZPAT)=2&(PSOQX("DAYS SUPPLY")=14):1,$G(CLOZPAT)=2&(PSOQX("DAYS SUPPLY")=7):3,$G(CLOZPAT)=1&(PSOQX("DAYS SUPPLY")=7):1,$D(CLOZPAT):0,1:5)
 ..N ZREF,ZTQ S ZREF=QTYHLD\PSOQX("QTY") I ZREF>0 D
 ...S ZTQ=QTYHLD*(PSOQX("# OF REFILLS")+1),ZTQ=ZTQ-PSOQX("QTY"),ZREF=ZTQ\PSOQX("QTY") S:ZREF>ZRFA ZREF=ZRFA
 ...I ZREF'=PSOQX("# OF REFILLS") W !,"# of Refills has been changed from "_PSOQX("# OF REFILLS")_" to "_ZREF S PSOQX("# OF REFILLS")=ZREF
 Q
ROUND ;
 Q:'$G(PSOQRND)
 Q:$$PRN()  ;584 - Do not create a default quantity for a PRN prescription
 I PSOQRND'["." S PSOQX("QTY")=PSOQRND Q
 S PSOQX("QTY")=$P(PSOQRND,".")+1
 Q
DAY(DATE) ;First 5 digits of FileMan date
 N X
 I DATE'?5N Q -1
 S X=$E(DATE,4,5) I X<1!(X>12) Q -1
 S X=DATE+1+(X=12*88)_"01"
 Q $E($$FMADD^XLFDT(X,-1),6,7)
 ;
QTYX(PSOQX) ;
 N PSOQLP,PSOQLN,PSOQAR,PSOCPRQT,QDOSE,QDOSEX S PSOCPRQT=1 F PSOQLP=0:0 S PSOQLP=$O(PSOQX("DURATION",PSOQLP)) Q:'PSOQLP  D
 .S PSOQAR("DURATION",PSOQLP)=$G(PSOQX("DURATION",PSOQLP))
 .I $E(PSOQX("DURATION",PSOQLP))?1A S PSOQLN=$L(PSOQX("DURATION",PSOQLP)) S PSOQX("DURATION",PSOQLP)=$E(PSOQX("DURATION",PSOQLP),2,PSOQLN)_$E(PSOQX("DURATION",PSOQLP))
 S QDOSE=0 F QDOSEX=0:0 S QDOSEX=$O(PSOQX("DOSE ORDERED",QDOSEX)) Q:'QDOSEX  S QDOSE=QDOSE+1
 I '$G(PSOQX("QTY")) D QTYCP G QPASS
 D QTYCP^PSOSIGDS
QPASS F PSOQLP=0:0 S PSOQLP=$O(PSOQAR("DURATION",PSOQLP)) Q:'PSOQLP  D
 .S PSOQX("DURATION",PSOQLP)=$G(PSOQAR("DURATION",PSOQLP))
 K PSOCPRQT
 Q
DAYS(PSOQX) ;Entry point for Days Supply calc for PSO
 ;Kill days supply here
 Q:'$G(PSOQX("QTY"))
 D QTYOPS^PSOSIGDS
 Q
 ;
UPDQTY(NEWQTY) ; If DAYS SUPPLY is being edited and previous QTY was not calculated, don't calculate and update QTY
 ; Also, if digitally signed, do not automatically calculate and update quantity if QTY increases
 ; Input: NEWQTY - Newly Calculated Quantity
 ;Output: 1: YES - Update Rx with re-calculated QTY / 0: NO - Don't update Rx with re-calculated QTY
 N UPDQTY,PSOFREQ,PSOOLDDS,PSONEWDS,PSOOLDQT,PSODOSOR,PSODUR
 S UPDQTY=1 I 'NEWQTY Q UPDQTY
 S PSOOLDDS=$G(PSOQX("DAYS SUPPLY OLD")) I 'PSOOLDDS Q UPDQTY ; DAYS SUPPLY value prior to edit
 S PSONEWDS=$G(PSOQX("DAYS SUPPLY")) I 'PSONEWDS Q UPDQTY ; New DAYS SUPPLY value
 S PSOOLDQT=$G(QTYHLD) I 'PSOOLDQT Q UPDQTY ; QTY on file, possibly calculated
 S PSOFREQ=$$SCHFREQ(),PSODOSOR=$G(PSOQX("DOSE ORDERED",1))
 S PSODUR=$G(PSOQX("DURATION",1)) I PSODUR,PSODUR<PSOOLDDS S PSOOLDDS=PSODUR
 ;
 ;Checking whether current QTY was previously calculated, if not don't update with new QTY
 I (PSODOSOR<1)!(PSOFREQ>1440) D
 . ;Different calculation for doses of less than one QTY (e.g., 1/2 tablet) OR frequency less than every 24hrs (e.g., every 72hrs)
 . I PSOFREQ,(((PSOOLDDS*(1440/PSOFREQ))*PSODOSOR)+.9\1)'=PSOOLDQT S UPDQTY=0
 E  D
 . ;Checking whether current QTY was previously calculated
 . I ((NEWQTY/PSONEWDS)'=(PSOOLDQT/PSOOLDDS)) S UPDQTY=0
 ;
 ;QTY is increasing for a CS Rx/Order, so don't update with new QTY
 I ($G(PSOFDR)&$P($G(OR0),"^",24)&(NEWQTY>PSOOLDQT)) S UPDQTY=0
 ;
 I 'UPDQTY D
 . W !!,"The Quantity (",PSOOLDQT,") has not been changed."
 . W !,"Please review and update it if necessary.",!,$C(7)
 . N DIR S DIR(0)="E",DIR("A")="Press Return to Continue" D ^DIR W !
 Q UPDQTY
 ;
SCHFREQ() ; Returns the Frequency (in minutes) for the schedule
 ; Output: SCHFREQ - Schedule Frequency (in minutes)
 N SCHED,SCHEDIEN
 S SCHED=$G(PSOQX("SCHEDULE",1)) I SCHED="" Q 0
 S SCHEDIEN=$O(^PS(51.1,"B",SCHED,0)) I SCHEDIEN Q $$GET1^DIQ(51.1,SCHEDIEN,2)
 S SCHEDIEN=$O(^PS(51,"B",SCHED,0)) I SCHEDIEN Q $$GET1^DIQ(51,SCHEDIEN,31)
 Q 0
 ;
PRN() ; Returns if the Schedule is PRN (1) or not (0)
 N PRN,SCH S PRN=0
 F SCH=1:1 Q:'$D(PSOQX("SCHEDULE",SCH))  I $G(PSOQX("SCHEDULE",SCH))["PRN" S PRN=1 Q
 Q PRN
 ;
UPPER(PSOSCUP) ;
 Q $TR(PSOSCUP,"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ")