C0CTIU	; C0C/ELN - PROCESSING FOR TIU NOTES ; 19/10/2010
	;;1.2;C0C;;May 11, 2012;Build 47
	;
	;
	;ELN - Modified Routine of C0CLABS
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) D  Q
	. S C0CO=$NA(^TMP("C0CCCR",$J,DFN,"RESULTS")) ;DEFAULT FOR OUTPUT
	. M @C0CO=C0COXML ; COPY RESULTS TO OUTPUT
	E  D
	. N C0COOXML
	. S CCRGLO=$NA(^TMP("C0CCCR",$J,DFN,"CCR"))
	. D QUERY^C0CXPATH(CCRGLO,"//ContinuityOfCareRecord/Body/Results","C0CRSXML")
	. S C0COCNT=$O(C0CRSXML(""),-1)
	. S C0CRES=0
	. F  S C0CRES=$O(C0COXML(C0CRES)) Q:C0CRES=""  D
	. . Q:$G(C0COXML(C0CRES))="<Results>"!($G(C0COXML(C0CRES))="</Results>")
	. . S C0CRSXML(C0COCNT)=$G(C0COXML(C0CRES))
	. . S C0COCNT=C0COCNT+1
	. S C0CRSXML(C0COCNT)="</Results>"
	. S C0CRSXML(0)=C0COCNT
	. D REPLACE^C0CXPATH(CCRGLO,"","//ContinuityOfCareRecord/Body")
	. D INSERT^C0CXPATH(CCRGLO,"C0CRSXML","//ContinuityOfCareRecord/Body")
	;
	S C0CO=MOXML,@C0CO@(0)=0
	K C0CRSXML,C0COCNT,C0COXML,C0CRES
	K C0CCNT
	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,) ; FIRST CALL EXTRACT
	I '$D(@C0CV@(0)) D  Q  ; NO VARS THERE
	. S RTN(0)=0 ; PASS BACK NO RESULTS INDICATOR
	; NO RESULTS QUIT
	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,C0CJS,C0CJE,C0CJN,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 QOPEN^C0CXPATH("C0CRBLD",C0CRTMP,C0CIS) ;1ST PART OF XML
	. 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 TIU NOTES INTO THE C0CLVAR GLOBAL
	;
	S C0CLB=$NA(^TMP("C0CCCR",$J,"RESULTS")) ; BASE GLB FOR LABS VARS
	D DT^DILF(,$$GET^C0CPARMS("TIULIMIT"),.C0CTSDT)
	D DT^DILF(,$$GET^C0CPARMS("TIUSTART"),.C0CTEDT) ; END DATE TODAY - IMPLEMENT END DATE PARM
	;
	S TIUIEN=0,TIUCNT=1
	F  S TIUIEN=$O(^TIU(8925,"C",DFN,TIUIEN)) Q:TIUIEN=""  D
	. S TIUY="",TIUDA=TIUIEN,ACTION="VIEW",U="^"
	. ;SELECT ONLY COMPLETED NOTES
	. Q:$P(^TIU(8925,TIUIEN,0),U,5)=""
	. Q:$P(^TIU(8925.6,$P(^TIU(8925,TIUIEN,0),U,5),0),U)'="COMPLETED"
	. ;VALIDATE ON SIGNATURE DATE #1501
	. Q:$P(^TIU(8925,TIUIEN,15),U)<C0CTSDT!($P(^TIU(8925,TIUIEN,15),U)>C0CTEDT)
	. D TGET(TIUY,TIUIEN,ACTION,TIUCNT)
	. S TIUCNT=TIUCNT+1
	;ELN K @C0CLB ; CLEAR OUT OLD VARS IF ANY
	N QTSAV S QTSAV=$G(C0CQT) ;SAVE QUIET FLAG
	S C0CQT=1 ; SURPRESS LISTING
	D LIST ; EXTRACT THE VARIABLES
	K ^TMP("C0CTIU",$J),TIUIEN,TIUCNT,TIUDA,TIUY,C0CLB,C0CTSDT,C0CTEDT
	S C0CQT=QTSAV ; RESET SILENT FLAG
	I $D(OLXML) S @OLXML@(0)=0 ; EXTRACT DOES NOT PRODUCE XML... SEE MAP^C0CLABS
	Q
	;REUSING from ^TIUSRVR2
