<!-- BEGIN outreach/data_summary.tt -->
  [% # PROCESS dumper.tt dump = request %]
  [% # PROCESS dumper.tt dump = raw_lab_test_data.haematology_data.as_tree %]

  [% # manipulate some immunology params:
    immunology = outreach.immunology;
    
    IF immunology.electrophoresis.result.match('Isotype undetermined');
      immunology.electrophoresis.result = '';
    END;
  
    FOREACH param IN ['IgG' 'IgA' 'IgM'];
      IF immunology.item(param).result;
        IF immunology.item(param).result * 100 == immunology.item(param).default * 100;
          immunology.item(param).result = '';
        END;
      END;
    END;
    
    IF immunology.paraprotein.result;
      IF immunology.paraprotein.result * 100 == immunology.paraprotein.default * 100;
        IF immunology.electrophoresis.result.match('Ig');
          immunology.paraprotein.result = 'Unquantifiable';
        ELSIF immunology.electrophoresis.result.match('No paraprotein');
          immunology.paraprotein.result = '';
        ELSE;
          immunology.paraprotein.result = 'N/K';
        END;
      END;
    END;
    
    IF outreach.flow_cytometry.cd19_neg_pc.result;
      IF outreach.flow_cytometry.cd19_neg_pc.result < 0.001;
        outreach.flow_cytometry.cd19_neg_pc.result = '< 0.001';
      ELSE;
        outreach.flow_cytometry.cd19_neg_pc.result = 
          sig_figs(outreach.flow_cytometry.cd19_neg_pc.result);
      END;
    END;
    
    IF outreach.calculated.abnormal_b_cells.result;
      IF outreach.calculated.abnormal_b_cells.result < 0.01;
        outreach.calculated.abnormal_b_cells.result = '< 0.01';
      ELSE;
        outreach.calculated.abnormal_b_cells.result = 
          sig_figs(outreach.calculated.abnormal_b_cells.result);
      END;
    END;

	this_diagnosis = report.diagnosis OR report_data.diagnosis; # depends on Outreach or not
	# is this a CML case ?
	is_cml = ( outreach.requested_lab_tests.grep('rq_cml').size
	  || this_diagnosis.icdo3.match('9875/3') ) ? 1 : 0; # INCLUDE dumper.tt dump = is_cml;

	IF is_cml; # add haematology_data neutrophils to outreach.heamatology data:
	  neutr = raw_lab_test_data.haematology_data.neutr;
	  IF neutr.match('\*'); # doesn't work as A ? B : C
		outreach.haematology.neutrophils.result = '****';
	  ELSIF neutr; # won't exist if no PB sample
		outreach.haematology.neutrophils.result = ( 0.1 * neutr ) | format('%.1f');
	  END;
	  outreach.haematology.neutrophils.field_label = 'Neutrophils';
	  outreach.haematology.neutrophils.units = 'x10/9/L';
	ELSE;	
	  # total B cells calculated so doesn't get value from default_and_ranges table:
	  outreach.calculated.total_b_cells.units = outreach.haematology.wbc.units;
	  outreach.calculated.total_b_cells.param_name = 'total_b_cells';
	  outreach.calculated.total_b_cells.field_label = 'Total B';
	  
	  # abnormal B cells calculated so doesn't get value from default_and_ranges table:
	  outreach.calculated.neoplastic_b_cells.units = outreach.haematology.wbc.units;
	  outreach.calculated.neoplastic_b_cells.param_name = 'neoplastic_b_cells';
	  outreach.calculated.neoplastic_b_cells.field_label = 'Abnormal B';
    END;
    # PROCESS dumper.tt dump = outreach.calculated;
  %]
  
  [% BLOCK rows;
    patient_id = request_data.patient_case.patient_id;    
    
    # if param carries default value, set to N/K:    
    IF is_numerical(param.result) AND param.default # AND param.result
        AND 100 * param.result == 100 * param.default; # PROCESS dumper.tt dump = param;
            param.result = 'N/K'; # PROCESS dumper.tt dump = param;
    END;
        
	IF is_numerical(param.result); # no maths funcs unless numerical:
    	IF
            ( param.lower_flag && param.result < param.lower_flag ) OR
            ( param.upper_flag && param.result > param.upper_flag );
            highlight_class = 'rangeFlag'; # stronger highlight
        ELSIF 
            ( param.lower_limit && param.result < param.lower_limit ) OR
            ( param.upper_limit && param.result > param.upper_limit );
            highlight_class = 'bold'; # weaker highlight
		END;
        
        result_sig_figs = sig_figs(param.result);
    END; %]
  
    <tr>
      <td align="right">
        [% IF skip_url; param.field_label;
            ELSE; var = param.param_name; %]
        <a href="[% app_url %]/chart/outreach_param/[% var %]/[% patient_id %]"
            onclick="return popup(this.href);">
          <span class="acronym" title="chart">[% param.field_label %]</span>			
        </a>
        [% END %]
      </td>
      <td class="[% highlight_class || 'normal' %]" align="center">
          [% ( result_sig_figs || param.result ) | html # to 3 sig figs if is_numerical() %]
      </td>
      <td align="center">
        [% IF param.lower_limit; 10 * param.lower_limit / 10; END %] -
        [% IF param.upper_limit; 10 * param.upper_limit / 10; END %]
      </td>
      <td>
        [% param.units.replace('10/9', '10<span class="superscript">9</span>') %]
      </td>
      <td style="border: none">
        [% UNLESS skip_url %]
        <a href="[% app_url %]/outreach/data_table/[% var %]/[% patient_id %]"
            target="_new"><img src="/images/table-icon.png" />
        [% END %]
      </td>
    </tr>
  [% END %]
  
  [% PROCESS site/popup.tt height=280, width=420, top=250 %]

	<table class="indent borderless">
    <tr>
      <td valign="top">
        <table class="outreachResults">
          <tr>
            <th scope="col">Param</th>
            <th scope="col">Result</th>
            <th scope="col">Range</th>
            <th scope="col">Units</th>
          </tr>          
          [%
            INCLUDE rows param = outreach.haematology.hb;
            INCLUDE rows param = outreach.haematology.wbc;
            INCLUDE rows param = outreach.haematology.plts;

			IF is_cml;
			  INCLUDE rows param = outreach.haematology.neutrophils;
			  INCLUDE rows param = outreach.biochemistry.bilirubin;
			  INCLUDE rows param = outreach.biochemistry.alt;
			ELSE;
			  INCLUDE rows param = outreach.haematology.lymphs;
			  INCLUDE rows param = outreach.calculated.total_b_cells;
			  INCLUDE rows param = outreach.calculated.neoplastic_b_cells;
			  INCLUDE rows param = outreach.flow_cytometry.cd19_neg_pc;
			END;
          %]
        </table>
      </td>
      
      <td width="10"></td>
      
      <td valign="top">
        <table class="outreachResults">
          <tr>
            <th scope="col">Param</th>
            <th scope="col">Result</th>
            <th scope="col">Range</th>
            <th scope="col">Units</th>
          </tr>
          [%
            INCLUDE rows param = outreach.biochemistry.creatinine;
            INCLUDE rows param = outreach.biochemistry.calcium;

			IF is_cml;
			  INCLUDE rows param = outreach.biochemistry.sodium;
			  INCLUDE rows param = outreach.biochemistry.potassium;
			  INCLUDE rows param = outreach.biochemistry.magnesium;
			ELSE;
			  INCLUDE rows param = outreach.immunology.IgA;
			  INCLUDE rows param = outreach.immunology.IgG;
			  INCLUDE rows param = outreach.immunology.IgM;
			  INCLUDE rows param = outreach.immunology.electrophoresis skip_url = 1;
			  INCLUDE rows param = outreach.immunology.paraprotein;
			END;
          %]
		  [%
			IF result_summaries.item('Molecular');
			  result = result_summaries.item('Molecular').results_summary;
			  ratio = result.match('BCR-ABL : ABL ratio = (.*)%');
		  %]
			<tr>
			  <td align="right">BCR-ABL : ABL</td>
			  <td align="center">[% ratio.0 # match capture is array %]</td>
			  <td></td>
			  <td align="center">%</td>
			  <td style="border: none"></td>
			</tr>
		  [% END %]
        </table>
      </td>
    </tr>

  </table>
  
  <p class="spacer"></p>
<!-- END outreach/data_summary.tt -->
