<?php

	require_once('../../../../../Config/config.php');
	require_once('../../../../../Config/odbc.php');



	$type 	= '';
	$from 	= '';
	$to 	= '';
  $radiologist = '';
  $modalitylist = '';

	if ( isset ( $_GET['type'] ) && $_GET['type'] != '' 	)		$type 			= $_GET['type'];
	if ( isset ( $_GET['radid'] ) && $_GET['radid'] != '' 	)		$radiologist 	= "and staff_member.id in ({$_GET['radid']})";
	if ( isset ( $_GET['modid'] ) && $_GET['modid'] != '' 	)		$modalitylist 		= "and device.id in({$_GET['modid']})";
	if ( isset ( $_GET['from']) && $_GET['from'] != ''		)		$fromDate 		= $_GET['from'];	else 	$fromDate 	= date("d/m/Y");
	if ( isset ( $_GET['to'] ) && $_GET['to'] != ''			)		$toDate 		= $_GET['to'];  	else 	$toDate 	= date("d/m/Y");
  	if ( isset ( $_GET['dow'] ) && $_GET['dow'] != '' 		)		$daysOfWeek 	= $_GET['dow'];

	// $rangeofdate = ' and study.date::date >= to_date(\''.$from.'\',\'DD/MM/YYYY\')';
	// $rangeofdate .= ' and study.date::date <= to_date(\''.$to.'\',\'DD/MM/YYYY\')';


	//**************************************************************
	// Fill radiologist array
	//

	$radArray = array ();

	$connQUAL 		= get_dbconnection ();
	$requestQUAL 	=  "select lastname || ' ' || firstname, ris_name from staff_member where removed = false and ris_name is not null and ris_name != '-1' and ris_name != '' {$radiologist};";
	$resultQUAL 	= pg_query ( $connQUAL, $requestQUAL );

	if ( $resultQUAL != FALSE )
	{
		$i = 0;
		while ( $rowQUAL = pg_fetch_row ($resultQUAL) )
		{
			$radArray [$i][0] = $rowQUAL [0];		// qual name
			$radArray [$i][1] = $rowQUAL [1];		// ris_name
			$radArray [$i][2] = $rowQUAL [1];		// ris_id

			$i++;
		}
	}
	pg_close ( $connQUAL );


	// **************************************************
	// Fill radiologist ID list in RIS
	//

	$risRadIDList = "";
	for ( $i=0; $i<count($radArray); $i++ )
	{
		if ( $radArray [$i][2] != -1 )
			$risRadIDList = $risRadIDList.$radArray [$i][2].",";
	}
	$risRadIDList = substr ( $risRadIDList, 0, -1 );



	//**************************************************************
	// Fill modalities array
	//

	$modArray = array ();

	$connQUAL = get_dbconnection ();
	$requestQUAL =  "select name, ris_name from device where removed = false and ris_name is not null  and ris_name != '-1' and ris_name != '' {$modalitylist}";
	$resultQUAL = pg_query ( $connQUAL, $requestQUAL );

	if ( $resultQUAL != FALSE )
	{
		$i = 0;
		while ( $rowQUAL = pg_fetch_row ($resultQUAL) )
		{
			$modArray [$i][0] = $rowQUAL [0];
			$modArray [$i][1] = $rowQUAL [1];
			$modArray [$i][2] = $rowQUAL [1];		// ris_id

			$i++;
		}
	}
	pg_close ( $connQUAL );


	// **************************************************
	// Fill modalities ID list in RIS
	//

	$risModIDList = "";
	for ( $i=0; $i<count($modArray); $i++ )
	{
		if ( $modArray [$i][2] != -1 )
			$risModIDList = $risModIDList.$modArray [$i][2].",";
	}
	$risModIDList = substr ( $risModIDList, 0, -1 );




	// *********************************************************************************************************************
	// GET DATA FROM RIS
	//

	$ToSend = array();

	$conn = get_dbconnectionRIS ();

	if ( $conn )
	{
		$request = '';

		switch ( $type )
		{
			case 'nstudybyradiologist':
				/*
				$request = "select rad_id, 'Nb examens', count(stu_id) from (select stu_id,rad_id,stationname,scheddate from study,visit where mod_id in (".$risModIDList.") and visit.removed=false and study.removed=false and study.vis_id=visit.vis_id and scheddate>='".$fromDate."' and scheddate<='".$toDate."' and extract( isodow from to_date(scheddate,'YYYYMMDD')) in (".$daysOfWeek.")) as t left join xuser on t.rad_id=xuser.use_id where xuser.use_id in (".$risRadIDList.") group by rad_id";
				*/
				$request = "select rad_id, 'Nb examens', count(vis_id) from (select visit.vis_id,rad_id,mod_id,scheddate from study,visit where mod_id in (".$risModIDList.") and visit.removed=false and study.removed=false and study.vis_id=visit.vis_id and scheddate>='".$fromDate."' and scheddate<='".$toDate."' and extract( isodow from to_date(scheddate,'YYYYMMDD')) in (".$daysOfWeek.") group by visit.vis_id,mod_id,scheddate) as t left join xuser on t.rad_id=xuser.use_id where xuser.use_id in (".$risRadIDList.") group by rad_id";
				break;

			case 'nstudybymodality':
				/*
				$request = "select mod_id,'Nb examens',count(stu_id) from (select stu_id,rad_id,stationname,scheddate,mod_id from study,visit where study.vis_id=visit.vis_id and mod_id in (".$risModIDList.") and visit.removed=false and study.removed=false and scheddate>='".$fromDate."' and scheddate<='".$toDate."' and extract( isodow from to_date(scheddate,'YYYYMMDD')) in (".$daysOfWeek.")) as t left join xuser on t.rad_id=xuser.use_id where xuser.use_id in (".$risRadIDList.") group by mod_id";
				*/
				$request = "select mod_id,'Nb examens',count(*) from (select mod_id,rad_id from study,visit where study.vis_id=visit.vis_id and mod_id in (".$risModIDList.") and visit.removed=false and study.removed=false and scheddate>='".$fromDate."' and scheddate<='".$toDate."' and extract( isodow from to_date(scheddate,'YYYYMMDD')) in (".$daysOfWeek.") group by visit.vis_id,mod_id) as t left join xuser on t.rad_id=xuser.use_id where xuser.use_id in (".$risRadIDList.") group by mod_id";
				break;

			case 'nstudybymodalitybyday':
				/*
				$request = "select to_char(to_date(dte,'YYYYMMDD'), 'YYYY-MM-DD'), t2.mod_id, count(stu_id) from ((select dte,mod_id from (select TO_CHAR(tm, 'YYYYMMDD') as dte from (SELECT generate_series(TO_DATE('".$fromDate."','YYYYMMDD'),TO_DATE('".$toDate."','YYYYMMDD'),'24 hours') as tm ) t where extract(isodow from tm ) in (".$daysOfWeek.")) t,xmodality where mod_id in(".$risModIDList.") order by dte,mod_id ) t1 left join (select stu_id,mod_id,TO_DATE(scheddate,'YYYYMMDD') as studyDate from visit,study where rad_id in (".$risRadIDList.") and visit.removed=false and study.removed=false and scheddate>='".$fromDate."' and scheddate <='".$toDate."' and study.vis_id=visit.vis_id) t2 on to_char(t2.studyDate,'YYYYMMDD')=dte and t2.mod_id=t1.mod_id) group by dte,t2.mod_id order by dte,t2.mod_id";
				*/
				$request = "select to_char(to_date(dte,'YYYYMMDD'), 'YYYY-MM-DD'), t2.mod_id, count(vis_id) from ((select dte,mod_id from (select TO_CHAR(tm, 'YYYYMMDD') as dte from (SELECT generate_series(TO_DATE('".$fromDate."','YYYYMMDD'),TO_DATE('".$toDate."','YYYYMMDD'),'24 hours') as tm ) t where extract(isodow from tm ) in (".$daysOfWeek.")) t,xmodality where mod_id in(".$risModIDList.") order by dte,mod_id ) t1 left join (select visit.vis_id,mod_id,TO_DATE(scheddate,'YYYYMMDD') as studyDate from visit,study where rad_id in (".$risRadIDList.") and visit.removed=false and study.removed=false and scheddate>='".$fromDate."' and scheddate <='".$toDate."' and study.vis_id=visit.vis_id group by visit.vis_id,mod_id,scheddate) t2 on to_char(t2.studyDate,'YYYYMMDD')=dte and t2.mod_id=t1.mod_id) where t2.mod_id is not null group by dte,t2.mod_id order by dte,t2.mod_id";
				break;

			case 'nstudybyradiologistbyday':
				/*
				$request = "select to_char(to_date(dte,'YYYYMMDD'), 'YYYY-MM-DD'),use_id,count(stu_id) from ((select dte,use_id from (select TO_CHAR(tm, 'YYYYMMDD') as dte from (SELECT generate_series(TO_DATE('".$fromDate."','YYYYMMDD'),TO_DATE('".$toDate."','YYYYMMDD'),'24 hours') as tm ) t where extract(isodow from tm ) in (".$daysOfWeek.")) t,xuser where use_id in (".$risRadIDList.") order by dte,use_id ) t1 left join (select stu_id,rad_id,TO_DATE(scheddate,'YYYYMMDD') as studyDate from visit,study where mod_id in (".$risModIDList.") and visit.removed=false and visit.removed=false and scheddate>='".$fromDate."' and scheddate <='".$toDate."' and study.vis_id=visit.vis_id) t2 on to_char(t2.studyDate,'YYYYMMDD')=dte and t2.rad_id=use_id) group by dte,use_id order by dte,use_id";
				*/
				$request = "select to_char(to_date(dte,'YYYYMMDD'), 'YYYY-MM-DD'),use_id,count(vis_id) from ((select dte,use_id from (select TO_CHAR(tm, 'YYYYMMDD') as dte from (SELECT generate_series(TO_DATE('".$fromDate."','YYYYMMDD'),TO_DATE('".$toDate."','YYYYMMDD'),'24 hours') as tm ) t where extract(isodow from tm ) in (".$daysOfWeek.")) t,xuser where use_id in (".$risRadIDList.") order by dte,use_id ) t1 left join (select visit.vis_id,rad_id,TO_DATE(scheddate,'YYYYMMDD') as studyDate from visit,study where mod_id in (".$risModIDList.") and visit.removed=false and visit.removed=false and scheddate>='".$fromDate."' and scheddate <='".$toDate."' and study.vis_id=visit.vis_id group by visit.vis_id,mod_id,scheddate) t2 on to_char(t2.studyDate,'YYYYMMDD')=dte and t2.rad_id=use_id) group by dte,use_id order by dte,use_id";
				break;

			case 'nstudybyday':

				$request = "select to_char(to_date(dte,'YYYYMMDD'), 'YYYY-MM-DD'), 'Nb examens', count(vis_id) from ((select dte,mod_id from (select TO_CHAR(tm, 'YYYYMMDD') as dte from (SELECT generate_series(TO_DATE('".$fromDate."','YYYYMMDD'),TO_DATE('".$toDate."','YYYYMMDD'),'24 hours') as tm ) t where extract(isodow from tm ) in (".$daysOfWeek.")) t,xmodality where mod_id in(".$risModIDList.") order by dte,mod_id ) t1 left join (select visit.vis_id,mod_id,TO_DATE(scheddate,'YYYYMMDD') as studyDate from visit,study where rad_id in (".$risRadIDList.") and visit.removed=false and study.removed=false and scheddate>='".$fromDate."' and scheddate <='".$toDate."' and study.vis_id=visit.vis_id group by visit.vis_id,mod_id,scheddate) t2 on to_char(t2.studyDate,'YYYYMMDD')=dte and t2.mod_id=t1.mod_id) group by dte order by dte";
				break;

			case 'delay':
				$request = "select TO_DATE(scheddate,'YYYYMMDD'), 'Delai (h)', round( (EXTRACT( epoch from avg(validated-scheduled))/3600)::numeric, 2) from StatTiming where validatedby<>-1 and stu_id in (select stu_id from study,visit,xuser where study.vis_id=visit.vis_id and rad_id=xuser.use_id and xuser.use_id in (".$risRadIDList.")  and visit.removed=false and study.removed=false and mod_id in (".$risModIDList.") and scheddate>='".$fromDate."' and scheddate<='".$toDate."' order by scheddate) group by scheddate order by scheddate";
				break;

			default:
				break;
		}


		if ( $request != '' )
		{
			if ($result = pg_query ( $conn, $request ) )
			{
				while ( $row = pg_fetch_row ($result) )
				{
					$ident0 = "";
					$ident1 = "";
					$ident2 = "";

					switch ( $type )
					{
						case 'nstudybyradiologist':
							for ( $i=0; $i<count($radArray); $i++ )
							{
								if ( $radArray[$i][2] == $row [0] )
									$ident0 = $radArray[$i][0];
							}
							$ident1	= $row [1];
							$ident2	= $row [2];
							break;

						case 'nstudybymodality':
							for ( $i=0; $i<count($modArray); $i++ )
							{
								if ( $modArray[$i][2] == $row [0] )
									$ident0 = $modArray[$i][0];
							}
							$ident1	= $row [1];
							$ident2	= $row [2];
							break;

						case 'nstudybymodalitybyday':
							for ( $i=0; $i<count($modArray); $i++ )
							{
								if ( $modArray[$i][2] == $row [1] )
									$ident1 = $modArray[$i][0];
							}
							$ident0	= $row [0];
							$ident2	= $row [2];
							break;

						case 'nstudybyradiologistbyday':
							for ( $i=0; $i<count($radArray); $i++ )
							{
								if ( $radArray[$i][2] == $row [1] )
									$ident1 = $radArray[$i][0];
							}
							$ident0	= $row [0];
							$ident2	= $row [2];
							break;
						case 'delay':
						case 'nstudybyday':
							$ident0	= $row [0];
							$ident1	= $row [1];
							$ident2	= $row [2];
							break;
					}



					if ( !array_key_exists ( $ident0, $ToSend ) )
						$ToSend[$ident0] = array();

					// $dataset["label"] 		= $row[1];
					// $dataset["value"] 		= $row[2];
					$dataset["label"] 		= $ident1;
					$dataset["value"] 		= $ident2;

					$ToSend [ $ident0 ][] 	= $dataset;
				}
			}
		}
	}

	header('Content-Type: application/json');

	echo json_encode ( $ToSend );

?>