TGET(TIUY,TIUDA,ACTION,TIUCNT)	; Build ^TMP("TIUVIEW",$J,
	N TIUL,TIUREC,TIUARR,TIUGDATA,TIUNAME,TIUPRM0,TIUPRM1,X,Y,TIUCPF,ONBROWSE
	K ^TMP("TIUVIEW",$J),^TMP("TIU FOCUS",$J)
	S C0CTIU=$NA(^TMP("C0CTIU",$J,TIUCNT))
	S ACTION=$G(ACTION,"VIEW"),TIUL=0
	D SETPARM^TIULE
	S TIUGDATA=$$SETGDATA^TIUSRVR1(TIUDA)
	S TIUY=$NA(^TMP("TIUVIEW",$J))
	S TIUARR="^TMP(""TIUVIEW"",$J)"
	I '$D(^TIU(8925,+TIUDA,0)) Q
	; Initialize ^TMP("TIU FOCUS",$J) to the entry that has focus
	S ^TMP("TIU FOCUS",$J)=TIUDA
	; Call INQUIRE to get record
	;Set a flag to indicate whether or not a Title is a memer of the
	;Clinical Procedures Class (1=Yes and 0=No)
	S TIUCPF=+$$ISA^TIULX(+$G(^TIU(8925,TIUDA,0)),+$$CLASS^TIUCP)
	; Call INQUIRE to get record
	D INQUIRE^TIUSRVR2(TIUDA,.TIUREC,TIUCPF)
	; First, load dictation, transcription data, etc.
	;D LOADTOP^TIUSRVR1(.TIUREC,TIUDA,.TIUL,TIUGDATA,TIUCPF)
	; Next, load the remainder of the record
	D LOADREC^TIUSRVR2(TIUDA,.TIUL,TIUGDATA,0,ACTION)
	K ^TMP("TIU FOCUS",$J)
	;S VALMCNT=+$G(TIUL)
	M @C0CTIU@("TIUREC")=TIUREC(8925,TIUDA)
	M @C0CTIU@("TIUTEXT")=@TIUY
	K ^TMP("TIUVEW",$J)
	Q
LIST	;EXTRACT THE RESULT VARIABLES TO C0CLB
	;
	;N C0CI,C0CJ,C0COBT,C0CHB,C0CVAR
	I '$D(C0CLB) S C0CLB=$NA(^TMP("C0CCCR",$J,"RESULTS")) ; BASE GLB FOR LABS VARS
	I '$D(C0CQT) S C0CQT=0
	I '$D(DFN) S DFN=1 ; DEFAULT TEST PATIENT
	S C0CI=""
	S @C0CLB@(0)=$O(^TMP("C0CCCR",$J,"RESULTS",""),-1) ; INITALIZE RESULTS VARS COUNT
	S C0CCNT=0,C0CTIU=$NA(^TMP("C0CTIU",$J))
	F  S C0CCNT=$O(@C0CTIU@(C0CCNT)) Q:C0CCNT=""  D
	. D C0CRES,C0CTRES
	K C0CCNT,C0CTIU,C0CI,C0CLI,C0CX1
	Q
C0CRES	;SET TITLE NAME PART EQUIVALENT TO TEST NAME PART
	N XV
	S @C0CLB@(0)=@C0CLB@(0)+1 ; INCREMENT COUNT
	S C0CLI=@C0CLB@(0) ; INDEX FOR THIS RESULT
	S XV("RESULTOBJECTID")="RESULT_"_C0CLI
	S C0CX1=$G(@C0CTIU@(C0CCNT,"TIUREC",1502))
	S XV("RESULTSOURCEACTORID")="ACTORPROVIDER_"_$O(^VA(200,"B",$G(C0CX1),0))
	S XV("RESULTASSESSMENTDATETIME")=$$FMDTOUTC^C0CUTIL($$C0CDATE^C0CTIU1($G(@C0CTIU@(C0CCNT,"TIUREC",1501))),"DT")
	S XV("RESULTCODE")=""
	S XV("RESULTCODINGSYSTEM")=""
	S XV("RESULTSTATUS")="COMPLETED"
	S XV("RESULTDESCRIPTIONTEXT")="Progress Notes"
	M @C0CLB@(C0CLI)=XV ; PERSIST THE OBR VARS
	Q
C0CTRES	;SET REPORT TEXT PART EQUIVALENT TO RESULT
	N XV,C0CLOBX,C0CZG,C0CLB2
	S C0CLOBX=0
	S XV("RESULTTESTCODEVALUE")=$G(@C0CTIU@(C0CCNT,"TIUREC",.01))
	S XV("RESULTTESTCODINGSYSTEM")=""
	S XV("RESULTTESTDESCRIPTIONTEXT")=$G(@C0CTIU@(C0CCNT,"TIUREC",.01)) ; DESCRIPTION TEXT
	S C0CZG=""
	S XV("RESULTTESTVALUE")="Notes"
	M XV("RESULTTESTVALUE","WP")=@C0CTIU@(C0CCNT,"TIUTEXT")
	S C0CLB2=$NA(@C0CLB@(C0CLI,"M","TEST")) ; INDENT FOR TEST RESULTS
	S C0CLOBX=C0CLOBX+1 ; INCREMENT TEST COUNT
	S @C0CLB2@(0)=C0CLOBX ; STORE THE TEST COUNT
	S XV("RESULTTESTOBJECTID")="RESULTTEST_"_C0CLI_"_"_C0CLOBX
	S XV("RESULTTESTSOURCEACTORID")="ACTORORGANIZATION_"_$O(^VA(200,$O(^VA(200,"B",$G(C0CX1),0)),2,0))
	S XV("RESULTTESTNORMALSOURCEACTORID")=XV("RESULTTESTSOURCEACTORID")
	S XV("RESULTTESTDATETIME")=$$FMDTOUTC^C0CUTIL($$C0CDATE^C0CTIU1($G(@C0CTIU@(C0CCNT,"TIUREC",1501))),"DT")
	S XV("RESULTTESTUNITS")=""
	S XV("RESULTTESTFLAG")=""
	S XV("RESULTTESTSTATUSTEXT")=""
	S XV("RESULTTESTNORMALDESCTEXT")=""
	M @C0CLB2@(C0CLOBX)=XV ; PERSIST THE TEST RESULT VARIABLES
	Q
