<?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 = "nom3010";
$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."-07-01";
	$fecfin1=$numAno."-06-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'";

	$dBono14 = "DELETE FROM bono14 	WHERE b14_emp='$empCod' AND b14_ano='$numAno'";

	function iBono14($cod,$ene,$feb,$mar,$abr,$may,$jun,$jul,$ago,$sep,$oct,$nov,$dic,$tot,$bono,$fing) { 
		global $empCod, $usuNom,  $numAno;
		return "INSERT INTO bono14(b14_emp, b14_ano, b14_cod, b14_ene, b14_feb, b14_mar, b14_abr, b14_may, b14_jun, b14_jul, b14_ago, b14_sep, b14_oct, b14_nov, b14_dic, b14_tot, b14_bono, b14_liqrec, b14_fing) VALUES ('$empCod', '$numAno','$cod','$ene','$feb','$mar', $abr,'$may','$jun','$jul','$ago','$sep','$oct','$nov','$dic','$tot','$bono','$bono', '$fing')";
	}
	function uBono14($cod,$ene,$feb,$mar,$abr,$may,$jun,$jul,$ago,$sep,$oct,$nov,$dic,$tot,$bono) { 
		global $empCod, $usuNom,  $numAno;
		return "UPDATE bono14 SET b14_ene='$ene', b14_feb='$feb', b14_mar='$mar', b14_abr='$abr', b14_may='$may', b14_jun='$jun', b14_jul='$jul', b14_ago='$ago', b14_sep='$sep', b14_oct='$oct', b14_nov='$nov', b14_dic='$dic', b14_tot='$tot', b14_bono='$bono', b14_liqrec='$bono' WHERE b14_emp='$empCod' AND b14_ano='$numAno' AND b14_cod='$cod'";
	}
	function sBono14($cod) { 
		global $empCod, $usuNom,  $numAno;
		return "SELECT * FROM bono14 WHERE b14_emp='$empCod' AND b14_ano='$numAno' AND b14_cod='$cod'";
	}


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

	$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["bono14"] = $db->query(sBono14($cod));
		$fetchAssoc["bono14"] = $db->fetchAssoc($select["bono14"]);
		if ($db->numRows($select["bono14"]) == 0) {
		   $tot=($ene+$feb+$mar+$abr+$may+$jun+$jul+$ago+$sep+$oct+$nov+$dic);
		   if ($tot>0) {
		      $bono=round($tot/12,2);
		   } else {
		   	  $bono=0;
		   }
	       $db->iEx(iBono14($cod,$ene,$feb,$mar,$abr,$may,$jun,$jul,$ago,$sep,$oct,$nov,$dic,$tot,$bono,$fing));
	       continue;
		} else {
		   $nuene=$fetchAssoc["bono14"]["b14_ene"]+$ene;
		   $nufeb=$fetchAssoc["bono14"]["b14_feb"]+$feb;
		   $numar=$fetchAssoc["bono14"]["b14_mar"]+$mar;
		   $nuabr=$fetchAssoc["bono14"]["b14_abr"]+$abr;
		   $numay=$fetchAssoc["bono14"]["b14_may"]+$may;
		   $nujun=$fetchAssoc["bono14"]["b14_jun"]+$jun;
		   $nujul=$fetchAssoc["bono14"]["b14_jul"]+$jul;
		   $nuago=$fetchAssoc["bono14"]["b14_ago"]+$ago;
		   $nusep=$fetchAssoc["bono14"]["b14_sep"]+$sep;
		   $nuoct=$fetchAssoc["bono14"]["b14_oct"]+$oct;
		   $nunov=$fetchAssoc["bono14"]["b14_nov"]+$nov;
		   $nudic=$fetchAssoc["bono14"]["b14_dic"]+$dic;
		   $tot=($nuene+$nufeb+$numar+$nuabr+$numay+$nujun+$nujul+$nuago+$nusep+$nuoct+$nunov+$nudic);
		   if ($tot>0) {
		      $bono=round($tot/12,2);
		   } else {
		   	  $bono=0;
		   }
	       $db->uEx(uBono14($cod,$nuene,$nufeb,$numar,$nuabr,$numay,$nujun,$nujul,$nuago,$nusep,$nuoct,$nunov,$nudic,$tot,$bono));
		}
	}

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

	$db->close();

	return json_encode($resp);
}

?>