<?php
include_once("utiles.php");

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

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

session_start();
$_SESSION['database'] = $_POST['database'];
$_SESSION['suc_id'] = $_POST['suc_id'];
$_SESSION['e_id'] = $_POST['e_id'];
$_SESSION['num_contr'] = $_POST['num_contr'];
$_SESSION['monto'] = $_POST['monto'];
$_SESSION['id_pago'] = $_POST['id_pago'];
$_SESSION['op_id'] = $_POST['op_id'];
$_SESSION['u_id'] = $_POST['u_id'];
 
$hoynow    = getdate();
$timeStamp = $hoynow["year"]."-".$hoynow["mon"]."-".$hoynow["mday"] ;

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

$Sql_Ln  =
"UPDATE ".$_SESSION['database'].".club_pagos SET
 	tipo_pago	  = 7,	
	autoriza	  = 1,
	anulado		  = 1,
	anulado_monto = monto,
	anulado_u_id  = '".$_SESSION['u_id']."',
	semanas       = 0.00,
	monto		  = 0.00
WHERE op_id = '".$_SESSION['op_id']."'";
$resp_pag01 = mysqli_query($connection, $Sql_Ln);

$Sql_Ln  =
"UPDATE ".$_SESSION['database'].".club_contratos
	SET monto_pag = (monto_pag   - ".$_SESSION['monto'].")
WHERE num_contr = '".$_SESSION['num_contr']."'";
$resp_cont01  = mysqli_query($connection, $Sql_Ln);

$Sql_Ln  =
"UPDATE ".$_SESSION['database'].".club_contratos
	SET semanas_pag	= (monto_pag / valor_club)
WHERE num_contr = '".$_SESSION['num_contr']."'";
$resp_cont02  = mysqli_query($connection, $Sql_Ln);

$Sql_Ln  =
"UPDATE ".$_SESSION['database'].".club_contratos SET
	fecha_pag_hasta	= DATE_ADD(primer_sorteo, INTERVAL (semanas_pag-1) WEEK),
	monto_disp_ant 	= monto_disp,
	monto_disp 		= (monto_disp - ".$_SESSION['monto'].")
WHERE num_contr = '".$_SESSION['num_contr']."'";
$resp_cont03  = mysqli_query($connection, $Sql_Ln);

$Sql_Ln  =
	"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']."'
	AND   p.op_id 	  = '".$_SESSION['op_id']."' ";
$resp_pag02  = mysqli_query($connection, $Sql_Ln);
	
if($resp_pag01 AND $resp_cont01 AND $resp_cont02 AND $resp_cont03 AND $resp_pag02){
	mysqli_query($connection, "COMMIT");
	
	$Sql_Ln  = "CALL ".$_SESSION['database'].".`club_actualizar_pagos_retiros`('".$_SESSION['num_contr']."',1,0)";
	$resp_pag03  = mysqli_query($connection, $Sql_Ln);

	$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);
/*
RCTECH SOFTWARE HOUSE
Johnn Gutierrez A.
Programador			 : Sistema de Club RCTECH
Fecha de Creación	 : 2018-11-07
Modificasiones		 : 1
Fecha de Modificacion: 2018-11-08
*/
?>
