<?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_det_varchar4 = $data['wkr_det_varchar4'];
$wkr_det_varchar5 = $data['wkr_det_varchar5'];
$wkr_mst_phone = $data['wkr_mst_phone'];
$wkr_det_varchar10 = $data['wkr_det_varchar10'];
$wkr_mst_work_class = $data['wkr_mst_work_class'];


$wkr_mst_service = $data['wkr_mst_service'];
$wkr_mst_wr_no = $data['wkr_mst_wr_no'];
$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_det_varchar7 = $data['wkr_det_varchar7'];
$wkr_det_varchar8 = $data['wkr_det_varchar8'];
$wkr_det_varchar9 = $data['wkr_det_varchar9'];
$wkr_mst_wr_descs = $data['wkr_mst_wr_descs'];


$wkr_mst_assetno = $data['wkr_mst_assetno'];
$ast_mst_asset_shortdesc = $data['ast_mst_asset_shortdesc'];
$ast_det_varchar5 = $data['ast_det_varchar5'];
$wkr_mst_chg_costcenter = $data['wkr_mst_chg_costcenter'];
$wkr_mst_work_area = $data['wkr_mst_work_area'];
$wkr_mst_projectid = $data['wkr_mst_projectid'];
$wkr_mst_location = $data['wkr_mst_location'];
$wkr_mst_assetlocn = $data['wkr_mst_assetlocn'];
$wkr_det_note1 = $data['wkr_det_note1'];

$service_type = $data['service_type'];


$ROW_ID = $data['RowID'];



$sq_wkr_mst = "UPDATE  	wkr_mst  SET							
						wkr_mst_originator=?,			
						wkr_mst_phone=?,
						wkr_mst_work_class=?,
						wkr_mst_projectid=?,	
						wkr_mst_assetlocn=?,			
						wkr_mst_assetno=?,
						wkr_mst_org_date=?,
						wkr_mst_due_date=?,			
						wkr_mst_chg_costcenter=?,			
						wkr_mst_work_area=?,	
						wkr_mst_orig_priority=?,
						wkr_mst_wr_descs =?,	
						audit_user=?,						
						audit_date=GETDATE()						
						where 	site_cd = ?
						AND		wkr_mst_wr_no = ?
						AND		RowID = ?";								
								
//echo $sql;
$params_wkr_mst = array($wkr_mst_originator,$wkr_mst_phone,$wkr_mst_work_class,$wkr_mst_projectid,$wkr_mst_assetlocn,$wkr_mst_assetno,$wkr_mst_org_date,$wkr_mst_due_date,$wkr_mst_chg_costcenter,$wkr_mst_work_area,$wkr_mst_orig_priority,$wkr_mst_wr_descs,$EmpID,$site_cd,$wkr_mst_wr_no,$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);	



$sq_wkr_det = "UPDATE  	wkr_det  SET							
						wkr_det_varchar4=?,			
						wkr_det_varchar5=?,
						wkr_det_varchar10=?,
						wkr_det_varchar7=?,	
						wkr_det_varchar8=?,							
						wkr_det_varchar9=?,			
						wkr_det_note1=?,							
						audit_user=?,						
						audit_date=GETDATE()						
						where 	site_cd = ? 
						AND		mst_RowID = ?";								
								
//echo $sql;
$params_wkr_det = array($wkr_det_varchar4,$wkr_det_varchar5,$wkr_det_varchar10,$wkr_det_varchar7,$wkr_det_varchar8,$wkr_det_varchar9,$wkr_det_note1,$EmpID,$site_cd,$ROW_ID);	

$stmt_wkr_det = sqlsrv_query( $conn, $sq_wkr_det,$params_wkr_det);

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



$sq_ast_det = "UPDATE  	ast_det  SET
						ast_det_varchar5=?,													
						audit_user=?,						
						audit_date=GETDATE()						
						where 	site_cd = ? 
						AND		mst_RowID = ?";								
								
//echo $sql;
$params_ast_det = array($ast_det_varchar5,$EmpID,$site_cd,$ROW_ID);	

$stmt_ast_det = sqlsrv_query( $conn, $sq_ast_det,$params_ast_det);

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





if( $stmt_wkr_mst && $stmt_wkr_det && $stmt_ast_det ) {
		 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();
}






 
?>