MAGDTR03 ;WOIFO/PMK/NST/MKN - Read a DICOM image file ; Mar 27, 2023 12:21 PM
 ;;3.0;IMAGING;**46,54,127,353**;Mar 19, 2002;Build 7
 ;; Per VA Directive 6402, this routine should not be modified.
 ;; +---------------------------------------------------------------+
 ;; | Property of the US Government.                                |
 ;; | No permission to copy or redistribute this software is given. |
 ;; | Use of unreleased versions of this software requires the user |
 ;; | to execute a written test agreement with the VistA Imaging    |
 ;; | Development Office of the Department of Veterans Affairs,     |
 ;; | telephone (301) 734-0100.                                     |
 ;; | The Food and Drug Administration classifies this software as  |
 ;; | a medical device.  As such, it may not be changed in any way. |
 ;; | Modifications to this software may result in an adulterated   |
 ;; | medical device under 21CFR820, the use of which is considered |
 ;; | to be a violation of US Federal Statutes.                     |
 ;; +---------------------------------------------------------------+
 ;;
 ;
ADD(OUT,GMRCIEN,EVENT,IMAGECNT) ; add an entry to the read/unread list
 N ACQSITE ;-- the site where the images were acquired
 N IFCIEN ;--- the IEN of the IFC consult at the reading site
 N IFCSITE ;-- 0 if not IFC
 N ISPECIDX ;- index to specialties - read/unread list sort key
 N IPROCIDX ;- index to procedures - read/unread list sort key (may be null)
 N STATUS ;--- status of unread list entry (Unread or Waiting)
 N TIMESTMP ;- FM date/time
 N TRIGGER ;-- create unread list trigger: Forward, Image, or Order
 N UNREAD ;-- pointer to entry in unread list
 N X
 ; should this consult be added to the Unread List?
 S OUT=0 ; return variable
 Q:$G(EVENT)=""  ; nope, an event F, I, or O must be specified
 Q:$$FINDLIST^MAGDTR01(GMRCIEN,.ISPECIDX,.IPROCIDX,.ACQSITE,.TRIGGER)<1
 S IMAGECNT=+$G(IMAGECNT) ; count of images
 ;
 S UNREAD=$$UNREAD^MAGDTR02(GMRCIEN)
 I UNREAD="",EVENT'[TRIGGER Q  ; nope, don't create it now
 ;
 I UNREAD="",((ISPECIDX="")!(IPROCIDX="")!(ACQSITE="")) Q  ; required fields are blank 
 ;
 ; get IFC information
 S IFCSITE=$$GET1^DIQ(123,GMRCIEN,.07,"I") ; Routing Facility
 S IFCIEN=$$GET1^DIQ(123,GMRCIEN,.06,"I") ; remote consult file entry
 ;
 I UNREAD="" D  ; add record to the unread list
 . N X
 . L +^MAG(2006.5849,0):1E9 ; serial generation
 . S UNREAD=$O(^MAG(2006.5849," "),-1)+1 ; get next IEN
 . L +^MAG(2006.5849,UNREAD):1E9
 . S X=$G(^MAG(2006.5849,0))
 . S X="TELEREADER READ/UNREAD LIST^2006.5849^"_UNREAD_"^"_($P(X,"^",4)+1)
 . S ^MAG(2006.5849,0)=X
 . S ^MAG(2006.5849,UNREAD,0)=GMRCIEN_"^"_ACQSITE_"^"_ISPECIDX_"^"_IPROCIDX
 . L -^MAG(2006.5849,0) ; end serial generation
 . S TIMESTMP=$$TIMESTMP^MAGDTR02(UNREAD)
 . S $P(^MAG(2006.5849,UNREAD,0),"^",7)=TIMESTMP ; acquisition start d/t
 . S ^MAG(2006.5849,"B",GMRCIEN,UNREAD)=""
 . ;
 . ; set status -- if unpaired IFC, status="Waiting", otherwise "Unread"
 . S STATUS=$S(IFCSITE&'IFCIEN:"W",1:"U")
 . S $P(^MAG(2006.5849,UNREAD,0),"^",11)=STATUS
 . S ^MAG(2006.5849,"D",ACQSITE,ISPECIDX,IPROCIDX,STATUS,UNREAD)=""
 . L -^MAG(2006.5849,UNREAD)
 . Q
 E  D
 . S TIMESTMP=$$TIMESTMP^MAGDTR02(UNREAD) ; update d/t piece of last activity
 . Q
 ;
 I IFCSITE D  ; record the IFC remote site times, if they are not set
 . ; save time the entry is added to the unread list, if not set
 . I $P($G(^MAG(2006.5849,UNREAD,0)),"^",5)="" S $P(^(0),"^",5)=TIMESTMP
 . I IFCIEN D
 . . ; IFC remote consult already created - save same time, if not set
 . . I $P($G(^MAG(2006.5849,UNREAD,0)),"^",6)="" S $P(^(0),"^",6)=TIMESTMP
 . . Q
 . Q
 ;
 I IMAGECNT D
 . S $P(^MAG(2006.5849,UNREAD,0),"^",8)=TIMESTMP ; last acquisition d/t
 . S $P(^(0),"^",10)=$P(^MAG(2006.5849,UNREAD,0),"^",10)+IMAGECNT
 . Q
 ;
 S OUT=UNREAD
 Q
 ;
COMPLETE ; entry point from ^MAGDTR01 & ^MAGDTRLU for COMPLETED consults
 N LOCATION,MAGPTR,TIMESTMP,UNREAD
 S UNREAD=$$UNREAD^MAGDTR02(GMRCIEN)
 S TIMESTMP=$$STATUPDT^MAGDTR02(UNREAD,"R")
 I TIMESTMP D  ; changed status to Resulted
 . D FINISH ; record who updated the consult
 . ; check if there is an image
 . S MAGPTR=$O(^MAG(2006.5839,"C",123,GMRCIEN,0))
 . I MAGPTR,'$$TIULAST^MAGDGMRC(GMRCIEN) D
 . . ; there is an image but no TIU note
 . . D TIUNOTE ; create a TIU note for the image
 . . Q
 . Q
 Q
 ;
CANCEL ; entry point from ^MAGDTR01 & ^MAGDTRLU for CANCELLED consults
 N TIMESTMP,UNREAD
 S UNREAD=$$UNREAD^MAGDTR02(GMRCIEN)
 S TIMESTMP=$$STATUPDT^MAGDTR02(UNREAD,"C")
 I TIMESTMP D  ; changed status to Cancelled
 . ; remove any old data about the reading activities
 . S $P(^MAG(2006.5849,UNREAD,0),"^",12,16)="^^^^"
 . D FINISH ; record who cancelled the consult
 . Q
 Q
 ;
FINISH ; finalize resulted or cancelled consult
 N ADDNDM,DUZACQ,FULLNAME,INITIALS,LOCKNAME,X
 ; record who resulted or cancelled the consult
 ;
 I $G(MODE)="REPAIR" G REPAIR ; set in ^MAGDTRLU
 ;
 S ADDNDM=$$ISADDNDM(GMRCIEN) ;P353
 ;
 ; process the transaction
 I $D(HLNEXT) D  ; IFC - data comes from HL7 message
 . D GETHL7B^MAGDTR01(.FULLNAME,.LOCATION)
 . Q:ADDNDM  ;P353
 . ; if resulter is not the person who locked the study, update name
 . S LOCKNAME=$P(^MAG(2006.5849,UNREAD,0),"^",12)
 . I LOCKNAME'=FULLNAME,FULLNAME'="" D  ; not the same person, update
 . . ; look up resulter in file 200 - check for >1 people with same name
 . . S DUZACQ=$$FIND1^DIC(200,"","BX",FULLNAME) ; IA # 10060
 . . S INITIALS=$$GET1^DIQ(200,DUZACQ,1) ; initial
 . . ; DUZread (piece 4) is not known - set to null
 . . S X=FULLNAME_"^"_INITIALS_"^"_DUZACQ_"^^"_LOCATION
 . . S $P(^MAG(2006.5849,UNREAD,0),"^",12,16)=X ; reader identification
 . . Q
 . Q
 E  D  ; local consult - FULLNAME array is for IFCs only
 . ; record the id of the resulter - DUZ comes from RPC session
 . ; assumes that the results were entered directly, not via IFC/HL7
 . Q:ADDNDM  ;P353
 . S FULLNAME=$$GET1^DIQ(200,DUZ,.01)
 . S INITIALS=$$GET1^DIQ(200,DUZ,1) ; initial
 . S X=FULLNAME_"^"_INITIALS_"^"_DUZ_"^"_DUZ_"^"_DUZ(2) ; DUZacq=DUZread
 . S $P(^MAG(2006.5849,UNREAD,0),"^",12,16)=X ; reader identification
 . Q
 ;
 D:'ADDNDM EXREF(UNREAD,TIMESTMP) ; set "E" cross-reference ; P353 added 'ADDNM ('Addendum)
 Q
 ;
EXREF(UNREAD,TIMESTMP) ; set cancellation or reading stop date/time and "E" cross-reference
 N ACQSITE ;-- acquisition site
 N LISTDATA ;- read/unread list data
 Q:UNREAD=""  Q:TIMESTMP=""
 S LISTDATA=^MAG(2006.5849,UNREAD,0)
 S ACQSITE=$P(LISTDATA,"^",2) Q:ACQSITE=""
 S $P(^MAG(2006.5849,UNREAD,0),"^",18)=TIMESTMP
 S ^MAG(2006.5849,"E",ACQSITE,TIMESTMP\1,UNREAD)=""
 Q
 ;
REPAIR ; code to repair a defective unread list entry
 N ACTIVITY,DUZACQ,FULLNAME,HIT,I,IFCSITE,INITIALS,LOCATION,SUBFILE,TIMESTMP
 S IFCSITE=$$GET1^DIQ(123,GMRCIEN,.07,"I") ; routing facility
 ;
 ; first find the consult request tracking "completion" activity in CPRS 
 S HIT=0 F I=1:1 D  Q:HIT  Q:ACTIVITY=""
 . S SUBFILE=I_","_GMRCIEN ; format: <subfile ien>,<gmrc ien>
 . S ACTIVITY=$$GET1^DIQ(123.02,SUBFILE,1) ; activity - from ^GMR(123.1)
 . I ACTIVITY="COMPLETE/UPDATE" S HIT=1
 . E  I ACTIVITY="CANCELLED" S HIT=2
 . E  I ACTIVITY="DISCONTINUED" S HIT=3
 . Q
 I 'HIT S SUBFILE=(I-1)_","_GMRCIEN ; use the last entry
 ;
 ; now make the corrections
 S TIMESTMP=$$GET1^DIQ(123.02,SUBFILE,2,"I") ; date/time of actual activity
 S DUZACQ=$$GET1^DIQ(123.02,SUBFILE,3,"I") ; who's responsible for activity
 I DUZACQ D  ; action was performed locally
 . S FULLNAME=$$GET1^DIQ(200,DUZACQ,.01) ; name
 . S INITIALS=$$GET1^DIQ(200,DUZACQ,1) ; initials
 . S LOCATION=ACQSITE
 . S X=FULLNAME_"^"_INITIALS_"^"_DUZACQ_"^"_DUZACQ_"^"_ACQSITE ; DUZacq=DUZread
 . Q
 E  I IFCSITE D  ; remotely completed IFC
 . S FULLNAME=$$GET1^DIQ(123.02,SUBFILE,.22) ; remote responsible person
 . ; look up resulter in file 200 - check for >1 people with same name
 . S DUZACQ=$$FIND1^DIC(200,"","BX",FULLNAME) ; IA # 10060
 . S INITIALS=$$GET1^DIQ(200,DUZACQ,1) ; initials
 . ; DUZread (piece 4) is not known - set to null
 . S X=FULLNAME_"^"_INITIALS_"^"_DUZACQ_"^^"_IFCSITE
 . Q
 E  S X="^^^^" ; problem with CPRS consult
 S $P(^MAG(2006.5849,UNREAD,0),"^",12,16)=X ; reader identification
 S $P(^MAG(2006.5849,UNREAD,0),"^",19)=$$NOW^XLFDT() ; Record Repair TimeStamp in piece 19 Field #18
 D EXREF(UNREAD,TIMESTMP) ; set "E" cross-reference
 Q
 ;
TIUNOTE ; create a TIU result note, if one is not present
 N DUZ ; this is called by the HL7 - DUZ and DUZ(2) are not set
 N MAGDFN,MAGTEXT,MAGTITLE,UNREAD,XECUTE,ZZ
 Q:'$D(HLNEXT)  ; only create TIU result note for IFCs
 Q:'$$FINDLIST^MAGDTR01(GMRCIEN,,,,,.MAGTITLE)
 S UNREAD=$$UNREAD^MAGDTR02(GMRCIEN) Q:'UNREAD
 S MAGTEXT(1)="Please refer to Inter-facility Consult for results."
 S MAGTEXT(2)=""
 S MAGTEXT(3)="Automatically generated note - signature not required."
 S DUZ=$P(^MAG(2006.5849,UNREAD,0),"^",14) Q:'DUZ  ; get DUZacq
 S DUZ(0)="@",DUZ(2)=LOCATION ; get reading site ien
 S MAGDFN=$$GET1^DIQ(123,GMRCIEN,.02,"I")
 D NEW^MAGGNTI1(.ZZ,MAGDFN,MAGTITLE,1,"E",,DUZ,,,GMRCIEN,.MAGTEXT)
 Q
 ;
ISADDNDM(GMRCIEN) ; P353
 ; Is the current patient consult transaction (activity) an ADDENDUM? 
 ; Return is 0 = NO  or 1 = YES - last consult transaction was an ADDENDUM?
 ;
 N MAGCOMPF,MAGIENS,MAGOUT
 D GETS^DIQ(123,GMRCIEN_",","40*","","MAGOUT")
 S MAGIENS=$O(MAGOUT(123.02,""),-1)
 Q:MAGIENS="" 0
 Q $S($G(MAGOUT(123.02,MAGIENS,1))="ADDENDUM ADDED TO":1,1:0)
 ;
 
--- Routine Detail   --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HMAGDTR03   9586     printed  Sep 23, 2025@19:38:20                                                                                                                                                                                                    Page 2
MAGDTR03  ;WOIFO/PMK/NST/MKN - Read a DICOM image file ; Mar 27, 2023 12:21 PM
 +1       ;;3.0;IMAGING;**46,54,127,353**;Mar 19, 2002;Build 7
 +2       ;; Per VA Directive 6402, this routine should not be modified.
 +3       ;; +---------------------------------------------------------------+
 +4       ;; | Property of the US Government.                                |
 +5       ;; | No permission to copy or redistribute this software is given. |
 +6       ;; | Use of unreleased versions of this software requires the user |
 +7       ;; | to execute a written test agreement with the VistA Imaging    |
 +8       ;; | Development Office of the Department of Veterans Affairs,     |
 +9       ;; | telephone (301) 734-0100.                                     |
 +10      ;; | The Food and Drug Administration classifies this software as  |
 +11      ;; | a medical device.  As such, it may not be changed in any way. |
 +12      ;; | Modifications to this software may result in an adulterated   |
 +13      ;; | medical device under 21CFR820, the use of which is considered |
 +14      ;; | to be a violation of US Federal Statutes.                     |
 +15      ;; +---------------------------------------------------------------+
 +16      ;;
 +17      ;
ADD(OUT,GMRCIEN,EVENT,IMAGECNT) ; add an entry to the read/unread list
 +1       ;-- the site where the images were acquired
           NEW ACQSITE
 +2       ;--- the IEN of the IFC consult at the reading site
           NEW IFCIEN
 +3       ;-- 0 if not IFC
           NEW IFCSITE
 +4       ;- index to specialties - read/unread list sort key
           NEW ISPECIDX
 +5       ;- index to procedures - read/unread list sort key (may be null)
           NEW IPROCIDX
 +6       ;--- status of unread list entry (Unread or Waiting)
           NEW STATUS
 +7       ;- FM date/time
           NEW TIMESTMP
 +8       ;-- create unread list trigger: Forward, Image, or Order
           NEW TRIGGER
 +9       ;-- pointer to entry in unread list
           NEW UNREAD
 +10       NEW X
 +11      ; should this consult be added to the Unread List?
 +12      ; return variable
           SET OUT=0
 +13      ; nope, an event F, I, or O must be specified
           if $GET(EVENT)=""
               QUIT 
 +14       if $$FINDLIST^MAGDTR01(GMRCIEN,.ISPECIDX,.IPROCIDX,.ACQSITE,.TRIGGER)<1
               QUIT 
 +15      ; count of images
           SET IMAGECNT=+$GET(IMAGECNT)
 +16      ;
 +17       SET UNREAD=$$UNREAD^MAGDTR02(GMRCIEN)
 +18      ; nope, don't create it now
           IF UNREAD=""
               IF EVENT'[TRIGGER
                   QUIT 
 +19      ;
 +20      ; required fields are blank 
           IF UNREAD=""
               IF ((ISPECIDX="")!(IPROCIDX="")!(ACQSITE=""))
                   QUIT 
 +21      ;
 +22      ; get IFC information
 +23      ; Routing Facility
           SET IFCSITE=$$GET1^DIQ(123,GMRCIEN,.07,"I")
 +24      ; remote consult file entry
           SET IFCIEN=$$GET1^DIQ(123,GMRCIEN,.06,"I")
 +25      ;
 +26      ; add record to the unread list
           IF UNREAD=""
               Begin DoDot:1
 +27               NEW X
 +28      ; serial generation
                   LOCK +^MAG(2006.5849,0):1E9
 +29      ; get next IEN
                   SET UNREAD=$ORDER(^MAG(2006.5849," "),-1)+1
 +30               LOCK +^MAG(2006.5849,UNREAD):1E9
 +31               SET X=$GET(^MAG(2006.5849,0))
 +32               SET X="TELEREADER READ/UNREAD LIST^2006.5849^"_UNREAD_"^"_($PIECE(X,"^",4)+1)
 +33               SET ^MAG(2006.5849,0)=X
 +34               SET ^MAG(2006.5849,UNREAD,0)=GMRCIEN_"^"_ACQSITE_"^"_ISPECIDX_"^"_IPROCIDX
 +35      ; end serial generation
                   LOCK -^MAG(2006.5849,0)
 +36               SET TIMESTMP=$$TIMESTMP^MAGDTR02(UNREAD)
 +37      ; acquisition start d/t
                   SET $PIECE(^MAG(2006.5849,UNREAD,0),"^",7)=TIMESTMP
 +38               SET ^MAG(2006.5849,"B",GMRCIEN,UNREAD)=""
 +39      ;
 +40      ; set status -- if unpaired IFC, status="Waiting", otherwise "Unread"
 +41               SET STATUS=$SELECT(IFCSITE&'IFCIEN:"W",1:"U")
 +42               SET $PIECE(^MAG(2006.5849,UNREAD,0),"^",11)=STATUS
 +43               SET ^MAG(2006.5849,"D",ACQSITE,ISPECIDX,IPROCIDX,STATUS,UNREAD)=""
 +44               LOCK -^MAG(2006.5849,UNREAD)
 +45               QUIT 
               End DoDot:1
 +46      IF '$TEST
               Begin DoDot:1
 +47      ; update d/t piece of last activity
                   SET TIMESTMP=$$TIMESTMP^MAGDTR02(UNREAD)
 +48               QUIT 
               End DoDot:1
 +49      ;
 +50      ; record the IFC remote site times, if they are not set
           IF IFCSITE
               Begin DoDot:1
 +51      ; save time the entry is added to the unread list, if not set
 +52               IF $PIECE($GET(^MAG(2006.5849,UNREAD,0)),"^",5)=""
                       SET $PIECE(^(0),"^",5)=TIMESTMP
 +53               IF IFCIEN
                       Begin DoDot:2
 +54      ; IFC remote consult already created - save same time, if not set
 +55                       IF $PIECE($GET(^MAG(2006.5849,UNREAD,0)),"^",6)=""
                               SET $PIECE(^(0),"^",6)=TIMESTMP
 +56                       QUIT 
                       End DoDot:2
 +57               QUIT 
               End DoDot:1
 +58      ;
 +59       IF IMAGECNT
               Begin DoDot:1
 +60      ; last acquisition d/t
                   SET $PIECE(^MAG(2006.5849,UNREAD,0),"^",8)=TIMESTMP
 +61               SET $PIECE(^(0),"^",10)=$PIECE(^MAG(2006.5849,UNREAD,0),"^",10)+IMAGECNT
 +62               QUIT 
               End DoDot:1
 +63      ;
 +64       SET OUT=UNREAD
 +65       QUIT 
 +66      ;
COMPLETE  ; entry point from ^MAGDTR01 & ^MAGDTRLU for COMPLETED consults
 +1        NEW LOCATION,MAGPTR,TIMESTMP,UNREAD
 +2        SET UNREAD=$$UNREAD^MAGDTR02(GMRCIEN)
 +3        SET TIMESTMP=$$STATUPDT^MAGDTR02(UNREAD,"R")
 +4       ; changed status to Resulted
           IF TIMESTMP
               Begin DoDot:1
 +5       ; record who updated the consult
                   DO FINISH
 +6       ; check if there is an image
 +7                SET MAGPTR=$ORDER(^MAG(2006.5839,"C",123,GMRCIEN,0))
 +8                IF MAGPTR
                       IF '$$TIULAST^MAGDGMRC(GMRCIEN)
                           Begin DoDot:2
 +9       ; there is an image but no TIU note
 +10      ; create a TIU note for the image
                               DO TIUNOTE
 +11                           QUIT 
                           End DoDot:2
 +12               QUIT 
               End DoDot:1
 +13       QUIT 
 +14      ;
CANCEL    ; entry point from ^MAGDTR01 & ^MAGDTRLU for CANCELLED consults
 +1        NEW TIMESTMP,UNREAD
 +2        SET UNREAD=$$UNREAD^MAGDTR02(GMRCIEN)
 +3        SET TIMESTMP=$$STATUPDT^MAGDTR02(UNREAD,"C")
 +4       ; changed status to Cancelled
           IF TIMESTMP
               Begin DoDot:1
 +5       ; remove any old data about the reading activities
 +6                SET $PIECE(^MAG(2006.5849,UNREAD,0),"^",12,16)="^^^^"
 +7       ; record who cancelled the consult
                   DO FINISH
 +8                QUIT 
               End DoDot:1
 +9        QUIT 
 +10      ;
FINISH    ; finalize resulted or cancelled consult
 +1        NEW ADDNDM,DUZACQ,FULLNAME,INITIALS,LOCKNAME,X
 +2       ; record who resulted or cancelled the consult
 +3       ;
 +4       ; set in ^MAGDTRLU
           IF $GET(MODE)="REPAIR"
               GOTO REPAIR
 +5       ;
 +6       ;P353
           SET ADDNDM=$$ISADDNDM(GMRCIEN)
 +7       ;
 +8       ; process the transaction
 +9       ; IFC - data comes from HL7 message
           IF $DATA(HLNEXT)
               Begin DoDot:1
 +10               DO GETHL7B^MAGDTR01(.FULLNAME,.LOCATION)
 +11      ;P353
                   if ADDNDM
                       QUIT 
 +12      ; if resulter is not the person who locked the study, update name
 +13               SET LOCKNAME=$PIECE(^MAG(2006.5849,UNREAD,0),"^",12)
 +14      ; not the same person, update
                   IF LOCKNAME'=FULLNAME
                       IF FULLNAME'=""
                           Begin DoDot:2
 +15      ; look up resulter in file 200 - check for >1 people with same name
 +16      ; IA # 10060
                               SET DUZACQ=$$FIND1^DIC(200,"","BX",FULLNAME)
 +17      ; initial
                               SET INITIALS=$$GET1^DIQ(200,DUZACQ,1)
 +18      ; DUZread (piece 4) is not known - set to null
 +19                           SET X=FULLNAME_"^"_INITIALS_"^"_DUZACQ_"^^"_LOCATION
 +20      ; reader identification
                               SET $PIECE(^MAG(2006.5849,UNREAD,0),"^",12,16)=X
 +21                           QUIT 
                           End DoDot:2
 +22               QUIT 
               End DoDot:1
 +23      ; local consult - FULLNAME array is for IFCs only
          IF '$TEST
               Begin DoDot:1
 +24      ; record the id of the resulter - DUZ comes from RPC session
 +25      ; assumes that the results were entered directly, not via IFC/HL7
 +26      ;P353
                   if ADDNDM
                       QUIT 
 +27               SET FULLNAME=$$GET1^DIQ(200,DUZ,.01)
 +28      ; initial
                   SET INITIALS=$$GET1^DIQ(200,DUZ,1)
 +29      ; DUZacq=DUZread
                   SET X=FULLNAME_"^"_INITIALS_"^"_DUZ_"^"_DUZ_"^"_DUZ(2)
 +30      ; reader identification
                   SET $PIECE(^MAG(2006.5849,UNREAD,0),"^",12,16)=X
 +31               QUIT 
               End DoDot:1
 +32      ;
 +33      ; set "E" cross-reference ; P353 added 'ADDNM ('Addendum)
           if 'ADDNDM
               DO EXREF(UNREAD,TIMESTMP)
 +34       QUIT 
 +35      ;
EXREF(UNREAD,TIMESTMP) ; set cancellation or reading stop date/time and "E" cross-reference
 +1       ;-- acquisition site
           NEW ACQSITE
 +2       ;- read/unread list data
           NEW LISTDATA
 +3        if UNREAD=""
               QUIT 
           if TIMESTMP=""
               QUIT 
 +4        SET LISTDATA=^MAG(2006.5849,UNREAD,0)
 +5        SET ACQSITE=$PIECE(LISTDATA,"^",2)
           if ACQSITE=""
               QUIT 
 +6        SET $PIECE(^MAG(2006.5849,UNREAD,0),"^",18)=TIMESTMP
 +7        SET ^MAG(2006.5849,"E",ACQSITE,TIMESTMP\1,UNREAD)=""
 +8        QUIT 
 +9       ;
REPAIR    ; code to repair a defective unread list entry
 +1        NEW ACTIVITY,DUZACQ,FULLNAME,HIT,I,IFCSITE,INITIALS,LOCATION,SUBFILE,TIMESTMP
 +2       ; routing facility
           SET IFCSITE=$$GET1^DIQ(123,GMRCIEN,.07,"I")
 +3       ;
 +4       ; first find the consult request tracking "completion" activity in CPRS 
 +5        SET HIT=0
           FOR I=1:1
               Begin DoDot:1
 +6       ; format: <subfile ien>,<gmrc ien>
                   SET SUBFILE=I_","_GMRCIEN
 +7       ; activity - from ^GMR(123.1)
                   SET ACTIVITY=$$GET1^DIQ(123.02,SUBFILE,1)
 +8                IF ACTIVITY="COMPLETE/UPDATE"
                       SET HIT=1
 +9               IF '$TEST
                       IF ACTIVITY="CANCELLED"
                           SET HIT=2
 +10              IF '$TEST
                       IF ACTIVITY="DISCONTINUED"
                           SET HIT=3
 +11               QUIT 
               End DoDot:1
               if HIT
                   QUIT 
               if ACTIVITY=""
                   QUIT 
 +12      ; use the last entry
           IF 'HIT
               SET SUBFILE=(I-1)_","_GMRCIEN
 +13      ;
 +14      ; now make the corrections
 +15      ; date/time of actual activity
           SET TIMESTMP=$$GET1^DIQ(123.02,SUBFILE,2,"I")
 +16      ; who's responsible for activity
           SET DUZACQ=$$GET1^DIQ(123.02,SUBFILE,3,"I")
 +17      ; action was performed locally
           IF DUZACQ
               Begin DoDot:1
 +18      ; name
                   SET FULLNAME=$$GET1^DIQ(200,DUZACQ,.01)
 +19      ; initials
                   SET INITIALS=$$GET1^DIQ(200,DUZACQ,1)
 +20               SET LOCATION=ACQSITE
 +21      ; DUZacq=DUZread
                   SET X=FULLNAME_"^"_INITIALS_"^"_DUZACQ_"^"_DUZACQ_"^"_ACQSITE
 +22               QUIT 
               End DoDot:1
 +23      ; remotely completed IFC
          IF '$TEST
               IF IFCSITE
                   Begin DoDot:1
 +24      ; remote responsible person
                       SET FULLNAME=$$GET1^DIQ(123.02,SUBFILE,.22)
 +25      ; look up resulter in file 200 - check for >1 people with same name
 +26      ; IA # 10060
                       SET DUZACQ=$$FIND1^DIC(200,"","BX",FULLNAME)
 +27      ; initials
                       SET INITIALS=$$GET1^DIQ(200,DUZACQ,1)
 +28      ; DUZread (piece 4) is not known - set to null
 +29                   SET X=FULLNAME_"^"_INITIALS_"^"_DUZACQ_"^^"_IFCSITE
 +30                   QUIT 
                   End DoDot:1
 +31      ; problem with CPRS consult
          IF '$TEST
               SET X="^^^^"
 +32      ; reader identification
           SET $PIECE(^MAG(2006.5849,UNREAD,0),"^",12,16)=X
 +33      ; Record Repair TimeStamp in piece 19 Field #18
           SET $PIECE(^MAG(2006.5849,UNREAD,0),"^",19)=$$NOW^XLFDT()
 +34      ; set "E" cross-reference
           DO EXREF(UNREAD,TIMESTMP)
 +35       QUIT 
 +36      ;
TIUNOTE   ; create a TIU result note, if one is not present
 +1       ; this is called by the HL7 - DUZ and DUZ(2) are not set
           NEW DUZ
 +2        NEW MAGDFN,MAGTEXT,MAGTITLE,UNREAD,XECUTE,ZZ
 +3       ; only create TIU result note for IFCs
           if '$DATA(HLNEXT)
               QUIT 
 +4        if '$$FINDLIST^MAGDTR01(GMRCIEN,,,,,.MAGTITLE)
               QUIT 
 +5        SET UNREAD=$$UNREAD^MAGDTR02(GMRCIEN)
           if 'UNREAD
               QUIT 
 +6        SET MAGTEXT(1)="Please refer to Inter-facility Consult for results."
 +7        SET MAGTEXT(2)=""
 +8        SET MAGTEXT(3)="Automatically generated note - signature not required."
 +9       ; get DUZacq
           SET DUZ=$PIECE(^MAG(2006.5849,UNREAD,0),"^",14)
           if 'DUZ
               QUIT 
 +10      ; get reading site ien
           SET DUZ(0)="@"
           SET DUZ(2)=LOCATION
 +11       SET MAGDFN=$$GET1^DIQ(123,GMRCIEN,.02,"I")
 +12       DO NEW^MAGGNTI1(.ZZ,MAGDFN,MAGTITLE,1,"E",,DUZ,,,GMRCIEN,.MAGTEXT)
 +13       QUIT 
 +14      ;
ISADDNDM(GMRCIEN) ; P353
 +1       ; Is the current patient consult transaction (activity) an ADDENDUM? 
 +2       ; Return is 0 = NO  or 1 = YES - last consult transaction was an ADDENDUM?
 +3       ;
 +4        NEW MAGCOMPF,MAGIENS,MAGOUT
 +5        DO GETS^DIQ(123,GMRCIEN_",","40*","","MAGOUT")
 +6        SET MAGIENS=$ORDER(MAGOUT(123.02,""),-1)
 +7        if MAGIENS=""
               QUIT 0
 +8        QUIT $SELECT($GET(MAGOUT(123.02,MAGIENS,1))="ADDENDUM ADDED TO":1,1:0)
 +9       ;