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

PXRMNTFY.m

Go to the documentation of this file.
PXRMNTFY ;SLC/PKR - Routines for notifications. ;Jan 13, 2023@19:04
 ;;2.0;CLINICAL REMINDERS;**24,45,71,84**;Feb 04, 2005;Build 2
 ;
 ;SAC EXEMPTION 20030908-01 : Use proper variable scoping instead of
 ;                            namespace variable scoping
 ;
 ;Reference to EN^ORB3 in ICR #1362
 ;Reference to GETRECIPS^WVRPCPT1 in ICR #6200
 ;Reference to $$SAVESRND^WVRPCPT1 in ICR #6336
 ;Reference to $$GETNOTID^ORBSMART in ICR #6341
 ;Reference to $$CODEC^ICDEX and $$CODECS^ICDEX in ICR #5747
 ;Reference to ^AUPNPROB( in ICR #3837
 ;Reference to ^AUPNVSIT( in ICR #2028
 ;
 ;========================
DELOPEN(DFN,ORN) ;Delete open OE/RR Notifications.
 N ALIST,IND,ORID,XQAID
 D PATIENT^XQALERT("ALIST",DFN)
 F IND=1:1:ALIST D
 . S XQAID=$P(ALIST(IND),U,2)
 . S ORID=$P(XQAID,";",1)
 . I $P(ORID,",",3)=ORN D DELETE^XQALERT
 Q
 ;
 ;========================
SUICIDE(EVENT,DFN,VISIT) ;Send an alert if the patient attempted or
 ;completed suicide, as marked by a health factor. This is called
 ;from DATACHGR^PXRMPINF which is invoked by the protocol PXK VISIT
 ;DATA EVENT.
 N DATE,HFAIEN,HFBIEN,HFSAIEN,HFSCIEN,MSG,VHFIEN
 S HFSAIEN=$$FIND1^DIC(9999999.64,"","","MH SUICIDE ATTEMPTED")
 S HFSCIEN=$$FIND1^DIC(9999999.64,"","","MH SUICIDE COMPLETED")
 S (MSG,VHFIEN)=""
 F  S VHFIEN=$O(^XTMP(EVENT,VISIT,"HF",VHFIEN)) Q:VHFIEN=""  D
 . S HFAIEN=$P($G(^XTMP(EVENT,VISIT,"HF",VHFIEN,0,"AFTER")),U,1)
 . S HFBIEN=$P($G(^XTMP(EVENT,VISIT,"HF",VHFIEN,0,"BEFORE")),U,1)
 .;If after is null then the health factor has been deleted so delete
 .;any open alerts.
 . I HFAIEN="",HFBIEN'="" D DELOPEN^PXRMNTFY(DFN,77) Q
 .;If the before and after are the same the HF is not new so do not
 .;send the alert.
 . I HFAIEN=HFBIEN Q
 . I HFAIEN=HFSAIEN S MSG="Suicide attempted"
 . I HFAIEN=HFSCIEN S MSG="Suicide completed"
 I MSG="" Q
 S DATE=$P(^AUPNVSIT(VISIT,0),U,1)
 ;If DATE is more than 30 days in the past do not send the alert.
 I $$FMDIFF^XLFDT(DT,DATE)>30 Q
 S MSG=MSG_" on "_$$FMTE^XLFDT(DATE,"5Z")
 D EN^ORB3(77,DFN,"","",MSG,"")
 Q
 ;
 ;========================
TALERT(DFN,PROV,ATYPE,MUC) ;Send pregnancy/lactation status change alert
 ;MUC - 1 => MEDICALLY UNABLE TO CONCEIVE AND LAB TEST
 ;      2 => MEDICALLY UNABLE TO CONCEIVE AND ICD CODE
 ;      0 OR UNDEFINED => MEDICALLY ABLE TO CONCEIVE
 S ATYPE=+$G(ATYPE),MUC=+$G(MUC)
 N ORNOTNM,ORNOTIEN,MESSAGE
 S ORNOTNM=$S(ATYPE=1:"PREGNANCY STATUS REVIEW",ATYPE=2:"LACTATION STATUS REVIEW",1:"")
 S ORNOTIEN=$$GETNOTID^ORBSMART(ORNOTNM) Q:ORNOTIEN<1
 I ATYPE=1 D
 .I MUC=1 S MESSAGE="R/O ectopic preg if Hx sterilization-POS preg test & Dx unable to conceive"
 .I MUC=2 S MESSAGE="Dx unable to conceive-ICD codes entered c/w preg-review/correct record"
 I $G(MESSAGE)="" S MESSAGE="Possible "_$S(ATYPE=1:"pregnancy",ATYPE=2:"lactation",1:"")_" status conflict: confirm, consider status update."
 D EN^ORB3(ORNOTIEN,DFN,"",.PROV,MESSAGE)
 Q
 ;
WH(EVENT,DFN,VISIT,GMPLIFN) ;Determine whether to send pregnancy/lactation status change
 ;based on ICD/SNOMED CT code.
 N CODELIST,EXIT,PROVARR,PXRMSDT,SEND,SENDMSG,STATUS,MESSAGES,TAX,TAXIEN,WHDATA
 N CACHE,CODE,ACODE
 I $G(EVENT)'="" D  Q:$G(EXIT)
 .I $G(VISIT)'="" D  Q:$G(EXIT)
 ..I '$D(^XTMP(EVENT,VISIT,"POV")) S EXIT=1 Q
 ..D WHICD(.CODELIST,.PROVARR,EVENT,VISIT)
 .I $G(VISIT)="" D  Q:$G(EXIT)
 ..I '(($D(^XTMP(EVENT,"DISCHARGE")))!($D(^XTMP(EVENT,"MOVEMENT")))!($D(^XTMP(EVENT,"SERVICE")))!($D(^XTMP(EVENT,"SERVICE46")))) S EXIT=1 Q
 ..D WHPTF(.CODELIST,.PROVARR,DFN)
 I (($G(EVENT)="")!($G(VISIT)=""))&($G(GMPLIFN)>0) D  Q:$G(EXIT)
 .I '$D(^AUPNPROB(GMPLIFN)) S EXIT=1 Q
 .D WHPBL(.CODELIST,.PROVARR,GMPLIFN)
 I '$D(CODELIST) Q
 ;
 S SENDMSG=0,STATUS=1
 F TAX="VA-WH CURRENTLY PREGNANT","VA-WH RECENTLY PREGNANT","VA-WH POSSIBLE PREGNANCY","VA-WH CURRENTLY LACTATING"  D  Q:STATUS'=1
 .S TAXIEN=$O(^PXD(811.2,"B",TAX,0)) Q:'+TAXIEN
 .S CODESYS=""
 .F  S CODESYS=$O(CODELIST(CODESYS)) Q:(CODESYS="")!(STATUS'=1)  D
 ..I '$D(^PXD(811.2,TAXIEN,20,"AE",CODESYS)) Q
 ..S CODE=""
 ..F  S CODE=$O(CODELIST(CODESYS,CODE)) Q:(CODE="")!(STATUS'=1)  D
 ...I $D(^PXD(811.2,TAXIEN,20,"AE",CODESYS,CODE)) D
 ....S STATUS=$$WHAPPL(DFN,$P($P(CODELIST(CODESYS,CODE),U,1),"|",1))
 ....I STATUS'=1 Q
 ....S SENDMSG=SENDMSG+1
 ....S SENDMSG(SENDMSG)=TAX_U_CODESYS_"|"_CODE_"|"_CODELIST(CODESYS,CODE)
 I (SENDMSG=0)!(STATUS'=1) Q
 F CODE=1:1:SENDMSG Q:$G(CACHE)=-1  D
 .S SEND=0
 .I SENDMSG(CODE)["PREGNAN" D
 ..I '$D(CACHE("PREGNANCY")) D
 ...S PXRMSDT=$P($P(SENDMSG(CODE),"|",3),U,1)
 ...D WHEVAL(.CACHE,"VA-WH UPDATE PREGNANCY STATUS",.PXRMSDT)
 ..Q:CACHE=-1
 ..I ((SENDMSG(CODE)["CURRENTLY")!(SENDMSG(CODE)["POSSIBLE"))&((CACHE("PREGNANCY","STATE")'="PREGNANT")!((CACHE("PREGNANCY","STATE")="PREGNANT")&(CACHE("PREGNANCY","OUT DATED")))) D  Q
 ...S SEND=1,ACODE=$P($P(SENDMSG(CODE),U,2),"|",2) Q:ACODE=""
 ...I $D(SEND("ACODES1",SEND,ACODE)) Q
 ...S SEND("ACODES1",SEND,ACODE)="",SEND("ACODES2",SEND,CODE)=""
 ..I (SENDMSG(CODE)["RECENTLY")&((CACHE("PREGNANCY","STATE")="PREGNANT")&(CACHE("PREGNANCY","OUT DATED")=0)) D
 ...S SEND=1,ACODE=$P($P(SENDMSG(CODE),U,2),"|",2) Q:ACODE=""
 ...I $D(SEND("ACODES1",SEND,ACODE)) Q
 ...S SEND("ACODES1",SEND,ACODE)="",SEND("ACODES2",SEND,CODE)=""
 .I SENDMSG(CODE)["LACTATING" D
 ..I '$D(CACHE("LACTATION")) D
 ...S PXRMSDT=$P($P(SENDMSG(CODE),"|",3),U,1)
 ...D WHEVAL(.CACHE,"VA-WH UPDATE LACTATION STATUS",.PXRMSDT)
 ..Q:CACHE=-1
 ..I CACHE("LACTATION","STATE")'="LACTATING" D
 ...S SEND=2,ACODE=$P($P(SENDMSG(CODE),U,2),"|",2) Q:ACODE=""
 ...I $D(SEND("ACODES1",SEND,ACODE)) Q
 ...S SEND("ACODES1",SEND,ACODE)="",SEND("ACODES2",SEND,CODE)=""
 .I $D(SEND("ACODES2",SEND,CODE)) D
 ..I '$D(MESSAGES(SEND,"PXRMPROV")) D
 ...N PXRMPROV
 ...D GETRECIPS^WVRPCPT1(.PXRMPROV,DFN,"CODE",$S(SEND=1:"P",SEND=2:"L",1:""),0,$P(SENDMSG(CODE),U,3))
 ...I +$G(PXRMPROV(0))=-1 D
 ....S ERROR(1,0)="Error retrieving Women's Health managers: "_$P(PXRMPROV(0),U,2)
 ....D ERROR(.ERROR)
 ....K PXRMPROV
 ....M PXRMPROV=PROVARR
 ...I $O(PXRMPROV(""))'="" M MESSAGES(SEND,"PXRMPROV")=PXRMPROV
 ..I $D(MESSAGES(SEND,"PXRMPROV")) D
 ...S WHDATA("ID")=DFN_U_$S(SEND=1:"P",SEND=2:"L",1:"")
 ...S WHDATA("CODE")=$P(SENDMSG(CODE),U,2)
 ...S STATUS=$$SAVESRND^WVRPCPT1(.WHDATA)
 ...I +STATUS=-1 S ERROR(1,0)="Error saving status conflict notification data: "_$P(STATUS,U,2) D ERROR(.ERROR)
 ...I +STATUS<1 K MESSAGES(SEND)
 S SEND=0 F  S SEND=$O(MESSAGES(SEND)) Q:'+SEND  D
 .N PXRMPROV
 .M PXRMPROV=MESSAGES(SEND,"PXRMPROV")
 .D TALERT(DFN,.PXRMPROV,SEND,$S((SEND=1)&($G(CACHE("PREGNANCY","MUC"))):2,1:0))
 Q
 ;
ERROR(ERROR) ;Send an email that an error occurred
 N CNT,DATA,INDEX,SOURCE
 S CNT=$O(ERROR("?"),-1)+1,ERROR(CNT,0)=""
 S CNT=CNT+1,ERROR(CNT,0)="The contents of the XTMP global that triggered this error:"
 F SOURCE="^XTMP(EVENT)","WHDATA" D
 .I $D(@SOURCE) D ACOPY^PXRMUTIL(SOURCE,"DATA()")
 .S INDEX=0 F  S INDEX=$O(DATA(INDEX)) Q:'+INDEX  S CNT=CNT+1,ERROR(CNT,0)=DATA(INDEX)
 .K DATA
 I $G(PTYPE)'="" S CNT=CNT+1,ERROR(CNT,0)="PTYPE="_PTYPE
 K ^TMP("PXRMXMZ",$J)
 M ^TMP("PXRMXMZ",$J)=ERROR
 D SEND^PXRMMSG("PXRMXMZ","Clinical Reminder Code Listener")
 K ^TMP("PXRMXMZ",$J)
 Q
WHAPPL(DFN,PXRMSDT) ;Determine if code is applicable for the given date
 ; $$WHAPPL: -1 => error
 ;           0 => not applicable
 ;           1 => applicable
 N NAME,RIEN,NODE,RNAME,DEFARR,FIEV,STATUS
 K ^TMP("PXRHM",$J)
 S NAME="VA-WH POTENTIALLY UNSAFE MEDICATIONS REPORT - COHORT"
 S RIEN=$O(^PXD(811.9,"B",NAME,"")) I RIEN'>0 Q -1
 S NODE=$G(^PXD(811.9,RIEN,0))
 S RNAME=$S($P(NODE,U,3)'="":$P(NODE,U,3),1:$P(NODE,U))
 D DEF^PXRMLDR(RIEN,.DEFARR)
 D EVAL^PXRM(DFN,.DEFARR,1,1,.FIEV,.PXRMSDT)
 S STATUS=$P($G(^TMP("PXRHM",$J,RIEN,RNAME)),U)
 K ^TMP("PXRHM",$J)
 I (STATUS="CNBD")!(STATUS="ERROR") Q -1
 I $$STATMTCH^PXRMORCH(STATUS,"N") Q 0
 Q 1
WHEVAL(CACHE,NAME,PXRMSDT) ;Evaluate pregancy or lactation reminder
 N RIEN,STATUS,SUB,FIND,DOC,DATE,EDD
 K ^TMP("PXRHM",$J)
 S RIEN=$O(^PXD(811.9,"B",NAME,"")) Q:RIEN<0
 S NODE=$G(^PXD(811.9,RIEN,0)) Q:NODE=""
 S RNAME=$S($P(NODE,U,3)'="":$P(NODE,U,3),1:$P(NODE,U)) Q:RNAME=""
 D MAINDF^PXRM(DFN,RIEN,1,PXRMSDT)
 S STATUS=$P($G(^TMP("PXRHM",$J,RIEN,RNAME)),U)
 I (STATUS="CNBD")!(STATUS="ERROR") S CACHE=-1 Q
 S SUB=$P($P(NAME,"VA-WH UPDATE ",2)," ") I SUB="" S CACHE=-1 Q
 S FIND=$S(SUB="PREGNANCY":3,SUB="LACTATION":2,1:"") I FIND="" S CACHE=-1 Q
 S CACHE(SUB,"STATE")=$G(^TMP("PXRHM",$J,RIEN,"FIEVAL",FIND,1,SUB_" STATE"))
 S DOC=$G(^TMP("PXRHM",$J,RIEN,"FIEVAL",FIND,1,"DOCUMENTATION STATUS"))
 S DATE=$G(^TMP("PXRHM",$J,RIEN,"FIEVAL",FIND,1,"DATE"))
 S CACHE(SUB,"OUT DATED")=0
 I DOC'="NO DOCUMENTATION",$$STATMTCH^PXRMORCH(STATUS,"D") D
 .I SUB="PREGNANCY" D
 ..I DOC="INCOMPLETE" D
 ...I CACHE(SUB,"STATE")="PREGNANT" D
 ....S EDD=$G(^TMP("PXRHM",$J,RIEN,"FIEVAL",FIND,1,"EDD"))
 ....I EDD="" S EDD=$$NEWDATE^PXRMDATE($G(^TMP("PXRHM",$J,RIEN,"FIEVAL",FIND,1,"LAST MENSTRUAL PERIOD DATE")),"+","40W")
 ....I EDD<DT S CACHE(SUB,"OUT DATED")=1
 ...I CACHE(SUB,"STATE")'="PREGNANT",DATE<$$NEWDATE^PXRMDATE(DT,"-","1Y") S CACHE(SUB,"OUT DATED")=1
 ..I DOC="COMPLETE" S CACHE(SUB,"OUT DATED")=1
 .I SUB="LACTATION" S CACHE(SUB,"OUT DATED")=1
 I SUB="PREGNANCY" S CACHE(SUB,"MUC")=$G(^TMP("PXRHM",$J,RIEN,"FIEVAL",1))
 K ^TMP("PXRHM",$J)
 S CACHE=1
 Q
 ;
WHICD(CODELIST,PROVARR,EVENT,VISIT) ;Retrieve data from ^XTMP
 N POVIEN,AFTER,BEFORE,CODEIEN,CODE,CODESYS,PRVIEN,PROVIEN,CDATE,ODATE
 N DIVISION
 S CDATE=$P($G(^XTMP(EVENT,VISIT,"VST",VISIT,0,"AFTER")),U),POVIEN=""
 S ODATE=$P($G(^XTMP(EVENT,VISIT,"VST",VISIT,0,"AFTER")),U,13)
 S DIVISION=$P($G(^XTMP(EVENT,VISIT,"VST",VISIT,0,"AFTER")),U,6)
 F  S POVIEN=$O(^XTMP(EVENT,VISIT,"POV",POVIEN)) Q:POVIEN=""  D
 .S AFTER=$G(^XTMP(EVENT,VISIT,"POV",POVIEN,0,"AFTER"))
 .S BEFORE=$G(^XTMP(EVENT,VISIT,"POV",POVIEN,0,"BEFORE"))
 .I (AFTER=BEFORE)!(AFTER="") Q
 .S CODEIEN=$P(AFTER,U,1)
 .D ADDICD(.CODELIST,CODEIEN,CDATE,ODATE,DIVISION)
 S PRVIEN=0
 F  S PRVIEN=$O(^XTMP(EVENT,VISIT,"PRV",PRVIEN)) Q:PRVIEN'>0  D
 .S AFTER=$G(^XTMP(EVENT,VISIT,"PRV",PRVIEN,0,"AFTER"))
 .S PROVIEN=+$P(AFTER,U,1) I PROVIEN>0 S PROVARR(PROVIEN)=""
 Q
 ;
WHPBL(CODELIST,PROVARR,GMPLIFN) ;Retrieve data from ^AUPNPROB
 N NODE1,PIECE,PROVIEN,CODEIEN,CDATE,ODATE,DIVISION
 I $P($G(^AUPNPROB(GMPLIFN,1)),U,2)="H" Q
 S CDATE=$P($G(^AUPNPROB(GMPLIFN,802)),U)
 S ODATE=$P($G(^AUPNPROB(GMPLIFN,0)),U,3)
 S CODE=$P($G(^AUPNPROB(GMPLIFN,800)),U)
 S DIVISION=$P($G(^AUPNPROB(GMPLIFN,0)),U,6)
 I CODE'="" S CODELIST("SCT",CODE)=CDATE_$S(ODATE'="":"|"_ODATE,1:"")_$S(DIVISION'="":U_DIVISION,1:"")
 S CODEIEN=$P($G(^AUPNPROB(GMPLIFN,0)),U)
 D ADDICD(.CODELIST,CODEIEN,CDATE,ODATE,DIVISION)
 S NODE1=$G(^AUPNPROB(GMPLIFN,1))
 F PIECE=3:1:5  S PROVIEN=+$P(NODE1,U,PIECE) I PROVIEN>0 S PROVARR(PROVIEN)=""
 Q
 ;
WHPTF(CODELIST,PROVARR,DFN) ;Retrieve data from ^XTMP
 N TYPE,FIELD,VAIN,VAERR,DATE,BEFORE,AFTER
 S DATE=$G(^XTMP(EVENT,"INTEREST DATE")),TYPE=""
 F  S TYPE=$O(^XTMP(EVENT,TYPE)) Q:TYPE=""  S FIELD="" F  S FIELD=$O(^XTMP(EVENT,TYPE,FIELD)) Q:FIELD=""  D
 .Q:FIELD="IENS"
 .S AFTER=$G(^XTMP(EVENT,TYPE,FIELD,"NEW"))
 .S BEFORE=$G(^XTMP(EVENT,TYPE,FIELD,"OLD"))
 .I (AFTER=BEFORE)!(AFTER="") Q
 .D ADDICD(.CODELIST,AFTER,DATE,$G(^XTMP(EVENT,"OCCURRED DATE")),$G(^XTMP(EVENT,"INSTITUTION")))
 Q:'$D(CODELIST)
 I $G(^XTMP(EVENT,"PRIMARY PROVIDER"))>0 S PROVARR(^XTMP(EVENT,"PRIMARY PROVIDER"))=""
 I $G(^XTMP(EVENT,"ATTENDING PHYSICIAN"))>0 S PROVARR(^XTMP(EVENT,"ATTENDING PHYSICIAN"))=""
 Q
ADDICD(CODELIST,CODEIEN,CODEDATE,OCCURDATE,DIVISION) ;Add ICD code to the CODELIST array
 N CODE,CODESYS
 S CODE=$$CODEC^ICDEX(80,CODEIEN)
 I $P(CODE,U,1)=-1 Q
 S CODESYS=$P($$CODECS^ICDEX(CODE,80,CODEDATE),U)
 S CODESYS=$S(CODESYS=1:"ICD",CODESYS=30:"10D",1:"")
 I CODESYS="" Q
 S CODELIST(CODESYS,CODE)=CODEDATE_$S($G(OCCURDATE)'="":"|"_OCCURDATE,1:"")_$S($G(DIVISION)'="":U_DIVISION,1:"")
 Q