<?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 	pur_mst.site_cd,   
					pur_mst.pur_mst_porqnnum,   
					pur_mst.pur_mst_status,   
					pur_mst.pur_mst_rqn_date,   
					pur_mst.pur_mst_requested_by,   
					pur_mst.pur_mst_dept,   
					pur_mst.pur_mst_req_date,    
					pur_mst.pur_mst_chg_costcenter,   
					pur_mst.pur_mst_chg_account,   
					pur_mst.pur_mst_crd_costcenter,   
					pur_mst.pur_mst_crd_account,   
					pur_mst.pur_mst_op_flag,   
					pur_mst.pur_mst_entered_by,
					pur_mst.pur_mst_tot_cost,
					pur_mst.pur_mst_purq_approve,   
					pur_mst.pur_mst_notes,   
					pur_mst.pur_mst_app_route_id,   
					pur_mst.pur_mst_projectid,    
					pur_mst.pur_mst_buyer,    
					pur_mst.pur_mst_app_level,    
					pur_mst.pur_mst_cur_app_level,
					pur_mst.pur_mst_approver,
					pur_mst.pur_mst_create_by,
					pur_mst.pur_mst_create_date,
					pur_mst.pur_mst_release_for_app,
					Approval_Status = CONVERT(VARCHAR(3), pur_mst_cur_app_level) + ' / ' + CONVERT(VARCHAR(3), pur_mst_app_level ),
					pur_mst.audit_user,   
					pur_mst.audit_date,   
					pur_mst.column1,   
					pur_mst.column2,   
					pur_mst.column3,   
					pur_mst.column4,   
					pur_mst.column5,   
					pur_mst.RowID  
		FROM 		pur_mst (NOLOCK)
		
		
					WHERE 		pur_mst.site_cd = '".$site_cd."'
					and			pur_mst.RowID = '".$RowID."'";




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

		if( !$stmt_header ) {
			
			 $error_message = "Error selecting pur_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	pur_ls1.site_cd,
						mst_RowID,
						pur_ls1_pr_lineno,
						pur_ls1_wo_no,
						pur_ls1_mr_no,
						pur_ls1_mr_lineno, 
						pur_ls1_quo_no, 
						pur_ls1_contract_id, 
						pur_ls1_contract_lineno, 
						pur_ls1_po_no,
						pur_ls1_po_lineno, 
						pur_ls1_op_refno,
						pur_ls1_item_category,
						pur_ls1_stockno,
						pur_ls1_stk_locn,
						pur_ls1_desc,
						pur_ls1_ord_uom,
						pur_ls1_ord_qty,
						pur_ls1_supplier,
						supplier_name = sup_mst_desc,
						pur_ls1_rec_supplier,
						pur_ls1_item_cost,
						pur_ls1_last_item_cost,
						pur_ls1_taxable,
						pur_ls1_tax_code,
						pur_ls1_chg_costcenter,
						pur_ls1_chg_account,
						pur_ls1_activity_code,
						pur_ls1_projectid,
						pur_ls1_rcv_in_full,
						pur_ls1_cur_code,
						pur_ls1_cur_exchange_rate,
						pur_ls1_retail_price,
						pur_ls1_tax_rate,
						pur_ls1_tax_value,
						pur_ls1_varchar1,
						pur_ls1_varchar2,
						pur_ls1_varchar3,
						pur_ls1_varchar4,
						pur_ls1_varchar5,
						pur_ls1_numeric1,
						pur_ls1_numeric2,
						pur_ls1_numeric3,
						pur_ls1_numeric4,
						pur_ls1_numeric5,
						pur_ls1_datetime1,
						pur_ls1_datetime2,
						pur_ls1_datetime3,
						pur_ls1_datetime4,
						pur_ls1_datetime5,
						pur_ls1.audit_user,
						pur_ls1.audit_date,
						pur_ls1.column1,
						pur_ls1.column2,
						pur_ls1.column3,
						pur_ls1.column4,
						pur_ls1.column5,
						
						pur_ls1.RowID
			FROM 		pur_ls1 (NOLOCK)
			
			LEFT 
			OUTER 
			JOIN		sup_mst (NOLOCK)
			ON			pur_ls1.site_cd = sup_mst.site_cd
			AND			pur_ls1.pur_ls1_supplier = sup_mst.sup_mst_supplier_cd
			
			WHERE 		pur_ls1.site_cd = '".$site_cd."'
			and			pur_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);
		
}


 
?>