Example #1
0
 public void Guardar(ComprobanteElectronico ce)
 {
     using (TransactionScope transactionScope = new TransactionScope())
     {
         IoC.GetObjectRepositorio <IComprobanteElectronicoRepositorio>().Insert(ce);
         transactionScope.Complete();
     }
 }
Example #2
0
        private bool Anular()
        {
            try
            {
                IAgencia oAgencia = AgenciaFactory.GetAgencia();
                oAgencia.AgenciaID = this.AgenciaConectadaID;

                IParametroUNegocio param = ParametroUNegocioFactory.GetParametroUNegocio();
                param.Parametro       = NegociosSisPackInterface.SisPack.Parametro.AgenciaDeAdministracion;
                param.UnidadNegocioID = this.UnidadNegocioID;
                param.Consultar();
                int agAdm = Utiles.Validaciones.obtieneEntero(param.ValorString);


                /* Verónica: La anulación la debe realizar la agencia de origen */
                /*SFE: 12/04/2011. Anulacion de Facturas a Terminal desde Administracion. Se agrega la condicion de controlar si la agencia Conectada es Administracion*/
                if (this.AgenciaConectadaID == Convert.ToInt32(this.txtAgenciaID.Text) || this.AgenciaConectadaID == agAdm)
                {
                    bool bOk = false;

                    IFactura factura = FacturaFactory.GetFactura();
                    factura.FacturaID              = Convert.ToInt32(this.txtFacturaID.Text.Trim());
                    factura.TipoGuia               = this.txtTipoGuia.Text.Trim();
                    factura.TipoImpresionGuiaID    = Convert.ToInt32(this.txtTipoImpresionGuiaID.Text.Trim());
                    factura.AgenciaID              = Convert.ToInt32(this.txtAgenciaID.Text.Trim());
                    factura.ClasificacionFacturaID = Convert.ToInt32(this.txtClasificacionFacturaID.Text.Trim());
                    factura.GuiaID                      = Utiles.Validaciones.obtieneEntero(this.txtGuiaID.Text.Trim());
                    factura.FechaEmision                = Convert.ToDateTime(Utiles.Validaciones.obtieneFecha(txtFecha.Text));
                    factura.ObservacionIDGuia           = Convert.ToInt32(Observacion1.ObservacionID);
                    factura.UnidadNegocioID             = UnidadNegocioID;
                    factura.EliminarEstadosConformacion = true;


                    bOk = factura.Anular(this.txtObservaciones.Text, this.usuario, this.AgenciaConectadaID);

                    //esto es porque si se anula la factura y esta factura esta asociada a una guia que es subia a una HRR
                    //se conforma y se agrega una observacion en la HRR
                    if (factura.CambiarHRRFactAnulada(Convert.ToInt32(Utiles.Validaciones.obtieneEntero(this.txtGuiaID.Text.Trim()))) == 1)
                    {
                        factura.CambiaEstadoHRRFactAnulada(Convert.ToInt32(Utiles.Validaciones.obtieneEntero(this.txtGuiaID.Text.Trim())));
                    }

                    //////////**********************************************************************************************************************//////////
                    DsFactura ds;

                    if (this.radioTipo.Checked)
                    {
                        ds = BuscarPorTipo();
                    }
                    else
                    {
                        ds = BuscarPorCodigo();
                    }

                    IClienteEventual ev = ClienteEventualFactory.GetClienteEventual();
                    if (ds.Tables[0].Rows[0]["ClienteEventualID"] != null)
                    {
                        ev.ClienteEventualID = int.Parse(ds.Tables[0].Rows[0]["ClienteEventualID"].ToString());
                        ev.Consultar();
                    }

                    if (ds.Tables[0].Rows[0]["TipoGuia"].ToString() == "A" || ds.Tables[0].Rows[0]["TipoGuia"].ToString() == "B")
                    {
                        try
                        {
                            // La validación del número de guía debe realizarse sólo si el modo de impresión no es "Autoimpresor"
                            IAgencia agencia = AgenciaFactory.GetAgencia();
                            agencia.AgenciaID = this.AgenciaConectadaID;
                            agencia.Consultar();

                            //ws = Session["WSFactElect"] as SisPack.WSFEL.FEService;

                            //if (ws == null)
                            //{
                            //    // create the proxy
                            //    //MyService = new localhost.MyDemo();
                            //    ws = new SisPack.WSFEL.FEService();

                            //    // create a container for the SessionID cookie
                            //    ws.CookieContainer = new CookieContainer();

                            //    ws.Inicializar(long.Parse(CUITExpress), NombreCertificadoExpress, Ambiente);
                            //    //ws.Inicializar(long.Parse(CUITCargas), NombreCertificadoCargas, Ambiente);

                            //    // store it in Session for next usage
                            //    Session["WSFactElect"] = ws;
                            //}
                            string referencia = string.Empty;
                            try
                            {
                                //string sError = string.Empty;
                                //DateTime sVencimientoCae = new DateTime();
                                //Detalles detalle = new Detalles();
                                int    puntoVta        = int.Parse(ds.Tables[0].Rows[0]["NroSucursalGuia"].ToString());
                                int    tipoCbe         = 6;
                                int    tipoDocumento   = 96;
                                long   numeroDocumento = 30414121;
                                int    tipoConcepto    = 2;
                                double totalConcepto   = 0;
                                double neto            = double.Parse(ds.Tables[0].Rows[0]["ImporteNeto"].ToString());
                                double exento          = 0;
                                double totalTributos   = 0;
                                double totalIva        = double.Parse(ds.Tables[0].Rows[0]["ImporteIVA"].ToString());
                                double total           = neto + totalIva;
                                int    tipoIva         = 5;
                                long   nroFactura      = 0;

                                if (ds.Tables[0].Rows[0]["TipoGuia"].ToString() == "A")
                                {
                                    tipoCbe         = 3; //Nota de Crédito A
                                    numeroDocumento = long.Parse(ev.NroCuit);

                                    if ((int)ev.TipoDocumentoCuit == 5)
                                    {
                                        tipoDocumento = 80; //CUIT
                                    }
                                    else if ((int)ev.TipoDocumentoCuit == 6)
                                    {
                                        tipoDocumento = 86; //CUIL
                                    }
                                    else
                                    {
                                        tipoDocumento = 91; //91
                                    }
                                }
                                else
                                {
                                    tipoCbe         = 8; //Nota de Crédito B
                                    numeroDocumento = long.Parse(ev.NroDocumento.ToString());
                                    if ((int)ev.TipoDocumento == 1)
                                    {
                                        tipoDocumento = 96; //DNI
                                    }
                                    else if ((int)ev.TipoDocumento == 2)
                                    {
                                        tipoDocumento = 90; //LC
                                    }
                                    else if ((int)ev.TipoDocumento == 3)
                                    {
                                        tipoDocumento = 89; //LE
                                    }
                                    else if ((int)ev.TipoDocumento == 4)
                                    {
                                        tipoDocumento = 94; //Pasaporte
                                    }
                                    else if ((int)ev.TipoDocumento == 6)
                                    {
                                        tipoDocumento = 86; //CUIL
                                    }
                                    else
                                    {
                                        tipoDocumento = 91; //91
                                    }
                                }

                                if (totalIva == 0)
                                {
                                    tipoIva = 3; //Iva 0
                                }
                                IAFIP  afip    = AFIPFactory.GetAFIP();
                                string fechaFE = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00");
                                //detalle.Ivas.AddIvasRow(tipoIva, neto, totalIva);
                                string NumeroCAE = string.Empty;

                                if (ds.Tables[0].Rows[0]["EmpresaFacturacionID"].ToString() == "1")
                                {
                                    //nroFactura = ws.ObtieneUltimoNroCbte(puntoVta, tipoCbe, long.Parse(CUITExpress)) + 1;

                                    //NumeroCAE = ws.ObtenerCAE(1, 1, puntoVta, tipoCbe, tipoConcepto, tipoDocumento, numeroDocumento, nroFactura, nroFactura, fechaFE,
                                    //                                   total, totalConcepto, neto, exento, totalTributos, totalIva, fechaFE, fechaFE, fechaFE, "PES", 1, detalle, ref sError, ref sVencimientoCae, long.Parse(CUITExpress));
                                    if (afip.ObtieneUltimoNroCbte(puntoVta, tipoCbe, long.Parse(CUITExpress), NombreCertificadoExpress, Ambiente))
                                    {
                                        nroFactura = afip.NumeroFactura;

                                        if (afip.ObtenerCAE(puntoVta, tipoCbe, long.Parse(CUITExpress), NombreCertificadoExpress, Ambiente, tipoConcepto, tipoDocumento, numeroDocumento, nroFactura, fechaFE, total, totalConcepto, neto, exento, totalTributos,
                                                            totalIva, tipoIva))
                                        {
                                            NumeroCAE = afip.NumeroCAE;
                                        }
                                    }
                                }
                                else
                                {
                                    //nroFactura = ws.ObtieneUltimoNroCbte(puntoVta, tipoCbe, long.Parse(CUITCargas)) + 1;

                                    //NumeroCAE = ws.ObtenerCAE(1, 1, puntoVta, tipoCbe, tipoConcepto, tipoDocumento, numeroDocumento, nroFactura, nroFactura, fechaFE,
                                    //                                   total, totalConcepto, neto, exento, totalTributos, totalIva, fechaFE, fechaFE, fechaFE, "PES", 1, detalle, ref sError, ref sVencimientoCae, long.Parse(CUITCargas));
                                }

                                //if (sError.Contains("10016-El número o fecha del comprobante no se corresponde con el próximo a autorizar."))
                                if (afip.errorCAE.Contains("10016-El número o fecha del comprobante no se corresponde con el próximo a autorizar."))
                                {
                                    //verifico nuevamente por si alguien estaba usando ese numero
                                    if (ds.Tables[0].Rows[0]["EmpresaFacturacionID"].ToString() == "1")
                                    {
                                        //nroFactura = ws.ObtieneUltimoNroCbte(puntoVta, tipoCbe, long.Parse(CUITExpress)) + 1;

                                        //NumeroCAE = ws.ObtenerCAE(1, 1, puntoVta, tipoCbe, tipoConcepto, tipoDocumento, numeroDocumento, nroFactura, nroFactura, fechaFE,
                                        //                                   total, totalConcepto, neto, exento, totalTributos, totalIva, fechaFE, fechaFE, fechaFE, "PES", 1, detalle, ref sError, ref sVencimientoCae, long.Parse(CUITExpress));
                                        if (afip.ObtieneUltimoNroCbte(puntoVta, tipoCbe, long.Parse(CUITExpress), NombreCertificadoExpress, Ambiente))
                                        {
                                            nroFactura = afip.NumeroFactura;

                                            if (afip.ObtenerCAE(puntoVta, tipoCbe, long.Parse(CUITExpress), NombreCertificadoExpress, Ambiente, tipoConcepto, tipoDocumento, numeroDocumento, nroFactura, fechaFE, total, totalConcepto, neto, exento, totalTributos,
                                                                totalIva, tipoIva))
                                            {
                                                NumeroCAE = afip.NumeroCAE;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        //nroFactura = ws.ObtieneUltimoNroCbte(puntoVta, tipoCbe, long.Parse(CUITCargas)) + 1;

                                        //NumeroCAE = ws.ObtenerCAE(1, 1, puntoVta, tipoCbe, tipoConcepto, tipoDocumento, numeroDocumento, nroFactura, nroFactura, fechaFE,
                                        //                                   total, totalConcepto, neto, exento, totalTributos, totalIva, fechaFE, fechaFE, fechaFE, "PES", 1, detalle, ref sError, ref sVencimientoCae, long.Parse(CUITCargas));
                                    }
                                }
                                else if (afip.errorCAE != string.Empty)
                                {
                                    using (System.IO.StreamWriter archivo = System.IO.File.AppendText(@"C:\log\LogFactura.log"))
                                    {
                                        archivo.WriteLine("AnularFactura.aspx.cs linea 753-------------------------------------------------------------------------------------");
                                        archivo.WriteLine("--------------------------------------------------------------------------------------");
                                    }
                                }

                                if (NumeroCAE != null && NumeroCAE != string.Empty)
                                {
                                    ComprobanteElectronico ce = new ComprobanteElectronico();
                                    ce.PuntoVenta          = puntoVta.ToString();
                                    ce.TipoComprobanteFEID = tipoCbe;
                                    ce.TipoConceptoFEID    = tipoConcepto;
                                    ce.TipoDocumentoFEID   = tipoDocumento;
                                    ce.NumeroDocumento     = numeroDocumento.ToString();
                                    ce.NumeroFactura       = nroFactura.ToString();
                                    ce.FechaFE             = DateTime.Now;
                                    ce.Total                = total;
                                    ce.TotalConcepto        = totalConcepto;
                                    ce.Neto                 = neto;
                                    ce.Exento               = exento;
                                    ce.TotalTributos        = totalTributos;
                                    ce.TotalIVA             = totalIva;
                                    ce.FechaVencimientoCae  = afip.fechaCAE;
                                    ce.NumeroCae            = NumeroCAE;
                                    ce.EmpresaFacturacionID = int.Parse(ds.Tables[0].Rows[0]["EmpresaFacturacionID"].ToString());
                                    IoC.GetObjectDominio <IComprobanteElectronicoDominio>().Guardar(ce);
                                }

                                //datet.FechaCae = sVencimientoCae;
                                //admGuiaO.FechaVencimientoCAI = sVencimientoCae;
                                //admGuiaO.NroGuia = int.Parse(nroFactura.ToString());
                            }
                            catch (Exception ex)
                            {
                                using (System.IO.StreamWriter archivo = System.IO.File.AppendText(@"C:\log\LogFactura.log"))
                                {
                                    archivo.WriteLine("AnularFactura.aspx.cs linea 786-------------------------------------------------------------------------------------");
                                    archivo.WriteLine(ex.Message);
                                    archivo.WriteLine("--------------------------------------------------------------------------------------");
                                }
                                ex = new Exception("Error al crear la nota de credito para asociar a la factura. " + ex.Message);
                                throw ex;
                            }

                            //Session["AdmGuia"] = admGuiaO;

                            //this.txtNroGuia.Text = admGuiaO.NroGuia.ToString();
                            // GuardarFactura(agencia.TipoImpresionGuiaId, (int)NegociosSisPackInterface.SisPack.TipoValidacionEmision.CAE);
                        }
                        catch (Exception ex)
                        {
                            oAgencia = null;
                            ex       = new Exception("Error al crear la nota de credito para asociar a la factura. No se puede conectar con el WS de la AFIP" + ex.Message);
                            throw ex;
                        }
                    }

                    /////////************************************************************************************************************************//////
                    factura  = null;
                    oAgencia = null;
                    return(bOk);
                }
                else
                {
                    oAgencia = null;
                    Exception ex = new Exception("Solo la agencia emisora puede anular la factura.");
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }