<?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');
include_once('../../../php/maxExecTime.php');

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

switch ($_POST['action']) {
	case 'consultarPla':
		echo consultarPla();
		break;
	case 'consultarAnti':
		echo consultarAnti();
		break;
	case 'guardar':
		echo guardar();
		break;
	case 'borrar':
		echo borrar();
		break;

}

function consultarPla() {
	global $empCod, $usuNom;

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

	$func = new functions();

	$numNum=$_POST['numNum'];

	# QUERYS
	$sCtrpla = "SELECT *
				FROM ctrpla 
				WHERE ctrp_emp='$empCod' AND ctrp_num='$numNum'";

	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp["ctrpla"] = $db->sFetchAssoc($sCtrpla);
	$db->close();

	$func = new functions();
	return $func->jsonEncodeArray($resp, 'ctrpla');
}

function consultarAnti() {
	global $empCod, $usuNom;

	$func = new functions();

	$numNum=$_POST['numNum'];
	$numCod=$_POST['numCod'];

	# QUERYS
	$sMaestro = "SELECT *, TRIM(mae.mae_nom) AS mae_nom, TRIM(mae.mae_ape) AS mae_ape,
				TRIM(tpu.tpu_nom) AS tpu_nom
				FROM maestro mae 
 				LEFT JOIN tpuesto tpu ON mae.mae_tpue = tpu.tpu_cod 
				WHERE mae_emp='$empCod' AND mae_cod='$numCod'";

	$sAnti = "SELECT *, TRIM(mae.mae_nom) AS mae_nom, TRIM(mae.mae_ape) AS mae_ape,
				TRIM(tpu.tpu_nom) AS tpu_nom
				FROM antipla ant
 				LEFT JOIN maestro mae ON ant.ant_emp=mae.mae_emp AND ant.ant_cod = mae.mae_cod 
 				LEFT JOIN tpuesto tpu ON mae.mae_tpue = tpu.tpu_cod 
 				LEFT JOIN ctrpla cp ON ant.ant_emp=cp.ctrp_emp AND ant.ant_numpla = cp.ctrp_num  
				WHERE ant_emp='$empCod' AND ant_numpla='$numNum' AND ant_cod='$numCod'";

	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp = $db->sEx($sAnti, "antipla");
	$resp["maestro"] = $db->sFetchAssoc($sMaestro);
	$db->close();

	$func = new functions();
	return $func->jsonEncodeArray($resp, 'antipla', 'maestro');
}

