Example #1
0
        private wsbfe.BFEResponseAuthorize ReprocessOnError(wsbfe.ClsBFEAuthRequest objBFEAuthRequest, wsbfe.ClsBFERequest objBFERequest)
        {
            //consulto el cae
            wsbfe.BFEGetCMPResponse objBFEGetCMPResponse = new wsbfe.BFEGetCMPResponse();
            wsbfe.ClsBFEGetCMP objCMP = new wsbfe.ClsBFEGetCMP();
            wsbfe.BFEResponseAuthorize objBFEResponseAuthorize = new wsbfe.BFEResponseAuthorize();

            objCMP.Punto_vta = Convert.ToInt16(objBFERequest.Punto_vta);
            objCMP.Tipo_cbte = Convert.ToInt16(objBFERequest.Tipo_cbte);
            objCMP.Cbte_nro = Convert.ToInt64(objBFERequest.Cbte_nro);

            try
            {
                objBFEGetCMPResponse = bfeService.BFEGetCMP(objBFEAuthRequest, objCMP);

                if (objBFEGetCMPResponse.BFEResultGet != null && objBFEGetCMPResponse.BFEResultGet.Fch_venc_Cae != null)
                {
                    if (objBFEResponseAuthorize.BFEErr == null)
                    {
                        objBFEResponseAuthorize.BFEErr = new wsbfe.ClsBFEErr();
                    }
                    objBFEResponseAuthorize.BFEErr.ErrCode = objBFEGetCMPResponse.BFEErr.ErrCode;
                    objBFEResponseAuthorize.BFEErr.ErrMsg = objBFEGetCMPResponse.BFEErr.ErrMsg;

                    if (objBFEResponseAuthorize.BFEEvents == null)
                    {
                        objBFEResponseAuthorize.BFEEvents = new wsbfe.ClsBFEEvents();
                    }
                    objBFEResponseAuthorize.BFEEvents.EventCode = objBFEGetCMPResponse.BFEEvents.EventCode;
                    objBFEResponseAuthorize.BFEEvents.EventMsg = objBFEGetCMPResponse.BFEEvents.EventMsg;

                    if (objBFEResponseAuthorize.BFEResultAuth == null)
                    {
                        objBFEResponseAuthorize.BFEResultAuth = new wsbfe.ClsBFEOutAuthorize();
                    }
                    objBFEResponseAuthorize.BFEResultAuth.Resultado = "A";
                    objBFEResponseAuthorize.BFEResultAuth.Cae = objBFEGetCMPResponse.BFEResultGet.Cae;
                    objBFEResponseAuthorize.BFEResultAuth.Cuit = objBFEGetCMPResponse.BFEResultGet.Cuit;
                    objBFEResponseAuthorize.BFEResultAuth.Fch_cbte = objBFEGetCMPResponse.BFEResultGet.Fecha_cbte_cae;
                    objBFEResponseAuthorize.BFEResultAuth.Fch_venc_Cae = objBFEGetCMPResponse.BFEResultGet.Fch_venc_Cae;
                    objBFEResponseAuthorize.BFEResultAuth.Id = objBFEGetCMPResponse.BFEResultGet.Id;
                    objBFEResponseAuthorize.BFEResultAuth.Obs = objBFEGetCMPResponse.BFEResultGet.Obs;
                    objBFEResponseAuthorize.BFEResultAuth.Resultado = objBFEGetCMPResponse.BFEResultGet.Resultado;
                    objBFEResponseAuthorize.BFEResultAuth.Reproceso = "S";
                }
            }
            catch
            {
                objBFEResponseAuthorize = null;
            }

            return objBFEResponseAuthorize;
        }
