<?php
// get these values from your DB.
require_once('config.php');
$error_message;
$valid = true;

$site_cd = $_REQUEST['site_cd'];
$emp_id = $_REQUEST['emp_id'];

//Version
$sql="Select Total = SUM(Total), DAY_NAME, DATEE, MONTHH FROM (
		SELECT	Total = Count(*), 
				DAY_NAME = Substring(DATENAME(WEEKDAY, wko_mst_org_date),1,3), 
				DATEE = CASE WHEN DAY(CAST(wko_mst_org_date as date)) < 10 Then '0' + Cast(DAY(CAST(wko_mst_org_date as date)) as varchar) Else Cast( DAY(CAST(wko_mst_org_date as date)) as varchar)End,
				MONTHH = CASE WHEN MONTH(CAST(wko_mst_org_date as date)) < 10 Then '0' + Cast(MONTH(CAST(wko_mst_org_date as date)) as varchar) Else Cast( MONTH(CAST(wko_mst_org_date as date)) as varchar)End
		FROM 	wko_mst (NOLOCK)
		WHERE	wko_mst_org_date >= DATEADD(dd, DATEDIFF(dd, 6, getdate()), 0)
		AND		wko_mst_org_date < DATEADD(dd, DATEDIFF(dd, -1, getdate()), 0)
		AND		wko_mst.site_cd = '".$site_cd ."'
		GROUP BY Substring(DATENAME(WEEKDAY, wko_mst_org_date),1,3), DAY(CAST(wko_mst_org_date as date)), MONTH(CAST(wko_mst_org_date as date))
		UNION 
		Select 0, Substring(DATENAME(WEEKDAY, DATEADD(dd, DATEDIFF(dd, 6, getdate()), 0)),1,3), 
		CASE WHEN DAY(DATEADD(dd, DATEDIFF(dd, 6, getdate()), 0)) < 10 Then '0'+ CAST(DAY(DATEADD(dd, DATEDIFF(dd, 6, getdate()), 0)) as varchar) Else Cast(DAY(DATEADD(dd, DATEDIFF(dd, 6, getdate()), 0)) as varchar) End , 
		CASE WHEN MONTH(DATEADD(dd, DATEDIFF(dd, 6, getdate()), 0)) < 10 Then '0'+ CAST(MONTH(DATEADD(dd, DATEDIFF(dd, 6, getdate()), 0)) as varchar) Else Cast(MONTH(DATEADD(dd, DATEDIFF(dd, 6, getdate()), 0)) as varchar) End
		UNION 
		Select 0, Substring(DATENAME(WEEKDAY, DATEADD(dd, DATEDIFF(dd, 5, getdate()), 0)),1,3), 
		CASE WHEN DAY(DATEADD(dd, DATEDIFF(dd, 5, getdate()), 0)) < 10 Then '0'+ CAST(DAY(DATEADD(dd, DATEDIFF(dd, 5, getdate()), 0)) as varchar) Else Cast(DAY(DATEADD(dd, DATEDIFF(dd, 5, getdate()), 0)) as varchar) End , 
		CASE WHEN MONTH(DATEADD(dd, DATEDIFF(dd, 5, getdate()), 0)) < 10 Then '0'+ CAST(MONTH(DATEADD(dd, DATEDIFF(dd, 5, getdate()), 0)) as varchar) Else Cast(MONTH(DATEADD(dd, DATEDIFF(dd, 5, getdate()), 0)) as varchar) End
		UNION 
		Select 0, Substring(DATENAME(WEEKDAY, DATEADD(dd, DATEDIFF(dd, 4, getdate()), 0)),1,3), 
		CASE WHEN DAY(DATEADD(dd, DATEDIFF(dd, 4, getdate()), 0)) < 10 Then '0'+ CAST(DAY(DATEADD(dd, DATEDIFF(dd, 4, getdate()), 0)) as varchar) Else Cast(DAY(DATEADD(dd, DATEDIFF(dd, 4, getdate()), 0)) as varchar) End , 
		CASE WHEN MONTH(DATEADD(dd, DATEDIFF(dd, 4, getdate()), 0)) < 10 Then '0'+ CAST(MONTH(DATEADD(dd, DATEDIFF(dd, 4, getdate()), 0)) as varchar) Else Cast(MONTH(DATEADD(dd, DATEDIFF(dd, 4, getdate()), 0)) as varchar) End
		UNION 
		Select 0, Substring(DATENAME(WEEKDAY, DATEADD(dd, DATEDIFF(dd, 3, getdate()), 0)),1,3), 
		CASE WHEN DAY(DATEADD(dd, DATEDIFF(dd, 3, getdate()), 0)) < 10 Then '0'+ CAST(DAY(DATEADD(dd, DATEDIFF(dd, 3, getdate()), 0)) as varchar) Else Cast(DAY(DATEADD(dd, DATEDIFF(dd, 3, getdate()), 0)) as varchar) End , 
		CASE WHEN MONTH(DATEADD(dd, DATEDIFF(dd, 3, getdate()), 0)) < 10 Then '0'+ CAST(MONTH(DATEADD(dd, DATEDIFF(dd, 3, getdate()), 0)) as varchar) Else Cast(MONTH(DATEADD(dd, DATEDIFF(dd, 3, getdate()), 0)) as varchar) End
		UNION 
		Select 0, Substring(DATENAME(WEEKDAY, DATEADD(dd, DATEDIFF(dd, 2, getdate()), 0)),1,3), 
		CASE WHEN DAY(DATEADD(dd, DATEDIFF(dd, 2, getdate()), 0)) < 10 Then '0'+ CAST(DAY(DATEADD(dd, DATEDIFF(dd, 2, getdate()), 0)) as varchar) Else Cast(DAY(DATEADD(dd, DATEDIFF(dd, 2, getdate()), 0)) as varchar) End , 
		CASE WHEN MONTH(DATEADD(dd, DATEDIFF(dd, 2, getdate()), 0)) < 10 Then '0'+ CAST(MONTH(DATEADD(dd, DATEDIFF(dd, 2, getdate()), 0)) as varchar) Else Cast(MONTH(DATEADD(dd, DATEDIFF(dd, 2, getdate()), 0)) as varchar) End
		UNION 
		Select 0, Substring(DATENAME(WEEKDAY, DATEADD(dd, DATEDIFF(dd, 1, getdate()), 0)),1,3), 
		CASE WHEN DAY(DATEADD(dd, DATEDIFF(dd, 1, getdate()), 0)) < 10 Then '0'+ CAST(DAY(DATEADD(dd, DATEDIFF(dd, 1, getdate()), 0)) as varchar) Else Cast(DAY(DATEADD(dd, DATEDIFF(dd, 1, getdate()), 0)) as varchar) End , 
		CASE WHEN MONTH(DATEADD(dd, DATEDIFF(dd, 1, getdate()), 0)) < 10 Then '0'+ CAST(MONTH(DATEADD(dd, DATEDIFF(dd, 1, getdate()), 0)) as varchar) Else Cast(MONTH(DATEADD(dd, DATEDIFF(dd, 1, getdate()), 0)) as varchar) End
		UNION 
		Select 0, Substring(DATENAME(WEEKDAY, DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)),1,3), 
		CASE WHEN DAY(DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)) < 10 Then '0'+ CAST(DAY(DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)) as varchar) Else Cast(DAY(DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)) as varchar) End , 
		CASE WHEN MONTH(DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)) < 10 Then '0'+ CAST(MONTH(DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)) as varchar) Else Cast(MONTH(DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)) as varchar) End) as X
		GROUP BY DAY_NAME, DATEE, MONTHH
		ORDER BY MONTHH, DATEE";
		
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting current week report (wko_mst)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}
$json =array();
do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {	
	 
		//$Total = $row ['Total'];	
		//$DAY_NAME = $row ['DAY_NAME'];
		 $json[]= $row;
	
     }
} while ( sqlsrv_next_result($stmt) );


if ($valid) {

	
	//echo $sql;
	returnData($json);	
	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);	
	
}

function returnData($json){
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Successfully',
	'data'=>$json);
	
	echo json_encode($returnData);	
}

function returnError($error_message){
	$json = array();
	
	$returnData = array(
	'status' => 'ERROR',
	'message' => $error_message,
	'data' => '');
	
	echo json_encode($returnData);	
	exit();	
		
}
	

 
?>