<?php
header("Content-Type: text/html;charset=utf-8");
require_once("dbcontroller.php");
include("JSON.php");
/* 
A domain Class to demonstrate RESTful web services
*/
Class Mobile {
	private $mobiles = array();
	
	public function getUsuario(){
	   if (isset($_REQUEST['usuario'])){$usuario=$_REQUEST['usuario'];}else{$usuario="";}
	   if (isset($_REQUEST['codemp'])){$codemp=$_REQUEST['codemp'];}else{$codemp="";}
	   if (isset($_REQUEST['extension'])){$extusr=$_REQUEST['extension'];}else{$extusr="";}
	   if (isset($_REQUEST['rol'])){$rol=$_REQUEST['rol'];}else{$rol="";}
	   if($rol=="empleado"){
		   $codemp="";
	   }
	   if($codemp!=''){$DBclase="DBC".$codemp;}else{$DBclase="DBController";}
		    $nomtab="usr_".$extusr;
			$query = "SELECT nomper as nombre,nomusu as usuario,conusu as clave,nriper,peremp as empresas,dftemp FROM ".$nomtab." where nomusu='".$usuario."'" ;
			 if($rol=="empleado"){
				 $query=$query." and conusu='8057' ";
	   		}
			//echo $query;
			$dbcontroller = new $DBclase();
			$this->mobiles = $dbcontroller->executeSelectQuery($query);
			
			return $this->mobiles;
			/* Mas adelante debemos desencriptar la clave aqui y no en javascript*/
	}	
	
	public function getLicencia(){
		if (isset($_REQUEST['nit'])){$nit=$_REQUEST['nit'];}else{$nit="";}
		if (isset($_REQUEST['codemp'])){$codemp=$_REQUEST['codemp'];}else{$codemp="";}
		if($codemp!=''){$DBclase="DBC".$codemp;}else{$DBclase="DBController";}
		//echo DBclase;
	   $conng = mysqli_connect('66.70.255.14','netempire_elsycb','Tm3Y@1CtWn67','netempire_elsy');
	   $conng-> set_charset("utf8");
	   if($result = $conng->query("select * from licemp where conedb='".$codemp."'")) {
   			$nr= $result->num_rows;
			if($nr>=1){
				while($row=mysqli_fetch_assoc($result)) {
			       $nit =  $row["nriper"];
           	   	} mysqli_free_result($result);
				//echo $nit;
				$query = "SELECT nitemp as nit,nomcom as nomcomercial,nomemp as nomempresa,diremp as dirempresa,fteodb FROM admemp where nitemp='".$nit."'" ;
				//$dbcontroller = new DBController();
				$dbcontroller = new $DBclase;
				$this->mobiles = $dbcontroller->executeSelectQuery($query);
				return $this->mobiles;
			}
	   }
	}
	
	public function getActividad(){
		$syseli="Eliminado";
		if (isset($_REQUEST['tipo'])){$tipo=$_REQUEST['tipo'];}else{$tipo="";}
	   	if (isset($_REQUEST['actividad'])){$actividad=$_REQUEST['actividad'];}else{$actividad="";}
		if (isset($_REQUEST['observacion'])){$observacion=$_REQUEST['observacion'];}else{$observacion="";}
		if($tipo=="r"){
			$query = "insert into lawact(nomact,obsact) values('".$actividad."','".$observacion."')";
			$dbcontroller = new DBController();
			$Resp = $dbcontroller->executeInsertQuery($query);
			if($Resp!="ok"){
				return "ERROR";
			}
		}
		if($tipo=="l"){
			$query = "SELECT * FROM lawact where estact!='".$syseli."' order by nomact" ;
			$dbcontroller = new DBController();
			$this->mobiles = $dbcontroller->executeSelectQuery($query);
			return $this->mobiles;
		}
		
	}	
	
	public function getFe(){
		$syseli="Eliminado";
		if (isset($_REQUEST['factura'])){$factura=$_REQUEST['factura'];}else{$factura="";}
		if($tipo=="l"){
			$query = "SELECT * FROM documentos where codigo!='".$factura."' " ;
			$dbcontroller = new DBController();
			$this->mobiles = $dbcontroller->executeSelectQuery($query);
			return $this->mobiles;
		}
	}	
	
	public function getEspecialidad(){
		$syseli="Eliminado";
		if (isset($_REQUEST['tipo'])){$tipo=$_REQUEST['tipo'];}else{$tipo="";}
	   	if (isset($_REQUEST['especialidad'])){$especialidad=$_REQUEST['especialidad'];}else{$especialidad="";}
		if (isset($_REQUEST['observacion'])){$observacion=$_REQUEST['observacion'];}else{$observacion="";}
		if($tipo=="r"){
			$query = "insert into lawesp(nomesp,obsesp) values('".$especialidad."','".$observacion."')";
			$dbcontroller = new DBController();
			$Resp = $dbcontroller->executeInsertQuery($query);
			if($Resp!="ok"){
				return "ERROR";
			}
		}
		if($tipo=="l"){
			$query = "SELECT * FROM lawesp where estesp!='".$syseli."' order by nomesp" ;
			$dbcontroller = new DBController();
			$this->mobiles = $dbcontroller->executeSelectQuery($query);
			return $this->mobiles;
		}
		
	}
	
	public function getProceso(){
		$syseli="Eliminado";
		if (isset($_REQUEST['tipo'])){$tipo=$_REQUEST['tipo'];}else{$tipo="";}
		if (isset($_REQUEST['proceso'])){$proceso=$_REQUEST['proceso'];}else{$proceso="";}
	   	if (isset($_REQUEST['especialidad'])){$especialidad=$_REQUEST['especialidad'];}else{$especialidad="";}
		if (isset($_REQUEST['observacion'])){$observacion=$_REQUEST['observacion'];}else{$observacion="";}
		if($tipo=="r"){
			$query = "insert into lawpro(nompro,codesp,nomesp,obspro) values('".$proceso."','".$especialidad."','".$especialidad."','".$observacion."')";
			$dbcontroller = new DBController();
			$Resp = $dbcontroller->executeInsertQuery($query);
			if($Resp!="ok"){
				return "ERROR";
			}
		}
		if($tipo=="l"){
			$query = "SELECT * FROM lawpro where estpro!='".$syseli."' order by nompro" ;
			$dbcontroller = new DBController();
			$this->mobiles = $dbcontroller->executeSelectQuery($query);
			return $this->mobiles;
		}
		
	}	
	
	public function getModulo(){
		$syseli="Eliminado";
		if (isset($_REQUEST['tipo'])){$tipo=$_REQUEST['tipo'];}else{$tipo="";}
		if (isset($_REQUEST['modulo'])){$modulo=$_REQUEST['modulo'];}else{$modulo="";}
		if (isset($_REQUEST['accion'])){$accion=$_REQUEST['accion'];}else{$accion="";}
		if($tipo=="r"){
			$query = "insert into lawpro(nompro,codesp,nomesp,obspro) values('".$proceso."','".$especialidad."','".$especialidad."','".$observacion."')";
			$dbcontroller = new DBController();
			$Resp = $dbcontroller->executeInsertQuery($query);
			if($Resp!="ok"){
				return "ERROR";
			}
		}
		if($tipo=="l"){
			if($accion=="e"){
				$query = "SELECT moddet.*,modcmp.titmod,modcmp.obsmod,modcmp.nommod FROM moddet,modcmp where moddet.modulo=modcmp.nommod and moddet.estado!='".$syseli."' and moddet.modulo='".$modulo."' and moddet.chkpub!=2 order by moddet.bloque,moddet.orden,moddet.nombre" ;
			}
			if($accion=="r"){
				$query = "SELECT moddet.*,modcmp.titmod,modcmp.obsmod,modcmp.nommod FROM moddet,modcmp where moddet.modulo=modcmp.nommod and moddet.estado!='".$syseli."' and moddet.modulo='".$modulo."' and  moddet.chkinf=1 order by moddet.bloque,moddet.orden,moddet.nombre" ;
			}
			if($accion!="e" && $accion!="r"){
				$query = "SELECT moddet.*,modcmp.titmod,modcmp.obsmod,modcmp.nommod FROM moddet,modcmp where moddet.modulo=modcmp.nommod and moddet.estado!='".$syseli."' and moddet.modulo='".$modulo."' and moddet.chkpub=1 order by moddet.bloque,moddet.orden,moddet.nombre" ;
			}
			//echo $query;
			$dbcontroller = new DBController();
			$this->mobiles = $dbcontroller->executeSelectQuery($query);
			return $this->mobiles;
		}
		
	}
	
	public function getRegistro(){
		
		$syseli="Eliminado";
		if (isset($_REQUEST['tipo'])){$tipo=$_REQUEST['tipo'];}else{$tipo="";}
		if (isset($_REQUEST['modulo'])){$modulo=$_REQUEST['modulo'];}else{$modulo="";}
		if (isset($_REQUEST['campo'])){$vcampo=$_REQUEST['campo'];}else{$vcampo="";}
		if (isset($_REQUEST['valor'])){$valor=$_REQUEST['valor'];}else{$valor="";}
		if (isset($_REQUEST['accion'])){$accion=$_REQUEST['accion'];}else{$accion="";}
		if (isset($_REQUEST['jsondatos'])){$jsondatos=$_REQUEST['jsondatos'];}else{$jsondatos="";}
		if (isset($_REQUEST['jsoncampos'])){$jsoncampos=$_REQUEST['jsoncampos'];}else{$jsoncampos="";}
		if (isset($_REQUEST['condicion'])){$condicion=$_REQUEST['condicion'];}else{$condicion="";}
		if (isset($_REQUEST['orden'])){$orden=$_REQUEST['orden'];}else{$orden="";}
		if (isset($_REQUEST['accion'])){$accion=$_REQUEST['accion'];}else{$accion="";}
		if (isset($_REQUEST['rol'])){$rol=$_REQUEST['rol'];}else{$rol="";}
		if (isset($_REQUEST['identidad'])){$identidad=$_REQUEST['identidad'];}else{$identidad="";}
		if (isset($_REQUEST['consld'])){$consld=$_REQUEST['consld'];}else{$consld=0;}
		if (isset($_REQUEST['j'])){$j=$_REQUEST['j'];}else{$j=0;}
		if (isset($_REQUEST['codemp'])){$codemp=$_REQUEST['codemp'];}else{$codemp="";}
		if($codemp!='' && $modulo!='modulo' && $rol!='empleado'){
			$DBclase="DBC".$codemp;
		}else{
			$DBclase="DBController";
		}
		if($modulo=='usuariosakd'){$DBclase="DBCakdcomunicate";}
		$jsondatos=utf8_decode($jsondatos);
		//echo $rol.$tipo;
		if($tipo=="r"){
			$tabla="";
			$conng = mysqli_connect('66.70.255.14','netempire_elsycb','Tm3Y@1CtWn67','netempire_elsy');
	   		$conng-> set_charset("utf8");
			if($result = $conng->query("select tabla,tabadi,datadi,proyecto from modcmp where nommod='".$modulo."'")) {
   				$nr= $result->num_rows;
				if($nr>=1){
					while($row=mysqli_fetch_assoc($result)) {
			           	$tabla =  $row["tabla"];
						$tabla2 =  $row["tabadi"];
						$camposadi = $row["datadi"];
						if($row["proyecto"]=='adm'){$DBclase="DBController";}
            	   	} mysqli_free_result($result);
				}
			}
			 
			if($tabla==""){
				return "ERROR";
			}
			
			//$jsondatos=utf8_encode($jsondatos);
 			$dcod = json_decode($jsoncampos);
 			$prop = get_object_vars ( $dcod );
 			$ddat = json_decode($jsondatos);
 			$pdat = get_object_vars ( $ddat );
			//echo $ddat;
			//echo $jsondatos;
 			if($accion=="e"){
				$i=0;
				while ($i < $j) {
					if($i>0 ){
						$campo=$campo.',';
						
					}
					$campo= $campo.$prop['c'.$i].'="'.$pdat[$prop['c'.$i]].'"';
					$i++;
				}
				$query=" update ".$tabla." set ".$campo." where Id='".$vcampo."'";
			}
			if($accion!="e"){
				$campo="insert into ".$tabla."(";
				$datos=" values(";
				if($tabla2!=""){
					$camposa="";
					$datosa="";
				}
				//echo $j;
				$i=0;
				while ($i < $j) {
					if($i>0){
						$campo=$campo.',';
						$datos=$datos.',';
					}
					$campo= $campo.$prop['c'.$i];
					$datos=$datos.'"'.$pdat[$prop['c'.$i]].'"';
					if($camposadi!=''){
						$pos=strpos($camposadi,$prop['c'.$i]);
						if ($pos !== false) {
							if($camposa!=""){
								$camposa=$camposa.','; 
								$datosa=$datosa.',';
							}
							$camposa= $camposa.$prop['c'.$i];
							$datosa = $datosa.'"'.$pdat[$prop['c'.$i]].'"';
						}
					}
					$i++;
				}
				$campo= $campo.')';
				$datos= $datos.')';
				$query = $campo.$datos;
			}
			//echo $camposa;
			//echo $tabla2;
			//echo $query;
			//echo $DBclase;
			//echo $jsondatos;
			$dbcontroller = new $DBclase();
			$Resp = $dbcontroller->executeInsertQuery($query);
			if($Resp!="ok"){
				return "ERROR";
			}else{
				if($tabla2!=""){//Aqui agrego el insert adicional que me permite crear el rol de usuario.
					//return "ERROR";
					$camposa= $camposa.')';
					$datosa= $datosa.')';
					$query = "insert into ".$tabla2."(".$camposa." values (".$datosa;
					echo $query;
					$dbcontroller = new $DBclase();
					$Resp = $dbcontroller->executeInsertQuery($query);
					if($Resp!="ok"){
						return "ERROR";
					}
				}
			}
		}
		if($tipo=="l"){
			$conng = mysqli_connect('66.70.255.14','netempire_elsycb','Tm3Y@1CtWn67','netempire_elsy');
	   		$conng-> set_charset("utf8");
	   		if($result = $conng->query("select listasql,tabla,sqlinit,sqledit,sqllost,tabadi,datadi,proyecto,sqlres from modcmp where nommod='".$modulo."'")) {
   				$nr= $result->num_rows;
				if($nr>=1){
					while($row=mysqli_fetch_assoc($result)) {
		            	$query =  $row["listasql"];
						if($rol=="empleado"){
							$query =  $query." and nriper='".$identidad."'";
						}
						if($accion=="i"){
							$query =  $row["sqlinit"];
						}
						$tabla = $row["tabla"];
						if($row["proyecto"]=='adm' || $rol=='empleado' ){$DBclase="DBController";}//Asigno la base de datos dnd para datos generales
						if($accion=="e"){
							$query=$row["sqledit"];
							//$query="select * from ".$tabla." where estado!='Eliminado' and id='".$vcampo."'";		
							$query=	$query." and id='".$vcampo."' ";				
						}
						
						if($accion=="b"){
							if($consld==0){
								$query=$row["sqllost"];
							}else{
								$query=$row["sqlres"];//Para consultar resumen de la busqueda
							}
							if($condicion!=''){
								$condicion=str_replace($modulo,$tabla,$condicion);//reemplazo modulo por tabla, para que no afecte si hay dobles campos en consulta compuesta
								$query=$query." ".$condicion;
							}else{
								$query = str_replace("CCC", $vcampo, $query);
								$query = str_replace("VVV", $valor, $query);	
							}
							if($orden!=""){
								$orden=str_replace($modulo,$tabla,$orden);//reemplazo modulo por tabla, para que no afecte si hay dobles campos en consulta compuesta
								$query=$query." order by ".$orden;
							}
							if($rol=="empleado" && $modulo=='volantesnom'){//esto para volantes de nomina rol empleado
								$query =  $query." and nomweb.nriper='".$identidad."'";
							}
							//echo $DBclase;
							//echo $query;		
						}
						if($modulo=="facturae"){
							$query="select documentos.*,licemp.keyfel,licemp.modope,prvtec.rutfel,prvtec.rutnce from documentos,licemp,prvtec where licemp.prvfel=prvtec.codprv and documentos.codemp=licemp.conedb and documentos.estado!='Eliminado' and documentos.codigo='".$vcampo."'";							
						}

                	} mysqli_free_result($result);
					//echo $DBclase;
					//echo $query;
					$dbcontroller = new $DBclase();
					//echo $dbcontroller;
					$this->mobiles = $dbcontroller->executeSelectQuery($query);
					return $this->mobiles;
				}
			}
		}
		
	}
	
	public function getMenu(){
		$syseli="Eliminado";
		if (isset($_REQUEST['tipo'])){$tipo=$_REQUEST['tipo'];}else{$tipo="";}
		if (isset($_REQUEST['proyecto'])){$proyecto=$_REQUEST['proyecto'];}else{$proyecto="";}
		if (isset($_REQUEST['rol'])){$rol=$_REQUEST['rol'];}else{$rol="";}
		if (isset($_REQUEST['codemp'])){$codemp=$_REQUEST['codemp'];}else{$codemp="";}
		
		//if($codemp!='' && $codemp!='dnd'){$DBclase="DBC".$codemp;}else{$DBclase="DBController";}
		
		if($tipo=="r"){
			$query = "insert into lawpro(nompro,codesp,nomesp,obspro) values('".$proceso."','".$especialidad."','".$especialidad."','".$observacion."')";
			$dbcontroller = new $DBclase();
			$Resp = $dbcontroller->executeInsertQuery($query);
			if($Resp!="ok"){
				return "ERROR";
			}
		}
		if($tipo=="l"){
			$query = "SELECT nommod,titmod,obsmod,link,proyecto,menu,smenu FROM modcmp order by nommod" ;
			if($proyecto!="Adm" && $proyecto!="Admempire"){
				$query = "SELECT nommod,titmod,obsmod,link,proyecto,menu,smenu FROM modcmp where (proyecto like'%".$proyecto."%' ) ";
				if($rol!="" and $rol!="admin"){
					$query = $query." and rol='".$rol."' ";
				}
				$query = $query." order by menu,smenu,orden";
				}
			$dbcontroller = new DBController();
			$this->mobiles = $dbcontroller->executeSelectQuery($query);
			return $this->mobiles;
		}
		
	}	
	

}
?>