Example #2
0
        public wsbfe.BFEResponseAuthorize BFEAuthorize(RequestBatch docBatch, Settings oSettings)
        {
            wsbfe.ClsBFERequest objBFERequest = new wsbfe.ClsBFERequest();
            wsbfe.BFEResponseAuthorize objBFEResponseAuthorize = new wsbfe.BFEResponseAuthorize();
            wsbfe.BFEResponse_LastID objBFEResponseLastID = null;

            DBEngine.SQLEngine sqlEngine = new FacturaElectronica.DBEngine.SQLEngine();

            int i = 0;

            try
            {
                //DEBUG LINE
                if (Convert.ToBoolean(oSettings.ActivarDebug))
                    Utils.Utils.DebugLine(Utils.Utils.SerializeObject(objBFEAuthRequest), oSettings.PathDebug + "\\ClsBFEAuthRequest-" + DateTime.Now.Hour.ToString("00") + DateTime.Now.Minute.ToString("00") + DateTime.Now.Second.ToString("00") + DateTime.Now.Millisecond.ToString("000") + ".xml");

                //Si no tiene identificador único hay que generarlo
                if (docBatch.BatchUniqueId == "AUTO")
                {
                    objBFEResponseLastID = GetLastBatchUniqueId();
                    if (objBFEResponseLastID.BFEResultGet == null)
                    {
                        sqlEngine.LogError(docBatch.RequestHeaders[0].SQLID, "0", "Autorización", "Error AFIP al obtener el último nro de requerimiento (" + objBFEResponseLastID.BFEErr.ErrCode + ") " + objBFEResponseLastID.BFEErr.ErrMsg);
                    }
                    else
                    {
                        objBFEResponseLastID.BFEResultGet.Id = objBFEResponseLastID.BFEResultGet.Id + 1;
                        docBatch.BatchUniqueId = (objBFEResponseLastID.BFEResultGet.Id).ToString();

                        //Guardar Unique Batch ID que luego se utilizara para reprocesos y obtener CAE
                        sqlEngine.UpdateCabeceraBatchUniqueId(docBatch.RequestHeaders[0].SQLID, docBatch.BatchUniqueId);
                    }
                }

                //DEBUG LINE
                if (Convert.ToBoolean(oSettings.ActivarDebug))
                    Utils.Utils.DebugLine(Utils.Utils.SerializeObject(docBatch), oSettings.PathDebug + "\\DocumentBatch-" + DateTime.Now.Hour.ToString("00") + DateTime.Now.Minute.ToString("00") + DateTime.Now.Second.ToString("00") + DateTime.Now.Millisecond.ToString("000") + ".xml");

                string fieldName = "";
                string seccionName = "";

                try
                {
                    seccionName = "Cabecera";
                    fieldName = "BatchUniqueId";
                    objBFERequest.Id = (long)Convert.ToDouble(docBatch.BatchUniqueId);
                    fieldName = "TipoComprobante";
                    objBFERequest.Tipo_cbte = Convert.ToInt16(docBatch.RequestHeaders[0].TipoComprobante);
                    fieldName = "PuntoVenta";
                    objBFERequest.Punto_vta = Convert.ToInt16(docBatch.RequestHeaders[0].PuntoVenta);
                    fieldName = "NroComprobanteDesde";
                    objBFERequest.Cbte_nro = (long)Convert.ToDouble(docBatch.RequestHeaders[0].NroComprobanteDesde);
                    fieldName = "ImporteMonedaFacturacion";
                    objBFERequest.Imp_total = Convert.ToDouble(docBatch.RequestHeaders[0].ImporteMonedaFacturacion);
                    fieldName = "FechaComprobante";
                    objBFERequest.Fecha_cbte = Convert.ToDateTime(docBatch.RequestHeaders[0].FechaComprobante).ToString("yyyyMMdd");
                    fieldName = "ImportePercepcionIIBBMonedaFacturacion";
                    objBFERequest.Imp_iibb = Convert.ToDouble(docBatch.RequestHeaders[0].ImportePercepcionIIBBMonedaFacturacion);
                    fieldName = "ImporteImpuestosInternosMonedaFacturacion";
                    objBFERequest.Imp_internos = Convert.ToDouble(docBatch.RequestHeaders[0].ImporteImpuestosInternosMonedaFacturacion);
                    fieldName = "ImporteExentoMonedaFacturacion";
                    objBFERequest.Imp_op_ex = Convert.ToDouble(docBatch.RequestHeaders[0].ImporteExentoMonedaFacturacion);
                    fieldName = "ImportePercepciones_PagosCuentaImpuestosNacionalesMonedaFacturacion";
                    objBFERequest.Imp_perc = Convert.ToDouble(docBatch.RequestHeaders[0].ImportePercepciones_PagosCuentaImpuestosNacionalesMonedaFacturacion);
                    fieldName = "ImportePercepcionImpuestosMunicipalesMonedaFacturacion";
                    objBFERequest.Imp_perc_mun = Convert.ToDouble(docBatch.RequestHeaders[0].ImportePercepcionImpuestosMunicipalesMonedaFacturacion);
                    fieldName = "ImporteNoGravadoMonedaFacturacion";
                    objBFERequest.Imp_tot_conc = Convert.ToDouble(docBatch.RequestHeaders[0].ImporteNoGravadoMonedaFacturacion);
                    fieldName = "ImporteImpuestoLiquidadoMonedaFacturacion";
                    objBFERequest.Impto_liq = Convert.ToDouble(docBatch.RequestHeaders[0].ImporteImpuestoLiquidadoMonedaFacturacion);
                    fieldName = "CodigoDocumentoComprador";
                    objBFERequest.Tipo_doc = Convert.ToInt16(docBatch.RequestHeaders[0].CompradorCodigoDocumento);
                    fieldName = "NroDocumentoComprador";
                    objBFERequest.Nro_doc = Convert.ToInt64(docBatch.RequestHeaders[0].CompradorNroDocumento);
                    fieldName = "CodigoMoneda";
                    objBFERequest.Imp_moneda_Id = docBatch.RequestHeaders[0].CodigoMoneda;
                    fieldName = "TasaCambio";
                    objBFERequest.Imp_moneda_ctz = Convert.ToDouble(docBatch.RequestHeaders[0].TasaCambio);
                    fieldName = "ImporteRNI_PercepcionMonedaFacturacion";
                    objBFERequest.Impto_liq_rni = Convert.ToDouble(docBatch.RequestHeaders[0].ImporteRNI_PercepcionMonedaFacturacion);

                    objBFERequest.Zona = 1;

                    objBFERequest.Items = new wsbfe.Item[Convert.ToInt32(docBatch.RequestHeaders[0].CantidadRegistrosDetalle)];

                    for (i = 0; i < objBFERequest.Items.Length; i++)
                    {
                        seccionName = "Línea " + i.ToString();
                        objBFERequest.Items[i] = new wsbfe.Item();
                        fieldName = "CodigoProductoEmpresa";
                        objBFERequest.Items[i].Pro_codigo_sec = docBatch.RequestHeaders[0].RequestLines[i].CodigoProductoEmpresa;
                        fieldName = "CodigoProductoNCM";
                        objBFERequest.Items[i].Pro_codigo_ncm = docBatch.RequestHeaders[0].RequestLines[i].CodigoProductoNCM;
                        fieldName = "Descripcion";
                        objBFERequest.Items[i].Pro_ds = docBatch.RequestHeaders[0].RequestLines[i].Descripcion;
                        fieldName = "UnidadMedida";
                        objBFERequest.Items[i].Pro_umed = Convert.ToInt32(docBatch.RequestHeaders[0].RequestLines[i].UnidadMedida);
                        fieldName = "Cantidad";
                        objBFERequest.Items[i].Pro_qty = Convert.ToDouble(docBatch.RequestHeaders[0].RequestLines[i].Cantidad);
                        fieldName = "ImportePrecioUnitarioMonedaFacturacion";
                        objBFERequest.Items[i].Pro_precio_uni = Convert.ToDouble(docBatch.RequestHeaders[0].RequestLines[i].ImportePrecioUnitarioMonedaFacturacion);
                        fieldName = "ImporteSubtotalMonedaFacturacion";
                        objBFERequest.Items[i].Imp_total = Convert.ToDouble(docBatch.RequestHeaders[0].RequestLines[i].ImporteSubtotalMonedaFacturacion);
                        fieldName = "ImporteBonificacionMonedaFacturacion";
                        objBFERequest.Items[i].Imp_bonif = Convert.ToDouble(docBatch.RequestHeaders[0].RequestLines[i].ImporteBonificacionMonedaFacturacion);
                        fieldName = "AlicuotaIVA";
                        objBFERequest.Items[i].Iva_id = Convert.ToInt16(docBatch.RequestHeaders[0].RequestLines[i].AlicuotaIVA);
                    }
                }
                catch (Exception ex)
                {
                    sqlEngine.LogError("0", "0", "Autorización", "Error al asignar el campo: " + seccionName + "." + fieldName + ", " + ex.Message);
                }

                //DEBUG LINE
                if (Convert.ToBoolean(oSettings.ActivarDebug))
                    Utils.Utils.DebugLine(Utils.Utils.SerializeObject(objBFERequest), oSettings.PathDebug + "\\ClsBFERequest-" + DateTime.Now.Hour.ToString("00") + DateTime.Now.Minute.ToString("00") + DateTime.Now.Second.ToString("00") + DateTime.Now.Millisecond.ToString("000") + ".xml");

                try
                {
                    objBFEResponseAuthorize = bfeService.BFEAuthorize(objBFEAuthRequest, objBFERequest);

                    ////WORKAROUND FE BIENES DE CAPITAL
                    if ((objBFEResponseAuthorize == null || objBFEResponseAuthorize.BFEResultAuth == null || objBFEResponseAuthorize.BFEResultAuth.Fch_venc_Cae == null || objBFEResponseAuthorize.BFEResultAuth.Fch_venc_Cae == string.Empty) &&
                        (objBFEResponseAuthorize.BFEResultAuth.Obs == string.Empty) &&
                        (objBFEResponseAuthorize.BFEErr == null ))
                    {

                        //WORKAROUND FE BIENES DE CAPITAL, REINTENTO 3 VECES EN LAPSOS DE 40 SEGUNDOS
                        for (int iw = 0; iw < 5; iw++)
                        {
                            //espero 40 seg
                            System.Threading.Thread.Sleep(60000);

                            //consulto el cae
                            objBFEResponseAuthorize = ReprocessOnError(objBFEAuthRequest, objBFERequest);

                            //verifico si me devuelve el cae
                            if (objBFEResponseAuthorize != null && objBFEResponseAuthorize.BFEResultAuth != null && objBFEResponseAuthorize.BFEResultAuth.Fch_venc_Cae != null && objBFEResponseAuthorize.BFEResultAuth.Fch_venc_Cae != string.Empty)
                                break;
                        }
                    }
                }
                catch
                {
                    //WORKAROUND FE BIENES DE CAPITAL, REINTENTO 3 VECES EN LAPSOS DE 40 SEGUNDOS
                    for (int iw = 0; iw < 5; iw++)
                    {
                        //espero 40 seg
                        System.Threading.Thread.Sleep(60000);

                        //consulto el cae
                        objBFEResponseAuthorize = ReprocessOnError(objBFEAuthRequest, objBFERequest);

                        //verifico si me devuelve el cae
                        if (objBFEResponseAuthorize != null && objBFEResponseAuthorize.BFEResultAuth != null && objBFEResponseAuthorize.BFEResultAuth.Fch_venc_Cae != null && objBFEResponseAuthorize.BFEResultAuth.Fch_venc_Cae != string.Empty)
                            break;
                    }
                }

                //DEBUG LINE
                if (Convert.ToBoolean(oSettings.ActivarDebug))
                    Utils.Utils.DebugLine(Utils.Utils.SerializeObject(objBFEResponseAuthorize), oSettings.PathDebug + "\\BFEResponseAuthorize-" + DateTime.Now.Hour.ToString("00") + DateTime.Now.Minute.ToString("00") + DateTime.Now.Second.ToString("00") + DateTime.Now.Millisecond.ToString("000") + ".xml");
            }
            catch (Exception ex)
            {
                int iElement = 0;
                if (i > 0)
                    iElement = i - 1;
                else
                    iElement = 0;

                if (docBatch == null || (docBatch.RequestHeaders == null && docBatch.RequestHeaders.Count == 0))
                {
                    sqlEngine.LogError("0", "0", "Autorización", "Error: " + ex.Message);
                }
                else
                {
                    if(docBatch.RequestHeaders[iElement] != null)
                        sqlEngine.LogError(docBatch.RequestHeaders[iElement].SQLID, "0", "Autorización", "Error: " + ex.Message);
                    else
                        sqlEngine.LogError("0", "0", "Autorización", "Error: " + ex.Message);
                }

                objBFEResponseAuthorize.BFEErr = new FacturaElectronica.WebServices.wsbfe.ClsBFEErr();

                if (docBatch.BatchUniqueId == "AUTO" || objBFEResponseLastID.BFEResultGet == null)
                {
                    objBFEResponseAuthorize.BFEErr.ErrCode = 667;
                    objBFEResponseAuthorize.BFEErr.ErrMsg = ex.Message + "Error en AFIP al obtener el último nro de requerimiento (" + objBFEResponseLastID.BFEErr.ErrCode.ToString() + ") " + objBFEResponseLastID.BFEErr.ErrMsg;
                }
                else
                {
                    objBFEResponseAuthorize.BFEErr.ErrCode = 668;
                    objBFEResponseAuthorize.BFEErr.ErrMsg = ex.Message;
                    sqlEngine.LogError(docBatch.RequestHeaders[iElement].SQLID, "0", "Autorización", "Error no conocido: (AfipBFE) " + ex.Message);
                }
            }

            return objBFEResponseAuthorize;
        }
