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

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

		
	$sql_header ="	SELECT 		
						mtr_mst.site_cd,   
						mtr_mst.mtr_mst_mtr_no,   
						mtr_mst.mtr_mst_assetno,   
						mtr_mst.mtr_mst_costcenter,   
						mtr_mst.mtr_mst_account,   
						mtr_mst.mtr_mst_org_date,   
						mtr_mst.mtr_mst_req_date,   
						mtr_mst.mtr_mst_cmpl_date,   
						mtr_mst.mtr_mst_wo_no,   
						mtr_mst.mtr_mst_requester,    
						mtr_mst.mtr_mst_entered_by,   
						mtr_mst.mtr_mst_status,
						mtr_mst.mtr_mst_mr_status,
						mtr_mst.mtr_mst_email_notification,   
						mtr_mst.mtr_mst_note,  
						mtr_mst.mtr_mst_tot_cost,
						Approval_Status = CONVERT(VARCHAR(3), mtr_mst_cur_app_level) + ' / ' + CONVERT(VARCHAR(3), mtr_mst_app_level ),			
						mtr_mst.mtr_mst_approver,
						mtr_mst.mtr_mst_release_for_app,
						mtr_mst.mtr_mst_email_requestor,
						mtr_mst.audit_user,   
						mtr_mst.audit_date,
						mtr_mst.mtr_mst_create_by,   
						mtr_mst.mtr_mst_create_date,      
						mtr_mst.column1,   
						mtr_mst.column2,   
						mtr_mst.column3,   
						mtr_mst.column4,   
						mtr_mst.column5,   
						mtr_mst.RowID  
			FROM 		mtr_mst (NOLOCK)
			WHERE 		mtr_mst.site_cd = '".$site_cd."'
			and			mtr_mst.RowID = '".$RowID."'";




		$stmt_header = sqlsrv_query( $conn, $sql_header);

		if( !$stmt_header ) {
			
			 $error_message = "Error selecting mtr_mst";
			 returnError($error_message);
			 die( print_r( sqlsrv_errors(), true));
			 
		}

		$json_header= array();

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


		$sql_line ="	SELECT	site_cd ,
				mst_RowID ,
				mtr_ls1_mtr_lineno ,
				mtr_ls1_stockno ,
				mtr_ls1_stk_locn ,
				mtr_ls1_mtl_uom ,
				mtr_ls1_desc ,  
				mtr_ls1_status , 
				mtr_ls1_chg_costcenter ,
				mtr_ls1_chg_account ,
				mtr_ls1_req_qty ,
				mtr_ls1_rcv_qty ,
				mtr_ls1_bo_qty ,
				mtr_ls1_item_cost ,
				mtr_ls1_po_no ,
				mtr_ls1_po_lineno ,
				mtr_ls1_rec_supplier ,
				mtr_ls1_supplier_partno ,
				mtr_ls1_type,
				mtr_ls1.mtr_ls1_varchar1,   
				mtr_ls1.mtr_ls1_varchar2,   
				mtr_ls1.mtr_ls1_varchar3,   
				mtr_ls1.mtr_ls1_varchar4,   
				mtr_ls1.mtr_ls1_varchar5,   
				mtr_ls1.mtr_ls1_varchar6,   
				mtr_ls1.mtr_ls1_varchar7,   
				mtr_ls1.mtr_ls1_varchar8,   
				mtr_ls1.mtr_ls1_varchar9,   
				mtr_ls1.mtr_ls1_varchar10,   
				mtr_ls1.mtr_ls1_numeric1,   
				mtr_ls1.mtr_ls1_numeric2,   
				mtr_ls1.mtr_ls1_numeric3,   
				mtr_ls1.mtr_ls1_numeric4,   
				mtr_ls1.mtr_ls1_numeric5,   
				mtr_ls1.mtr_ls1_numeric6,   
				mtr_ls1.mtr_ls1_numeric7,   
				mtr_ls1.mtr_ls1_numeric8,   
				mtr_ls1.mtr_ls1_numeric9,   
				mtr_ls1.mtr_ls1_numeric10,   
				mtr_ls1.mtr_ls1_datetime1,   
				mtr_ls1.mtr_ls1_datetime2,   
				mtr_ls1.mtr_ls1_datetime3,   
				mtr_ls1.mtr_ls1_datetime4,   
				mtr_ls1.mtr_ls1_datetime5,   
				mtr_ls1.mtr_ls1_datetime6,   
				mtr_ls1.mtr_ls1_datetime7,   
				mtr_ls1.mtr_ls1_datetime8,   
				mtr_ls1.mtr_ls1_datetime9,   
				mtr_ls1.mtr_ls1_datetime10,   
				TotalAvailable = ( 	SELECT	itm_mst_ttl_oh 
									FROM	itm_mst (NOLOCK)
									WHERE	itm_mst.site_cd = mtr_ls1.site_cd 
									AND		itm_mst.itm_mst_stockno = mtr_ls1.mtr_ls1_stockno ),
				mtr_ls1_clear_qty,
				mtr_ls1.audit_user,   
				mtr_ls1.audit_date,   
				mtr_ls1.column1,   
				mtr_ls1.column2,   
				mtr_ls1.column3,   
				mtr_ls1.column4,   
				mtr_ls1.column5,   
				mtr_ls1.RowID  
		FROM	mtr_ls1 (NOLOCK)
		WHERE 		mtr_ls1.site_cd = '".$site_cd."'
		and			mtr_ls1.mst_RowID = '".$RowID."'";




		$stmt_line = sqlsrv_query( $conn, $sql_line);

		if( !$stmt_line ) {
			
			 $error_message = "Error selecting mtr_ls1";
			 returnError($error_message);
			 die( print_r( sqlsrv_errors(), true));
			 
		}

		$json_line= array();

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






if ($valid) {

	returnData($json_header,$json_line);	
	
}


function returnData($json_header,$json_line){
	$returnData = array(
		'status' => 'SUCCESS',
		'message' => 'Successfully',
		'header' => $json_header,		
		'line' => $json_line);

	echo json_encode($returnData);
}

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


 
?>