<?php

  $debugin              = $decoded['debug'];
  $documento            = $decoded['documento'];

  //Ventas
  $Query = "
    SELECT
      ven.Id, ven.Documento
    FROM KB_Ventas ven
    WHERE
      ven.Documento = '$documento'
  ";
  $Result = mysqli_query($mysqli, $Query);
  $Ven    = mysqli_fetch_assoc($Result);
  if( $Ven[Id] ){

    //Delete Pagos
    $Query = "
      DELETE FROM KB_Pagos
      WHERE Fk_Venta = '$Ven[Id]'
    ";
    $Result = mysqli_query($mysqli, $Query);

    //Delete Productos de la Venta
    $Query = "
      DELETE FROM KB_VentasProductos
      WHERE Fk_Venta = '$Ven[Id]'
    ";
    $Result = mysqli_query($mysqli, $Query);

    //Delete Venta
    $Query = "
      DELETE FROM KB_Ventas
      WHERE Id = '$Ven[Id]'
    ";
    $Result = mysqli_query($mysqli, $Query);

    //Respuesta
    $Data = array(
      'action'              => 'ok',
      'clave'               => "$Ven[Documento]"
    );
    echo json_encode($Data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

  }else{

    //Error
    $Query = "
      SELECT
        err.Id, err.error, err.msn
      FROM KB_Api_Errores err
      WHERE err.error = 'pro-4'
    ";
    $Result = mysqli_query($mysqli, $Query);
    $Error = mysqli_fetch_assoc($Result);

    $Data = array(
      'action'      => 'error',
      'error'       => $Error[error],
      'error_msn'   => $Error[msn]
    );
    echo json_encode($Data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

  }

?>
