<?php
class class_GlobalEvents extends GlobalEventsBase
{
	function init() {
		$this->events = array(
	'pageEvents' => array(
		'AfterAppInit' => true,
		'AfterSuccessfulLogin' => true 
	),
	'onScreenEvents' => array(
		'46012' => true,
		'46013' => true,
		'46016' => true,
		'46017' => true,
		'46018' => true,
		'46019' => true 
	),
	'dashboardEvents' => array(
		 
	),
	'buttons' => array(
		'46007' => true,
		'46008' => true,
		'46009' => true,
		'46010' => true,
		'46011' => true,
		'46014' => true,
		'46015' => true,
		'46020' => true,
		'46021' => true,
		'46022' => true,
		'46023' => true,
		'46024' => true,
		'46025' => true,
		'46026' => true,
		'46027' => true,
		'46028' => true,
		'46029' => true,
		'46030' => true,
		'46031' => true,
		'46032' => true,
		'46033' => true,
		'46034' => true,
		'46035' => true,
		'46036' => true,
		'46037' => true,
		'46038' => true,
		'46039' => true,
		'52167' => true,
		'52202' => true 
	),
	'maps' => array(
		 
	),
	'tablePermissions' => array(
		 
	),
	'recordEditable' => array(
		 
	) 
);
	}

		function AfterAppInit(  ) {
		// Poner zona horaria distinta al servidor
$now = date('d-m-Y H:i:s');
$fecha = new DateTime($now, new DateTimeZone('America/La_Paz')); 
$timezone = $fecha->format('P');  // "+01:00" para Madrid
// MYSQL ---------------------------------------------------------------
DB::Exec("SET time_zone = '$timezone'"); // 
// PHP  ----------------------------------------------------------------
date_default_timezone_set('America/La_Paz'); // Madrid
		;
		
	}

	function AfterSuccessfulLogin( $username, $password, &$data, $pageObject ) {
		$_SESSION['user_id']=$data['ID']; 


// Place event code here.
// Use "Add Action" button to add code snippets.
		;
		
	}
	
