MAGGI12 ;WOIFO/GEK/SG/JSL - IMAGE FILE API (PROPERTIES) ; 1/13/09 11:20am
 ;;3.0;IMAGING;**93,94,122,138**;Mar 19, 2002;Build 5380;Sep 03, 2013
 ;; Per VHA Directive 2004-038, 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.                     |
 ;; +---------------------------------------------------------------+
 ;;
 Q
 ;
 ;##### RETURNS IEN OF THE GROUP PARENT FOR THE IMAGE
 ;
 ; IEN           Internal Entry Number of the image record
 ;
 ; Return Values
 ; =============
 ;           <0  Error descriptor (see the $$ERROR^MAGUERR)
 ;            0  Image is not a member of a group
 ;           >0  IEN of the group parent
 ;
 ; Notes
 ; =====
 ;
 ; For a deleted image, the function returns the IEN of the group
 ; that the image belonged to before it was marked as deleted.
 ;
GRPIEN(IEN) ; Returns IEN of the Group parent for the Image.
 N ERR,NODE
 S NODE=$$NODE^MAGGI11(IEN,.ERR)
 I NODE=""  D STORE^MAGUERR(ERR)  Q ERR
 Q +$P($G(@NODE@(0)),U,10)  ; GROUP PARENT (14)
 ;
 ;##### ALLOCATES A NEW RECORD IN THE IMAGE FILE (#2005) AND LOCKS IT
 ; 
 ; Return Values
 ; =============
 ;           >0  IEN for the new record in the IMAGE file (#2005)
 ;
 ; Notes
 ; =====
 ;
 ; The placeholder for the new record (^MAG(2005,IEN) node) is LOCKed 
 ; by this function. It is responsibility of the caller to unlock the 
 ; record after it is created or the record creation is canceled.
 ;
NEWIEN() ;
 N DIEN,IEN,NEWIEN,NODE
 S NEWIEN=0
 ;---
 F  D  Q:NEWIEN
 . S IEN=$O(^MAG(2005," "),-1)+1
 . ;--- Check the IMAGE AUDIT file for a deleted image
 . S DIEN=$O(^MAG(2005.1," "),-1)+1
 . S:DIEN>IEN IEN=DIEN
 . ;--- If the record already exists, skip it
 . S NODE=$NA(^MAG(2005,IEN))  Q:$D(@NODE)
 . ;--- Lock the placeholder in order to make sure that nobody
 . ;--- else is trying to allocate it at the same time.
 . D LOCK^DILF(NODE)  E  Q
 . ;--- Double check that the record has not been created after the
 . ;--- previous $D() check and the LOCK command (a race condition)
 . I $D(@NODE)  L -@NODE  Q
 . ;--- Success
 . S NEWIEN=IEN
 . Q
 ;---
 Q NEWIEN
 ;
 ;##### RETURNS THE PARENT DATA (SUB)FILE REFERENCE
 ;
 ; IEN           Internal Entry Number of the image record
 ;
 ; Return Values
 ; =============
 ;           <0  Error descriptor (see the $$ERROR^MAGUERR)
 ;            0  Image does not have a parent file reference
 ;           >0  Parent data file reference (file/subfile number and
 ;               IEN in the PARENT DATA FILE file (#2005.03) at the
 ;               same time).
 ;
PARFILE(IEN) ;
 N ERR,NODE,PARENTFILE,PFN0
 S NODE=$$NODE^MAGGI11(IEN,.ERR)
 I NODE=""  D STORE^MAGUERR(ERR)  Q ERR
 ;--- Check if the image record has a parent data file reference
 S PARENTFILE=$P($G(@NODE@(2)),U,6)  ; PARENT DATA FILE# (16)
 Q:PARENTFILE="" 0
 ;--- Check if the pointer to the PARENT DATA FILE
 ;--- file (#2005.03) is valid
 S PFN0=$G(^MAG(2005.03,PARENTFILE,0))
 Q:PFN0="" $$ERROR^MAGUERR(-34,,IEN,PARENTFILE)
 ;--- Check if file descriptor has a value in FILE POINTER field (.04)
 Q:$P(PFN0,U,4)="" $$ERROR^MAGUERR(-35,,PARENTFILE)
 ;--- Return the reference
 Q PARENTFILE
 ;
 ;##### RETURNS STATUS OF THE IMAGE
 ;
 ; IEN           Internal Entry Number of the image record
 ;
 ; Return Values
 ; =============
 ;           <0  Error descriptor (see the $$ERROR^MAGUERR)
 ;           ""  Status is not defined
 ;           >0  Image status
 ;                 ^01: Status code (internal value)
 ;                 ^02: Description (external value)
 ;
STATUS(IEN) ;
 N ERR,STATUS
 S STATUS=$$IMGST^MAGGI11(IEN,.ERR)
 D:ERR<0 STORE^MAGUERR(ERR)
 Q $S(STATUS>0:STATUS,ERR<0:ERR,1:"")
 ;
 ;##### RETURNS THE VIEWABLE STATUS OF THE IMAGE
 ;
 ; IEN           IEN of the image record in the file #2005
 ;
 ; [FLAGS]       Flags that control execution (can be combined):
 ;
 ;                 Q  Perform the integrity checks
 ;
 ; [.MESSAGES]   Reference to a local array for messages returned
 ;               by the image data checks. A node in this array is
 ;               defined only if the result value contains the 
 ;               corresponding subscript value (e.g. the "Q" node is
 ;               defined only if integrity checks fail and the result 
 ;               contains "Q").
 ;
 ; MESSAGES(
 ;
 ;   "Q")        Message returned by the integrity checks.
 ;
 ;   "R")        Message returned by the Radiology report checks
 ;               (reserved but not implemented)
 ;
 ;   "S")        Message regarding the image status.
 ;
 ;   "T")        Message returned by the TIU note checks.
 ;
 ; Return Values
 ; =============
 ;           ""  Image can be viewed
 ;          ...  One or more characters that indicate why
 ;               the image cannot be viewed "as usual":
 ;
 ;                 D  Deleted image
 ;                 Q  Questionable integrity
 ;                 R  Problem with the Radiology report
 ;                    (reserved but not implemented)
 ;                 S  Check the value of the STATUS field
 ;                 T  Can't view the TIU note
 ;
