<?php
include_once('../../../php/sessionStart.php');
include_once('../../../php/db/dbExec.php');
include_once('../../../php/functions.php');
include_once('../../../php/timezone.php');
include_once('../../../php/privileges.php');

$bitForm = "cnt4070";
$bitMod = "cnt";
$empCod = $_SESSION['empCod'];
$usuCod = utf8_decode($_SESSION['usuCod']);
$usuNom = utf8_decode($_SESSION['usuNom']);

switch ($_POST['action']) {
	case 'consultar':
		echo consultar();
		break;

	case 'guardar':
		echo guardar();
		break;

	case 'borrar':
		echo borrar();
		break;
}

function consultar() {
	global $empCod;

	$priv = new privileges();
	if (!$priv->consult($_SESSION['usuPriv'])) {
		return json_encode($priv->rErrorConsult());
	}

	$func = new functions();

	$numTipoPol=$_POST['numTipoPol'];

	# QUERYS
	$sTipoPol = "SELECT tp.*,
				TRIM(tp.tp_nom) AS tp_nom, 
				TRIM(tp.tp_Abrev) AS tp_Abrev,
				cpol.cp_num   AS tp_corr
				FROM tipopol AS tp
				LEFT JOIN corrpol cpol ON cpol.cp_emp = '$empCod' AND cpol.cp_tipo=tp.tp_cod
				WHERE tp.tp_cod='$numTipoPol'";
	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp = $db->sEx($sTipoPol, "tipopol");
	$db->close();

	$func = new functions();
	return $func->jsonEncodeArray($resp, "tipopol");
}


function guardar() {
	global $empCod, $usuNom, $bitMod, $bitForm;

	$priv = new privileges();
	if (!$priv->modify($_SESSION['usuPriv'])) {
		return json_encode($priv->rErrorModify());
	}

	$func = new functions();

	$numTipoPol=$_POST['numTipoPol'];
	$txtNom=utf8_decode($_POST['txtNom']);
	$txtAbrev=utf8_decode($_POST['txtAbrev']);
	$numCorr=utf8_decode($_POST['numCorr']);
	if ($func->stringToBool($_POST['crBansn1'])) {
		$crBansn="S";
		} else {
		$crBansn="N";
	}

	# QUERYS PARA UN REGISTRO
	$iTipoPol = "INSERT INTO tipopol(tp_cod, tp_nom, tp_abrev, tp_ulcam,tp_bansn) VALUES ('$numTipoPol','$txtNom','$txtAbrev','$usuNom','$crBansn')";

	$uTipoPol = "UPDATE tipopol SET tp_nom='$txtNom',tp_abrev='$txtAbrev',tp_ulcam='$usuNom',tp_bansn='$crBansn' WHERE tp_cod='$numTipoPol'";

	$iCorrPol = "INSERT INTO corrpol(cp_emp, cp_tipo, cp_num) VALUES ('$empCod','$numTipoPol','$numCorr')";

	$uCorrPol = "UPDATE corrpol SET cp_num='$numCorr' WHERE cp_emp='$empCod' AND cp_tipo='$numTipoPol'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();

	if ($func->stringToBool($_POST['nuevo'])) {
		$resp = $db->iEx($iTipoPol);
		$resp = $db->iEx($iCorrPol);
		$db->iEx($func->iBitacora(utf8_decode("Se creó Tipo de Poliza, Numero: $numTipoPol Usuario: ").$usuNom, $bitMod, $bitForm));
	} else {
		$resp = $db->uEx($uTipoPol);
  		$resp = $db->iuEx($iCorrPol,$uCorrPol);
		$db->iEx($func->iBitacora(utf8_decode("Se modificó Tipo de Poliza, Numero: $numTipoPol Usuario: ").$usuNom, $bitMod, $bitForm));
	}
	
	$db->close();

	return json_encode($resp);
}

function borrar() {
	global $empCod, $usuNom, $bitMod, $bitForm;

	$func = new functions();

	$numTipoPol=$_POST['numTipoPol'];

	# QUERYS
	$dTipoPol = "DELETE FROM tipopol WHERE tp_cod='$numTipoPol'";
	$dCorrPol = "DELETE FROM corrpol WHERE cp_emp='$empCod' AND cp_tipo='$numTipoPol'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	$resp = $db->dEx($dTipoPol);
	$resp = $db->dEx($dCorrPol);

	$db->iEx($func->iBitacora(utf8_decode("Se borró Tipo de Poliza, Numero: $numTipoPol Usuario: ").$usuNom, $bitMod, $bitForm));
	$db->close();

	return json_encode($resp);
}

?>