<?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 = "nom3110";
$bitMod = "nom";
$empCod = $_SESSION['empCod'];
$usuCod = utf8_decode($_SESSION['usuCod']);
$usuNom = utf8_decode($_SESSION['usuNom']);

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

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

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


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

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

	$func = new functions();

	$numAno=$_POST['numAno'];
	$anoant=$numAno-1;
	$fecini1=$anoant."-12-01";
	$fecfin1=$numAno."-11-30";
	$fini = date("Y-m-d", strtotime($fecini1));
	$ffin = date("Y-m-d", strtotime($fecfin1));

	# QUERYS PARA UN REGISTRO
	$sNomper = "SELECT np.*, mae.mae_cod, TRIM(mae.mae_nom) AS mae_nom, TRIM(mae.mae_ape) AS mae_ape,
			mae_fing, TRIM(pue.tpu_nom) AS tpu_nom
			FROM nomper np
			LEFT JOIN maestro mae ON mae.mae_emp=np.np_emp AND mae.mae_cod=np.np_cod 
			LEFT JOIN tpuesto pue ON mae.mae_tpue=pue.tpu_cod
			WHERE np_emp='$empCod' AND np_ffin>='$fini' AND np_ffin<='$ffin' AND mae_stat!='B'";

	$dAguinal = "DELETE FROM aguinal WHERE agui_emp='$empCod' AND agui_ano='$numAno'";

	function iAguinal($cod,$dic,$ene,$feb,$mar,$abr,$may,$jun,$jul,$ago,$sep,$oct,$nov,$tot,$agui,$fing) { 
		global $empCod, $usuNom,  $numAno;
		return "INSERT INTO aguinal(agui_emp, agui_ano, agui_cod, agui_dic, agui_ene, agui_feb, agui_mar, agui_abr, agui_may, agui_jun, agui_jul, agui_ago, agui_sep, agui_oct, agui_nov, agui_tot, agui_agui, agui_liqrec, agui_fing) VALUES ('$empCod', '$numAno','$cod','$dic','$ene','$feb','$mar', $abr,'$may','$jun','$jul','$ago','$sep','$oct','$nov','$tot','$agui','$agui', '$fing')";
	}
	function uAguinal($cod,$dic,$ene,$feb,$mar,$abr,$may,$jun,$jul,$ago,$sep,$oct,$nov,$tot,$agui) { 
		global $empCod, $usuNom,  $numAno;
		return "UPDATE aguinal SET agui_ene='$ene', agui_feb='$feb', agui_mar='$mar', agui_abr='$abr', agui_may='$may', agui_jun='$jun', agui_jul='$jul', agui_ago='$ago', agui_sep='$sep', agui_oct='$oct', agui_nov='$nov', agui_dic='$dic', agui_tot='$tot', agui_agui='$agui', agui_liqrec='$agui' WHERE agui_emp='$empCod' AND agui_ano='$numAno' AND agui_cod='$cod'";
	}
	function sAguinal($cod) { 
		global $empCod, $usuNom,  $numAno;
		return "SELECT * FROM aguinal WHERE agui_emp='$empCod' AND agui_ano='$numAno' AND agui_cod='$cod'";
	}


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

	$select["nomper"] = $db->query($sNomper);
	while ($rec = $db->fetchAssoc($select['nomper'])) {
	    $cod = $rec["np_cod"];
	    $fing = $rec["mae_fing"];
	    $fecha = $rec["np_ffin"];
	    $sdia = round($rec["np_snom"]/30,6);
	    $svaca=0;
	    $ssusi=0;
	    if ($rec["np_dvaca"]>0) {
	       $svaca= round($rec["np_dvaca"]*$sdia,2);
	    }
	    if ($rec["np_dsusi"]>0) {
		   $ssusi= round($rec["np_dsusi"]*$sdia,2);
	    }
	    $mesnp= date("m", strtotime($fecha));
	    $ene=0;
	    $feb=0;
	    $mar=0;
	    $abr=0;
	    $may=0;
	    $jun=0;
	    $jul=0;
	    $ago=0;
	    $sep=0;
	    $oct=0;
	    $nov=0;
	    $dic=0;

	    switch ($mesnp){
	      case 1:
	        $ene = $rec["np_sord"]+$svaca+$ssusi;
	        break;
	      case 2:
	        $feb = $rec["np_sord"]+$svaca+$ssusi;
	        break;
	      case 3:
	        $mar = $rec["np_sord"]+$svaca+$ssusi;
	        break;
	      case 4:
	        $abr = $rec["np_sord"]+$svaca+$ssusi;
	        break;
	      case 5:
	        $may = $rec["np_sord"]+$svaca+$ssusi;
	        break;
	      case 6:
	        $jun = $rec["np_sord"]+$svaca+$ssusi;
	        break;
	      case 7:
	        $jul = $rec["np_sord"]+$svaca+$ssusi;
	        break;
	      case 8:
	        $ago = $rec["np_sord"]+$svaca+$ssusi;
	        break;
	      case 9:
	        $sep = $rec["np_sord"]+$svaca+$ssusi;
	        break;
	      case 10:
	        $oct = $rec["np_sord"]+$svaca+$ssusi;
	        break;
	      case 11:
	        $nov = $rec["np_sord"]+$svaca+$ssusi;
	        break;
	      case 12:
	        $dic = $rec["np_sord"]+$svaca+$ssusi;
	        break;
	    }

		$select["aguinal"] = $db->query(sAguinal($cod));
		$fetchAssoc["aguinal"] = $db->fetchAssoc($select["aguinal"]);
		if ($db->numRows($select["aguinal"]) == 0) {
		   $tot=($ene+$feb+$mar+$abr+$may+$jun+$jul+$ago+$sep+$oct+$nov+$dic);
		   if ($tot>0) {
		      $agui=round($tot/12,2);
		   } else {
		   	  $agui=0;
		   }
	       $db->iEx(iAguinal($cod,$dic,$ene,$feb,$mar,$abr,$may,$jun,$jul,$ago,$sep,$oct,$nov,$tot,$agui,$fing));
	       continue;
		} else {
		   $nuene=$fetchAssoc["aguinal"]["agui_ene"]+$ene;
		   $nufeb=$fetchAssoc["aguinal"]["agui_feb"]+$feb;
		   $numar=$fetchAssoc["aguinal"]["agui_mar"]+$mar;
		   $nuabr=$fetchAssoc["aguinal"]["agui_abr"]+$abr;
		   $numay=$fetchAssoc["aguinal"]["agui_may"]+$may;
		   $nujun=$fetchAssoc["aguinal"]["agui_jun"]+$jun;
		   $nujul=$fetchAssoc["aguinal"]["agui_jul"]+$jul;
		   $nuago=$fetchAssoc["aguinal"]["agui_ago"]+$ago;
		   $nusep=$fetchAssoc["aguinal"]["agui_sep"]+$sep;
		   $nuoct=$fetchAssoc["aguinal"]["agui_oct"]+$oct;
		   $nunov=$fetchAssoc["aguinal"]["agui_nov"]+$nov;
		   $nudic=$fetchAssoc["aguinal"]["agui_dic"]+$dic;
		   $tot=($nuene+$nufeb+$numar+$nuabr+$numay+$nujun+$nujul+$nuago+$nusep+$nuoct+$nunov+$nudic);
		   if ($tot>0) {
		      $agui=round($tot/12,2);
		   } else {
		   	  $agui=0;
		   }
	       $db->uEx(uAguinal($cod,$nudic,$nuene,$nufeb,$numar,$nuabr,$numay,$nujun,$nujul,$nuago,$nusep,$nuoct,$nunov,$tot,$agui));
		}
	}

	$db->iEx($func->iBitacora(utf8_decode("Se Genero Aguinaldo:  $numAno Usuario: ").$usuNom, $bitMod, $bitForm));

	$db->close();

	return json_encode($resp);
}

?>