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

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

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

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

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

function consultar() {
	global $empCod;
	$numCod=$_POST['numCod'];

	# QUERYS
	$sDep = "SELECT dep.*, TRIM(dep.dep_nom) AS dep_nom,
			TRIM(dep.dep_ctaord) AS dep_ctaord,
			TRIM(dep.dep_ctahext) AS dep_ctahext,
			TRIM(dep.dep_ctabonil) AS dep_bonil,
			TRIM(dep.dep_ctabonii) AS dep_ctabonii,
			TRIM(dep.dep_ctacomi) AS dep_ctacomi,
			TRIM(dep.dep_ctabonoe) AS dep_ctabonoe,
			TRIM(dep.dep_ctadepre) AS dep_ctadepre,
			TRIM(dep.dep_ctaviati) AS dep_ctaviati,
			TRIM(dep.dep_ctadevisr) AS dep_ctadevisr,
			TRIM(dep.dep_ctaotrp) AS dep_ctaotrp,
			TRIM(dep.dep_ctaigss) AS dep_ctaigss,
			TRIM(dep.dep_ctaisr) AS dep_ctaisr,
			TRIM(dep.dep_ctapreb) AS dep_ctapreb,
			TRIM(dep.dep_ctapree) AS dep_ctapree,
			TRIM(dep.dep_ctadesj) AS dep_ctadesj,
			TRIM(dep.dep_ctaotrd) AS dep_ctaotrd,
			TRIM(dep.dep_ctasueldos) AS dep_ctasueldos,
			TRIM(dep.dep_ctacpi) AS dep_ctacpi,
			TRIM(dep.dep_ctairtra) AS dep_ctairtra,
			TRIM(dep.dep_ctaintecap) AS dep_ctaintecap,
			TRIM(dep.dep_ctavac) AS dep_ctavac,
			TRIM(dep.dep_ctaag) AS dep_ctaag,
			TRIM(dep.dep_ctabon) AS dep_ctabon,
			TRIM(dep.dep_ctaind) AS dep_ctaind,
			TRIM(dep.dep_ctaprovac) AS dep_ctaprovac,
			TRIM(dep.dep_ctaproag) AS dep_ctaproag,
			TRIM(dep.dep_ctaprobon) AS dep_ctaprobon,
			TRIM(dep.dep_ctaproind) AS dep_ctaproind,
			TRIM(dep.dep_ctaprocpi) AS dep_ctaprocpi,
			TRIM(ctaord.cta_nom) AS dep_ctaordnom, 
			TRIM(ctahext.cta_nom) AS dep_ctahextnom, 
			TRIM(ctabonil.cta_nom) AS dep_ctabonilnom, 
			TRIM(ctabonii.cta_nom) AS dep_ctaboniinom, 
			TRIM(ctacomi.cta_nom) AS dep_ctacominom,
			TRIM(ctabonoe.cta_nom) AS dep_ctabonoenom,
			TRIM(ctadepre.cta_nom) AS dep_ctadeprenom,
			TRIM(ctaviati.cta_nom) AS dep_ctaviatinom,
			TRIM(ctadevisr.cta_nom) AS dep_ctadevisrnom,
			TRIM(ctaotrp.cta_nom) AS dep_ctaotrpnom,
			TRIM(ctaigss.cta_nom) AS dep_ctaigssnom,
			TRIM(ctaisr.cta_nom) AS dep_ctaisrnom,
			TRIM(ctapreb.cta_nom) AS dep_ctaprebnom,
			TRIM(ctapree.cta_nom) AS dep_ctapreenom,
			TRIM(ctadesj.cta_nom) AS dep_ctadesjnom,
			TRIM(ctaotrd.cta_nom) AS dep_ctaotrdnom,
			TRIM(ctasueldos.cta_nom) AS dep_ctasueldosnom,
			TRIM(ctacpi.cta_nom) AS dep_ctacpinom,
			TRIM(ctairtra.cta_nom) AS dep_ctairtranom,
			TRIM(ctaintecap.cta_nom) AS dep_ctaintecapnom,
			TRIM(ctavac.cta_nom) AS dep_ctavacnom,
			TRIM(ctaag.cta_nom) AS dep_ctaagnom,
			TRIM(ctabon.cta_nom) AS dep_ctabonnom,
			TRIM(ctaind.cta_nom) AS dep_ctaindnom,
			TRIM(ctaprovac.cta_nom) AS dep_ctaprovacnom,
			TRIM(ctaproag.cta_nom) AS dep_ctaproagnom,
			TRIM(ctaprobon.cta_nom) AS dep_ctaprobonnom,
			TRIM(ctaproind.cta_nom) AS dep_ctaproindnom,
			TRIM(ctaprocpi.cta_nom) AS dep_ctaprocpinom
			FROM dep dep 
			LEFT JOIN cuentas ctaord ON ctaord.cta_emp='$empCod' AND dep.dep_ctaord=ctaord.cta 
			LEFT JOIN cuentas ctahext ON ctahext.cta_emp = '$empCod' AND dep.dep_ctahext=ctahext.cta 
			LEFT JOIN cuentas ctabonil ON ctabonil.cta_emp = '$empCod' AND dep.dep_ctabonil=ctabonil.cta 
			LEFT JOIN cuentas ctabonii ON ctabonii.cta_emp = '$empCod' AND dep.dep_ctabonii=ctabonii.cta 
			LEFT JOIN cuentas ctacomi ON ctacomi.cta_emp = '$empCod' AND dep.dep_ctacomi=ctacomi.cta 
			LEFT JOIN cuentas ctabonoe ON ctabonoe.cta_emp = '$empCod' AND dep.dep_ctabonoe=ctabonoe.cta 
			LEFT JOIN cuentas ctadepre ON ctadepre.cta_emp = '$empCod' AND dep.dep_ctadepre=ctadepre.cta 
			LEFT JOIN cuentas ctaviati ON ctaviati.cta_emp = '$empCod' AND dep.dep_ctaviati=ctaviati.cta 
			LEFT JOIN cuentas ctadevisr ON ctadevisr.cta_emp = '$empCod' AND dep.dep_ctadevisr=ctadevisr.cta 
			LEFT JOIN cuentas ctaotrp ON ctaotrp.cta_emp = '$empCod' AND dep.dep_ctaotrp=ctaotrp.cta 
			LEFT JOIN cuentas ctaigss ON ctaigss.cta_emp = '$empCod' AND dep.dep_ctaigss=ctaigss.cta 
			LEFT JOIN cuentas ctaisr ON ctaisr.cta_emp = '$empCod' AND dep.dep_ctaisr=ctaisr.cta 
			LEFT JOIN cuentas ctapreb ON ctapreb.cta_emp = '$empCod' AND dep.dep_ctapreb=ctapreb.cta 
			LEFT JOIN cuentas ctapree ON ctapree.cta_emp = '$empCod' AND dep.dep_ctapree=ctapree.cta 
			LEFT JOIN cuentas ctadesj ON ctadesj.cta_emp = '$empCod' AND dep.dep_ctadesj=ctadesj.cta 
			LEFT JOIN cuentas ctaotrd ON ctaotrd.cta_emp = '$empCod' AND dep.dep_ctaotrd=ctaotrd.cta 
			LEFT JOIN cuentas ctasueldos ON ctasueldos.cta_emp = '$empCod' AND dep.dep_ctasueldos=ctasueldos.cta 
			LEFT JOIN cuentas ctacpi ON ctacpi.cta_emp = '$empCod' AND dep.dep_ctacpi=ctacpi.cta 
			LEFT JOIN cuentas ctairtra ON ctairtra.cta_emp = '$empCod' AND dep.dep_ctairtra=ctairtra.cta 
			LEFT JOIN cuentas ctaintecap ON ctaintecap.cta_emp = '$empCod' AND dep.dep_ctaintecap=ctaintecap.cta 
			LEFT JOIN cuentas ctavac ON ctavac.cta_emp = '$empCod' AND dep.dep_ctavac=ctavac.cta 
			LEFT JOIN cuentas ctaag ON ctaag.cta_emp = '$empCod' AND dep.dep_ctaag=ctaag.cta 
			LEFT JOIN cuentas ctabon ON ctabon.cta_emp = '$empCod' AND dep.dep_ctabon=ctabon.cta 
			LEFT JOIN cuentas ctaind ON ctaind.cta_emp = '$empCod' AND dep.dep_ctaind=ctaind.cta 
			LEFT JOIN cuentas ctaprovac ON ctaprovac.cta_emp = '$empCod' AND dep.dep_ctaprovac=ctaprovac.cta 
			LEFT JOIN cuentas ctaproag ON ctaproag.cta_emp = '$empCod' AND dep.dep_ctaproag=ctaproag.cta 
			LEFT JOIN cuentas ctaprobon ON ctaprobon.cta_emp = '$empCod' AND dep.dep_ctaprobon=ctaprobon.cta 
			LEFT JOIN cuentas ctaproind ON ctaproind.cta_emp = '$empCod' AND dep.dep_ctaproind=ctaproind.cta 
			LEFT JOIN cuentas ctaprocpi ON ctaprocpi.cta_emp = '$empCod' AND dep.dep_ctaprocpi=ctaprocpi.cta 
			WHERE dep_emp='$empCod' AND dep_cod='$numCod'";
	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp = $db->sEx($sDep, "dep");
	$db->close();

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

function consultarCta() {
	global $empCod;
	$txtCta=utf8_decode($_POST['txtCta']);

	# QUERYS
	$sCuentas = "SELECT *, TRIM(cta) AS cta, TRIM(cta_nom) AS cta_nom
				FROM cuentas  WHERE cta='$txtCta' AND cta_emp='$empCod'";
	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp = $db->sEx($sCuentas, "cuentas");
	$db->close();

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

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

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

	$func = new functions();

	$numCod=$_POST['numCod'];
	$txtNom=utf8_decode($_POST['txtNom']);
	$txtCtaord=utf8_decode($_POST['txtCtaord']);
	$txtCtahext=utf8_decode($_POST['txtCtahext']);
    $txtCtabonil=utf8_decode($_POST['txtCtabonil']);
    $txtCtabonii=utf8_decode($_POST['txtCtabonii']);
    $txtCtacomi=utf8_decode($_POST['txtCtacomi']);
    $txtCtabonoe=utf8_decode($_POST['txtCtabonoe']);
    $txtCtadepre=utf8_decode($_POST['txtCtadepre']);
    $txtCtaviati=utf8_decode($_POST['txtCtaviati']);
    $txtCtadevisr=utf8_decode($_POST['txtCtadevisr']);
    $txtCtaotrp=utf8_decode($_POST['txtCtaotrp']);
    $txtCtaigss=utf8_decode($_POST['txtCtaigss']);
    $txtCtaisr=utf8_decode($_POST['txtCtaisr']);
    $txtCtapreb=utf8_decode($_POST['txtCtapreb']);
    $txtCtapree=utf8_decode($_POST['txtCtapree']);
    $txtCtadesj=utf8_decode($_POST['txtCtadesj']);
    $txtCtaotrd=utf8_decode($_POST['txtCtaotrd']);
    $txtCtasueldos=utf8_decode($_POST['txtCtasueldos']);
    $txtCtacpi=utf8_decode($_POST['txtCtacpi']);
    $txtCtairtra=utf8_decode($_POST['txtCtairtra']);
    $txtCtaintecap=utf8_decode($_POST['txtCtaintecap']);
    $txtCtavac=utf8_decode($_POST['txtCtavac']);
    $txtCtaag=utf8_decode($_POST['txtCtaag']);
    $txtCtabon=utf8_decode($_POST['txtCtabon']);
    $txtCtaind=utf8_decode($_POST['txtCtaind']);
    $txtCtaprovac=utf8_decode($_POST['txtCtaprovac']);
    $txtCtaproag=utf8_decode($_POST['txtCtaproag']);
    $txtCtaprobon=utf8_decode($_POST['txtCtaprobon']);
    $txtCtaproind=utf8_decode($_POST['txtCtaproind']);
    $txtCtaprocpi=utf8_decode($_POST['txtCtaprocpi']);

	# QUERYS PARA UN REGISTRO

	$iDep = "INSERT INTO dep(dep_emp,dep_cod, dep_nom, dep_ctaord, dep_ctahext, dep_ctabonil, dep_ctabonii, dep_ctacomi, dep_ctabonoe, dep_ctadepre, dep_ctaviati, dep_ctadevisr, dep_ctaotrp, dep_ctaigss, dep_ctaisr, dep_ctapreb, dep_ctapree, dep_ctadesj, dep_ctaotrd, dep_ctasueldos, dep_ctacpi, dep_ctairtra, dep_ctaintecap, dep_ctavac, dep_ctaag, dep_ctabon, dep_ctaind, dep_ctaprovac, dep_ctaproag, dep_ctaprobon, dep_ctaproind, dep_ctaprocpi) VALUES ('$empCod','$numCod','$txtNom','$txtCtaord','$txtCtahext','$txtCtabonil','$txtCtabonii','$txtCtacomi','$txtCtabonoe','$txtCtadepre','$txtCtaviati','$txtCtadevisr','$txtCtaotrp','$txtCtaigss','$txtCtaisr','$txtCtapreb','$txtCtapree','$txtCtadesj','$txtCtaotrd','$txtCtasueldos','$txtCtacpi','$txtCtairtra','$txtCtaintecap','$txtCtavac','$txtCtaag','$txtCtabon','$txtCtaind','$txtCtaprovac','$txtCtaproag','$txtCtaprobon','$txtCtaproind','$txtCtaprocpi')";

	$uDep = "UPDATE dep SET dep_nom='$txtNom',dep_ctaord='$txtCtaord',dep_ctahext='$txtCtahext',dep_ctabonil='$txtCtabonil',dep_ctabonii='$txtCtabonii',dep_ctacomi='$txtCtacomi',dep_ctabonoe='$txtCtabonoe',dep_ctadepre='$txtCtadepre',dep_ctaviati='$txtCtaviati',dep_ctadevisr='$txtCtadevisr',dep_ctaotrp='$txtCtaotrp',dep_ctaigss='$txtCtaigss',dep_ctaisr='$txtCtaisr',dep_ctapreb='$txtCtapreb',dep_ctapree='$txtCtapree',dep_ctadesj='$txtCtadesj',dep_ctaotrd='$txtCtaotrd',dep_ctasueldos='$txtCtasueldos',dep_ctacpi='$txtCtacpi',dep_ctairtra='$txtCtairtra',dep_ctaintecap='$txtCtaintecap',dep_ctavac='$txtCtavac',dep_ctaag='$txtCtaag',dep_ctabon='$txtCtabon',dep_ctaind='$txtCtaind',dep_ctaprovac='$txtCtaprovac',dep_ctaproag='$txtCtaproag',dep_ctaprobon='$txtCtaprobon',dep_ctaproind='$txtCtaproind',dep_ctaprocpi='$txtCtaprocpi' WHERE dep_emp='$empCod' AND dep_cod='$numCod'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	if ($func->stringToBool($_POST['nuevo'])) {
		$resp = $db->iEx($iDep);
		$db->iEx($func->iBitacora(utf8_decode("Se creó Departamento Planilla, Codigo: $numCod   $txtNom  Usuario: ").$usuNom, $bitMod, $bitForm));
	} else {
		$resp = $db->uEx($uDep);
		$db->iEx($func->iBitacora(utf8_decode("Se modificó Departamento Planilla, Codigo: $numCod   $txtNom  Usuario: ").$usuNom, $bitMod, $bitForm));
	}
	
	$db->close();

	return json_encode($resp);
}

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

	$func = new functions();

	$numCod=$_POST['numCod'];
	$txtNom=$_POST['txtNom'];

	# QUERYS
	$dDep = "DELETE FROM dep WHERE dep_emp='$empCod' AND dep_cod='$numCod'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	$resp = $db->dEx($dDep);
	$db->iEx($func->iBitacora(utf8_decode("Se borró el Departamento planilla, Codigo: $numCod  $txtNom  Usuario: ").$usuNom, $bitMod, $bitForm));
	$db->close();

	return json_encode($resp);
}
?>