<?php
include 'conn.php';
include 'utiles.php';
date_default_timezone_set("America/Panama");
session_start();
error_reporting(E_ALL);
$_SESSION['database'] = $_POST['database'];
$_SESSION['c_id'] = $_POST['c_id'];

$Cod_Cte = $_SESSION['c_id'];

$hoynow = getdate();

# -----*INSERTA EL REGISTRO DEL CLIENTE, SI NO EXISTE*----#
$Sql_Cmd = 'INSERT IGNORE INTO ' . $_SESSION['database'] . '.puntos_clientes (p_cod_cte, p_acumulados, p_utilizados, p_disponibles, p_redimidos )
	VALUES ("' . $Cod_Cte . '",0,0,0,0)';
$connection->query($Sql_Cmd);

$Sql_Cmd = 'SELECT SUM(puntos) AS Tot_Ptos FROM ' . $_SESSION['database'] . '.puntos_detalle WHERE (acum=1) and (c_id="' . $Cod_Cte . '") GROUP BY c_id,acum';
$records = 0;
$result = getRow($connection, $Sql_Cmd, $records, false);
if (($result['Tot_Ptos'] != '') && ($result['Tot_Ptos'] != 'null') && ($result['Tot_Ptos'] != 'NULL')) {
    $Ptos_Acu = $result['Tot_Ptos'];
} else {
    $Ptos_Acu = 0;
}

$Sql_Cmd = 'SELECT SUM(puntos) AS Tot_Ptos FROM ' . $_SESSION['database'] . '.puntos_detalle WHERE (acum=0) and (c_id="' . $Cod_Cte . '") GROUP BY c_id,acum';
$records = 0;
$result = getRow($connection, $Sql_Cmd, $records, false);
if (($result['Tot_Ptos'] != '') && ($result['Tot_Ptos'] != 'null') && ($result['Tot_Ptos'] != 'NULL')) {
    $Ptos_Usa = $result['Tot_Ptos'];
} else {
    $Ptos_Usa = 0;
}

$Ptos_Dis = ($Ptos_Acu - $Ptos_Usa);

$Sql_Cmd = 'UPDATE ' . $_SESSION['database'] . '.puntos_clientes SET p_acumulados=' . $Ptos_Acu . ', p_utilizados=' . $Ptos_Usa . ',p_disponibles=' . $Ptos_Dis . ' WHERE (p_cod_cte="' . $Cod_Cte . '"); ';
$connection->query($Sql_Cmd);

$records = 0;
$Sql_Cmd = 'SELECT cat_id FROM ' . $_SESSION['database'] . '.clientes WHERE (c_id="' . $Cod_Cte . '") ';
$result = getRow($connection, $Sql_Cmd, $records, false);
if (($result['cat_id'] != '') && ($result['cat_id'] != 'null') && ($result['cat_id'] != 'NULL')) {
    $Cod_Cat = $result['cat_id'];
} else {
    $Cod_Cat = -1;
}

$records = 0;
$Sql_Cmd = 'SELECT IFNULL(redim_porc,0) AS redim_porc FROM ' . $_SESSION['database'] . '.puntos_conf WHERE (cod_cat="' . $Cod_Cat . '") ';
$result = getRow($connection, $Sql_Cmd, $records, false);
if (($result['redim_porc'] != '') && ($result['redim_porc'] != 'null') && ($result['redim_porc'] != 'NULL')) {
    $Porc_Redenc = $result['redim_porc'];
} else {
    $Porc_Redenc = 0;
}

$Sql_Cmd = 'SELECT
    IFNULL(p_disponibles,0) AS disponible ,
    (IFNULL(p_disponibles,0)*(IFNULL(' . $Porc_Redenc . ',0)/100)) AS money,
    IFNULL(' . $Porc_Redenc . ',0) AS redim_porc
    FROM  ' . $_SESSION['database'] . '.puntos_clientes
    WHERE (p_cod_cte="' . $Cod_Cte . '") LIMIT 1';

$records = 0;
$r = getRow($connection, $Sql_Cmd, $records, false);
$JsonResp=null;
if ($r == true) {
    $JsonResp->disponible = $r['disponible'];
    $JsonResp->money = $r['money'];
    $JsonResp->redim_porc = $r['redim_porc'];
    $JsonResp->success = "true";
} else {
    $JsonResp->disponible = 0;
    $JsonResp->money = 0.00;
    $JsonResp->redim_porc = 0.00;
    $JsonResp->success = "false";
}

$jsonData = json_encode($JsonResp);

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

@mysqli_close($connection);
#JOHNN GUTIERREZ
#ULTMA MODIFICASION 2021-07-29
#ULTMA MODIFICASION 2021-08-02
?>