<?php
require_once('config.php');
require_once('send_notification.php');
require_once('f_insert_notification_log.php');
require_once('find_online_tablet.php');

$error_message;
$valid = true;
$insert_ntf1 = false;
$insert_ntf2 = false;
$insert_ntf3 = false;
$insert_ntf4 = false;
$wko_status;

/* 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'];

$wko_mst_originator = $data['wko_mst_originator'];
$wko_mst_phone = $data['wko_mst_phone'];
 
$ast_mst_asset_no = $data['ast_mst_asset_no'];
$ast_mst_asset_shortdesc = $data['ast_mst_asset_shortdesc'];
$ast_mst_asset_group = $data['ast_mst_asset_group'];
$mst_war_work_area = $data['mst_war_work_area'];
$ast_mst_asset_locn = $data['ast_mst_asset_locn'];
$ast_mst_asset_lvl = $data['ast_mst_asset_lvl'];
$ast_mst_asset_code = $data['ast_mst_asset_code'];

$wko_mst_wo_no = $data['wko_mst_wo_no'];

$wko_det_laccount = $data['wko_det_laccount'];
$wko_det_caccount = $data['wko_det_caccount'];
$wko_det_maccount = $data['wko_det_maccount'];

$wko_mst_chg_costcenter = $data['wko_mst_chg_costcenter'];
$wko_mst_orig_priority = $data['wko_mst_orig_priority'];
$wko_mst_plan_priority = $data['wko_mst_plan_priority'];
$wko_mst_org_date = $data['wko_mst_org_date'];
$wko_mst_due_date = $data['wko_mst_due_date'];
$wko_mst_status = $data['wko_mst_status'];
$wko_det_work_type = $data['wko_det_work_type'];
$wko_mst_flt_code = $data['wko_mst_flt_code'];
$wko_mst_descs = $data['wko_mst_descs'];
$wko_det_assign_to =$data['wko_det_assign_to'];
$cnt_mst_numbering = $data['cnt_mst_numbering'];
$per_assignto = $data['per_assignto'];
$dvc_id = $data['dvc_id'];
$assign_remake = $data['assign_remake'];

$ROW_ID = $data['RowID'];


$LOGINID = $data['LOGINID'];


$sql_wko_mst_status = " select wko_mst_status from wko_mst where site_cd = ?  and wko_mst_wo_no = ?";

$params_wko_mst_status = array($site_cd,$wko_mst_wo_no);	
$stmt_wko_mst_status = sqlsrv_query( $conn, $sql_wko_mst_status,$params_wko_mst_status);	
		
if( !$stmt_wko_mst_status ) {
	$error_message = "Error Select table (wko_mst)";
	returnError($error_message);
	die( print_r( sqlsrv_errors(), true));
}
do {
     while ($row = sqlsrv_fetch_array($stmt_wko_mst_status, SQLSRV_FETCH_ASSOC)) {
	 
	 $wko_status = $row['wko_mst_status'];
	
	 	 
   }
} while ( sqlsrv_next_result($stmt_wko_mst_status));
sqlsrv_free_stmt( $stmt_wko_mst_status);


if($wko_mst_status != $wko_status ){
	
	
			$sql_wko_sts = "UPDATE	wko_sts 
							SET 	wko_sts_end_date = GetDate(),
									wko_sts_duration = 0,
									audit_user = ?,
									audit_date = getdate()
							WHERE	wko_sts_end_date IS NULL
							AND		site_cd = ?
							AND		wko_sts_wo_no = ?
							AND		wko_sts_status = ?
							AND		mst_RowID = ?";
				
			
			$params_wko_sts = array($EmpID,$site_cd,$wko_mst_wo_no,$wko_status,$ROW_ID);
			$stmt_wko_sts = sqlsrv_query( $conn, $sql_wko_sts,$params_wko_sts);	
				
		if( !$stmt_wko_sts ) {
			$error_message = "Error update table (wko_sts)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}sqlsrv_free_stmt( $stmt_wko_sts);
	
	
		$sql_insert_wko_sts = "	INSERT wko_sts 
								(	site_cd ,					mst_RowID ,					wko_sts_wo_no ,					wko_sts_status , 
									wko_sts_originator ,		wko_sts_start_date ,		wko_sts_end_date ,				wko_sts_duration , 
									audit_user ,				audit_date ) 
						VALUES 
								(	?,							?,							?,								?,
									?,							GetDate(),					NULL,							NULL,
									?,							GetDate())";

		$params_insert_wko_sts = array($site_cd,$ROW_ID,$wko_mst_wo_no,$wko_mst_status,$wko_mst_originator,$LOGINID);
		$stmt_insert_wko_sts = sqlsrv_query( $conn, $sql_insert_wko_sts,$params_insert_wko_sts);	
		if( !$stmt_insert_wko_sts ) {
		$error_message = "Error (INSERT Table wko_sts)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
			
		}
		sqlsrv_free_stmt( $stmt_insert_wko_sts);

}

$sq_wko_mst = "UPDATE  	wko_mst  SET							
						wko_mst_originator=?,			
						wko_mst_phone=?,				
						wko_mst_asset_level=?,			
						wko_mst_assetno=?,				
						wko_mst_flt_code=?,			
						wko_mst_status=?,
						wko_mst_descs=?,	
						wko_mst_org_date=?,
						wko_mst_due_date=?,			
						wko_mst_chg_costcenter=?,			
						wko_mst_work_area=?,				
						wko_mst_asset_location=?,		
						wko_mst_asset_group_code=?,	
						wko_mst_orig_priority=?,			
						wko_mst_plan_priority=?,					
						wko_mst_ast_cod=?,				
						audit_user=?,						
						audit_date=GETDATE()						
						where 	site_cd = ?
						AND		wko_mst_wo_no = ?	
						AND		RowID = ?";								
								
//echo $sql;
$params_wko_mst = array($wko_mst_originator,$wko_mst_phone,$ast_mst_asset_lvl,$ast_mst_asset_no,$wko_mst_flt_code,$wko_mst_status,$wko_mst_descs,$wko_mst_org_date,$wko_mst_due_date,$wko_mst_chg_costcenter,$mst_war_work_area,$ast_mst_asset_locn,$ast_mst_asset_group,$wko_mst_orig_priority,$wko_mst_plan_priority,$ast_mst_asset_code,$LOGINID,$site_cd,$wko_mst_wo_no,$ROW_ID);	

$stmt_wko_mst = sqlsrv_query( $conn, $sq_wko_mst,$params_wko_mst);

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



$sql_wko_det = "UPDATE  wko_det  SET
						wko_det_work_type=?,
						wko_det_laccount=?,			
						wko_det_caccount=?,				
						wko_det_maccount=?,
						wko_det_assign_to=?						
						where 	site_cd = ?		
						AND		mst_RowID = ?";								
								
$params_wko_det = array($wko_det_work_type,$wko_det_laccount,$wko_det_caccount,$wko_det_maccount,$wko_det_assign_to,$site_cd,$ROW_ID);	
$stmt_wko_det = sqlsrv_query( $conn, $sql_wko_det,$params_wko_det);
				

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



//AssignTo Value From Value A(WIthdraw) to Value B(Assign)
 if($per_assignto != $wko_det_assign_to and !empty($per_assignto) and !empty($wko_det_assign_to)){	

	//Insert notification_log for Old Value Withdrawal
	$title = "Work Order Withdrawal";
	$body = "The work order  ('".$wko_mst_wo_no."') been assigned to another employee.";
	$module = "Work Order";
	$message_withdraw ="Asset No:".$ast_mst_asset_no." 
	\r\n   Asset Desc:".$ast_mst_asset_shortdesc."
	\r\n   Work Area:".$ast_mst_asset_shortdesc."
	\r\n   Location:".$ast_mst_asset_locn."";
	
	
	
	
	$insert_ntf1 = insert_notification_log($site_cd, $EmpID, $wko_det_assign_to, $wko_mst_wo_no, $title, $body, $message_withdraw, $module, $dvc_id, $LOGINID);
	
	foreach($insert_ntf1 as $x => $val) {
			
			$row = $x = $val["row"];
			$insert_ntf = $x = $val["insert_ntf"];
			
			//echo '<pre>'; print_r($row); echo '</pre>';
			//echo '<pre>'; print_r($insert_ntf); echo '</pre>';		
			
		
		}
	
	
	//Get New Value Assignto Online Token
	$tkn_token_1 = get_online($per_assignto, $site_cd);
	
	//echo '<pre>'; print_r($tkn_token_1); echo '</pre>';
	
	//Notify token user
	if (sizeof($tkn_token_1) > 0){		
		
		
		foreach($tkn_token_1 as $x => $val) {			
			$tokenlist = $x = $val["token"];
			$RowID = $x = $val["RowID"];
			$tkn_site = $x = $val["tkn_site"];
			
			//echo '<pre>'; print_r($tkn_site); echo '</pre>';
			//echo '<pre>'; print_r($RowID); echo '</pre>';			
			
			push_notification_android( $tokenlist,'The work order ('.$wko_mst_wo_no.') had been assigned to another employee',"Work Order Withdrawal (".$site_cd.")",$site_cd,$tokenlist,$RowID,$row,$tkn_site,$LOGINID);			
			
		
		}		
		
	}	
	
	
	
		
	
	//Insert notification_log for New Value Assign
	$title = "Work Order Assigned";
	$body = "The work order  ('".$wko_mst_wo_no."') been assigned to you.";
	$module = "Work Order";
	$message_assign ="Asset No:".$ast_mst_asset_no." 
	\r\n   Asset Desc:".$ast_mst_asset_shortdesc."
	\r\n   Work Area:".$ast_mst_asset_shortdesc."
	\r\n   Location:".$ast_mst_asset_locn."";
	
	
	
	$sql_wko_ls7_max = "select max_level = coalesce( MAX (wko_ls7_level ),0) + 1 from  wko_ls7 where site_cd =? and mst_RowID = ?";
				
			
			$params_wko_ls7_max = array($site_cd,$ROW_ID);
			$stmt_wko_ls7_max = sqlsrv_query( $conn, $sql_wko_ls7_max,$params_wko_ls7_max);	
				
		if( !$stmt_wko_ls7_max ) {
			$error_message = "Error select table (wko_ls7)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}
		do {
			
			while ($row = sqlsrv_fetch_array($stmt_wko_ls7_max, SQLSRV_FETCH_ASSOC)) {		
			
			$max_level = $row['max_level'];		
			
			}
			
		} while ( sqlsrv_next_result($stmt_wko_ls7_max) );
		sqlsrv_free_stmt( $stmt_wko_ls7_max);
	
	
	
	
	$sql_wko_ls7 = "UPDATE	wko_ls7 
					SET 	wko_ls7_date3 = GetDate()
					WHERE		wko_ls7.site_cd = ?
					AND			wko_ls7.mst_rowid = ?
					AND			wko_ls7.wko_ls7_emp_id = ? 
					AND			wko_ls7.wko_ls7_date3 IS NULL ";
				
			
			$params_wko_ls7 = array($site_cd,$ROW_ID,$per_assignto);
			$stmt_wko_ls7 = sqlsrv_query( $conn, $sql_wko_ls7,$params_wko_ls7);	
				
		if( !$stmt_wko_ls7 ) {
			$error_message = "Error update table (wko_ls7)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}sqlsrv_free_stmt( $stmt_wko_ls7);
	
	
	
	$sql_insert_wko_ls7 = "	INSERT wko_ls7 
								(	site_cd ,					mst_rowid ,					wko_ls7_level ,					wko_ls7_emp_id , 
									wko_ls7_due_days ,			wko_ls7_wo_org_date ,		wko_ls7_due_date ,				wko_ls7_total_days , 
									audit_user ,				audit_date,					column1 ) 
						VALUES 
								(	?,							?,							?,								?,
									?,							?,							?,								'1',
									?,							GetDate(),					?)";

		$params_insert_wko_ls7 = array($site_cd,$ROW_ID,$max_level,$wko_det_assign_to,'1',$wko_mst_org_date,$wko_mst_due_date,$LOGINID,$assign_remake);
		$stmt_insert_wko_ls7 = sqlsrv_query( $conn, $sql_insert_wko_ls7,$params_insert_wko_ls7);	
		if( !$stmt_insert_wko_ls7 ) {
		$error_message = "Error (INSERT Table wko_ls7)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
			
		}
		sqlsrv_free_stmt( $stmt_insert_wko_ls7);
	
	$insert_ntf2 = insert_notification_log($site_cd, $EmpID, $wko_det_assign_to, $wko_mst_wo_no, $title, $body, $message_assign, $module, $dvc_id, $LOGINID);
	
	foreach($insert_ntf2 as $x => $val) {
			
			$row = $x = $val["row"];
			$insert_ntf = $x = $val["insert_ntf"];
			
			//echo '<pre>'; print_r($row); echo '</pre>';
			//echo '<pre>'; print_r($insert_ntf); echo '</pre>';		
			
		
		}
	
	
	//Get New Value Assignto Online Token
	$tkn_token_2 = get_online($wko_det_assign_to, $site_cd);
	
	//echo '<pre>'; print_r($tkn_token_2); echo '</pre>';
	
	//Notify token user
	if (sizeof($tkn_token_2) > 0){		
		
		
		foreach($tkn_token_2 as $x => $val) {			
			$tokenlist = $x = $val["token"];
			$RowID = $x = $val["RowID"];
			$tkn_site = $x = $val["tkn_site"];
			
			//echo '<pre>'; print_r($tkn_site); echo '</pre>';
			//echo '<pre>'; print_r($RowID); echo '</pre>';			
			
			push_notification_android( $tokenlist,'The work order ('.$wko_mst_wo_no.')  had been assigned to you',"Work Order Assigned (".$site_cd.")",$site_cd,$tokenlist,$RowID,$row,$tkn_site,$LOGINID);			
			
		
		}		
		
	}	
	
	
		
}

//AssignTo Value From Value A to NULL
 if($per_assignto != $wko_det_assign_to and !empty($per_assignto) and empty($wko_det_assign_to)){	

	//Insert notification_log for Old Value Withdrawal
	$title = "Work Order Withdrawal";
	$body = "The work order  ('".$wko_mst_wo_no."') been withdraw.";
	$module = "Work Order";
	$message_withdraw ="Asset No:".$ast_mst_asset_no." 
	\r\n   Asset Desc:".$ast_mst_asset_shortdesc."
	\r\n   Work Area:".$ast_mst_asset_shortdesc."
	\r\n   Location:".$ast_mst_asset_locn."";
	
	
	
	$sql_wko_ls7 = "UPDATE	wko_ls7 
					SET 	wko_ls7_date3 = GetDate(),
					column1 = 
					CASE WHEN column1 IS NULL THEN '".$assign_remake."'
					ELSE column1 + ' | ' + '".$assign_remake."'
					END
					WHERE		wko_ls7.site_cd = ?
					AND			wko_ls7.mst_rowid = ?
					AND			wko_ls7.wko_ls7_emp_id = ? 
					AND			wko_ls7.wko_ls7_date3 IS NULL ";
				
			
			$params_wko_ls7 = array($site_cd,$ROW_ID,$per_assignto);
			$stmt_wko_ls7 = sqlsrv_query( $conn, $sql_wko_ls7,$params_wko_ls7);	
				
		if( !$stmt_wko_ls7 ) {
			$error_message = "Error update table (wko_ls7)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}sqlsrv_free_stmt( $stmt_wko_ls7);
		
	
	$insert_ntf3 = insert_notification_log($site_cd, $EmpID, $wko_det_assign_to, $wko_mst_wo_no, $title, $body, $message_withdraw, $module, $dvc_id, $LOGINID);
	
	foreach($insert_ntf3 as $x => $val) {
			
			$row = $x = $val["row"];
			$insert_ntf = $x = $val["insert_ntf"];
			
			//echo '<pre>'; print_r($row); echo '</pre>';
			//echo '<pre>'; print_r($insert_ntf); echo '</pre>';		
			
		
		}
	
	
	//Get New Value Assignto Online Token
	$tkn_token_3 = get_online($per_assignto, $site_cd);
	
	//echo '<pre>'; print_r($tkn_token_2); echo '</pre>';
	
	//Notify token user
	if (sizeof($tkn_token_3) > 0){		
		
		
		foreach($tkn_token_3 as $x => $val) {			
			$tokenlist = $x = $val["token"];
			$RowID = $x = $val["RowID"];
			$tkn_site = $x = $val["tkn_site"];
			
			//echo '<pre>'; print_r($tkn_site); echo '</pre>';
			//echo '<pre>'; print_r($RowID); echo '</pre>';			
			
			push_notification_android( $tokenlist,'The work order ('.$wko_mst_wo_no.') had been withdraw',"Work Order Withdrawal  (".$site_cd.")",$site_cd,$tokenlist,$RowID,$row,$tkn_site,$LOGINID);			
			
		
		}		
		
	}	
	
	
}




//AssignTo Value From Value Null to A
if(!empty($wko_det_assign_to) and empty($per_assignto)){

	
	$title = "Work Order Assigned";
	$body = "The work order  ('".$wko_mst_wo_no."') been assigned to you.";
	$module = "Work Order";
	$message_assign ="Asset No:".$ast_mst_asset_no." 
	\r\n   Asset Desc:".$ast_mst_asset_shortdesc."
	\r\n   Work Area:".$ast_mst_asset_shortdesc."
	\r\n   Location:".$ast_mst_asset_locn."";
	
	
	
	$sql_wko_ls7_max = "select max_level = coalesce( MAX (wko_ls7_level ),0) + 1 from  wko_ls7 where site_cd =? and mst_RowID = ?";
				
			
			$params_wko_ls7_max = array($site_cd,$ROW_ID);
			$stmt_wko_ls7_max = sqlsrv_query( $conn, $sql_wko_ls7_max,$params_wko_ls7_max);	
				
		if( !$stmt_wko_ls7_max ) {
			$error_message = "Error select table (wko_ls7)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}
		do {
			
			while ($row = sqlsrv_fetch_array($stmt_wko_ls7_max, SQLSRV_FETCH_ASSOC)) {		
			
			$max_level = $row['max_level'];		
			
			}
			
		} while ( sqlsrv_next_result($stmt_wko_ls7_max) );
		//sqlsrv_free_stmt( $stmt_wko_ls7_max);
	
	
	$sql_insert_wko_ls7 = "	INSERT wko_ls7 
								(	site_cd ,					mst_rowid ,					wko_ls7_level ,					wko_ls7_emp_id , 
									wko_ls7_due_days ,			wko_ls7_wo_org_date ,		wko_ls7_due_date ,				wko_ls7_total_days , 
									audit_user ,				audit_date,					column1 ) 
						VALUES 
								(	?,							?,							?,								?,
									?,							?,							?,								'1',
									?,							GetDate(),					?)";

		$params_insert_wko_ls7 = array($site_cd,$ROW_ID,$max_level,$wko_det_assign_to,'1',$wko_mst_org_date,$wko_mst_due_date,$LOGINID,$assign_remake);
		$stmt_insert_wko_ls7 = sqlsrv_query( $conn, $sql_insert_wko_ls7,$params_insert_wko_ls7);	
		if( !$stmt_insert_wko_ls7 ) {
		$error_message = "Error (INSERT Table wko_ls7)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
			
		}
		sqlsrv_free_stmt( $stmt_insert_wko_ls7);
	
	$insert_ntf4 = insert_notification_log($site_cd, $EmpID, $wko_det_assign_to, $wko_mst_wo_no, $title, $body, $message_assign, $module, $dvc_id, $LOGINID);
	
	foreach($insert_ntf4 as $x => $val) {
			
			$row = $x = $val["row"];
			$insert_ntf = $x = $val["insert_ntf"];
			
			//echo '<pre>'; print_r($row); echo '</pre>';
			//echo '<pre>'; print_r($insert_ntf); echo '</pre>';		
			
		
		}
	
	
	//Get New Value Assignto Online Token
	$tkn_token_4 = get_online($wko_det_assign_to, $site_cd);
	
	//echo '<pre>'; print_r($tkn_token_2); echo '</pre>';
	
	//Notify token user
	if (sizeof($tkn_token_4) > 0){		
		
		
		foreach($tkn_token_4 as $x => $val) {			
			$tokenlist = $x = $val["token"];
			$RowID = $x = $val["RowID"];
			$tkn_site = $x = $val["tkn_site"];
			
			//echo '<pre>'; print_r($tkn_site); echo '</pre>';
			//echo '<pre>'; print_r($RowID); echo '</pre>';			
			
			push_notification_android( $tokenlist,'The work order ('.$wko_mst_wo_no.') had been assigned to you',"Work Order Assigned (".$site_cd.")",$site_cd,$tokenlist,$RowID,$row,$tkn_site,$LOGINID);			
			
		
		}		
		
	}	
}	



 
 if($stmt_wko_mst_status){
	 
	 if($wko_mst_status != $wko_status){
		 
		 if( $stmt_wko_sts && $stmt_insert_wko_sts &&  $stmt_wko_mst && $stmt_wko_det  ) {			 
			
			
			 //AssignTo Value From Value A(WIthdraw) to Value B(Assign)
			if($per_assignto != $wko_det_assign_to and !empty($per_assignto) and !empty($wko_det_assign_to)){
				if($insert_ntf1 && $insert_ntf2){
					
					sqlsrv_commit( $conn );
					returnData($ROW_ID,$wko_mst_wo_no);
				}else{
					
					sqlsrv_rollback( $conn );
					$error_message = "Transaction rolled back.<br />";
					returnError($error_message); 
				}
				
			}
			//AssignTo Value From Value A to NULL
			else if($per_assignto != $wko_det_assign_to and !empty($per_assignto) and empty($wko_det_assign_to)){
				if($insert_ntf3){
					sqlsrv_commit( $conn );
					returnData($ROW_ID,$wko_mst_wo_no);
				}else{
					sqlsrv_rollback( $conn );
					$error_message = "Transaction rolled back.<br />";
					returnError($error_message); 
				}
			}
			//AssignTo Value From Value Null to A
			else if(!empty($wko_det_assign_to) and empty($per_assignto)){
				if($insert_ntf4){
					sqlsrv_commit( $conn );
					returnData($ROW_ID,$wko_mst_wo_no); 
				}else{
					sqlsrv_rollback( $conn );
					$error_message = "Transaction rolled back.<br />";
					returnError($error_message); 
				}
			}
			else{
					sqlsrv_commit( $conn );
					returnData($ROW_ID,$wko_mst_wo_no); 
			}
			 
		 }else{
			sqlsrv_rollback( $conn );
			$error_message = "Transaction rolled back.<br />";
			returnError($error_message); 
		 }
		 
	 }else{
		if( $stmt_wko_mst && $stmt_wko_det  ) {
			
			 //AssignTo Value From Value A(WIthdraw) to Value B(Assign)
			if($per_assignto != $wko_det_assign_to and !empty($per_assignto) and !empty($wko_det_assign_to)){
				if($insert_ntf1 && $insert_ntf2){
					
					sqlsrv_commit( $conn );
					returnData($ROW_ID,$wko_mst_wo_no);
				}else{
					
					sqlsrv_rollback( $conn );
					$error_message = "Transaction rolled back.<br />";
					returnError($error_message); 
				}
				
			}
			//AssignTo Value From Value A to NULL
			else if($per_assignto != $wko_det_assign_to and !empty($per_assignto) and empty($wko_det_assign_to)){
				if($insert_ntf3){
					sqlsrv_commit( $conn );
					returnData($ROW_ID,$wko_mst_wo_no);
				}else{
					sqlsrv_rollback( $conn );
					$error_message = "Transaction rolled back.<br />";
					returnError($error_message); 
				}
			}
			//AssignTo Value From Value Null to A
			else if(!empty($wko_det_assign_to) and empty($per_assignto)){
				if($insert_ntf4){
					sqlsrv_commit( $conn );
					returnData($ROW_ID,$wko_mst_wo_no); 
				}else{
					sqlsrv_rollback( $conn );
					$error_message = "Transaction rolled back.<br />";
					returnError($error_message); 
				}
			}
			else{
					sqlsrv_commit( $conn );
					returnData($ROW_ID,$wko_mst_wo_no); 
			}
			 
		 }else{
			sqlsrv_rollback( $conn );
			$error_message = "Transaction rolled back.<br />";
			returnError($error_message); 
		 } 
	 }
	 
	 
 }else{
	sqlsrv_rollback( $conn );
	$error_message = "Transaction rolled back.<br />";
	returnError($error_message);
 }
	

 
function returnData($ROW_ID,$wko_mst_wo_no){
	$returnData = array(
	'status' => 'SUCCESS',
	'ROW_ID'=>	$ROW_ID,
	'message' => ' Work Order No:'.$wko_mst_wo_no.' Update Successfully');
	echo json_encode($returnData);
}

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


 
?>