function guardar() {
	global $empCod, $usuNom, $bitMod, $bitForm, $dtDel, $dtAl, $numNum, $numCod, $numAnti;

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

	$func = new functions();

	$numNum = utf8_decode($_POST["numNum"]);
	$numCod = $_POST["numCod"];
	$dtDel = $_POST["dtDel"];
	$dtAl = $_POST["dtAl"];
    $ano=date("Y", strtotime($dtAl));
    $mes=date("n", strtotime($dtAl));
	$numDtra = (!empty($_POST["numDtra"])) ? $_POST["numDtra"] : 0;
	$numAnti = (!empty($_POST["numAnti"])) ? $_POST["numAnti"] : 0;
	$diasantes = (!empty($_POST["diasantes"])) ? $_POST["diasantes"] : 0;
	$snomdia = (!empty($_POST["snomdia"])) ? $_POST["snomdia"] : 0;
	$snomdiab = (!empty($_POST["snomdiab"])) ? $_POST["snomdiab"] : 0;
	$igsssn = $_POST["igsssn"];

	# QUERYS PARA UN REGISTRO
	$iAnti = "INSERT INTO antipla (ant_emp, ant_numpla, ant_cod, ant_mes, ant_ano, ant_dtra, ant_sord, ant_liqrec) VALUES ('$empCod','$numNum','$numCod','$mes','$ano','$numDtra','$numAnti','$numAnti')";

	$uAnti = "UPDATE antipla SET ant_dtra='$numDtra', ant_sord='$numAnti', ant_liqrec='$numAnti' WHERE ant_emp='$empCod' AND ant_numpla='$numNum' AND ant_cod='$numCod'";

	$sNomper = "SELECT *
				FROM nomper
				WHERE np_emp ='$empCod' AND np_numpla='$numNum' AND np_cod='$numCod'";

	function uNomper($dsusi,$diastrab,$sord,$bonil,$totdev,$totdes,$igss,$liqrec) { 
		global $empCod, $usuNom, $dtDel, $dtAl, $numNum, $numCod, $numAnti;
		return "UPDATE nomper SET np_dtra=$diastrab,np_dsusi=$dsusi,np_sord=$sord,np_boni=$bonil,np_totdev=$totdev,np_igss=$igss,np_anti='$numAnti', np_totdes='$totdes', np_liqrec='$liqrec' WHERE np_emp='$empCod' AND np_numpla='$numNum' AND np_cod='$numCod'";
	}

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	$select["nomper"] = $db->query($sNomper);
	$fetchAssoc["nomper"] = $db->fetchAssoc($select["nomper"]);
    $dsusi=15-$numDtra;
    $dtrames=15+$numDtra;
    $sord=round(($snomdia*$dtrames),2);
    $bonil=round(($snomdiab*$dtrames),2);
    $sexts=$fetchAssoc["nomper"]["np_sexts"];
    $sextd=$fetchAssoc["nomper"]["np_sextd"];
    $bonii=$fetchAssoc["nomper"]["np_bonii"];
    $bonoe=$fetchAssoc["nomper"]["np_bonoe"];
    $depre=$fetchAssoc["nomper"]["np_depre"];
    $viati=$fetchAssoc["nomper"]["np_viati"];
    $comi=$fetchAssoc["nomper"]["np_comi"];
    $otrp=$fetchAssoc["nomper"]["np_otrp"];
    $totdev=$sord+$bonil+$sexts+$sextd+$bonii+$bonoe+$depre+$viati+$comi+$otrp;
	if ($igsssn=='S') {
		$igss=round(($sord+$sexts+$sextd+$comi+$otrp)*$_SESSION['pIgsse']/100,2);
	} else {
		$igss=0;	
	}
    $isr=$fetchAssoc["nomper"]["np_isr"];
    $pree=$fetchAssoc["nomper"]["np_pree"];
    $preb=$fetchAssoc["nomper"]["np_preb"];
    $unif=$fetchAssoc["nomper"]["np_unif"];
    $lent=$fetchAssoc["nomper"]["np_lent"];
    $desj=$fetchAssoc["nomper"]["np_desj"];
    $otrd=$fetchAssoc["nomper"]["np_otrd"];
    $totdes=$igss+$isr+$numAnti+$preb+$pree+$unif+$lent+$desj+$otrd;
    $liqrec=$totdev-$totdes;

	$db->uEx(uNomper($dsusi,$dtrames,$sord,$bonil,$totdev,$totdes,$igss,$liqrec));

	if ($func->stringToBool($_POST['nuevo'])) {
		$resp = $db->iEx($iAnti);
		$db->iEx($func->iBitacora(utf8_decode("Se creó Anticipo empleado, Código: $numCod  Planilla  $numNum  del $dtDel al $dtAl  Usuario: ").$usuNom, $bitMod, $bitForm));
	} else {
		$resp = $db->uEx($uAnti);
		$db->iEx($func->iBitacora(utf8_decode("Se modifico Anticipo empleado, Código: $numCod  Planilla $numNum  del $dtDel  al $dtAl Usuario: ").$usuNom, $bitMod, $bitForm));
	}

	$db->close();
	return json_encode($resp);
}

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

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

	$func = new functions();

	$numNum = utf8_decode($_POST["numNum"]);
	$numCod = $_POST["numCod"];

	# QUERYS PARA UN REGISTRO
	$dAnti = "DELETE FROM antipla WHERE ant_emp='$empCod' AND ant_cod='$numCod'";

	$sNomper = "SELECT *
				FROM nomper
				WHERE np_emp ='$empCod' AND np_numpla='$numNum' AND np_cod='$numCod'";

	function uNomper($totdes,$liqrec) { 
		global $empCod, $usuNom, $dtDel, $dtAl, $numNum, $numCod, $numAnti;
		return "UPDATE nomper SET np_anti=0, np_totdes='$totdes', np_liqrec='$liqrec' WHERE np_emp='$empCod' AND np_numpla='$numNum' AND np_cod='$numCod'";
	}

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	$select["nomper"] = $db->query($sNomper);
	$fetchAssoc["nomper"] = $db->fetchAssoc($select["nomper"]);
    $totdev=$fetchAssoc["nomper"]["np_totdev"];
    $igss=$fetchAssoc["nomper"]["np_igss"];
    $isr=$fetchAssoc["nomper"]["np_isr"];
    $pree=$fetchAssoc["nomper"]["np_pree"];
    $preb=$fetchAssoc["nomper"]["np_preb"];
    $unif=$fetchAssoc["nomper"]["np_unif"];
    $lent=$fetchAssoc["nomper"]["np_lent"];
    $desj=$fetchAssoc["nomper"]["np_desj"];
    $otrd=$fetchAssoc["nomper"]["np_otrd"];
    $totdes=$igss+$isr+$preb+$pree+$unif+$lent+$desj+$otrd;
    $liqrec=$totdev-$totdes;
	$db->uEx(uNomper($totdes,$liqrec));

	$resp = $db->dEx($dAnti);
	$db->iEx($func->iBitacora(utf8_decode("Se Borro el Anticipo empleado, Código: $numCod  Planilla $numNum   Usuario: ").$usuNom, $bitMod, $bitForm));

	$db->close();

	return json_encode($resp);
}

?>