<?php

class ReqDev{
	var  $tabla = '0';
	var  $fol_ven = '0';
	var  $tip_ven = '0';
	var  $fec_ven = '0';
	var  $cod_cli = '0';
	var  $por_des = '0';
	var  $alm_gen = '0';
	var  $sub_ven = '0';
	var  $iva_ven = '0';
	var  $tot_ven = '0';
	var  $oper = '0';
	var  $paso = '0';
	var  $facturado = '0';
	var  $num_fac = '0';
	var  $fac_ant = '0';
	var  $con_cre = '0';
	var  $iva_des = '0';
	var  $nom_cli_alt = '0';
	var  $cod_ven = '0';
	var  $des_ven = '0';
	var  $cancelado = '0';
	var  $fec_can = '0';
	var  $aseg = '0';
	var  $nom_aseg = '0';
	var  $sinies = '0';
	var  $poliza = '0';
	var  $solici = '0';
	var  $act_ped = '0';
	var  $num_pol = '0';
	var  $ped_nm = '0';
	var  $tip_cam = '0';
	var  $tot_dll = '0';
	var  $imp_pag = '0';
	var  $pagado = '0';
	var  $acc_fec = '0';
	var  $acc_hor = '0';
	var  $usuario = '0';
	var  $folio_control = '0';
	var  $lastInsId = '0';

	function ReqDev($data){
		$this->tabla='dgreqdev';
		$this->resetData($data);
	}

	function resetData($data){
		$this->fol_ven=$this->checkData($data['idReqdev'],'');
		$this->tip_ven=$this->checkData($data['tipReq'],'');
		$this->fec_ven=$this->checkData($data['fecReq'],'');
		$this->cod_cli=$this->checkData($data['codCli'],'');
		$this->por_des=$this->checkData($data['porDes'],'');
		$this->alm_gen=$this->checkData($data['idAlmacen'],'');
		$this->sub_ven=$this->checkData($data['subEnt'],'');
		$this->iva_ven=$this->checkData($data['ivaEnt'],'');
		$this->tot_ven=$this->checkData($data['totEnt'],'');
		$this->oper=$this->checkData($data['opeR'],'');
		$this->paso=$this->checkData($data['staReq'],'');
		$this->facturado=$this->checkData($data['facTurAdo'],'');
		$this->num_fac=$this->checkData($data['numFac'],'');
		$this->fac_ant=$this->checkData($data['facAnt'],'');
		$this->con_cre=$this->checkData($data['conCre'],'');
		$this->iva_des=$this->checkData($data['ivaDes'],'');
		$this->nom_cli_alt=$this->checkData($data['nomCliAlt'],'');
		$this->cod_ven=$this->checkData($data['codVen'],'');
		$this->des_ven=$this->checkData($data['desVen'],'');
		$this->cancelado=$this->checkData($data['canCelAdo'],'');
		$this->fec_can=$this->checkData($data['fecCan'],'');
		$this->aseg=$this->checkData($data['aseG'],'');
		$this->nom_aseg=$this->checkData($data['nomAseG'],'');
		$this->sinies=$this->checkData($data['sinIes'],'');
		$this->poliza=$this->checkData($data['polIza'],'');
		$this->solici=$this->checkData($data['solIci'],'');
		$this->act_ped=$this->checkData($data['actPed'],'');
		$this->num_pol=$this->checkData($data['numPol'],'');
		$this->ped_nm=$this->checkData($data['pedNm'],'');
		$this->tip_cam=$this->checkData($data['tipCam'],'');
		$this->tot_dll=$this->checkData($data['totDll'],'');
		$this->imp_pag=$this->checkData($data['impPag'],'');
		$this->pagado=$this->checkData($data['pagAdo'],'');
		$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']);
		$this->folio_control=$this->checkData($data['folIoConTroL'],'');
	}

	function agregar(){
		$sq_create="INSERT INTO $this->tabla SET tip_ven='$this->tip_ven', fec_ven='$this->fec_ven', cod_cli='$this->cod_cli', por_des='$this->por_des', alm_gen='$this->alm_gen', sub_ven='$this->sub_ven', iva_ven='$this->iva_ven', tot_ven='$this->tot_ven', oper='$this->oper', paso='$this->paso', facturado='$this->facturado', num_fac='$this->num_fac', fac_ant='$this->fac_ant', con_cre='$this->con_cre', iva_des='$this->iva_des', nom_cli_alt='$this->nom_cli_alt', cod_ven='$this->cod_ven', des_ven='$this->des_ven', cancelado='$this->cancelado', fec_can='$this->fec_can', aseg='$this->aseg', nom_aseg='$this->nom_aseg', sinies='$this->sinies', poliza='$this->poliza', solici='$this->solici', act_ped='$this->act_ped', num_pol='$this->num_pol', ped_nm='$this->ped_nm', tip_cam='$this->tip_cam', tot_dll='$this->tot_dll', imp_pag='$this->imp_pag', pagado='$this->pagado', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario', folio_control='$this->folio_control'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_create);
		echo mysqli_error($GLOBALS['con']);
		if($rs){
			$this->lastInsId=mysqli_insert_id();
			return 1;
		}
		else
			return -1;
	}

