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

AGMPHMFN.m

Go to the documentation of this file.
AGMPHMFN ; IHS/SD/TPF - HLO MPI MFN-M05 PROCESSING RTN ;    
 ;;7.2;IHS PATIENT REGISTRATION;**1,3,7**;MAY 20, 2010;Build 26
 ;
 Q
 ;
PROC(HLMSGIEN,SUCCESS) ;EP - PROCESS MFN-M05 TREATING FACILITY UPDATE MESSAGE FROM MPI
 N DATA,HLMSTATE,MSGID,MSGSEG,ICNEUID,ERROR,MFK,MFNERR,GARBLED,AGERROR
 ; 05/28/2013 - KJH - TFS8069 - 'SUCCESS' must have a default value (of NULL - meaning failed) in case of an early QUIT due to an error.
 S SUCCESS=""
 D PARSE(.DATA,HLMSGIEN,.HLMSTATE)
 M MFK=DATA
 D NOW^%DTC S NOW=%
 S NOW=$TR($$FMTE^XLFDT($P(NOW,"@"),"5DZ"),"/")
 S NOW=$E(NOW,5,8)_$E(NOW,1,2)_$E(NOW,3,4)
 N NXT,ICNEUID,ACKIEN,LOCALID,SETICN,SEGIEN,SEGMENT,OSTANUMB,OSTANAME
 S SEGMENT=DATA(1,1,1,1,1)
 Q:SEGMENT'="MFI"            ;MFN W/O MFI AS FIRST SEGMENT - THROW ERROR
 I SEGMENT="MFA" D
 .S ERROR="MFA IN MFN?" D MSGERR(0,HLMSGIEN,HLMSGIEN,ERROR)
 S TYPEUPD=$G(DATA(1,4,1,1,1))  ;TYPE OF UPDATE UPD=UPDATE OR REP=REPLACE
 IF TYPEUPD="" D
 .S ERROR="TYPEUPD PULLED FROM DATA(1,4,1,1,1) WAS EMPTY." D MSGERR(0,HLMSGIEN,HLMSGIEN,ERROR)
 ;
 ;IF 'REP' THEN ALL MFEs SHOULD BE 'MAD'
 ;PER PROTOCOL ALL SHOULD BE REP. WE DELETE THE ENTRIES AND REPLACE
 ;WITH WHAT THE MPI SENDS TO UPDATE
 ;FIND LOCAL ID BASED ON STATION NUMBER IN MESSAGE
 S LOCALID=$$GETLOCAL(.DATA,DATA("HDR","RECEIVING FACILITY",1))
 ; 05/28/2013 - KJH - TFS8069 - Report error if LOCALID is not returned due to the UNDEF fix below.
 I LOCALID="" D  Q
 .S ERROR="MISSING LOCALID" D MSGERR(0,HLMSGIEN,HLMSGIEN,ERROR)
 I TYPEUPD="REP" D DELPTREC(LOCALID)  ;DELETE ALL PT RECORDS FOR LOCAL ID
 ;PROCESS SEGIEN BEGINNING AT FIRST MFE SEGMENT
 S GARBLED=0
 S SEGIEN=1
 F  S SEGIEN=$O(DATA(SEGIEN)) Q:'SEGIEN  D
 .S SEGMENT=DATA(SEGIEN,1,1,1,1)
 .Q:SEGMENT="ZET"  ;DON'T PROCESS THESE YET
 .I SEGMENT'="MFE" D  Q   ;SEND ONLY ONE ALERT
 ..K MFK(SEGIEN),DATA(SEGIEN)
 ..Q:GARBLED
 ..S ERROR="POSSIBLE GARBLED MESSAGE"
 ..D MSGERR("",HLMSGIEN,HLMSGIEN,ERROR)
 ..S GARBLED=1
 .D GETVARS(SEGIEN)
 .;1/15/2018 - GCD - CR 10010 - Update ICN for add ("MAD") or update ("MUP"); delete ICN for delete ("MDL").
 .I DATA("HDR","RECEIVING FACILITY",1)=STATNUM,REMOTEID'="" D
 ..I UPDTYPE="MAD"!(UPDTYPE="MUP") S ICNEUID=$G(DATA(SEGIEN,5,1,4,1)) I ICNEUID'="" D UPDEUID(REMOTEID,ICNEUID) Q
 ..I UPDTYPE="MDL" D DELEUID(REMOTEID) Q
 .I UPDTYPE="MUP" D UPD Q
 .I UPDTYPE="MDL" D DEL Q
 .I UPDTYPE="MAD" D ADD Q
 D CREATMSG^AGMPIHLO(LOCALID,"M05",,.SUCCESS)  ;SENDMFK BACK
 I SUCCESS D MSGCMPLT^AGMPIBGP(HLMSGIEN) I 1  ;I 1 USED TO RESTORE THE VALUE OF $T AFTER THE CALL
 E  S ERROR="CAN'T CREATE MFK" D
 .D MSGERR(LOCALID,HLMSGIEN,HLMSGIEN,ERROR)
 Q
 ;
