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

include 'funciones.php';


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



if ($connection->query("DELETE FROM ".$_SESSION['database'].".operacion_barcode WHERE op_id='".$_SESSION['op_id']."'") === TRUE) {
    $msg = array("success" => "true", "detalle" => "Detalle delete");

} else {
	
	$msg = array("success" => "false", "detalle" => $conn);

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



if ($connection->query("INSERT INTO ".$_SESSION['database'].".operacion_barcode( 
     op_id, fecha, pr_id, pr_nro_parte, 
     pr_referencia, pr_descripcion, po_cant, 
     precio_regul, precio_prom, po_costo,prom_codigo, 
     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, 
      pla.fecha, 
   pdet.pr_id, 
   pr.pr_nro_parte, 
   pr.pr_referencia, 
   pr.pr_descripcion, 
   pdet.cantidad AS po_cant, 
   IFNULL(pdet.precio,0.00) AS precio_regul, 
   0000000000.00 AS precio_prom, 
	IFNULL(pdet.costo ,0.00) AS po_costo, 
   pdet.prom_codigo, 
   000000000.00 ASprec_venta1   , 
   000000000.00 AS prec_venta_imp1, 
   000000000.00 AS prec_venta2, 
   000000000.00 AS prec_venta_imp2, 
   000000000.00 AS prec_venta3, 
   000000000.00 AS prec_venta_imp3, 
   00000000.00 AS prec_venta4, 
   000000000.00 AS prec_venta_imp4, 
   000000000.00 AS prec_venta5, 
   000000000.00 AS prec_venta_imp5, 
   000000000.00 AS prec_venta6, 
   000000000.00 AS prec_venta_imp6,  
   NOW() AS prod_fech_crea , 
   NOW() AS prod_fech_mod  
FROM ".$_SESSION['database'].".plantilla_productos_det    pdet  
INNER JOIN ".$_SESSION['database'].".productos            pr ON pdet.pr_id = pr.pr_id  
INNER JOIN ".$_SESSION['database'].".plantilla_productos  pla ON pla.autoin = pdet.plantilla_id  
WHERE ".$_SESSION['where']."  ORDER BY pdet.autoin;") === 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, '1')  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);
?>