<?php
 
class EncFac{
	var  $tabla = '0';
	var  $id_encfac = '0';
	var  $tip_fac = '0';
	var  $num_fac = '0';
	var  $fec_fac = '0';
	var  $codigo = '0';
	var  $iva_des = '0';
	var  $cve_iva = '0';
	var  $tip_pro = '0';
	var  $sub_fac = '0';
	var  $iva_fac = '0';
	var  $tot_fac = '0';
	var  $es_can = '0';
	var  $fec_can = '0';
	var  $usu_can = '0';
	var  $acc_fec = '0';
	var  $acc_hor = '0';
	var  $usuario = '0';
	var  $lastInsId = '0';

	function EncFac($data){
		$this->tabla='dvencfac';
		$this->resetData($data);
	}

	function resetData($data){
		$this->id_encfac=$this->checkData($data['idEncFac'],'');
		$this->tip_fac=$this->checkData($data['tipFac'],'');
		$this->num_fac=$this->checkData($data['numFac'],'');
		$this->fec_fac=$this->checkData($data['fecFac'],date('Y-m-d'));
		$this->hor_fac=$this->checkData($data['horFac'],date('H:i:s'));
		$this->codigo =$this->checkData($data['codigo'],'');
		$this->iva_des=$this->checkData($data['ivaDes'],'');
		$this->cve_iva=$this->checkData($data['cveIva'],'');
		$this->tip_pro=$this->checkData($data['tipPro'],'N');
		$this->sub_fac=$this->checkData($data['subFac'],'');
		$this->iva_fac=$this->checkData($data['ivaFac'],'');
		$this->tot_fac=$this->checkData($data['totFac'],'');
		$this->es_can =$this->checkData($data['esCan'],'0');
		$this->fec_can=$this->checkData($data['fecCan'],'0000-00-00');
		$this->hor_can=$this->checkData($data['horCan'],'00:00:00');
		$this->usu_can=$this->checkData($data['usuCan'],'0');
		$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 agregar(){
		$sq_create="INSERT INTO $this->tabla SET tip_fac='$this->tip_fac', num_fac='$this->num_fac', fec_fac='$this->fec_fac', hor_fac='$this->hor_fac', codigo='$this->codigo', iva_des='$this->iva_des', cve_iva='$this->cve_iva', tip_pro='$this->tip_pro', sub_fac='$this->sub_fac', iva_fac='$this->iva_fac', tot_fac='$this->tot_fac', es_can='$this->es_can', fec_can='$this->fec_can',hor_can='$this->hor_can',usu_can='$this->usu_can', 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 tip_fac='$this->tip_fac', num_fac='$this->num_fac', fec_fac='$this->fec_fac', hor_fac='$this->hor_fac', codigo='$this->codigo', iva_des='$this->iva_des', cve_iva='$this->cve_iva', tip_pro='$this->tip_pro', sub_fac='$this->sub_fac', iva_fac='$this->iva_fac', tot_fac='$this->tot_fac', es_can='$this->es_can', fec_can='$this->fec_can',hor_can='$this->hor_can', usu_can='$this->usu_can', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario' WHERE id_encfac='$this->id_encfac'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_update);
		if($rs)
			return 1;
		else
			return 0;
	}

	function cancelaFactura(){
		$sq_update="UPDATE $this->tabla SET   sub_fac=0, iva_fac=0, tot_fac=0, es_can='1', fec_can='$this->fec_can',hor_can='$this->hor_can', usu_can='$this->usu_can', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario' WHERE id_encfac='$this->id_encfac'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_update);
		if($rs)
			return 1;
		else
			return 0;
	}
	
	function actTotFac(){
		$sq_update="UPDATE $this->tabla SET  sub_fac='$this->sub_fac', iva_fac='$this->iva_fac', tot_fac='$this->tot_fac', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario' WHERE id_encfac='$this->id_encfac'";
		$rs=mysqli_query($GLOBALS['con'] ,$sq_update);
		if($rs)
			return 1;
		else
			return 0;
	}


	function busReg(){
		$sq_retrieve="SELECT 
						$this->tabla.id_encfac as idEncFac,
						$this->tabla.tip_fac as idTipFac, 
						dgtipfac.des_tipfac as desTipFac,
						dgtipfac.nom_for_imp as nomRepTipFac,
						dgtipfac.num_apr_fol as numAprFac,
						dgtipfac.axo_apr_fol as axoAprFac,
						$this->tabla.num_fac as numFac,
						DATE_FORMAT($this->tabla.fec_fac,'%d/%m/%Y') as fecFac,
						CONCAT($this->tabla.fec_fac,'T',$this->tabla.hor_fac) as fecThorFac,
						$this->tabla.codigo as idBascps,
						CONCAT(dxbascps.ap_paterno,' ',dxbascps.ap_materno,' ',dxbascps.nombre) as nomCli,
						dxbascps.rfc as rfcCli,
						dxpaises.des_pais    as desPaisCli,
						dxestado.des_estado  as desEdoCli,
						dxdelmun.des_delmun  as desMunCli,
						dxcoloni.des_colonia as desColCli,
						dxbascps.cod_pos     as codPosCli,
						dxbascps.calle calleCli,
						dxbascps.no_ext numExtCli,
						dxbascps.no_int numIntCli,
						dxbascps.entre_calle as entCallCli,
						dxbascps.y_calle as yCallCli,
						$this->tabla.iva_des as ivaDes,
						$this->tabla.cve_iva as idTasIva,
						dxtasiva.porc_iva as porTasIva,
						$this->tabla.tip_pro as tipPro,
						$this->tabla.sub_fac as subFac,
						$this->tabla.iva_fac as ivaFac,
						$this->tabla.tot_fac as totFac,
						$this->tabla.es_can as esCan,
						DATE_FORMAT($this->tabla.fec_can,'%d/%m/%Y') as fecCan,
						$this->tabla.hor_can as horCan,
						$this->tabla.usu_can as usuCan												
					  FROM 
					  	$this->tabla
					  INNER JOIN dgtipfac ON(dgtipfac.tip_fac=$this->tabla.tip_fac)
					  INNER JOIN dxtasiva ON(dxtasiva.cve_iva=$this->tabla.cve_iva)
					  INNER JOIN dxbascps ON(dxbascps.codigo=$this->tabla.codigo)
					  LEFT JOIN geoyos_jc.dxpaises ON(geoyos_jc.dxpaises.cve_pais  =dxbascps.cve_pais)
					  LEFT JOIN geoyos_jc.dxestado ON(geoyos_jc.dxestado.cve_estado=dxbascps.cve_estado)
					  LEFT JOIN geoyos_jc.dxdelmun ON(geoyos_jc.dxdelmun.cve_delmun=dxbascps.cve_delmun)
					  LEFT JOIN geoyos_jc.dxcoloni ON(geoyos_jc.dxcoloni.cve_col   =dxbascps.des_colonia)
					  WHERE 
					  	$this->tabla.id_encfac='$this->id_encfac'";
		//echo $sq_retrieve."<br>";
		echo mysqli_error($GLOBALS['con']);		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_retrieve);
		if($rs)
			return $rs;
		else
			return 0;
	}
	function getReportData($dataBus){
		if(count($dataBus)>0){
			$WHERE="WHERE ";
			$WHERE.=implode(" AND ",$dataBus);
		}
		
		$sq_retrieve="SELECT
						$this->tabla.id_encfac as idEncFac, 
						$this->tabla.tip_fac as idTipFac, 
						dgtipfac.des_tipfac as desTipFac,
						$this->tabla.num_fac as numFac,
						DATE_FORMAT($this->tabla.fec_fac,'%d/%m/%Y') as fecFac,
						$this->tabla.codigo as idBascps,
						CONCAT(dxbascps.ap_paterno,' ',dxbascps.ap_materno,' ',dxbascps.nombre) as nomCli,
						CONCAT(dxbascps.calle,' ',dxbascps.no_ext,'. ',dxdelmun.des_delmun,' C.P.',dxbascps.cod_pos,' ',dxcoloni.des_colonia) as dirCli,
						dxbascps.rfc as rfcCli,
						dxestado.des_estado  as desEstado,
						$this->tabla.iva_des as ivaDes,
						$this->tabla.cve_iva as idTasIva,
						dxtasiva.porc_iva as porTasIva,
						$this->tabla.tip_pro as tipPro,
						$this->tabla.sub_fac as subFac,
						$this->tabla.iva_fac as ivaFac,
						$this->tabla.tot_fac as totFac,
						$this->tabla.es_can as esCan,
						DATE_FORMAT($this->tabla.fec_can,'%d/%m/%Y') as fecCan,
						$this->tabla.hor_can as horCan,
						$this->tabla.usu_can as usuCan
					  FROM 
					  	$this->tabla
					  LEFT JOIN dgtipfac ON(dgtipfac.tip_fac=$this->tabla.tip_fac)
					  LEFT JOIN dxbascps ON(dxbascps.codigo=$this->tabla.codigo)
					  LEFT JOIN dxtasiva ON(dxtasiva.cve_iva=$this->tabla.cve_iva)
					  LEFT JOIN geoyos_jc.dxestado ON(geoyos_jc.dxestado.cve_estado=dxbascps.cve_estado)
					  LEFT JOIN geoyos_jc.dxdelmun ON(geoyos_jc.dxdelmun.cve_delmun=dxbascps.cve_delmun)
					  LEFT  JOIN geoyos_jc.dxcoloni ON(geoyos_jc.dxcoloni.cve_col=dxbascps.des_colonia)
					  $WHERE
					  ORDER BY 
					  	dgtipfac.des_tipfac,$this->tabla.num_fac";
		//echo $sq_retrieve;
		//echo mysqli_error($GLOBALS['con']);
		//echo $sq_retrieve;
		$rs=mysqli_query($GLOBALS['con'] ,$sq_retrieve);
		if($rs)
			return $rs;
		else
			return 0;	
	}
	function eliminar(){
		$sq_delete="DELETE FROM $this->tabla WHERE id_encfac='$this->id_encfac'";
		
		$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 DetFac{
	var  $tabla = '0';
	var  $id_detfac = '0';
	var  $id_encfac = '0';
	var  $tip_nota = '0';
	var  $obs_ope = '0';
	var  $pre_bas = '0';
	var  $por_des = '0';
	var  $pre_uni = '0';
	var  $can_det = '0';
	var  $cve_iva = '0';
	var  $sub_det = '0';
	var  $iva_det = '0';
	var  $tot_det = '0';
	var  $acc_fec = '0';
	var  $acc_hor = '0';
	var  $usuario = '0';
	var  $lastInsId = '0';

	function DetFac($data){
		$this->tabla='dvdetfac';
		$this->resetData($data);
	}

	function resetData($data){
		$this->id_detfac=$this->checkData($data['idDetFac'],'');
		$this->id_encfac=$this->checkData($data['idEncFac'],'');
		$this->tip_nota =$this->checkData($data['tipNot'],'');
		$this->obs_ope  =$this->checkData($data['obsOpe'],'');
		$this->pre_bas  =$this->checkData($data['preBas'],'');
		$this->por_des  =$this->checkData($data['porDes'],'');
		$this->pre_uni  =$this->checkData($data['preUni'],'');
		$this->can_det  =$this->checkData($data['canDet'],'');
		$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 agregar(){
		$sq_create="INSERT INTO $this->tabla SET id_encfac='$this->id_encfac', tip_nota='$this->tip_nota', obs_ope='$this->obs_ope', pre_bas='$this->pre_bas', por_des='$this->por_des', pre_uni='$this->pre_uni', cve_iva='$this->cve_iva', sub_det='$this->sub_det', iva_det='$this->iva_det', tot_det='$this->tot_det', can_det='$this->can_det', 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 id_encfac='$this->id_encfac', tip_nota='$this->tip_nota', obs_ope='$this->obs_ope', pre_bas='$this->pre_bas', por_des='$this->por_des', pre_uni='$this->pre_uni', cve_iva='$this->cve_iva', sub_det='$this->sub_det', iva_det='$this->iva_det', tot_det='$this->tot_det', can_det='$this->can_det', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario' WHERE id_detfac='$this->id_detfac'";
		//echo $sq_update;
		$rs=mysqli_query($GLOBALS['con'] ,$sq_update);
		if($rs)
			return 1;
		else
			return 0;
	}

	function busReg(){
		$sq_retrieve="SELECT 
							$this->tabla.id_detfac as idDetFac,
							$this->tabla.id_encfac as idEncFac,
							$this->tabla.tip_nota  as idTipNot,
							dvtipnot.des_nota      as desTipNot,
							$this->tabla.obs_ope   as obsOpe,
							$this->tabla.can_det   as canDet,
							$this->tabla.cve_iva   as idTasIva,
							dxtasiva.porc_iva      as porTasIva,
							dxtasiva.conc_iva      as desTasIva,
							$this->tabla.pre_bas as preBas,
							$this->tabla.por_des as porDes,
							$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
					  LEFT JOIN dvtipnot ON(dvtipnot.tip_nota=$this->tabla.tip_nota) 
					  INNER JOIN dxtasiva ON(dxtasiva.cve_iva =$this->tabla.cve_iva)
					  WHERE 
					  	$this->tabla.id_detfac='$this->id_detfac'";

		$rs=mysqli_query($GLOBALS['con'] ,$sq_retrieve);
		if($rs)
			return $rs;
		else
			return 0;
	}

	function busByEnc(){
		$sq_retrieve="SELECT 
							$this->tabla.id_detfac as idDetFac,
							$this->tabla.id_encfac as idEncFac,
							$this->tabla.tip_nota  as idTipNot,
							dvtipnot.des_nota      as desTipNot,
							$this->tabla.tip_nota  as idProd,
							dvtipnot.des_nota      as desProd,
							$this->tabla.obs_ope   as obsOpe,
							$this->tabla.can_det   as canDet,
							$this->tabla.cve_iva   as idTasIva,
							dxtasiva.porc_iva      as porTasIva,
							dxtasiva.conc_iva      as desTasIva,
							$this->tabla.pre_bas as preBas,
							$this->tabla.por_des as porDes,
							$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
					  LEFT JOIN dvtipnot ON(dvtipnot.tip_nota=$this->tabla.tip_nota) 
					  INNER JOIN dxtasiva ON(dxtasiva.cve_iva =$this->tabla.cve_iva)
					  WHERE 
					  	$this->tabla.id_encfac='$this->id_encfac'";
		$rs=mysqli_query($GLOBALS['con'] ,$sq_retrieve);
		if($rs)
			return $rs;
		else
			return 0;
	}
	
	function eliminar(){
		$sq_delete="DELETE FROM $this->tabla WHERE id_detfac='$this->id_detfac'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_delete);
		if($rs)
			return 1;
		else
			return 0;
	}

	function getTotFac(){
		$sq_retrieve="SELECT id_encfac as idEncFac, SUM(sub_det) as subFac,SUM(iva_det) as ivaFac,SUM(tot_det) as totFac FROM $this->tabla WHERE id_encfac='$this->id_encfac' ";
		$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']); 
	}

}

function getDataBus(){
	$dataBus=array();
	if(isset($_POST['por_fec'])){
		$fecIni=chfecha($_POST['fecini'],'d/m/Y','Y-m-d');
		$fecFin=chfecha($_POST['fecfin'],'d/m/Y','Y-m-d');		
		$dataBus[]="dvencfac.fec_fac BETWEEN '$fecIni' AND '$fecFin'";
	}
	if(isset($_POST['por_cli'])){
		$dataBus[]="dvencfac.codigo='".mysqli_real_escape_string($con,$_POST['id_cli'])."'";	
	}
	if(isset($_POST['por_fac'])){
		$dataBus[]="dvencfac.tip_fac='".mysqli_real_escape_string($con,$_POST['tip_fac'])."'";
		$dataBus[]="dvencfac.num_fac='".mysqli_real_escape_string($con,$_POST['num_fac'])."'";
	}
	return $dataBus;
}


class CFD{
	var  $encFac = '0';
	var  $detFac = '0';
	var  $objTipFac = '0';
	var  $lastStringSource = '0';
	
	var  $tipGenFac = '0';	//1=>Comprobantes Fiscales Digitales
					//2=>Comprobantes Solicitados a un Establecimiento
					//3=>Comprobantes Impresos por el propio Contribuyente
	var  $rfcCon = '0';    //RFC del contribuyente
					//	1=>MORAL   (12 posiciones)
					//  2=>FISICA  (13 posiciones)
					//Si alguno cuenta con Representante legal:
					//			RFC del contribuyente / RFC del Representante Legal
	var  $mesPro = '0';    //mes de proceso de factura
	var  $axoPro = '0';    //Año de proceso de factura
	
	/*Setup XML & StringSource*/
	var  $verXSD = '0';    //version
	var  $impMetPag = '0'; //Imprimir metodo de pago(1,0)
	var  $impConPag = '0'; //Imprimir Condiciones de pago(1,0)
	var  $impTipSer = '0';    //Serie de Facturas (opcional 0=>Sin Serie)
	var  $impCer64 = '0';
	var	$impNumExtEmi;
	var	$impNumIntEmi;
	var	$impColEmi;
	var	$impLocEmi;
	var	$impRef;
	var	$impNumExtExp;
	var	$impNumIntExp;
	var	$impColExp;
	var	$impLocExp;
	var	$impRefExp;
	var	$impMunExp;
	var	$impEstExp;
	var	$impCodPosExp;
	var	$impNumExtCli;
	var	$impNumIntCli;
	var	$impColCli;
	var	$impLocCli;
	var	$impRefCli;
	var	$impMunCli;
	var	$impEstCli;
	var	$impCodPosCli;	
	var  $impDomExp = '0';//1=>Imprime Domicilio de Expedicion de Factura 0=>no imprime
	var  $impUniVen = '0';//Imprimir Unidad de Venta de Articulo(0,1)      CONCEPTO
	var  $impIdProd = '0';//Imprimir No. Serie o Identificador de Producto CONCEPTO
	var  $impInfAdu = '0';//Imprimir Informacion Aduanera                  CONCEPTO
	var  $impCuePred = '0';//Imprimir Cuenta Predial                       CONCEPTO
	var  $impComCon = '0';//Imprimir Complemento de Concepto               CONCEPTO
	var  $impParCon = '0';//Imprimir Parte o componentes que componen el concepto CONCEPTO
	var  $impConParInfAdu = '0';//Imprimir Informacion Aduanera            CONCEPTO:PARTE	
	var  $impRet = '0';
	var  $impTra = '0';
	var  $impTotImpRet = '0';//Imprime Total Impuestos Retenidos          IMPUESTOS
	var  $impTotImpTra = '0';//Imprime Total Impuestos Trasladados        IMPUESTOS
	/****************************/
	/*Setup StringSource*/
	var  $rootPathOPENSSL = '0';   //PATH OPENSSL
	var  $rootPathKey = '0';       //carpeta padre para las Llaves y Certificados
	var  $rootPathOutput = '0';    //Carpeta padre para salvar archivos generados en el proceso
	var  $rootPathTemp = '0';      //Carpeta padre para salvar temporalmente los archivos generados en el proceso(son borrados al fin de esto)
	var  $relPathMD5 = '0';        //PATH relativo en donde se guardaran las digestiones md5
	var  $relPathDigitalSign = '0';//PATH relativo en donde se guardara el certificado digital
	var  $relPathXML = '0';        //PATH relativo en donde se guardaran los XML
	var  $relPathSourceString = '0';//PATH relativo en donde se guardara la cadena original
	var  $pathSourceString = '0';//PATH en donde se guardaran las cadenas originales
	var  $pathMD5 = '0';        //PATH  en donde se guardaran las digestiones md5
	var  $pathDigitalSign = '0';//PATH  en donde se guardara el certificado digital
	var  $pathXML = '0';        //PATH  en donde se guardaran los XML
	var  $saveSourceString = '0';//salvar Cadena Original(1,0)	
	var  $saveMD5 = '0';       //Salvar MD5 (1,0)
	var  $saveDigitalSign = '0';//Salvar Certificado Digital (1,0)
	var  $saveXML = '0';       //SalvarXML(1,0)
	var  $arrSuc = '0';       //array asociativo que contiene las distintas sucursales con la relacion de su llave y certificado
	var  $idSuc = '0';        //id de la sucursal a Facturar
	/*******************/
	var  $error = '0';
	function CFD($dataEnc=NULL,$dataDet=NULL,$dataCFD=NULL){
		$this->encFac=new EncFac($dataEnc);
		$this->detFac=new DetFac($dataDet);

		/**************VALORES PREDETERMINADOS**************/
		$this->tipGenFac    ='1';
		$this->mesPro       =date('m');
		$this->axoPro       =date('Y');
		
		//--------------->Configuracion XML-------------------------->
		$this->verXSD       ='2.0';
		$this->impMetPag    =false;
		$this->impConPag    =false;
		$this->impTipSer    =false;
		$this->impCer64     =false;
		$this->impNumExtEmi=true;
		$this->impNumIntEmi=true;
		$this->impColEmi   =true;
		$this->impLocEmi   =false;
		$this->impRef      =false;
		$this->impNumExtExp=true;
		$this->impNumIntExp=true;
		$this->impColExp   =true;
		$this->impLocExp   =false;
		$this->impRefExp   =false;
		$this->impMunExp   =true;
		$this->impEstExp   =true;
		$this->impCodPosExp=true;
		$this->impNumExtCli=true;
		$this->impNumIntCli=true;
		$this->impColCli   =true;
		$this->impLocCli   =false;
		$this->impRefCli   =false;
		$this->impMunCli   =true;
		$this->impEstCli   =true;
		$this->impCodPosCli=true;			
		
		$this->impDomExp    =false;
		$this->impUniVen    =false;
		$this->impIdProd    =false;
		$this->impInfAdu    =false;
		$this->impCuePred   =false;
		$this->impComCon    =false;
		$this->impParCon    =false;
		$this->impConParInfAdu=false;
		$this->impRet       =false;
		$this->impTra       =true;
		$this->impTotImpRet =false;
		$this->impTotImpTra =true;
		//------------->Configuracion Certificado Digital------------>
		$this->rootPathOPENSSL='/usr/bin/openssl';
		$this->rootPathKey    ='./facEle/keys/';
		$this->rootPathOutput ='./facEle/output/';
		$this->rootPathTemp   ='./facEle/temp/';
		$this->relPathSourceString='digital_sign/';
		$this->relPathMD5     ='md5/';
		$this->relPathDigitalSign='digital_sign/';
		$this->relPathXML     ='cfd_xml/';
		$this->saveSourceString=true;
		$this->saveMD5        =true;
		$this->saveDigitalSign=true;
		$this->saveXML        =true;
		$this->arrSuc=array("OA"=>array(
								"key"=>"aaa010101aaa_CSD_01.key.pem",
								"cer"=>"aaa010101aaa_CSD_01.cer",
								"numSerCer"=>"00001100000200000154",
								"numAut"=>"1"
								),
							"OB"=>array(
								"key"=>"aaa010101aaa_CSD_02.key.pem",
								"cer"=>"aaa010101aaa_CSD_02.cer",
								"numSerCer"=>"00001100000200000154",
								"numAut"=>"1"
								)
							);
		$this->idSuc        ='OA';
		if($this->saveSourceString)
			$this->pathSourceString=$this->rootPathOutput.$this->relPathSourceString;
		else
			$this->pathSourceString=$this->rootPathTemp.$this->relPathSourceString;
		if($this->saveMD5)
			$this->pathMD5=$this->rootPathOutput.$this->relPathMD5;
		else
			$this->pathMD5=$this->rootPathTemp.$this->relPathMD5;
		if($this->saveDigitalSign)
			$this->pathDigitalSign=$this->rootPathOutput.$this->relPathDigitalSign;
		else
			$this->pathDigitalSign=$this->rootPathTemp.$this->relPathDigitalSign;	
		if($this->saveXML)
			$this->pathXML=$this->rootPathOutput.$this->relPathXML;
		else
			$this->pathXML=$this->rootPathTemp.$this->relPathXML;											
		/***************************************************/
		//$this->setup($dataCFD);

	}



	function setup($data){
		$this->tipGenFac   =$this->checkData($data['tipGenFac'],$this->tipGenFac);
		$this->mesPro      =$this->checkData($data['mesPro'],$this->mesPro);
		$this->axoPro      =$this->checkData($data['axoPro'],$this->axoPro);
		$this->verXSD      =$this->checkData($data['verXSD'],$this->verXSD);
		$this->impMetPag   =$this->checkData($data['impMetPag'],$this->impMetPag);
		$this->impConPag   =$this->checkData($data['impConPag'],$this->impConPag);
		$this->impTipSer   =$this->checkData($data['impTipSer'],$this->impTipSer);
		$this->impCer64    =$this->checkData($data['impSer64'],$this->impCer64);
		$this->impNumExtEmi=$this->checkData($data['impNumExtEmi'],$this->impNumExtEmi);
		$this->impNumIntEmi=$this->checkData($data['impNumIntEmi'],$this->impNumIntEmi);
		$this->impColEmi   =$this->checkData($data['impColEmi'],$this->impColEmi);
		$this->impLocEmi   =$this->checkData($data['impLocEmi'],$this->impLocEmi);
		$this->impRef      =$this->checkData($data['impRef'],$this->impRef);
		$this->impNumExtExp=$this->checkData($data['impNumExtExp'],$this->impNumExtExp);
		$this->impNumIntExp=$this->checkData($data['impNumIntExp'],$this->impNumIntExp);
		$this->impColExp   =$this->checkData($data['impColExp'],$this->impColExp);
		$this->impLocExp   =$this->checkData($data['impLocExp'],$this->impLocExp);
		$this->impRefExp   =$this->checkData($data['impRefExp'],$this->impRefExp);
		$this->impMunExp   =$this->checkData($data['impMunExp'],$this->impMunExp);
		$this->impEstExp   =$this->checkData($data['impEstExp'],$this->impEstExp);
		$this->impCodPosExp=$this->checkData($data['impCodPosExp'],$this->impCodPosExp);
		$this->impNumExtCli=$this->checkData($data['impNumExtCli'],$this->impNumExtCli);
		$this->impNumIntCli=$this->checkData($data['impNumIntCli'],$this->impNumIntCli);
		$this->impColCli   =$this->checkData($data['impColCli'],$this->impColCli);
		$this->impLocCli   =$this->checkData($data['impLocCli'],$this->impLocCli);
		$this->impRefCli   =$this->checkData($data['impRefCli'],$this->impRefCli);
		$this->impMunCli   =$this->checkData($data['impMunCli'],$this->impMunCli);
		$this->impEstCli   =$this->checkData($data['impEstCli'],$this->impEstCli);
		$this->impCodPosCli=$this->checkData($data['impCodPosCli'],$this->impCodPosCli);					
		$this->impDomExp   =$this->checkData($data['impDomExp'],$this->impDomExp);
		$this->impUniVen   =$this->checkData($data['impUniVen'],$this->impUniVen);
		$this->impIdProd   =$this->checkData($data['impIdProd'],$this->impIdProd);
		$this->impInfAdu   =$this->checkData($data['impInfAdu'],$this->impInfAdu);
		$this->impCuePred  =$this->checkData($data['impCuePred'],$this->impCuePred);
		$this->impComCon   =$this->checkData($data['impComCon'],$this->impComCon);
		$this->impParCon   =$this->checkData($data['impParCon'],$this->impParCon);
		$this->impConParInfAdu  =$this->checkData($data['impConParInfAdu'],$this->impConParInfAdu);
		$this->impRet      =$this->checkData($data['impRet'],$this->impRet);
		$this->impTra      =$this->checkData($data['impTra'],$this->impTra);
		$this->impTotImpRet=$this->checkData($data['impTotImpRet'],$this->impTotImpRet);
		$this->impTotImpTra=$this->checkData($data['impTotImpTra'],$this->impTotImpTra);
		
		$this->rootPathOPENSSL=$this->checkData($data['rootPathOPENSSL'],$this->rootPathOPENSSL);
		$this->rootPathKey    =$this->checkData($data['rootPathKey'],$this->rootPathKey);
		$this->rootPathOutput =$this->checkData($data['rootPathOutput'],$this->rootPathOutput);
		$this->rootPathTemp   =$this->checkData($data['rootPathTemp'],$this->rootPathTemp);
		$this->relPathMD5     =$this->checkData($data['relPathMD5'],$this->relPathMD5);
		$this->relPathDigitalSign=$this->checkData($data['relPathDigitalSign'],$this->relPathDigitalSign);
		$this->relPathXML     =$this->checkData($data['relPathXML'],$this->relPathXML);
		$this->saveMD5        =$this->checkData($data['saveMD5'],$this->saveMD5);
		$this->saveDigitalSign   =$this->checkData($data['saveDigitalSign'],$this->saveDigitalSign);
		$this->saveXML        =$this->checkData($data['saveXML'],$this->saveXML);
		$this->arrSuc         =$this->checkData($data['arrSuc'],$this->arrSuc);
		$this->idSuc          =$this->checkData($data['idSuc'],$this->idSuc);
	}
		
	function getStringSource(){
		$impDomExp=$this->impDomExp;
		$impInfAdu=$this->impInfAdu;//Imprimir Informacion Aduanera                  CONCEPTO
		$impCuePred=$this->impCuePred;//Imprimir Cuenta Predial                       CONCEPTO
		$impComCon=$this->impComCon;//Imprimir Complemento de Concepto               CONCEPTO
		$impParCon=$this->impParCon;//Imprimir Parte o componentes que componen el concepto CONCEPTO		
		$impRet=$this->impRet;//Imprimir retenciones IMPUESTOS
		$impTra=$this->impTra;//Imprimir traslados   IMPUESTOS
		$sq_encfac=$this->encFac->busReg();
		$dt_encfac=mysqli_fetch_array($sq_encfac);
		$sq_detfac=$this->detFac->busByEnc();
		$dt_encfac['tipCom']='ingreso';
				
		$dt_empres=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT geoyos_jc.dxempres.rfc, geoyos_jc.dxempres.nom_emp as nom,geoyos_jc.dxempres.calle, geoyos_jc.dxempres.no_ext as numExt,geoyos_jc.dxempres.no_int as numInt, geoyos_jc.dxempres.des_col as desCol,geoyos_jc.dxdelmun.des_delmun as desMun,geoyos_jc.dxestado.des_estado as desEst,geoyos_jc.dxpaises.des_pais as desPais,geoyos_jc.dxempres.cod_pos as codPos FROM geoyos_jc.dxempres INNER JOIN geoyos_jc.dxpaises ON(geoyos_jc.dxpaises.cve_pais=geoyos_jc.dxempres.cve_pais) INNER JOIN geoyos_jc.dxestado ON(geoyos_jc.dxestado.cve_estado=geoyos_jc.dxempres.cve_estado) INNER JOIN geoyos_jc.dxdelmun ON(geoyos_jc.dxdelmun.cve_delmun=geoyos_jc.dxempres.cve_delmun) WHERE  geoyos_jc.dxempres.cla_emp='".$_SESSION['empresa']."'"));
				
		//-----------------------------COMPROBANTE------------------------------------------------------------------------------>
		$atrComprobante=$this->getData_Comprobante($dt_encfac,'str_src');
		//-----------------------------/COMPROBANTE------------------------------------------------------------------------------>		
		//-----------------------------EMISOR------------------------------------------------------------------------------>
		$atrComprobanteEmisor=$this->getData_Comprobante_Emisor($dt_empres,'str_src');
		
		$atrComprobanteEmisorDomicilioFiscal=$this->getData_Comprobante_Emisor_DomicilioFiscal($dt_empres,'str_src');	
		if($impDomExp)
			$atrComprobanteEmisorExpedidoEn=$this->getData_Comprobante_Emisor_ExpedidoEn($dt_encfac,'str_src');	
		//-----------------------------/EMISOR------------------------------------------------------------------------------>
		//-----------------------------RECEPTOR------------------------------------------------------------------------------>
		$atrComprobanteReceptor=$this->getData_Comprobante_Receptor($dt_encfac,'str_src');
		$atrComprobanteReceptorDomicilio=$this->getData_Comprobante_Receptor_Domicilio($dt_encfac,'str_src');		
		//-----------------------------/RECEPTOR------------------------------------------------------------------------------>
		//-----------------------------IMPUESTOS------------------------------------------------------------------------------>
		$arrImpuestos['totImpTra']=$dt_encfac['ivaFac'];
		$arrImpuestos['totImpRet']="0.00";
		$atrComprobanteImpuestos=$this->getData_Comprobante_Impuestos($arrImpuestos,'str_src');	
		$arrTra[]=array("tipImp"=>"IVA","impTra"=>$arrImpuestos['totImpTra'],"tasaImp"=>$dt_encfac['porTasIva']);
		//-----------------------------/IMPUESTOS------------------------------------------------------------------------------>		
		$stringSource .="|";
		$stringSource .="|$atrComprobante";
		$stringSource 	.="|$atrComprobanteEmisor";
		$stringSource 		.="|$atrComprobanteEmisorDomicilioFiscal";
		if($impDomExp){
			$stringSource 	.="|$atrComprobanteEmisorExpedidoEn";
		}
		$stringSource 	.="|$atrComprobanteReceptor";
		$stringSource 		.="|$atrComprobanteReceptorDomicilio";	
		while($dt_detfac=mysqli_fetch_array($sq_detfac)){
			$atrComprobanteConceptosConcepto=$this->getData_Comprobante_Conceptos_Concepto($dt_detfac,'str_src');
			$stringSource 	.="|$atrComprobanteConceptosConcepto";
			if($impInfAdu){
				for($i=0;$i<count($arrInfAdu);$i++){
					$atrConceptoInformacionAduanera=$this->getData_Comprobante_Conceptos_Concepto_InformacionAduanera($arrInfAdu[$i],'str_src');
					$stringSource 	.="|$atrConceptoInformacionAduanera";
				}
			}
			if($impCuePred){
				$atrConceptocuentaPredial=$this->getData_Comprobante_Conceptos_Concepto_CuentaPredial($dt_detfac,'str_src');
				$stringSource 	.="|$atrConceptocuentaPredial";
			}
			if($impComCon){
				for($i=0;$i<count($arrComCon);$i++){
					$atrConceptoComplementoConcepto=$this->getData_Comprobante_Conceptos_Concepto_ComplementoConcepto($arrComCon[$i],'str_src');			
					$stringSource 	.="|$atrConceptoComplementoConcepto";
				}
			}
			if($impParCon){
				for($j=0;$j<count($arrConPar);$j++){
					$atrConceptoParte=$this->getData_Comprobante_Conceptos_Concepto_Parte($arrConPar[$j],'str_src');
					$stringSource 	.="|$atrConceptoParte";
					if($impConParInfAdu){
						for($i=0;$i<count($arrConParInfAdu);$i++){
							$atrConceptoParteInformacionAduanera=$this->getData_Comprobante_Conceptos_Concepto_Parte_InformacionAduanera($arrConParInfAdu[$i],'str_src');
							$stringSource 	.="|$atrConceptoParteInformacionAduanera";
						}
					}
				}
			}
		}
		if($impRet){
			for($i=0;$i<count($arrRet);$i++){
				$atrConceptoRetencionesRetencion=$this->getData_Comprobante_Retenciones_Retencion($arrRet[$i],'str_src');
				$stringSource 	.="|$atrConceptoRetencionesRetencion";
			}
		}
		if($impTra){
			for($i=0;$i<count($arrTra);$i++){
				$atrConceptoTrasladosTraslado=$this->getData_Comprobante_Traslados_Traslado($arrTra[$i],'str_src');
				$stringSource 	.="|$atrConceptoTrasladosTraslado";
			}			
		}
		$stringSource 	.="|$atrComprobanteImpuestos";
		$stringSource.="||";
		return $stringSource;					
	}
	
	function getDigitalSign(){
		$saveSourceString=$this->saveSourceString;
		$saveMD5=$this->saveMD5;
		$saveDigitalSign=$this->saveDigitalSign;
		$sq_encfac=$this->encFac->busReg();
		$dt_encfac=mysqli_fetch_array($sq_encfac);
		$sourceString=trim($this->getStringSource());	
		$key=$this->arrSuc[$this->idSuc]['key'];
		$cer=$this->arrSuc[$this->idSuc]['key'];
		$pathKey=$this->rootPathKey;
		$pathSourceString=$this->pathSourceString;
		$pathMD5=$this->pathMD5;
		$pathXML=$this->pathXML;
		$pathDigitalSign=$this->pathDigitalSign;
		$pathOPENSSL=$this->rootPathOPENSSL;
		$nameSourceString_file="strSrc_$dt_encfac['idTipFac']-$dt_encfac['numFac']_$dt_encfac['idBascps'].txt";
		$nameMD5_file="md5_$dt_encfac['idTipFac']-$dt_encfac['numFac']_$dt_encfac['idBascps'].txt";
		$nameDigitalSign_file="digSig_$dt_encfac['idTipFac']-$dt_encfac['numFac']_$dt_encfac['idBascps'].txt";
		$nameXML_file="xml_$dt_encfac['idTipFac']-$dt_encfac['numFac']_$dt_encfac['idBascps'].xml";
		
		
		//---------->Codificar Cadena Original a UTF-8----------------------------->
		$sourceString = mb_convert_encoding($sourceString, 'UTF-8', 'HTML-ENTITIES');
		//---------->Aplicar MD5 a Cadena Original--------------------------------->
		$sourceStringMD5=md5($sourceString);
		//---------->Guardar Archivo conteniendo MD5 de Cadena Original------------>
		$sourceStringMD5_file=$this->createFile($sourceStringMD5,$pathMD5.$nameMD5_file);
		//---------->Crear Comando Para general Certificado Digital---------------->
		$digitalSign_command = $pathOPENSSL." dgst -md5 -sign ".$pathKey.$key." ".$sourceStringMD5_file." | ".$pathOPENSSL." enc -base64  -A >  ".$pathDigitalSign.$nameDigitalSign_file;
		//---------->Ejecutar Comando--------------------------------------------->
		exec($digitalSign_command);
		//---------->Recuperar Certificado Digital de Archivo Generado------------>
		$digitalSign=$this->getTextFile($pathDigitalSign.$nameDigitalSign_file);
		
		//Verifica si guarda Cadena Original
		if($saveSourceString) $this->createFile($sourceString,$pathSourceString.$nameSourceString_file);
		//Verifica si debe dejar archivos de proceso guardados
		if(!$saveMD5) unlink($pathMD5.$nameMD5_file);
		if(!$saveDigitalSign) unlink($pathDigitalSign.nameDigitalSign_file);
		return $digitalSign;
		//echo $stringSource;
	}
	function getXML(){
		$saveXML=$this->saveXML;
		$pathXML=$this->pathXML;
		$impDomExp=$this->impDomExp;
		$impInfAdu=$this->impInfAdu;//Imprimir Informacion Aduanera                  CONCEPTO
		$impCuePred=$this->impCuePred;//Imprimir Cuenta Predial                       CONCEPTO
		$impComCon=$this->impComCon;//Imprimir Complemento de Concepto               CONCEPTO
		$impParCon=$this->impParCon;//Imprimir Parte o componentes que componen el concepto CONCEPTO		
		$impRet=$this->impRet;//Imprimir retenciones IMPUESTOS
		$impTra=$this->impTra;//Imprimir traslados   IMPUESTOS
		$sq_encfac=$this->encFac->busReg();
		$dt_encfac=mysqli_fetch_array($sq_encfac);
		$sq_detfac=$this->detFac->busByEnc();
		$dt_encfac['tipCom']='ingreso';
		$nameXML_file="xml_$dt_encfac['idTipFac']-$dt_encfac['numFac']_$dt_encfac['idBascps'].xml";	
		
		$dt_empres=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT geoyos_jc.dxempres.rfc, geoyos_jc.dxempres.nom_emp as nom,geoyos_jc.dxempres.calle, geoyos_jc.dxempres.no_ext as numExt,geoyos_jc.dxempres.no_int as numInt, geoyos_jc.dxempres.des_col as desCol,geoyos_jc.dxdelmun.des_delmun as desMun,geoyos_jc.dxestado.des_estado as desEst,geoyos_jc.dxpaises.des_pais as desPais,geoyos_jc.dxempres.cod_pos as codPos FROM geoyos_jc.dxempres INNER JOIN geoyos_jc.dxpaises ON(geoyos_jc.dxpaises.cve_pais=geoyos_jc.dxempres.cve_pais) INNER JOIN geoyos_jc.dxestado ON(geoyos_jc.dxestado.cve_estado=geoyos_jc.dxempres.cve_estado) INNER JOIN geoyos_jc.dxdelmun ON(geoyos_jc.dxdelmun.cve_delmun=geoyos_jc.dxempres.cve_delmun) WHERE  geoyos_jc.dxempres.cla_emp='".$_SESSION['empresa']."'"));
				
		//-----------------------------COMPROBANTE------------------------------------------------------------------------------>
		$atrComprobante=$this->getData_Comprobante($dt_encfac);
		//-----------------------------/COMPROBANTE------------------------------------------------------------------------------>		
		//-----------------------------EMISOR------------------------------------------------------------------------------>
		$atrComprobanteEmisor=$this->getData_Comprobante_Emisor($dt_empres);
		
		$atrComprobanteEmisorDomicilioFiscal=$this->getData_Comprobante_Emisor_DomicilioFiscal($dt_empres);	
		if($impDomExp)
			$atrComprobanteEmisorExpedidoEn=$this->getData_Comprobante_Emisor_ExpedidoEn($dt_encfac);	
		//-----------------------------/EMISOR------------------------------------------------------------------------------>
		//-----------------------------RECEPTOR------------------------------------------------------------------------------>
		$atrComprobanteReceptor=$this->getData_Comprobante_Receptor($dt_encfac);
		$atrComprobanteReceptorDomicilio=$this->getData_Comprobante_Receptor_Domicilio($dt_encfac);		
		//-----------------------------/RECEPTOR------------------------------------------------------------------------------>
		//-----------------------------IMPUESTOS------------------------------------------------------------------------------>
		$arrImpuestos['totImpTra']=$dt_encfac['ivaFac'];
		$arrImpuestos['totImpRet']="0.00";
		$atrComprobanteImpuestos=$this->getData_Comprobante_Impuestos($arrImpuestos);	
		$arrTra[]=array("tipImp"=>"IVA","impTra"=>$arrImpuestos['totImpTra'],"tasaImp"=>$dt_encfac['porTasIva']);
		//-----------------------------/IMPUESTOS------------------------------------------------------------------------------>		
		$xmlCFD .="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		$xmlCFD .="<Comprobante xmlns=\"http://www.sat.gob.mx/cfd/2\"
						xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
						xsi:schemaLocation=\"
						http://www.sat.gob.mx/cfd/2
						http://www.sat.gob.mx/sitio_internet/cfd/2/cfdv2.xsd\" 
						$atrComprobante>";
		$xmlCFD 	.="<Emisor $atrComprobanteEmisor>";
		$xmlCFD 		.="<DomicilioFiscal $atrComprobanteEmisorDomicilioFiscal>";
		$xmlCFD 		.="</DomicilioFiscal>";
		if($impDomExp){
			$xmlCFD 	.="<ExpedidoEn $atrComprobanteEmisorExpedidoEn>";
			$xmlCFD 	.="</ExpedidoEn>";
		}
		$xmlCFD 	.="</Emisor>";
		$xmlCFD 	.="<Receptor $atrComprobanteReceptor>";
		$xmlCFD 		.="<Domicilio $atrComprobanteReceptorDomicilio>";
		$xmlCFD 		.="</Domicilio>";		
		$xmlCFD 	.="</Receptor>";			 	
		$xmlCFD 	.="<Conceptos>";
		while($dt_detfac=mysqli_fetch_array($sq_detfac)){
			$atrComprobanteConceptosConcepto=$atrComprobanteConceptosConcepto=$this->getData_Comprobante_Conceptos_Concepto($dt_detfac);
			$xmlCFD 	.="<Concepto $atrComprobanteConceptosConcepto>";
			if($impInfAdu){
				for($i=0;$i<count($arrInfAdu);$i++){
					$atrConceptoInformacionAduanera=$this->getData_Comprobante_Conceptos_Concepto_InformacionAduanera($arrInfAdu[$i]);
					$xmlCFD 	.="<InformacionAduanera $atrConceptoInformacionAduanera>";
					$xmlCFD 	.="</InformacionAduanera>";
				}
			}
			if($impCuePred){
				$atrConceptocuentaPredial=$this->getData_Comprobante_Conceptos_Concepto_CuentaPredial($dt_detfac);
				$xmlCFD 	.="<CuentaPredial $atrConceptocuentaPredial>";
				$xmlCFD 	.="</CuentaPredial>";
			}
			if($impComCon){
				for($i=0;$i<count($arrComCon);$i++){
					$atrConceptoComplementoConcepto=$this->getData_Comprobante_Conceptos_Concepto_ComplementoConcepto($arrComCon[$i]);			
					$xmlCFD 	.="<ComplementoConcepto $atrConceptoComplementoConcepto>";
					$xmlCFD 	.="</ComplementoConcepto>";
				}
			}
			if($impParCon){
				for($j=0;$j<count($arrConPar);$j++){
					$atrConceptoParte=$this->getData_Comprobante_Conceptos_Concepto_Parte($arrConPar[$j]);
					$xmlCFD 	.="<Parte $atrConceptoParte>";
					if($impConParInfAdu){
						for($i=0;$i<count($arrConParInfAdu);$i++){
							$atrConceptoParteInformacionAduanera=$this->getData_Comprobante_Conceptos_Concepto_Parte_InformacionAduanera($arrConParInfAdu[$i]);
							$xmlCFD 	.="<InformacionAduanera $atrConceptoParteInformacionAduanera>";
							$xmlCFD 	.="</InformacionAduanera>";
						}
					}
					$xmlCFD 	.="</Parte>";
				}
			}
			$xmlCFD 	.="</Concepto>";		
		}
		$xmlCFD 	.="</Conceptos>";
		$xmlCFD 	.="<Impuestos $atrComprobanteImpuestos>";
		if($impRet){
			$xmlCFD 	.="<Retenciones>";
			for($i=0;$i<count($arrRet);$i++){
				$atrConceptoRetencionesRetencion=$this->getData_Comprobante_Retenciones_Retencion($arrRet[$i]);
				$xmlCFD 	.="<Retencion $atrConceptoRetencionesRetencion>";
				$xmlCFD 	.="</Retencion>";
			}
			$xmlCFD 	.="</Retenciones>";
		}
		if($impTra){
			$xmlCFD 	.="<Traslados>";
			for($i=0;$i<count($arrTra);$i++){
				$atrConceptoTrasladosTraslado=$this->getData_Comprobante_Traslados_Traslado($arrTra[$i]);
				$xmlCFD 	.="<Traslado $atrConceptoTrasladosTraslado>";
				$xmlCFD 	.="</Traslado>";
			}			
			$xmlCFD 	.="</Traslados>";
		}					
		$xmlCFD 	.="</Impuestos>";
		$xmlCFD .="</Comprobante>";
		
		if($saveXML){
			$xmlCFD=mb_convert_encoding($xmlCFD, 'UTF-8');
			$sourceStringMD5_file=$this->createFile($xmlCFD,$pathXML.$nameXML_file);
		}
		return $xmlCFD;
	}
	
	function getData_Comprobante($dtEncFac,$typeGen='xml'){
		$impMetPag=$this->impMetPag;
		$impConPag=$this->impConPag;
		$impCer64 =$this->impCer64;
		$noCertificado=$this->arrSuc[$this->idSuc]['numSerCer'];
		$dtEncFac['numAprFac']=$this->arrSuc[$this->idSuc]['numAut'];
		$dtEncFac['axoAprFac']='2009';
		$tipoDeComprobante='ingreso';
		
		$data['version']      =$this->verXSD;
		//OPCIONAL-->Verifica Si Aplica Numero de Serie
		if(strcmp("$dt_encfac['idTipFac']","0")!=0)  
			$data['serie']=$dtEncFac['idTipFac'];	
		$data['folio']        =$dtEncFac['numFac'];
		$data['fecha']        =$dtEncFac['fecThorFac'];
		if(strcmp($typeGen,'str_src')!=0)
			$data['sello']        =$this->getDigitalSign();	//Obtenemos sello digital
		$data['noAprobacion'] =$dtEncFac['numAprFac'];
		$data['anoAprobacion']=$dtEncFac['axoAprFac'];
		$data['tipoDeComprobante']=$tipoDeComprobante; //Atributo requerido para expresar el efecto del comprobante fiscal para el contribuyente emisor.(Valores: ingreso egreso traslado)
		$data['formaDePago']  ="Pago en una sola exhibicion"; //Forma De Pago (Parcialidad 1 de X, Pago en una sola exhibición)
		if(strcmp($typeGen,"xml")==0)
			$data['noCertificado']=$noCertificado;                //Numero de Serie del Certificado a 20 posiciones expedido por el SAT
		if($impCer64)
			$data['certificado']  =$certificado;                  //OPCIONAL->Certificado en base 64 que ampara el CFD
		if($impConPag)
			$data['condicionesDePago']=$condicionesDePago;        //OPCIONAL->Expresa condiciones comerciales aplicables para el pago del cfd
		$data['subTotal']     =$dtEncFac['subFac'];
		//Verifica si aplico descuento
		if($dt_encfac['impDesFac']!=0){
			$data['descuento']    =$dtEncFac['impDesFac'];   //OPCIONAL->Importe Total de Descuentos
			$data['motivoDescuento']=$dtEncFac['obsDesFac'];  //OPCIONAL->Motivos de descuento
		}
		$data['total']=$dtEncFac['totFac'];
		//OPCIONAL
		if($impMetPag)//Se entiende como método de pago leyendas tales como: cheque, tarjeta de crédito o debito, depósito en cuenta, etc.
			$data['metodoDePago']=$metodoDePago;
		$retDat=$this->implodeData($typeGen,$data);	
		return $retDat;
	}

	function getData_Comprobante_Emisor($dtEncFac,$typeGen="xml"){
		//$dt_empres=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT rfc,nom_emp as nom FROM geoyos_jc.dxempres WHERE cla_emp='".$_SESSION['empresa']."'"));	
		//RFC sin guines o espacios
		$data['rfc']   =str_replace(array("-"," "),"",$dtEncFac['rfc']);
		$data['nombre']=trim($dtEncFac['nom']);
		$retDat=$this->implodeData($typeGen,$data);	
		return $retDat;
	}
	
	function getData_Comprobante_Emisor_DomicilioFiscal($dtInfEmi,$typeGen="xml"){
		$impNumExt=$this->impNumExtEmi;
		$impNumInt=$this->impNumIntEmi;
		$impCol   =$this->impColEmi;
		$impLoc   =$this->impLocEmi;
		$impRef   =$this->impRefEmi;
		$data['calle']      =$dtInfEmi['calle'];
		//OPCIONAL
		if($impNumExt)
			$data['noExterior'] =$dtInfEmi['numExt'];
		//OPCIONAL
		if($impNumInt && trim($dtInfEmi['numInt'])!="" && trim($dtInfEmi['numInt'])!='0')
			$data['noInterior'] =$dtInfEmi['numInt'];
		//OPCIONAL
		if($impCol)
			$data['colonia']    =$dtInfEmi['desCol'];
		//OPCIONAL
		if($impLoc)
			$data['localidad']  =$dtInfEmi['loc'];
		//OPCIONAL
		if($impRef)
			$data['referencia'] =$dtInfEmi['ref'];
		$data['municipio']   =$dtInfEmi['desMun'];
		$data['estado']      =$dtInfEmi['desEst'];
		$data['pais']        =$dtInfEmi['desPais'];
		$data['codigoPostal']=$dtInfEmi['codPos'];
		
		$retDat=$this->implodeData($typeGen,$data);	
		return $retDat;
	}

	function getData_Comprobante_Emisor_ExpedidoEn($dtInfEmi,$typeGen="xml"){
		$impNumExt=$this->impNumExtExp;
		$impNumInt=$this->impNumIntExp;
		$impCol   =$this->impColExp;
		$impLoc   =$this->impLocExp;
		$impRef   =$this->impRefExp;
		$impMun   =$this->impMunExp;
		$impEst   =$this->impEstExp;
		$impCodPos=$this->impCodPosExp;
		$data['calle']      =$dtInfEmi['calle'];
		//OPCIONAL
		if($impNumExt)
			$data['noExterior'] =$dtInfEmi['numExt'];
		//OPCIONAL
		if($impNumInt && trim($dtInfEmi['numInt'])!="" && trim($dtInfEmi['numInt'])!='0')
			$data['noInterior'] =$dtInfEmi['numInt'];
		//OPCIONAL
		if($impCol)
			$data['colonia']    =$dtInfEmi['desCol'];
		//OPCIONAL
		if($impLoc)
			$data['localidad']  =$dtInfEmi['loc'];
		//OPCIONAL
		if($impRef)
			$data['referencia'] =$dtInfEmi['ref'];
		//OPCIONAL
		if($impMun)
			$data['municipio']   =$dtInfEmi['desMun'];
		//OPCIONAL
		if($impEst)
			$data['estado']      =$dtInfEmi['desEst'];
		$data['pais']        =$dtInfEmi['desPais'];
		//OPCIONAL
		if($impCodPos)
			$data['codigoPostal']=$dtInfEmi['codPos'];
		
		$retDat=$this->implodeData($typeGen,$data);	
		return $retDat;
	}
	
	function getData_Comprobante_Receptor($dtEncFac,$typeGen="xml"){
		//RFC sin guines o espacios
		$data['rfc']   =str_replace(array("-"," "),"",$dtEncFac['rfcCli']);
		//OPCIONAL
		$data['nombre']=trim($dtEncFac['nomCli']);
		$retDat=$this->implodeData($typeGen,$data);	

		return $retDat;
	}
	
	function getData_Comprobante_Receptor_Domicilio($dtEncFac,$typeGen="xml"){
		$impNumExt=$this->impNumExtCli;
		$impNumInt=$this->impNumIntCli;
		$impCol   =$this->impColCli;
		$impLoc   =$this->impLocCli;
		$impRef   =$this->impRefCli;
		$impMun   =$this->impMunCli;
		$impEst   =$this->impEstCli;
		$impCodPos=$this->impCodPosCli;		
		$data['calle']      =$dtEncFac['calleCli'];
		//OPCIONAL
		if($impNumExt)
			$data['noExterior'] =$dtEncFac['numExtCli'];

		//OPCIONAL
		if($impNumInt && trim($dtEncFac['numIntCli'])!="" && trim($dtEncFac['numIntCli'])!='0')
			$data['noInterior'] =$dtEncFac['numIntCli'];
		//OPCIONAL
		if($impCol)
			$data['colonia']    =$dtEncFac['desColCli'];
		//OPCIONAL
		if($impLoc)
			$data['localidad']  =$dtEncFac['locCli'];
		//OPCIONAL
		if($impRef)
			$data['referencia'] =$dtEncFac['refCli'];
		//OPCIONAL
		if($impMun)
			$data['municipio']   =$dtEncFac['desMunCli'];
		//OPCIONAL
		if($impEst)
			$data['estado']      =$dtEncFac['desEdoCli'];
		$data['pais']        =$dtEncFac['desPaisCli'];
		//OPCIONAL
		if($impCodPos)
			$data['codigoPostal']=$dtEncFac['codPosCli'];
		
		$retDat=$this->implodeData($typeGen,$data);		
		return $retDat;
	}

	function getData_Comprobante_Conceptos_Concepto($dtDetFac,$typeGen="xml"){
		$impUniVen=$this->impUniVen;
		$impIdProd=$this->impIdProd;
		$data['cantidad']   =$dtDetFac['canDet'];
		//OPCIONAL
		if($impUniVen)
			$data['unidad']=$dtDetFac['uniDet'];
		if($impIdprod)
			$data['noIdentificacion']=$dtDetFac['uniDet'];
		$data['descripcion']=$dtDetFac['desProd'];
		$data['valorUnitario']=$dtDetFac['preUni'];	
		$data['valorUnitario']=$dtDetFac['preUni'];	
		$data['importe']=$dtDetFac['subDet'];	
		$retDat=$this->implodeData($typeGen,$data);	

		return $retDat;
	}

	function getData_Comprobante_Conceptos_Concepto_InformacionAduanera($dtDetFac,$typeGen="xml"){
		$data['numero']=$dtDetFac['numDocAdu'];
		$data['fecha'] =$dtDetFac['fecDocAdu'];//aaaa-mm-dd	
		$data['aduana']=$dtDetFac['desAdu'];//Precisa Inf. Aduana	
		$retDat=$this->implodeData($typeGen,$data);	
		return $retDat;
	}
	function getData_Comprobante_Conceptos_Concepto_CuentaPredial($dtDetFac,$typeGen="xml"){
		$data['numero']=$dtDetFac['numCuePred'];
		$retDat=$this->implodeData($typeGen,$data);	
		return $retDat;
	}
	function getData_Comprobante_Conceptos_Concepto_ComplementoConcepto($dtDetFac,$typeGen="xml"){
		//No Tiene Atributos, contiene nodos de extencion particulares, acorde con el SAT	
		return $retDat;
	}
	function getData_Comprobante_Conceptos_Concepto_Parte($dtDetFac,$typeGen="xml"){
		$retDat=$this-> getData_Comprobante_Conceptos_Concepto($dtDetFac);	
		return $retDat;
	}
	function getData_Comprobante_Conceptos_Concepto_Parte_InformacionAduanera($dtDetFac,$typeGen="xml"){
		$retDat=$this-> getData_Comprobante_Conceptos_Concepto_InformacionAduanera($dtDetFac);	
		return $retDat;
	}
	function getData_Comprobante_Impuestos($dtEncFac,$typeGen="xml"){
		$impTotImpRet=$this->impTotImpRet;
		$impTotImpTra=$this->impTotImpTra;
		if($impTotImpRet)
			$data['totalImpuestosRetenidos']=$dtEncFac['totImpRet'];
		if($impTotImpTra)
			$data['totalImpuestosTrasladados'] =$dtEncFac['totImpTra'];
		$retDat=$this->implodeData($typeGen,$data);	
		return $retDat;
	}
	function getData_Comprobante_Retenciones_Retencion($dtDetFac,$typeGen="xml"){
		$data['impuesto']=$dtDetFac['tipImp'];//DOS POSIBLES VALORES (ISR,IVA)
		$data['importe'] =$dtDetFac['impRet'];//importe de la retencion
		$retDat=$this->implodeData($typeGen,$data);	
		return $retDat;
	}
	function getData_Comprobante_Traslados_Traslado($dtDetFac,$typeGen="xml"){
		$data['impuesto']=$dtDetFac['tipImp'];//DOS POSIBLES VALORES (IEPS,IVA)
		$data['tasa']=$dtDetFac['tasaImp'];//Señalar la tasa de impuesto que se traslada (decimal)
		$data['importe'] =$dtDetFac['impTra'];//importe de la retencion
		$retDat=$this->implodeData($typeGen,$data);	
		return $retDat;
	}								
									
	function implodeData($typeImplode,$arrData){
		if(strcmp("xml",$typeImplode)==0){ 
			foreach($arrData as $atr=>$val){
				$retDat.="$atr=\"$val\" ";
			}
			$retDat=trim($retDat);
		}elseif(strcmp("str_src",$typeImplode)==0){ 
			$retDat=implode("|",$arrData);
		}
		return $retDat;
	}	
	
	function checkData($data,$default){
		/*if(isset($data)){ 
			if(is_array($data)){
				foreach($data as $id=>$value)
					$data[$id]=$this->checkData($data[$id],$default);
			}else{	
				$auxData=$data;
			}
		}else{ 
			$auxData=$default;
		}
		if(!is_array($data)){
			if(get_magic_quotes_gpc()) 
				$auxData = stripslashes($auxData);
			$auxData=htmlentities($auxData);
			$auxData=mysqli_real_escape_string($con,$auxData);
		}*/
		
		return $data;
	}
	
	/*
	 * Genera un archivo aleatoriamente en base a un texto pasado
	 * */
	function createFile($textContent="", $filename=""){
			if($filename!=""){
					$fh = fopen($filename, 'w') or die("ERROR");
					fwrite($fh, $textContent);
					fclose($fh);
					return $filename;
			}
	}
	
	/*
	 * Obtiene el contenido de un archivo de texto
	 */
	function getTextFile($file=""){
			$f = fopen($file, 'r') or die("ERROR");
			$line = fgets($f, 4096) ;
			return $line;
	}	
}
?>