C0COVREL	; CCDCCR/ELN - CCR/CCD PROCESSING FOR LAB,RAD,TIU RESULTS ; 10/12/15
	       ;;1.2;C0C;;May 11, 2012;Build 46
LIST	   ; LIST THE HL7 MESSAGE; ALSO, EXTRACT THE RESULT VARIABLES TO C0CLB
	       N C0CI,C0CJ,C0COBT,C0CHB,C0CVAR,C0CLB2,C0CLB,C0CLI,C0CLOBX,C0CTAB,C0CTYP
	       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
	       I '$D(^TMP("C0CCCR","LABTBL",0)) D SETTBL^C0COVREU ;INITIALIZE LAB TABLE
	       I ^TMP("C0CCCR","LABTBL",0)'="V3" D SETTBL^C0COVREU ;NEED NEWEST VERSION
	       I '$D(^TMP("HLS",$J,1)) D GHL7^C0COVREU ; GET HL7 MGS IF NOT ALREADY DONE
	       S C0CTAB=$NA(^TMP("C0CCCR","LABTBL")) ; BASE OF OBX TABLE
	       S C0CHB=$NA(^TMP("HLS",$J))
	       S C0CI=""
	       S @C0CLB@(0)=0 ; INITALIZE RESULTS VARS COUNT
	       F  S C0CI=$O(@C0CHB@(C0CI)) Q:C0CI=""  D  ; FOR ALL RECORDS IN HL7 MSG
	       . K C0CVAR,XV,C0CX1,C0CX2 ; CLEAR OUT VARIABLE VALUES
	       . S C0CTYP=$P(@C0CHB@(C0CI),"|",1)
	       . D LTYP^C0COVREU(@C0CHB@(C0CI),C0CTYP,.C0CVAR,C0CQT)
	       . M XV=C0CVAR ;
	       . I C0CTYP="OBR" D  ; BEGINNING OF NEW SECTION
	       . . S @C0CLB@(0)=@C0CLB@(0)+1 ; INCREMENT COUNT
	       . . S C0CLI=@C0CLB@(0) ; INDEX FOR THIS RESULT
	       . . ;M @C0CLB@(C0CLI)=C0CVAR ; PERSIST THE OBR VARS
	       . . S XV("RESULTOBJECTID")="RESULT_"_C0CLI
	       . . S C0CX1=XV("RESULTSOURCEACTORID") ; SOURCE FROM OBR
	       . . S XV("RESULTSOURCEACTORID")="ACTORPROVIDER_"_$P($P(C0CX1,"^",1),"-",1)
	       . . S C0CX1=XV("RESULTASSESSMENTDATETIME") ;DATE TIME IN HL7 FORMAT
	       . . S C0CX2=$$HL7TFM^XLFDT(C0CX1,"L") ;FM DT LOCAL
	       . . S XV("RESULTASSESSMENTDATETIME")=$$FMDTOUTC^C0CUTIL(C0CX2,"DT") ;UTC TIME
	       . . M @C0CLB@(C0CLI)=XV ; PERSIST THE OBR VARS
	       . . S C0CLOBX=0 ; MARK THE BEGINNING OF A NEW SECTION
	       . I C0CTYP="OBX" D  ; SPECIAL CASE FOR OBX
	       . . ; RESULTTESTCODEVALUE AND RESULTTESTDESCRIPTIONTEXT
	       . . I C0CVAR("C3")="LN" D  ; PRIMARY CODE IS LOINC
	       . . . S XV("RESULTTESTCODEVALUE")=C0CVAR("C1") ; THE LOINC CODE VALUE
	       . . . S XV("RESULTTESTCODINGSYSTEM")="LOINC" ; DISPLAY NAME FOR LOINC
	       . . . S XV("RESULTTESTDESCRIPTIONTEXT")=C0CVAR("C2") ; DESCRIPTION TEXT
	       . . E  I C0CVAR("C6")="LN" D  ; SECONDARY CODE IS LOINC
	       . . . S XV("RESULTTESTCODEVALUE")=C0CVAR("C4") ; THE LOINC CODE VALUE
	       . . . S XV("RESULTTESTCODINGSYSTEM")="LOINC" ; DISPLAY NAME FOR LOINC
	       . . . S XV("RESULTTESTDESCRIPTIONTEXT")=C0CVAR("C5") ; DESCRIPTION TEXT
	       . . E  I C0CVAR("C6")'="" D  ; NO LOINC CODES, USE SECONDARY IF PRESENT
	       . . . S XV("RESULTTESTCODEVALUE")=C0CVAR("C4") ; SECONDARY CODE VALUE
	       . . . S XV("RESULTTESTCODINGSYSTEM")=C0CVAR("C6") ; SECONDARY CODE NAME
	       . . . S XV("RESULTTESTDESCRIPTIONTEXT")=C0CVAR("C5") ; SECONDARY TEXT
	       . . E  D  ; NO SECONDARY, USE PRIMARY
	       . . . S XV("RESULTTESTCODEVALUE")=C0CVAR("C1") ; PRIMARY CODE VALUE
	       . . . S XV("RESULTTESTCODINGSYSTEM")=C0CVAR("C3") ; PRIMARY DISPLAY NAME
	       . . . S XV("RESULTTESTDESCRIPTIONTEXT")=C0CVAR("C2") ; USE PRIMARY TEXT
	       . . N C0CZG S C0CZG=XV("RESULTTESTNORMALDESCTEXT") ;
	       . . S XV("RESULTTESTNORMALDESCTEXT")=C0CZG
	       . . S C0CZG=XV("RESULTTESTVALUE")
	       . . S XV("RESULTTESTVALUE")=C0CZG
	       . I C0CTYP="OBX" D  ; PROCESS TEST RESULTS
	       . . I C0CLOBX=0 D  ; FIRST TEST RESULT FOR THIS SECTION
	       . . . 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 C0CX1=XV("RESULTTESTSOURCEACTORID") ; TEST SOURCE
	       . . S C0CX2=$P($P(C0CX1,"^",1),"-",1) ; PULL OUT STATION NUMBER
	       . . S XV("RESULTTESTSOURCEACTORID")="ACTORORGANIZATION_"_C0CX2
	       . . S XV("RESULTTESTNORMALSOURCEACTORID")=XV("RESULTTESTSOURCEACTORID")
	       . . S C0CX1=XV("RESULTTESTDATETIME") ;DATE TIME IN HL7 FORMAT
	       . . S C0CX2=$$HL7TFM^XLFDT(C0CX1,"L") ;FM DT LOCAL
	       . . S XV("RESULTTESTDATETIME")=$$FMDTOUTC^C0CUTIL(C0CX2,"DT") ;UTC TIME
	       . . M @C0CLB2@(C0CLOBX)=XV ; PERSIST THE TEST RESULT VARIABLES
	       . I 'C0CQT D  ;
	       . . W C0CI," ",C0CTYP,!
	       Q
