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

switch ($_POST['action']) {

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

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

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

	# 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 consultarOperaciones() {
	global $empCod, $usuNom;

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

	$func = new functions();

	$txtCod=$_POST['txtCod'];
	$numLinea=$_POST['numLinea'];

	# QUERYS
	$sEstilo_d = "SELECT fesd.*, TRIM(tm.tm_nom) AS tm_nom
		FROM festilos_d fesd 
		LEFT JOIN tipomaquina tm ON fesd.fesd_maquinaria=tm.tm_cod
		WHERE fesd_emp='$empCod' AND fesd_cod='$txtCod' AND fesd_linea='$numLinea'";

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

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

function consultarFecha() {
	global $empCod, $usuNom, $txtCod, $dtFec;

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

	$func = new functions();

	$txtCod=$_POST['txtCod'];
	$dtFec=$_POST['dtFec'];

	# QUERYS
	$sFestilod = "SELECT * FROM festilos_d
				WHERE fesd_emp='$empCod' AND fesd_cod='$txtCod' ORDER BY fesd_linea";

	function sNomper_prod($numd) { 
		global $empCod, $usuNom, $txtCod, $dtFec;
		return "SELECT * FROM nomper_prod WHERE npp_emp='$empCod' AND npp_cod='$txtCod' AND npp_fec='$dtFec' AND npp_linea='$numd'";
	}
	function iNomper_prod($numd, $valord) { 
		global $empCod, $usuNom, $txtCod, $dtFec;
		return "INSERT INTO nomper_prod (npp_emp, npp_cod, npp_fec, npp_linea, npp_codemple, npp_cant, npp_valor, npp_tot) VALUES('$empCod','$txtCod','$dtFec','$numd', 0, 0, '$valord', 0)";
	}

	$sNomper_prod = "SELECT npp.*, TRIM(mae.mae_nom) AS mae_nom, TRIM(mae.mae_ape) AS mae_ape, TRIM(tm.tm_nom) AS tm_nom, TRIM(fesd.fesd_desc) AS fesd_desc,
				TRIM(fesd.fesd_maquinaria) AS fesd_maquinaria 
				FROM nomper_prod npp 
				LEFT JOIN festilos_d fesd ON fesd.fesd_emp=npp.npp_emp AND fesd.fesd_cod=npp.npp_cod AND fesd.fesd_linea=npp.npp_linea
				LEFT JOIN maestro mae ON npp.npp_emp=mae.mae_emp AND npp.npp_codemple=mae.mae_cod
				LEFT JOIN tipomaquina tm ON fesd.fesd_maquinaria=tm.tm_cod
				WHERE npp_emp='$empCod' AND npp_cod='$txtCod' AND npp_fec='$dtFec' ORDER BY npp_linea";

	# EJECUCION DE QUERYS
	$paso="N";
	$db = new dbExec();
	$select["festilos_d"] = $db->query($sFestilod);
	while ($recDet = $db->fetchAssoc($select["festilos_d"])) {
        $numd=$recDet["fesd_linea"];
        $valord=$recDet["fesd_valor"];
		$select["nomper_prod"] = $db->query(sNomper_prod($numd));
		if ($db->numRows($select["nomper_prod"]) == 0) {
			$db->query(iNomper_prod($numd,$valord));
			$paso="S";
		}	
	}
	$resp["nomper_prod"] = $db->sFetchAll($sNomper_prod);
	$db->close();
	$resp["paso"]=$paso;
	$func = new functions();
	//return $func->jsonEncodeArray($resp, array('abonos', 'poli_det', 'abo_det'));
	return $func->jsonEncodeArray($resp, 'nomper_prod');
}

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, $dtFec, $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'];
	$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
	$dNomper_prod="DELETE FROM nomper_prod WHERE npp_emp='$empCod' AND npp_cod='$txtCod' AND npp_fec='$dtFec'";

	function iNomper_prod($numd, $codempled,  $unid, $valord, $totd) { 
		global $empCod, $usuNom, $txtCod, $dtFec;
		return "INSERT INTO nomper_prod(npp_emp, npp_cod, npp_fec, npp_linea, npp_codemple, npp_cant, npp_valor, npp_tot) VALUES('$empCod','$txtCod','$dtFec','$numd','$codempled','$unid','$valord','$totd')";
	}

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	$resp = $db->iEx($func->iBitacora(utf8_decode("Se Trabajo Produccion Código: $txtCod  Fecha:  $dtFec  Usuario: ").$usuNom, $bitMod, $bitForm));
    $db->dEx($dNomper_prod);
	foreach ($jsonDetao as $obj) {
		$numd = trim($obj->numd);
		$codempled = $obj->codempled;
		$unid = $obj->unid;
		$valord = $obj->valord;
		$totd = $obj->totd;
        // Registra el detalle
		$db->query(iNomper_prod($numd, $codempled, $unid, $valord, $totd));
	}
	//$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'];
	$dtfec = $_POST['dtFec'];
	$anu='**** ANULADO ****';
	$valcero=0;

	# QUERYS PARA UN REGISTRO

	$dNomper_prod="DELETE FROM nomper_prod WHERE npp_emp='$empCod' AND npp_cod='$txtCod' AND npp_fec='$dtFec'";


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

	$resp = $db->dEx($dNomper_prod);

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

	$db->close();

	return json_encode($resp);
}

?>