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


$error_message;
$valid = true;

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

/* 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.wko_ls8_empl_id,		-- The Selected Employee ID 
					wko_ls8.wko_ls8_craft,			-- The Craft Code selected from (#101)
					wko_ls8.wko_ls8_hours_type,		-- The Hours Type Code selected from (#201)
					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.wko_ls8_datetime1,		-- Start Date
					wko_ls8.wko_ls8_datetime2,		-- End Date
					wko_ls8.audit_user,				-- The Login User ID
					wko_ls8.audit_date,				-- GETDATE()
					wko_ls8.RowID,
					wko_mst_wo_no
	
		FROM 	wko_mst (NOLOCK)     			
	
				INNER JOIN wko_det (NOLOCK)
				ON		wko_mst.site_cd = wko_det.site_cd
				AND		wko_mst.rowid = wko_det.mst_rowid
				AND 	wko_mst_type  IN ('C', 'P')
				
				
				INNER JOIN wko_ls8 (NOLOCK)
				ON		wko_mst.site_cd = wko_ls8.site_cd
				AND		wko_mst.rowid = wko_ls8.mst_rowid

				INNER JOIN	wrk_sts (NOLOCK)
				ON			wko_mst.site_cd = wrk_sts.site_cd
				AND			wko_mst.wko_mst_status = wrk_sts.wrk_sts_status				
		
		WHERE	wko_ls8.site_cd = '".$site_cd."'
		AND 	wko_det.wko_det_assign_to LIKE '".$wko_det_assign_to."' 
		AND 	(wrk_sts_typ_cd NOT IN ('CLOSE', 'COMPLETE', 'FORCE-CLOSE', 'CANCEL'))
		
		
		UNION ALL
		
		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.wko_ls8_empl_id,		-- The Selected Employee ID 
					wko_ls8.wko_ls8_craft,			-- The Craft Code selected from (#101)
					wko_ls8.wko_ls8_hours_type,		-- The Hours Type Code selected from (#201)
					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.wko_ls8_datetime1,		-- Start Date
					wko_ls8.wko_ls8_datetime2,		-- End Date
					wko_ls8.audit_user,				-- The Login User ID
					wko_ls8.audit_date,				-- GETDATE()
					wko_ls8.RowID,
					wko_mst_wo_no
	
		FROM 	wko_mst (NOLOCK)     			
	
				INNER JOIN wko_det (NOLOCK)
				ON		wko_mst.site_cd = wko_det.site_cd
				AND		wko_mst.rowid = wko_det.mst_rowid
				AND 	wko_mst_type  IN ('G')
				
				
				INNER JOIN wko_ls8 (NOLOCK)
				ON		wko_mst.site_cd = wko_ls8.site_cd
				AND		wko_mst.rowid = wko_ls8.mst_rowid	
				
				INNER JOIN	wrk_sts (NOLOCK)
				ON			wko_mst.site_cd = wrk_sts.site_cd
				AND			wko_mst.wko_mst_status = wrk_sts.wrk_sts_status				
		
		WHERE	wko_ls8.site_cd = '".$site_cd."'
		AND 	wko_det.wko_det_assign_to LIKE '".$wko_det_assign_to."' 
		AND 	(wrk_sts_typ_cd NOT IN ('CLOSE', 'COMPLETE', 'FORCE-CLOSE', 'CANCEL')) ";	
				
					
						
	$stmt = sqlsrv_query( $conn, $sql);

			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();
}

 
?>