function get_ObjetoFactura($GenerarFacturaRequest)
 {
     require_once 'lib/class.face.txt_amece.php';
     $reporte = "";
     $factura = NULL;
     $msgEmision = "Reporte: <br>\n";
     $msgEmision .= "<br>procesando archivo...";
     $buffer = strtr(base64_decode($GenerarFacturaRequest->cfd), utf8_decode("á,é,í,ó,ú,Á,É,Í,Ó,Ú"), "a,e,i,o,u,A,E,I,O,U");
     $buffer = trim(utf8_encode($buffer));
     $factura = new Factura();
     $factura->bu = $GenerarFacturaRequest->client;
     $factura = getObjetoFactura($buffer, $factura);
     if (is_null($factura)) {
         $reporte .= "EN EL REGISTRO DE LA FACTURA " . $factura->encabezado->serie . $factura->encabezado->folio . "\n\r";
     } else {
         $ROUTETOINCLUDE = '../../../../';
         $empresaController = new EmpresaController($ROUTETOINCLUDE);
         $data['bu'] = $factura->bu;
         $factura->encabezado->bu = $factura->bu;
         $data['status'] = 0;
         $empresa = $empresaController->execute('buStatus', $data);
         if ($empresa['cantidad'] == 0) {
             $reporte .= "ERROR EN EMPRESA PROCESANDO FACTURA ";
             $factura->exito = false;
             $factura->mensaje = $reporte;
             return $factura;
         } else {
             $factura->emisor = $empresa['respuesta'];
             $this->getDatosInternos(&$factura);
             $data['sucursal'] = $factura->encabezado->sucursal;
             $data['serie'] = $factura->encabezado->serie;
             $data['fechahoraemision'] = $factura->encabezado->fechaemision . " " . $factura->encabezado->horaemision;
             $data['idempresa'] = $factura->emisor['idempresa'];
             $factura->encabezado->lugarExpedicion = $factura->emisor['municipio'];
             /**
              * @internal Seleccion de folio (si viene se pone, si no se calcula el folio maximo de FACE)
              */
             if ($factura->encabezado->folio == "getFolio") {
                 $dato = $factura->addenda;
                 $folio = $this->getFolio($factura->emisor['idempresa'], $factura->encabezado->serie);
                 $factura->encabezado->folio = isset($dato['Consecutive']) && trim($dato['Consecutive']) != "" ? $dato['Consecutive'] : intval($folio);
             }
         }
         $sucursalController = new SucursalController($ROUTETOINCLUDE);
         $sucursal = $sucursalController->execute('nombreSucIdempresa', $data);
         if ($sucursal['cantidad'] > 0) {
             $folioController = new FolioController($ROUTETOINCLUDE);
             $data['folio'] = $factura->encabezado->folio;
             $factura->encabezado->folio = $data['folio'];
             $folioA = $folioController->execute('actualizaFolioActual', $data);
             $facturaController = new FacturaController($ROUTETOINCLUDE);
             $facturaExistente = $facturaController->execute('existeRegistro', $data);
             if ($facturaExistente['cantidad'] == 0) {
                 //VALIDAR  CERTIFICADO DE LA SUCURSAL
                 $colname_sello = "0";
                 $selloController = new SelloController($ROUTETOINCLUDE);
                 $sello = $selloController->execute('selloSucursalStatus', $data);
                 if ($sello['cantidad'] > 0) {
                     $factura->sello = $sello['respuesta'];
                     error_log("CS: Objeto Factura completo " . date("Y-m-d H:m:s"));
                     if ($factura->encabezado->mailEntrega != FALSE || $factura->encabezado->mailEntrega != "") {
                         $factura->mail = TRUE;
                         $factura->exito = true;
                     }
                 } else {
                     $factura->exito = false;
                 }
             } else {
                 //Reproceso de facturas, el folio ya existe
                 $reporte .= "verifica si existe el registro " . $factura->encabezado->serie . "-" . $factura->encabezado->folio;
                 $factura->exito = FALSE;
             }
         } else {
             $reporte .= " EN EL REGISTRO DE LA FACTURA SUCURSAL";
             $factura->exito = FALSE;
         }
     }
     $factura->mensaje = $reporte;
     return $factura;
 }
