<?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['nombre'] = $books["nombre"];
$_SESSION['precio'] = $books["precio"];
$_SESSION['fam'] = $books["fam"];
$_SESSION['fab'] = $books["fab"];
$_SESSION['marca'] = $books["marca"];
$_SESSION['imp'] = $books["imp"];
$_SESSION['inserta'] = $books["inserta"];

$hoynow = getdate();

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

logs($timeStamp);

$montoesql = "select pr_id FROM " . $_SESSION['database'] . ".productos where pr_id='" . $_SESSION['pr_id'] . "' limit 1 ";

logs($montoesql);
$_SESSION['pr_id2'] = "";

logs('valor: ' . $_SESSION['pr_id2']);

$qur = $connection->query($montoesql);

while ($r = mysqli_fetch_assoc($qur)) {

    $_SESSION['pr_id2'] = $r['pr_id'];

    logs('valor: ' . $_SESSION['pr_id2']);

}

if ($_SESSION['pr_id2'] == "") {

    $SQLupdate1 = "  INSERT INTO " . $_SESSION['database'] . ".productos (pr_id, pr_descripcion, pr_id_familia, pr_referencia, fab_id, id_marca, pr_tipo, usermodify)

        VALUES ('" . $_SESSION['pr_id'] . "', '" . $_SESSION['nombre'] . "','" . $_SESSION['fam'] . "', '1', '" . $_SESSION['fab'] . "','" . $_SESSION['marca'] . "', '3', '1')";

    $SQLupdate2 = "  INSERT INTO " . $_SESSION['database'] . ".productos_det (pr_id, pr_venta1, pr_itbms, pr_costo_prom, pr_activo, pr_activa_desemp, pr_des_tecla, suc_id)

         VALUES ('" . $_SESSION['pr_id'] . "', '" . $_SESSION['precio'] . "', '" . $_SESSION['imp'] . "', '0', '1', 'False', '" . $_SESSION['nombre'] . "', '1')";

    logs($SQLupdate1);

    logs($SQLupdate2);

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

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

            $result = (object) array(
                'success' => 'true',
                'registro' => 1,
                'inserta' => $_SESSION['inserta'],

            );

            print_r(json_encode($result));

        }

    }

} else {

//actualiza

    $SQLupdate12 = "  update " . $_SESSION['database'] . ".productos set   pr_descripcion='" . $_SESSION['nombre'] . "', pr_id_familia='" . $_SESSION['fam'] . "', fab_id='" . $_SESSION['fab'] . "', id_marca='" . $_SESSION['marca'] . "',  usermodify=1 where pr_id= '" . $_SESSION['pr_id'] . "'";

    $SQLupdate22 = "  update  " . $_SESSION['database'] . ".productos_det set   pr_venta1='" . $_SESSION['precio'] . "', pr_itbms='" . $_SESSION['imp'] . "' , pr_des_tecla='" . $_SESSION['nombre'] . "' where pr_id= '" . $_SESSION['pr_id'] . "' ";

    logs($SQLupdate12);

    logs($SQLupdate22);

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

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

            $result = (object) array(
                'success' => 'true',
                'registro' => 1,
                'inserta' => $_SESSION['inserta'],

            );
        }
        print_r(json_encode($result));

    }

}

@mysqli_close($connection);
?>