<?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_pago_club"] = $_POST['no_pago_club'];
$_SESSION["pagos_trans"] = $_POST['pagos_trans'];

$Insert_Trans = $_SESSION["pagos_trans"];
  
$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) 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')";

$upd_conf  =
	"UPDATE ".$_SESSION['database'].".conf_sistema SET 
		no_pago_club = '".$_SESSION['no_pago_club']."'
	WHERE suc_id = '".$_SESSION['suc_id']."'
	AND   no_estacion	 = '".$_SESSION["e_id"]."'";
	
$upd_cont  =
	"UPDATE ".$_SESSION['database'].".club_contratos SET
		monto_pag 		= (monto_pag   + ".$_SESSION['monto']."),
		semanas_pag		= (semanas_pag + ".$_SESSION['semanas']."),
		fecha_pag_hasta	= 
		CASE   
		WHEN (DAYOFWEEK(fecha_compra)=1) AND (tipo_sorteo=6) AND (HOUR(fecha_compra)>13) THEN DATE_ADD(primer_sorteo, INTERVAL (semanas_pag) WEEK)  
		WHEN (DAYOFWEEK(fecha_compra)=4) AND (tipo_sorteo=2) AND (HOUR(fecha_compra)>13) THEN DATE_ADD(primer_sorteo, INTERVAL (semanas_pag) WEEK)  
		ELSE 
			DATE_ADD(primer_sorteo, INTERVAL (semanas_pag-1) WEEK)   
		END,
		monto_disp_ant 	= monto_disp,
		monto_disp 		= (monto_disp + ".$_SESSION['monto']."),
		monto_upago		= '".$_SESSION['monto']."',
		fecha_upago		= ".$_SESSION['fecha'].",
		tipo_status		= IF(tipo_status='10','1','10')
	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']."'
	AND   p.op_id = '".$_SESSION['op_id']."'";

$resp_pagos  = mysqli_query($connection, $ins_pagos);
//echo "sql: ".$ins_pagos."\n";

$resp_conf  = mysqli_query($connection, $upd_conf);
//echo "sql: ".$upd_conf."\n";

$resp_cont  = mysqli_query($connection, $upd_cont);
//echo "sql: ".$upd_cont."\n";

$resp_pag  = mysqli_query($connection, $upd_pag);
//echo "sql: ".$upd_pag."\n";

$array = explode(';',$Insert_Trans,0);
foreach( $array as $value ) {
	$resp_ins  = mysqli_query($connection, $value);
	//echo "sql: ".$value."\n";
};

if($resp_pagos and $resp_conf and $resp_cont and $resp_pag){
	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);
?>