GETLOCAL(DATA,LSTATNUM) ;EP - GET LOCAL
 N STATNUM,SEGIEN,SEGMENT,LOCALID,EUID
 ; 05/28/2013 - KJH - TFS8069 - Fix UNDEF error on the LOCALID and EUID variables. See also checks on the call to UPDEUID below.
 S SEGIEN=1,LOCALID="",EUID=""
 F  S SEGIEN=$O(DATA(SEGIEN)) Q:'SEGIEN  D
 .S SEGMENT=DATA(SEGIEN,1,1,1,1)
 .Q:SEGMENT="ZET"
 .S STATNUM=$G(DATA(SEGIEN,5,1,2,1))
 .I LSTATNUM=STATNUM D  Q
 ..S LOCALID=$G(DATA(SEGIEN,5,1,1,1))
 ..S EUID=$G(DATA(SEGIEN,5,1,4,1))
 ; 1/15/2018 - GCD - CR 10010 - Commented out so we can do this correctly above.
 ;I LOCALID]"",EUID]"" D UPDEUID(LOCALID,EUID)
 Q LOCALID
 ;
UPDEUID(LOCALID,EUID)  ;UPDATE ICN IF DIFFERENT FROM THIS ONE
 N CURICN,NOW
 S CURICN=$$GET1^DIQ(2,LOCALID_",",991.01)
 ;1/15/2018 - GCD - CR 10010 - Allow updating the ICN when there isn't one already assigned.
 ;Q:CURICN=EUID!(CURICN="")
 Q:CURICN=EUID
 D ADDICN^AGMPHLVQ(EUID,LOCALID)  ;REPLACE OLD WITH NEW "UNLINK" AT MPI
 K DIR,DIE,DIC,DR,DA
 S DA(1)=LOCALID
 S DIC="^DPT("_DA(1)_",""MPIFHIS"","
 S DIC(0)="L"
 S X=CURICN
 D ^DIC
 I Y<0 D  Q
 .D MSGERR(LOCALID,HLMSGIEN,HLMSGIEN,"ICN HIST "_CURICN)
 S DA=+Y
 K DIR,DIE,DIC,DR
 D NOW^%DTC S AGNOW=%
 ;B "S+"
 ;S DIE="^DPT("_DA(1)_",""MPIFHIS"","_DA_","
 ;S DR="3///^S X=AGNOW"
 ;D ^DIE
 S $P(^DPT(DA(1),"MPIFHIS",DA,0),U,4)=AGNOW
 K DIR,DIE,DIC,DR,DA
 Q
 ;
 ;1/15/2018 - GCD - CR 10010 - Delete the ICN when an "MDL" message is received.
DELEUID(LOCALID) ;DELETE ICN FROM PATIENT RECORD
 I $P($G(^DPT($G(LOCALID," "),"MPI")),U)="" Q
 N DIE,DA,DR
 S DIE="^DPT(",DA=LOCALID
 S DR="991.01///@"
 D ^DIE
 Q
 ;
ADD ;EP - ADD ENTRY TO TREATING FACILITY LIST
 K FDA,FDAIEN,DD,DO,DIC,DA,RESULT,UPDERR
 S FDA(1,391.91,"+1,",.01)=LOCALID
 S FDA(1,391.91,"+1,",.02)=INST
 S FDA(1,391.91,"+1,",.03)=DTLASTTR
 S FDA(1,391.91,"+1,",9999999.01)=REMOTEID
 S FDA(1,391.91,"+1,",9999999.02)=EUID
 ;S FDA(1,391.91,"+1,",.07)=EVENT
 D UPDATE^DIE("","FDA(1)","FDAIEN","UPDERR")
 I $D(UPDERR("DIERR",1)) D
 .S MFNERR(SEGIEN)="Add of TFL"_INST_" failed for "_LOCALID_" due to "_$G(UPDERR("DIERR",1,"TEXT",1))
 .S ERROR=MFNERR(SEGIEN) D MSGERR(LOCALID,HLMSGIEN,HLMSGIEN,ERROR)
 S MFK(SEGIEN+.5)="MFA"_DATA("HDR","FIELD SEPARATOR")_UPDTYPE_DATA("HDR","FIELD SEPARATOR")_STATNUM_DATA("HDR","FIELD SEPARATOR")_NOW_DATA("HDR","FIELD SEPARATOR")_$S($D(MFNERR(SEGIEN)):"U",1:"S")
 Q
