<?php

if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400'); // cache for 1 day
}

include 'conn.php';
//Select data from database

include 'funciones.php';

session_start();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $data = json_decode(file_get_contents("php://input"), true);
    //print_r($data);
    //print_r( json_encode($data));

}

$books = $data;

//echo ' hola '. $books["database"] ;

$_SESSION['database'] = $books["database"];

$_SESSION['pr_id'] = $books["pr_id"];

$_SESSION['cant'] = $books["cant"];

$hoynow = getdate();

$timeStamp = $hoynow["year"] . "-" . $hoynow["mon"] . "-" . $hoynow["mday"] . " " . $hoynow["hours"] . ":" . $hoynow["minutes"] . ":" . $hoynow["seconds"];

logs($timeStamp);

$SQLupdate0 = "delete from   " . $_SESSION['database'] . ".toma_inv  where pr_id='" . $_SESSION['pr_id'] . "' and nodoc='3'";

if ($connection->query($SQLupdate0) === true) {
//echo "borro bien";
} else {
    echo "Error: " . $SQLupdate0 . "<br>" . $connection->error;

}

$SQLupdate1 = "insert into  " . $_SESSION['database'] . ".toma_inv (nodoc, suc_id, fecha, u_id, pr_id, cantidad, tipo_prod, status, nombre) VALUES ('3', '1', '2016-07-18 08:00:00', '1', '" . $_SESSION['pr_id'] . "', '" . $_SESSION['cant'] . "', '0', '2', 'Toma1')";

logs($SQLupdate1);

if ($connection->query($SQLupdate1) === true) {

    $result = (object) array(
        'success' => 'true',
        'registro' => 1,
        'msg' => "Ajustado correctamente",
    );
    print_r(json_encode($result));

} else {

    $result = (object) array(
        'success' => 'false',
        'registro' => 0,
        'msg' => "Error ajustando",
    );
    print_r(json_encode($result));

}

@mysqli_close($connection);
?>