Example #3
0
        public string ProcesarLoteFacturasBienesCapital(string EmpresaID, string xmlDocument)
        {
            #region Inicialización

            Settings oSettings = new Settings(EmpresaID);

            wsbfe.Service afipBfexService = new wsbfe.Service();
            afipBfexService.Url = oSettings.UrlAFIPwsbfe;

            wsbfe.ClsBFEAuthRequest afipObjBFEAuthRequest = new wsbfe.ClsBFEAuthRequest();
            wsbfe.BFEResponseAuthorize bfeResponseAuthorize = new wsbfe.BFEResponseAuthorize();

            ResponseBatch batchResponse = new ResponseBatch();
            RequestBatch loteDocs = new RequestBatch();

            string SQLID = "0";
            string estadoDocumento = "";
            string cae = "";
            string FechaVencimiento = "";
            string strEquivalenciaErrorFields = string.Empty;

            bool bRegistrarInicio = false;
            bool bEquivalenciaError = false;

            //Cargar el lote recibido
            loteDocs.LoadXMLString(xmlDocument);

            #endregion

            #region Registrar pedido

            try
            {
                bRegistrarInicio = sqlEngine.LogBatchStart(ref loteDocs);

                SQLID = loteDocs.RequestHeaders[0].SQLID;

                //DEBUG LINE
                if (Convert.ToBoolean(oSettings.ActivarDebug))
                    Utils.Utils.DebugLine(Utils.Utils.SerializeObject(batchResponse), oSettings.PathDebug + "\\" + SQLID + "-FERequestBatch-" + DateTime.Now.Hour.ToString("00") + DateTime.Now.Minute.ToString("00") + DateTime.Now.Second.ToString("00") + DateTime.Now.Millisecond.ToString("000") + ".xml");
            }
            catch (Exception ex)
            {
                bRegistrarInicio = false;
                sqlEngine.LogError(SQLID, "0", "Procesando lote", "Error: " + ex.Message);
            }

            #endregion

            if (bRegistrarInicio)
            {
                #region Verificar Login con AFIP

                AfipConnection afipConn = new AfipConnection("wsbfe", oSettings);
                if (afipConn.ConnectionErrorDescription == string.Empty)
                {
                    //Inicializo el objeto AuthRequest de la Afip
                    afipObjBFEAuthRequest.Cuit = afipConn.Cuit;
                    afipObjBFEAuthRequest.Sign = afipConn.Sign;
                    afipObjBFEAuthRequest.Token = afipConn.Token;
                }
                else
                {
                    try
                    {
                        sqlEngine.LogError(SQLID, "0", "AfipConnection", afipConn.ConnectionErrorDescription);

                        sqlEngine.LogBatchEnd(SQLID, "Error", cae, FechaVencimiento);
                    }
                    catch (Exception ex)
                    {
                        sqlEngine.LogError(SQLID, "0", "FEService-AFIP Login", "Error: " + ex.Message);
                    }
                }

                AfipBFE afipBFE = new AfipBFE(ref afipBfexService, ref afipObjBFEAuthRequest, oSettings);

                #endregion

                if (afipConn.ConnectionErrorDescription == string.Empty)
                {
                    #region Buscar Equivalencias

                    bEquivalenciaError = BuscarEquivalencias(ref loteDocs, oSettings, ref strEquivalenciaErrorFields);

                    #endregion

                    if (!bEquivalenciaError)
                    {
                        #region Realizar Validaciones
                        #endregion

                        #region Hacer el pedido a AFIP

                        try
                        {
                            if (afipConn.IsConnected)
                            {
                                bfeResponseAuthorize = afipBFE.BFEAuthorize(loteDocs, oSettings);

                                if (bfeResponseAuthorize.BFEErr == null)
                                {
                                    sqlEngine.LogError(SQLID, "0", "Respuesta AFIP", "AFIP no pudo procesar por un error previo. Vea el log de errores.");
                                    bfeResponseAuthorize.BFEErr = new wsbfe.ClsBFEErr();
                                    bfeResponseAuthorize.BFEErr.ErrCode = 0;
                                    bfeResponseAuthorize.BFEErr.ErrMsg = "AFIP no pudo procesar por un error previo. Vea el log de errores.";
                                }
                            }
                            else
                            {
                                sqlEngine.LogError(SQLID, "0", "Respuesta AFIP", "Sin Conexion. Descripcion: " + afipConn.ConnectionErrorDescription);
                                bfeResponseAuthorize.BFEErr = new wsbfe.ClsBFEErr();
                                bfeResponseAuthorize.BFEErr.ErrCode = 0;
                                bfeResponseAuthorize.BFEErr.ErrMsg = afipConn.ConnectionErrorDescription;
                            }
                        }
                        catch (Exception ex)
                        {
                            sqlEngine.LogError(SQLID, "0", "Respuesta AFIP", "Error: " + ex.Message);
                        }

                        #endregion
                    }
                }

                #region Armar y devolver respuesta

                //Armar info del lote
                if (bfeResponseAuthorize.BFEResultAuth != null && bfeResponseAuthorize.BFEResultAuth.Cae != null )
                {
                    batchResponse.BatchUniqueId = loteDocs.BatchUniqueId;
                    batchResponse.BatchUniqueId = bfeResponseAuthorize.BFEResultAuth.Id.ToString();
                    batchResponse.Resultado = bfeResponseAuthorize.BFEResultAuth.Resultado;
                    batchResponse.Reproceso = bfeResponseAuthorize.BFEResultAuth.Reproceso;
                    batchResponse.CUITInformante = bfeResponseAuthorize.BFEResultAuth.Cuit.ToString();
                    batchResponse.CantidadComprobantes = "1";

                    //Armar info del documento
                    ResponseHeader thisHeader = new ResponseHeader();

                    thisHeader.CAE = bfeResponseAuthorize.BFEResultAuth.Cae;
                    thisHeader.NroComprobanteDesde = loteDocs.RequestHeaders[0].NroComprobanteDesde.ToString();
                    thisHeader.NroComprobanteHasta = loteDocs.RequestHeaders[0].NroComprobanteHasta.ToString();
                    thisHeader.FechaComprobante = bfeResponseAuthorize.BFEResultAuth.Fch_cbte;
                    thisHeader.FechaVencimiento = bfeResponseAuthorize.BFEResultAuth.Fch_venc_Cae;

                    thisHeader.PuntoVenta = loteDocs.RequestHeaders[0].PuntoVenta.ToString();
                    thisHeader.TipoComprobante = loteDocs.RequestHeaders[0].TipoComprobante.ToString();
                    thisHeader.NroInternoERP = loteDocs.RequestHeaders[0].NroInternoERP;
                    thisHeader.LetraComprobante = loteDocs.RequestHeaders[0].LetraComprobante;
                    thisHeader.SQLID = loteDocs.RequestHeaders[0].SQLID;
                    thisHeader.UltimoIDUsado = "";

                    //Obtengo el último último comprobante
                    wsbfe.BFEResponseLast_CMP bfeLastCMPRespose = afipBFE.BFERecuperaLastCMPRequest(thisHeader.TipoComprobante, thisHeader.PuntoVenta);
                    thisHeader.UltimoNroComprobanteUsado = bfeLastCMPRespose.BFEResult_LastCMP.Cbte_nro.ToString();

                    batchResponse.ResponseHeaders.Add(thisHeader);

                    if (bfeResponseAuthorize.BFEResultAuth.Reproceso.ToLower() == "s")
                    {
                        thisHeader.Resultado = "R";
                        thisHeader.Motivo = "12";
                        thisHeader.MotivoDescripcion = AfipBFE.GetMotivoDescripcion(thisHeader.Motivo);
                    }
                    else
                    {
                        thisHeader.Motivo = bfeResponseAuthorize.BFEResultAuth.Obs;
                        thisHeader.MotivoDescripcion = AfipBFE.GetMotivoDescripcion(bfeResponseAuthorize.BFEResultAuth.Obs);
                    }
                }
                else
                {
                    batchResponse.BatchUniqueId = "0";
                    batchResponse.Resultado = "R";
                    batchResponse.Reproceso = "";
                    batchResponse.CUITInformante = "";
                    batchResponse.CantidadComprobantes = "1";

                    if (bfeResponseAuthorize.BFEErr != null)
                    {
                        batchResponse.CodigoError = bfeResponseAuthorize.BFEErr.ErrCode.ToString();
                        batchResponse.MensajeError = bfeResponseAuthorize.BFEErr.ErrMsg.ToString();
                    }
                    else if (afipConn.ConnectionErrorDescription != string.Empty)
                    {
                        batchResponse.CodigoError = "669";
                        batchResponse.MensajeError = afipConn.ConnectionErrorDescription;
                    }
                    else if (bEquivalenciaError)
                    {
                        batchResponse.Resultado = "E";
                        batchResponse.CodigoError = "Equivalencias";
                        batchResponse.MensajeError = "No se encontró equivalencia con AFIP. Campos: " + strEquivalenciaErrorFields;
                    }

                    //Devolver los documentos originales
                    ResponseHeader thisHeader = new ResponseHeader();
                    thisHeader.CAE = "";
                    thisHeader.NroComprobanteDesde = loteDocs.RequestHeaders[0].NroComprobanteDesde;
                    thisHeader.FechaComprobante = loteDocs.RequestHeaders[0].FechaComprobante;
                    thisHeader.FechaVencimiento = "";
                    thisHeader.Motivo = "";
                    thisHeader.MotivoDescripcion = "";
                    thisHeader.PuntoVenta = loteDocs.RequestHeaders[0].PuntoVenta;
                    thisHeader.TipoComprobante = loteDocs.RequestHeaders[0].TipoComprobante;
                    thisHeader.NroInternoERP = loteDocs.RequestHeaders[0].NroInternoERP;
                    thisHeader.LetraComprobante = loteDocs.RequestHeaders[0].LetraComprobante;
                    thisHeader.SQLID = loteDocs.RequestHeaders[0].SQLID;

                    //Obtengo el último último comprobante
                    if (afipConn.ConnectionErrorDescription == string.Empty)
                    {
                        wsbfe.BFEResponseLast_CMP bfeLastCMPRespose = afipBFE.BFERecuperaLastCMPRequest(thisHeader.TipoComprobante, thisHeader.PuntoVenta);
                        thisHeader.UltimoNroComprobanteUsado = bfeLastCMPRespose.BFEResult_LastCMP.Cbte_nro.ToString();
                    }

                    batchResponse.ResponseHeaders.Add(thisHeader);
                }

                #endregion

                #region Registrar respuesta recibida

                if (batchResponse.Resultado == "A" || batchResponse.Resultado == "R")
                {
                    switch (batchResponse.Resultado)
                    {
                        case "A":
                            estadoDocumento = "Aceptado";
                            cae = batchResponse.ResponseHeaders[0].CAE;
                            FechaVencimiento = batchResponse.ResponseHeaders[0].FechaVencimiento;

                            break;
                        case "R":
                            estadoDocumento = "Rechazado";
                            cae = "";
                            FechaVencimiento = "";
                            break;
                    }
                }
                else if (bfeResponseAuthorize.BFEResultAuth.Reproceso.ToLower() == "s")
                {
                    estadoDocumento = "Rechazado";
                    cae = batchResponse.ResponseHeaders[0].CAE;
                    FechaVencimiento = batchResponse.ResponseHeaders[0].FechaVencimiento;
                }
                else
                {
                    estadoDocumento = "Error";
                    cae = "";
                    FechaVencimiento = "";
                }

                try
                {
                    sqlEngine.LogBatchEnd(SQLID, estadoDocumento, cae, FechaVencimiento);
                }
                catch (Exception ex)
                {
                    sqlEngine.LogError(SQLID, "0", "Respuesta Recibida", "Error: " + ex.Message);
                }
                #endregion
            }
            return batchResponse.GetXMLString();
        }