Example #1
0
        protected void btnEmitir_Click(object sender, EventArgs e)
        {
            try
            {
                // Valido los datos
                if (ValidarEmision())
                {
                    // Cargo los datos a guardar
                    IFactura factura = FacturaFactory.GetFactura();
                    factura.GuiaID                 = GuiaID;
                    factura.ClienteID              = ClienteFacturacion1.ClienteCorporativoID ?? 0;
                    factura.ClienteEventualID      = ClienteFacturacion1.ClienteEventualID ?? 0;
                    factura.AgenciaID              = AgenciaConectadaID;
                    factura.UnidadNegocioID        = UnidadNegocioID;
                    factura.CondicionIVAAgentePago = ClienteFacturacion1.CondicionIVA ?? 0;
                    factura.NroCUITAgentePago      = ClienteFacturacion1.NroCUIT;

                    if (rbtContado.Checked)
                    {
                        factura.CondicionPagoID = (Int32)NegociosSisPackInterface.SisPack.CondicionPago.Contado;
                    }
                    else if (rbtCtaCte.Checked)
                    {
                        factura.CondicionPagoID = (Int32)NegociosSisPackInterface.SisPack.CondicionPago.CuentaCorriente;
                    }
                    else if (rbtPagoEnDestino.Checked)
                    {
                        factura.CondicionPagoID = (Int32)NegociosSisPackInterface.SisPack.CondicionPago.PagoEnDestino;
                    }
                    else
                    {
                        factura.CondicionPagoID = (Int32)NegociosSisPackInterface.SisPack.CondicionPago.TarjCredDeb;
                    }
                    factura.UsuarioIDAlta   = usuario.UsuarioID;
                    factura.ClienteEventual = ClienteFacturacion1.ClienteEventual;

                    if (rbtTarjetaDeCredito.Checked)
                    {
                        Int32 _auxTarjeta;
                        if (Int32.TryParse(txtNroLote.Text, out _auxTarjeta))
                        {
                            factura.NroLoteTarjeta = _auxTarjeta;
                        }
                        if (Int32.TryParse(txtNroCupon.Text, out _auxTarjeta))
                        {
                            factura.NroCuponTarjeta = _auxTarjeta;
                        }
                    }

                    if (txtObservaciones.Text.Length > 500)
                    {
                        txtObservaciones.Text = txtObservaciones.Text.Substring(0, 500);
                    }
                    factura.Observaciones      = txtObservaciones.Text;
                    factura.NroRemitoCliente   = txtNroRemitoCliente.Text.Trim();
                    factura.NroFacturaCliente  = txtNroFacturaCliente.Text.Trim();
                    factura.NroHojaRutaCliente = txtNroHojaRutaCliente.Text.Trim();

                    IFacturaDetalle detalle = FacturaDetalleFactory.GetFacturaDetalle();
                    detalle.ConceptoFacturacionID = Convert.ToInt32(ddlConceptoFacturacion.SelectedValue);
                    if (txtTope.Text != "")
                    {
                        detalle.Tope = Convert.ToDouble(txtTope.Text);//.Replace(".",","));
                    }
                    if (txtCodigoBarras.Text != "")
                    {
                        detalle.CodigoBarras = txtCodigoBarras.Text.Trim();//.Replace(".",","));
                    }
                    factura.DetalleFactura.Add(detalle);

                    factura.ValidarCredito();
                    factura.ValidarCodigosDeBarra();


                    string _tipoGuia = factura.DeterminarTipoFacturaConceptoFacturacion();
                    factura.TipoGuia = _tipoGuia;
                    // Reviso si tengo que determinar la empresa de facturación

                    if (_tipoGuia == "A" || _tipoGuia == "B")
                    {
                        try
                        {
                            int _empresaFacturacionID = factura.DeterminarEmpresaFacturacionConceptoFacturacion();
                            factura.EmpresaFacturacionID = _empresaFacturacionID;

                            factura.CargarInfoComprobante();

                            factura.CalcularImporteConceptoFacturacion();

                            /*****************************************************************/
                            //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();

                            //    // store it in Session for next usage
                            //    Session["WSFactElect"] = ws;

                            //    ws.Inicializar(long.Parse(CUITExpress), NombreCertificadoExpress, Ambiente);
                            //   // ws.Inicializar(long.Parse(CUITCargas), NombreCertificadoCargas, Ambiente);
                            //}
                            IAFIP  afip       = AFIPFactory.GetAFIP();
                            string referencia = string.Empty;

                            //string sError = string.Empty;
                            //DateTime sVencimientoCae = new DateTime();
                            // Detalles detalleFE = new Detalles();
                            int    puntoVta        = int.Parse(factura.NroSucursalGuia);
                            int    tipoCbe         = 6;
                            int    tipoDocumento   = 96;
                            long   numeroDocumento = 30414121;
                            int    tipoConcepto    = 2;
                            double total           = factura.ImporteTotal;
                            double totalConcepto   = 0;
                            double neto            = factura.ImporteNeto;
                            double exento          = 0;
                            double totalTributos   = 0;
                            double totalIva        = factura.ImporteIVA;
                            int    tipoIva         = 5;
                            long   nroFactura      = 0;

                            if (factura.TipoGuia == "A")
                            {
                                tipoCbe         = 1; //Factura A
                                numeroDocumento = long.Parse(factura.ClienteEventual.NroCuit);

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

                            string fechaFE = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00");

                            if (totalIva == 0)
                            {
                                tipoIva = 3; //Iva 0
                            }
                            //detalleFE.Ivas.AddIvasRow(tipoIva, neto, totalIva);

                            if (factura.EmpresaFacturacionID == 1)
                            {
                                //nroFactura = ws.ObtieneUltimoNroCbte(puntoVta, tipoCbe, long.Parse(CUITExpress)) + 1;

                                //factura.NumeroCAE = ws.ObtenerCAE(1, 1, puntoVta, tipoCbe, tipoConcepto, tipoDocumento, numeroDocumento, nroFactura, nroFactura, fechaFE,
                                //                                   total, totalConcepto, neto, exento, totalTributos, totalIva, fechaFE, fechaFE, fechaFE, "PES", 1, detalleFE, 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))
                                    {
                                        factura.NumeroCAE = afip.NumeroCAE;
                                    }
                                }
                            }
                            else
                            {
                                //nroFactura = ws.ObtieneUltimoNroCbte(puntoVta, tipoCbe, long.Parse(CUITCargas)) + 1;

                                //factura.NumeroCAE = ws.ObtenerCAE(1, 1, puntoVta, tipoCbe, tipoConcepto, tipoDocumento, numeroDocumento, nroFactura, nroFactura, fechaFE,
                                //                                   total, totalConcepto, neto, exento, totalTributos, totalIva, fechaFE, fechaFE, fechaFE, "PES", 1, detalleFE, 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 (factura.EmpresaFacturacionID == 1)
                                {
                                    //nroFactura = ws.ObtieneUltimoNroCbte(puntoVta, tipoCbe, long.Parse(CUITExpress)) + 1;

                                    //factura.NumeroCAE = ws.ObtenerCAE(1, 1, puntoVta, tipoCbe, tipoConcepto, tipoDocumento, numeroDocumento, nroFactura, nroFactura, fechaFE,
                                    //                                   total, totalConcepto, neto, exento, totalTributos, totalIva, fechaFE, fechaFE, fechaFE, "PES", 1, detalleFE, 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))
                                        {
                                            factura.NumeroCAE = afip.NumeroCAE;
                                        }
                                    }
                                }
                                else
                                {
                                    //nroFactura = ws.ObtieneUltimoNroCbte(puntoVta, tipoCbe, long.Parse(CUITCargas)) + 1;

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

                                if (afip.errorCAE != string.Empty)
                                {
                                    using (System.IO.StreamWriter archivo = System.IO.File.AppendText(@"C:\log\LogEmisionConcepto.log"))
                                    {
                                        archivo.WriteLine("EmisionConceptoFacturacion.aspx.cs linea 815-------------------------------------------------------------------------------------");
                                        archivo.WriteLine("--------------------------------------------------------------------------------------");
                                    }
                                    factura.EmitirFacturaConceptoFacturacion(null);
                                }
                                else
                                {
                                    //factura.FechaCae = sVencimientoCae;
                                    factura.FechaCae = afip.fechaCAE;
                                    factura.NroGuia  = int.Parse(nroFactura.ToString());
                                    factura.EmitirFacturaConceptoFacturacionFE(null);
                                }
                            }
                            else if (afip.errorCAE != string.Empty || (afip.errorCAE == string.Empty && factura.NumeroCAE == string.Empty))
                            {
                                using (System.IO.StreamWriter archivo = System.IO.File.AppendText(@"C:\log\LogEmisionConcepto.log"))
                                {
                                    archivo.WriteLine("EmisionConceptoFacturacion.aspx.cs linea 832-------------------------------------------------------------------------------------");
                                    archivo.WriteLine("--------------------------------------------------------------------------------------");
                                }
                                //Paso a la modalidad autoimpresor
                                factura.EmitirFacturaConceptoFacturacion(null);
                            }
                            else
                            {
                                //factura.FechaCae = sVencimientoCae;
                                factura.FechaCae = afip.fechaCAE;
                                factura.NroGuia  = int.Parse(nroFactura.ToString());
                                factura.EmitirFacturaConceptoFacturacionFE(null);
                            }
                        }
                        catch (Exception ex)
                        {
                            using (System.IO.StreamWriter archivo = System.IO.File.AppendText(@"C:\log\LogEmisionConcepto.log"))
                            {
                                archivo.WriteLine("EmisionConceptoFacturacion.aspx.cs linea 850-------------------------------------------------------------------------------------");
                                archivo.WriteLine("--------------------------------------------------------------------------------------");
                            }
                            factura.EmitirFacturaConceptoFacturacion(null);
                        }
                        /*****************************************************************/
                    }
                    else
                    {
                        factura.EmitirFacturaConceptoFacturacion(null);
                    }

                    // Cargo los datos para imprimir
                    IAgencia agencia = AgenciaFactory.GetAgencia();


                    txtPaginaImpresion.Value = agencia.GetPaginaImpresion(factura.TipoImpresionGuiaID, Implementacion);

                    txtFacturaID.Value = factura.FacturaID.ToString();

                    //if (factura.ImportePercepcion != 0)
                    //{
                    //    int facturaid = factura.FacturaID;
                    //    //guardar en una tabla nueva la guiaid, percepcionid, percpecion importe.
                    //    //parte de inserrtar el origen
                    //    IPercepcionDominio _perDominio = IoC.GetObjectDominio<IPercepcionDominio>();
                    //    List<Dominio.Entidades.GetOnePercepcionDatos> lstPerOrigen = _perDominio.PercepcionGetAllBYAgOri(this.AgenciaConectadaID);
                    //    if (lstPerOrigen.Count > 0)
                    //    {
                    //        _perDominio.InsertPercepcionFactura(lstPerOrigen[0].PercepcionID, facturaid, Convert.ToDouble(lstPerOrigen[0].PorcentajeImporte), "Origen", factura.ImportePercepcion);

                    //    }

                    //}

                    //guardar el mail
                    if (txtFacturaID.Value != "0")
                    {
                        this.GuardarMail(Convert.ToInt32(txtFacturaID.Value));
                    }


                    Session["DatosGuia"] = ToDsGuiaFacturaImpresion(factura.GetFacturaConceptoImpresionByFacturaID());
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("Datos inválidos:"))
                {
                    throw ex;
                }
                else
                {
                    throw new Exception("Error al guardar factura");
                }
            }
        }
