<?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');
include_once('../../../php/maxExecTime.php');

$bitForm = "inv2150";
$bitMod = "inv";
$empCod = $_SESSION['empCod'];
$usuCod = utf8_decode($_SESSION['usuCod']);
$usuNom = utf8_decode($_SESSION['usuNom']);

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

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

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

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


function consultarArtic() {
	global $empCod;

	$func = new functions();

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

	# 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(um.um_nom) AS um_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
				WHERE a.art_emp='$empCod' AND a.art_cod='$txtCodd'";

	$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 
				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 
				WHERE a.art_emp='$empCod' AND cb.cb_barr='$txtCodd'";



	function sExiart($codigo) { 
		global $empCod;
		return "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='$codigo'";
	}

	$sCod_barr = "SELECT * FROM cod_barr WHERE cb_emp='$empCod' AND cb_cod='$txtCodd'";
	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$codigo=$txtCodd;
	$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($codigo));
	//$resp["imgArtic"] = "imgArticulos/$empCod"."_".$func->imgCharacters($codigo).".jpeg?".rand(1,9999);
	$db->close();

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

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

	# QUERYS
	$sProvcp = "SELECT *, TRIM(pro_nom) AS pro_nom, TRIM(pro_retsn) AS pro_retsn FROM provcp 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 consultarDoc() {
	global $empCod;

	$dtFeci=$_POST['dtFecI'];
	$dtFecf=$_POST['dtFecF'];
	$txtCodd=TRIM($_POST['txtCodd']);
	$selBodega=$_POST['selBodega'];
	$selTipo=$_POST['selTipo'];

	# QUERYS
	if ($selBodega=='0000') {
	    $sDoctose = "SELECT *, TRIM(de.de_obs) AS de_obs, TRIM(td.td_nom) AS td_nom, TRIM(pc.pro_nom) AS pro_nom, TRIM(bod.bod_nom) AS bod_nom, TRIM(bods.bod_nom) AS bod_noms, TRIM(bods.bod_cod) AS bod_cods
	       FROM doctose de 
	       LEFT JOIN doce_det ded ON ded.ded_emp=de.de_emp and ded.ded_tipo=de.de_tipo and ded.ded_num=de.de_num 
	       LEFT JOIN tipodoci td ON  ded.ded_tipo=td.td_cod 
	       LEFT JOIN bodegas bod ON  de.de_bodent=bod.bod_cod 
	       LEFT JOIN bodegas bods ON  de.de_bodsal=bods.bod_cod 
	       LEFT JOIN provcp pc ON  de.de_codp=pc.pro_cod 
	       WHERE de_emp='$empCod' AND de_tipo='$selTipo' AND de_fec>='$dtFeci' AND de_fec<='$dtFecf' AND ded_cod LIKE '%$txtCodd%' GROUP BY de_emp,de_tipo,de_num ORDER BY de_fec,de_tipo,de_num";
	} else {
	    $sDoctose = "SELECT *, TRIM(de.de_obs) AS de_obs, TRIM(td.td_nom) AS td_nom, TRIM(pc.pro_nom) AS pro_nom, TRIM(bod.bod_nom) AS bod_nom, TRIM(bods.bod_nom) AS bod_noms, TRIM(bods.bod_cod) AS bod_cods
	       FROM doctose de 
	       LEFT JOIN doce_det ded ON ded.ded_emp=de.de_emp and ded.ded_tipo=de.de_tipo and ded.ded_num=de.de_num 
	       LEFT JOIN tipodoci td ON  ded.ded_tipo=td.td_cod 
	       LEFT JOIN bodegas bod ON  de.de_bodent=bod.bod_cod 
	       LEFT JOIN bodegas bods ON  de.de_bodsal=bods.bod_cod 
	       LEFT JOIN provcp pc ON  de.de_codp=pc.pro_cod 
	       WHERE de_emp='$empCod' AND (de_bodent='$selBodega' OR de_bodsal='$selBodega')AND de_tipo='$selTipo' AND de_fec>='$dtFeci' AND de_fec<='$dtFecf' AND ded_cod LIKE '%$txtCodd%' GROUP BY de_emp,de_tipo,de_num ORDER BY de_fec,de_tipo,de_num";
	}	
	# EJECUCION DE QUERYS
	$db = new dbExec();
//	$select["doctose"] = $db->query($sDoctose);
	$resp["doctose"] = $db->sFetchAll($sDoctose);
	$db->close();

	$func = new functions();
//	$resp["prueba"] = "prueba";

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

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

	$_SESSION["print"] = array(
		'dtFecI'		=>$_POST["dtFecI"],
		'dtFecF'		=>$_POST["dtFecF"],
		'txtCodd'		=>$_POST["txtCodd"],
		'txtDescd'		=>$_POST["txtDescd"],
		'selBodega'		=>$_POST["selBodega"],
		'BodegaNom'		=>$_POST["BodegaNom"]
	);
	return "";
}
?>