UPD ;EP - UPDATE BASED ON INSTITUTION LOOKUP
 K DIE,DIC,DR,DA,DIR
 ;05/30/2013 - KJH - TFS8079 - Updated next line to use 'LOCALID'. Was originally using 'REMOTE' which does not exist. 'REMOTEID' is also wrong.
 S DA=$O(^DGCN(391.91,"AINST",INST,LOCALID,""))
 I 'DA D ADD Q
 S DIE="^DGCN(391.91,"
 S DR=".03///"_DTLASTTR
 D ^DIE
 I $$GET1^DIQ(391.91,DA_",",.03)="" D
 .S MFNERR(SEGIEN)="UPD UPDATE DID NOT UPDATE 'LAST TREATED DATE' FOR "_LOCALID_" AND "_INST_" INSTITUTION."
 .;05/29/2013 - KJH - TFS8079 - Added call to MSGERR.
 .S ERROR=MFNERR(SEGIEN) D MSGERR(LOCALID,HLMSGIEN,HLMSGIEN,ERROR)
 S MFK(SEGIEN+.5)="MFA"_DATA("HDR","FIELD SEPARATOR")_UPDTYPE_DATA("HDR","FIELD SEPARATOR")_STATNUM_DATA("HDR","FIELD SEPARATOR")_NOW_DATA("HDR","FIELD SEPARATOR")_$S($D(MFNERR(SEGIEN)):"U",1:"S")
 Q
 ;
DEL ;EP - DELETE ENTRY
 K DIE,DIC,DR,DA,DIR
 ;05/30/2013 - KJH - TFS8079 - Updated next line to use 'LOCALID'. Was originally using 'REMOTEID' which is wrong.
 S DA=$O(^DGCN(391.91,"AINST",INST,LOCALID,""))
 I 'DA  D  Q  ;THROW ERROR AND SEND BACK IN AL ACK
 .;05/29/2013 - KJH - TFS8079 - Update error message text and added call to MSGERR.
 .S MFNERR(SEGIEN)="DEL DID NOT FIND TFL RECORD FOR "_LOCALID_" AND "_INST_" INSTITUTION."
 .S ERROR=MFNERR(SEGIEN) D MSGERR(LOCALID,HLMSGIEN,HLMSGIEN,ERROR)
 .S MFK(SEGIEN+.5)="MFA"_DATA("HDR","FIELD SEPARATOR")_UPDTYPE_DATA("HDR","FIELD SEPARATOR")_STATNUM_DATA("HDR","FIELD SEPARATOR")_NOW_DATA("HDR","FIELD SEPARATOR")_$S($D(MFNERR(SEGIEN)):"U",1:"S")
 ; 05/29/2013 - KJH - TFS8079 - Moved next 3 lines outside the dotted DO since they need to be run when DA exists.
 S DIK="^DGCN(391.91,"
 D ^DIK
 S MFK(SEGIEN+.5)="MFA"_DATA("HDR","FIELD SEPARATOR")_UPDTYPE_DATA("HDR","FIELD SEPARATOR")_STATNUM_DATA("HDR","FIELD SEPARATOR")_NOW_DATA("HDR","FIELD SEPARATOR")_$S($D(MFNERR(SEGIEN)):"U",1:"S")
 Q
 ;
