<?php
error_reporting(E_ALL & ~ E_NOTICE & ~ E_WARNING);
include_once ("utiles.php");
include_once ("apt_funciones.php");

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

$_SESSION['dbname'] = $_POST['database'];
$_SESSION['suc_id'] = $_POST['suc_id'];
$_SESSION['est_id'] = $_POST['est_id'];
$_SESSION['Json'] = $_POST['datos'];
$_SESSION['swsavelog'] = $_POST['swsavelog'];

$cDb_Nam = $_SESSION['dbname'];
$cSuc_id = $_SESSION['suc_id'];
$cEst_id = $_SESSION['est_id'];
$jsonStr = $_SESSION['Json'];
$SaveLog = $_SESSION['swsavelog'];

if ($SaveLog == '1') {
    logs2('++++++++++++++++++++++++++++++++++++++++++apt_subir_op.php+++++++++++++++++++++++++++++++++++++++++++++++++');
    logs2('database : ' . $cDb_Nam);
    logs2('Sucursal : ' . $cSuc_id);
    logs2('Estacion : ' . $cEst_id);
    // logs2('Json : ' . $jsonStr);
    logs2('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
}

$reg1 = json_decode($jsonStr, TRUE);

$TablaOp = $reg1['operacion'];
$TablaPo = $reg1['producto_op'];
$TablaDp = $reg1['operacion_det_pago'];
$TablaCx = $reg1['operacion_cxc'];
//$TablaCt = $reg1['clientes'];

$reg_op = $reg1['regs_op'];
$reg_po = $reg1['regs_po'];
$reg_fp = $reg1['regs_fp'];
$reg_cx = $reg1['regs_cx'];
//$reg_ct = $reg1['regs_ct'];

$paso1 = jsonmysql2($TablaOp, $reg_op, 'operacion', $connection, $cDb_Nam, $SaveLog);
$paso2 = jsonmysql2($TablaPo, $reg_po, 'producto_op', $connection, $cDb_Nam, $SaveLog);
$paso3 = jsonmysql2($TablaDp, $reg_fp, 'operacion_det_pago', $connection, $cDb_Nam, $SaveLog);
$paso4 = jsonmysql2($TablaCx, $reg_cx, 'operacion_cxc', $connection, $cDb_Nam, $SaveLog);
//$paso5 = jsonmysql2($TablaCt, $reg_ct, 'clientes', $connection, $cDb_Nam, $SaveLog);

if ($SaveLog == '1') {
    logs2('++++++++++++++++++++++++++++++++++++++++++apt_subir_op.php+++++++++++++++++++++++++++++++++++++++++++++++++');
}

if (($paso1 == true) and ($paso2 == true)) {
    $msg = array(
        "success" => 'true',
        "mensaje" => 'Procesado correctamente'
    );
} else {
    $msg = array(
        "success" => 'false',
        "mensaje" => 'Error de procesamiento'
    );
}
;

$jsonData[] = $msg;

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

@mysqli_close($connection);
?>