<?php
include '../comunes_php/cors.php';

$id = intval($_REQUEST['id']);
	include '../rdb/conn.php';

try {    
mysqli_query( $conn,"SET AUTOCOMMIT=0");
mysqli_query( $conn,"START TRANSACTION");

$sql = "delete from contrato_detalle_adicionales  where contrato_id={$id}";
@mysqli_query( $conn,$sql);
               		    $myfile = fopen("logFile.txt", "a") or die("Unable to open file!");
              		    fwrite($myfile, "\n");
        
                	    fwrite($myfile, $sql);
        		        fwrite($myfile, "\n");        	    
                	    fclose($myfile);	
                	    
$sql  = "INSERT INTO visitas ( fecha, visitante, accion) VALUES ";
$sql .= " ('$fecha','$visitante','$accion')";
@mysqli_query( $conn,$sql);

$sql = "delete from contratos_detalle where contrato_id={$id}";
@mysqli_query( $conn,$sql);
               		    $myfile = fopen("logFile.txt", "a") or die("Unable to open file!");
              		    fwrite($myfile, "\n");
        
                	    fwrite($myfile, $sql);
        		        fwrite($myfile, "\n");        	    
                	    fclose($myfile);	
                	    
$sql  = "INSERT INTO visitas ( fecha, visitante, accion) VALUES ";
$sql .= " ('$fecha','$visitante','$accion')";
@mysqli_query( $conn,$sql);

$sql = "delete from estados_de_contrato where contrato_id={$id}";
@mysqli_query( $conn,$sql);
               		    $myfile = fopen("logFile.txt", "a") or die("Unable to open file!");
              		    fwrite($myfile, "\n");
        
                	    fwrite($myfile, $sql);
        		        fwrite($myfile, "\n");        	    
                	    fclose($myfile);	
                	    
$sql  = "INSERT INTO visitas ( fecha, visitante, accion) VALUES ";
$sql .= " ('$fecha','$visitante','$accion')";
@mysqli_query( $conn,$sql);

$sql = "delete from reclamos_contrato where contrato_id={$id}";
@mysqli_query( $conn,$sql);
               		    $myfile = fopen("logFile.txt", "a") or die("Unable to open file!");
              		    fwrite($myfile, "\n");
        
                	    fwrite($myfile, $sql);
        		        fwrite($myfile, "\n");        	    
                	    fclose($myfile);	
                	    
$sql  = "INSERT INTO visitas ( fecha, visitante, accion) VALUES ";
$sql .= " ('$fecha','$visitante','$accion')";
@mysqli_query( $conn,$sql);

$visitante ="Acceso desde IP: " .$_SERVER['REMOTE_ADDR'] ;
$fecha= date("Y-m-d H:i:s");
$accion = $sql;

$sql = "delete from contratos_cabecera where id={$id}";
@mysqli_query( $conn,$sql);
               		    $myfile = fopen("logFile.txt", "a") or die("Unable to open file!");
              		    fwrite($myfile, "\n");
        
                	    fwrite($myfile, $sql);
        		        fwrite($myfile, "\n");        	    
                	    fclose($myfile);	
                	    
$sql  = "INSERT INTO visitas ( fecha, visitante, accion) VALUES ";
$sql .= " ('$fecha','$visitante','$accion')";
@mysqli_query( $conn,$sql);
            
mysqli_query( $conn,"COMMIT");
echo json_encode(array('success'=>true));

} catch(exception $error) {
    
mysqli_query( $conn,"ROLLBACK");

echo 'Ocurrio el siguiente error'. $error->getMessage();    
}
?>