	function modificar(){
		$sq_update="UPDATE $this->tabla SET tip_ven='$this->tip_ven', fec_ven='$this->fec_ven', cod_cli='$this->cod_cli', por_des='$this->por_des', alm_gen='$this->alm_gen', sub_ven='$this->sub_ven', iva_ven='$this->iva_ven', tot_ven='$this->tot_ven', oper='$this->oper', paso='$this->paso', facturado='$this->facturado', num_fac='$this->num_fac', fac_ant='$this->fac_ant', con_cre='$this->con_cre', iva_des='$this->iva_des', nom_cli_alt='$this->nom_cli_alt', cod_ven='$this->cod_ven', des_ven='$this->des_ven', cancelado='$this->cancelado', fec_can='$this->fec_can', aseg='$this->aseg', nom_aseg='$this->nom_aseg', sinies='$this->sinies', poliza='$this->poliza', solici='$this->solici', act_ped='$this->act_ped', num_pol='$this->num_pol', ped_nm='$this->ped_nm', tip_cam='$this->tip_cam', tot_dll='$this->tot_dll', imp_pag='$this->imp_pag', pagado='$this->pagado', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario', folio_control='$this->folio_control' WHERE fol_ven='$this->fol_ven'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_update);
		if($rs)
			return 1;
		else
			return 0;
	}
	function getArrRegByTipFec(){
		$sq_retrieve="
			SELECT 
				count(*) as numRows,
				fol_ven as idReqdev,
				fec_ven as fecReq,
				alm_gen as idAlmacen,
				tip_ven as tipReq, 
				cod_cli as codCli,
				sub_ven as subReq,
				iva_ven as ivaReq,
				tot_ven as totReq 
			FROM 
				$this->tabla 
			WHERE 
				tip_ven='$this->tip_ven' AND fec_ven='$this->fec_ven' ";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_retrieve);
		if($rs)
			return mysqli_fetch_array($rs);
		else
			return 0;
	}
	
	function actTotEnt(){
		$sq_update="UPDATE $this->tabla SET paso='1', sub_ven='$this->sub_ven', iva_ven='$this->iva_ven', tot_ven='$this->tot_ven', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario' WHERE fol_ven='$this->fol_ven'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_update);
		if($rs)
			return 1;
		else
			return 0;
	}

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

	function eliminar(){
		$sq_delete="DELETE FROM $this->tabla WHERE fol_ven='$this->fol_ven'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_delete);
		if($rs)
			return 1;
		else
			return 0;
	}

	
	function checkData($data,$default){
		if(isset($data)) 
			$auxData=$data;
		else 
			$auxData=$default;
		if(get_magic_quotes_gpc()) 
			$auxData = stripslashes($auxData);
		$auxData=htmlentities($auxData);
		return mysqli_real_escape_string($con,$auxData);
	}

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

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

}

class ReqDed{
	var  $tabla = '0';
	var  $fol_ven1 = '0';
	var  $fec_ven = '0';
	var  $cons_ven = '0';
	var  $nparte1 = '0';
	var  $can_ref = '0';
	var  $pre_uni = '0';
	var  $imp_des = '0';
	var  $por_des = '0';
	var  $total_cd = '0';
	var  $almacen = '0';
	var  $cos_ref = '0';
	var  $iva_pre = '0';
	var  $pre_bas = '0';
	var  $can_dev = '0';
	var  $tip_camd = '0';
	var  $pre_uni_dll = '0';
	var  $cve_iva = '0';
	var  $ref_ipm = '0';
	var  $subtotal = '0';
	var  $des = '0';
	var  $acc_fec = '0';
	var  $acc_hor = '0';
	var  $usuario = '0';
	var  $lastInsId = '0';

	function ReqDed($data){
		$this->tabla='dgreqded';
		$this->resetData($data);
	}