VIEWSTAT(IEN,FLAGS,MESSAGES) ;
 N MAGCF,MAGVS
 K MESSAGES  S MAGVS="",MAGCF=$G(FLAGS)
 D
 . N BUF,ERR,GRPIEN,NODE,STATUS
 . ;--- Validate IEN and get the image status
 . S STATUS=$$IMGST^MAGGI11(IEN,.ERR)
 . S NODE=$$NODE^MAGGI11(IEN)
 . ;--- Force the integrity check in case of error(s)
 . I (ERR<0)!(NODE="")  S MAGCF=MAGCF_"Q" Q
 . ; gek/ P94t7  if IEN is in a group, we also mark child as "T" if Grp is "T"
 . S GRPIEN=+$P($G(@NODE@(0)),U,10)
 . I GRPIEN D
 . . N GBUF,GNODE
 . . S GNODE=$$NODE^MAGGI11(GRPIEN)
 . . S GBUF=$G(@GNODE@(2))
 . . D:+$P(GBUF,U,6)=8925
 . . . N IEN,TMP
 . . . S IEN=+$P(GBUF,U,7)  Q:'IEN
 . . . S TMP=$$CANDO^TIULP(IEN,"VIEW")  ;IA#2322
 . . . S:'TMP MAGVS=MAGVS_"T",MESSAGES("T")=$P(TMP,U,2)
 . . . Q
 . . Q
 . I MAGVS["T" Q
 . ; gek/ p94t7 __ Done changes
 . ;--- Check the image status
 . D:STATUS'<10
 . . I +STATUS=12  S MAGVS=MAGVS_"D"  Q
 . . ;Patch 140 Status of 13 will continue and MESSAGES("S") will have reason
 . . S MAGVS=MAGVS_"S"
 . . S MESSAGES("S")=$$MSG^MAGUERR(-33,,$P(STATUS,U,2))
 . . Q
 . ;--- Check if the TIU note can be viewed
 . S BUF=$G(@NODE@(2))
 . D:+$P(BUF,U,6)=8925
 . . N IEN,TMP  S IEN=+$P(BUF,U,7)  Q:'IEN
 . . S TMP=$$CANDO^TIULP(IEN,"VIEW")  ;IA#2322
 . . S:'TMP MAGVS=MAGVS_"T",MESSAGES("T")=$P(TMP,U,2)
 . . Q
 . ;--- Check the status of the group if necessary
 . I MAGVS'["D",MAGVS'["S"  D
 . . S GRPIEN=$P($G(@NODE@(0)),U,10)   Q:GRPIEN'>0  ; GROUP PARENT
 . . S STATUS=$$IMGST^MAGGI11(GRPIEN)  Q:STATUS<10
 . . ;--- Force the integrity check if the existing
 . . ;--- image entry belongs to a deleted group.
 . . I +STATUS=12  S MAGCF=MAGCF_"Q" Q
 . . ;--- Extend the "non-viewable" group status to the image
 . . S MAGVS=MAGVS_"S"
 . . S MESSAGES("S")=$$MSG^MAGUERR(-33,,$P(STATUS,U,2))
 . . Q
 . Q
 ;
 ;--- Radiology report
 ; Reserved but not implemented
 ;
 ;--- Questionable integrity
 D:MAGCF["Q"
 . N MAGQI
 . D CHK^MAGGSQI(.MAGQI,IEN)
 . S:'$G(MAGQI(0)) MAGVS=MAGVS_"Q",MESSAGES("Q")=$P(MAGQI(0),U,2)
 . Q
 ;
 ;---
 Q MAGVS
 ;+++++   ANNSTAT     *******
 ;  Determine if this image can be annotated.
 ;  *** INPUT Parameters  
 ;  
 ;  VSTCODE    : The Image 'View Status' numeric code.
 ;    if this isn't sent as a parameter, it is computed.
 ;   
 ;  MAGIEN     : Image IEN (^MAG(2005))
 ;
 ; *** OUTPUT Parameters  (optional)
 ;  DESC    : Text Reason for the result;
 ;  
 ; Result Values
 ; --------------------------
 ;  RESULT of this call is a numeric value 
 ;     0 : success, image can be annotated.
 ;     
 ;          Values of STATUS field (113) that are okay. 
 ;     1 :  Viewable   (RESULT will be changed to 0)
 ;     2 :  QA Reviewed (RESULT will be changed to 0)
 ;     
 ;     
 ;          Values of STATUS field (113) 
 ;                   that will block Annotation.
 ;     10 : Image capture is in progress, (Future for groups.)
 ;     11 : Image Needs Review
 ;     12 : Image is Deleted.
 ;     13 : Image Never Existed.  ;p130
 ;     
 ;          Values of Image "View Status" that will block Annotation
 ;          ("View Status" is a computed value, not an IMAGE Field)
 ;     21 : Image has Questionable Integrity issues.
 ;     22 : User cannot "VIEW" the associated TIU Note. 
 ;                 due to TIU Business Rules
 ;     23 : Image is not Viewable by Radiology Rule (Future ) 
 ;     
 ;          Values of other codes specific for blocked Annotation 
 ;     30 : User Cannot "MAKE ADDENDUM" to the associated TIU Note. 
 ;                 due to TIU Business Rules
 ;     31 : Image in Rescinded.
 ;    
 ;   
ANNSTAT(MAGIEN,VSTCODE,DESC) ;Annotation Status
 N ANCODE,FLG,STI,VSTCD,TIUIEN,AMND,MSG,X,X2,GRPIEN
 ;
 S ANCODE=$G(VSTCODE)
 S DESC=""
 I ANCODE="" D
 . ; compute View Status Code if not sent.
 . ; QI Check already done in MAGAII ;S FLG="Q"
 . S FLG=""
 . S VSTCD=$$VIEWSTAT(MAGIEN,FLG,.MSG)
 . ;   example of Result from $$VIEWSTAT 
 . ;   ""    : success , viewable
 . ;   "QT"  : a Text String if not viewable   
 . ;   
 . ;   -MSG- : is an array of reasons for failure, each letter 
 . ;           of Result is node of array i.e. MSG("Q")="..."
 . ;           MSG("T")="..." 
 . ;    for this call ANNSTAT, we will just get first entry
 . ;
 . ;if the View Status Code (VSTCD) is not "", return it's text in MSG
 . I VSTCD]"" S X=$O(MSG("")) S DESC=$G(MSG(X))
 . S STI=+$P($G(^MAG(2005,MAGIEN,100)),"^",8)       ; STATUS (113)
 . ;--- Now get View Status Numeric value
 . S ANCODE=$$VSTCODE^MAGGAII(VSTCD,STI)
 . Q
 ;
 ; if the View Status Code > 9 then user cannot view the image, so
 ;   we won't let user annotate the image.
 ;   and will get a generic description of the code, if DESC is still ""
 I (ANCODE>9) S DESC=$S(DESC="":$$VSTTEXT(ANCODE),1:DESC) Q ANCODE
 ; if Image is attached to TIU Note, we check TIU Business Rules.
 S GRPIEN=$P($G(^MAG(2005,MAGIEN,0)),U,10),GRPIEN=$S(+GRPIEN:GRPIEN,1:MAGIEN)
 S X2=$G(^MAG(2005,GRPIEN,2))
 I $P(X2,"^",6)=8925 D
 . S TIUIEN=$P(X2,"^",7)
 . ; If we don't have a pointer to 8925, that is DB corruption
 . ; This would probably already be caught, 
 . ;    but check in case. VIEWSTAT wasn't run prior to this call.
 . I 'TIUIEN S ANCODE=21,DESC="Invalid pointer to TIU Package ("_TIUIEN_")" Q
 . S AMND=$$CANDO^TIULP(TIUIEN,"MAKE ADDENDUM")  ;IA#2322
 . ; Example of result from TIULP
 . ;   0^ You may not ADDEND this UNSIGNED PRIMARY CARE.
 . ;   1
 . I 'AMND S ANCODE=30,DESC=$P(AMND,"^",2)
 . Q
 ; if ANCODE > 9 then TIU Business rule says no Annotation.
 I (ANCODE>9) S DESC=$S(DESC="":$$VSTTEXT(ANCODE),1:DESC) Q ANCODE
 ;
 I $P($G(^MAG(2005,MAGIEN,100)),"^",12)=1 D
 . ; Image is Rescinded
 . S ANCODE=31
 . Q
 ; if ANCODE > 9 then Image is rescinded and stop Annotation.
 I (ANCODE>9) S DESC=$$VSTTEXT(ANCODE) Q ANCODE
 ; If we get here, Image can be annotated.
 ; - not changing to 0.  Status here is 1 or 2 for viewable.
 ;S DESC="Image can be annotated."
 ; Send exact numeric code.  Delphi understands < 10 is viewable
 ; and can be annotated.
 S DESC=$$VSTTEXT(ANCODE)
 Q ANCODE
 ;
 ; ##### VSTTEXT  returns a Description of the Status Code.
 ; 
 ; CODE is the numeric code for the Annotation Status.
 ;    This is based on Status, Viewable Status, and 
 ;    two new return values for Annotation Status. 
 ;    
VSTTEXT(CODE) ;Description for Annotation Status
 N I,DONE,STR
 S (I,DONE)=0
 F  D  Q:DONE
 . S I=I+1
 . S STR=$T(DATA+I)
 . I $P(STR,";",3)=CODE S DESC=$P(STR,";",4),DONE=1
 . I $P(STR,";",3)="END" S DESC=$P(STR,";",4)_CODE,DONE=1
 . Q
 Q DESC
 ; ##### DATA Table for Image Status, View Status,  Annotation Status.
 ;    
 ;    the Code is in Piece 3, and the Description is in Piece 4.
 ;    
 ; Image STATUS field (113) 
 ;    Codes 0..12 are the Status Codes from field 113
 ;    
 ; Viewable Status and Annotation Status are computed values, 
 ;    based on, but not equal to STATUS (113)
 ;    they depend on TIU Business Rules, QI Of image, User Keys.
 ;    The Codes are computed in other functions.  Description of the 
 ;    code is returned from this table.
 ;
 ;    Codes 21..23 are the Viewable Status Codes. (computed value)
 ;    Codes 30..31 are Annotation Status Codes.
DATA ;
 ;;0;Image is Viewable;
 ;;1;Image is Viewable.;
 ;;2;Image is QA Reviewed.;
 ; 
 ;;10;Image capture is in progress.; (Future for groups.)
 ;;11;Image needs review.;
 ;;12;Image is deleted.;
 ;;13;Image Never Existed.;  << p130 for images that didn't exist.
 ;     
 ;;21;Image has Questionable Integrity issues.;
 ;;22;User cannot 'View' the associated TIU note. TIU Business Rule.;
 ;;23;Image is not Viewable by Radiology Rule.; (future) 
 ;     
 ;;30;User cannot 'Make Addendum' to the associated TIU Note. TIU Business Rule.;
 ;;31;Image is Rescinded.;
 ;;END;User cannot Annotate the Image. Unknown CODE: ;
 Q
 
--- Routine Detail   --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HMAGGI12   13927     printed  Sep 23, 2025@19:38:40                                                                                                                                                                                                    Page 2
MAGGI12   ;WOIFO/GEK/SG/JSL - IMAGE FILE API (PROPERTIES) ; 1/13/09 11:20am
 +1       ;;3.0;IMAGING;**93,94,122,138**;Mar 19, 2002;Build 5380;Sep 03, 2013
 +2       ;; Per VHA Directive 2004-038, 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       QUIT 
 +18      ;
 +19      ;##### RETURNS IEN OF THE GROUP PARENT FOR THE IMAGE
 +20      ;
 +21      ; IEN           Internal Entry Number of the image record
 +22      ;
 +23      ; Return Values
 +24      ; =============
 +25      ;           <0  Error descriptor (see the $$ERROR^MAGUERR)
 +26      ;            0  Image is not a member of a group
 +27      ;           >0  IEN of the group parent
 +28      ;
 +29      ; Notes
 +30      ; =====
 +31      ;
 +32      ; For a deleted image, the function returns the IEN of the group
 +33      ; that the image belonged to before it was marked as deleted.
 +34      ;
GRPIEN(IEN) ; Returns IEN of the Group parent for the Image.
 +1        NEW ERR,NODE
 +2        SET NODE=$$NODE^MAGGI11(IEN,.ERR)
 +3        IF NODE=""
               DO STORE^MAGUERR(ERR)
               QUIT ERR
 +4       ; GROUP PARENT (14)
           QUIT +$PIECE($GET(@NODE@(0)),U,10)
 +5       ;
 +6       ;##### ALLOCATES A NEW RECORD IN THE IMAGE FILE (#2005) AND LOCKS IT
 +7       ; 
 +8       ; Return Values
 +9       ; =============
 +10      ;           >0  IEN for the new record in the IMAGE file (#2005)
 +11      ;
 +12      ; Notes
 +13      ; =====
 +14      ;
 +15      ; The placeholder for the new record (^MAG(2005,IEN) node) is LOCKed 
 +16      ; by this function. It is responsibility of the caller to unlock the 
 +17      ; record after it is created or the record creation is canceled.
 +18      ;
NEWIEN()  ;
 +1        NEW DIEN,IEN,NEWIEN,NODE
 +2        SET NEWIEN=0
 +3       ;---
 +4        FOR 
               Begin DoDot:1
 +5                SET IEN=$ORDER(^MAG(2005," "),-1)+1
 +6       ;--- Check the IMAGE AUDIT file for a deleted image
 +7                SET DIEN=$ORDER(^MAG(2005.1," "),-1)+1
 +8                if DIEN>IEN
                       SET IEN=DIEN
 +9       ;--- If the record already exists, skip it
 +10               SET NODE=$NAME(^MAG(2005,IEN))
                   if $DATA(@NODE)
                       QUIT 
 +11      ;--- Lock the placeholder in order to make sure that nobody
 +12      ;--- else is trying to allocate it at the same time.
 +13               DO LOCK^DILF(NODE)
                  IF '$TEST
                       QUIT 
 +14      ;--- Double check that the record has not been created after the
 +15      ;--- previous $D() check and the LOCK command (a race condition)
 +16               IF $DATA(@NODE)
                       LOCK -@NODE
                       QUIT 
 +17      ;--- Success
 +18               SET NEWIEN=IEN
 +19               QUIT 
               End DoDot:1
               if NEWIEN
                   QUIT 
 +20      ;---
 +21       QUIT NEWIEN
 +22      ;
 +23      ;##### RETURNS THE PARENT DATA (SUB)FILE REFERENCE
 +24      ;
 +25      ; IEN           Internal Entry Number of the image record
 +26      ;
 +27      ; Return Values
 +28      ; =============
 +29      ;           <0  Error descriptor (see the $$ERROR^MAGUERR)
 +30      ;            0  Image does not have a parent file reference
 +31      ;           >0  Parent data file reference (file/subfile number and
 +32      ;               IEN in the PARENT DATA FILE file (#2005.03) at the
 +33      ;               same time).
 +34      ;
PARFILE(IEN) ;
 +1        NEW ERR,NODE,PARENTFILE,PFN0
 +2        SET NODE=$$NODE^MAGGI11(IEN,.ERR)
 +3        IF NODE=""
               DO STORE^MAGUERR(ERR)
               QUIT ERR
 +4       ;--- Check if the image record has a parent data file reference
 +5       ; PARENT DATA FILE# (16)
           SET PARENTFILE=$PIECE($GET(@NODE@(2)),U,6)
 +6        if PARENTFILE=""
               QUIT 0
 +7       ;--- Check if the pointer to the PARENT DATA FILE
 +8       ;--- file (#2005.03) is valid
 +9        SET PFN0=$GET(^MAG(2005.03,PARENTFILE,0))
 +10       if PFN0=""
               QUIT $$ERROR^MAGUERR(-34,,IEN,PARENTFILE)
 +11      ;--- Check if file descriptor has a value in FILE POINTER field (.04)
 +12       if $PIECE(PFN0,U,4)=""
               QUIT $$ERROR^MAGUERR(-35,,PARENTFILE)
 +13      ;--- Return the reference
 +14       QUIT PARENTFILE
 +15      ;
 +16      ;##### RETURNS STATUS OF THE IMAGE
 +17      ;
 +18      ; IEN           Internal Entry Number of the image record
 +19      ;
 +20      ; Return Values
 +21      ; =============
 +22      ;           <0  Error descriptor (see the $$ERROR^MAGUERR)
 +23      ;           ""  Status is not defined
 +24      ;           >0  Image status
 +25      ;                 ^01: Status code (internal value)
 +26      ;                 ^02: Description (external value)
 +27      ;
STATUS(IEN) ;
 +1        NEW ERR,STATUS
 +2        SET STATUS=$$IMGST^MAGGI11(IEN,.ERR)
 +3        if ERR<0
               DO STORE^MAGUERR(ERR)
 +4        QUIT $SELECT(STATUS>0:STATUS,ERR<0:ERR,1:"")
 +5       ;
 +6       ;##### RETURNS THE VIEWABLE STATUS OF THE IMAGE
 +7       ;
 +8       ; IEN           IEN of the image record in the file #2005
 +9       ;
 +10      ; [FLAGS]       Flags that control execution (can be combined):
 +11      ;
 +12      ;                 Q  Perform the integrity checks
 +13      ;
 +14      ; [.MESSAGES]   Reference to a local array for messages returned
 +15      ;               by the image data checks. A node in this array is
 +16      ;               defined only if the result value contains the 
 +17      ;               corresponding subscript value (e.g. the "Q" node is
 +18      ;               defined only if integrity checks fail and the result 
 +19      ;               contains "Q").
 +20      ;
 +21      ; MESSAGES(
 +22      ;
 +23      ;   "Q")        Message returned by the integrity checks.
 +24      ;
 +25      ;   "R")        Message returned by the Radiology report checks
 +26      ;               (reserved but not implemented)
 +27      ;
 +28      ;   "S")        Message regarding the image status.
 +29      ;
 +30      ;   "T")        Message returned by the TIU note checks.
 +31      ;
 +32      ; Return Values
 +33      ; =============
 +34      ;           ""  Image can be viewed
 +35      ;          ...  One or more characters that indicate why
 +36      ;               the image cannot be viewed "as usual":
 +37      ;
 +38      ;                 D  Deleted image
 +39      ;                 Q  Questionable integrity
 +40      ;                 R  Problem with the Radiology report
 +41      ;                    (reserved but not implemented)
 +42      ;                 S  Check the value of the STATUS field
 +43      ;                 T  Can't view the TIU note
 +44      ;
VIEWSTAT(IEN,FLAGS,MESSAGES) ;
 +1        NEW MAGCF,MAGVS
 +2        KILL MESSAGES
           SET MAGVS=""
           SET MAGCF=$GET(FLAGS)
 +3        Begin DoDot:1
 +4            NEW BUF,ERR,GRPIEN,NODE,STATUS
 +5       ;--- Validate IEN and get the image status
 +6            SET STATUS=$$IMGST^MAGGI11(IEN,.ERR)
 +7            SET NODE=$$NODE^MAGGI11(IEN)
 +8       ;--- Force the integrity check in case of error(s)
 +9            IF (ERR<0)!(NODE="")
                   SET MAGCF=MAGCF_"Q"
                   QUIT 
 +10      ; gek/ P94t7  if IEN is in a group, we also mark child as "T" if Grp is "T"
 +11           SET GRPIEN=+$PIECE($GET(@NODE@(0)),U,10)
 +12           IF GRPIEN
                   Begin DoDot:2
 +13                   NEW GBUF,GNODE
 +14                   SET GNODE=$$NODE^MAGGI11(GRPIEN)
 +15                   SET GBUF=$GET(@GNODE@(2))
 +16                   if +$PIECE(GBUF,U,6)=8925
                           Begin DoDot:3
 +17                           NEW IEN,TMP
 +18                           SET IEN=+$PIECE(GBUF,U,7)
                               if 'IEN
                                   QUIT 
 +19      ;IA#2322
                               SET TMP=$$CANDO^TIULP(IEN,"VIEW")
 +20                           if 'TMP
                                   SET MAGVS=MAGVS_"T"
                                   SET MESSAGES("T")=$PIECE(TMP,U,2)
 +21                           QUIT 
                           End DoDot:3
 +22                   QUIT 
                   End DoDot:2
 +23           IF MAGVS["T"
                   QUIT 
 +24      ; gek/ p94t7 __ Done changes
 +25      ;--- Check the image status
 +26           if STATUS'<10
                   Begin DoDot:2
 +27                   IF +STATUS=12
                           SET MAGVS=MAGVS_"D"
                           QUIT 
 +28      ;Patch 140 Status of 13 will continue and MESSAGES("S") will have reason
 +29                   SET MAGVS=MAGVS_"S"
 +30                   SET MESSAGES("S")=$$MSG^MAGUERR(-33,,$PIECE(STATUS,U,2))
 +31                   QUIT 
                   End DoDot:2
 +32      ;--- Check if the TIU note can be viewed
 +33           SET BUF=$GET(@NODE@(2))
 +34           if +$PIECE(BUF,U,6)=8925
                   Begin DoDot:2
 +35                   NEW IEN,TMP
                       SET IEN=+$PIECE(BUF,U,7)
                       if 'IEN
                           QUIT 
 +36      ;IA#2322
                       SET TMP=$$CANDO^TIULP(IEN,"VIEW")
 +37                   if 'TMP
                           SET MAGVS=MAGVS_"T"
                           SET MESSAGES("T")=$PIECE(TMP,U,2)
 +38                   QUIT 
                   End DoDot:2
 +39      ;--- Check the status of the group if necessary
 +40           IF MAGVS'["D"
                   IF MAGVS'["S"
                       Begin DoDot:2
 +41      ; GROUP PARENT
                           SET GRPIEN=$PIECE($GET(@NODE@(0)),U,10)
                           if GRPIEN'>0
                               QUIT 
 +42                       SET STATUS=$$IMGST^MAGGI11(GRPIEN)
                           if STATUS<10
                               QUIT 
 +43      ;--- Force the integrity check if the existing
 +44      ;--- image entry belongs to a deleted group.
 +45                       IF +STATUS=12
                               SET MAGCF=MAGCF_"Q"
                               QUIT 
 +46      ;--- Extend the "non-viewable" group status to the image
 +47                       SET MAGVS=MAGVS_"S"
 +48                       SET MESSAGES("S")=$$MSG^MAGUERR(-33,,$PIECE(STATUS,U,2))
 +49                       QUIT 
                       End DoDot:2
 +50           QUIT 
           End DoDot:1
 +51      ;
 +52      ;--- Radiology report
 +53      ; Reserved but not implemented
 +54      ;
 +55      ;--- Questionable integrity
 +56       if MAGCF["Q"
               Begin DoDot:1
 +57               NEW MAGQI
 +58               DO CHK^MAGGSQI(.MAGQI,IEN)
 +59               if '$GET(MAGQI(0))
                       SET MAGVS=MAGVS_"Q"
                       SET MESSAGES("Q")=$PIECE(MAGQI(0),U,2)
 +60               QUIT 
               End DoDot:1
 +61      ;
 +62      ;---
 +63       QUIT MAGVS
 +64      ;+++++   ANNSTAT     *******
 +65      ;  Determine if this image can be annotated.
 +66      ;  *** INPUT Parameters  
 +67      ;  
 +68      ;  VSTCODE    : The Image 'View Status' numeric code.
 +69      ;    if this isn't sent as a parameter, it is computed.
 +70      ;   
 +71      ;  MAGIEN     : Image IEN (^MAG(2005))
 +72      ;
 +73      ; *** OUTPUT Parameters  (optional)
 +74      ;  DESC    : Text Reason for the result;
 +75      ;  
 +76      ; Result Values
 +77      ; --------------------------
 +78      ;  RESULT of this call is a numeric value 
 +79      ;     0 : success, image can be annotated.
 +80      ;     
 +81      ;          Values of STATUS field (113) that are okay. 
 +82      ;     1 :  Viewable   (RESULT will be changed to 0)
 +83      ;     2 :  QA Reviewed (RESULT will be changed to 0)
 +84      ;     
 +85      ;     
 +86      ;          Values of STATUS field (113) 
 +87      ;                   that will block Annotation.
 +88      ;     10 : Image capture is in progress, (Future for groups.)
 +89      ;     11 : Image Needs Review
 +90      ;     12 : Image is Deleted.
 +91      ;     13 : Image Never Existed.  ;p130
 +92      ;     
 +93      ;          Values of Image "View Status" that will block Annotation
 +94      ;          ("View Status" is a computed value, not an IMAGE Field)
 +95      ;     21 : Image has Questionable Integrity issues.
 +96      ;     22 : User cannot "VIEW" the associated TIU Note. 
 +97      ;                 due to TIU Business Rules
 +98      ;     23 : Image is not Viewable by Radiology Rule (Future ) 
 +99      ;     
 +100     ;          Values of other codes specific for blocked Annotation 
 +101     ;     30 : User Cannot "MAKE ADDENDUM" to the associated TIU Note. 
 +102     ;                 due to TIU Business Rules
 +103     ;     31 : Image in Rescinded.
 +104     ;    
 +105     ;   
ANNSTAT(MAGIEN,VSTCODE,DESC) ;Annotation Status
 +1        NEW ANCODE,FLG,STI,VSTCD,TIUIEN,AMND,MSG,X,X2,GRPIEN
 +2       ;
 +3        SET ANCODE=$GET(VSTCODE)
 +4        SET DESC=""
 +5        IF ANCODE=""
               Begin DoDot:1
 +6       ; compute View Status Code if not sent.
 +7       ; QI Check already done in MAGAII ;S FLG="Q"
 +8                SET FLG=""
 +9                SET VSTCD=$$VIEWSTAT(MAGIEN,FLG,.MSG)
 +10      ;   example of Result from $$VIEWSTAT 
 +11      ;   ""    : success , viewable
 +12      ;   "QT"  : a Text String if not viewable   
 +13      ;   
 +14      ;   -MSG- : is an array of reasons for failure, each letter 
 +15      ;           of Result is node of array i.e. MSG("Q")="..."
 +16      ;           MSG("T")="..." 
 +17      ;    for this call ANNSTAT, we will just get first entry
 +18      ;
 +19      ;if the View Status Code (VSTCD) is not "", return it's text in MSG
 +20               IF VSTCD]""
                       SET X=$ORDER(MSG(""))
                       SET DESC=$GET(MSG(X))
 +21      ; STATUS (113)
                   SET STI=+$PIECE($GET(^MAG(2005,MAGIEN,100)),"^",8)
 +22      ;--- Now get View Status Numeric value
 +23               SET ANCODE=$$VSTCODE^MAGGAII(VSTCD,STI)
 +24               QUIT 
               End DoDot:1
 +25      ;
 +26      ; if the View Status Code > 9 then user cannot view the image, so
 +27      ;   we won't let user annotate the image.
 +28      ;   and will get a generic description of the code, if DESC is still ""
 +29       IF (ANCODE>9)
               SET DESC=$SELECT(DESC="":$$VSTTEXT(ANCODE),1:DESC)
               QUIT ANCODE
 +30      ; if Image is attached to TIU Note, we check TIU Business Rules.
 +31       SET GRPIEN=$PIECE($GET(^MAG(2005,MAGIEN,0)),U,10)
           SET GRPIEN=$SELECT(+GRPIEN:GRPIEN,1:MAGIEN)
 +32       SET X2=$GET(^MAG(2005,GRPIEN,2))
 +33       IF $PIECE(X2,"^",6)=8925
               Begin DoDot:1
 +34               SET TIUIEN=$PIECE(X2,"^",7)
 +35      ; If we don't have a pointer to 8925, that is DB corruption
 +36      ; This would probably already be caught, 
 +37      ;    but check in case. VIEWSTAT wasn't run prior to this call.
 +38               IF 'TIUIEN
                       SET ANCODE=21
                       SET DESC="Invalid pointer to TIU Package ("_TIUIEN_")"
                       QUIT 
 +39      ;IA#2322
                   SET AMND=$$CANDO^TIULP(TIUIEN,"MAKE ADDENDUM")
 +40      ; Example of result from TIULP
 +41      ;   0^ You may not ADDEND this UNSIGNED PRIMARY CARE.
 +42      ;   1
 +43               IF 'AMND
                       SET ANCODE=30
                       SET DESC=$PIECE(AMND,"^",2)
 +44               QUIT 
               End DoDot:1
 +45      ; if ANCODE > 9 then TIU Business rule says no Annotation.
 +46       IF (ANCODE>9)
               SET DESC=$SELECT(DESC="":$$VSTTEXT(ANCODE),1:DESC)
               QUIT ANCODE
 +47      ;
 +48       IF $PIECE($GET(^MAG(2005,MAGIEN,100)),"^",12)=1
               Begin DoDot:1
 +49      ; Image is Rescinded
 +50               SET ANCODE=31
 +51               QUIT 
               End DoDot:1
 +52      ; if ANCODE > 9 then Image is rescinded and stop Annotation.
 +53       IF (ANCODE>9)
               SET DESC=$$VSTTEXT(ANCODE)
               QUIT ANCODE
 +54      ; If we get here, Image can be annotated.
 +55      ; - not changing to 0.  Status here is 1 or 2 for viewable.
 +56      ;S DESC="Image can be annotated."
 +57      ; Send exact numeric code.  Delphi understands < 10 is viewable
 +58      ; and can be annotated.
 +59       SET DESC=$$VSTTEXT(ANCODE)
 +60       QUIT ANCODE
 +61      ;
 +62      ; ##### VSTTEXT  returns a Description of the Status Code.
 +63      ; 
 +64      ; CODE is the numeric code for the Annotation Status.
 +65      ;    This is based on Status, Viewable Status, and 
 +66      ;    two new return values for Annotation Status. 
 +67      ;    
VSTTEXT(CODE) ;Description for Annotation Status
 +1        NEW I,DONE,STR
 +2        SET (I,DONE)=0
 +3        FOR 
               Begin DoDot:1
 +4                SET I=I+1
 +5                SET STR=$TEXT(DATA+I)
 +6                IF $PIECE(STR,";",3)=CODE
                       SET DESC=$PIECE(STR,";",4)
                       SET DONE=1
 +7                IF $PIECE(STR,";",3)="END"
                       SET DESC=$PIECE(STR,";",4)_CODE
                       SET DONE=1
 +8                QUIT 
               End DoDot:1
               if DONE
                   QUIT 
 +9        QUIT DESC
 +10      ; ##### DATA Table for Image Status, View Status,  Annotation Status.
 +11      ;    
 +12      ;    the Code is in Piece 3, and the Description is in Piece 4.
 +13      ;    
 +14      ; Image STATUS field (113) 
 +15      ;    Codes 0..12 are the Status Codes from field 113
 +16      ;    
 +17      ; Viewable Status and Annotation Status are computed values, 
 +18      ;    based on, but not equal to STATUS (113)
 +19      ;    they depend on TIU Business Rules, QI Of image, User Keys.
 +20      ;    The Codes are computed in other functions.  Description of the 
 +21      ;    code is returned from this table.
 +22      ;
 +23      ;    Codes 21..23 are the Viewable Status Codes. (computed value)
 +24      ;    Codes 30..31 are Annotation Status Codes.
DATA      ;
 +1       ;;0;Image is Viewable;
 +2       ;;1;Image is Viewable.;
 +3       ;;2;Image is QA Reviewed.;
 +4       ; 
 +5       ;;10;Image capture is in progress.; (Future for groups.)
 +6       ;;11;Image needs review.;
 +7       ;;12;Image is deleted.;
 +8       ;;13;Image Never Existed.;  << p130 for images that didn't exist.
 +9       ;     
 +10      ;;21;Image has Questionable Integrity issues.;
 +11      ;;22;User cannot 'View' the associated TIU note. TIU Business Rule.;
 +12      ;;23;Image is not Viewable by Radiology Rule.; (future) 
 +13      ;     
 +14      ;;30;User cannot 'Make Addendum' to the associated TIU Note. TIU Business Rule.;
 +15      ;;31;Image is Rescinded.;
 +16      ;;END;User cannot Annotate the Image. Unknown CODE: ;
 +17       QUIT