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

require_once ('f_logging.php');

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods:  GET");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

if ( sqlsrv_begin_transaction( $conn ) === false ) {
    die( print_r( sqlsrv_errors(), true ));
}

$now = date("Ymdhisa");
$file_name = __DIR__ . "\mobile_log\_delete_asset_spare_part.log";

$error_message;
$valid = true;

$site_cd = $_REQUEST['site_cd'];
$RowID = $_REQUEST['RowID'];
 
 
$sql = "DELETE FROM ast_ls1 WHERE   site_cd = '".$site_cd."' AND RowID = '".$RowID."'";
		
$stmt = sqlsrv_query( $conn, $sql);	
		
// Check for errors in the query execution
if ($stmt === false)
{
	if (($errors = sqlsrv_errors()) != null)
	{
		foreach ($errors as $error)
		{

			file_put_contents($file_name, $now . "\r\n" . $sql . "\r\n" . "SQLSTATE: " . $error['SQLSTATE'] . "\r\n" . "code: " . $error['code'] . "\r\n" . "message: " . $error['message'] . "\r\n\r\n", FILE_APPEND);
		}
	}
	sqlsrv_rollback($conn);
	sqlsrv_close($conn);

	$error_message = "(".$now.") Error deleting table (ast_ls1)";
	returnError($error_message);
}
 while ( sqlsrv_next_result($stmt));

 
if ($valid) {
	
	sqlsrv_close( $conn);	
	returnData();	
}
 
function returnData(){
	$returnData = array(
	'status' => 'SUCCESS',	
	'message' => 'Deleted successfully');

	echo json_encode($returnData);
}

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



 
 
?>