<?php
require_once('config.php');


$error_message;
$valid = true;

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

/* Begin the transaction. */
if ( sqlsrv_begin_transaction( $conn ) === false ) {
     die( print_r( sqlsrv_errors(), true ));
}


$sql="SELECT	wko_ls8.site_cd,				-- The login site code
				wko_ls8.mst_RowID,				-- Work Order Row ID
				wko_ls8.wko_ls8_assetno,		-- The Asset Number in the WO Asset No
				wko_ls8_empl_id					= COALESCE(wko_ls8.wko_ls8_empl_id + ' : ' + emp_mst.emp_mst_name,''),	-- The Selected Employee ID 
				wko_ls8_craft					= COALESCE(wko_ls8.wko_ls8_craft + ' : ' + crf_mst.crf_mst_desc,''),		-- The Craft Code selected from 
				wko_ls8_hours_type				= COALESCE(wko_ls8.wko_ls8_hours_type + ' : ' + hours_type.hours_type_desc,''),-- The Hours Type Code selected from
				wko_ls8.wko_ls8_hrs,			-- The Total Hours calculate from wko_ls8_datetime1 (Start Date) To wko_ls8_datetime2 (End Date)
				wko_ls8.wko_ls8_rate,			-- The Rate selected from (#102)
				wko_ls8.wko_ls8_multiplier,		-- The Multiplier selected from (#202)
				wko_ls8.wko_ls8_adder,			-- The Adder Selected from #203
				wko_ls8.wko_ls8_act_cost,		-- Act Cost = (( wko_ls8_hrs * (wko_ls8_rate + wko_ls8_adder) ) * wko_ls8_multiplier )   
				wko_ls8.wko_ls8_chg_costcenter, -- Cost Center in the WO Main Page (wko_mst_chg_costcenter)
				wko_ls8.wko_ls8_chg_account,	-- The Labor Account in the WO Main Page (wko_det_laccount)
				wko_ls8_crd_costcenter			= COALESCE(wko_ls8.wko_ls8_crd_costcenter,''), -- Leave it empty first
				wko_ls8_crd_account				= COALESCE(wko_ls8.wko_ls8_crd_account,''),	-- Leave it empty first
				wko_ls8.wko_ls8_time_card_no,	-- Leave it empty if the End Date did not fill in yet
				wko_ls8_datetime1 				= COALESCE(wko_ls8.wko_ls8_datetime1,''),		-- Start Date
				wko_ls8_datetime2 				= COALESCE(wko_ls8.wko_ls8_datetime2,''),		-- End Date
				wko_ls8.audit_user,				-- The Login User ID
				wko_ls8.audit_date,				-- GETDATE()
				wko_ls8.RowID
	FROM 		wko_ls8 (NOLOCK)
	
	LEFT 
	OUTER 
	JOIN	emp_mst (NOLOCK) 
	ON		wko_ls8.site_cd  =  emp_mst.site_cd
	and     wko_ls8.wko_ls8_empl_id   = emp_mst.emp_mst_empl_id

	LEFT 
	OUTER 
	JOIN	crf_mst (NOLOCK) 
	ON		wko_ls8.site_cd = crf_mst.site_cd  
	and     wko_ls8.wko_ls8_craft  = crf_mst.crf_mst_crf_cd


	LEFT 
	OUTER 
	JOIN	hours_type (NOLOCK) 
	ON		wko_ls8.site_cd = hours_type.site_cd  
	and     wko_ls8.wko_ls8_hours_type = hours_type.hours_type_cd 
	
	WHERE		wko_ls8.site_cd = ?
	AND 		wko_ls8.mst_RowID = ?";	
				
	//echo $sql;
	$params = array($site_cd,$mst_RowID);					
						
	$stmt = sqlsrv_query( $conn, $sql,$params);

			if( !$stmt ) {
				$error_message = "Error SELECT Table (wko_ls8)";
				returnError($error_message);
				die( print_r( sqlsrv_errors(), true));
			}

			$json= array();

			do {
				 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {	
				 
					$json[] = $row;		
				
				 }
			} while ( sqlsrv_next_result($stmt) );


if( $stmt) {
	 sqlsrv_commit( $conn );
	 sqlsrv_close( $conn);	
	 returnData($json);		
} else {
	 sqlsrv_rollback( $conn );
	 $error_message = "Transaction rolled back.<br />";
	 returnError($error_message);
}

function returnData($json){
	
	 $json1 = json_encode($json);
	  
/* if(empty(json_decode($json1,1))) {
     $returnData = array(
	'status' => 'Alert',
	'message' => 'No Records found',
	'data' => $json);
}else{
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Successfully',	
	'data' => $json);
} */
	
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Successfully',	
	'data' => $json);

	echo json_encode($returnData);
}

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

 
?>