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

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

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

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

function consultarClienped() {
	global $empCod;

	$numIdPed=$_POST['numIdPed'];

	# QUERYS
	$sClien_ped = "SELECT cped.*, TRIM(cped.clip_nom) AS clip_nom,
				TRIM(cped.clip_nit) AS clip_nit,
				TRIM(cped.clip_dir) AS clip_dir,
				TRIM(cped.clip_nom2) AS clip_nom2,
				TRIM(cped.clip_dir2) AS clip_dir2,
				TRIM(cped.clip_nom3) AS clip_nom3,
				TRIM(cped.clip_dir3) AS clip_dir3,
				TRIM(cped.clip_tel) AS pro_tel,
				TRIM(cped.clip_email) AS clip_email,
				TRIM(cped.clip_zona) AS clip_zona,
				TRIM(cped.clip_nomfac) AS clip_nomfac,
				TRIM(cped.clip_dirfac) AS clip_dirfac
				FROM clien_ped as cped
				WHERE Id='$numIdPed'";

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

	$func = new functions();

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



function guardar() {
	global $empCod, $usuNom, $bitMod, $bitForm,$numIdPed,$txtCel,$txtNom,$txtNit,$txtDir,$txtTel,$txtEmail,$txtZona,$txtNomfac,$txtDirfac,$txtObsPed;

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

	$func = new functions();

	$numIdPed=$_POST['numIdPed'];
	$txtCel=$_POST['txtCel'];
	$txtNom=$_POST['txtNom'];
	$txtNit=$_POST['txtNit'];
	$txtDir=$_POST['txtDir'];
	$txtNom2=$_POST['txtNom2'];
	$txtDir2=$_POST['txtDir2'];
	$txtNom3=$_POST['txtNom3'];
	$txtDir3=$_POST['txtDir3'];
	$txtTel=$_POST['txtTel'];
	$txtEmail=$_POST['txtEmail'];
	$txtZona=$_POST['txtZona'];
	$txtNomfac=$_POST['txtNomfac'];
	$txtDirfac=$_POST['txtDirfac'];
	$txtObsPed=$_POST['txtObsPed'];

	# QUERYS PARA UN REGISTRO

	$iClien_ped = "INSERT INTO clien_ped(clip_cel, clip_nit, clip_nom, clip_dir,  clip_nom2, clip_dir2,  clip_nom3, clip_dir3, clip_tel, clip_email, clip_zona, clip_nomfac, clip_dirfac, clip_obs) VALUES ('$txtCel','$txtNit','$txtNom','$txtDir','$txtNom2','$txtDir2','$txtNom3','$txtDir3','$txtTel','$txtEmail','$txtZona','$txtNomfac','$txtDirfac','$txtObsPed')";

	$uClien_ped = "UPDATE clien_ped SET clip_cel='$txtCel',clip_nit='$txtNit',clip_nom='$txtNom',clip_dir='$txtDir',clip_nom2='$txtNom2',clip_dir2='$txtDir2',clip_nom3='$txtNom3',clip_dir3='$txtDir3',clip_tel='$txtTel',clip_email='$txtEmail',clip_zona='$txtZona',clip_nomfac='$txtNomfac',clip_dirfac='$txtDirfac',clip_obs='$txtObsPed' WHERE id='$numIdPed'";

	$sClien = "SELECT * FROM clien WHERE cli_nit='$txtNit'";

	$sTABLESPed = "SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_NAME='clien_ped' AND TABLE_SCHEMA='{$_SESSION['nombredb']}'";

	function uClien($idnumped){
		global $empCod,$txtNit,$txtNomfac,$txtDirfac,$txtTel,$txtCel,$txtZona,$txtEmail,$txtObsPed;
		return "UPDATE clien SET cli_nom='$txtNomfac', cli_dir='$txtDirfac', cli_tel='$txtTel', cli_cel='$txtCel', cli_zona='$txtZona', cli_email='$txtEmail',cli_obsped='$txtObsPed', cli_idped='$idnumped' WHERE cli_nit='$txtNit'";
	}

	function iClien($idnumped){
		global $empCod,$txtNit,$txtNomfac,$txtDirfac,$txtTel,$txtCel,$txtZona,$txtEmail,$txtObsPed;
		return "INSERT INTO clien (cli_nit, cli_nom, cli_dir, cli_tel, cli_cel, cli_zona, cli_email, cli_obsped, cli_idped) VALUES('$txtNit','$txtNomfac','$txtDirfac','$txtTel', '$txtCel','$txtZona','$txtEmail','$txtObsPed','$idnumped')";
	}

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	if ($func->stringToBool($_POST['nuevo'])) {
		$db->iEx($iClien_ped);
		$db->iEx($func->iBitacora(utf8_decode("Se creó Cliente, Celular: $txtCel   txtNom Usuario: ").$usuNom, $bitMod, $bitForm));
	} else {
		$db->uEx($uClien_ped);
		$db->iEx($func->iBitacora(utf8_decode("Se modificó Cliente, Celular: $txtCel   $txtNom Usuario: ").$usuNom, $bitMod, $bitForm));
	}

    $idnumped=$numIdPed;
	if ($numIdPed==0) {
	   $idnumped = $db->sFetchAssoc($sTABLESPed)['AUTO_INCREMENT'];
	}

	$select["clien"] = $db->query($sClien);
	if ($db->numRows($select["clien"]) == 0) {
    	$resp = $db->iEx(iClien($idnumped));
	} else {
    	$resp = $db->uEx(uClien($idnumped));
	}	
	
	$db->close();

	return json_encode($resp);
}

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

	$func = new functions();

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

	# QUERYS
	$dClien_ped = "DELETE FROM clien_ped WHERE id='$numIdPed'";

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

	return json_encode($resp);
}
?>