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

switch ($_POST['action']) {
	case 'consultarPla':
		echo consultarPla();
		break;
	case 'consultarPla1':
		echo consultarPla1();
		break;
	case 'consultarEstilo':
		echo consultarEstilo();
		break;
}

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

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

	$func = new functions();

	$txtCod=$_POST['txtCod'];

	# QUERYS
	$sEstilo = "SELECT fes.*, TRIM(mae.mae_nom) AS mae_nom, TRIM(mae.mae_ape) AS mae_ape,
		TRIM(fes_prenda) AS fes_prenda
		FROM festilos fes LEFT JOIN maestro mae ON fes.fes_emp=mae.mae_emp AND fes.fes_superv=mae.mae_cod
		WHERE fes_emp='$empCod' AND fes_cod='$txtCod'";

	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp = $db->sEx($sEstilo, "festilos");
	$db->close();

	$func = new functions();
	//return $func->jsonEncodeArray($resp, array('abonos', 'poli_det', 'abo_det'));
	return $func->jsonEncodeArray($resp, 'festilos');
}

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

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

	$func = new functions();

	$numNum=$_POST['numNum'];
	$selBodegapro=$_POST['selBodegapro'];
	$selDep=$_POST['selDep'];
	$selPuesto=$_POST['selPuesto'];
	$numCod=(empty($_POST['numCod'])) ? 0 : $_POST['numCod'];
	# QUERYS
	if ($numCod!=0) {
		$sNomper = "SELECT np.*, TRIM(mae.mae_nom) AS mae_nom, TRIM(mae.mae_ape) AS mae_ape,
					TRIM(tpu.tpu_nom) AS tpu_nom 
					FROM nomper np
	 				LEFT JOIN maestro mae ON np.np_emp=mae.mae_emp AND np.np_cod = mae.mae_cod 
	 				LEFT JOIN tpuesto tpu ON mae.mae_tpue = tpu.tpu_cod 
					WHERE np_emp='$empCod' AND np_numpla='$numNum' AND np_cod='$numCod' ORDER BY np_dep, mae.mae_tpue,mae.mae_nom,mae.mae_ape";
	} else {
		if ($selDep=="000") {
			if ($selPuesto=="000") {
				$sNomper = "SELECT np.*, TRIM(mae.mae_nom) AS mae_nom, TRIM(mae.mae_ape) AS mae_ape,
							TRIM(tpu.tpu_nom) AS tpu_nom 
							FROM nomper np
			 				LEFT JOIN maestro mae ON np.np_emp=mae.mae_emp AND np.np_cod = mae.mae_cod 
			 				LEFT JOIN tpuesto tpu ON mae.mae_tpue = tpu.tpu_cod 
							WHERE np_emp='$empCod' AND np_numpla='$numNum' and mae.mae_bodega='$selBodegapro' ORDER BY np_dep, mae.mae_tpue,mae.mae_nom,mae.mae_ape";
			} else {
				$sNomper = "SELECT np.*, TRIM(mae.mae_nom) AS mae_nom, TRIM(mae.mae_ape) AS mae_ape,
							TRIM(tpu.tpu_nom) AS tpu_nom 
							FROM nomper np
			 				LEFT JOIN maestro mae ON np.np_emp=mae.mae_emp AND np.np_cod = mae.mae_cod 
			 				LEFT JOIN tpuesto tpu ON mae.mae_tpue = tpu.tpu_cod 
							WHERE np_emp='$empCod' AND np_numpla='$numNum' and mae.mae_bodega='$selBodegapro' and mae.mae_tpue='$selPuesto' ORDER BY np_dep, mae.mae_tpue,mae.mae_nom,mae.mae_ape";
			}		
		} else {
			if ($selPuesto=="000") {
				$sNomper = "SELECT np.*, TRIM(mae.mae_nom) AS mae_nom, TRIM(mae.mae_ape) AS mae_ape,
							TRIM(tpu.tpu_nom) AS tpu_nom 
							FROM nomper np
			 				LEFT JOIN maestro mae ON np.np_emp=mae.mae_emp AND np.np_cod = mae.mae_cod 
			 				LEFT JOIN tpuesto tpu ON mae.mae_tpue = tpu.tpu_cod 
							WHERE np_emp='$empCod' AND np_numpla='$numNum' and mae.mae_bodega='$selBodegapro' and np_dep='$selDep' ORDER BY np_dep, mae.mae_tpue,mae.mae_nom,mae.mae_ape";
			} else {
				$sNomper = "SELECT np.*, TRIM(mae.mae_nom) AS mae_nom, TRIM(mae.mae_ape) AS mae_ape,
							TRIM(tpu.tpu_nom) AS tpu_nom 
							FROM nomper np
			 				LEFT JOIN maestro mae ON np.np_emp=mae.mae_emp AND np.np_cod = mae.mae_cod 
			 				LEFT JOIN tpuesto tpu ON mae.mae_tpue = tpu.tpu_cod 
							WHERE np_emp='$empCod' AND np_numpla='$numNum' and mae.mae_bodega='$selBodegapro' and np_dep='$selDep' and mae.mae_tpue='$selPuesto' ORDER BY np_dep, mae.mae_tpue,mae.mae_nom,mae.mae_ape";
			}		
		}		
	}
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp["nomper"] = $db->sFetchAll($sNomper);
	$db->close();

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

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

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

	$func = new functions();

	$numNum=$_POST['numNum'];

	# QUERYS

	$sNomper = "SELECT np.*, TRIM(mae.mae_nom) AS mae_nom, TRIM(mae.mae_ape) AS mae_ape,
				TRIM(tpu.tpu_nom) AS tpu_nom 
				FROM nomper np
 				LEFT JOIN maestro mae ON np.np_emp=mae.mae_emp AND np.np_cod = mae.mae_cod 
 				LEFT JOIN tpuesto tpu ON mae.mae_tpue = tpu.tpu_cod 
				WHERE np_emp='$empCod' AND np_numpla='$numNum'";

	$sCtrpla = "SELECT * FROM ctrpla
				LEFT JOIN grupo ON ctrpla.ctrp_gru=grupo.gru_cod
				WHERE ctrp_emp='$empCod' AND ctrp_num='$numNum'";

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

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


?>