<?php
 
require_once("DF80000.php");
require_once("RG16000_CL.php");
class dieTa{
	var $tabla;
	var $fnombre;
	var $fapellido;
	var $ftipocto;
	var $fnumcto;
	//var $id_dieta;
	//var $cve_diet;
	//var $nom_diet;
	//var $cantidad;
	//var $es_act;
	//var $id_tipdie;
	//var $des_diet;
	//var $sub_dieta;
	//var $iva_dieta;
	//var $tot_dieta;
	//var $fec_alt;
	//var $fec_act;
	//var $acc_fec;
	//var $acc_hor;
	//var $usuario;
	var $lastInsId;


	function dieTa($data){
		$this->tabla='valeryus19';
		$this->resetData($data);
	}

	function resetData($data){
		$this->fnombre=$this->checkData($data['fnoMbrE'],'');
		//$this->id_dieta=$this->checkData($data['idDieTa'],'');
		//$this->cve_diet=$this->checkData($data['cveDieT'],'');
		//$this->nom_diet=$this->checkData($data['nomDieT'],'');
		//$this->cantidad=$this->checkData($data['canTidAd'],'');
		//$this->es_act=$this->checkData($data['esAct'],'0');
		//$this->id_tipdie=$this->checkData($data['idTipDie'],'');
		//$this->des_diet=$this->checkData($data['desDieT'],'');
		//$this->sub_dieta=$this->checkData($data['subDieta'],'');
		//$this->iva_dieta=$this->checkData($data['ivaDieta'],'');
		//$this->tot_dieta=$this->checkData($data['totDieta'],'');
		//$this->fec_alt=$this->checkData($data['fecAlt'],date('Y-m-d'));		
		//$this->fec_act=$this->checkData($data['fecAct'],'0000-00-00');		
		//$this->acc_fec=$this->checkData($data['accFec'],date('Y-m-d'));
		//$this->acc_hor=$this->checkData($data['accHor'],date('H:i:s'));
		//$this->usuario=$this->checkData($data['usuAriO'],$_SESSION['num_usu']);
	}
	function busReg(){
		//$sq_retrieve="SELECT * FROM $this->tabla WHERE id_dieta='$this->id_dieta'";
		$sq_retrieve="SELECT * FROM $this->tabla WHERE n_reg='$this->n_reg'";
		
		$rs=mysqli_query($con ,$sq_retrieve);
		if($rs)
			return $rs;
		else
			return 0;
	}

	function busByEnc(){
		$sq_retrieve="SELECT * FROM $this->tabla WHERE id_dieta ='$this->id_dieta'";
		
		$rs=mysqli_query($con ,$sq_retrieve);
		if($rs)
			return $rs;
		else
			return 0;
		
	}
	function actTotDieta(){
		$sq_update="UPDATE $this->tabla SET sub_dieta='$this->sub_dieta', iva_dieta='$this->iva_dieta', tot_dieta='$this->tot_dieta', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario' WHERE id_dieta='$this->id_dieta'";
		
		$rs=mysqli_query($con ,$sq_update);
		if($rs)
			return 1;
		else
			return 0;	
	}
	function actStaDieta(){
		$sq_update="UPDATE $this->tabla SET es_act='$this->es_act', fec_act='$this->fec_act', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario' WHERE id_dieta='$this->id_dieta'";
		$rs=mysqli_query($con ,$sq_update);
		if($rs)
			return 1;
		else
			return 0;	
	}	
	   function getReportDatas(){
		$sq_retrieves="SELECT *,tot_dieta as totFac FROM $this->tabla";
		$rs=mysqli_query($con ,$sq_retrieves);
		if($rs)
			return $rs;
		else
			return 0;
	}
	function getReportData(){
		$sq_retrieve="SELECT * FROM $this->tabla";
		
		$rs=mysqli_query($con ,$sq_retrieve);
		if($rs)
			return $rs;
		else
			return 0;
	}
	function getRegBySta(){
		$sq_retrieve="SELECT * FROM $this->tabla WHERE es_act='$this->es_act'";
		$rs=mysqli_query($con ,$sq_retrieve);
		if($rs)
			return $rs;
		else
			return 0;
	}
	
