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

$site_cd = $_REQUEST['site_cd'];



$sql= "SELECT 	sup_mst.sup_mst_supplier_cd,   
				sup_mst.sup_mst_desc,  
				sup_mst.sup_mst_status  
		FROM 	sup_mst (NOLOCK), sup_sts (NOLOCK)
		WHERE (sup_mst.site_cd = sup_sts.site_cd  
		AND			sup_mst.sup_mst_status = sup_sts.sup_sts_status  
		AND			sup_sts.sup_sts_typ_cd NOT IN ('DEACTIVATE')) 
		AND sup_mst.site_cd = '".$site_cd."' ORDER BY sup_mst_supplier_cd ASC";
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (sup_mst)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$supplier = array();

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




$sql= "SELECT 	uom_mst.uom_mst_uom,  uom_mst.uom_mst_desc  FROM uom_mst(NOLOCK)  
		WHERE 	uom_mst.site_cd = '".$site_cd."' 
		AND 	uom_mst_type = 'SERVICE' 
		AND 	uom_mst_disable_flag = '0' ORDER BY uom_mst_uom ASC ";
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (Mydefault values)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$uom = array();

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



$sql= "SELECT 	cf_cost_center.costcenter, cf_cost_center.descs    FROM 	cf_cost_center (NOLOCK)  WHERE (	cf_cost_center.disable_flag = 0  ) AND cf_cost_center.site_cd = '".$site_cd."' AND disable_flag = '0' ORDER BY costcenter ASC";
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (Workorder Status)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$cost_center = array();

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



$sql= "SELECT cf_account.account,  cf_account.descs FROM cf_account (NOLOCK)   WHERE cf_account.site_cd = '".$site_cd."' AND disable_flag = '0' ORDER BY account ASC";
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (Priority)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$cf_account = array();

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


$sql= "SELECT 	tax_mst.tax_mst_tax_cd, tax_mst.tax_mst_desc, tax_mst.tax_mst_tax_rate  FROM 	tax_mst (NOLOCK)    WHERE (	tax_mst.tax_mst_disable_flag = '0') AND tax_mst.site_cd = '".$site_cd."' AND tax_mst_type IN ('A', 'P') AND tax_mst_disable_flag = '0' ORDER BY tax_mst_tax_cd ASC";
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (Priority)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$tax_cd = array();

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




if ($valid) {

	//echo $sql;
	returnData($supplier,$uom,$cost_center,$cf_account,$tax_cd);	
	
}

function returnData($supplier,$uom,$cost_center,$cf_account,$tax_cd){
	//echo $json;
	$returnData = array(												
	'status' => 'SUCCESS',
	'message' => 'Successfully',	
	'supplier' => $supplier,
	'uom' =>$uom,
	'cost_center' =>$cost_center,
	'tax_cd' =>$tax_cd,
	'cf_account'=>$cf_account);
	
	echo json_encode($returnData);
}

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

 
?>