<?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["monto"] = $_POST['monto'];
$_SESSION["id_pago"] = $_POST['id_pago'];
$_SESSION["u_id"] = $_POST['u_id'];
$_SESSION["num_contr"] = $_POST['num_contr'];
   
$hoynow    = getdate();
$timeStamp = $hoynow["year"]."-".$hoynow["mon"]."-".$hoynow["mday"] ;

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

$upd_pag  =
"UPDATE ".$_SESSION['database'].".club_pagos SET
	tipo_pago	  = 7,	
	autoriza	  = 1,
	anulado		  = 1,
	anulado_monto = monto,
	anulado_u_id  = '".$_SESSION['u_id']."',
	monto		  = 0.00
WHERE autoinc = '".$_SESSION['id_pago']."'";

$upd_cont1  =
"UPDATE ".$_SESSION['database'].".club_contratos SET
	monto_pag 		= (monto_pag   - ".$_SESSION['monto']."),
	semanas_pag		= (monto_pag / valor_club),
	fecha_pag_hasta	= DATE_ADD(primer_sorteo, INTERVAL semanas_pag WEEK),
	monto_disp_ant 	= (monto_disp_ant - ".$_SESSION['monto']."),
	monto_disp 		= (monto_disp - ".$_SESSION['monto'].")
WHERE num_contr = '".$_SESSION['num_contr']."'";

$upd_cont2  =
	"UPDATE ".$_SESSION['database'].".club_pagos p
	INNER JOIN ".$_SESSION['database'].".club_contratos c ON (p.num_contr = c.num_contr)
		SET p.pagado_hasta = c.fecha_pag_hasta
	WHERE c.num_contr = '".$_SESSION['num_contr']."'";

$resp_pag1  = mysqli_query($connection, $upd_pag);
//echo "sql: ".$upd_pag;

$resp_pag2  = mysqli_query($connection, $upd_cont1);
//echo "sql: ".$upd_cont1;

$resp_pag3  = mysqli_query($connection, $upd_cont2);
//echo "sql: ".$upd_cont2;

if($resp_pag1 and $resp_pag2 and $resp_pag3){
	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);
?>
