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

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

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

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

function consultarProvee() {
	global $empCod;
	$txtNit=utf8_decode($_POST['txtNit']);

	# QUERYS
	$sProvee = "SELECT *,TRIM(pro_nom) AS pro_nom FROM provee WHERE pro_nit='$txtNit'";
	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp = $db->sEx($sProvee, "provee");
	$db->close();

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

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

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

	$func = new functions();

	$txtNit=$_POST['txtNit'];
	$txtProv=utf8_decode($_POST['txtProv']);
	$crRet=$_POST['crRet'];

	# QUERYS PARA UN REGISTRO
	$iProv = "INSERT INTO provee(pro_nit, pro_nom, pro_retsn) VALUES ('$txtNit','$txtProv','$crRet')";

	$uProv = "UPDATE provee SET pro_nom='$txtProv',pro_retsn='$crRet' WHERE pro_nit='$txtNit'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	if ($func->stringToBool($_POST['nuevo'])) {
		$resp = $db->iEx($iProv);
	} else {
		$resp = $db->uEx($uProv);
	}
	
	$db->close();

	return json_encode($resp);
}

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

	$func = new functions();

	$txtNit=$_POST['txtNit'];
	$txtProv=utf8_decode($_POST['txtProv']);

	# QUERYS
	$dProv = "DELETE FROM provee WHERE pro_nit='$txtNit'";

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

	return json_encode($resp);
}
?>