public void ImprimeNotaCredito(Factura documento)
        {
            if (documento == null)
            {
                throw new Exception("Documento en blanco no se puede imprimir");
            }
            unsafe
            {
                try
                {
                    string sCmd;
                    bool   bRet = false;
                    System.Threading.Thread.Sleep(500);
                    fiscal.SendCmd("i01Cedula/Rif:" + documento.CedulaRif);
                    fiscal.SendCmd("i02Razon Social:");
                    fiscal.SendCmd("i03" + documento.RazonSocial);
                    fiscal.SendCmd("i04Direccion:");
                    if (documento.Direccion != null)
                    {
                        if (documento.Direccion.Length > 40)
                        {
                            fiscal.SendCmd("i05" + documento.Direccion);
                            fiscal.SendCmd("i06" + documento.Direccion.Substring(40, documento.Direccion.Length - 40));
                        }
                        else
                        {
                            fiscal.SendCmd("i06" + documento.Direccion);
                        }
                    }
                    fiscal.SendCmd("i07 # FACTURA AFECTADA:" + documento.Numero);
                    // Agrego el servicio en la ultima fila
                    // DS.ImpresionTicket.AddImpresionTicketRow(1, 1, 1, 1, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, 1, "", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, "SERVICIO 10%", 1, documento[0].MontoServicio, 0, "", "", "");
                    double SubTotal = 0;
                    double MontoIva = 0;
                    System.Threading.Thread.Sleep(500);
                    foreach (var d in documento.FacturasPlatos.ToList())
                    {
                        sCmd = "d1"; // Default parametros.TasaIva
                        if (d.TasaIva == 0)
                        {
                            sCmd = "d0";
                        }
                        if (d.TasaIva == parametro.TasaIvaB)
                        {
                            sCmd = "d2";
                        }
                        if (d.TasaIva == parametro.TasaIvaC)
                        {
                            sCmd = "d3";
                        }
                        SubTotal += ((double)d.Cantidad * (double)d.Precio.GetValueOrDefault(0));
                        MontoIva += ((double)d.Cantidad * ((double)d.PrecioConIva.GetValueOrDefault(0)) - (double)d.Precio.GetValueOrDefault(0));
                        string Precio = "0000000000";
                        if (parametro.TipoIva == "INCLUIDO")
                        {
                            Precio = (d.PrecioConIva.GetValueOrDefault(0) * 100).ToString("0000000000");
                        }
                        else
                        {
                            Precio = (d.Precio.GetValueOrDefault(0) * 100).ToString("0000000000");
                        }
                        string Cantidad    = ((int)(d.Cantidad * 1000)).ToString("00000000");
                        string Descripcion = d.Descripcion.PadRight(37);
                        if (d.Descripcion.Length <= 37)
                        {
                            bRet = fiscal.SendCmd(sCmd + Precio + Cantidad + d.Descripcion);
                            if (!bRet)
                            {
                                TfhkaNet.IF.PrinterStatus e = fiscal.GetPrinterStatus();
                                // OK.ManejarException(new Exception(string.Format("Estatus:{0},Error:{1}", e.PrinterStatusDescription, e.PrinterErrorDescription)));
                            }
                        }
                        else
                        {
                            bRet = fiscal.SendCmd(sCmd + Precio + Cantidad + Descripcion.Substring(0, 36));
                            if (!bRet)
                            {
                                TfhkaNet.IF.PrinterStatus e = fiscal.GetPrinterStatus();
                                // OK.ManejarException(new Exception(string.Format("Estatus:{0},Error:{1}", e.PrinterStatusDescription, e.PrinterErrorDescription)));
                            }
                        }
                    }
                    System.Threading.Thread.Sleep(500);
                    //if (documento.DescuentoBs != 0)
                    //{

                    //    documento.DescuentoBs = documento.MontoTotal - SubTotal - MontoIva;
                    //    documento.DescuentoBs = documento.DescuentoBs * -1;
                    //    documento.DescuentoPorcentaje = (documento.DescuentoBs * 100) / (SubTotal + MontoIva);
                    //    bRet = fiscal.SendCmd( "3");
                    //    string DescuentoPorcentaje = ((double)documento.DescuentoPorcentaje * 100).ToString("0000");
                    //    bRet = fiscal.SendCmd( "p-" + DescuentoPorcentaje);
                    //}
                    //Pagos
                    double TotalPagos = 0;
                    //if (documento.MontoServicio.GetValueOrDefault(0) > 0)
                    //{
                    //    sCmd = "d0";
                    //    string Precio = (documento.MontoServicio.GetValueOrDefault(0) * 100).ToString("0000000000");
                    //    bRet = fiscal.SendCmd( sCmd + Precio + "00001000" + "SERVICIO 10%");
                    //}
                    // Pago pago = pago; // != null ? pagos : new Pago();
                    if (documento.Efectivo.GetValueOrDefault(0) != 0)
                    {
                        double x = documento.Efectivo.GetValueOrDefault(0) + documento.Cambio.GetValueOrDefault(0);
                        sCmd        = "f01" + (x * 100).ToString("000000000000");
                        bRet        = fiscal.SendCmd(sCmd);
                        TotalPagos += documento.Efectivo.Value;
                    }
                    if (documento.CestaTicket.GetValueOrDefault(0) != 0)
                    {
                        sCmd        = "f02" + ((double)documento.CestaTicket * 100).ToString("000000000000");
                        bRet        = fiscal.SendCmd(sCmd);
                        TotalPagos += documento.CestaTicket.Value;
                    }

                    if (documento.Cheque.GetValueOrDefault(0) != 0)
                    {
                        sCmd        = "f05" + ((double)documento.Cheque * 100).ToString("000000000000");
                        bRet        = fiscal.SendCmd(sCmd);
                        TotalPagos += documento.Cheque.Value;
                    }
                    if (documento.Tarjeta.GetValueOrDefault(0) != 0)
                    {
                        sCmd        = "f09" + ((double)documento.Tarjeta * 100).ToString("000000000000");
                        bRet        = fiscal.SendCmd(sCmd);
                        TotalPagos += documento.Tarjeta.Value;
                    }
                    if (documento.MontoTotal.GetValueOrDefault(0) > TotalPagos)
                    {
                        sCmd = "f16" + ((double)documento.MontoTotal - (double)TotalPagos * 100).ToString("000000000000");
                        bRet = fiscal.SendCmd(sCmd);
                    }
                    CargarX();
                    CargarS1(false);
                    System.Threading.Thread.Sleep(500);
                    documento.NumeroZ       = ultimoZ.Value.ToString("0000");
                    documento.MaquinaFiscal = NumeroRegistro;
                    documento.Numero        = ultimaDevolucion;
                }
                catch (TfhkaNet.IF.PrinterException x)
                {
                    throw new Exception(string.Format("Error de impresora: {0}, estatus {1}", x.Message, fiscal.Estado));
                }
                catch (Exception x)
                {
                    throw new Exception(string.Format("Error de conexión\n{0}\nEstatus {1}", x.Message, fiscal.Status_Error));
                }
            }
        }
        public void ImprimeFactura(Factura documento)
        {
            try
            {
                if (documento == null)
                {
                    throw new Exception("Documento en blanco no se puede imprimir");
                }
                if (documento.MontoTotal <= 0)
                {
                    throw new Exception("Esta factura no tiene productos");
                }
                if (fiscal.Estado != "OK")
                {
                    throw new Exception(string.Format("Error en impresora Fiscal Error {0}, Estado {1} ", fiscal.Status_Error, fiscal.StatusPort));
                }
                double SubTotal = 0;
                double MontoIva = 0;
                System.Threading.Thread.Sleep(500);
                fiscal.SendCmd("i01Cedula/Rif:" + documento.CedulaRif);
                fiscal.SendCmd("i02Razon Social:");
                if (documento.RazonSocial.Length <= 37)
                {
                    fiscal.SendCmd("i03" + documento.RazonSocial);
                }
                else
                {
                    fiscal.SendCmd("i03" + documento.RazonSocial.Substring(0, 36));
                    fiscal.SendCmd("i04" + documento.RazonSocial.Substring(36, (documento.RazonSocial.Length - 36)));
                }
                if (documento.CedulaRif == "V000000000")
                {
                    fiscal.SendCmd("i04 SIN DERECHO A CREDITO FISCAL");
                }
                if (documento.Direccion != null)
                {
                    if (documento.Direccion.Length > 40)
                    {
                        fiscal.SendCmd("i05" + documento.Direccion);
                        fiscal.SendCmd("i06" + documento.Direccion.Substring(40, documento.Direccion.Length - 40));
                    }
                    else
                    {
                        fiscal.SendCmd("i06" + documento.Direccion);
                    }
                }
                if (!string.IsNullOrEmpty(documento.NumeroOrden))
                {
                    fiscal.SendCmd(string.Format("i07       MESA:{0}", documento.NumeroOrden));
                }
                var Acumulado = from p in documento.FacturasPlatos.Where(x => x.Cantidad.GetValueOrDefault(0) >= 0.5)
                                group p by new { p.Descripcion, p.TasaIva, p.Precio, p.PrecioConIva }
                into itemResumido
                    select new
                {
                    Descripcion  = itemResumido.Key.Descripcion,
                    TasaIva      = itemResumido.Key.TasaIva,
                    Cantidad     = itemResumido.Sum(x => x.Cantidad),
                    Precio       = itemResumido.Key.Precio,
                    PrecioConIva = itemResumido.Key.PrecioConIva
                };
                System.Threading.Thread.Sleep(500);
                foreach (var d in Acumulado.Where(x => x.Cantidad.GetValueOrDefault() > 0 && x.Precio.GetValueOrDefault() > 0))
                {
                    var sCmd = "!";     // Default parametros.TasaIva
                    if (d.TasaIva == 0)
                    {
                        sCmd = " ";
                    }
                    if (d.TasaIva == parametro.TasaIvaB)
                    {
                        sCmd = '"'.ToString();
                    }
                    if (d.TasaIva == parametro.TasaIvaC)
                    {
                        sCmd = '#'.ToString();
                    }
                    SubTotal  = ((double)d.Cantidad.GetValueOrDefault(0) * (double)d.PrecioConIva.GetValueOrDefault(0));
                    MontoIva += ((double)d.Cantidad.GetValueOrDefault(0) * ((double)d.PrecioConIva.GetValueOrDefault(0)) - (double)d.Precio.GetValueOrDefault(0));
                    string Precio = "0000000000";
                    if (parametro.TipoIva == "INCLUIDO")
                    {
                        Precio = (d.PrecioConIva.GetValueOrDefault(0) * 100).ToString("0000000000");
                    }
                    else
                    {
                        Precio = (d.Precio.GetValueOrDefault(0) * 100).ToString("0000000000");
                    }
                    string Cantidad    = (d.Cantidad.GetValueOrDefault(0) * 1000).ToString("00000000");
                    string Descripcion = d.Descripcion.PadRight(37);
                    if (d.Descripcion.Length <= 37)
                    {
                        bRet = fiscal.SendCmd(sCmd + Precio + Cantidad + d.Descripcion);
                        if (!bRet)
                        {
                            TfhkaNet.IF.PrinterStatus e = fiscal.GetPrinterStatus();
                            // OK.ManejarException(new Exception( string.Format("Estatus:{0},Error:{1}",e.PrinterStatusDescription,e.PrinterErrorDescription)));
                        }
                    }
                    else
                    {
                        bRet = fiscal.SendCmd(sCmd + Precio + Cantidad + Descripcion.Substring(0, 36));
                        string Descripcion2 = d.Descripcion.Substring(36, (d.Descripcion.Length - 36));
                        bRet = fiscal.SendCmd("@" + Descripcion2);
                        if (!bRet)
                        {
                            TfhkaNet.IF.PrinterStatus e = fiscal.GetPrinterStatus();
                            // OK.ManejarException(new Exception( string.Format("Estatus:{0},Error:{1}",e.PrinterStatusDescription,e.PrinterErrorDescription)));
                        }
                    }
                }
                //if (documento.DescuentoBs.GetValueOrDefault(0) != 0)
                //{

                //    documento.DescuentoBs = documento.MontoTotal.GetValueOrDefault(0) - SubTotal - MontoIva;
                //    documento.DescuentoBs = documento.DescuentoBs.GetValueOrDefault(0) * -1;
                //    documento.DescuentoPorcentaje = (documento.DescuentoBs.GetValueOrDefault(0) * 100) / (SubTotal + MontoIva);
                //    bRet = fiscal.SendCmd( "3");
                //    string DescuentoPorcentaje = (documento.DescuentoPorcentaje.GetValueOrDefault(0) * 100).ToString("0000");
                //    bRet = fiscal.SendCmd( "p-" + DescuentoPorcentaje);
                //}
                //Pagos
                // CargarS2();

                //if (documento.MontoServicio.GetValueOrDefault(0) > 0)
                //{
                //    sCmd =  " ";
                //    string Precio = (documento.MontoServicio.GetValueOrDefault(0) * 100).ToString("0000000000");
                //    bRet = fiscal.SendCmd( sCmd + Precio + "00001000" + "SERVICIO 10%");
                //}
                System.Threading.Thread.Sleep(500);
                double TotalPagos = 0;
                if (documento.Efectivo.GetValueOrDefault(0) != 0)
                {
                    double x = documento.Efectivo.GetValueOrDefault(0) + documento.Cambio.GetValueOrDefault(0);
                    fiscal.SendCmd("201" + (x * 100).ToString("000000000000"));
                    TotalPagos += documento.Efectivo.Value;
                }
                if (documento.CestaTicket.GetValueOrDefault(0) != 0)
                {
                    fiscal.SendCmd("202" + ((double)documento.CestaTicket * 100).ToString("000000000000"));
                    TotalPagos += documento.CestaTicket.Value;
                }
                //
                if (documento.Cheque.GetValueOrDefault(0) != 0)
                {
                    fiscal.SendCmd("205" + ((double)documento.Cheque * 100).ToString("000000000000"));
                    TotalPagos += documento.Cheque.Value;
                }
                if (documento.Tarjeta.GetValueOrDefault(0) != 0)
                {
                    fiscal.SendCmd("209" + ((double)documento.Tarjeta * 100).ToString("000000000000"));
                    TotalPagos += documento.Tarjeta.Value;
                }
                if (documento.Credito.GetValueOrDefault(0) != 0)
                {
                    fiscal.SendCmd("216" + ((double)documento.Credito * 100).ToString("000000000000"));
                    TotalPagos += documento.Credito.Value;
                }
                System.Threading.Thread.Sleep(1000);
                CargarS2();
                if (MontoPorPagar > 0)
                {
                    fiscal.SendCmd("115" + (MontoPorPagar.Value * 100).ToString("000000000000"));
                }
                System.Threading.Thread.Sleep(1000);
                CargarS1(false);
                documento.Fecha  = Fecha;
                documento.Hora   = DateTime.Now;
                documento.Numero = NumeroFactura;
                //   documento.MontoGravable = SubTotalBases;
                //   documento.MontoIva = MontoIva;
                //   documento.MontoTotal = SubTotalBases + MontoIva;
                documento.NumeroZ       = (ultimoZ.Value).ToString("0000");
                documento.MaquinaFiscal = NumeroRegistro;
            }
            catch (TfhkaNet.IF.PrinterException x)
            {
                throw new Exception(string.Format("Error de impresora: {0}, estatus {1}", x.Message, fiscal.Estado));
            }
            catch (Exception x)
            {
                throw new Exception(string.Format("Error de conexión\n{0}\nEstatus {1}", x.Message, fiscal.Status_Error));
            }
        }