function get_ObjetoFactura($GenerarFacturaRequest){		
		require_once(dirname(dirname(__FILE__)).'/kio/lib/class.face.txt_amece.php');
		$path='../';
		$data = "";
		$reporte 	= "";
		$factura 	= NULL;		
		$msgEmision = "Reporte: <br>\n";	
		$msgEmision.= "<br>procesando archivo...";
		$buffer = trim(utf8_encode(utf8_decode(base64_decode($GenerarFacturaRequest->cfd))));
		$buffer = substr($buffer, 1, strlen($buffer));
		$buffer = json_decode($buffer);
		
		$factura = new Factura;
		$factura->bu = $GenerarFacturaRequest->client;
		$factura->bu = 1;//quitar
		$factura = getObjetoFactura($buffer, $factura);
		
		if(!$this->isJson($buffer)){
			$reporte.="ERROR EN JSON ";
			$factura->exito = false; 
			$factura->mensaje=$reporte;
			return $factura;
		}
		
		if(is_null($factura)){
			$reporte.="EN EL REGISTRO DE LA FACTURA ".$factura->encabezado->serie.$factura->encabezado->folio."\n\r";		
		} else {
			$ROUTETOINCLUDE='../../../../';
			$empresaController  = new EmpresaController($ROUTETOINCLUDE);
			$sucursalController = new SucursalController($ROUTETOINCLUDE);
			$facturaController = new FacturaController($ROUTETOINCLUDE);
			$selloController = new SelloController($ROUTETOINCLUDE);
			
			$data['bu'] = $factura->bu;			
			$data['status'] = 1; //chs - 20131111 valida que esten activa la empresa y valida que este activa la factura
			$data['sucursal']=$factura->encabezado->sucursal;
			$data['serie'] =$factura->encabezado->serie;
			$data['folio'] =$factura->encabezado->folio;
			$data['fechahoraemision'] = $factura->encabezado->fechaemision." ".$factura->encabezado->horaemision;	
			
			$empresa = $empresaController -> execute('buStatus',$data);
			
			if($empresa['cantidad'] == 0 ){
				$reporte.="ERROR EN EMPRESA PROCESANDO FACTURA ";
				$factura->exito = false; 
				$factura->mensaje=$reporte;
				return $factura;
			}else{
				$factura->emisor = $empresa['respuesta'];
				$data['idempresa'] = $factura->emisor['idempresa'];
			}
			
			$sucursal = $sucursalController-> execute('nombreSucIdempresa',$data);

			if($sucursal['cantidad'] > 0 ){
				$factura -> sucursal = $sucursal['respuesta'];
				if($factura->encabezado->folio != 0){
					$facturaExistente = $facturaController-> execute('existeRegistro',$data);
					if($facturaExistente['cantidad'] != 0){
						$reporte.=sprintf("El registro %s - %s ya existe",$factura->encabezado->serie,$factura->encabezado->folio);
						$factura -> exito	= FALSE;
						$factura -> mensaje	= $reporte;
						return $factura;
					}
				} 
					
				//VALIDAR  CERTIFICADO DE LA SUCURSAL
				$colname_sello = "0";
				$sello = $selloController-> execute('selloSucursalStatus',$data);
				if($sello['cantidad'] > 0){
					$factura->sello = $sello['respuesta'];
					log_action("CS: Objeto Factura completo ".date("Y-m-d H:m:s"));
					$factura->exito=true;
				}else{
					$reporte = sprintf("No se encontro sello para el documento %s %d",$factura->encabezado->serie,$factura->encabezado->folio);
					$factura->exito=false;						
				}
			}else{
				$reporte.=" EN EL REGISTRO DE LA FACTURA SUCURSAL";
				$factura->exito=FALSE;
			}
		}
		$factura->mensaje=$reporte;
		return $factura;
		}/* {