HMPWB2 ;;ASMR/PJH/JD - WRITEBACK DEMOGRAPHICS/VITALS TO VISTA;Nov 13, 2015@16:42:53
 ;;2.0;ENTERPRISE HEALTH MANAGEMENT PLATFORM;**2**;Dec 11, 2014;Build 28
 ;Per VA Directive 6402, this routine should not be modified.
 ;
 Q
 ;
DEMOG(RSLT,IEN,DFN,DATA) ;File demographic data - WIP not part of PSI 6
 ;
 ;Output
 ; RSLT = JSON format string for demographics
 ;Input
 ; IEN  = record to be updated (not used)
 ; DFN  = patient IEN
 ; DATA("data") - input format - string
 ;   Piece 1: DFN
 ;   Piece 2: Home Phone Number - ^DD(2,.131 - ^DPT(DFN,.13) piece 1
 ;   Piece 3: Cell Phone Number - ^DD(2,.134 - ^DPT(DFN,.13) piece 4
 ;   Piece 4: Work Phone Number - ^DD(2,.132 - ^DPT(DFN,.13) piece 2
 ;   Piece 5: Emergency Phone Number - ^DD(2,.339 - ^DPT(DFN,.33) piece 9
 ;   Piece 6: NOK Phone Number - ^DD(2,.219 - ^DPT(DFN,.21) piece 9
 ;
 ; If a piece contains -1, it means DELETE it
 ; If a piece is null, it means LEAVE it ALONE
 ; If a piece is not -1 and not null, it means UPDATE it
 ;
 ;Update Demographics Logic
 ;-------------------------
 ;Quit if DFN is not defined - IEN is not used
 S RSLT="ERROR" Q:'$G(DFN)
 ;Assume input is presented as a string with DFN in piece1
 N HMP,HMPI,INVAR,OUTVAR
 S INVAR=$G(DATA("data")) Q:$P(INVAR,U)'=DFN
 ;Call existing HMPPTDEM api
 D FILE^HMPPTDEM(.OUTVAR,INVAR) I $G(OUTVAR(0))'=1 M RSLT=OUTVAR Q
 ;Build JSON in ^TMP("HMP",$J) from VistA - expects DFN to be defined
 S HMP=$NA(^TMP("HMP",$J)),HMPI=0 K @HMP
 D DPT1^HMPDJ00
 ;Call $$EXTRACT to pass JSON back as RSLT string
 S RSLT=$$EXTRACT(HMP)
 ;Clear work files
 K @HMP
 Q
 ;
VMADD(RSLT,IEN,DFN,DATA) ;Add vital measurement
 ;
 ;
 ; Note: Original GMVDCSAV routine supports the following IAs:
 ; #3996 - GMV ADD VM RPC called at EN1  (private)
 ;
 ;Output
 ; RSLT = JSON format string for vital added
 ;Input
 ; IEN  = record to be updated (not used)
 ; DFN  = patient IEN (not used, DFN is sent in $P(DATA,"^",2)
 ; DATA - input format - string
 ;   piece1 = date/time in FileMan internal format
 ;   piece2 = patient number from FILE 2 (i.e., DFN)
 ;   piece3 = vital type, a semi-colon, the reading, a semi-colon, and
 ;            oxygen flow rate and percentage values [optional] (e.g.,
 ;            21;99;1 l/min 90%)
 ;   piece4 = hospital location (FILE 44) pointer value
 ;   piece5 = FILE 200 user number (i.e., DUZ), an asterisk, and the 
 ;            qualifier (File 120.52) internal entry numbers separated by
 ;            colons (e.g., 547*50:65)
 ; Example:
 ;            "3051011.1635^134^1;120/80;^67^87*2:38:50:75"
 ;
 ;Add Vital Logic
 ;---------------
 N HMPDFN,VMADD,HMPVEIE
 S VMADD=1,HMPVEIE=0
 ;Quit if DFN is null or IEN is not zero
 S RSLT="ERROR" Q:$G(IEN)'=0
 ;Assume input is presented as a string with DFN in second piece
 N HMP,HMPI,INVAR,OUTVAR,GMRIEN,SEQ,TOT,HMPODT,HMP
 S HMPODT=$P(DATA,"^",1),HMPODT=17000000+$P(HMPODT,".",1)_$P(HMPODT,".",2)
 S HMP=$NA(^TMP("HMP",$J)),HMPI=0 K @HMP
 S INVAR=$G(DATA) I $P(INVAR,U,2)'?1N.N S RSLT="DFN NOT PRESENT IN DATA STRING" Q
 S HMPDFN=$P(INVAR,U,2)
 ;Add vital record to file #120.5
 D EN1^GMVDCSAV(.OUTVAR,INVAR) I $G(OUTVAR(0))="ERROR" S RSLT="ERROR ADDING VITAL TO FILE 120.5" Q
 ;Check if added record exist in VistA
 S GMRIEN=$$FINDVM(HMPDFN) I 'GMRIEN S RSLT="RECORD DOES NOT EXIST IN VISTA" Q
 ;Determine total count and sequence
 S TOT=$$COUNTVM(HMPDFN,GMRIEN,.SEQ)
 ;Build JSON in ^TMP("HMP",$J) from VistA data in file #120.5
 D GMV1(HMPDFN,GMRIEN,SEQ,TOT)
 ;Call $$EXTRACT to pass JSON back as RSLT string
 K RSLT
 S RSLT=$$EXTRACT(HMP)
 M ^TMP("HMPVIT",$J)=RSLT
 K RSLT
 S RSLT=$NA(^TMP("HMPVIT",$J))
 ;Clear work files
 K @HMP
 Q
 ;
VMERR(RSLT,DATA) ;Mark vital measurement entered in error
 ;
 ; Note: Original GMRVUTL1 routine supports the following IAs:
 ; #4414 - GMV MARK ERROR RPC is called at ERROR (private)
 ;
 ;DE2983 - Since DFN is not relevant as an input parameter, we removed it from the DATA string.
 ;Once we know the vital IEN, DFN will also be known.  JD - 11/9/15.
 ;
 ;Output
 ; RSLT = JSON format string for vital added
 ;Input
 ; DATA - Input format - string 
 ;    piece1 = FILE #120.5 IEN
 ;    piece2 
 ;      1 = INCORRECT DATE/TIME
 ;      2 = INCORRECT READING
 ;      3 = INCORRECT PATIENT
 ;      4 = INVALID RECORD
 ; Example:
 ;            "24048^3^2"
 ;
 ;Mark in error logic
 ;-------------------
 ;Assume input is presented as a string with 120.5 ien in piece 1
 N HMP,HMPI,HMPSTOP,HMPVEIE,INVAR,OUTVAR,GMRIEN,GMRRSN,VMERR
 S VMERR=1
 S HMPSTOP=0,HMPVEIE=1
 S HMP=$NA(^TMP("HMP",$J)),HMPI=0 K @HMP
 D CHECK
 S INVAR=$G(DATA)
 I $D(RSLT(1)) D
 . S ^TMP("HMP",$J,1,1)=RSLT(1)
 . S RSLT=$NA(^TMP("HMP",$J))
 . K RSLT(1)
 I HMPSTOP Q
 ;Call existing GMVUTL1 api - seq logic
 D ERROR^GMVUTL1(.OUTVAR,INVAR) I OUTVAR'="OK" D 
 . D MSG^HMPTOOLS("Invalid input parameters",2,INVAR)
 . S HMPSTOP=1
 . S ^TMP("HMP",$J,1,1)=RSLT(1)
 . S RSLT=$NA(^TMP("HMP",$J))
 . K RSLT(1)
 . Q
 I HMPSTOP Q
 ;Determine total count and sequence
 S TOT=$$COUNTVM(DFN,GMRIEN,.SEQ)
 ;Build JSON in ^TMP("HMP",$J) from VistA data in file #120.5
 D GMV1(DFN,GMRIEN,.SEQ,TOT)
 ;Call $$EXTRACT to pass JSON back as RSLT string
 K RSLT
 S RSLT=$$EXTRACT(HMP)
 M ^TMP("HMPVIT",$J)=RSLT
 K RSLT
 S RSLT=$NA(^TMP("HMPVIT",$J))
 ;Clear work files
 K @HMP
 Q
 ;
GMV1(DFN,ID,SEQ,TOT) ; -- vital/measurement ^UTILITY($J,"GMRVD",HMPIDT,HMPTYP,ID)
 ; Note: this code is a copy of GMV1^HMPDJ02 with addition of header 
 N HMPY,X0,TYPE,LOC,FAC,X,Y,MRES,MUNT,HIGH,LOW,I,ARRAY,VIT,FILTER,HMPE,HMPFCNT,HMPUID,J,STMPTM
 S FILTER("id")=ID
 S FILTER("noHead")=1
 S FILTER("domain")="vital"
 S FILTER("patientId")=DFN
 D GET^HMPDJ(.RSLT,.FILTER)
 K ^TMP("VITAL",$J)
 M ^TMP("VITAL",$J)=@RSLT
 S RSLT=$NA(^TMP("VITAL",$J))
 ;I $G(HMPERR)'="" S RSLT=HMPERR Q
 S HMPFCNT=0
 S HMPUID=$$SETUID^HMPUTILS("vital",DFN,GMRIEN)
 S HMPE=^TMP("VITAL",$J,1,1)
 S STMPTM=$TR($P($P(HMPE,"lastUpdateTime",2),","),""":")
 D ADHOC^HMPUTIL2("vital",HMPFCNT,DFN,HMPUID,STMPTM)
 K RSLT
 M RSLT=^TMP("HMPF",$J)
 Q
 ;
FINDVM(DFN) ;Get most recent vital measurement for this patient
 Q $O(^GMR(120.5,"C",DFN,"A"),-1)
 ;
COUNTVM(DFN,IEN,SEQ) ;Count vitals for this patient
 N SUB,TOT S SUB=0,SEQ=0,TOT=0
 F  S SUB=$O(^GMR(120.5,"C",DFN,SUB)) Q:'SUB  D
 .;Ignore entered in error for ADDs
 .I $G(VMADD)=1 Q:$P($G(^GMR(120.5,SUB,2)),U)=1
 .;Save SEQ if this is the entry
 .S TOT=TOT+1 S:SUB=IEN SEQ=TOT
 ;Return count of vitals for this patient (plus sequence in SEQ)
 Q TOT
 ;
CHECK ;Check for valid parameters
 S DATA=$G(DATA)
 S GMRIEN=$P(DATA,U)
 I GMRIEN'=+GMRIEN D MSG^HMPTOOLS("Vital identifier is invalid/null: "_GMRIEN) S HMPSTOP=1 Q
 ;Removed DFN from the input parameter DATA but for integrity purposes (and not to modify
 ;too much code), we need to keep the number of pieces in DATA the same.
 S DATA=GMRIEN_U_$P($G(^GMR(120.5,GMRIEN,0)),U,2)_U_$P(DATA,U,2,999)
 S DFN=$P(DATA,U,2),GMRRSN=$P(DATA,U,3)
 I '$D(^GMR(120.5,GMRIEN)) D MSG^HMPTOOLS("The vital identifier "_GMRIEN_" does not exist.") S HMPSTOP=1 Q
 I $G(GMRRSN)<1!($G(GMRRSN)>4) D MSG^HMPTOOLS("The Entered in Error reason must be 1, 2, 3 or 4: "_GMRRSN) S HMPSTOP=1 Q
 I $D(^GMR(120.5,GMRIEN,2))>0 D MSG^HMPTOOLS("Vital already entered in error: "_GMRIEN) S HMPSTOP=1 Q
 Q
 N HMPSTOP,HMPFND,L,PCE
 S RSLT="",X=0,HMPSTOP=0,HMPFND=0
 S (I,J)=0
 F  S I=$O(^TMP("HMPF",$J,I)) Q:I=""!(HMPSTOP)  D
 . F  S J=$O(^TMP("HMPF",$J,I,J)) Q:J=""  D
 .. I $G(^TMP("HMPF",$J,I,J))["syncStatus" D
 ... Q:$P(^TMP("HMPF",$J,I,J),":",1)["domainTotals"
 ... S RSLT(X)=RSLT(X)_$P(^TMP("HMPF",$J,I,J),",",1)
 ... S HMPSTOP=1
 ... Q
 .. Q:$G(^TMP("HMPF",$J,I,J))=""
 .. Q:$P(^TMP("HMPF",$J,I,J),",",1)'["vital"
 .. Q:$P(^TMP("HMPF",$J,I,J),",",4)'["localId"
 .. Q:$P(^TMP("HMPF",$J,I,J),":",1)["domainTotals"
 .. S X=X+1
 .. S RSLT(X)=$G(^TMP("HMPF",$J,I,J))
 .. F  S J=$O(^TMP("HMPF",$J,I,J)) Q:J=""  D
 ... Q:$P(^TMP("HMPF",$J,I,J),":",1)["domainTotals"
 ... S X=X+1
 ... S RSLT(X)=$G(^TMP("HMPF",$J,I,J))
 ... S HMPFND=1
 ... Q
 .. S I=$O(^TMP("HMPF",$J,I))
 .. Q
 . Q
 Q RSLT
 
--- Routine Detail   --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HHMPWB2   8269     printed  Sep 23, 2025@19:30:46                                                                                                                                                                                                      Page 2
HMPWB2    ;;ASMR/PJH/JD - WRITEBACK DEMOGRAPHICS/VITALS TO VISTA;Nov 13, 2015@16:42:53
 +1       ;;2.0;ENTERPRISE HEALTH MANAGEMENT PLATFORM;**2**;Dec 11, 2014;Build 28
 +2       ;Per VA Directive 6402, this routine should not be modified.
 +3       ;
 +4        QUIT 
 +5       ;
DEMOG(RSLT,IEN,DFN,DATA) ;File demographic data - WIP not part of PSI 6
 +1       ;
 +2       ;Output
 +3       ; RSLT = JSON format string for demographics
 +4       ;Input
 +5       ; IEN  = record to be updated (not used)
 +6       ; DFN  = patient IEN
 +7       ; DATA("data") - input format - string
 +8       ;   Piece 1: DFN
 +9       ;   Piece 2: Home Phone Number - ^DD(2,.131 - ^DPT(DFN,.13) piece 1
 +10      ;   Piece 3: Cell Phone Number - ^DD(2,.134 - ^DPT(DFN,.13) piece 4
 +11      ;   Piece 4: Work Phone Number - ^DD(2,.132 - ^DPT(DFN,.13) piece 2
 +12      ;   Piece 5: Emergency Phone Number - ^DD(2,.339 - ^DPT(DFN,.33) piece 9
 +13      ;   Piece 6: NOK Phone Number - ^DD(2,.219 - ^DPT(DFN,.21) piece 9
 +14      ;
 +15      ; If a piece contains -1, it means DELETE it
 +16      ; If a piece is null, it means LEAVE it ALONE
 +17      ; If a piece is not -1 and not null, it means UPDATE it
 +18      ;
 +19      ;Update Demographics Logic
 +20      ;-------------------------
 +21      ;Quit if DFN is not defined - IEN is not used
 +22       SET RSLT="ERROR"
           if '$GET(DFN)
               QUIT 
 +23      ;Assume input is presented as a string with DFN in piece1
 +24       NEW HMP,HMPI,INVAR,OUTVAR
 +25       SET INVAR=$GET(DATA("data"))
           if $PIECE(INVAR,U)'=DFN
               QUIT 
 +26      ;Call existing HMPPTDEM api
 +27       DO FILE^HMPPTDEM(.OUTVAR,INVAR)
           IF $GET(OUTVAR(0))'=1
               MERGE RSLT=OUTVAR
               QUIT 
 +28      ;Build JSON in ^TMP("HMP",$J) from VistA - expects DFN to be defined
 +29       SET HMP=$NAME(^TMP("HMP",$JOB))
           SET HMPI=0
           KILL @HMP
 +30       DO DPT1^HMPDJ00
 +31      ;Call $$EXTRACT to pass JSON back as RSLT string
 +32       SET RSLT=$$EXTRACT(HMP)
 +33      ;Clear work files
 +34       KILL @HMP
 +35       QUIT 
 +36      ;
VMADD(RSLT,IEN,DFN,DATA) ;Add vital measurement
 +1       ;
 +2       ;
 +3       ; Note: Original GMVDCSAV routine supports the following IAs:
 +4       ; #3996 - GMV ADD VM RPC called at EN1  (private)
 +5       ;
 +6       ;Output
 +7       ; RSLT = JSON format string for vital added
 +8       ;Input
 +9       ; IEN  = record to be updated (not used)
 +10      ; DFN  = patient IEN (not used, DFN is sent in $P(DATA,"^",2)
 +11      ; DATA - input format - string
 +12      ;   piece1 = date/time in FileMan internal format
 +13      ;   piece2 = patient number from FILE 2 (i.e., DFN)
 +14      ;   piece3 = vital type, a semi-colon, the reading, a semi-colon, and
 +15      ;            oxygen flow rate and percentage values [optional] (e.g.,
 +16      ;            21;99;1 l/min 90%)
 +17      ;   piece4 = hospital location (FILE 44) pointer value
 +18      ;   piece5 = FILE 200 user number (i.e., DUZ), an asterisk, and the 
 +19      ;            qualifier (File 120.52) internal entry numbers separated by
 +20      ;            colons (e.g., 547*50:65)
 +21      ; Example:
 +22      ;            "3051011.1635^134^1;120/80;^67^87*2:38:50:75"
 +23      ;
 +24      ;Add Vital Logic
 +25      ;---------------
 +26       NEW HMPDFN,VMADD,HMPVEIE
 +27       SET VMADD=1
           SET HMPVEIE=0
 +28      ;Quit if DFN is null or IEN is not zero
 +29       SET RSLT="ERROR"
           if $GET(IEN)'=0
               QUIT 
 +30      ;Assume input is presented as a string with DFN in second piece
 +31       NEW HMP,HMPI,INVAR,OUTVAR,GMRIEN,SEQ,TOT,HMPODT,HMP
 +32       SET HMPODT=$PIECE(DATA,"^",1)
           SET HMPODT=17000000+$PIECE(HMPODT,".",1)_$PIECE(HMPODT,".",2)
 +33       SET HMP=$NAME(^TMP("HMP",$JOB))
           SET HMPI=0
           KILL @HMP
 +34       SET INVAR=$GET(DATA)
           IF $PIECE(INVAR,U,2)'?1N.N
               SET RSLT="DFN NOT PRESENT IN DATA STRING"
               QUIT 
 +35       SET HMPDFN=$PIECE(INVAR,U,2)
 +36      ;Add vital record to file #120.5
 +37       DO EN1^GMVDCSAV(.OUTVAR,INVAR)
           IF $GET(OUTVAR(0))="ERROR"
               SET RSLT="ERROR ADDING VITAL TO FILE 120.5"
               QUIT 
 +38      ;Check if added record exist in VistA
 +39       SET GMRIEN=$$FINDVM(HMPDFN)
           IF 'GMRIEN
               SET RSLT="RECORD DOES NOT EXIST IN VISTA"
               QUIT 
 +40      ;Determine total count and sequence
 +41       SET TOT=$$COUNTVM(HMPDFN,GMRIEN,.SEQ)
 +42      ;Build JSON in ^TMP("HMP",$J) from VistA data in file #120.5
 +43       DO GMV1(HMPDFN,GMRIEN,SEQ,TOT)
 +44      ;Call $$EXTRACT to pass JSON back as RSLT string
 +45       KILL RSLT
 +46       SET RSLT=$$EXTRACT(HMP)
 +47       MERGE ^TMP("HMPVIT",$JOB)=RSLT
 +48       KILL RSLT
 +49       SET RSLT=$NAME(^TMP("HMPVIT",$JOB))
 +50      ;Clear work files
 +51       KILL @HMP
 +52       QUIT 
 +53      ;
VMERR(RSLT,DATA) ;Mark vital measurement entered in error
 +1       ;
 +2       ; Note: Original GMRVUTL1 routine supports the following IAs:
 +3       ; #4414 - GMV MARK ERROR RPC is called at ERROR (private)
 +4       ;
 +5       ;DE2983 - Since DFN is not relevant as an input parameter, we removed it from the DATA string.
 +6       ;Once we know the vital IEN, DFN will also be known.  JD - 11/9/15.
 +7       ;
 +8       ;Output
 +9       ; RSLT = JSON format string for vital added
 +10      ;Input
 +11      ; DATA - Input format - string 
 +12      ;    piece1 = FILE #120.5 IEN
 +13      ;    piece2 
 +14      ;      1 = INCORRECT DATE/TIME
 +15      ;      2 = INCORRECT READING
 +16      ;      3 = INCORRECT PATIENT
 +17      ;      4 = INVALID RECORD
 +18      ; Example:
 +19      ;            "24048^3^2"
 +20      ;
 +21      ;Mark in error logic
 +22      ;-------------------
 +23      ;Assume input is presented as a string with 120.5 ien in piece 1
 +24       NEW HMP,HMPI,HMPSTOP,HMPVEIE,INVAR,OUTVAR,GMRIEN,GMRRSN,VMERR
 +25       SET VMERR=1
 +26       SET HMPSTOP=0
           SET HMPVEIE=1
 +27       SET HMP=$NAME(^TMP("HMP",$JOB))
           SET HMPI=0
           KILL @HMP
 +28       DO CHECK
 +29       SET INVAR=$GET(DATA)
 +30       IF $DATA(RSLT(1))
               Begin DoDot:1
 +31               SET ^TMP("HMP",$JOB,1,1)=RSLT(1)
 +32               SET RSLT=$NAME(^TMP("HMP",$JOB))
 +33               KILL RSLT(1)
               End DoDot:1
 +34       IF HMPSTOP
               QUIT 
 +35      ;Call existing GMVUTL1 api - seq logic
 +36       DO ERROR^GMVUTL1(.OUTVAR,INVAR)
           IF OUTVAR'="OK"
               Begin DoDot:1
 +37               DO MSG^HMPTOOLS("Invalid input parameters",2,INVAR)
 +38               SET HMPSTOP=1
 +39               SET ^TMP("HMP",$JOB,1,1)=RSLT(1)
 +40               SET RSLT=$NAME(^TMP("HMP",$JOB))
 +41               KILL RSLT(1)
 +42               QUIT 
               End DoDot:1
 +43       IF HMPSTOP
               QUIT 
 +44      ;Determine total count and sequence
 +45       SET TOT=$$COUNTVM(DFN,GMRIEN,.SEQ)
 +46      ;Build JSON in ^TMP("HMP",$J) from VistA data in file #120.5
 +47       DO GMV1(DFN,GMRIEN,.SEQ,TOT)
 +48      ;Call $$EXTRACT to pass JSON back as RSLT string
 +49       KILL RSLT
 +50       SET RSLT=$$EXTRACT(HMP)
 +51       MERGE ^TMP("HMPVIT",$JOB)=RSLT
 +52       KILL RSLT
 +53       SET RSLT=$NAME(^TMP("HMPVIT",$JOB))
 +54      ;Clear work files
 +55       KILL @HMP
 +56       QUIT 
 +57      ;
GMV1(DFN,ID,SEQ,TOT) ; -- vital/measurement ^UTILITY($J,"GMRVD",HMPIDT,HMPTYP,ID)
 +1       ; Note: this code is a copy of GMV1^HMPDJ02 with addition of header 
 +2        NEW HMPY,X0,TYPE,LOC,FAC,X,Y,MRES,MUNT,HIGH,LOW,I,ARRAY,VIT,FILTER,HMPE,HMPFCNT,HMPUID,J,STMPTM
 +3        SET FILTER("id")=ID
 +4        SET FILTER("noHead")=1
 +5        SET FILTER("domain")="vital"
 +6        SET FILTER("patientId")=DFN
 +7        DO GET^HMPDJ(.RSLT,.FILTER)
 +8        KILL ^TMP("VITAL",$JOB)
 +9        MERGE ^TMP("VITAL",$JOB)=@RSLT
 +10       SET RSLT=$NAME(^TMP("VITAL",$JOB))
 +11      ;I $G(HMPERR)'="" S RSLT=HMPERR Q
 +12       SET HMPFCNT=0
 +13       SET HMPUID=$$SETUID^HMPUTILS("vital",DFN,GMRIEN)
 +14       SET HMPE=^TMP("VITAL",$JOB,1,1)
 +15       SET STMPTM=$TRANSLATE($PIECE($PIECE(HMPE,"lastUpdateTime",2),","),""":")
 +16       DO ADHOC^HMPUTIL2("vital",HMPFCNT,DFN,HMPUID,STMPTM)
 +17       KILL RSLT
 +18       MERGE RSLT=^TMP("HMPF",$JOB)
 +19       QUIT 
 +20      ;
FINDVM(DFN) ;Get most recent vital measurement for this patient
 +1        QUIT $ORDER(^GMR(120.5,"C",DFN,"A"),-1)
 +2       ;
COUNTVM(DFN,IEN,SEQ) ;Count vitals for this patient
 +1        NEW SUB,TOT
           SET SUB=0
           SET SEQ=0
           SET TOT=0
 +2        FOR 
               SET SUB=$ORDER(^GMR(120.5,"C",DFN,SUB))
               if 'SUB
                   QUIT 
               Begin DoDot:1
 +3       ;Ignore entered in error for ADDs
 +4                IF $GET(VMADD)=1
                       if $PIECE($GET(^GMR(120.5,SUB,2)),U)=1
                           QUIT 
 +5       ;Save SEQ if this is the entry
 +6                SET TOT=TOT+1
                   if SUB=IEN
                       SET SEQ=TOT
               End DoDot:1
 +7       ;Return count of vitals for this patient (plus sequence in SEQ)
 +8        QUIT TOT
 +9       ;
CHECK     ;Check for valid parameters
 +1        SET DATA=$GET(DATA)
 +2        SET GMRIEN=$PIECE(DATA,U)
 +3        IF GMRIEN'=+GMRIEN
               DO MSG^HMPTOOLS("Vital identifier is invalid/null: "_GMRIEN)
               SET HMPSTOP=1
               QUIT 
 +4       ;Removed DFN from the input parameter DATA but for integrity purposes (and not to modify
 +5       ;too much code), we need to keep the number of pieces in DATA the same.
 +6        SET DATA=GMRIEN_U_$PIECE($GET(^GMR(120.5,GMRIEN,0)),U,2)_U_$PIECE(DATA,U,2,999)
 +7        SET DFN=$PIECE(DATA,U,2)
           SET GMRRSN=$PIECE(DATA,U,3)
 +8        IF '$DATA(^GMR(120.5,GMRIEN))
               DO MSG^HMPTOOLS("The vital identifier "_GMRIEN_" does not exist.")
               SET HMPSTOP=1
               QUIT 
 +9        IF $GET(GMRRSN)<1!($GET(GMRRSN)>4)
               DO MSG^HMPTOOLS("The Entered in Error reason must be 1, 2, 3 or 4: "_GMRRSN)
               SET HMPSTOP=1
               QUIT 
 +10       IF $DATA(^GMR(120.5,GMRIEN,2))>0
               DO MSG^HMPTOOLS("Vital already entered in error: "_GMRIEN)
               SET HMPSTOP=1
               QUIT 
 +11       QUIT 
 +1        NEW HMPSTOP,HMPFND,L,PCE
 +2        SET RSLT=""
           SET X=0
           SET HMPSTOP=0
           SET HMPFND=0
 +3        SET (I,J)=0
 +4        FOR 
               SET I=$ORDER(^TMP("HMPF",$JOB,I))
               if I=""!(HMPSTOP)
                   QUIT 
               Begin DoDot:1
 +5                FOR 
                       SET J=$ORDER(^TMP("HMPF",$JOB,I,J))
                       if J=""
                           QUIT 
                       Begin DoDot:2
 +6                        IF $GET(^TMP("HMPF",$JOB,I,J))["syncStatus"
                               Begin DoDot:3
 +7                                if $PIECE(^TMP("HMPF",$JOB,I,J),"
                                       QUIT 
 +8                                SET RSLT(X)=RSLT(X)_$PIECE(^TMP("HMPF",$JOB,I,J),",",1)
 +9                                SET HMPSTOP=1
 +10                               QUIT 
                               End DoDot:3
 +11                       if $GET(^TMP("HMPF",$JOB,I,J))=""
                               QUIT 
 +12                       if $PIECE(^TMP("HMPF",$JOB,I,J),",",1)'["vital"
                               QUIT 
 +13                       if $PIECE(^TMP("HMPF",$JOB,I,J),",",4)'["localId"
                               QUIT 
 +14                       if $PIECE(^TMP("HMPF",$JOB,I,J),"
                               QUIT 
 +15                       SET X=X+1
 +16                       SET RSLT(X)=$GET(^TMP("HMPF",$JOB,I,J))
 +17                       FOR 
                               SET J=$ORDER(^TMP("HMPF",$JOB,I,J))
                               if J=""
                                   QUIT 
                               Begin DoDot:3
 +18                               if $PIECE(^TMP("HMPF",$JOB,I,J),"
                                       QUIT 
 +19                               SET X=X+1
 +20                               SET RSLT(X)=$GET(^TMP("HMPF",$JOB,I,J))
 +21                               SET HMPFND=1
 +22                               QUIT 
                               End DoDot:3
 +23                       SET I=$ORDER(^TMP("HMPF",$JOB,I))
 +24                       QUIT 
                       End DoDot:2
 +25               QUIT 
               End DoDot:1
 +26       QUIT RSLT