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

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

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

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

	$func = new functions();

	$txtCod=$_POST['txtCod'];

	# QUERYS
	$sEmpresas = "SELECT emp.*,
				TRIM(emp.emp_nom) AS emp_nom  
				FROM empresas AS emp
				WHERE emp.emp_cod='$txtCod'";
	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp = $db->sEx($sEmpresas, "empresas");
	$db->close();

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

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

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

	$func = new functions();

	$txtCodo=$_POST['txtCodo'];
	$txtCodd=$_POST['txtCodd'];
	$txtCodoNom=utf8_decode($_POST['txtCodoNom']);
	$txtCoddNom=utf8_decode($_POST['txtCoddNom']);

	# QUERYS
	$iCuentas = "INSERT INTO cuentas (cta_emp, cta, cta_nom, cta_con, cta_niv, cta_acta, cta_gru, cta_admi, cta_ulcam,cta_presu) SELECT '$txtCodd', cta, cta_nom, cta_con, cta_niv, cta_acta, cta_gru, cta_admi, cta_ulcam,cta_presu FROM cuentas WHERE cta_emp='$txtCodo'";
	
	# EJECUCION DE QUERYS 
	$db = new dbExec();

	$resp = $db->iEx($iCuentas);
	$db->iEx($func->iBitacora(utf8_decode("Se copio la nomenclatura de la empresa: $txtCodoNom a la empresa $txtCoddNom Usuario: ").$usuNom, $bitMod, $bitForm));
	
	$db->close();

	return json_encode($resp);
}

?>