<?php
include 'conn.php';
date_default_timezone_set("America/Panama");
session_start();

$_SESSION['database'] = $_POST['database'];
$_SESSION['json_data'] = $_POST['json_data'];
$_SESSION['debug_var'] = $_POST['debug_var'];

$cDb_Nam = $_SESSION['database'];

$StrJSON=$_SESSION['json_data'];
$DebugOn=$_SESSION['debug_var'];

$hoynow = getdate();

$JsonArray = json_decode($StrJSON, true);

if ($DebugOn==1){
    var_dump($JsonArray);  
}
foreach ($JsonArray as $key => $value) {
    $op_id = $value["op_id"];
    $prom_codigo =$value["prom_codigo"];
    $cupon_codigo =$value["cupon_codigo"];
    $accion =$value["accion"];

    $cSqlLn = "INSERT IGNORE INTO $cDb_Nam.operacion_cupon (`op_id`, `prom_codigo`, `cupon_codigo`, `accion`) VALUES ('$op_id','$prom_codigo','$cupon_codigo',$accion)";
    if ($DebugOn==1){
        var_dump($cSqlLn);  
    }
    mysqli_query($connection, $cSqlLn);
    if ($accion<0)
        $cSqlLn = "UPDATE $cDb_Nam.productos_prom_cupon SET disponibles = (disponibles -1) WHERE (num_cupon = '$cupon_codigo')";
    else
        $cSqlLn = "UPDATE $cDb_Nam.productos_prom_cupon SET disponibles = (disponibles +1) WHERE (num_cupon = '$cupon_codigo')";
    
    if ($DebugOn==1){
        var_dump($cSqlLn);  
    }
    if (mysqli_query($connection, $cSqlLn)==TRUE) {
        $msg = array("success" => "true", "detalle" => "ok");
    } else {
        $msg = array("success" => "false", "detalle" => $conn);
    
    }
}
$jsonData[] =$msg;

header('content-type: application/json');
echo json_encode($jsonData);

@mysqli_close($connection);
/*
Johnn Gutierrez A.
Fecha de Creación	 : 2019-11-03
Modificasiones		 : 1
Fecha de Modificacion: 2018-11-03
Fecha de Modificacion: 2022-11-22
*/
?>
