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

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

 
 $sql="Select 
				wko_ls2.site_cd,   
				wko_ls2.wko_ls2_assetno,
				wko_ls2.wko_ls2_activity_code,   
				wko_ls2.wko_ls2_stockno,   
				wko_ls2_qty_needed				= COALESCE(wko_ls2.wko_ls2_qty_needed,0),    
				wko_ls2.wko_ls2_desc,   
				wko_ls2.wko_ls2_item_cost,   
				wko_ls2.wko_ls2_mr_no, 
				wko_ls2_mr_lineno, 
				mtr_mst.mtr_mst_status ,
				mtr_ls1_rcv_qty 				= COALESCE(mtr_ls1.mtr_ls1_rcv_qty,0),
				wko_ls2.wko_ls2_rec_supplier,   
				wko_ls2.wko_ls2_act_qty,   
				wko_ls2.wko_ls2_act_cost,   
				wko_ls2.wko_ls2_mtl_uom,  				
				wko_ls2.wko_ls2_taxable,   
				wko_ls2.wko_ls2_chg_costcenter,   
				wko_ls2.wko_ls2_chg_account,   
				wko_ls2.wko_ls2_stk_locn,			
				wko_ls2.audit_user,   
				wko_ls2.audit_date,  				
				wko_ls2.mst_RowID,				
				TotalAvailable = ( 	SELECT 	itm_mst_ttl_oh 
									FROM 	itm_mst 
									WHERE 	itm_mst.site_cd = wko_ls2.site_cd 
									AND 	itm_mst.itm_mst_stockno = wko_ls2.wko_ls2_stockno ) ,
				mtr_ls1_clear_qty	= COALESE(mtr_ls1.mtr_ls1_clear_qty,0), 
				wko_ls2.RowID

							
									
			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_ls2 (NOLOCK)
					ON		wko_mst.site_cd = wko_ls2.site_cd
					AND		wko_mst.rowid = wko_ls2.mst_rowid
					
					
					LEFT OUTER JOIN mtr_mst
					ON				mtr_mst.site_cd = wko_ls2.site_cd
					AND			mtr_mst.mtr_mst_mtr_no =  wko_ls2.wko_ls2_mr_no


					LEFT OUTER JOIN mtr_ls1
					ON			mtr_mst.site_cd = mtr_ls1.site_cd
					AND			mtr_mst.RowID =  mtr_ls1.mst_RowID
					AND			mtr_ls1.mtr_ls1_mtr_lineno =  wko_ls2.wko_ls2_mr_lineno	
					
					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_mst.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_ls2.site_cd,   
				wko_ls2.wko_ls2_assetno,
				wko_ls2.wko_ls2_activity_code,   
				wko_ls2.wko_ls2_stockno,   
				wko_ls2.wko_ls2_desc,    
				wko_ls2_qty_needed				= COALESCE(wko_ls2.wko_ls2_qty_needed,0),  
				wko_ls2.wko_ls2_item_cost,   
				wko_ls2.wko_ls2_mr_no, 
				wko_ls2_mr_lineno, 
				mtr_mst.mtr_mst_status ,
				mtr_ls1_rcv_qty 				= COALESCE(mtr_ls1.mtr_ls1_rcv_qty,0),
				wko_ls2.wko_ls2_rec_supplier,   
				wko_ls2.wko_ls2_act_qty,   
				wko_ls2.wko_ls2_act_cost,   
				wko_ls2.wko_ls2_mtl_uom,  				
				wko_ls2.wko_ls2_taxable,   
				wko_ls2.wko_ls2_chg_costcenter,   
				wko_ls2.wko_ls2_chg_account,   
				wko_ls2.wko_ls2_stk_locn,			
				wko_ls2.audit_user,   
				wko_ls2.audit_date,  				
				wko_ls2.mst_RowID,				
				TotalAvailable = ( 	SELECT 	itm_mst_ttl_oh 
									FROM 	itm_mst 
									WHERE 	itm_mst.site_cd = wko_ls2.site_cd 
									AND 	itm_mst.itm_mst_stockno = wko_ls2.wko_ls2_stockno ) ,
				mtr_ls1_clear_qty	= COALESE(mtr_ls1.mtr_ls1_clear_qty,0), 
				wko_ls2.RowID
					
									
			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_ls2 (NOLOCK)
					ON		wko_mst.site_cd = wko_ls2.site_cd
					AND		wko_mst.rowid = wko_ls2.mst_rowid
					
					
					LEFT OUTER JOIN mtr_mst
					ON				mtr_mst.site_cd = wko_ls2.site_cd
					AND			mtr_mst.mtr_mst_mtr_no =  wko_ls2.wko_ls2_mr_no


					LEFT OUTER JOIN mtr_ls1
					ON			mtr_mst.site_cd = mtr_ls1.site_cd
					AND			mtr_mst.RowID =  mtr_ls1.mst_RowID
					AND			mtr_ls1.mtr_ls1_mtr_lineno =  wko_ls2.wko_ls2_mr_lineno	
					
					
					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_mst.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 selecting table (emp_mst)";
	 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 ($valid) {
	
	
	returnData($json);	
}
 
function returnData($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();
}
	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);	

 
?>