C0COVRES	; CCDCCR/ELN - CCR/CCD PROCESSING FOR LAB,RAD,TIU RESULTS ; 10/12/15
	       ;;1.2;C0C;;May 11, 2012;Build 47
	       ;
MAP(MIXML,DFN,MOXML)	   ;TO MAKE THIS COMPATIBLE WITH OLD CALLING FOR EXTRACT
	       ; ASSUMES THAT EXTRACT HAS BEEN RUN AND THE VARIABLES STORED IN MIVAR
	       ; MIXML,MIVAR, AND MOXML ARE PASSED BY NAME
	       ; MIXML IS THE TEMPLATE TO USE
	       ; MOXML IS THE OUTPUT XML ARRAY
	       ; DFN IS THE PATIENT RECORD NUMBER
	       N C0COXML,C0CO,C0CV,C0CIXML
	       I '$D(MIVAR) S C0CV="" ;DEFAULT
	       E  S C0CV=MIVAR ;PASSED VARIABLE ARRAY
	       I '$D(MIXML) S C0CIXML="" ;DEFAULT
	       E  S C0CIXML=MIXML ;PASSED INPUT XML
	       D RPCMAP(.C0COXML,DFN,C0CV,C0CIXML) ; CALL RPC TO DO THE WORK
	       I '$D(MOXML) S C0CO=$NA(^TMP("C0CCCR",$J,DFN,"RESULTS")) ;DEFAULT FOR OUTPUT
	       E  S C0CO=MOXML
	       M @C0CO=C0COXML ; COPY RESULTS TO OUTPUT
	       Q
