<?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 = "cxp3010";
$bitMod = "cxp";
$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 'consultarProvcp':
		echo consultarProvcp();
		break;
	case 'guardar':
		echo guardar();
		break;

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

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

	# QUERYS
	$sProvcp = "SELECT pcp.*, TRIM(pcp.pro_nom) AS pro_nom,
				TRIM(pcp.pro_dir) AS pro_dir,
				TRIM(pcp.pro_nit) AS pro_nit,
				TRIM(pcp.pro_tel) AS pro_tel,
				TRIM(pcp.pro_fax) AS pro_fax,
				TRIM(pcp.pro_razon) AS pro_razon,
				TRIM(pcp.pro_regimen) AS pro_regimen,
				TRIM(pcp.pro_tel2) AS pro_tel2,
				TRIM(pcp.pro_email) AS pro_email,
				TRIM(pcp.pro_retsn) AS pro_retsn,
				TRIM(pcp.pro_cta) AS pro_cta,
				TRIM(pcp.pro_ctad) AS pro_ctad,
				TRIM(cta.cta_nom) AS pro_ctanom,
				TRIM(dcta.cta_nom) AS pro_ctadnom 
				FROM provcp as pcp
				LEFT JOIN cuentas cta ON pcp.pro_emp = cta.cta_emp AND pcp.pro_cta=cta.cta
				LEFT JOIN cuentas dcta ON pcp.pro_emp = dcta.cta_emp AND pcp.pro_ctad=dcta.cta
				WHERE pro_emp='$empCod' AND pro_cod='$txtCod'";

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

	$func = new functions();

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

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'];
	$txtProv=utf8_decode($_POST['txtProv']);
	$txtRazon=utf8_decode($_POST['txtRazon']);
	$txtDir=utf8_decode($_POST['txtDir']);
	$txtRegimen=utf8_decode($_POST['txtRegimen']);
	$txtNit=utf8_decode($_POST['txtNit']);
	$txtTel=utf8_decode($_POST['txtTel']);
	$txtFax=utf8_decode($_POST['txtFax']);
	$txtTel2=utf8_decode($_POST['txtTel2']);
	$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'];
	$crRet=$_POST['crRet'];
	$selCodtel=$_POST['selCodtel'];
	$tipo='L';
	$sigmon='Q';
	if ($selClasi == '020') {
       $tipo='E';
       $sigmon='$';
	}       
	# QUERYS PARA UN REGISTRO

	$iProvcp = "INSERT INTO provcp(pro_emp, pro_cod, pro_nom, pro_dir, pro_nit, pro_tel, pro_tel2, pro_cta, pro_ctad, pro_dcre, pro_tipo, pro_sigmon, pro_clas, pro_retsn, pro_razon, pro_regimen, pro_email, pro_fax, pro_codtelpais, pro_ulcam) VALUES ('$empCod','$txtCodp','$txtProv','$txtDir','$txtNit','$txtTel','$txtTel2','$txtCta','$txtCtad','$numDcre','$tipo','$sigmon','$selClasi','$crRet','$txtRazon','$txtRegimen','$txtEmail','$txtFax','$selCodtel','$usuNom')";

	$uProvcp = "UPDATE provcp SET pro_nom='$txtProv',pro_dir='$txtDir',pro_nit='$txtNit',pro_tel='$txtTel',pro_tel2='$txtTel2',pro_cta='$txtCta',pro_ctad='$txtCtad',pro_dcre='$numDcre',pro_tipo='$tipo',pro_sigmon='$sigmon',pro_clas='$selClasi',pro_retsn='$crRet',pro_razon='$txtRazon',pro_regimen='$txtRegimen',pro_email='$txtEmail',pro_fax='$txtFax',pro_codtelpais='$selCodtel',pro_ulcam='$usuNom' WHERE pro_emp='$empCod' AND pro_cod='$txtCodp'";

	$iProvee = "INSERT INTO provee(pro_nit, pro_nom, pro_retsn) VALUES ('$txtNit','$txtProv','$crRet')";
	$uProvee = "UPDATE provee SET pro_nom='$txtProv', pro_retsn='$crRet' WHERE pro_nit='$txtNit'";
	$sProvee = "SELECT * FROM provee WHERE pro_nit='$txtNit'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	if ($func->stringToBool($_POST['nuevo'])) {
		$resp = $db->iEx($iProvcp);
		$db->iEx($func->iBitacora(utf8_decode("Se creó Proveedor, Codigo: $txtCodp   txtProv Usuario: ").$usuNom, $bitMod, $bitForm));
	} else {
		$resp = $db->uEx($uProvcp);
		$db->iEx($func->iBitacora(utf8_decode("Se modificó Proveedor, Codigo: $txtCodp   $txtProv Usuario: ").$usuNom, $bitMod, $bitForm));
	}
	$select["provee"] = $db->query($sProvee);
	if ($db->numRows($select["provee"]) > 0) {
		$db->query($uProvee);
	} else {
		$db->query($iProvee);
	}		
	
	$db->close();

	return json_encode($resp);
}

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

	$func = new functions();

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

	# QUERYS
	$dProvcp = "DELETE FROM provcp WHERE pro_emp='$empCod' AND pro_cod='$txtCodp'";

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

	return json_encode($resp);
}
?>