<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
//error_reporting(E_ALL);
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
include_once("utiles.php");
include_once("cxc_funciones.php");


date_default_timezone_set("America/Panama");

if (!$connection = connectDb()) {
    die("err db");
} else {
    mysqli_query($connection, "SET NAMES 'utf8';");
}

session_start();
$_SESSION['database'] = $_POST['database'];
$_SESSION['json_data'] = $_POST['json_data'];
$_SESSION["op__id"] = $_POST['op__id'];
$_SESSION["cte_id"] = $_POST['cte_id'];
$_SESSION["op__cxc"] = $_POST['op__cxc'];
$_SESSION["suc_id"] = $_POST['suc_id'];
$_SESSION["id_stat"] = $_POST['id_stat'];
$_SESSION['swsavelog'] = $_POST['swsavelog'];

$cDb_Nam = $_SESSION["database"];
$JsonDat = $_SESSION["json_data"];
$cSuc_id = $_SESSION["suc_id"];
$cOp__id = $_SESSION["op__id"];
$cCte_id = $_SESSION["cte_id"];
$cOp_cxc = $_SESSION["op__cxc"];
$id_stat = $_SESSION['id_stat'];
$SaveLog = $_SESSION['swsavelog'];

$SW_LOGFILE = $SaveLog;

$hoynow = getdate();
$timeStamp = $hoynow["year"] . "-" . $hoynow["mon"] . "-" . $hoynow["mday"];
$timeStamp = date("Y-m-d H:i:s");

if ($SaveLog == 1) {
    echo '++++++++++++++++++++++++++++++++++++++++++++++insert_json_op_gen.php++++++++++++++++++++++++++++++++++++++++++';
    echo 'database : ' . $cDb_Nam;
    echo 'Sucursal : ' . $cSuc_id;
    echo 'Tbl_OpId : ' . $cOp__id;
    echo 'Cxc_OpId : ' . $cOp_cxc;
    echo 'json_data: ' . $JsonDat;
    echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++';
}

mysqli_select_db($connection, $cDb_Nam);

// $connection->autocommit(false);

$Saldo_Ant = Gen_Saldo_cxc($connection, $cDb_Nam, $cSuc_id, $cOp__id, $cOp_cxc, $SaveLog);
$rest1 = jsonmysql($connection, $cDb_Nam, $JsonDat, 'operacion_cxc', $SaveLog);
$Saldo_Act = Gen_Saldo_cxc($connection, $cDb_Nam, $cSuc_id, $cOp__id, $cOp_cxc, $SaveLog);

if ($rest1 == TRUE) {
    $result = true;
    $msg = array(
        "success" => "true",
        "detalle" => "OK",
        "saldo_ant" => $Saldo_Ant,
        "saldo_abn" => $Monto_Abn,
        "saldo_act" => $Saldo_Act

    );
} else {
    $result = false;
    $msg = array(
        "success" => "false",
        "detalle" => $connection,
        "saldo_ant" => 0.00,
        "saldo_abn" => 0.00,
        "saldo_act" => 0.00
    );
    echo "<br>" . $connection->error;
}

if ($SaveLog == 1) {
    echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++';
    echo 'RESP: ' . ($result ? 'true' : 'false');
    echo '++++++++++++++++++++++++++++++++++++++++++++++insert_json_op_gen.php++++++++++++++++++++++++++++++++++++++++++';
}

$jsonData[] = $msg;

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

@mysqli_close($connection);
// CREADO POR: JOHNN GUTIERREZ 2021-02-17
// ULTIMA MODIFICACION: 2021-02-17
?>