<?php
include_once ("conn.php");
include_once ("funciones.php");

error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING );

date_default_timezone_set("America/Panama");

session_start();

$_SESSION['database'] = $_POST['database'];
$_SESSION['suc_id'] = $_POST['suc_id'];
$_SESSION['e_id'] = $_POST['e_id'];
$_SESSION['c_id'] = $_POST['c_id'];
$_SESSION['num_contr'] = $_POST['num_contr'];
$_SESSION['fecha'] = $_POST['fecha'];
$_SESSION['semanas'] = $_POST['semanas'];
$_SESSION['monto'] = $_POST['monto'];
$_SESSION['tipo_pago'] = $_POST['tipo_pago'];
$_SESSION['det_pago'] = $_POST['det_pago'];
$_SESSION['ci_op_idx'] = $_POST['ci_op_idx'];
$_SESSION['ci_op_idz'] = $_POST['ci_op_idz'];
$_SESSION['u_id'] = $_POST['u_id'];
$_SESSION['op_id'] = $_POST['op_id'];
$_SESSION['no_retiro_club'] = $_POST['no_retiro_club'];
$_SESSION['tipo_p_id'] = $_POST['tipo_p_id'];
$_SESSION['id_forma'] = $_POST['id_forma'];

$hoynow    = getdate();
$timeStamp = $hoynow["year"]."-".$hoynow["mon"]."-".$hoynow["mday"] ;

mysqli_query($connection, "SET AUTOCOMMIT=0");
mysqli_query($connection, "START TRANSACTION");

$ins_pagos = "INSERT INTO ".$_SESSION['database'].".club_pagos (
	num_contr	 ,	fecha		 ,	semanas		,
	monto		 ,	e_id		 ,	useroption	,
	suc_id		 ,	tipo_pago	 ,	c_id		,
	det_pago	 ,	ci_op_idx	 ,	ci_op_idz	,
	u_id		 ,	signo		 ,	autoriza    ,
	op_id		 ,	autoriza_memo,	anulado		,
	anulado_monto,	pagado_hasta ,
	tipo_p_id    , id_forma) VALUES (
	".$_SESSION['num_contr'].",
	".$_SESSION['fecha'].",
	".$_SESSION['semanas'].",
	".$_SESSION['monto'].",
	".$_SESSION['e_id'].",1,
	".$_SESSION['suc_id'].",
	".$_SESSION['tipo_pago'].",
	".$_SESSION['c_id'].",
	".$_SESSION['det_pago'].",
	".$_SESSION['ci_op_idx'].",
	".$_SESSION['ci_op_idz'].",
	".$_SESSION['u_id'].",-1,0,
    ".$_SESSION['op_id'].",'','0',
    '0.00','".$timeStamp."',
	".$_SESSION['tipo_p_id'].",
	".$_SESSION['id_forma'].")";

$upd_conf  =
"UPDATE ".$_SESSION['database'].".conf_sistema SET
		no_retiro_club = ".$_SESSION['no_retiro_club']."
	WHERE suc_id = ".$_SESSION['suc_id']."
	AND   no_estacion	 = ".$_SESSION["e_id"];

$upd_cont = "UPDATE ".$_SESSION['database'].".club_contratos SET
	monto_retiro 	= (monto_retiro   + ".$_SESSION['monto']."),
	monto_disp_ant 	= monto_disp,
	monto_disp 		= (monto_disp - ".$_SESSION['monto']."),
	monto_uretiro	= ".$_SESSION['monto'].",
	fecha_uretiro	= ".$_SESSION['fecha']."
WHERE num_contr = ".$_SESSION['num_contr'];

$resp_pag = mysqli_query($connection, $ins_pagos);
 //echo "sql: ".$ins_pagos;

$resp_con = mysqli_query($connection, $upd_conf);
// echo "sql: ".$upd_conf;

$resp_con = mysqli_query($connection, $upd_cont);
// echo "sql: ".$upd_cont;

if ($resp_pag and $resp_con and $resp_con) {
    mysqli_query($connection, "COMMIT");
    $msg = array(
        "success" => "true",
        "detalle" => "OK"
    );
} else {
    mysqli_query($connection, "ROLLBACK");
    $msg = array(
        "success" => "false",
        "detalle" => $connection
    );
    echo "Error: " . $connection->error;
}

print_r(json_encode($msg));

@mysqli_close($connection);
?>
