<?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 = "inv4010";
$bitMod = "inv";
$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 'consultarProvcp':
		echo consultarProvcp();
		break;

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

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

function consultar() {
	global $empCod;

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

	$func = new functions();

	$txtCod=utf8_decode($_POST['txtCod']);

	# QUERYS
	$sArtic = "SELECT a.*, 
				TRIM(a.art_cod) AS art_cod, 
				TRIM(a.art_desc) AS art_desc, 
				TRIM(a.art_desd) AS art_desd,
				TRIM(ctacos.cta_nom) AS art_ctacosnom,
				TRIM(ctainv.cta_nom) AS art_ctainvnom,
				TRIM(ctaven.cta_nom) AS art_ctavennom,
				TRIM(p.pro_nom) AS art_provnom,
				TRIM(sd.sdep_nom) AS sdep_nom
				FROM artic a
				LEFT JOIN cuentas ctacos ON a.art_emp = ctacos.cta_emp AND a.art_ctacos=ctacos.cta
				LEFT JOIN cuentas ctainv ON a.art_emp = ctainv.cta_emp AND a.art_ctainv=ctainv.cta
				LEFT JOIN cuentas ctaven ON a.art_emp = ctaven.cta_emp AND a.art_ctaven=ctaven.cta
				LEFT JOIN provcp p ON a.art_emp = p.pro_emp AND a.art_prov=p.pro_cod
				LEFT JOIN subdeptosi sd ON a.art_dep=sd.sdep_dep AND a.art_subdep=sd.sdep_cod
				WHERE a.art_emp='$empCod' AND a.art_cod='$txtCod'";

	$sArticbarr = "SELECT a.*, 
				TRIM(a.art_cod) AS art_cod, 
				TRIM(a.art_desc) AS art_desc, 
				TRIM(a.art_desd) AS art_desd,
				TRIM(ctaCos.cta_nom) AS art_ctaCosnom,
				TRIM(ctaInv.cta_nom) AS art_ctaInvnom,
				TRIM(ctaVen.cta_nom) AS art_ctaVennom,
				TRIM(p.pro_nom) AS art_provnom,
				TRIM(um.um_nom) AS um_nom,
				TRIM(cb.cb_cod) AS cb_cod,
				TRIM(cb.cb_barr) AS cb_barr,
				TRIM(sd.sdep_nom) AS sdep_nom
				FROM artic a
				LEFT JOIN cuentas ctaCos ON a.art_emp = ctaCos.cta_emp AND a.art_ctaCos=ctaCos.cta
				LEFT JOIN cuentas ctaInv ON a.art_emp = ctaInv.cta_emp AND a.art_ctaInv=ctaInv.cta
				LEFT JOIN cuentas ctaVen ON a.art_emp = ctaVen.cta_emp AND a.art_ctaven=ctaVen.cta
				LEFT JOIN provcp p ON a.art_emp = p.pro_emp AND a.art_prov=p.pro_cod
				LEFT JOIN unimed um ON a.art_medi=um.um_cod
				LEFT JOIN cod_barr cb ON a.art_emp=cb.cb_emp AND a.art_cod=cb.cb_cod 
				LEFT JOIN subdeptosi sd ON a.art_dep=sd.sdep_dep AND a.art_subdep=sd.sdep_cod
				WHERE a.art_emp='$empCod' AND cb.cb_barr='$txtCod'";



	function sExiart($codigo) { 
		global $empCod;
		return "SELECT e.*, TRIM(b.bod_nom) AS bod_nom, SUM(ea_exist) AS ea_existb
				FROM exiart_peps e 
				LEFT JOIN bodegas b ON e.ea_emp=b.bod_emp AND e.ea_bod=b.bod_cod
				WHERE e.ea_emp='$empCod' AND e.ea_cod='$codigo' AND e.ea_exist>=0
				GROUP BY ea_emp,ea_cod,ea_bod";
	}

	/* $sExiart = "SELECT e.*, TRIM(b.bod_nom) AS bod_nom
				FROM exiart e 
				LEFT JOIN bodegas b ON e.ea_emp=b.bod_emp AND e.ea_bod=b.bod_cod
				WHERE e.ea_emp='$empCod' AND e.ea_cod='$txtCod'"; */

	function sCod_barr($codigo) { 
		global $empCod;
		return "SELECT *,
				  TRIM(cb_barr) AS cb_barr
				 FROM cod_barr WHERE cb_emp='$empCod' AND cb_cod='$codigo'";
	}

	# EJECUCION DE QUERYS
	$db = new dbExec();
	$codigo=$txtCod;
	$resp = $db->sEx($sArtic, "artic");
	$select["artic"] = $db->query($sArtic);
	if ($db->numRows($select["artic"]) == 0) {
		$resp = $db->sEx($sArticbarr, "artic");
		$codigo=$resp["artic"]["art_cod"];
	}
	//$resp["exiart"] = $db->sFetchAll($sExiart);
	//$resp["imgArtic"] = "imgArticulos/$empCod"."_".$func->imgCharacters($txtCod).".jpeg?".rand(1,9999);
	$resp["exiart"] = $db->sFetchAll(sExiart($codigo));
	$resp["cod_barr"] = $db->sFetchAll(sCod_barr($codigo));
	$resp["imgArtic"] = "imgArticulos/$empCod"."_".$func->imgCharacters($codigo).".jpeg?".rand(1,9999);

	$db->close();

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

function consultarCta() {
	global $empCod;
	$txtCta=$_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 consultarProvcp() {
	global $empCod;
	$txtProv=utf8_decode($_POST['txtProv']);

	# QUERYS
	$sProvcp = "SELECT *, TRIM(pro_nom) AS pro_nom FROM provcp WHERE pro_emp='$empCod' AND pro_cod='$txtProv'";
	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp = $db->sEx($sProvcp, "provcp");
	$db->close();

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

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

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

	$func = new functions();

	$txtCod = utf8_decode($_POST["txtCod"]);
	$txtCodf = utf8_decode($_POST["txtCodf"]);
//	$txtCodBarr = utf8_decode($_POST["txtCodBarr"]);
	$txtDesc = utf8_decode($_POST["txtDesc"]);
	$txtDesd = utf8_decode($_POST["txtDesd"]);
	$txtCtaCos = utf8_decode($_POST["txtCtaCos"]);
	$txtCtaInv = utf8_decode($_POST["txtCtaInv"]);
	$txtCtaVen = utf8_decode($_POST["txtCtaVen"]);
	$txtProv = utf8_decode($_POST["txtProv"]);
	$txtMarca = utf8_decode($_POST["txtMarca"]);
	$selDeptoCod = utf8_decode($_POST["selDeptoCod"]);
	$selSubDeptoCod = utf8_decode($_POST["selSubDeptoCod"]);
	$selMedidaCod = utf8_decode($_POST["selMedidaCod"]);
	$numPre1 = (!empty($_POST["numPre1"])) ? $_POST["numPre1"] : 0;
	$numPre2 = (!empty($_POST["numPre2"])) ? $_POST["numPre2"] : 0;
	$numPre3 = (!empty($_POST["numPre3"])) ? $_POST["numPre3"] : 0;
	$numPre4 = (!empty($_POST["numPre4"])) ? $_POST["numPre4"] : 0;
	$numCosp = (!empty($_POST["numCosp"])) ? $_POST["numCosp"] : 0;
	$numCosr = (!empty($_POST["numCosr"])) ? $_POST["numCosr"] : 0;
	$numCosa = (!empty($_POST["numCosa"])) ? $_POST["numCosa"] : 0;
	$numMini = (!empty($_POST["numMini"])) ? $_POST["numMini"] : 0;
	$numMaxi = (!empty($_POST["numMaxi"])) ? $_POST["numMaxi"] : 0;
	$cospant = (!empty($_POST["cospant"])) ? $_POST["cospant"] : 0;
	$jsonDetbarr=json_decode($_POST['tblDetbarr']);

	$crBienSer = $_POST["crBienSer"];
	$crExen = $_POST["crExen"];
	$cbBloqueado = ($func->stringToBool($_POST['cbBloqueado'])) ? 1 : 0;

	$fecIng = date('Y-n-d');

	# QUERYS PARA UN REGISTRO
	$iArtic = "INSERT INTO artic (art_emp, art_cod, art_codf, art_dep, art_subdep, art_desc, art_desd, art_medi, art_mini, art_maxi, art_uent, art_usal, art_cosp, art_cosa, art_cosr, art_pre1, art_pre2, art_pre3, art_pre4, art_pre5, art_fecing, art_exen, art_serie, art_gru, art_marca, art_prov, art_codn, art_regsani, art_fecv1, art_fecv2, art_fecv3, art_natura, art_ibh, art_cosresp, art_bloqueado, art_ctacos, art_ctainv, art_ctaven, art_bienser) VALUES ('$empCod', '$txtCod', '$txtCodf', '$selDeptoCod', '$selSubDeptoCod', '$txtDesc', '$txtDesd', '$selMedidaCod', '$numMini', '$numMaxi', '0000-00-00', '0000-00-00', '$numCosp', '$numCosr', '$numCosa', '$numPre1', '$numPre2', '$numPre3', '$numPre4', '0.00', '$fecIng', '$crExen', '', '0', '$txtMarca', '$txtProv', '0', '', '0000-00-00', '0000-00-00', '0000-00-00', '', '0.00', '0.00000', $cbBloqueado, '$txtCtaCos', '$txtCtaInv', '$txtCtaVen', '$crBienSer')";

	$uArtic = "UPDATE artic SET art_codf='$txtCodf',art_dep='$selDeptoCod',art_subdep='$selSubDeptoCod',art_desc='$txtDesc',art_desd='$txtDesd',art_medi='$selMedidaCod',art_mini='$numMini',art_maxi='$numMaxi',art_pre1='$numPre1',art_pre2='$numPre2',art_cosp='$numCosp',art_exen='$crExen',art_marca='$txtMarca',art_prov='$txtProv',art_bloqueado='$cbBloqueado',art_ctacos='$txtCtaCos',art_bienser='$crBienSer' WHERE art_emp='$empCod' AND art_cod='$txtCod'";

	function uCostopeps($codd,$cosu) { 
		global $empCod, $usuNom;
		return "UPDATE exiart_peps SET ea_cosu='$cosu' WHERE ea_emp='$empCod' AND ea_cod='$codd' AND ea_exist>0";
	}

	$dCod_barr = "DELETE FROM cod_barr WHERE cb_emp='$empCod' AND cb_cod='$txtCod'";

	# QUERYS PARA VARIOS REGISTROS
	function iCod_barr($jsonDetbarr) {
		global $empCod, $txtCod;
		$func = new functions();
		$r = "INSERT INTO cod_barr (cb_emp, cb_cod, cb_barr) VALUES ";
		foreach ($jsonDetbarr as $obj) {
			$r = $r."('$empCod','$txtCod','$obj->codbarr')".$func->addValues($obj, $jsonDetbarr);
		}
		return $r;
	}

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	if ($numCosp>0) {
      $db->uEx(uCostopeps($txtCod,$numCosp));
    }
	if ($func->stringToBool($_POST['nuevo'])) {
		$resp = $db->iEx($iArtic);
		$db->iEx($func->iBitacora(utf8_decode("Se creó artículo, Código: $txtCod  Costo que tenia: $cospant Costo que grabo: $numCosp Usuario: ").$usuNom, $bitMod, $bitForm));
	} else {
		$resp = $db->uEx($uArtic);
		$db->iEx($func->iBitacora(utf8_decode("Se modificó artículo, Código: $txtCod  Costo que tenia: $cospant Costo que grabo: $numCosp  Usuario: ").$usuNom, $bitMod, $bitForm));
	}
	// Para el codigo de barras
	$db->dEx($dCod_barr);
	if ((sizeof($jsonDetbarr)>0)) {
	  $resp = $db->iEx(iCod_barr($jsonDetbarr));
	}
	
	$db->close();

	if (isset($_FILES['file'])) {
		$func->uploadEmpCod($empCod, $txtCod, $_FILES['file'], '../imgArticulos');
	}

	return json_encode($resp);
}

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

	$func = new functions();

	$txtCod=utf8_decode($_POST['txtCod']);

	# QUERYS
	$dArtic = "DELETE FROM artic WHERE art_emp='$empCod' AND art_cod='$txtCod'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	$resp = $db->dEx($dArtic);
	$db->iEx($func->iBitacora(utf8_decode("Se borró artículo, Código: $txtCod Usuario: ").$usuNom, $bitMod, $bitForm));
	$db->close();

	//$func->unlinkEmpCod($empCod, $txtCod, '../imgArticulos');

	return json_encode($resp);
}
?>