RPCMAP(RTN,DFN,RMIVAR,RMIXML)	; RPC ENTRY POINT FOR MAPPING RESULTS
	       ; RTN IS PASSED BY REFERENCE
	       N C0CT0,C0CT,C0CV ; CCR TEMPLATE, RESULTS SUBTEMPLATE, VARIABLES
	       N C0CRT,C0CTT ; TEST REQUEST TEMPLATE, TEST RESULT TEMPLATE
	       I '$D(DEBUG) S DEBUG=0 ; DEFAULT NO DEBUGGING
	       I RMIXML="" D  ; INPUT XML NOT PASSED
	       . D LOAD^C0CCCR0("C0CT0") ; LOAD ENTIRE CCR TEMPLATE
	       . D QUERY^C0CXPATH("C0CT0","//ContinuityOfCareRecord/Body/Results","C0CT0R")
	       . S C0CT="C0CT0R" ; NAME OF EXTRACTED RESULTS TEMPLATE
	       E  S C0CT=RMIXML ; WE ARE PASSED THE RESULTS PART OF THE TEMPLATE
	       I RMIVAR="" D  ; LOCATION OF VARIABLES NOT PASSED
	       . S C0CV=$NA(^TMP("C0CCCR",$J,"RESULTS")) ;DEFAULT VARIABLE LOCATION
	       E  S C0CV=RMIVAR ; PASSED LOCATIONS OF VARS
	       D CP^C0CXPATH(C0CT,"C0CRT") ; START MAKING TEST REQUEST TEMPLATE
	       D REPLACE^C0CXPATH("C0CRT","","//Results/Result/Test") ; DELETE TEST FROM REQ
	       D QUERY^C0CXPATH(C0CT,"//Results/Result/Test","C0CTT") ; MAKE TEST TEMPLATE
	       D EXTRACT("C0CT",DFN,) ; LAB EXTRACT
	       D EXTRACT^C0CRARPT("C0CT",DFN,) ; RAD REPORT EXTRACT
	       ;OHUM/RUT 3111221
	       ;D EXTRACT^C0CTIU("C0CT",DFN,) ; TIU EXTRACT
	       I ^TMP("C0CCCR","TIULIMIT")'="" D EXTRACT^C0CTIU("C0CT",DFN,) ; TIU EXTRACT
	       ;OHUM/RUT
	       I '$D(@C0CV@(0)) D  Q  ; NO VARS THERE
	       . S RTN(0)=0 ; PASS BACK NO RESULTS INDICATOR
	       ; NO RESULTS
	       I @C0CV@(0)=0 S RTN(0)=0 Q
	       S RIMVARS=$NA(^TMP("C0CRIM","VARS",DFN,"RESULTS"))
	       K @RIMVARS
	       ;M @RIMVARS=@C0CV ; UPDATE RIMVARS SO THEY STAY IN SYNCH
	       N C0CI,C0CIN,C0CJ,C0CJN,C0CJE,C0CJS,C0CMAP,C0CTMAP,C0CTMP
	       S C0CIN=@C0CV@(0) ; COUNT OF RESULTS (OBR)
	       N C0CRTMP ; AREA TO BUILD ONE RESULT REQUEST AND ALL TESTS FOR IT
	       N C0CRBASE S C0CRBASE=$NA(^TMP($J,"TESTTMP")) ;WORK AREA
	       N C0CRBLD ; BUILD LIST FOR XML - THE BUILD IS DELAYED UNTIL THE END
	       ; TO IMPROVE PERFORMANCE
	       D QUEUE^C0CXPATH("C0CRBLD","C0CRT",1,1) ;<Results>
	       F C0CI=1:1:C0CIN D  ; LOOP THROUGH VARIABLES
	       . K C0CMAP,C0CTMP ;EMPTY OUT LAST BATCH OF VARIABLES
	       . S C0CRTMP=$NA(@C0CRBASE@(C0CI)) ;PARTITION OF WORK AREA FOR EACH TEST
	       . S C0CMAP=$NA(@C0CV@(C0CI)) ;MAPPING FOR TEST REQUEST GOES HERE
	       . D MAP^C0CXPATH("C0CRT",C0CMAP,C0CRTMP) ; MAP OBR DATA
	       . D QUEUE^C0CXPATH("C0CRBLD",C0CRTMP,2,@C0CRTMP@(0)-4) ;UP TO <Test>
	       . I $D(@C0CMAP@("M","TEST",0)) D  ; TESTS EXIST
	       . . S C0CJN=@C0CMAP@("M","TEST",0) ; NUMBER OF TESTS
	       . . K C0CTO ; CLEAR OUTPUT VARIABLE
	       . . F C0CJ=1:1:C0CJN D   ;FOR EACH TEST RESULT
	       . . . K C0CTMAP ; EMPTY MAPS FOR TEST RESULTS
	       . . . S C0CTMP=$NA(@C0CRBASE@(C0CI,C0CJ)) ;WORK AREA FOR TEST RESULTS
	       . . . S C0CTMAP=$NA(@C0CMAP@("M","TEST",C0CJ)) ;
	       . . . D XMAP^C0CTIU1("C0CTT",C0CTMAP,C0CTMP) ; MAP TO TMP
	       . . . I C0CJ=1 S C0CJS=2 E  S C0CJS=1 ;FIRST TIME,SKIP THE <Test>
	       . . . I C0CJ=C0CJN S C0CJE=@C0CTMP@(0)-1 E  S C0CJE=@C0CTMP@(0) ;</Test>
	       . . . S C0CJS=1 S C0CJE=@C0CTMP@(0) ; INSERT ALL OF THE TEXT XML
	       . . . D QUEUE^C0CXPATH("C0CRBLD",C0CTMP,C0CJS,C0CJE) ; ADD TO BUILD LIST
	       . D QUEUE^C0CXPATH("C0CRBLD","C0CRT",C0CRT(0)-1,C0CRT(0)-1) ;</Result>
	       D QUEUE^C0CXPATH("C0CRBLD","C0CRT",C0CRT(0),C0CRT(0)) ;</Results>
	       D BUILD^C0CTIU1("C0CRBLD","RTN") ;RENDER THE XML
	       K @C0CRBASE ; CLEAR OUT TEMPORARY STURCTURE
	       Q
EXTRACT(ILXML,DFN,OLXML)	; EXTRACT LABS INTO THE C0CLVAR GLOBAL
	       ; LABXML AND LABOUTXML ARE PASSED BY NAME SO GLOBALS CAN BE USED
	       N C0CNSSN,C0CLB ; IS THERE AN SSN FLAG
	       S C0CNSSN=0
	       S C0CLB=$NA(^TMP("C0CCCR",$J,"RESULTS")) ; BASE GLB FOR LABS VARS
	       D GHL7^C0COVREU ; GET HL7 MESSAGE FOR THIS PATIENT
	       I C0CNSSN=1 D  Q  ; NO SSN, CAN'T GET HL7 FOR THIS PATIENT
	       . S @C0CLB@(0)=0
	       ;K @C0CLB ; CLEAR OUT OLD VARS IF ANY
	       N QTSAV S QTSAV=$G(C0CQT) ;SAVE QUIET FLAG
	       S C0CQT=1 ; SURPRESS LISTING
	       D LIST^C0COVREL ; EXTRACT THE VARIABLES
	       S C0CQT=QTSAV ; RESET SILENT FLAG
	       K ^TMP("HLS",$J) ; KILL HL7 MESSAGE OUTPUT
	       I $D(OLXML) S @OLXML@(0)=0 ; EXTRACT DOES NOT PRODUCE XML... SEE MAP^C0CLABS
	       Q
