RORHDT04 ;HCIOFO/SG - HISTORICAL DATA EXTRACTION PROCESS ;1/22/06 8:18pm
 ;;1.5;CLINICAL CASE REGISTRIES;**10**;Feb 17, 2006;Build 32
 ;
 ; This routine uses the following IAs:
 ;
 ; #2320  CLOSE^ZISH (supported)
 ; #2053  FILE^DIE (supported)
 ; #2055  $$ROOT^DILFD (supported)
 Q
 ;
 ;***** DATA EXTRACTION PROCESS
 ;
 ; .REGLST       Reference to a local array containing registry
 ;               names as subscripts and registry IENs as values
 ;
 ; HDEIEN        Data Extract IEN
 ; TASKIEN       Task IEN
 ;
 ; FAM           File Access Mode
 ;
 ; Return Values:
 ;       <0  Error code
 ;      >=0  Statistics
 ;             ^1: Total number of processed patients
 ;             ^2: Number of patients processed with errors
 ;
 N ROREXT        ; Data extraction descriptor
 N RORHL         ; HL7 variables
 N RORLRC        ; List of codes of Lab results to be extracted
 ;
 N CNT           ; Number of processed registry records
 N ECNT          ; Number of records processed with errors
 N FILE          ; Name of the output file
 N OUTDIR        ; Name of the output directory
 ;
 N BDT,EDT,NEXT,POP,RC,REGIEN,REGNAME,RGIENLST,RRBIEN,RREIEN,STOP,TMP
 K ^TMP("RORHDT",$J,"PR"),^TMP("HLS",$J),^TMP("RORPTF",$J)
 S (CNT,ECNT,STOP)=0,RORHDT("BHS")=1
 ;--- Prepare the list of registry IENs
 S REGNAME="",REGIEN=0
 F  S REGNAME=$O(REGLST(REGNAME))  Q:REGNAME=""  D  Q:REGIEN<0
 . S REGIEN=+REGLST(REGNAME)
 . S:REGIEN'>0 REGIEN=$$REGIEN^RORUTL02(REGNAME)
 . S:REGIEN>0 RGIENLST(REGIEN)=""
 Q:REGIEN<0 REGIEN
 ;
 ;=== Load parameters
 S RC=$$HDEPARM^RORHDT05(HDEIEN,.BDT,.EDT,.OUTDIR)
 Q:RC<0 RC
 S RC=$$TASKPARM^RORHDT05(HDEIEN,TASKIEN,.RRBIEN,.RREIEN,.FILE)
 Q:RC<0 RC
 ;
 ;=== Prepare data extraction rules
 S RC=$$PREPARE^ROREXPR(.REGLST,BDT,EDT)
 Q:RC<0 $$ERROR^RORERR(-22)
 ;--- Load and process historical data extraction parameters
 S RC=$$PREPARE^RORHDT06(HDEIEN)  Q:RC<0 RC
 K ROREXT("MAXHL7SIZE")  ; Do not limit the size
 ;
 ;=== Initialize the HL7 environment
 S RC=$$INIT^RORHL7()  Q:RC<0 RC
 ;
 ;=== Delete the old output host file(s)
 S TMP=$$DELFILES^RORHDT05(OUTDIR,FILE)
 ;
 D
 . N COMMIT,IENS,NODE,NRTC,PTIEN
 . S NRTC=100 ; Number of records to commit
 . ;
 . ;=== Try to re-extract the erroneous records
 . S NODE=$$ROOT^DILFD(799.641,","_(+TASKIEN)_","_(+HDEIEN)_",",1)
 . S NODE=$NA(@NODE@("B"))
 . S PTIEN=0,RC=0
 . F  D  Q:RC!STOP!(PTIEN'>0)
 . . K ^TMP("HLS",$J)
 . . F  S PTIEN=$O(@NODE@(PTIEN))  Q:PTIEN'>0  D  Q:RC!'((CNT-ECNT)#NRTC)
 . . . S RC=$$LOOP^RORTSK01()
 . . . I RC<0  S:RC=-42 STOP=1  Q
 . . . S RC=$$PROCREC(PTIEN,.RGIENLST),CNT=CNT+1
 . . . S ^TMP("RORHDT",$J,"PR",PTIEN)=RC
 . . . I RC'<0  S RC=0  Q
 . . . ;--- Process the error
 . . . S RC=$$ERROR^RORERR(-15,,,PTIEN),ECNT=ECNT+1
 . . . S:$G(RORPARM("DEBUG"))<3 RC=0
 . . I RC<0  Q:'STOP
 . . ;--- Commit the data
 . . S TMP=$$COMMIT^RORHDT05(OUTDIR,FILE)
 . . S:TMP<0 RC=TMP
 . Q:STOP!(RC=-34)
 . ;
 . ;=== Extract the remaining registry data
 . S PTIEN=$S(RRBIEN>0:+$O(^RORDATA(798,"KEY",RRBIEN),-1),1:0)
 . S RC=0
 . F  D  Q:RC!STOP!(PTIEN'>0)
 . . K ^TMP("HLS",$J)  S COMMIT=0
 . . F  S PTIEN=$$NEXTPAT(PTIEN,.RGIENLST)  Q:PTIEN'>0  D  Q:RC!COMMIT
 . . . S RC=$$LOOP^RORTSK01()
 . . . I RC<0  S:RC=-42 STOP=1  Q
 . . . I RREIEN>0,PTIEN'<RREIEN  S PTIEN="",RC=1  Q
 . . . Q:$D(^TMP("RORHDT",$J,"PR",PTIEN))
 . . . S RC=$$PROCREC(PTIEN,.RGIENLST),CNT=CNT+1
 . . . I RC'<0  S COMMIT='((CNT-ECNT)#NRTC),RC=0  Q
 . . . ;--- Process the error
 . . . S RC=$$ERROR^RORERR(-15,,,PTIEN),ECNT=ECNT+1
 . . . S:$G(RORPARM("DEBUG"))<3 RC=0
 . . . S TMP=$$ADDERR^RORHDT05(HDEIEN,TASKIEN,PTIEN)
 . . . S:TMP<0 RC=TMP
 . . I RC<0  Q:'STOP
 . . ;--- Commit the data
 . . S NEXT=$S(COMMIT:$$NEXTPAT(PTIEN,.RGIENLST),1:PTIEN)
 . . S TMP=$$COMMIT^RORHDT05(OUTDIR,FILE)
 . . S:TMP<0 RC=TMP
 ;
 ;--- The $$COMMIT^RORHDT05 returns -34 if it was not able to create
 ;--- the output file (wrong directory name, protection error, etc.).
 D:RC'=-34
 . N NODE,RORFDA,RORMSG
 . ;
 . ;=== Write the batch trailer segment and close the file if
 . ;=== the batch is not empty. Otherwise, record a warning.
 . I '$G(RORHDT("BHS"))  D
 . . S TMP=$S(ECNT!(RC<0):"Completed with errors",STOP:"Stopped",1:"")
 . . U IO  W $$BTS^RORHL7A($$MSGCNT^RORHL7,TMP),$C(13)
 . . D CLOSE^%ZISH("HL7FILE")
 . E  D ERROR^RORERR(-89)
 . ;
 . ;=== Update the NEXT RECORD IEN field in the task record
 . I $D(NEXT)  D:NEXT'>0
 . . ;--- If the task completed successfully, the NEXT RECORD IEN
 . . ;    field is set to an empty string. If the task is restarted
 . . ;--- afterwards, it will re-extract all data again.
 . . I 'ECNT  S NEXT=""  Q
 . . ;--- If completed with errors, use IEN of the last record
 . . ;--- processed by the task incremented by 1.
 . . I RREIEN>0  S NEXT=RREIEN+1  Q
 . . ;--- Or the IEN of the last patient record incremented by 1
 . . ;--- (in case of the last/single task).
 . . S NEXT=$O(^RORDATA(798,"KEY",""),-1)+1
 . . ;--- When the task is restarted, it will try to re-extract only
 . . ;    erroneous records and will not process already extracted
 . . ;    data (the PTIEN will not be less than the RREIEN or the
 . . ;--- $ORDER function will not return a value greater than zero).
 . E  Q:(RC<0)!ECNT!STOP  S NEXT=""
 . ;
 . ;=== Update the task record
 . S IENS=(+TASKIEN)_","_(+HDEIEN)_","
 . S RORFDA(799.64,IENS,.04)=NEXT
 . D FILE^DIE("K","RORFDA","RORMSG")
 . S TMP=$$DBS^RORERR("RORMSG",-9,,,799.64,IENS)
 ;
 ;=== Cleanup
 K ^TMP("RORPTF",$J)
 S:RC'<0 RC=$$CLRERRS^RORHDT05(HDEIEN,TASKIEN)
 Q $S(RC<0:RC,1:CNT_U_ECNT)
 ;
 ;***** RETURNS THE NEXT PATIENT FOR DATA EXTRACTION
 ;
 ; PTIEN         Patient IEN (DFN)
 ;
 ; .RGIENLST     Reference to a local array containing registry
 ;               IENs as subscripts. The IENs of the corresponding
 ;               patient's registry records are returned as values.
 ;
 ; Return Values:
 ;        0  No more patients
 ;       >0  IEN (DFN) of the next patient who belongs to at least
 ;           one of the registries defined by the RGIENLST parameter.
 ;
NEXTPAT(PTIEN,RGIENLST) ;
 N CNT,IEN,REGIEN
 S CNT=0
 F  S PTIEN=$O(^RORDATA(798,"KEY",PTIEN))  Q:PTIEN'>0  D  Q:CNT
 . S REGIEN=0
 . F  S REGIEN=$O(RGIENLST(REGIEN))  Q:REGIEN'>0  D
 . . S RGIENLST(REGIEN)=0
 . . S IEN=+$O(^RORDATA(798,"KEY",PTIEN,REGIEN,""))
 . . Q:IEN'>0
 . . ;************************************************************
 . . ; Patch 10: also include pending patients
 . . ;--- Skip inactive records (pending and marked for deletion)
 . . ;Q:'$$ACTIVE^RORDD(IEN)
 . . ;************************************************************
 . . ;--- skip patients marked for deletion
 . . I $$DEL^RORDD(IEN) Q
 . . ;--- Skip records tagged as "DON'T SEND" and skip test patients
 . . I (($P($G(^RORDATA(798,IEN,2)),U,4))!($$TESTPAT^RORUTL01(PTIEN))) Q
 . . ;--- Consider the record
 . . S RGIENLST(REGIEN)=IEN,CNT=CNT+1
 Q $S(PTIEN>0:PTIEN,1:0)
 ;
 ;***** PROCESSES A RECORD IN THE REGISTRY
 ;
 ; PTIEN         Patient IEN (DFN)
 ;
 ; .RGIENLST     Reference to a local array containing registry
 ;               IENs as subscripts and IENs of the corresponding
 ;               patient's registry records as values.
 ;
 ; Return Values:
 ;       <0  Error code
 ;        0  Ok
 ;        1  Nothing has been extracted
 ;
PROCREC(PTIEN,RGIENLST) ;
 N RORERRDL      ; Default error location
 ;
 N BATCHID,CNT,DXDTS,IEN,MSHPTR,RC,REGIEN,RORMSH,TMP
 D CLEAR^RORERR("PROCREC^RORHDT04")
 ;
 ;--- Compile the data extraction time frames
 S (CNT,RC,REGIEN)=0
 F  S REGIEN=$O(RGIENLST(REGIEN))  Q:REGIEN'>0  D  Q:RC<0
 . S IEN=+RGIENLST(REGIEN)  Q:IEN'>0
 . S RC=$$DXPERIOD^ROREXTUT(.DXDTS,IEN,PTIEN)
 . S:'RC CNT=CNT+1
 . S:RC>0 RGIENLST(REGIEN)=0
 Q:RC<0 RC
 ;--- If the patient should be skipped in all registries
 ;    that are being processed, then do not perform the data
 ;--- extraction for this patient at all.
 I 'CNT  D:$G(RORPARM("DEBUG"))  Q 0
 . D LOG^RORLOG(4,"There is no data to extract.",PTIEN)
 ;
 ;--- Create an HL7 message for the patient
 S MSHPTR=$$CREATE^RORHL7(.RORMSH)  Q:MSHPTR<0 MSHPTR
 S RC=$$MESSAGE^ROREXT02(PTIEN,.RGIENLST,.DXDTS,$G(ROREXT("HDTIEN")))
 ;
 ;--- Delete the unfinished message from the ^TMP("HLS",$J)
 ;    if there is no data to send (RC>0) or there was an error
 ;    during the data extraction (RC<0). Return the error code
 ;--- in the latter case.
 I RC!($O(^TMP("HLS",$J,""),-1)=MSHPTR)  D  Q:RC<0 RC
 . D ROLLBACK^RORHL7(MSHPTR)  S:'RC RC=1
 ;---
 Q RC
 
--- Routine Detail   --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HRORHDT04   8681     printed  Sep 23, 2025@19:17:36                                                                                                                                                                                                    Page 2
RORHDT04  ;HCIOFO/SG - HISTORICAL DATA EXTRACTION PROCESS ;1/22/06 8:18pm
 +1       ;;1.5;CLINICAL CASE REGISTRIES;**10**;Feb 17, 2006;Build 32
 +2       ;
 +3       ; This routine uses the following IAs:
 +4       ;
 +5       ; #2320  CLOSE^ZISH (supported)
 +6       ; #2053  FILE^DIE (supported)
 +7       ; #2055  $$ROOT^DILFD (supported)
 +8        QUIT 
 +9       ;
 +10      ;***** DATA EXTRACTION PROCESS
 +11      ;
 +12      ; .REGLST       Reference to a local array containing registry
 +13      ;               names as subscripts and registry IENs as values
 +14      ;
 +15      ; HDEIEN        Data Extract IEN
 +16      ; TASKIEN       Task IEN
 +17      ;
 +18      ; FAM           File Access Mode
 +19      ;
 +20      ; Return Values:
 +21      ;       <0  Error code
 +22      ;      >=0  Statistics
 +23      ;             ^1: Total number of processed patients
 +24      ;             ^2: Number of patients processed with errors
 +25      ;
 +1       ; Data extraction descriptor
           NEW ROREXT
 +2       ; HL7 variables
           NEW RORHL
 +3       ; List of codes of Lab results to be extracted
           NEW RORLRC
 +4       ;
 +5       ; Number of processed registry records
           NEW CNT
 +6       ; Number of records processed with errors
           NEW ECNT
 +7       ; Name of the output file
           NEW FILE
 +8       ; Name of the output directory
           NEW OUTDIR
 +9       ;
 +10       NEW BDT,EDT,NEXT,POP,RC,REGIEN,REGNAME,RGIENLST,RRBIEN,RREIEN,STOP,TMP
 +11       KILL ^TMP("RORHDT",$JOB,"PR"),^TMP("HLS",$JOB),^TMP("RORPTF",$JOB)
 +12       SET (CNT,ECNT,STOP)=0
           SET RORHDT("BHS")=1
 +13      ;--- Prepare the list of registry IENs
 +14       SET REGNAME=""
           SET REGIEN=0
 +15       FOR 
               SET REGNAME=$ORDER(REGLST(REGNAME))
               if REGNAME=""
                   QUIT 
               Begin DoDot:1
 +16               SET REGIEN=+REGLST(REGNAME)
 +17               if REGIEN'>0
                       SET REGIEN=$$REGIEN^RORUTL02(REGNAME)
 +18               if REGIEN>0
                       SET RGIENLST(REGIEN)=""
               End DoDot:1
               if REGIEN<0
                   QUIT 
 +19       if REGIEN<0
               QUIT REGIEN
 +20      ;
 +21      ;=== Load parameters
 +22       SET RC=$$HDEPARM^RORHDT05(HDEIEN,.BDT,.EDT,.OUTDIR)
 +23       if RC<0
               QUIT RC
 +24       SET RC=$$TASKPARM^RORHDT05(HDEIEN,TASKIEN,.RRBIEN,.RREIEN,.FILE)
 +25       if RC<0
               QUIT RC
 +26      ;
 +27      ;=== Prepare data extraction rules
 +28       SET RC=$$PREPARE^ROREXPR(.REGLST,BDT,EDT)
 +29       if RC<0
               QUIT $$ERROR^RORERR(-22)
 +30      ;--- Load and process historical data extraction parameters
 +31       SET RC=$$PREPARE^RORHDT06(HDEIEN)
           if RC<0
               QUIT RC
 +32      ; Do not limit the size
           KILL ROREXT("MAXHL7SIZE")
 +33      ;
 +34      ;=== Initialize the HL7 environment
 +35       SET RC=$$INIT^RORHL7()
           if RC<0
               QUIT RC
 +36      ;
 +37      ;=== Delete the old output host file(s)
 +38       SET TMP=$$DELFILES^RORHDT05(OUTDIR,FILE)
 +39      ;
 +40       Begin DoDot:1
 +41           NEW COMMIT,IENS,NODE,NRTC,PTIEN
 +42      ; Number of records to commit
               SET NRTC=100
 +43      ;
 +44      ;=== Try to re-extract the erroneous records
 +45           SET NODE=$$ROOT^DILFD(799.641,","_(+TASKIEN)_","_(+HDEIEN)_",",1)
 +46           SET NODE=$NAME(@NODE@("B"))
 +47           SET PTIEN=0
               SET RC=0
 +48           FOR 
                   Begin DoDot:2
 +49                   KILL ^TMP("HLS",$JOB)
 +50                   FOR 
                           SET PTIEN=$ORDER(@NODE@(PTIEN))
                           if PTIEN'>0
                               QUIT 
                           Begin DoDot:3
 +51                           SET RC=$$LOOP^RORTSK01()
 +52                           IF RC<0
                                   if RC=-42
                                       SET STOP=1
                                   QUIT 
 +53                           SET RC=$$PROCREC(PTIEN,.RGIENLST)
                               SET CNT=CNT+1
 +54                           SET ^TMP("RORHDT",$JOB,"PR",PTIEN)=RC
 +55                           IF RC'<0
                                   SET RC=0
                                   QUIT 
 +56      ;--- Process the error
 +57                           SET RC=$$ERROR^RORERR(-15,,,PTIEN)
                               SET ECNT=ECNT+1
 +58                           if $GET(RORPARM("DEBUG"))<3
                                   SET RC=0
                           End DoDot:3
                           if RC!'((CNT-ECNT)#NRTC)
                               QUIT 
 +59                   IF RC<0
                           if 'STOP
                               QUIT 
 +60      ;--- Commit the data
 +61                   SET TMP=$$COMMIT^RORHDT05(OUTDIR,FILE)
 +62                   if TMP<0
                           SET RC=TMP
                   End DoDot:2
                   if RC!STOP!(PTIEN'>0)
                       QUIT 
 +63           if STOP!(RC=-34)
                   QUIT 
 +64      ;
 +65      ;=== Extract the remaining registry data
 +66           SET PTIEN=$SELECT(RRBIEN>0:+$ORDER(^RORDATA(798,"KEY",RRBIEN),-1),1:0)
 +67           SET RC=0
 +68           FOR 
                   Begin DoDot:2
 +69                   KILL ^TMP("HLS",$JOB)
                       SET COMMIT=0
 +70                   FOR 
                           SET PTIEN=$$NEXTPAT(PTIEN,.RGIENLST)
                           if PTIEN'>0
                               QUIT 
                           Begin DoDot:3
 +71                           SET RC=$$LOOP^RORTSK01()
 +72                           IF RC<0
                                   if RC=-42
                                       SET STOP=1
                                   QUIT 
 +73                           IF RREIEN>0
                                   IF PTIEN'<RREIEN
                                       SET PTIEN=""
                                       SET RC=1
                                       QUIT 
 +74                           if $DATA(^TMP("RORHDT",$JOB,"PR",PTIEN))
                                   QUIT 
 +75                           SET RC=$$PROCREC(PTIEN,.RGIENLST)
                               SET CNT=CNT+1
 +76                           IF RC'<0
                                   SET COMMIT='((CNT-ECNT)#NRTC)
                                   SET RC=0
                                   QUIT 
 +77      ;--- Process the error
 +78                           SET RC=$$ERROR^RORERR(-15,,,PTIEN)
                               SET ECNT=ECNT+1
 +79                           if $GET(RORPARM("DEBUG"))<3
                                   SET RC=0
 +80                           SET TMP=$$ADDERR^RORHDT05(HDEIEN,TASKIEN,PTIEN)
 +81                           if TMP<0
                                   SET RC=TMP
                           End DoDot:3
                           if RC!COMMIT
                               QUIT 
 +82                   IF RC<0
                           if 'STOP
                               QUIT 
 +83      ;--- Commit the data
 +84                   SET NEXT=$SELECT(COMMIT:$$NEXTPAT(PTIEN,.RGIENLST),1:PTIEN)
 +85                   SET TMP=$$COMMIT^RORHDT05(OUTDIR,FILE)
 +86                   if TMP<0
                           SET RC=TMP
                   End DoDot:2
                   if RC!STOP!(PTIEN'>0)
                       QUIT 
           End DoDot:1
 +87      ;
 +88      ;--- The $$COMMIT^RORHDT05 returns -34 if it was not able to create
 +89      ;--- the output file (wrong directory name, protection error, etc.).
 +90       if RC'=-34
               Begin DoDot:1
 +91               NEW NODE,RORFDA,RORMSG
 +92      ;
 +93      ;=== Write the batch trailer segment and close the file if
 +94      ;=== the batch is not empty. Otherwise, record a warning.
 +95               IF '$GET(RORHDT("BHS"))
                       Begin DoDot:2
 +96                       SET TMP=$SELECT(ECNT!(RC<0):"Completed with errors",STOP:"Stopped",1:"")
 +97                       USE IO
                           WRITE $$BTS^RORHL7A($$MSGCNT^RORHL7,TMP),$CHAR(13)
 +98                       DO CLOSE^%ZISH("HL7FILE")
                       End DoDot:2
 +99              IF '$TEST
                       DO ERROR^RORERR(-89)
 +100     ;
 +101     ;=== Update the NEXT RECORD IEN field in the task record
 +102              IF $DATA(NEXT)
                       if NEXT'>0
                           Begin DoDot:2
 +103     ;--- If the task completed successfully, the NEXT RECORD IEN
 +104     ;    field is set to an empty string. If the task is restarted
 +105     ;--- afterwards, it will re-extract all data again.
 +106                          IF 'ECNT
                                   SET NEXT=""
                                   QUIT 
 +107     ;--- If completed with errors, use IEN of the last record
 +108     ;--- processed by the task incremented by 1.
 +109                          IF RREIEN>0
                                   SET NEXT=RREIEN+1
                                   QUIT 
 +110     ;--- Or the IEN of the last patient record incremented by 1
 +111     ;--- (in case of the last/single task).
 +112                          SET NEXT=$ORDER(^RORDATA(798,"KEY",""),-1)+1
 +113     ;--- When the task is restarted, it will try to re-extract only
 +114     ;    erroneous records and will not process already extracted
 +115     ;    data (the PTIEN will not be less than the RREIEN or the
 +116     ;--- $ORDER function will not return a value greater than zero).
                           End DoDot:2
 +117             IF '$TEST
                       if (RC<0)!ECNT!STOP
                           QUIT 
                       SET NEXT=""
 +118     ;
 +119     ;=== Update the task record
 +120              SET IENS=(+TASKIEN)_","_(+HDEIEN)_","
 +121              SET RORFDA(799.64,IENS,.04)=NEXT
 +122              DO FILE^DIE("K","RORFDA","RORMSG")
 +123              SET TMP=$$DBS^RORERR("RORMSG",-9,,,799.64,IENS)
               End DoDot:1
 +124     ;
 +125     ;=== Cleanup
 +126      KILL ^TMP("RORPTF",$JOB)
 +127      if RC'<0
               SET RC=$$CLRERRS^RORHDT05(HDEIEN,TASKIEN)
 +128      QUIT $SELECT(RC<0:RC,1:CNT_U_ECNT)
 +129     ;
 +130     ;***** RETURNS THE NEXT PATIENT FOR DATA EXTRACTION
 +131     ;
 +132     ; PTIEN         Patient IEN (DFN)
 +133     ;
 +134     ; .RGIENLST     Reference to a local array containing registry
 +135     ;               IENs as subscripts. The IENs of the corresponding
 +136     ;               patient's registry records are returned as values.
 +137     ;
 +138     ; Return Values:
 +139     ;        0  No more patients
 +140     ;       >0  IEN (DFN) of the next patient who belongs to at least
 +141     ;           one of the registries defined by the RGIENLST parameter.
 +142     ;
NEXTPAT(PTIEN,RGIENLST) ;
 +1        NEW CNT,IEN,REGIEN
 +2        SET CNT=0
 +3        FOR 
               SET PTIEN=$ORDER(^RORDATA(798,"KEY",PTIEN))
               if PTIEN'>0
                   QUIT 
               Begin DoDot:1
 +4                SET REGIEN=0
 +5                FOR 
                       SET REGIEN=$ORDER(RGIENLST(REGIEN))
                       if REGIEN'>0
                           QUIT 
                       Begin DoDot:2
 +6                        SET RGIENLST(REGIEN)=0
 +7                        SET IEN=+$ORDER(^RORDATA(798,"KEY",PTIEN,REGIEN,""))
 +8                        if IEN'>0
                               QUIT 
 +9       ;************************************************************
 +10      ; Patch 10: also include pending patients
 +11      ;--- Skip inactive records (pending and marked for deletion)
 +12      ;Q:'$$ACTIVE^RORDD(IEN)
 +13      ;************************************************************
 +14      ;--- skip patients marked for deletion
 +15                       IF $$DEL^RORDD(IEN)
                               QUIT 
 +16      ;--- Skip records tagged as "DON'T SEND" and skip test patients
 +17                       IF (($PIECE($GET(^RORDATA(798,IEN,2)),U,4))!($$TESTPAT^RORUTL01(PTIEN)))
                               QUIT 
 +18      ;--- Consider the record
 +19                       SET RGIENLST(REGIEN)=IEN
                           SET CNT=CNT+1
                       End DoDot:2
               End DoDot:1
               if CNT
                   QUIT 
 +20       QUIT $SELECT(PTIEN>0:PTIEN,1:0)
 +21      ;
 +22      ;***** PROCESSES A RECORD IN THE REGISTRY
 +23      ;
 +24      ; PTIEN         Patient IEN (DFN)
 +25      ;
 +26      ; .RGIENLST     Reference to a local array containing registry
 +27      ;               IENs as subscripts and IENs of the corresponding
 +28      ;               patient's registry records as values.
 +29      ;
 +30      ; Return Values:
 +31      ;       <0  Error code
 +32      ;        0  Ok
 +33      ;        1  Nothing has been extracted
 +34      ;
PROCREC(PTIEN,RGIENLST) ;
 +1       ; Default error location
           NEW RORERRDL
 +2       ;
 +3        NEW BATCHID,CNT,DXDTS,IEN,MSHPTR,RC,REGIEN,RORMSH,TMP
 +4        DO CLEAR^RORERR("PROCREC^RORHDT04")
 +5       ;
 +6       ;--- Compile the data extraction time frames
 +7        SET (CNT,RC,REGIEN)=0
 +8        FOR 
               SET REGIEN=$ORDER(RGIENLST(REGIEN))
               if REGIEN'>0
                   QUIT 
               Begin DoDot:1
 +9                SET IEN=+RGIENLST(REGIEN)
                   if IEN'>0
                       QUIT 
 +10               SET RC=$$DXPERIOD^ROREXTUT(.DXDTS,IEN,PTIEN)
 +11               if 'RC
                       SET CNT=CNT+1
 +12               if RC>0
                       SET RGIENLST(REGIEN)=0
               End DoDot:1
               if RC<0
                   QUIT 
 +13       if RC<0
               QUIT RC
 +14      ;--- If the patient should be skipped in all registries
 +15      ;    that are being processed, then do not perform the data
 +16      ;--- extraction for this patient at all.
 +17       IF 'CNT
               if $GET(RORPARM("DEBUG"))
                   Begin DoDot:1
 +18                   DO LOG^RORLOG(4,"There is no data to extract.",PTIEN)
                   End DoDot:1
               QUIT 0
 +19      ;
 +20      ;--- Create an HL7 message for the patient
 +21       SET MSHPTR=$$CREATE^RORHL7(.RORMSH)
           if MSHPTR<0
               QUIT MSHPTR
 +22       SET RC=$$MESSAGE^ROREXT02(PTIEN,.RGIENLST,.DXDTS,$GET(ROREXT("HDTIEN")))
 +23      ;
 +24      ;--- Delete the unfinished message from the ^TMP("HLS",$J)
 +25      ;    if there is no data to send (RC>0) or there was an error
 +26      ;    during the data extraction (RC<0). Return the error code
 +27      ;--- in the latter case.
 +28       IF RC!($ORDER(^TMP("HLS",$JOB,""),-1)=MSHPTR)
               Begin DoDot:1
 +29               DO ROLLBACK^RORHL7(MSHPTR)
                   if 'RC
                       SET RC=1
               End DoDot:1
               if RC<0
                   QUIT RC
 +30      ;---
 +31       QUIT RC