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

switch ($_POST['action']) {

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

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

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

	case 'borrar':
		echo borrar();
		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'";

	$sEstilo_d = "SELECT fesd.*, TRIM(mae.mae_nom) AS mae_nom, TRIM(mae.mae_ape) AS mae_ape, TRIM(tm.tm_nom) AS tm_nom 
				FROM festilos_d fesd 
				LEFT JOIN maestro mae ON fesd.fesd_emp=mae.mae_emp AND fesd.fesd_codemple=mae.mae_cod
				LEFT JOIN tipomaquina tm ON fesd.fesd_maquinaria=tm.tm_cod
				WHERE fesd_emp='$empCod' AND fesd_cod='$txtCod' ORDER BY fesd_linea";

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

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


function consultarEmple() {
	global $empCod;

	$func = new functions();

	$numCodEmple=utf8_decode($_POST['numCodEmple']);

	# QUERYS
	# QUERYS
	$sMaestro = "SELECT *, 
				TRIM(mae_nom) AS mae_nom, 
				TRIM(mae_ape) AS mae_ape
				FROM maestro  WHERE mae_emp='$empCod' AND mae_cod='$numCodEmple'";

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

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


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

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

	$func = new functions();

	$txtdoc=$_POST['txtCod'];
	$txtCod=$_POST['txtCod'];
	$txtDesc=$_POST['txtDesc'];
	$selBodegapro=$_POST['selBodegapro'];
	$selLineapro=$_POST['selLineapro'];
	$numCods=$_POST['numCods'];
	$txtSupervNom=$_POST['txtSupervNom'];
	$dtFec=$_POST['dtFec'];
	$jsonDetao=json_decode($_POST['tblDetao']);
	$dtGrabado=date("Y-m-d H:i:s", STRTOTIME(date('h:i:sa')));

	# QUERYS PARA UN REGISTRO

	$iFestilo="INSERT INTO festilos(fes_emp, fes_cod, fes_prenda, fes_bod, fes_lineapro, fes_superv, fes_fec) VALUES ('$empCod','$txtCod','$txtDesc','$selBodegapro','$selLineapro','$numCods','$dtFec')";

	$uFestilo="UPDATE festilos SET fes_prenda='$txtDesc', fes_bod='$selBodegapro', fes_lineapro='$selLineapro', fes_fec='$dtFec' WHERE fes_emp='$empCod' AND fes_cod='$txtCod'";

	$dFestilod="DELETE FROM festilos_d WHERE fesd_emp='$empCod' AND fesd_cod='$txtCod'";

	function iFestilod($numd, $descd, $tipomaqd, $codempled, $catd, $unid, $horad, $metad, $valord, $diariod, $quincenad) { 
		global $empCod, $usuNom, $txtCod;
		return "INSERT INTO festilos_d(fesd_emp, fesd_cod, fesd_linea, fesd_desc, fesd_maquinaria, fesd_codemple, fesd_cat, fesd_uni, fesd_hora, fesd_meta, fesd_valor, fesd_diario, fesd_quincena ) VALUES('$empCod','$txtCod','$numd','$descd','$tipomaqd','$codempled','$catd','$unid','$horad','$metad','$valord','$diariod','$quincenad')";
	}

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	if ($func->stringToBool($_POST['nuevo'])) {
		$resp = $db->iEx($iFestilo);
		$db->iEx($func->iBitacora(utf8_decode("Se creó Estilo, Código: $txtCod Usuario: ").$usuNom, $bitMod, $bitForm));
	} else {
		$resp = $db->uEx($uFestilo);
		$db->iEx($func->iBitacora(utf8_decode("Se modificó Estilo, Código: $txtCod Usuario: ").$usuNom, $bitMod, $bitForm));
	}
	$db->dEx($dFestilod);

	foreach ($jsonDetao as $obj) {
		$numd = trim($obj->numd);
		$descd = trim($obj->descd);
		$tipomaqd = $obj->tipomaqd;
		$codempled = $obj->codempled;
		$catd = $obj->catd;
		$unid = $obj->unid;
		$horad = $obj->horad;
		$metad = $obj->metad;
		$valord = $obj->valord;
		$diariod = $obj->diariod;
		$quincenad = $obj->quincenad;

        // Registra el detalle
		$db->query(iFestilod($numd, $descd, $tipomaqd, $codempled, $catd, $unid, $horad, $metad, $valord, $diariod, $quincenad));
	}
	//$resp["tipoabo"] = $db->query($sTipoabo);
	$db->close();
	
	$resp["dtGrabado"]=$dtGrabado;
	$resp["txtdoc"]=$txtdoc;
	//return $func->jsonEncodeArray($resp, "tipoabo");
	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();

	$txtCod=$_POST['txtCod'];
	$anu='**** ANULADO ****';
	$valcero=0;

	# QUERYS PARA UN REGISTRO

	$dFestilo = "DELETE FROM festilos WHERE fes_emp='$empCod' AND fes_cod='$txtCod'";
	$dFestilod = "DELETE FROM festilos_d WHERE fesd_emp='$empCod' AND fesd_cod='$txtCod'";


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

	$resp = $db->dEx($dFestilo);
	$db->uEx($dFestilod);
	//$db->uEx($uDoctose);

	//
	$db->iEx($func->iBitacora(utf8_decode("Se Borro Estilo Codigo.: $txtCod  Usuario: ").$usuNom, $bitMod, $bitForm));

	$db->close();

	return json_encode($resp);
}

?>