	function resetData($data){
		$this->fol_ven1=$this->checkData($data['idReqded'],'');
		$this->fec_ven=$this->checkData($data['fecVen'],'');
		$this->cons_ven=$this->checkData($data['idReqdev'],'');
		$this->nparte1=$this->checkData($data['idInvent'],'');
		$this->can_ref=$this->checkData($data['canRef'],'');
		$this->pre_uni=$this->checkData($data['preUni'],'');
		$this->imp_des=$this->checkData($data['impDes'],0);
		$this->por_des=$this->checkData($data['porDes'],0);
		$this->total_cd=$this->checkData($data['totDet'],'');
		$this->almacen=$this->checkData($data['idAlmacen'],'');
		$this->cos_ref=$this->checkData($data['cosRef'],'');
		$this->iva_pre=$this->checkData($data['ivaDet'],'');
		$this->pre_bas=$this->checkData($data['preBas'],'');
		$this->can_dev=$this->checkData($data['canDev'],'');
		$this->tip_camd=$this->checkData($data['tipCamD'],'');
		$this->pre_uni_dll=$this->checkData($data['preUniDll'],'');
		$this->cve_iva=$this->checkData($data['cveIva'],'');
		$this->ref_ipm=$this->checkData($data['refIpm'],'');
		$this->subtotal=$this->checkData($data['subDet'],0);
		$this->des=$this->checkData($data['desDet'],0);
		$this->acc_fec=$this->checkData($data['accFec'],date('d/m/Y'));
		$this->acc_hor=$this->checkData($data['accHor'],date('H:i:s'));
		$this->usuario=$this->checkData($data['usuAriO'],$_SESSION['num_usu']);
	}

	function agregar(){
		$sq_create="INSERT INTO $this->tabla SET fec_ven='$this->fec_ven', cons_ven='$this->cons_ven', nparte1='$this->nparte1', can_ref='$this->can_ref', pre_uni='$this->pre_uni', imp_des='$this->imp_des', por_des='$this->por_des', total_cd='$this->total_cd', almacen='$this->almacen', cos_ref='$this->cos_ref', iva_pre='$this->iva_pre', pre_bas='$this->pre_bas', can_dev='$this->can_dev', tip_camd='$this->tip_camd', pre_uni_dll='$this->pre_uni_dll', cve_iva='$this->cve_iva', ref_ipm='$this->ref_ipm', subtotal='$this->subtotal', des='$this->des', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_create);
		if($rs){
			$this->lastInsId=mysqli_insert_id();
			return 1;
		}
		else
			return -1;
	}

	function modificar(){
		$sq_update="UPDATE $this->tabla SET fec_ven='$this->fec_ven', cons_ven='$this->cons_ven', nparte1='$this->nparte1', can_ref='$this->can_ref', pre_uni='$this->pre_uni', imp_des='$this->imp_des', por_des='$this->por_des', total_cd='$this->total_cd', almacen='$this->almacen', cos_ref='$this->cos_ref', iva_pre='$this->iva_pre', pre_bas='$this->pre_bas', can_dev='$this->can_dev', tip_camd='$this->tip_camd', pre_uni_dll='$this->pre_uni_dll', cve_iva='$this->cve_iva', ref_ipm='$this->ref_ipm', subtotal='$this->subtotal', des='$this->des', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario' WHERE fol_ven1='$this->fol_ven1'";
		$this->lastInsId=$this->fol_ven1;
		$rs=mysqli_query($GLOBALS['con'] ,$sq_update);
		if($rs)
			return 1;
		else
			return 0;
	}

	function busReg(){
		$sq_retrieve="SELECT * FROM $this->tabla WHERE fol_ven1='$this->fol_ven1'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_retrieve);
		if($rs)
			return $rs;
		else
			return 0;
	}
	function getArrRegByEncPro(){
		$sq_retrieve="
			SELECT 
				count(*) as numRows,
				fol_ven1 as idReqded,
				cons_ven as idReqdev,
				nparte1 as idInvent,
				almacen as idAlmacen,
				pre_uni as preUni,
				cve_iva as idTasiva, 
				pre_bas as preBas,
				can_ref as canRef,
				total_cd as totDet,
				subtotal as subDet,
				iva_pre as ivaDet 
			FROM 
				$this->tabla 
			WHERE
				cons_ven='$this->cons_ven' AND nparte1='$this->nparte1'
			GROUP BY
				cons_ven";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_retrieve);
		if($rs)
			return mysqli_fetch_array($rs);
		else
			return 0;
	}
	function getArrTotEnt(){
		$sq_retrieve="SELECT cons_ven as idReqdev, SUM(total_cd) as totEnt, SUM(subtotal) as subEnt, SUM(iva_pre) as ivaEnt FROM $this->tabla WHERE cons_ven='$this->cons_ven' ";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_retrieve);
		if($rs){
			$auxRs=mysqli_fetch_array($rs);
			mysqli_free_result($rs);
			return $auxRs;
		}else
			return 0;
	}
	function eliminar(){
		$sq_delete="DELETE FROM $this->tabla WHERE fol_ven1='$this->fol_ven1'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_delete);
		if($rs)
			return 1;
		else
			return 0;
	}

	function getArrTotReq(){
		$sq_retrieve="
			SELECT 
				cons_ven as idReqdev,			
				SUM(total_cd) as totEnt,
				SUM(subtotal) as subEnt,
				SUM(iva_pre) as ivaEnt 
			FROM 
				$this->tabla 
			WHERE
				cons_ven='$this->cons_ven' ";
		
		$rs=mysqli_query($GLOBALS['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);
		$auxData=htmlentities($auxData);
		return mysqli_real_escape_string($con,$auxData);
	}

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

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

}
?>
