<?php
require_once('config.php');

$error_message;
$valid = true;

if (isset($_REQUEST['itm_mst_stockno'])){
	$where = " AND itm_mst.itm_mst_stockno = '".$_REQUEST['itm_mst_stockno']. "' ";
}else{
	$where = "";
}

$sql = "SELECT 'itm_mst_rowid' = itm_mst.rowid,
			itm_mst.site_cd,
			itm_mst.itm_mst_stockno,
			itm_mst.itm_mst_desc,
			itm_mst_partno= COALESCE(itm_mst.itm_mst_partno,''),
			itm_mst_com_code = COALESCE(itm_mst_com_code,''),
			itm_mst_issue_price = COALESCE(itm_mst_issue_price,0),
			itm_mst_ttl_oh = COALESCE(itm_mst_ttl_oh,0),
			itm_mst.itm_mst_issue_uom,
			itm_mst.itm_mst_costcenter,
			itm_mst.itm_mst_account,
			itm_mst.itm_mst_mstr_locn,
			itm_mst_ext_desc = COALESCE(cast(itm_mst.itm_mst_ext_desc as varchar (255)),''),	
			itm_mst_itm_grp = COALESCE(itm_mst_itm_grp,''),
			itm_mst_audit_user = itm_mst.audit_user,
			itm_mst_audit_date = itm_mst.audit_date,
			itm_det.itm_det_issue_uom,
			itm_det.itm_det_rcv_uom,	
			itm_det_order_pt= COALESCE(itm_det_order_pt,0),	
			itm_det_ttl_oh= COALESCE(itm_det_ttl_oh,0),		
			itm_det_ttl_hard_resrv= COALESCE(itm_det_ttl_hard_resrv,0),		
			itm_det_ttl_short= COALESCE(itm_det_ttl_short,0),	
			itm_det_due_in= COALESCE(itm_det_due_in,0),		
			itm_det_pr_due_in= COALESCE(itm_det_pr_due_in,0),	
			itm_det_maximum= COALESCE(itm_det_maximum,0),	
			itm_det_item_cost= COALESCE(itm_det_item_cost,0),	
			itm_det.itm_det_part_deac_status
	FROM 	itm_mst, itm_det
	WHERE 	itm_mst.site_cd = '".$_REQUEST['site_cd']."' 
	AND		itm_mst.site_cd = itm_det.site_cd 
	AND		itm_mst.rowid = itm_det.mst_rowid ".$where." 
	AND 	itm_det.itm_det_part_deac_status = '".$_REQUEST['status']."' ORDER BY itm_mst_stockno ASC";
			
$stmt = sqlsrv_query( $conn, $sql);

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

$json_itm = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
	 //echo preg_match('/"/', $row['itm_mst_desc']);
	 $row['itm_mst_desc'] =  utf8_encode($row['itm_mst_desc']);
     $json_itm[] = $row;
	 
	 $itm_det_order_pt = $row['itm_det_order_pt'];
	 $itm_det_ttl_oh = $row['itm_det_ttl_oh'];
	 $itm_det_ttl_hard_resrv = $row['itm_det_ttl_hard_resrv'];
	 $itm_det_ttl_short = $row['itm_det_ttl_short'];
	 $itm_det_due_in = $row['itm_det_due_in'];
	 $itm_det_pr_due_in = $row['itm_det_pr_due_in'];
	 
	 
	 
     }
} while ( sqlsrv_next_result($stmt));


$sub =$itm_det_ttl_oh - $itm_det_ttl_hard_resrv - $itm_det_ttl_short + $itm_det_due_in + $itm_det_pr_due_in;

//echo $sub;
if($itm_det_order_pt == 0){
	$Order='Invisible';
	//echo $Order;
}else if($itm_det_order_pt > 0 & $itm_det_order_pt > $sub ){
	$Order='Visible';
	//echo $Order;
}else if($itm_det_order_pt > 0 & $itm_det_order_pt <= $sub){
	$Order='Invisible';
	//echo $Order;
}

$json['itm'] = $json_itm;

if ($valid) {

	returnData($json,$Order);	
}

function returnData($json,$Order){
	$returnData = array(
	'status' => 'SUCCESS',
	'Order' => $Order,
	'message' => 'Select Successfully',
	'data' => $json);

	echo json_encode($returnData);
}

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

?>