SCDXMSG0 ;ALB/JRP - AMB CARE MESSAGE BUILDER;07-MAY-1996 ; 23 Oct 98 1:47 PM
;;5.3;Scheduling;**44,59,66,162,387,1015**;AUG 13, 1993;Build 21
;
BUILDHL7(XMITPTR,HL,MID,XMITARRY,INSRTPNT,VALERR) ;Build an HL7 message for an
; entry in the TRANSMITTED OUTPATIENT ENCOUNTER file (#409.73)
;
;Input : XMITPTR - Pointer to entry in TRANSMITTED OUTPATIENT
; ENCOUNTER file (#409.73)
; HL - Array containing HL7 variables - pass by reference
; This is the output of the call to INIT^HLFNC2()
; MID - Message Control ID to use in the MSH segment
; XMITARRY - Array to store HL7 message in (full global ref)
; (Defaults to ^TMP("HLS",$J))
; INSRTPNT - Line to begin inserting message text at
; (Defaults to 1)
;Output : LINES - Number of lines in message (success)
; XMITARRY(N) = Line N of HL7 message
; XMITARRY(N,M) = Continuation number M of line N
; -1^Error - Unable to build message / bad input
;Notes : It is the responsibility of the calling program to
; initialize (i.e. KILL) XMITARRY
; : The MSH segment will not be built if MID is not passed
; : When retransmitting, an EDIT event will be used if the
; OUTPATIENT ENCOUNTER field (#.02) has a value and a
; DELETE event will be used if the DELETED OUTPATIENT
; ENCOUNTER field (#.03) has a value
;
;Check input
S XMITPTR=+$G(XMITPTR)
Q:('$D(^SD(409.73,XMITPTR,0))) "-1^Did not pass valid pointer to Transmitted Outpatient Encounter file"
Q:($O(HL(""))="") "-1^Did not pass variables required to interact with the HL7 package"
S MID=$G(MID)
S XMITARRY=$G(XMITARRY)
S:(XMITARRY="") XMITARRY="^TMP(""HLS"","_$J_")"
S INSRTPNT=$G(INSRTPNT)
S:(INSRTPNT="") INSRTPNT=1
;Declare variables
N ENCDT,NODE,DFN,XMITEVNT,ENCPTR,DELPTR,LINESADD,LINETAG,EVNTDATE
N CURLINE,EVNTHL7,VAFARRY,ORIGMTN,ORIGETN,RESULT
N ERROR,VERROR,SEGMENTS,SEGORDR,SEGNAME,XMITDATE,VAFSTR,ENCNDT
;Get zero node of Transmitted Outpatient Encounter
S NODE=$G(^SD(409.73,XMITPTR,0))
;Get pointer to Outpatient Encounter file
S ENCPTR=+$P(NODE,"^",2)
;Get pointer to Deleted Outpatient Encounter file
S DELPTR=+$P(NODE,"^",3)
;Pointer to either type of encounter not found - done
Q:(('ENCPTR)&('DELPTR)) "-1^Entry in Transmitted Outpatient Encounter file does not reference an encounter"
;Get transmission event
S XMITEVNT=+$P(NODE,"^",5)
;Retransmitting - use EDIT event for Outpatient Encounters and
; DELETE event for Deleted Outpatient Encounter
S:('XMITEVNT) XMITEVNT=$S(ENCPTR:2,1:3)
;Convert event type to HL7 event
; Using A08 for ADD & EDIT and A23 for DELETE
S EVNTHL7="A23"
S:(XMITEVNT'=3) EVNTHL7="A08"
;Get event date/time
S EVNTDATE=+$P(NODE,"^",6)
;Determine patient and encounter date/time
S DFN=0
S (ENCDT,ENCNDT)=0
;Get data from Outpatient Encounter
I (ENCPTR) D
.S NODE=$G(^SCE(ENCPTR,0))
.S DFN=+$P(NODE,"^",2)
.S ENCDT=+$P($P(NODE,"^"),".")
.S ENCNDT=+$P(NODE,"^")
;Get data from Deleted Outpatient Encounter
I (DELPTR) D
.S NODE=$G(^SD(409.74,DELPTR,1))
.S DFN=+$P(NODE,"^",2)
.S ENCDT=+$P($P(NODE,"^"),".")
.S ENCNDT=+$P(NODE,"^")
;Unable to determine patient - done
Q:('DFN) "-1^"_$S(DELPTR:"Deleted ",1:"")_"Outpatient Encounter did not refer to a patient"
;Couldn't determine encounter date/time - use today
S:('$G(ENCDT)) ENCDT=DT
;Build MSH segment if MID was passed
S LINESADD=0
S CURLINE=INSRTPNT
S ERROR=0
I (MID'="") D
.;Remember original message & event types (only applicable to batch)
.S ORIGMTN=HL("MTN")
.S ORIGETN=HL("ETN")
.;Put in message & event types for actual message
.S HL("MTN")="ADT"
.S HL("ETN")=EVNTHL7
.;Build MSH segment
.K RESULT D MSH^HLFNC2(.HL,MID,.RESULT)
.;Reset message & event types to original values
.S HL("MTN")=ORIGMTN
.S HL("ETN")=ORIGETN
.;Error
.I (RESULT="") S ERROR="-1^Unable to create MSH segment" Q
.;Copy MSH segment into HL7 message
.S @XMITARRY@(CURLINE)=RESULT
.;Increment number of lines added
.S LINESADD=LINESADD+1
.;Check for continuation node
.I ($D(RESULT(1))) D
..;Copy continuation into HL7 message
..S @XMITARRY@(CURLINE,1)=RESULT(1)
..;Increment number of lines added
..S LINESADD=LINESADD+1
.;Increment current line number
.S CURLINE=CURLINE+1
;Error building MSH segment - done
Q:(ERROR) ERROR
;Get list of segments
D SEGMENTS^SCDXMSG1(EVNTHL7,"SEGMENTS")
;Loop through list of segments
S (VERROR,ERROR)=0
S SEGORDR=0
F S SEGORDR=+$O(SEGMENTS(SEGORDR)) Q:('SEGORDR) D Q:(ERROR)
.S SEGNAME=""
.F S SEGNAME=$O(SEGMENTS(SEGORDR,SEGNAME)) Q:(SEGNAME="") D Q:(ERROR)
..;Build segment
..S VAFSTR=SEGMENTS(SEGORDR,SEGNAME)
..S VAFARRY="^TMP(""SCDX-XMIT-BLD"","_$J_","""_SEGNAME_""")"
..S LINETAG="BLD"_SEGNAME S ERROR=0 D @LINETAG^SCDXMSG1
..;Error - delete segment & quit
..I (ERROR) S LINETAG="DEL"_SEGNAME D @LINETAG^SCDXMSG1 Q
..;Validate segment, if appropriate
..S LINETAG="VLD"_SEGNAME S ERROR=0 D:$$VSEG() @LINETAG^SCDXMSG1
..I ERROR S VERROR=ERROR
..;Copy segment into HL7 message
..I 'ERROR S LINETAG="CPY"_SEGNAME D @LINETAG^SCDXMSG1
..S ERROR=0
..;Delete segment
..S LINETAG="DEL"_SEGNAME D @LINETAG^SCDXMSG1
..;Increment current line number
..S CURLINE=CURLINE+1
;Error building segment - remove segments already put into HL7
; message & quit
I (ERROR)!(VERROR) D UNWIND^SCDXMSG1(XMITARRY,INSRTPNT) Q $S(ERROR:ERROR,1:VERROR)
;Done
Q LINESADD
;
VSEG() ;Determine if segment should be validated
;All segments for 'add' or 'edit' transactions
;Only PID and PV1 segments for 'delete' transactions
I EVNTHL7="A08" Q 1
I EVNTHL7="A23","PID^PV1"[SEGNAME Q 1
Q 0
SCDXMSG0 ;ALB/JRP - AMB CARE MESSAGE BUILDER;07-MAY-1996 ; 23 Oct 98 1:47 PM
+1 ;;5.3;Scheduling;**44,59,66,162,387,1015**;AUG 13, 1993;Build 21
+2 ;
BUILDHL7(XMITPTR,HL,MID,XMITARRY,INSRTPNT,VALERR) ;Build an HL7 message for an
+1 ; entry in the TRANSMITTED OUTPATIENT ENCOUNTER file (#409.73)
+2 ;
+3 ;Input : XMITPTR - Pointer to entry in TRANSMITTED OUTPATIENT
+4 ; ENCOUNTER file (#409.73)
+5 ; HL - Array containing HL7 variables - pass by reference
+6 ; This is the output of the call to INIT^HLFNC2()
+7 ; MID - Message Control ID to use in the MSH segment
+8 ; XMITARRY - Array to store HL7 message in (full global ref)
+9 ; (Defaults to ^TMP("HLS",$J))
+10 ; INSRTPNT - Line to begin inserting message text at
+11 ; (Defaults to 1)
+12 ;Output : LINES - Number of lines in message (success)
+13 ; XMITARRY(N) = Line N of HL7 message
+14 ; XMITARRY(N,M) = Continuation number M of line N
+15 ; -1^Error - Unable to build message / bad input
+16 ;Notes : It is the responsibility of the calling program to
+17 ; initialize (i.e. KILL) XMITARRY
+18 ; : The MSH segment will not be built if MID is not passed
+19 ; : When retransmitting, an EDIT event will be used if the
+20 ; OUTPATIENT ENCOUNTER field (#.02) has a value and a
+21 ; DELETE event will be used if the DELETED OUTPATIENT
+22 ; ENCOUNTER field (#.03) has a value
+23 ;
+24 ;Check input
+25 SET XMITPTR=+$GET(XMITPTR)
+26 IF ('$DATA(^SD(409.73,XMITPTR,0)))
QUIT "-1^Did not pass valid pointer to Transmitted Outpatient Encounter file"
+27 IF ($ORDER(HL(""))="")
QUIT "-1^Did not pass variables required to interact with the HL7 package"
+28 SET MID=$GET(MID)
+29 SET XMITARRY=$GET(XMITARRY)
+30 IF (XMITARRY="")
SET XMITARRY="^TMP(""HLS"","_$JOB_")"
+31 SET INSRTPNT=$GET(INSRTPNT)
+32 IF (INSRTPNT="")
SET INSRTPNT=1
+33 ;Declare variables
+34 NEW ENCDT,NODE,DFN,XMITEVNT,ENCPTR,DELPTR,LINESADD,LINETAG,EVNTDATE
+35 NEW CURLINE,EVNTHL7,VAFARRY,ORIGMTN,ORIGETN,RESULT
+36 NEW ERROR,VERROR,SEGMENTS,SEGORDR,SEGNAME,XMITDATE,VAFSTR,ENCNDT
+37 ;Get zero node of Transmitted Outpatient Encounter
+38 SET NODE=$GET(^SD(409.73,XMITPTR,0))
+39 ;Get pointer to Outpatient Encounter file
+40 SET ENCPTR=+$PIECE(NODE,"^",2)
+41 ;Get pointer to Deleted Outpatient Encounter file
+42 SET DELPTR=+$PIECE(NODE,"^",3)
+43 ;Pointer to either type of encounter not found - done
+44 IF (('ENCPTR)&('DELPTR))
QUIT "-1^Entry in Transmitted Outpatient Encounter file does not reference an encounter"
+45 ;Get transmission event
+46 SET XMITEVNT=+$PIECE(NODE,"^",5)
+47 ;Retransmitting - use EDIT event for Outpatient Encounters and
+48 ; DELETE event for Deleted Outpatient Encounter
+49 IF ('XMITEVNT)
SET XMITEVNT=$SELECT(ENCPTR:2,1:3)
+50 ;Convert event type to HL7 event
+51 ; Using A08 for ADD & EDIT and A23 for DELETE
+52 SET EVNTHL7="A23"
+53 IF (XMITEVNT'=3)
SET EVNTHL7="A08"
+54 ;Get event date/time
+55 SET EVNTDATE=+$PIECE(NODE,"^",6)
+56 ;Determine patient and encounter date/time
+57 SET DFN=0
+58 SET (ENCDT,ENCNDT)=0
+59 ;Get data from Outpatient Encounter
+60 IF (ENCPTR)
Begin DoDot:1
+61 SET NODE=$GET(^SCE(ENCPTR,0))
+62 SET DFN=+$PIECE(NODE,"^",2)
+63 SET ENCDT=+$PIECE($PIECE(NODE,"^"),".")
+64 SET ENCNDT=+$PIECE(NODE,"^")
End DoDot:1
+65 ;Get data from Deleted Outpatient Encounter
+66 IF (DELPTR)
Begin DoDot:1
+67 SET NODE=$GET(^SD(409.74,DELPTR,1))
+68 SET DFN=+$PIECE(NODE,"^",2)
+69 SET ENCDT=+$PIECE($PIECE(NODE,"^"),".")
+70 SET ENCNDT=+$PIECE(NODE,"^")
End DoDot:1
+71 ;Unable to determine patient - done
+72 IF ('DFN)
QUIT "-1^"_$SELECT(DELPTR:"Deleted ",1:"")_"Outpatient Encounter did not refer to a patient"
+73 ;Couldn't determine encounter date/time - use today
+74 IF ('$GET(ENCDT))
SET ENCDT=DT
+75 ;Build MSH segment if MID was passed
+76 SET LINESADD=0
+77 SET CURLINE=INSRTPNT
+78 SET ERROR=0
+79 IF (MID'="")
Begin DoDot:1
+80 ;Remember original message & event types (only applicable to batch)
+81 SET ORIGMTN=HL("MTN")
+82 SET ORIGETN=HL("ETN")
+83 ;Put in message & event types for actual message
+84 SET HL("MTN")="ADT"
+85 SET HL("ETN")=EVNTHL7
+86 ;Build MSH segment
+87 KILL RESULT
DO MSH^HLFNC2(.HL,MID,.RESULT)
+88 ;Reset message & event types to original values
+89 SET HL("MTN")=ORIGMTN
+90 SET HL("ETN")=ORIGETN
+91 ;Error
+92 IF (RESULT="")
SET ERROR="-1^Unable to create MSH segment"
QUIT
+93 ;Copy MSH segment into HL7 message
+94 SET @XMITARRY@(CURLINE)=RESULT
+95 ;Increment number of lines added
+96 SET LINESADD=LINESADD+1
+97 ;Check for continuation node
+98 IF ($DATA(RESULT(1)))
Begin DoDot:2
+99 ;Copy continuation into HL7 message
+100 SET @XMITARRY@(CURLINE,1)=RESULT(1)
+101 ;Increment number of lines added
+102 SET LINESADD=LINESADD+1
End DoDot:2
+103 ;Increment current line number
+104 SET CURLINE=CURLINE+1
End DoDot:1
+105 ;Error building MSH segment - done
+106 IF (ERROR)
QUIT ERROR
+107 ;Get list of segments
+108 DO SEGMENTS^SCDXMSG1(EVNTHL7,"SEGMENTS")
+109 ;Loop through list of segments
+110 SET (VERROR,ERROR)=0
+111 SET SEGORDR=0
+112 FOR
SET SEGORDR=+$ORDER(SEGMENTS(SEGORDR))
IF ('SEGORDR)
QUIT
Begin DoDot:1
+113 SET SEGNAME=""
+114 FOR
SET SEGNAME=$ORDER(SEGMENTS(SEGORDR,SEGNAME))
IF (SEGNAME="")
QUIT
Begin DoDot:2
+115 ;Build segment
+116 SET VAFSTR=SEGMENTS(SEGORDR,SEGNAME)
+117 SET VAFARRY="^TMP(""SCDX-XMIT-BLD"","_$JOB_","""_SEGNAME_""")"
+118 SET LINETAG="BLD"_SEGNAME
SET ERROR=0
DO @LINETAG^SCDXMSG1
+119 ;Error - delete segment & quit
+120 IF (ERROR)
SET LINETAG="DEL"_SEGNAME
DO @LINETAG^SCDXMSG1
QUIT
+121 ;Validate segment, if appropriate
+122 SET LINETAG="VLD"_SEGNAME
SET ERROR=0
IF $$VSEG()
DO @LINETAG^SCDXMSG1
+123 IF ERROR
SET VERROR=ERROR
+124 ;Copy segment into HL7 message
+125 IF 'ERROR
SET LINETAG="CPY"_SEGNAME
DO @LINETAG^SCDXMSG1
+126 SET ERROR=0
+127 ;Delete segment
+128 SET LINETAG="DEL"_SEGNAME
DO @LINETAG^SCDXMSG1
+129 ;Increment current line number
+130 SET CURLINE=CURLINE+1
End DoDot:2
IF (ERROR)
QUIT
End DoDot:1
IF (ERROR)
QUIT
+131 ;Error building segment - remove segments already put into HL7
+132 ; message & quit
+133 IF (ERROR)!(VERROR)
DO UNWIND^SCDXMSG1(XMITARRY,INSRTPNT)
QUIT $SELECT(ERROR:ERROR,1:VERROR)
+134 ;Done
+135 QUIT LINESADD
+136 ;
VSEG() ;Determine if segment should be validated
+1 ;All segments for 'add' or 'edit' transactions
+2 ;Only PID and PV1 segments for 'delete' transactions
+3 IF EVNTHL7="A08"
QUIT 1
+4 IF EVNTHL7="A23"
IF "PID^PV1"[SEGNAME
QUIT 1
+5 QUIT 0