<?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 ("apt_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["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"];
$cOp_cxc = $_SESSION["op__cxc"];
$id_stat = $_SESSION['id_stat'];
$SaveLog = $_SESSION['swsavelog'];

$SW_LOGFILE=$SaveLog;
/*
$cDb_Nam = 'ocadb';
$cOp_cxc = '5570000012';
$cOp__id = '557000011';
$cSuc_id = '5';
$id_stat = '2';
$SaveLog = '1';
$JsonDat = '{"regs_cnt":"1","operacion_cxc":[{"autoin":"5570000012","op_fechae":"2017-11-17 00:00:00","id_tipo":"25","op_id":"PA000012","c_id":"11-22-33","cxc_detalle":"Abono a apartado # 55700007 FORMAS DE PAGO:   EF $1.00 ","op_fechav":"2017-11-17","op_debito":"0.000000000","op_credito":"1.000000000","op_saldo":"0.000000000","op_abono":"0.000000000","op_canc":"true","op_apagar":"0.000000000","op_estado":"Pendiente","op_tipodoc":"P/A","u_id":"1","op_sec_id":"","activado":"0","autoinc_banco":"0","op_neto":"1.000000000","suc_id":"5","conc_id":"0","op_signo":"-1","op_fechac":"2017-11-01 00:00:00","op_contab":"0","op_base":"1.000000000","op_itbms":"0.00","pers_id":"0","op_orig":"2","op_rel":"557000011","op_num_doc":"PA000012","op_num_fact":"PA000012","op_fecha_capt":"2017-11-17 08:21:12","op_num_docfis":"","op_recarg":"","mon_op":"01","mon_taza_op":"1.000000000","op_retenc":"0.00","id_retenc":"","mon_id1":"01","mon_taza_v1":"1.000000000","mon_taza_c1":"1.000000000","mon_id2":"01","mon_taza_v2":"1.000000000","mon_taza_c2":"1.000000000","mon_id3":"01","mon_taza_v3":"1.000000000","mon_taza_c3":"1.000000000","op_info":"","op_aptdo":"1","op_visible":"1","syncprocesing":"0","usermodify":"1","useroption":"1"}]}';
*/
    
$hoynow = getdate();
$timeStamp = $hoynow["year"] . "-" . $hoynow["mon"] . "-" . $hoynow["mday"];
$timeStamp = date("Y-m-d H:i:s");

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

mysqli_select_db($connection, $cDb_Nam);

// $connection->autocommit(false);

$Saldo_Ant = Gen_Saldo_Actual_Aptdo($connection, $cDb_Nam, $cSuc_id, $cOp__id, $cOp_cxc, $SaveLog);
    
$rest1 = jsonmysql($connection, $cDb_Nam, $JsonDat, 'operacion_cxc', $SaveLog);
$Monto_Abn = Gen_Monto_Abonos_Aptdo($connection, $cDb_Nam, $cSuc_id, $cOp__id, $SaveLog);

$Saldo_Act = Gen_Saldo_Actual_Aptdo($connection, $cDb_Nam, $cSuc_id, $cOp__id, $cOp_cxc, $SaveLog);

$rest2 = Actualiza_saldos_Aptdo($connection, $cDb_Nam, $cSuc_id, $cOp__id, $id_stat, $Saldo_Ant, $Monto_Abn, $Saldo_Act, $SaveLog);
if ($SaveLog == 1) {
    logs2('*FIN------------------------------------------------------------*');
    logs2('--->RESP1:' . $rest1);
    logs2('--->RESP2:' . $rest2);
    logs2('*FIN----------------------->Act_saldos--------------------------*');
    logs2('', false);
}

if (($rest1 == TRUE) and ($rest2 == 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) {
    logs2('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
    logs2('RESP: ' . ($result ? 'true' : 'false'));
    logs2('++++++++++++++++++++++++++++++++++++++++++++++insert_json_op_gen.php++++++++++++++++++++++++++++++++++++++++++');
    logs2('', false);
}

$jsonData[] = $msg;

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

@mysqli_close($connection);
?>