	function getDataBus(){
		$fecIni=chfecha($_POST['fecini'],'d/m/Y','Y-m-d');
		$fecFin=chfecha($_POST['fecfin'],'d/m/Y','Y-m-d');		
		$dataBus[]="$this->tabla.fec_act BETWEEN '$fecIni' AND '$fecFin'";
		if($_POST['setipdie']!="T"){
			$dataBus[]="$this->tabla.id_tipdie='".mysqli_real_escape_string($con,$_POST['id_cli'])."'";	
		}
		return $dataBus;
	}
	
	function cat_getDataBus(){
		$fecIni=chfecha($_POST['fecini'],'d/m/Y','Y-m-d');
		$fecFin=chfecha($_POST['fecfin'],'d/m/Y','Y-m-d');		
		$staRec=$_POST['serecact'];
		$idRec=$_POST['bot'];
		if(isset($_POST['bot'])){
			$dataBus[]="$this->tabla.id_dieta ='$idRec'";					
		}elseif(isset($_POST['fecini'])){
			$dataBus[]="$this->tabla.fec_alt BETWEEN '$fecIni' AND '$fecFin'";
			if($staRec!='T')
				$dataBus[]="$this->tabla.es_act ='$staRec'";		

		}else{
			$dataBus[]="$this->tabla.es_act ='0'";		
		}
		return $dataBus;
	}	
	function cat_getData(){
		$tipoDieta = new tipDie($data);
		$tabTipDie = $tipoDieta->getTableName();
		$arrDataBus=$this->cat_getDataBus();
		$WHERE="WHERE ".implode(" AND ",$arrDataBus);
		$sq_retrieve="
			SELECT 
				$this->tabla.n_reg as nreG,
				$this->tabla.fnombre as fnoMbrE,
				$this->tabla.fapellido as desDieta,
				$this->tabla.ftipocto as obsDieta,
				$this->tabla.fnumcto as canDieta
				
				
				FROM $this->tabla
	
				WHERE 
			$this->tabla.n_reg ='$this->n_reg'";
			
				//$this->tabla.des_diet as obsDieta,
				//$this->tabla.cantidad as canDieta,
				//$this->tabla.id_tipdie as idTipdie,
				//$tabTipDie.cve_tip as cveTipDieta,
				//$tabTipDie.nom_tip as desTipDieta,
				//$tabTipDie.des_tip as obsTipDieta,
				//DATE_FORMAT($this->tabla.fec_alt,'%d/%m/%Y') as fecAltDieta,
				//$this->tabla.es_act as esActDieta, 
				//IF($this->tabla.es_act='1','SI','NO') desEsActDieta,			
				//DATE_FORMAT($this->tabla.fec_act,'%d/%m/%Y') as fecActDieta,
				//$this->tabla.sub_dieta as subDieta,
				//$this->tabla.iva_dieta as ivaDieta,
				//$this->tabla.tot_dieta as totDieta
			//FROM $this->tabla 
				//INNER JOIN $tabTipDie ON($tabTipDie.id_tipdie=$this->tabla.id_tipdie)
			//$WHERE
			//ORDER BY
				//$this->tabla.fec_alt,$this->tabla.fec_act"
		$rs=mysqli_query($con ,$sq_retrieve);
		echo mysqli_error($con);		
		if($rs)
			return $rs;
		else
			return 0;		
	}
	
	function checkData($data,$default){
		if(isset($data)) 
			$auxData=$data;
		else 
			$auxData=$default;
		if(get_magic_quotes_gpc()) 
			$auxData = stripslashes($auxData);
		return mysqli_real_escape_string($con,$auxData);
	}

	function getLastInsId(){
		return $this->lastInsId;
	}

