<?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['tipo_p_id'] = $_POST['tipo_p_id'];
$_SESSION['id_forma'] = $_POST['id_forma'];
$_SESSION['op_id'] = $_POST['op_id'];
   
$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    ,
	tipo_p_id   ,   id_forma    ,   op_id) 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['tipo_p_id']."',
	'".$_SESSION['id_forma']."',
	'".$_SESSION['op_id']."')";

$upd_cont  =
"UPDATE ".$_SESSION['database'].".club_contratos SET
	monto_pag 		= (monto_pag   + ".$_SESSION['monto']."),
	semanas_pag		= (semanas_pag + ".$_SESSION['semanas']."),
	fecha_pag_hasta	= DATE_ADD(primer_sorteo, INTERVAL semanas_pag WEEK),
	monto_disp_ant 	= monto_disp,
	monto_disp 		= (monto_disp + ".$_SESSION['monto']."),
	monto_upago		= '".$_SESSION['monto']."',
	fecha_upago		= '".$_SESSION['fecha']."'
WHERE num_contr = '".$_SESSION['num_contr']."'";

$upd_pag  =
	"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_pag  = mysqli_query($connection, $ins_pagos);
//echo "sql: ".$ins_pagos;

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

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

if($resp_pag and $resp_con and $resp_pag2){
	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;
}

$procInput1= $_SESSION['num_contr']; 
$procInput2= "1";
$procInput3= "1";

$sql = "CALL ".$_SESSION['database'].".club_actualizar_pagos_retiros(?,?,?);";
$call = mysqli_prepare($connection, $sql);
mysqli_stmt_bind_param($call, 'sss', $procInput1, $procInput2, $procInput3);
mysqli_stmt_execute($call);

print_r(json_encode($msg));

@mysqli_close($connection);
?>
