<?php
require_once('config.php');
$error_message;
$valid = true;



$json = file_get_contents('php://input');
// Converts it into a PHP object
$data = json_decode($json, true);

$site_cd = $data['site_cd'];
$EmpID = $data['EmpID'];
$RowID = $data['RowID'];
$LogingID = $data['LOGINID'];
$service_type = $data['service_type'];

$ast_mst_asset_no = $data['ast_mst_asset_no'];
$ast_mst_asset_shortdesc = $data['ast_mst_asset_shortdesc'];
$ast_mst_asset_longdesc = $data['ast_mst_asset_longdesc'];
$ast_mst_sec_asset_no = $data['ast_mst_sec_asset_no'];

$ast_mst_asset_status = $data['ast_mst_asset_status'];
$ast_mst_cri_factor = $data['ast_mst_cri_factor'];
$ast_det_mfg_cd = $data['ast_det_mfg_cd'];
$ast_det_modelno = $data['ast_det_modelno'];
$ast_det_warranty_date = $data['ast_det_warranty_date'];

$ast_det_varchar5 = $data['ast_det_varchar5'];
$ast_mst_non_eq_type = $data['ast_mst_non_eq_type'];
$ast_mst_perm_id = $data['ast_mst_perm_id'];
$ast_mst_cost_center = $data['ast_mst_cost_center'];
$ast_mst_work_area = $data['ast_mst_work_area'];
$ast_mst_assigned_to = $data['ast_mst_assigned_to'];
$ast_mst_ast_lvl = $data['ast_mst_ast_lvl'];
$ast_mst_asset_locn = $data['ast_mst_asset_locn'];

$ast_det_varchar6 = $data['ast_det_varchar6'];
$ast_mst_asset_type = $data['ast_mst_asset_type'];
$ast_mst_wrk_grp = $data['ast_mst_wrk_grp'];
$ast_mst_asset_code = $data['ast_mst_asset_code'];
$ast_mst_asset_grpcode = $data['ast_mst_asset_grpcode'];
$ast_mst_safety_rqmts = $data['ast_mst_safety_rqmts'];




 
/*  
$sql = "SELECT ast_mst_asset_status,ast_mst_work_area,ast_mst_asset_locn From ast_mst WHERE site_cd = '".$site_cd."' AND ast_mst_asset_no = '".$ast_mst_asset_no."'";
		
$stmt = sqlsrv_query( $conn, $sql);	
		
if( !$stmt ) {
	$error_message = "Error update table (itm_mst)";
	returnError($error_message);
	die( print_r( sqlsrv_errors(), true));
}
do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
	 
	 $ast_mst_asset_status = $row['ast_mst_asset_status'];
	 $ast_mst_work_area = $row['ast_mst_work_area'];
	 $ast_mst_asset_locn = $row['ast_mst_asset_locn']; 
	 	 
   }
} while ( sqlsrv_next_result($stmt));
sqlsrv_free_stmt( $stmt); */	

/* if($ast_mst_asset_status != $asset_status){
	
	$sql = "UPDATE	ast_aud 
		SET			ast_aud_end_date = GetDate(),
					ast_aud_duration = 0,
					audit_user = ? ,
					audit_date = getdate()
		WHERE		ast_aud_end_date IS NULL
		AND			site_cd = ?
		AND			ast_aud_asset_no = ?
		AND			ast_aud_status = ?
		AND			mst_RowID = ?";
		
	
$params_update_ast_aud = array($LogingID,$site_cd,$ast_mst_asset_no,$ast_mst_asset_status,$RowID);	
$stmt_update_ast_aud = sqlsrv_query( $conn, $sql,$params_update_ast_aud);	
		
if( !$stmt_update_ast_aud ) {
	$error_message = "Error update table (UPDATE ast_aud)";
	returnError($error_message);
	die( print_r( sqlsrv_errors(), true));
}sqlsrv_free_stmt( $stmt_update_ast_aud);



$sql_insert_ast_aud = "INSERT INTO ast_aud
						(	site_cd ,					mst_RowID ,				ast_aud_asset_no ,			ast_aud_status ,	ast_aud_originator , 
							ast_aud_start_date ,		ast_aud_end_date ,		ast_aud_duration ,			audit_user ,		audit_date ) 												
				VALUES	(	?,							?,						?,							?,					?,
							GetDate(),					NULL,					NULL,						?,					GetDate())";


$params_insert_ast_aud = array($site_cd,$RowID,$ast_mst_asset_no,$asset_status,$EmpID,$LogingID);	
$stmt_insert_ast_aud = sqlsrv_query( $conn, $sql_insert_ast_aud,$params_insert_ast_aud);	
if( !$stmt_insert_ast_aud ) {
$error_message = "Error insert table (INSERT Table ast_aud)";
returnError($error_message);
die( print_r( sqlsrv_errors(), true));
}	
sqlsrv_free_stmt( $stmt_insert_ast_aud);
	
	
} */
/* if($ast_mst_work_area != $work_area){
	
	
	$sql_insert_ast_aud = "INSERT INTO ast_war_s
						( 	site_cd,				ast_war_s_asset_no,		ast_war_s_asset_olocn,		ast_war_s_asset_nlocn,				ast_war_s_asset_reason, 
							audit_user,				audit_date,				column1,					column2,							column3, 
							column4, 				column5 )											
				VALUES	(	?,						?,						?,							?,									?,
							?,						GETDATE(),				NULL,						NULL,								NULL,
							NULL,					NULL)";


$params_ast_war_s = array($site_cd,$ast_mst_asset_no,$ast_mst_work_area,$work_area,$work_remake,$LogingID);
$stmt_ast_war_s = sqlsrv_query( $conn, $sql_insert_ast_aud,$params_ast_war_s);	
if( !$stmt_ast_war_s ) {
$error_message = "Error insert table (INSERT Table ast_aud)";
returnError($error_message);
die( print_r( sqlsrv_errors(), true));
}	
sqlsrv_free_stmt( $stmt_ast_war_s);
	
	
	
	
} */
/* if($ast_mst_asset_locn != $asset_locn){
	

	$sql_insert_ast_aud = "INSERT INTO ast_loc_s
						( 	site_cd,				ast_loc_s_asset_no,		ast_loc_s_asset_olocn,		ast_loc_s_asset_nlocn,				ast_loc_s_asset_reason, 
							audit_user,				audit_date,				column1,					column2,							column3, 
							column4, 				column5 )											
				VALUES	(	?,						?,						?,							?,									?,
							?,						GETDATE(),				NULL,						NULL,								NULL,
							NULL,					NULL)";


$params_ast_loc_s = array($site_cd,$asset_no,$ast_mst_asset_locn,$asset_locn,$Loc_remake,$LogingID);
$stmt_ast_loc_s = sqlsrv_query( $conn, $sql_insert_ast_aud,$params_ast_loc_s);	
if( !$stmt_ast_loc_s ) {
$error_message = "Error insert table (INSERT Table ast_aud)";
returnError($error_message);
die( print_r( sqlsrv_errors(), true));
}	
sqlsrv_free_stmt( $stmt_ast_loc_s);
	
} */