Example #2
0
        protected void btnCalcularImporte_Click(object sender, EventArgs e)
        {
            Page.Validate("CalcularImportes");
            if (Page.IsValid)
            {
                IFactura factura = FacturaFactory.GetFactura();

                // Calculo el importe de la factura
                factura.UnidadNegocioID = UnidadNegocioID;
                IFacturaDetalle detalle = FacturaDetalleFactory.GetFacturaDetalle();
                detalle.ConceptoFacturacionID = Convert.ToInt32(ddlConceptoFacturacion.SelectedValue);
                if (txtTope.Text != "")
                {
                    //Diego 31/03/2014  Modificado para que se pueda poner decimales en los m3
                    //detalle.Tope = Convert.ToInt32(txtTope.Text);
                    detalle.Tope = Convert.ToDouble(txtTope.Text);//.Replace(".",","));
                }

                if (txtCodigoBarras.Text != "")
                {
                    detalle.CodigoBarras = txtCodigoBarras.Text.Trim();//.Replace(".",","));
                }

                factura.DetalleFactura.Add(detalle);
                factura.AgenciaID = AgenciaConectadaID;
                factura.CalcularImporteConceptoFacturacion();

                // Determino el tipo de factura que se va a emitir con los datos ingresados
                factura.CondicionIVAAgentePago = ClienteFacturacion1.CondicionIVA ?? 0;
                if (rbtContado.Checked)
                {
                    factura.CondicionPagoID = (Int32)NegociosSisPackInterface.SisPack.CondicionPago.Contado;
                }
                else if (rbtCtaCte.Checked)
                {
                    factura.CondicionPagoID = (Int32)NegociosSisPackInterface.SisPack.CondicionPago.CuentaCorriente;
                }
                else if (rbtPagoEnDestino.Checked)
                {
                    factura.CondicionPagoID = (Int32)NegociosSisPackInterface.SisPack.CondicionPago.PagoEnDestino;
                }
                else
                {
                    factura.CondicionPagoID = (Int32)NegociosSisPackInterface.SisPack.CondicionPago.TarjCredDeb;
                }

                // Muestro los importes
                if (factura.DeterminarTipoFacturaConceptoFacturacion() == "A")
                {
                    lblNeto.Text  = factura.ImporteNeto.ToString("#,###0.00");
                    lblIVA.Text   = factura.ImporteIVA.ToString("#,###0.00");
                    lblTotal.Text = factura.ImporteTotal.ToString("#,###0.00");
                    //PERCEPCION
                    //  lblPercepcion.Text = factura.ImportePercepcion.ToString("#,###0.00");
                }
                else
                {
                    lblNeto.Text = factura.ImporteTotal.ToString("#,###0.00");
                    lblIVA.Text  = "";
                    //PERCEPCION
                    // lblPercepcion.Text = factura.ImportePercepcion.ToString("#,###0.00");
                    lblTotal.Text = factura.ImporteTotal.ToString("#,###0.00");
                }
            }
        }