<?php
 
session_start();
include("DMMYSQLG.php");
include("DF80000.php");
?>
<?php

function CAJselectOptions(&$arrJSON,$valor,$etiqueta){
	$arrJSON[]="{valueOption:'$valor',labelOption:'$etiqueta'}";
}

function CSJselectOptions(&$strJSON,$arrJSON,$separador=","){
		 $strJSON="[".implode($separador,$arrJSON)."]";
}

function char_invalido($string, $reemplazo=' ',$opciones=' -_'){
	$permitidos = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	$permitidos = $permitidos.$opciones;
	$auxString;
	for($i=0 ; $i < strlen($string) ; $i++){
		if(strpos($permitidos, $string[$i]) === false){
			$reempaux=$reemplazo;
			if($string[$i]=='Ñ' || $string[$i]=='ñ')	$reempaux='n';
				$auxString=$auxString.$reempaux;
		}
		else
			$auxString=$auxString.$string[$i];
	}
	return $auxString;
}


//->Regresa Paises
if(isset($_POST["buscaProducto"])){
	$pattern="[A|a]+";
	$replacement=" ";
	if($_POST['r1']=='clave')
		$sq_invent=mysqli_query($con ,"SELECT * FROM  dginvent WHERE dginvent.modulo='".$_SESSION['mod_sis']."' AND   nparte1 ='".$_POST['tbus']."' order by nparte1");
	else
		$sq_invent=mysqli_query($con ,"SELECT * FROM  dginvent WHERE dginvent.modulo='".$_SESSION['mod_sis']."' AND   np_desc1 like '%".$_POST['tbus']."%' order by nparte1");
	if(mysqli_num_rows($sq_invent)>0){
		while($dt_invent=mysqli_fetch_array($sq_invent)){
			CAJselectOptions($jsonOptions,char_invalido($dt_invent['nparte1']),char_invalido($dt_invent['nparte1'].' '.$dt_invent['np_desc1']));
		}
		CSJselectOptions($jsonOptionsString,$jsonOptions,",");
		echo $jsonOptionsString;
	}
	else
		echo -1;
}
elseif(isset($_POST['accedeD'])){//alteraciones a Detalle ('A'->Agregar,'M'->Modificar,'E'->Eliminar,'C'->Consulta Reg)
	include("DH25000_CL.php");
	switch($_POST['accedeD']){
		case 'A':
			$objrequd=new requd('',$_POST['id_enc'],$_POST['cantidad'],$_POST['uni_med'],$_POST['descripcion'],$_POST['color'],$_POST['ancho'],$_POST['exis_alma'],$_POST['restante'],$_POST['pre_uni'],chfecha($_POST['fecha_ent'],'d/m/Y','Y-m-d'),chfecha($_POST['fecha_com'],'d/m/Y','Y-m-d'));
			if($objrequd->agregar()) echo '1';
			else echo '0';
		break;
		case 'M':
			$objrequd=new requd($_POST['numReg'],'',$_POST['cantidad'],$_POST['uni_med'],$_POST['descripcion'],$_POST['color'],$_POST['ancho'],$_POST['exis_alma'],$_POST['restante'],$_POST['pre_uni'],chfecha($_POST['fecha_ent'],'d/m/Y','Y-m-d'),chfecha($_POST['fecha_com'],'d/m/Y','Y-m-d'));
			if($objrequd->modificar()) echo '1';
			else echo '0';
		break;
		case 'E':
			$objrequd=new requd($_POST['numReg']);
			if($objrequd->eliminar()) echo '1';
			else echo '0';
		break;
		case 'C':
		break;
	}
}
elseif(isset($_POST['getProd'])){
	$nparte1=$_POST['nparte1'];
	$dt_invent=mysqli_fetch_array(mysqli_query($con ,"SELECT  dggrupos.des_uni as unidadMedida FROM  dginvent LEFT JOIN dggrupos ON(dginvent.cve_uni_com=dggrupos.cve_uni) WHERE dginvent.modulo='".$_SESSION['mod_sis']."' AND   nparte1 ='$nparte1' order by nparte1"));
	echo "[{uniMed:'".$dt_invent['unidadMedida']."'}]";
}
elseif(isset($_POST['generaTabla'])){
	include("DH25000_CL.php");
	if($_POST['generaTabla']=='C') $disabled=' disabled="disabled" ';
	$objrequd=new requd('',$_POST['id_enc']);
	$sq_requd=$objrequd->recuperarGrupo();
	echo '<table  align="center">';
	echo '<tr>
		  <th><div align="center">Sel.</div></th>
		  <th><div align="center">Cantidad</div></th>
		  <th><div align="center">Uni. Med. </div></th>
		  <th><div align="center">Descripcion</div></th>
		  <th><div align="center">Color</div></th>
		  <th><div align="center">Ancho</div></th>
		  <th><div align="center">Existencia Almacen </div></th>
		  <th><div align="center">Restante</div></th>
		  <th><div align="center">Precio Unitario </div></th>
		  <th><div align="center">Fecha de Entrega </div></th>
		  <th><div align="center">Fecha de Compra </div></th>
    	</tr>';
	while($dt_requd=mysqli_fetch_array($sq_requd)){
		echo '<tr>';
		echo '<td><div align="center"><input name="beliminar"'.$disabled.' id="beliminar'.$dt_requd['n_reg'].'" value="'.$dt_requd['n_reg'].'" type="image" src="logos/cerrar.gif" onClick="eliminaProd(this.value); return false;"/>&nbsp;</div></td>';
		echo '<td><div align="center">'.$dt_requd['cantidad'].'&nbsp;</div></td>';
		echo '<td><div align="center">'.$dt_requd['uni_med'].'&nbsp;</div></td>';
		echo '<td><div align="center">'.$dt_requd['descripcion'].'&nbsp;</div></td>';
		echo '<td><div align="center">'.$dt_requd['color'].'&nbsp;</div></td>';
		echo '<td><div align="center">'.$dt_requd['ancho'].'&nbsp;</div></td>';
		echo '<td><div align="center">'.$dt_requd['exis_alma'].'&nbsp;</div></td>';
		echo '<td><div align="center">'.$dt_requd['restante'].'&nbsp;</div></td>';
		echo '<td><div align="center">'.$dt_requd['pre_uni'].'&nbsp;</div></td>';		
		echo '<td><div align="center">'.chfecha($dt_requd['fecha_ent'],'Y-m-d','d/m/Y').'&nbsp;</div></td>';		
		echo '<td><div align="center">'.chfecha($dt_requd['fecha_com'],'Y-m-d','d/m/Y').'&nbsp;</div></td>';		
		echo '</tr>';
	}
	echo '</table>';
}
?>