<?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 = "cxc3010";
$bitMod = "cxc";
$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 'consultarCtad':
		echo consultarCtad();
		break;
	case 'consultarCorr':
		echo consultarCorr();
		break;
	case 'consultarCliencc':
		echo consultarCliencc();
		break;
	case 'guardar':
		echo guardar();
		break;

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

function consultarCorr() {
	global $empCod;

	# QUERYS
	$sCorrcli = "SELECT * FROM corrcli  WHERE cc_emp='$empCod'";
	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$select["corrcli"] = $db->query($sCorrcli);
	$fetchAssoc["corrcli"] = $db->fetchAssoc($select["corrcli"]);
    $numcli=$fetchAssoc["corrcli"]["cc_corr"];
	$db->close();
	$resp["codcorr"]= str_pad($numcli, 10, "0", STR_PAD_LEFT);
	return json_encode($resp);
}

function consultarCliencc() {
	global $empCod;
	$txtCod=utf8_decode($_POST['txtCod']);

	# QUERYS
	$sCliencc = "SELECT cl.*, TRIM(cl.cli_nom) AS cli_nom,
				TRIM(cl.cli_nomfac) AS cli_nomfac,
				TRIM(cl.cli_dir) AS cli_dir,
				TRIM(cl.cli_dirfac) AS cli_dirfac,
				TRIM(cl.cli_nit) AS cli_nit,
				TRIM(cl.cli_tel) AS cli_tel,
				TRIM(cl.cli_cont) AS cli_cont,
				TRIM(cl.cli_email) AS cli_email,
				TRIM(cl.cli_reti) AS cli_reti,
				TRIM(cl.cli_cta) AS cli_cta,
				TRIM(cl.cli_ctad) AS cli_ctad,
				TRIM(cta.cta_nom) AS cli_ctanom,
				TRIM(dcta.cta_nom) AS cli_ctadnom 
				FROM cliencc as cl
				LEFT JOIN cuentas cta ON cl.cli_emp = cta.cta_emp AND cl.cli_cta=cta.cta
				LEFT JOIN cuentas dcta ON cl.cli_emp = dcta.cta_emp AND cl.cli_ctad=dcta.cta
				WHERE cli_emp='$empCod' AND cli_cod='$txtCod'";

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

	$func = new functions();

	return $func->jsonEncodeArray($resp, "cliencc");
}

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 consultarCtad() {
	global $empCod;
	$txtCtad=utf8_decode($_POST['txtCtad']);

	# QUERYS
	$sCuentas = "SELECT *, TRIM(cta) AS cta, TRIM(cta_nom) AS cta_nom
				FROM cuentas  WHERE cta='$txtCtad' 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();

	$txtCodp=$_POST['txtCodp'];
	$nuevocorrcli=intval($txtCodp)+1;
	$txtNom=utf8_decode($_POST['txtNom']);
	$txtNomFac=utf8_decode($_POST['txtNomFac']);
	$txtDir=utf8_decode($_POST['txtDir']);
	$txtDirFac=utf8_decode($_POST['txtDirFac']);
	$txtNit=utf8_decode($_POST['txtNit']);
	$txtTel=utf8_decode($_POST['txtTel']);
	$txtCont=utf8_decode($_POST['txtCont']);
	$txtEmail=utf8_decode($_POST['txtEmail']);
	$txtCta=utf8_decode($_POST['txtCta']);
	$txtCtad=utf8_decode($_POST['txtCtad']);
	$selClasi=$_POST['selClasi'];
	$numDcre=(empty($_POST['numDcre'])) ? 0 : $_POST['numDcre'];
	$crReti=$_POST['crReti'];
	$selVend=$_POST['selVend'];
	$tipo='L';
	$sigmon='Q';
	if ($selClasi == '020') {
       $tipo='E';
       $sigmon='$';
	}
	$codpos = '01000';
	$depto = 'GUATEMALA';
	$pais = 83;
	# QUERYS PARA UN REGISTRO

	$iCliencc = "INSERT INTO cliencc(cli_emp, cli_cod, cli_nom, cli_nomfac, cli_dir, cli_dirfac, cli_nit, cli_tel, cli_cont, cli_cta, cli_ctad, cli_dcre, cli_tipo, cli_sigmon, cli_clas, cli_reti, cli_email, cli_ven, cli_ulcam, cli_stat, cli_codpos, cli_depto, cli_pais) VALUES ('$empCod','$txtCodp','$txtNom','$txtNomFac','$txtDir','$txtDirFac','$txtNit','$txtTel','$txtCont','$txtCta','$txtCtad','$numDcre','$tipo','$sigmon','$selClasi','$crReti','$txtEmail','$selVend','$usuNom','A', '$codpos', '$depto', $pais)";

	$uCliencc = "UPDATE cliencc SET cli_nom='$txtNom',cli_nomfac='$txtNomFac',cli_dir='$txtDir',cli_dirfac='$txtDirFac',cli_nit='$txtNit',cli_tel='$txtTel',cli_cont='$txtCont',cli_cta='$txtCta',cli_ctad='$txtCtad',cli_dcre='$numDcre',cli_tipo='$tipo',cli_sigmon='$sigmon',cli_clas='$selClasi',cli_reti='$crReti',cli_email='$txtEmail',cli_ven='$selVend',cli_ulcam='$usuNom' WHERE cli_emp='$empCod' AND cli_cod='$txtCodp'";

	$iClien = "INSERT INTO clien(cli_nit, cli_nom) VALUES ('$txtNit','$txtNom')";
	$uClien = "UPDATE clien SET cli_nom='$txtNom' WHERE cli_nit='$txtNit'";
	$sClien = "SELECT * FROM clien WHERE cli_nit='$txtNit'";

//	$uCorrcli = "UPDATE corrcli SET cc_corr='$nuevocorrcli' WHERE cc_emp='$empCod'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	if ($func->stringToBool($_POST['nuevo'])) {
		$resp = $db->iEx($iCliencc);
	//	$resp = $db->uEx($uCorrcli);
		$db->iEx($func->iBitacora(utf8_decode("Se creó Cliente, Codigo: $txtCodp   txtNom Usuario: ").$usuNom, $bitMod, $bitForm));
	} else {
		$resp = $db->uEx($uCliencc);
		$db->iEx($func->iBitacora(utf8_decode("Se modificó Cliente, Codigo: $txtCodp   $txtNom Usuario: ").$usuNom, $bitMod, $bitForm));
	}
	$select["clien"] = $db->query($sClien);
	if ($db->numRows($select["clien"]) > 0) {
		$db->query($uClien);
	} else {
		$db->query($iClien);
	}		
	
	$db->close();

	return json_encode($resp);
}

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

	$func = new functions();

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

	# QUERYS
	$dCliencc = "DELETE FROM cliencc WHERE cli_emp='$empCod' AND cli_cod='$txtCodp'";

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

	return json_encode($resp);
}
?>