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

include 'funciones.php';

session_start();
$_SESSION['database'] = $_POST['database'];
$_SESSION['op_id'] = $_POST['op_id'];
$_SESSION['pr_id'] = $_POST['pr_id'];
$_SESSION['suc_id'] = $_POST['suc_id'];
$_SESSION['po_cant'] = $_POST['po_cant'];

if ($connection->query("INSERT INTO " . $_SESSION['database'] . ".operacion_barcode(
     op_id, pr_id, fecha, pr_nro_parte,
     pr_referencia, pr_descripcion, po_cant,
     po_costo, precio_regul, precio_prom,
     prec_venta1,prec_venta_imp1,
     prec_venta2,prec_venta_imp2,
     prec_venta3,prec_venta_imp3,
     prec_venta4,prec_venta_imp4,
     prec_venta5, prec_venta_imp5,
     prec_venta6,prec_venta_imp6,
     prod_fech_crea,prod_fech_mod)
   SELECT
		'" . $_SESSION['op_id'] . "'AS op_id,
     pd.pr_id,
     NOW() AS fecha,
     pr.pr_nro_parte,
     pr.pr_referencia,
     pr.pr_descripcion,
	' " . $_SESSION['po_cant'] . "'  AS po_cant,
     pd.pr_costo_prom AS po_costo,
     pd.pr_venta1 AS precio_regul,
     0.000 AS precio_prom,
     pd.pr_venta1,
     pd.pr_precio1_imp,
     pd.pr_venta2,
     pd.pr_precio2_imp,
     pd.pr_venta3,
     pd.pr_precio3_imp,
     pd.pr_venta5,
     pd.pr_precio5_imp,
     pd.pr_venta6,
     pd.pr_precio6_imp,
     pd.pr_venta7,
     pd.pr_precio7_imp,
     pd.pr_creacion  AS prod_fech_crea,
     pd.pr_modificacion prod_fech_mod
   FROM " . $_SESSION['database'] . ".productos_det pd
   INNER JOIN " . $_SESSION['database'] . ".productos pr ON pd.pr_id = pr.pr_id
   Where  pd.pr_id='" . $_SESSION['pr_id'] . "'  AND   pd.suc_id=1  ORDER BY pd.suc_id, pd.pr_id;") === true) {
    $msg = array("success" => "true", "detalle insert" => "Detalle insert");

} else {

    $msg = array("success" => "false", "detalle insert" => $conn);

    echo "Error: " . $SQLupdate . "<br>" . $conn->error;
}

if ($connection->query("UPDATE " . $_SESSION['database'] . ".operacion_barcode
	SET precio_prom = " . $_SESSION['database'] . ".Resumen_Promo(pr_id, 0, '" . $_SESSION['suc_id'] . "')  WHERE (op_id='" . $_SESSION['op_id'] . "') AND NOT ( (pr_id = '') OR (pr_id = '0') OR pr_id IS NULL )") === true) {
    $msg = array("success" => "true", "detalle" => "Detalle update");

} else {

    $msg = array("success" => "false", "detalleupdate" => $conn);

    echo "Error: " . $SQLupdate . "<br>" . $conn->error;
}

$json = $msg;
header('content-type: application/json');

echo json_encode($json);

@mysqli_close($connection);
?>