	function getMySQLError(){
		return 'Error N:' . mysqli_errno($con) . ": " . mysqli_error($con); 
	}

}
class detDie{
	var $tabla;
	var $id_detdie;
	var $id_dieta;
	var $nparte1;
	var $cantidad;
	var $pre_uni;
	var $cve_iva;
	var $sub_det;
	var $iva_det;
	var $tot_det;
	var $acc_fec;
	var $acc_hor;
	var $usuario;
	var $lastInsId;

	function detDie($data){
		$this->tabla='rgdetdie';
		$this->resetData($data);
	}

	function resetData($data){
		$this->id_detdie=$this->checkData($data['idDetDie'],'');
		$this->id_dieta=$this->checkData($data['idDieTa'],'');
		$this->nparte1=$this->checkData($data['idInvent'],'');
		$this->cantidad=$this->checkData($data['canTidAd'],'');
		$this->pre_uni=$this->checkData($data['preUni'],'');
		$this->cve_iva=$this->checkData($data['cveIva'],'');
		$this->sub_det=$this->checkData($data['subDet'],'');
		$this->iva_det=$this->checkData($data['ivaDet'],'');
		$this->tot_det=$this->checkData($data['totDet'],'');
		$this->acc_fec=$this->checkData($data['accFec'],date('Y-m-d'));
		$this->acc_hor=$this->checkData($data['accHor'],date('H:i:s'));
		$this->usuario=$this->checkData($data['usuAriO'],$_SESSION['num_usu']);
	}

	function busReg(){
		$sq_retrieve="SELECT * FROM $this->tabla WHERE id_detdie='$this->id_detdie'";
		
		$rs=mysqli_query($con ,$sq_retrieve);
		if($rs)
			return $rs;
		else
			return 0;
	}

function busByEnc(){
		$sq_retrieve="SELECT * FROM $this->tabla WHERE id_dieta ='$this->id_dieta'";
		
		$rs=mysqli_query($con ,$sq_retrieve);
		if($rs)
			return $rs;
		else
			return 0;
	}
    function getByEnc(){
		$sq_retrieve="
		SELECT
			$this->tabla.id_detdie as idDetDie, 
			$this->tabla.id_dieta as idDieta,
			$this->tabla.nparte1 as idProd,
			rgubicacion.id_ubicacion as idUbi,
			rgubicacion.des_ubicacion as desUbi,
			dginvent.cod_var as cveProd,
			dginvent.np_desc1 as desProd,
			$this->tabla.cantidad as canProd,
			$this->tabla.pre_uni as preUni,
			$this->tabla.sub_det as subDet,
			$this->tabla.iva_det as ivaDet,
			$this->tabla.tot_det as totDet						
		FROM 
			$this->tabla 
			INNER JOIN dginvent ON(dginvent.nparte1=$this->tabla.nparte1)
			LEFT JOIN rgubicacion ON(dginvent.ubicacion=rgubicacion.id_ubicacion)
		WHERE 
			$this->tabla.id_dieta ='$this->id_dieta'
		ORDER BY
			rgubicacion.des_ubicacion,dginvent.np_desc1";
		
		$rs=mysqli_query($con ,$sq_retrieve);
		if($rs)
			return $rs;
		else
			return 0;
	}	
    function getArrTotDieta(){
		$sq_retrieve="SELECT id_dieta as idDieTa, SUM(sub_det) as subDieta, SUM(iva_det) as ivaDieta, SUM(tot_det) as totDieta FROM $this->tabla WHERE id_dieta ='$this->id_dieta' ";
		
		$rs=mysqli_query($con ,$sq_retrieve);
		if($rs)
			return mysqli_fetch_array($rs);
		else
			return 0;
	}
	
	function checkData($data,$default){
		if(isset($data)) 
			$auxData=$data;
		else 
			$auxData=$default;
		if(get_magic_quotes_gpc()) 
			$auxData = stripslashes($auxData);
		return mysqli_real_escape_string($con,$auxData);
	}

	function getLastInsId(){
		return $this->lastInsId;
	}

	function getMySQLError(){
		return 'Error N:' . mysqli_errno($con) . ": " . mysqli_error($con); 
	}

}
?>