$sql = "UPDATE ast_mst 
		SET 	
				ast_mst_asset_shortdesc	= ?,
				ast_mst_asset_longdesc	= ?,
				ast_mst_sec_asset_no	= ?,
				ast_mst_asset_status 	= ?,
				ast_mst_cri_factor 		= ?,				
				ast_mst_non_eq_type		= ?,
				ast_mst_perm_id 		= ?,
				ast_mst_cost_center 	= ?,
				ast_mst_work_area		= ?,
				ast_mst_assigned_to		= ?,
				ast_mst_ast_lvl			= ?,
				ast_mst_asset_locn 		= ?,				
				ast_mst_asset_type 		= ?,
				ast_mst_wrk_grp			= ?,
				ast_mst_asset_code		= ?, 
				ast_mst_asset_grpcode 	= ?,				
				ast_mst_safety_rqmts	= ?,								
				ast_mst.audit_user = ?,
				ast_mst.audit_date = GETDATE()						
		WHERE site_cd =?
		AND ast_mst_asset_no =? 
		AND RowID = ?";
		
	
$params_ast_mst = array(
						$ast_mst_asset_shortdesc,
						$ast_mst_asset_longdesc,
						$ast_mst_sec_asset_no,
						$ast_mst_asset_status,
						$ast_mst_cri_factor,						
						$ast_mst_non_eq_type,
						$ast_mst_perm_id,
						$ast_mst_cost_center,
						$ast_mst_work_area,
						$ast_mst_assigned_to,
						$ast_mst_ast_lvl,
						$ast_mst_asset_locn,						
						$ast_mst_asset_type,
						$ast_mst_wrk_grp,
						$ast_mst_asset_code,
						$ast_mst_asset_grpcode,
						$ast_mst_safety_rqmts,
						$LogingID,
						$site_cd,
						$ast_mst_asset_no,
						$RowID);	
						
						
						$stmt_ast_mst = sqlsrv_query( $conn, $sql,$params_ast_mst );	
						//echo '<pre>'; print_r($params_ast_mst); echo '</pre>';
		
if( !$stmt_ast_mst ) {
	$error_message = "Error update table (UPDATE ast_aud)";
	returnError($error_message);
	die( print_r( sqlsrv_errors(), true));
}sqlsrv_free_stmt( $stmt_ast_mst);



$sql = "UPDATE ast_det 
		SET ast_det_warranty_date =?,
			ast_det_mfg_cd	=?,	
			ast_det_modelno	=?,	
			ast_det_varchar5	=?,	
			ast_det_varchar6	=?,
			ast_det.audit_user = ?,
			ast_det.audit_date = GETDATE()	
		WHERE site_cd =? 		
		AND mst_RowID = ?";
		

$params_ast_det = array($ast_det_warranty_date,$ast_det_mfg_cd,$ast_det_modelno,$ast_det_varchar5,$ast_det_varchar6,$LogingID,$site_cd,$RowID);
$stmt_ast_det = sqlsrv_query( $conn, $sql,$params_ast_det );	
		
if( !$stmt_ast_det ) {
	$error_message = "Error update table (UPDATE ast_aud)";
	returnError($error_message);
	die( print_r( sqlsrv_errors(), true));
}sqlsrv_free_stmt( $stmt_ast_det);



 
if ($valid) {
	
	sqlsrv_close( $conn);	
	returnData();	
}
 
function returnData(){
	$returnData = array(
	'status' => 'SUCCESS',	
	'message' => 'Update Successfully');
	echo json_encode($returnData);
}

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



 
 
?>