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

APSPES3.m

Go to the documentation of this file.
APSPES3 ;IHS/MSC/PLS - SureScripts HL7 interface - con't;10-Sep-2013 09:40;PLS
 ;;7.0;IHS PHARMACY MODIFICATIONS;**1008,1011,1013,1016**;Sep 23, 2004;Build 74
 ; Send denial message
 ; Input:  ORID - ^OR(100 IEN
 ;        RXIEN - Prescription IEN
 ;          OCC - Order Control Code  (default to DF)
 ;       MSGTXT - optional
 ;          STA - Status (default to 3)
DENY(ORID,RXIEN,OCC,MSGTXT,STA) ;
 N RR
 S RR=$$VALUE^ORCSAVE2(+ORID,"SSRREQIEN")
 Q:'RR
DENY1 N DATA,HLMSGIEN,HLMSTATE,ARY,SEG,ACT,HLECH,HLFS
 N PARMS,ACK,ERR,I,FLG,LP,LOG,DNYC,DNYR,DFN,ACTUSR
 S ORID=+$G(ORID)
 S RXIEN=+$G(RXIEN)
 S ACT=$$GET1^DIQ(9009033.91,RR,.08,"I")
 S ACTUSR=$$GET1^DIQ(9009033.91,RR,.09,"I")
 S DFN=$$GET1^DIQ(9009033.91,RR,1.2,"I")
 ;S OCC=$G(OCC,"DF")
 S OCC=$S($G(OCC):OCC,ACT=3:"RP",ACT=4:"DF",1:"DF")
 S HLMSGIEN=$$GET1^DIQ(9009033.91,RR,.05)  ; Message ID
 ; TODO- Add logic to use the HL7 message text in the RR entry if HL7 message has been purged.
 Q:'HLMSGIEN
 S PARMS("ACK CODE")="AA"
 S PARMS("MESSAGE TYPE")="RRE"
 S PARMS("EVENT")="O26"
 S PARMS("VERSION")=2.5
 S PARMS("ACCEPT ACK TYPE")="AL"
 I $L($G(MSGTXT)) D
 .I $L($P(MSGTXT,"-",1)) S DNYC=$P(MSGTXT,"-",1),MSGTXT=$P(MSGTXT,"-",2)
 .E  S DNYC="AF"
 E  D
 .S DNYR=$$VALUE^ORCSAVE2(+ORID,"SSDENYRSN")
 .I $L(DNYR) D
 ..S DNYC=$P(DNYR,"-")
 ..S MSGTXT=$P(DNYR,"-",2)
 .E  S DNYC="AF"
 S MSGTXT=$G(MSGTXT,"Have patient return to clinic.")
 D PARSE^APSPES2(.DATA,HLMSGIEN,.HLMSTATE)
 S HLFS=$G(DATA("HDR","FIELD SEPARATOR"))
 S HLECH=HLMSTATE("HDR","ENCODING CHARACTERS")
 I $$ACK^HLOAPI2(.HLMSTATE,.PARMS,.ACK,.ERR) D
 .D SETACK
 .I '$$SENDACK^HLOAPI2(.ACK,.ERR) D UPTRRACT(RR,$G(ERR,"There was a problem sending the HL7 message."))
 E  D UPTRRACT(RR,$G(ERR,"There was a problem generating the HL7 message."))
 K FDA,ERR
 S:+$$GET1^DIQ(9009033.91,RR,.08,"I")'=3 FDA(9009033.91,RR_",",.02)="@"  ; Remove order ien
 S FDA(9009033.91,RR_",",.03)=$S($G(STA):STA,+$$GET1^DIQ(9009033.91,RR,.08,"I")=3:5,1:3)  ; Set status to processed-denied
 S FDA(9009033.91,RR_",",.07)=$$NOW^XLFDT()
 S FDA(9009033.91,RR_",",4)=$G(DNYC)_"-"_$G(MSGTXT)
 I ACTUSR="" S FDA(9009033.91,RR_",",.09)=DUZ
 D FILE^DIE(,"FDA","ERR")
 ; Get original order prescription IEN
 I 'RXIEN D
 .N SEGIEN,SEGORC
 .D PARSE^APSPES2(.DATA,HLMSGIEN,.HLMSTATE)
 .S SEGIEN=$$FSEGIEN^APSPES1(.DATA,"ORC")
 .I SEGIEN D
 ..M SEGORC=DATA(SEGIEN)
 ..S RXIEN=$$GET^HLOPRS(.SEGORC,2,1)
 ; Update activity log
 I RXIEN D
 .N LOG,RET
 .S LOG("REASON")="X"
 .S LOG("RX REF")=0
 .S ARY("TYPE")="U"
 .S LOG("COM")="eRx denial response sent to "_$$PHMINFO^APSPES2(RXIEN)
 .D UPTLOG^APSPFNC2(.RET,RXIEN,0,.LOG)
 Q
SETACK ;Get original message to include in ack
 N PRV,ACT,NM,LP,VAL,INST,MRN
 D PREPARY^APSPES1(.DATA,"PID",.ARY)
 ;Get pt id
 D:$G(DFN) SET(.ARY,$$HRCNF^BDGF2(DFN,+$G(DUZ(2))),3,1)  ; Patient HRN
 S SEG=$$ADDSEG^HLOAPI(.ACK,.ARY)
 D SET(.ARY,OCC,1)  ; Order Control Code
 D PREPARY^APSPES1(.DATA,"ORC",.ARY)
 D SET(.ARY,OCC,1)  ; Order Control Code
 D SET(.ARY,$$HLDATE^HLFNC($$NOW^XLFDT()),9)  ; Written Date/Time
 D SET(.ARY,DNYC,16,1)  ; Order Control Code Reason Identifier
 D SET(.ARY,$E(MSGTXT,1,70),16,2)  ; Order Control Code Reason Text
 D SET(.ARY,"NCPDP1131",16,3)  ; Order Control Code Reason System
 D SET(.ARY,$$GET1^DIQ(4,+$G(DUZ(2)),.01),21)  ; Institution Name
 S PRV=$$GET1^DIQ(9009033.91,RR,1.3,"I")
 S ACT=$$GET1^DIQ(9009033.91,RR,.09,"I")
 I +PRV'=+ACT D
 .D SET(.ARY,ACT,19,1)
 .S NM=$$HLNAME^HLFNC($$GET1^DIQ(200,ACT,.01),HLECH)
 .F LP=1:1:$L(NM,$E(HLECH)) S VAL=$P(NM,$E(HLECH),LP) D
 ..D SET(.ARY,VAL,19,LP+1)  ; Acting provider
 S SEG=$$ADDSEG^HLOAPI(.ACK,.ARY)
 D PREPARY^APSPES1(.DATA,"RXO",.ARY)
 D SET(.ARY,0,13,1)  ; Refill count to zero
 S SEG=$$ADDSEG^HLOAPI(.ACK,.ARY)
 D PREPARY^APSPES1(.DATA,"RXR",.ARY)
 S SEG=$$ADDSEG^HLOAPI(.ACK,.ARY)
 D PREPARY^APSPES1(.DATA,"RXE",.ARY)
 S SEG=$$ADDSEG^HLOAPI(.ACK,.ARY)
 D PREPARY^APSPES1(.DATA,"RXD",.ARY)
 D SET(.ARY,0,8,1)  ; Refill count to zero
 S SEG=$$ADDSEG^HLOAPI(.ACK,.ARY)
 S LP=0
 S FLG=0 F I=1:1 D  Q:FLG
 .S LP=$$FSEGIEN^APSPES1(.DATA,"DG1",LP)
 .I LP=0 S FLG=1 Q
 .D PREPARY^APSPES1(.DATA,"DG1",.ARY,LP-1)
 .S SEG=$$ADDSEG^HLOAPI(.ACK,.ARY)
 Q
 ; Send accept message
 ; Input:  ORID - ^OR(100 IEN
ACCEPT(RXIEN,ORID,MSGTXT) ;
 N RR,DATA,HLMSGIEN,HLMSTATE,ARY,SEG,SEGIEN,SEGRXO,DFN,PROV
 N PARMS,HLST,ERR,PRN,REF,REFILLS,I,FLG,LP,DISP,OCC,ACT
 N RX0,RX2,HLFS,HLECH,SSNUM,RRPRV
 S REFILLS=$$GET1^DIQ(52,RXIEN,9,"I")
 S DFN=$$GET1^DIQ(52,RXIEN,2,"I")
 S RX0=^PSRX(RXIEN,0)
 S PROV=$P(RX0,U,4)
 S RX2=^PSRX(RXIEN,2)
 S DISP=REFILLS+1  ; Number of dispenses
 S RR=$$VALUE^ORCSAVE2(+ORID,"SSRREQIEN")
 Q:'RR
 S HLMSGIEN=$$GET1^DIQ(9009033.91,RR,.05)  ; Message ID
 ; TODO- Add logic to use the HL7 message text in the RR entry if HL7 message has been purged.
 Q:'HLMSGIEN
 S PARMS("ACK CODE")="AA"
 S PARMS("MESSAGE TYPE")="RRE"
 S PARMS("EVENT")="O26"
 S PARMS("VERSION")=2.5
 S PARMS("ACCEPT ACK TYPE")="AL"
 S MSGTXT=$G(MSGTXT,"")
 D PARSE^APSPES2(.DATA,HLMSGIEN,.HLMSTATE)
 S HLFS=$G(DATA("HDR","FIELD SEPARATOR"))
 S HLECH=$G(DATA("HDR","ENCODING CHARACTERS"))
 I $$ACK^HLOAPI2(.HLMSTATE,.PARMS,.HLST,.ERR) D
 .;TODO - SEND NOTIFICATION ON ERROR
 .S PRN=$$GETVAL^APSPES2(HLMSGIEN,"ORC",7,7)="PRN"  ; Check for PRN value
 .S REF=+$$GETVAL^APSPES2(HLMSGIEN,"RXO",13,1)  ; incoming Refill count
 .;Get PID information as for a new order
 .D PID^APSPES1(DFN)
 .;S OCC=$S(PRN:"AF",'REF:"AF",DISP'=REF:"CF",1:"AF")
 .S ACT=$$GET1^DIQ(9009033.91,RR,.08,"I")
 .S OCC=$S(ACT=1:"AF",ACT=2:"CF",1:"AF")
 .D ORCNW^APSPES1(OCC,0)
 .S SSNUM=$$GET1^DIQ(9009033.91,RR,.1)
 .D SET(.ARY,SSNUM,3,1)   ;Set the surescripts number into ORC3
 .I OCC="CF" D
 ..N NM,LP,VAL
 ..S RRPRV=$$GET1^DIQ(9009033.91,RR,1.3,"I")
 ..I PROV'=RRPRV D
 ...D SET(.ARY,PROV,19,1)
 ...S NM=$$HLNAME^HLFNC($$GET1^DIQ(200,PROV,.01),HLECH)
 ...F LP=1:1:$L(NM,$E(HLECH)) S VAL=$P(NM,$E(HLECH),LP) D
 ....D SET(.ARY,VAL,19,LP+1)  ; Acting provider
 ...D SET(.ARY,$$SPI^APSPES1(RRPRV),12,1)
 ...S NM=$$HLNAME^HLFNC($$GET1^DIQ(200,RRPRV,.01),HLECH)
 ...F LP=1:1:$L(NM,$E(HLECH)) S VAL=$P(NM,$E(HLECH),LP) D
 ....D SET(.ARY,VAL,12,LP+1)  ;Original Provider
 .;D PREPARY^APSPES1(.DATA,"ORC",.ARY)
 .;TODO - ADJUST RX NUMBER, CHECK REFILL PRN $S(ORC 7.7="PRN" OR RXO13.1=REFILL SET APPROVED (AF), RXO 13.1<>REFILLS SET TO APPROVED WITH CHANGES (CF)
 .;D SET(.ARY,$S(PRN:"AF",'REF:"AF",DISP'=REF:"CF",1:"AF"),1)  ; Order Control Code - Accept
 .;D SET(.ARY,RX,2)  ; Prescription number
 .;D SET(.ARY,$$HLDATE^HLFNC($$NOW^XLFDT()),9)  ; Written Date/Time
 .;D SET(.ARY,"AM",16,1)  ; Order Control Code Reason Identifier
 .S SEG=$$ADDSEG^HLOAPI(.HLST,.ARY)
 .D RXO^APSPES1(0)
 .D SET(.ARY,DISP,13,1)  ; Set refill count to # of dispenses
 .S SEG=$$ADDSEG^HLOAPI(.HLST,.ARY)
 .D PREPARY^APSPES1(.DATA,"RXD",.ARY)
 .D SET(.ARY,$$HLDATE^HLFNC($$NOW^XLFDT(),"DT"),3)
 .D SET(.ARY,DISP,8,1)   ; Set refill count to # of dispenses
 .S SEG=$$ADDSEG^HLOAPI(.HLST,.ARY)
 .D RXR^APSPES1
 .D PREPARY^APSPES1(.DATA,"RXE",.ARY)
 .S SEG=$$ADDSEG^HLOAPI(.HLST,.ARY)
 .D RXC^APSPES1,DG1^APSPES1,AL1^APSPES1
 .;S LP=0
 .;S FLG=0 F I=1:1 D  Q:FLG
 .;.S LP=$$FSEGIEN^APSPES1(.DATA,"DG1",LP)
 .;.I LP=0 S FLG=1 Q
 .;.D PREPARY^APSPES1(.DATA,"DG1",.ARY,LP-1)
 .;.S SEG=$$ADDSEG^HLOAPI(.ACK,.ARY)
 .I '$$SENDACK^HLOAPI2(.HLST,.ERR) D UPTRRACT(RR,$G(ERR,"There was a problem sending the HL7 message."))
 E  D UPTRRACT(RR,$G(ERR,"There was a problem with generation the HL7 message."))
 K FDA,ERR
 S FDA(9009033.91,RR_",",.03)=2  ; Set status to processed-accepted
 S FDA(9009033.91,RR_",",.07)=$$NOW^XLFDT()
 D FILE^DIE("","FDA","ERR")
 I $D(ERR) D UPTRRACT(RR,$G(ERR(1),"Error updating status"))
 ; Update activity log
 I RXIEN D
 .N LOG,RET
 .S LOG("REASON")="X"
 .S LOG("RX REF")=0
 .S LOG("TYPE")="U"
 .S LOG("COM")="eRx "_$$GET1^DIQ(9009033.91,RR,.08)_" response sent to "_$$PHMINFO^APSPES2(RXIEN)
 .D UPTLOG^APSPFNC2(.RET,RXIEN,0,.LOG)
 Q
 ;
SET(ARY,V,F,C,S,R) ;EP
 D SET^HLOAPI(.ARY,.V,.F,.C,.S,.R)
 Q
 ; Supports Refill Request Queue Deny message
 ; Input - IEN to APSP REFILL REQUEST file
DENYRPC(DATA,RR,MSGTXT) ;EP-
 S DATA=1
 Q:'$G(RR)
 D DENY1
 Q
 ; Update activity multiple of APSP Refill Request file
UPTRRACT(IEN,MSG) ;EP-
 N FDA,IENS,ERR,FN
 S IENS="+1,"_IEN_","
 S FN=9009033.916
 S FDA(FN,IENS,.01)=$$NOW^XLFDT()
 S FDA(FN,IENS,.02)=$E($G(MSG,"NO MESSAGE TEXT"),1,160)
 S FDA(FN,IENS,.03)=$G(DUZ)
 D UPDATE^DIE(,"FDA",,"ERR")
 Q
ARSPRRE ; EP - callback for RRE/O26 event
 N AACK,MSG,WHO,OPRV,ARY,RET,RXIEN,DATA,HLMSTATE,MSA
 N SEGIEN,SEGMSA,MSGIEN,SEGERR,ERRTXT,ORDCTL,TXT
 S MSGIEN=0
 D PARSE^APSPES2(.DATA,HLMSGIEN,.HLMSTATE)
 S SEGIEN=$$FSEGIEN^APSPES1(.DATA,"MSA")
 I 'SEGIEN D  Q
 .D BADORP^APSPES4
 M SEGMSA=DATA(SEGIEN)
 S MSGIEN=+$P($$GET^HLOPRS(.SEGMSA,2)," ",2)
 S AACK=$$GET^HLOPRS(.SEGMSA,1)
 S TXT=$$GET^HLOPRS(.SEGMSA,3)
 I AACK'="AA" D
 .S SEGIEN=$$FSEGIEN^APSPES1(.DATA,"ERR")
 .M SEGERR=DATA(SEGIEN)
 .S ERRTXT=$$GET^HLOPRS(.SEGERR,8)
 S RXIEN=$$RXIEN^APSPES2(MSGIEN)
 S OPRV=$$OPRV^APSPES2(MSGIEN)
 S ARY("REASON")="X"
 S ARY("RX REF")=0
 S ARY("USER")=OPRV
 I AACK'="AA" D  Q
 .D BADORP^APSPES4
 .;Only send notification if it was an Accept message
 .S ORDCTL=$$ORDCTL(MSGIEN)
 .I RXIEN D
 ..S ARY("TYPE")="F"
 ..S ARY("COM")=$S($L($G(ERRTXT)):ERRTXT,1:"ERROR: Electronic Prescription did not transmit.")
 ..D UPTLOG^APSPFNC2(.RET,RXIEN,0,.ARY)
 ..I ORDCTL="AF"!(ORDCTL="CF") D NOTIF^APSPES4(RXIEN,"ERROR: Electronic Prescription did not transmit.",$S($L($G(ERRTXT)):ERRTXT,1:"Transmission was not accepted"))
 Q:'RXIEN
 S ARY("TYPE")="U"
 S ARY("COM")=$S(TXT'="":TXT,1:"eRx update: Prescription delivered to pharmacy.")
 ;"e-Pres update: Received STATUS update."
 D UPTLOG^APSPFNC2(.RET,RXIEN,0,.ARY)
 Q
ORDCTL(MSGIEN) ; EP
 N CTL,SEGORC
 D PARSE^APSPES2(.DATA,MSGIEN,.HLMSTATE)
 S SEGIEN=$$FSEGIEN^APSPES1(.DATA,"ORC")
 Q:'SEGIEN 0
 M SEGORC=DATA(SEGIEN)
 S CTL=$$GET^HLOPRS(.SEGORC,1,1)
 Q CTL
 ;
DCORIG(RET,ORIEN) ; EP
 ;DC the original order on a deny message
 N ORGPKGID,ORXNUM,REA,MSG,DA,ORGIEN,PSCAN
 S RET=""
 S ORIEN=+$G(ORIEN)
 S ORGIEN=$$GET1^DIQ(100,ORIEN,9)
 Q:'+ORGIEN
 S STAT=$$GET1^DIQ(100,ORGIEN,5,"I")
 Q:STAT=1!(STAT=12)!(STAT=13)
 S ORGPKGID=+$$GET1^DIQ(100,ORGIEN,33,"I")
 Q:'ORGPKGID
 S ORXNUM=$$GET1^DIQ(52,ORGPKGID,.01)
 S REA="C",DA=ORGPKGID
 S MSG="Provider denied Surescripts refill request"
 S PSCAN(ORXNUM)=DA_"^C"
 D CAN^PSOCAN
 Q
ISRENEW(DATA,ORIEN) ;EP
 ;See if the new order is a auto-renewal order
 S DATA=$$GET1^DIQ(100,+$G(ORIEN),9)>0
 Q