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

require_once('send_notification.php');

$error_message;
$valid = true;


/* Begin the transaction. */
if ( sqlsrv_begin_transaction( $conn ) === false ) {
     die( print_r( sqlsrv_errors(), 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'];
$EmpName = $data['EmpName'];

$wkr_mst_originator = $data['wkr_mst_originator'];
$wkr_mst_phone = $data['wkr_mst_phone'];
 
$wkr_mst_assetno = $data['wkr_mst_assetno'];
$mst_war_work_area = $data['mst_war_work_area'];
$wkr_mst_assetlocn = $data['wkr_mst_assetlocn'];
$wkr_mst_location = $data['ast_mst_asset_lvl'];



$wkr_mst_wr_no = $data['wkr_mst_wr_no'];
$wkr_mst_chg_costcenter = $data['wkr_mst_chg_costcenter'];


$wkr_mst_orig_priority = $data['wkr_mst_orig_priority'];
$wkr_mst_org_date = $data['wkr_mst_org_date'];
$wkr_mst_due_date = $data['wkr_mst_due_date'];
$wkr_mst_fault_code = $data['wkr_mst_fault_code'];
$wkr_mst_work_group = $data['wkr_mst_work_group'];
$wkr_mst_wr_descs = $data['wkr_mst_wr_descs'];
$service_type = $data['service_type'];


$ROW_ID = $data['RowID'];



$sq_wkr_mst = "UPDATE  	wkr_mst  SET							
						wkr_mst_originator=?,			
						wkr_mst_phone=?,				
						wkr_mst_assetlocn=?,			
						wkr_mst_assetno=?,				
						wkr_mst_fault_code=?,
						wkr_mst_org_date=?,
						wkr_mst_due_date=?,			
						wkr_mst_chg_costcenter=?,			
						wkr_mst_work_area=?,				
						wkr_mst_location=?,							
						wkr_mst_orig_priority=?,
						wkr_mst_wr_descs =?,	
						wkr_mst_work_group =?,						
						audit_user=?,						
						audit_date=GETDATE()						
						where 	site_cd = ?
						AND		wkr_mst_wr_no = ?	
						AND		wkr_mst_service = ?	
						AND		RowID = ?";								
								
//echo $sql;
$params_wkr_mst = array($wkr_mst_originator,$wkr_mst_phone,$wkr_mst_assetlocn,$wkr_mst_assetno,$wkr_mst_fault_code,$wkr_mst_org_date,$wkr_mst_due_date,$wkr_mst_chg_costcenter,$mst_war_work_area,$wkr_mst_assetlocn,$wkr_mst_orig_priority,$wkr_mst_wr_descs,$wkr_mst_work_group,$EmpID,$site_cd,$wkr_mst_wr_no,$service_type,$ROW_ID);	

$stmt_wkr_mst = sqlsrv_query( $conn, $sq_wkr_mst,$params_wkr_mst);

if( !$stmt_wkr_mst ) {
	$error_message = "Error insert table ( UPDATE Table wkr_mst )";
	returnError($error_message);
	die( print_r( sqlsrv_errors(), true));
}	
sqlsrv_free_stmt( $stmt_wkr_mst);	





if( $stmt_wkr_mst ) {
		 sqlsrv_commit( $conn );
		 sqlsrv_close( $conn);	
		 returnData($ROW_ID,$wkr_mst_wr_no);	
		} else {
		 sqlsrv_rollback( $conn );
		 $error_message = "Transaction rolled back.<br />";
		 returnError($error_message);
		} 

 

 
function returnData($ROW_ID,$wkr_mst_wr_no){
	$returnData = array(
	'status' => 'SUCCESS',
	'ROW_ID'=>	$ROW_ID,
	'message' => ' Work Request No:'.$wkr_mst_wr_no.' Update Successfully');

	echo json_encode($returnData);
}

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






 
?>