<?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_ls4.site_cd,   
				wko_ls4.wko_ls4_assetno,   
				wko_ls4.wko_ls4_activity_code,   
				wko_ls4.wko_ls4_supplier,   
				wko_ls4.wko_ls4_contract_id,   
				wko_ls4.wko_ls4_po_no,    
				wko_ls4.wko_ls4_po_lineno,   
				wko_ls4.wko_ls4_cmpl_date,    
				wko_ls4.wko_ls4_est_cost,    
				wko_ls4.wko_ls4_act_cost,   
				wko_ls4.wko_ls4_descr,   
				wko_ls4.wko_ls4_qty_needed,   
				wko_ls4.wko_ls4_svc_uom,   
				wko_ls4.wko_ls4_pr_no,   
				wko_ls4.wko_ls4_pr_lineno,   
				wko_ls4.wko_ls4_nonrefpono,   
				wko_ls4.wko_ls4_com_code,   
				wko_ls4.wko_ls4_taxable,    
				wko_ls4.wko_ls4_chg_costcenter,   
				wko_ls4.wko_ls4_chg_account,
				wko_ls4.wko_ls4_tax_cd,   
				wko_ls4.wko_ls4_tax_rate,   
				wko_ls4.wko_ls4_tax_value,   
				pur_mst.pur_mst_purq_approve,
				pur_ls1.pur_ls1_po_no,
				pur_ls1.pur_ls1_po_lineno,
				wko_ls4.audit_user,   
				wko_ls4.audit_date,  				  
				wko_ls4.mst_RowID,   
				wko_ls4.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	wrk_sts (NOLOCK)
				ON			wko_mst.site_cd = wrk_sts.site_cd
				AND			wko_mst.wko_mst_status = wrk_sts.wrk_sts_status
				
				INNER JOIN	wko_ls4 (NOLOCK)
				ON			wko_mst.site_cd = wko_ls4.site_cd
				AND			wko_mst.RowID = wko_ls4.mst_RowID
				
				LEFT OUTER JOIN pur_mst
				ON			pur_mst.site_cd = wko_ls4.site_cd
				AND			pur_mst.pur_mst_porqnnum =  wko_ls4.wko_ls4_pr_no


				LEFT OUTER JOIN pur_ls1
				ON			pur_mst.site_cd = pur_ls1.site_cd		
				AND			pur_mst.RowID =  pur_ls1.mst_RowID
				AND			pur_ls1.pur_ls1_pr_lineno =  wko_ls4.wko_ls4_pr_lineno
											
												
							

		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_ls4.site_cd,   
			wko_ls4.wko_ls4_assetno,   
			wko_ls4.wko_ls4_activity_code,   
			wko_ls4.wko_ls4_supplier,   
			wko_ls4.wko_ls4_contract_id,   
			wko_ls4.wko_ls4_po_no,    
			wko_ls4.wko_ls4_po_lineno,   
			wko_ls4.wko_ls4_cmpl_date,    
			wko_ls4.wko_ls4_est_cost,    
			wko_ls4.wko_ls4_act_cost,   
			wko_ls4.wko_ls4_descr,   
			wko_ls4.wko_ls4_qty_needed,   
			wko_ls4.wko_ls4_svc_uom,   
			wko_ls4.wko_ls4_pr_no,   
			wko_ls4.wko_ls4_pr_lineno,   
			wko_ls4.wko_ls4_nonrefpono,   
			wko_ls4.wko_ls4_com_code,   
			wko_ls4.wko_ls4_taxable,    
			wko_ls4.wko_ls4_chg_costcenter,   
			wko_ls4.wko_ls4_chg_account,
			wko_ls4.wko_ls4_tax_cd,   
			wko_ls4.wko_ls4_tax_rate,   
			wko_ls4.wko_ls4_tax_value,   
			pur_mst.pur_mst_purq_approve,
			pur_ls1.pur_ls1_po_no,
			pur_ls1.pur_ls1_po_lineno,
			wko_ls4.audit_user,   
			wko_ls4.audit_date,  				  
			wko_ls4.mst_RowID,   
			wko_ls4.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	wrk_sts (NOLOCK)
					ON			wko_mst.site_cd = wrk_sts.site_cd
					AND			wko_mst.wko_mst_status = wrk_sts.wrk_sts_status
					
					INNER JOIN	wko_ls4 (NOLOCK)
					ON			wko_mst.site_cd = wko_ls4.site_cd
					AND			wko_mst.RowID = wko_ls4.mst_RowID
					
					LEFT OUTER JOIN pur_mst
					ON			pur_mst.site_cd = wko_ls4.site_cd
					AND			pur_mst.pur_mst_porqnnum =  wko_ls4.wko_ls4_pr_no


					LEFT OUTER JOIN pur_ls1
					ON			pur_mst.site_cd = pur_ls1.site_cd		
					AND			pur_mst.RowID =  pur_ls1.mst_RowID
					AND			pur_ls1.pur_ls1_pr_lineno =  wko_ls4.wko_ls4_pr_lineno
							
							

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

 
?>