<?php
include_once("../conn_obj/utiles.php");
include_once("../conn_obj/conn_param.php");

date_default_timezone_set("America/Panama");
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);

if (!$connection = connectDb()) {
	die("err db");
} else {
	mysqli_query($connection, "SET NAMES 'utf8'");
}
header('content-type: application/json');

session_start();
$_SESSION['db_name'] = $_POST['db_name'];
$_SESSION['cod_suc'] = $_POST['cod_suc'];
$_SESSION['cod_est'] = $_POST['cod_est'];
$_SESSION['cod_usu'] = $_POST['cod_usu'];
$_SESSION['tip_fac'] = $_POST['tip_fac'];
$_SESSION['opi_fac'] = $_POST['opi_fac'];
$_SESSION['tip_ncr'] = $_POST['tip_ncr'];
$_SESSION['opi_ncr'] = $_POST['opi_ncr'];
$_SESSION['mnt_ncr'] = $_POST['mnt_ncr'];

$cDbBame = $_SESSION['db_name'];
$Cod_Suc = $_SESSION['cod_suc'];
$Cod_Est = $_SESSION['cod_est'];
$Cod_Usu = $_SESSION['cod_usu'];
$Tip_Fac = $_SESSION['tip_fac'];
$OpI_Fac = $_SESSION['opi_fac'];
$Tip_Ncr = $_SESSION['tip_ncr'];
$OpI_Ncr = $_SESSION['opi_ncr'];
$Mnt_Ncr = $_SESSION['mnt_ncr'];

$hoynow = getdate();
$timeStamp = $hoynow["year"] . "-" . $hoynow["mon"] . "-" . $hoynow["mday"];
$bErrors = 0;
try {
	$SQL_Update = "UPDATE $cDbBame.operacion op SET op.saldo_act = -1*( abs(op.saldo_act)-abs($Mnt_Ncr)) WHERE (op.id_tipo='$Tip_Ncr') AND (op.op_id='$OpI_Ncr');";
	mysqli_query($connection, $SQL_Update);

	$SQL_Update = "UPDATE $cDbBame.operacion_cxc cx SET cx.op_abono = $Mnt_Ncr WHERE  (cx.id_tipo=21) AND (cx.op_sec_id='$OpI_Ncr');";
	mysqli_query($connection, $SQL_Update);

	$SQL_Update = "UPDATE $cDbBame.operacion_cxc cx SET cx.op_canc='false' WHERE  (cx.id_tipo=21) AND ( (abs(cx.op_debito) + abs(cx.op_credito)) = abs($Mnt_Ncr) ) AND (cx.op_sec_id='$OpI_Ncr');";
	mysqli_query($connection, $SQL_Update);
} catch (Exception  $e) {
	$bErrors = 1;
}

if ($bErrors == 0) {
	$msg = array("success" => "true", "detalle" => "OK");
} else {
	$msg = array("success" => "false", "detalle" => $conn);
	echo "Error: " . $SQL_Update . "<br>" . $conn->error;
}

print_r(json_encode($msg));

@mysqli_close($connection);
?>