GETVARS(SEGIEN) ;EP - SET VARIABLES
 N MFNCTLID
 S UPDTYPE=$G(DATA(SEGIEN,2,1,1,1))  ;MAD=ADD RECORD;MDL=DELETE RECORD;MUP=UPDATE RECORD
 I UPDTYPE="" D  ;THROW ERROR AND SEND BACK IN MFK
 .S MFNERR(SEGIEN)="MISSING TYPE OF EVENT"
 .D MSGERR(LOCALID,HLMSGIEN,HLMSGIEN,MFNERR(SEGIEN))
 ;05/29/2013 - KJH - TFS8079 - Make 'MFN CONTROL ID' variable name, comment, and error message consistent.
 S MFNCTLID=$G(DATA(SEGIEN,3,1,1,1))   ;MFN CONTROL ID
 I 'MFNCTLID D  ;THROW ERROR AND SEND BACK IN MFK
 .S MFNERR(SEGIEN)="MISSING MFN CONTROL ID"
 .D MSGERR(LOCALID,HLMSGIEN,HLMSGIEN,MFNERR(SEGIEN))
 S DTLASTTR=$P($G(DATA(SEGIEN,4,1,1,1))," ")  ;DATE LAST TREATED
 S DTLASTTR=$P(DTLASTTR,"-",2)_"-"_$P(DTLASTTR,"-",3)_"-"_$P(DTLASTTR,"-")
 S TIMELST=$TR($P($G(DATA(SEGIEN,4,1,1,1))," ",2),":")  ;TIME LAST TREATED
 ;2010-07-10 00:00:00  E.G.
 K %DT S X=DTLASTTR D ^%DT S DTLASTTR=Y
 S DTLASTTR=DTLASTTR_"."_TIMELST
 S EUID=$G(DATA(SEGIEN,5,1,4,1))      ;EUID/ICN
 I EUID="" D   ;THROW ERROR AND SEND BACK IN MFK
 .S MFNERR(SEGIEN)="MISSING EUID"
 .D MSGERR(LOCALID,HLMSGIEN,HLMSGIEN,MFNERR(SEGIEN))
 S STATNUM=$G(DATA(SEGIEN,5,1,2,1))
 I 'STATNUM D
 .S MFNERR(SEGIEN)="NO STATION NUMBER"
 .D MSGERR(LOCALID,HLMSGIEN,HLMSGIEN,MFNERR(SEGIEN))
 S:STATNUM INST=$O(^DIC(4,"D",STATNUM,""))  ;INSTITUTION PTR
 I $G(INST)="" D  ;THROW ERROR AND SEND BACK IN AL ACK
 .S MFNERR(SEGIEN)="CAN'T FIND INSTITUTION FOR GIVEN STATION NUMBER MSG:"_STATNUM
 .D MSGERR(LOCALID,HLMSGIEN,HLMSGIEN,MFNERR(SEGIEN))
 ;05/30/2013 - KJH - TFS8069 - Change message to use 'REMOTE ID' since we have another message using 'LOCALID' already.
 S REMOTEID=$G(DATA(SEGIEN,5,1,1,1))
 I 'REMOTEID D  ;THROW AN ERROR AND SEND BACK IN AL ACK
 .S MFNERR(SEGIEN)="NO REMOTE ID"
 .D MSGERR(LOCALID,HLMSGIEN,HLMSGIEN,MFNERR(SEGIEN))
 I $D(MFNERR(SEGIEN)) D
 .S MFK(SEGIEN+.5)="MFA"_DATA("HDR","FIELD SEPARATOR")_UPDTYPE_DATA("HDR","FIELD SEPARATOR")_STATNUM_DATA("HDR","FIELD SEPARATOR")_NOW_DATA("HDR","FIELD SEPARATOR")_$S($D(MFNERR(SEGIEN)):"U",1:"S")
 ;05/29/2013 - KJH - TFS8079 - Following lines are not needed as MSGERR is called above for each error.
 ;I $D(MFNERR) D
 ;.S ERROR=$G(MFNERR(SEGIEN))
 ;.I ERROR="" S ERROR="UNKNOWN ERROR IN MFN MESSAGE FROM MPI"
 ;.D MSGERR(LOCALID,HLMSGIEN,HLMSGIEN,ERROR)
 Q
 ;
DELPTREC(LOCALID) ;EP - DELETE RECORDS FOR LOCAL ID
 N DIK,IEN,DA
 ;^DGCN(391.91,"B",5571,1)=
 ;^DGCN(391.91,"B",5571,2)=
 S DIK="^DGCN(391.91,"
 S DA=0
 F  S DA=$O(^DGCN(391.91,"B",LOCALID,DA)) Q:'DA  D
 .D ^DIK
 Q
 ;
PARSE(DATA,MIEN,HLMSTATE) ;EP
 N SEG,CNT
 Q:'$$STARTMSG^HLOPRS(.HLMSTATE,MIEN)
 M DATA("HDR")=HLMSTATE("HDR")
 S CNT=0
 F  Q:'$$NEXTSEG^HLOPRS(.HLMSTATE,.SEG)  D
 .S CNT=CNT+1
 .M DATA(CNT)=SEG
 Q
 ;
MSGERR(LOCALID,MSGIEN,HLMSGIEN,ERROR) ;EP - ERROR OCURRED IN A28 ACK
 S AGERROR="MPI MSG MFN:"_HLMSGIEN_": "_ERROR
 D NOTIF^AGMPIHLO(LOCALID,AGERROR)
 Q