	// custom buttons 
		// custom button - pdf
	function buttonHandler_46007( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		$data = $button->getCurrentRecord();
$result['record'] = $data;
$_SESSION['id_derecho_Propietario'] = $data["id_derecho_Propietario"];;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - Certificado_taco
	function buttonHandler_46008( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		$data = $button->getCurrentRecord();
$result['record'] = $data;
$_SESSION['id_derecho_Propietario'] = $data["id_derecho_Propietario"];;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - Nota_Entrega
	function buttonHandler_46009( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		$data = $button->getCurrentRecord();
$result['record'] = $data;
$_SESSION['id_Entrega'] = $data["id_Entrega"];;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - pdf1
	function buttonHandler_46010( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		$data = $button->getCurrentRecord();
$result['record'] = $data;
$_SESSION['id_derecho_Propietario'] = $data["id_derecho_Propietario"];;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - Nota_de_Pago
	function buttonHandler_46011( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		$data = $button->getCurrentRecord();
$result['record'] = $data;
$_SESSION['id_Pago'] = $data["id_Pago"];;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - Entrega_de_titulo
	function buttonHandler_46020( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - nuevo
	function buttonHandler_46021( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - nuevo_pago
	function buttonHandler_46022( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - nuevo_pago_biometrico
	function buttonHandler_46023( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - Reporte_de_pago
	function buttonHandler_46024( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - Ver_kardex
	function buttonHandler_46025( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		/*$data = $button->getCurrentRecord();
$result['record'] = $data;
$_SESSION['id_Socio'] = $data["id_Socio"];*/

$record = $button->getCurrentRecord();
$result["id_Socio"] = $record["id_Socio"];;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - titulo_pre_venta
	function buttonHandler_46026( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		/*$data = $button->getCurrentRecord();
$result['record'] = $data;
$_SESSION['id_derecho_Propietario'] = $data["id_derecho_Propietario"];*/
$record = $button->getCurrentRecord();
$result["id_derecho_Propietario"] = $record["id_derecho_Propietario"];;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - recibo_cobro_pre
	function buttonHandler_46027( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		/*$data = $button->getCurrentRecord();
$result['record'] = $data;
$_SESSION['id_derecho_Propietario'] = $data["id_derecho_Propietario"];*/

$record = $button->getCurrentRecord();
$result["id_derecho_Propietario"] = $record["id_derecho_Propietario"];;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - Reporte_Preventa
	function buttonHandler_46028( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - nuevo_preventa
	function buttonHandler_46029( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - Pendientes__pago
	function buttonHandler_46030( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		$data = $button->getCurrentRecord();
/*$result['record'] = $data;*/


$result['id_Gestion'] = $data["id_Gestion"];
$result['id_constitucion'] = $data["id_constitucion"];

global $conn2;





$sqlupdate= "
UPDATE gestion SET estado_pago = '1' WHERE

 id_Gestion = '".$result['id_Gestion']."'

"        ;
db_exec ($sqlupdate,$conn2);









global $conn;




$sql= "INSERT INTO pago_dp (fecha_Pago, derecho_Propietario, accionista,  estado, tipo_pago, 
 fecha_creacion, total_bs, nro_Acciones, utilidad_gestion, id_constitucion, gestion, correlativo,
 ci, nombre, paterno, materno,casado , procedencia, complemento , nro_derecho ) 


SELECT
	pp.fechaPago,
	pp.id_derecho_Propietario,
	pp.accionista,
	pp.estado,
	pp.tipo_pago,
	pp.fecha_creacion,
	pp.total_bs,
	pp.nro_Acciones,
	pp.utilidad_gestion,
	pp.id_constitucion,
	pp.id_Gestion,
	pp.correlativo,
	s.ci,
	s.nombres,
	s.apellido_Paterno,
	s.apellido_Materno,
	s.apellido_Casada,
	s.ci_procedencia,
	s.ci_complemento,
	dp.nro_derecho 
FROM
	pendientes_pago AS pp
	INNER JOIN socio AS s ON pp.accionista = s.id_Socio
	INNER JOIN derecho_propietario AS dp ON pp.id_derecho_Propietario = dp.id_derecho_Propietario


	 where   pp.id_Gestion = '".$result['id_Gestion']."'

and 
	pp.id_constitucion = '".$result['id_constitucion']."'   "        ;
db_exec ($sql,$conn);;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - nota_pago_pendiente
	function buttonHandler_46031( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		$data = $button->getCurrentRecord();
$result['record'] = $data;
$_SESSION['id_Pago'] = $data["id_Pago"];;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - lista_derecho_propietario
	function buttonHandler_46032( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		$record = $button->getCurrentRecord();
$result["id_constitucion"] = $record["id_constitucion"];;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - vigentes_excel
	function buttonHandler_46033( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		$record = $button->getCurrentRecord();
$result["id_constitucion"] = $record["id_constitucion"];;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - planilla_pago_pdf
	function buttonHandler_46034( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		$record = $button->getCurrentRecord();
$result["id_Gestion"] = $record["id_Gestion"];;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - planilla_pago_excel
	function buttonHandler_46035( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		$record = $button->getCurrentRecord();
$result["id_Gestion"] = $record["id_Gestion"];;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - api
	function buttonHandler_46036( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		// Conectar a la base de datos MySQL
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "titulo";

// Crear conexión
$conn = new mysqli($servername, $username, $password, $dbname);

// Verificar la conexión
if ($conn->connect_error) {
  die("Error de conexión: " . $conn->connect_error);
}

// URL de la API REST
$url = "http://localhost/biometrico/api/v1.php?table=socio&action=list&records=1";

// Realizar la solicitud GET a la API
$response = file_get_contents($url);

// Verificar si la solicitud fue exitosa
if ($response !== false) {
  // Decodificar la respuesta JSON
  $data = json_decode($response, true);

  // Verificar si la respuesta incluye datos
  if (isset($data['data'])) {
    // Iterar sobre los datos y guardarlos en la base de datos
    foreach ($data['data'] as $item) {
      $empleadoID = $item['empleadoID'];
      $fechahora = $item['fechahora'];
      $nombre = $item['nombre'];
		   $fecha = $item['fecha'];
       $hora = $item['hora'];
        $descrpcion = $item['descrpcion'];
        $nombredis = $item['nombredis'];
        $sndis = $item['sndis'];
        $ci = $item['ci'];



      // Preparar la consulta SQL para insertar los datos
      $sql = "INSERT INTO tickeo (empleadoID, fechahora, nombre ,fecha , hora ,  descrpcion ,  nombredis , sndis ,  ci  ) 
              VALUES ('$empleadoID', '$fechahora', '$nombre',   '$fecha' ,  '$hora' , '$descrpcion',  '$nombredis',  '$sndis',  '$ci' )";

      


      // Ejecutar la consulta SQL
      if ($conn->query($sql) === TRUE)

{
       // echo "Datos insertados correctamente";
      } else 

 {
        echo "Error al insertar datos: " . $conn->error;
      }
    }
  } else {
    echo "La respuesta no incluye datos";
  }
} else {
  echo "Error al realizar la solicitud a la API";
}

// Cerrar la conexión
$conn->close();;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - Resumen_de_Pagos_Pendiente
	function buttonHandler_46037( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - Detalle_Pagos_Pendientes
	function buttonHandler_46038( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - Detalle_de_Pagos_Pendientes
	function buttonHandler_46039( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - Devolver
	function buttonHandler_52167( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		$data = $button->getCurrentRecord();
/*$result['record'] = $data;*/


$result['id_derecho_Propietario'] = $data["id_derecho_Propietario"];

$result['responsable'] = $data["responsable"];
$result['nro_Acciones_2'] = $data["nro_Acciones_2"];


global $conn2;

$sqlupdate= "
UPDATE derecho_propietario_2 SET 
nro_Titulo =  '".$result['nro_Acciones_2']."' ,
nro_Acciones_2 = '0'  ,
responsable_devolucion  = '".$_SESSION['user_id']."',
fecha_devolucion  = now(),
estado_titulo = '67'  
WHERE  id_derecho_Propietario = '".$result['id_derecho_Propietario']."'
"        ;

db_exec ($sqlupdate,$conn2);;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

		// custom button - recibo_devoluvion_Pre
	function buttonHandler_52202( $_params )
	{
		$result = array();

		$button = $this->prepareButtonContext( $_params );
		$ajax = $button; 
		$keys = $button->getKeys();

		// .NET naming
		$parameters = $_params;
		$params = &$_params;

		$data = $button->getCurrentRecord();
$result['record'] = $data;
$_SESSION['id_derecho_Propietario'] = $data["id_derecho_Propietario"];;

		RunnerContext::pop();
		echo runner_json_encode( $result);
		$button->deleteTempFiles();
	}

	
	// ajax code snippets
	
	
	// field events
	

	// code snippets
	









	// code snippet - Socios
	function event_46017(  ) {
		
		$res=DB::query(" 

SELECT
	
	id_constitucion as constitucion,
	count(nro_Acciones) AS Nro_titulos
FROM
	derecho_propietario
	
	where 	estado_titulo = 37 and id_constitucion = 2
	
GROUP BY
	id_constitucion

 ");
$data = $res->fetchAssoc();
$res = "<div class='info-box'>";
$res.= "<span class='info-img img_bgcolor_green glyphicon glyphicon-book'></span>";
$res.= "<div class='dashtext'>";
$res.= "<b>Derechos Propietarios Activos</b><br>";
$res.= $data["Nro_titulos"];
$res.= "</div>";
$res.= "</div>";
echo $res;


		;
		
	}


	// code snippet - Bs
	function event_46019(  ) {
		
		$res=DB::query(" SELECT
	
	id_constitucion as constitucion,
	sum(nro_Acciones) AS Nro_accionesb
FROM
	derecho_propietario
	
	where 	estado_titulo = 37 and id_constitucion = 2
	
GROUP BY
	id_constitucion");
$data = $res->fetchAssoc();
$res = "<div class='info-box'>";
$res.= "<span class='info-img img_bgcolor_green glyphicon glyphicon-piggy-bank'></span>";
$res.= "<div class='dashtext'>";
$res.= "<b>Nro acciones Asignadas</b><br>";

$res.= number_format( $data["Nro_accionesb"],2);


$res.= "</div>";
$res.= "</div>";

echo  $res









		;
		
	}























}
?>
