Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 120.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using MetroFramework;
  8. using MetroFramework.Drawing;
  9. using MetroFramework.Forms;
  10. using System.Windows.Forms;
  11. using System.Drawing;
  12. using System.IO;
  13. using Microsoft.Office.Interop.Excel;
  14. using System.Reflection;
  15. using System.Drawing.Printing;
  16. using System.Security.Cryptography.X509Certificates;
  17. using Universidad_ITEC.Clases;
  18. using System.Net.Mail;
  19. using System.Net.Mime;
  20. using System.Net;
  21. using MultiFacturas.PAC;
  22. using System.Net.NetworkInformation;
  23. using System.Net.Security;
  24. using Universidad_ITEC.Utils;
  25. namespace Universidad_ITEC
  26. {
  27.     public static class Global
  28.     {
  29.         public static class MENSAJES
  30.         {
  31.             public static string EJECUTANDO = "WM_EJECUTANDO";
  32.         }
  33.         public static readonly string STR_NOMBRE_SISTEMA = "Universidad itec";
  34.         public static readonly string STR_MYSQL_SERVER = Properties.Settings.Default.host;//"127.0.0.1";
  35.         public static readonly string STR_MYSQL_USERNAME = Properties.Settings.Default.user;//"root";
  36.         public static readonly string STR_MYSQL_PASSWORD = Properties.Settings.Default.password;//"s3cr3t";/*"mysql";*/
  37.         public static readonly string STR_MYSQL_DATABASE = Properties.Settings.Default.database;//"itec";
  38.         public const int DOWN = 0;
  39.         public const int UP = 1;
  40.         public const int HOVER = 2;
  41.         public const int LEAVE = 3;
  42.         public static float PERCENTAJE = (float)0.5;
  43.         private static readonly string[] Alphabit = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
  44.                               "N", "O","P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z","AA","AB","AC","AD","AE","AF","AG","AH","AI","AJ","AK","AL","AM","AN","AO","AP","AQ","AR","AS","AT","AU","AV","AW","AX","AY","AZ" };
  45.  
  46.         #region Facturacion
  47.  
  48.         public static readonly string NOMBRE_FISCAL = "UNIVERSIDAD ITEC";
  49.         public static readonly string DIRECCION_FISCAL = "LEON 160 SUR";
  50.         public static readonly string COLONIA_FISCAL = "TEPIC CENTRO";
  51.         public static readonly string MUNICIPIO_FISCAL = "TEPIC";
  52.         public static readonly string NoEXTERIOR_FISCAL = "160 SUR";
  53.         public static readonly string ESTADO_FISCAL = "NAYARIT";
  54.         public static readonly string PAIS_FISCAL = "MÉXICO";
  55.         public static readonly string CODIGO_POSTAL_FISCAL = "63000";
  56.         public static readonly string RFC_FISCAL = "ITS100910B60";//"AAA010101AAA";//
  57.         public static readonly string TELEFONO_FISCAL = "3112130744";
  58.  
  59.         public static readonly string RFC_PUBLICO_GENERAL_NACIONAL = "XAXX010101000";
  60.         public static readonly string RFC_PUBLICO_GENERAL_EXTRANJERO = "XEXX010101000";
  61.         public static readonly string NOMBRE_FISCAL_PUBLICO_GENERAL = "PÚBLICO GENERAL";
  62.         private static readonly string USER_EMAIL = "itec.tepic.contabilidad@gmail.com";//"facturacion@itectepic.edu.mx";
  63.         private static readonly string PASS_EMAIL = "CHIPUCHI";//"ITEC1009";
  64.         private static readonly string USER_TIMBRADO = "ITS100910B60";//"ITS100910B60";//"checksystems";//"ITS100910B60";//"DEMO700101XXX";//
  65.         private static readonly string PASS_TIMBRADO = "1T3c";//"1T3c";//"Ch3cksyst3ms";//"1T3c";//"DEMO700101XXX";//
  66.         public static readonly ComboBoxItem[] METODOS_DE_PAGO = new ComboBoxItem[] { new ComboBoxItem("Efectivo", new MetodoPago(01, "Efectivo")),
  67.         new ComboBoxItem("Cheque nominativo", new MetodoPago(02, "Cheque nominativo")),new ComboBoxItem("Transferencia electrónica de fondos", new MetodoPago(03, "Transferencia electrónica de fondos")),new ComboBoxItem("Tarjeta de Crédito", new MetodoPago(04, "Tarjeta de Crédito")),        
  68.         new ComboBoxItem("Tarjeta de Débito", new MetodoPago(28, "Tarjeta de Débito"))};
  69.  
  70.         public static readonly ComboBoxItem[] NIVEL_EDUCATIVO = new ComboBoxItem[] { new ComboBoxItem("Profesional técnico", "Profesional técnico"),
  71.         new ComboBoxItem("Bachillerato o su equivalente", "Bachillerato o su equivalente"),new ComboBoxItem("Licenciatura", "Licenciatura"),
  72.         new ComboBoxItem("Maestria", "Maestria"),new ComboBoxItem("Especialidad", "Especialidad")};
  73.         public static readonly ComboBoxItem[] GRADO_Y_GRUPO = new ComboBoxItem[] { new ComboBoxItem("1 A", "1 A"),new ComboBoxItem("1 B", "1 B"),new ComboBoxItem("1 C", "1 C"),
  74.         new ComboBoxItem("2 A", "2 A"),new ComboBoxItem("2 B", "2 B"),new ComboBoxItem("2 C", "2 C"),
  75.         new ComboBoxItem("3 A", "3 A"),new ComboBoxItem("3 B", "3 B"),new ComboBoxItem("3 C", "3 C"),
  76.         new ComboBoxItem("4 A", "4 A"),new ComboBoxItem("4 B", "4 B"),new ComboBoxItem("4 C", "4 C"),
  77.         new ComboBoxItem("5 A", "5 A"),new ComboBoxItem("5 B", "5 B"),new ComboBoxItem("5 C", "5 C")};
  78.         private static readonly string clientID = "966254033375-9vfh3bja168517plbgcqclve50637rhg.apps.googleusercontent.com";
  79.         private static readonly string clientSecret = "PnlMASvaYAKoyK18eRMtGyoC";
  80.         private static readonly string redirectURL = "urn:ietf:wg:oauth:2.0:oob";
  81.         private static string acessToken = string.Empty;
  82.         private static string refreshtoken = string.Empty;
  83.         public static DateTime _dtDefaultDateTime = new DateTime(1900, 1, 1);
  84.         public static readonly string CORTE_CAJA = "universidaditec@itectepic.edu.mx,kvin.bw@gmail.com,marsol19452010@hotmail.com,contabilidad@itectepic.edu.mx";
  85.         public static string getEmailPublicoGeneral()
  86.         {
  87.             return USER_EMAIL;
  88.         }
  89.         public static string getUserTimbrado()
  90.         {
  91.             return USER_TIMBRADO;
  92.         }
  93.         public static string getClientId
  94.         {
  95.             get
  96.             {
  97.                 return clientID;
  98.             }
  99.         }
  100.         public static string RedirectURL
  101.         {
  102.             get
  103.             {
  104.                 return redirectURL;
  105.             }
  106.         }
  107.         public static string getClientSecret
  108.         {
  109.             get
  110.             {
  111.                 return clientSecret;
  112.             }
  113.         }
  114.         public static string getPassTimbrado()
  115.         {
  116.             return PASS_TIMBRADO;
  117.         }
  118.         public static string Access_Token
  119.         {
  120.             get
  121.             {
  122.                 return acessToken;
  123.             }
  124.             set
  125.             {
  126.                 acessToken = value;
  127.             }
  128.         }
  129.         public static string Refresh_Token
  130.         {
  131.             get
  132.             {
  133.                 return refreshtoken;
  134.             }
  135.             set
  136.             {
  137.                 refreshtoken = value;
  138.             }
  139.         }
  140.         //https://www.google.com/settings/security/lesssecureapps
  141.         public static bool enviarFactura(string correo, string pathxml, string pathpdf)
  142.         {
  143.             try
  144.             {
  145.                 return Utility.enviarFactura(correo, pathxml, pathpdf);
  146.             }
  147.             catch (Exception e)
  148.             {
  149.                 MessageBox.Show(e.ToString());
  150.                 System.Diagnostics.Debug.WriteLine(e.ToString());
  151.             }
  152.             return false;
  153.         }
  154.         //public static bool enviarFactura(string correo, string pathxml, string pathpdf)
  155.         //{
  156.         //    try
  157.         //    {
  158.         //        string EMAIL_BODY = string.Empty;
  159.         //        using (var resource = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Universidad_ITEC.ResourcesFacturacion.plantilla_correo.html"))
  160.         //        {
  161.         //            using (StreamReader r = new StreamReader(resource))
  162.         //            {
  163.         //                EMAIL_BODY = r.ReadToEnd();
  164.         //                r.Dispose();
  165.         //            }
  166.         //            using (MailMessage mail = new MailMessage())
  167.         //            {
  168.         //                mail.From = new MailAddress(USER_EMAIL);
  169.         //                mail.To.Add(correo.Replace(";",","));
  170.         //                mail.Subject = "Envio de factura UNIVERSIDAD ITEC TEPIC";
  171.         //                mail.Body = EMAIL_BODY;
  172.         //                mail.IsBodyHtml = true;
  173.         //                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  174.         //                using (var img = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Universidad_ITEC.Resources.itec.fw.png"))
  175.         //                {
  176.         //                    var inlineLogo = new LinkedResource(img, MediaTypeNames.Image.Jpeg);
  177.         //                    inlineLogo.ContentId = "logo";
  178.         //                    var view = AlternateView.CreateAlternateViewFromString(EMAIL_BODY, null, MediaTypeNames.Text.Html);
  179.         //                    view.LinkedResources.Add(inlineLogo);
  180.         //                    mail.AlternateViews.Add(view);
  181.         //                    if (File.Exists(pathpdf))
  182.         //                    {
  183.         //                        mail.Attachments.Add(new Attachment(pathpdf));
  184.         //                    }
  185.         //                    if (File.Exists(pathxml))
  186.         //                    {
  187.         //                        mail.Attachments.Add(new Attachment(pathxml));
  188.         //                    }
  189.         //                    using (SmtpClient smtp = new SmtpClient("mail.itectepic.edu.mx", 26))
  190.         //                    {
  191.         //                        smtp.EnableSsl = true;
  192.         //                        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
  193.         //                        smtp.UseDefaultCredentials = false;
  194.         //                        smtp.Credentials = new NetworkCredential(USER_EMAIL, PASS_EMAIL);
  195.         //                        ServicePointManager.ServerCertificateValidationCallback = delegate(object s
  196.         //                        , X509Certificate certificate
  197.         //                        , X509Chain chai
  198.         //                        , SslPolicyErrors sslPolicyErrors)
  199.         //                   { return true; };
  200.         //                        smtp.Send(mail);
  201.         //                    }
  202.         //                }
  203.         //            }
  204.         //            return true;
  205.         //        }
  206.         //    }
  207.         //    catch (SmtpFailedRecipientException e)
  208.         //    {
  209.         //        MessageBox.Show(e.ToString());
  210.         //        return false;
  211.         //    }
  212.         //    catch (Exception e)
  213.         //    {
  214.         //        MessageBox.Show(e.ToString());
  215.         //        System.Diagnostics.Debug.WriteLine(e.ToString());
  216.         //    }
  217.         //    return false;
  218.         //}
  219.  
  220.         public static bool enviarCorte(string corteNormal, string corteComplementario, DateTime fecha)
  221.         {
  222.             try
  223.             {
  224.                 try
  225.                 {
  226.                     return Utility.enviarCorteDeCaja(CORTE_CAJA, fecha, corteNormal, corteComplementario);
  227.                 }
  228.                 catch (Exception e)
  229.                 {
  230.                     MessageBox.Show(e.ToString());
  231.                     System.Diagnostics.Debug.WriteLine(e.ToString());
  232.                 }
  233.                 return true;
  234.             }
  235.             catch (SmtpFailedRecipientException e)
  236.             {
  237.                 MessageBox.Show(e.ToString());
  238.                 return false;
  239.             }
  240.             catch (Exception e)
  241.             {
  242.                 MessageBox.Show(e.ToString());
  243.                 System.Diagnostics.Debug.WriteLine(e.ToString());
  244.             }
  245.             return false;
  246.         }
  247.  
  248.         //public static bool enviarCorte(string corteNormal, string corteComplementario, DateTime fecha)
  249.         //{
  250.         //    try
  251.         //    {
  252.         //        string EMAIL_BODY = string.Empty;
  253.         //        using (var resource = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Universidad_ITEC.ResourcesFacturacion.plantilla_cortecaja.html"))
  254.         //        {
  255.         //            using (StreamReader r = new StreamReader(resource))
  256.         //            {
  257.         //                EMAIL_BODY = string.Format(r.ReadToEnd(), fecha.ToString("dd/MM/yyyy"));
  258.         //                r.Dispose();
  259.         //            }
  260.         //            using (MailMessage mail = new MailMessage())
  261.         //            {
  262.         //                mail.From = new MailAddress(USER_EMAIL);
  263.         //                mail.To.Add("universidaditec@itectepic.edu.mx,kvin.bw@gmail.com,marsol19452010@hotmail.com,contabilidad@itectepic.edu.mx");
  264.         //                mail.Subject = "Envio de corte de caja del dia " + fecha.ToString("dd/MM/yyyy");
  265.         //                mail.Body = EMAIL_BODY;
  266.         //                mail.IsBodyHtml = true;
  267.         //                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  268.         //                using (var img = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Universidad_ITEC.Resources.itec.fw.png"))
  269.         //                {
  270.         //                    var inlineLogo = new LinkedResource(img, MediaTypeNames.Image.Jpeg);
  271.         //                    inlineLogo.ContentId = "logo";
  272.         //                    var view = AlternateView.CreateAlternateViewFromString(EMAIL_BODY, null, MediaTypeNames.Text.Html);
  273.         //                    view.LinkedResources.Add(inlineLogo);
  274.         //                    mail.AlternateViews.Add(view);
  275.         //                    if (File.Exists(corteNormal))
  276.         //                    {
  277.         //                        mail.Attachments.Add(new Attachment(corteNormal));
  278.         //                    }
  279.         //                    if (File.Exists(corteComplementario))
  280.         //                    {
  281.         //                        mail.Attachments.Add(new Attachment(corteComplementario));
  282.         //                    }
  283.         //                    using (SmtpClient smtp = new SmtpClient("mail.itectepic.edu.mx", 26))
  284.         //                    {
  285.         //                        smtp.EnableSsl = true;
  286.         //                        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
  287.         //                        smtp.UseDefaultCredentials = false;
  288.         //                        smtp.Credentials = new NetworkCredential(USER_EMAIL, PASS_EMAIL);
  289.         //                        ServicePointManager.ServerCertificateValidationCallback = delegate(object s
  290.         //                        , X509Certificate certificate
  291.         //                        , X509Chain chai
  292.         //                        , SslPolicyErrors sslPolicyErrors)
  293.         //                        { return true; };
  294.         //                        smtp.Send(mail);
  295.         //                    }
  296.         //                }
  297.         //            }
  298.         //            return true;
  299.         //        }
  300.         //    }
  301.         //    catch (SmtpFailedRecipientException e)
  302.         //    {
  303.         //        MessageBox.Show(e.ToString());
  304.         //        return false;
  305.         //    }
  306.         //    catch (Exception e)
  307.         //    {
  308.         //        MessageBox.Show(e.ToString());
  309.         //        System.Diagnostics.Debug.WriteLine(e.ToString());
  310.         //    }
  311.         //    return false;
  312.         //}
  313.         public static void obtenerLugarDeExpedicion(string sucursal, ref XSDs.cfdv32.t_Ubicacion ubicacion)
  314.         {
  315.             if (frmPrincipal._UbicacionFiscal != null)
  316.             {
  317.                 if (frmPrincipal._UbicacionFiscal.Calle.Length > 0)
  318.                 {
  319.                     ubicacion.calle = frmPrincipal._UbicacionFiscal.Calle;
  320.                 }
  321.                 if (frmPrincipal._UbicacionFiscal.CodigoPostal.Length > 0)
  322.                 {
  323.                     ubicacion.codigoPostal = frmPrincipal._UbicacionFiscal.CodigoPostal;
  324.                 }
  325.                 if (frmPrincipal._UbicacionFiscal.Colonia.Length > 0)
  326.                 {
  327.                     ubicacion.colonia = frmPrincipal._UbicacionFiscal.Colonia;
  328.                 }
  329.                 if (frmPrincipal._UbicacionFiscal.Estado.Length > 0)
  330.                 {
  331.                     ubicacion.estado = frmPrincipal._UbicacionFiscal.Estado;
  332.                 }
  333.                 if (frmPrincipal._UbicacionFiscal.Localidad.Length > 0)
  334.                 {
  335.                     ubicacion.localidad = frmPrincipal._UbicacionFiscal.Localidad;
  336.                 }
  337.                 if (frmPrincipal._UbicacionFiscal.Municipio.Length > 0)
  338.                 {
  339.                     ubicacion.municipio = frmPrincipal._UbicacionFiscal.Municipio;
  340.                 }
  341.                 if (frmPrincipal._UbicacionFiscal.NumeroExterior.Length > 0)
  342.                 {
  343.                     ubicacion.noExterior = frmPrincipal._UbicacionFiscal.NumeroExterior;
  344.                 }
  345.                 else
  346.                 {
  347.                     ubicacion.noExterior = "S/N";
  348.                 }
  349.                 if (frmPrincipal._UbicacionFiscal.NumeroInterior.Length > 0)
  350.                 {
  351.                     ubicacion.noInterior = frmPrincipal._UbicacionFiscal.NumeroInterior;
  352.                 }
  353.                 else
  354.                 {
  355.                     ubicacion.noInterior = "S/N";
  356.                 }
  357.                 ubicacion.pais = frmPrincipal._UbicacionFiscal.Pais;
  358.             }
  359.             else
  360.             {
  361.                 if (frmPrincipal._Empresa.Calle.Length > 0)
  362.                 {
  363.                     ubicacion.calle = frmPrincipal._Empresa.Calle;
  364.                 }
  365.                 if (frmPrincipal._Empresa.CodigoPostal.Length > 0)
  366.                 {
  367.                     ubicacion.codigoPostal = frmPrincipal._Empresa.CodigoPostal;
  368.                 }
  369.                 if (frmPrincipal._Empresa.Colonia.Length > 0)
  370.                 {
  371.                     ubicacion.colonia = frmPrincipal._Empresa.Colonia;
  372.                 }
  373.                 if (frmPrincipal._Empresa.Estado.Length > 0)
  374.                 {
  375.                     ubicacion.estado = frmPrincipal._Empresa.Estado;
  376.                 }
  377.                 if (frmPrincipal._Empresa.Ciudad.Length > 0)
  378.                 {
  379.                     ubicacion.localidad = frmPrincipal._Empresa.Ciudad;
  380.                     ubicacion.municipio = frmPrincipal._Empresa.Ciudad;
  381.                 }                
  382.                 if (frmPrincipal._Empresa.NumeroExterior.Length > 0)
  383.                 {
  384.                     ubicacion.noExterior = frmPrincipal._Empresa.NumeroExterior;
  385.                 }
  386.                 else
  387.                 {
  388.                     ubicacion.noExterior = "S/N";
  389.                 }
  390.                 if (frmPrincipal._Empresa.NumeroInterior.Length > 0)
  391.                 {
  392.                     ubicacion.noInterior = frmPrincipal._Empresa.NumeroInterior;
  393.                 }
  394.                 else
  395.                 {
  396.                     ubicacion.noInterior = "S/N";
  397.                 }
  398.                 ubicacion.pais = frmPrincipal._Empresa.Pais;
  399.             }
  400.         }
  401.  
  402.         public static string obtenerLugarDeExpedicion()
  403.         {
  404.             if (frmPrincipal._UbicacionFiscal != null)
  405.             {
  406.                 return frmPrincipal._UbicacionFiscal.Municipio + ", " + frmPrincipal._UbicacionFiscal.Estado;
  407.             }
  408.             else
  409.             {
  410.                 return frmPrincipal._Empresa.Ciudad + ", " + frmPrincipal._Empresa.Estado;
  411.             }
  412.         }
  413.  
  414.         public static bool CancelarFactura(FacturaDigital factura, ref string message, ref int error)
  415.         {
  416.             return CancelacionFactura(factura, ref message, ref error);
  417.         }
  418.  
  419.         private static bool CancelacionFactura(FacturaDigital factura, ref string message, ref int error)
  420.         {
  421.             try
  422.             {
  423.                 Certificados certificado = frmPrincipal._BaseDatos.ObtenerRequisitosFacturacion();
  424.                 if (certificado == null)
  425.                 {
  426.                     FrmCertificadosDigitales requisitos = new FrmCertificadosDigitales();
  427.                     if (requisitos.ShowDialog() == DialogResult.OK)
  428.                     {
  429.                         certificado = requisitos.obtenerCertificado();
  430.                     }
  431.                 }
  432.                 else
  433.                 {
  434.                     certificado = frmPrincipal._BaseDatos.ObtenerRequisitosFacturacion();
  435.                 }
  436.                 if (File.Exists(certificado.RutaCertificado) && File.Exists(certificado.RutaClave))
  437.                 {
  438.                     if (esCertificadoValido(certificado, ref error))
  439.                     {
  440.                         Random r = new Random();
  441.                         int ntimes = 0;
  442.                         string pac = string.Empty;
  443.                     Again:
  444.                         try
  445.                         {
  446.                             pac = r.Next(1, 9).ToString();
  447.                             WSMFRespuesta respuesta = WSMultifacturas.Cancelar(pac, Global.getUserTimbrado(), Global.getPassTimbrado(), factura.No, certificado.RutaCertificado, certificado.RutaClave, certificado.ContraseñaClave);
  448.                             if (CultureInfo.CurrentCulture.CompareInfo.IndexOf(respuesta.CodigoMFTexto, "UUID CANCELADO CORRECTAMENTE", CompareOptions.IgnoreCase) != -1)
  449.                             {
  450.                                 return true;
  451.                             }
  452.                             else
  453.                             {
  454.                                 if (ntimes < 8)
  455.                                 {
  456.                                     ntimes++;
  457.                                     goto Again;
  458.                                 }
  459.                                 else
  460.                                 {
  461.                                     message = "La factura no se ha podido cancelar debido a " + mensajeErroWEbService(respuesta.CodigoMFNumero, respuesta.codigo_mf_texto);
  462.                                     return false;
  463.                                 }
  464.                             }
  465.                         }
  466.                         catch (Exception e)
  467.                         {
  468.                             System.Diagnostics.Debug.WriteLine(e.ToString());
  469.                             if (ntimes < 8)
  470.                             {
  471.                                 ntimes++;
  472.                                 goto Again;
  473.                             }
  474.                             else
  475.                             {
  476.                                 message = "La factura no se ha podido cancelar debido a " + e.ToString();
  477.                                 return false;
  478.                             }
  479.                         }
  480.                     }
  481.                     else
  482.                     {
  483.                         if (error == 1)
  484.                         {
  485.                             message = "La factura no pudo ser generada debido a que el certificado digital no corresponde al del rfc fiscal del emisor.\nFavor de asignar el certificado digital correspondiente al emisor.";
  486.                         }
  487.                         else
  488.                         {
  489.                             error = 1;
  490.                             message = "La factura no ha sido cancelada debido a que el certificado digital no es válido o ha expirado.\nFavor de proporcionar un certificado válido.\nVaya a Reportes, seleccione Facturas e intente nuevamente.";
  491.                         }
  492.                     }
  493.                 }
  494.                 else
  495.                 {
  496.                     if (error == 1)
  497.                     {
  498.                         message = "La factura no pudo ser generada debido a que el certificado digital no corresponde al del rfc fiscal del emisor.\nFavor de asignar el certificado digital correspondiente al emisor.";
  499.                     }
  500.                     else
  501.                     {
  502.                         error = 1;
  503.                         message = "La factura no ha sido cancelada debido a que el certificado y/o la llave digital no existe(n).\nFavor de proporcionar un certificado y/o llave digital válida.\nVaya a Reportes, seleccione Facturas e intente nuevamente.";
  504.                     }
  505.                 }
  506.                 return false;
  507.             }
  508.             catch (Exception e)
  509.             {
  510.                 System.Diagnostics.Debug.WriteLine(e.ToString());
  511.             }
  512.             return false;
  513.         }
  514.  
  515.         private static string mensajeErroWEbService(string code, string exception)
  516.         {
  517.             string error = string.Empty;
  518.             switch (code)
  519.             {
  520.                 case "0":
  521.                     error = exception;
  522.                     break;
  523.                 case "1":
  524.                     error = "saldo insufuciente con su servicio de timbrado.\nFavor de consultar su saldo con su disribuidor de servicio de timbrado.";
  525.                     break;
  526.                 case "2":
  527.                     error = "un error al timbrar.\n" + exception;
  528.                     break;
  529.                 case "3":
  530.                     error = "un error al conectarse al servidor de timbrado.\nCheque su conexión a internet e intentelo más tarde.";
  531.                     break;
  532.                 case "4":
  533.                     error = "un error de autentificación con su servicio de timbrado.\nFavor de reportarlo con su distribuidor de servicio de timbrado.\nEspere un momento (entre 10-20 min) e intente nuevamente.";
  534.                     break;
  535.                 case "5":
  536.                     error = "un error de autentificación con su servicio de timbrado.\nFavor de verificar que su usuario y contraseña\nsean los mismos que los le proporcionó su\ndistribuidor de servicio de timbrado.";
  537.                     break;
  538.                 case "6":
  539.                     error = "un error al timbrar por parte de su distribuidor de servicio de timbrado.\nfavor de reportarlo con su distribuidor.\nEspere un momento (entre 10-20 min) e intente nuevamente.";
  540.                     break;
  541.                 case "7":
  542.                     error = "un error interno del servidor.\n" + exception;
  543.                     break;
  544.                 case "8":
  545.                     error = "un error al generar el ticket.";
  546.                     break;
  547.             }
  548.             return error;
  549.         }
  550.  
  551.         public static bool esCertificadoValido(Certificados certificado, ref int error)
  552.         {
  553.             X509Certificate2 x509 = null;
  554.             try
  555.             {
  556.                 x509 = new X509Certificate2(certificado.RutaCertificado);
  557.             }
  558.             catch (Exception e)
  559.             {
  560.                 System.Diagnostics.Debug.WriteLine(e.ToString());
  561.             }
  562.             if (x509 != null)
  563.             {
  564.                 DateTime dtExpiration = x509.NotAfter;
  565.                 DateTime dtNow = DateTime.Now;
  566.                 if (dtExpiration.Year >= dtNow.Year)
  567.                 {
  568.                     string RFC = frmPrincipal._Empresa.DatosVerificados ? frmPrincipal._Empresa.RFC : Global.RFC_FISCAL;
  569.                     if (dtExpiration.Year > dtNow.Year)
  570.                     {
  571.                         if (CultureInfo.CurrentCulture.CompareInfo.IndexOf(x509.Subject, RFC, CompareOptions.IgnoreCase) != -1)
  572.                         {
  573.                             return true;
  574.                         }
  575.                         else
  576.                         {
  577.                             error = 1;
  578.                             return false;
  579.                         }
  580.                     }
  581.                     else
  582.                     {
  583.                         if (CultureInfo.CurrentCulture.CompareInfo.IndexOf(x509.Subject, RFC, CompareOptions.IgnoreCase) != -1)
  584.                         {
  585.                             if (dtExpiration.Month > dtNow.Month)
  586.                             {
  587.                                 return true;
  588.                             }
  589.                             else
  590.                             {
  591.                                 if (dtExpiration.Month == dtNow.Month)
  592.                                 {
  593.                                     if (dtExpiration.Day >= dtNow.Day)
  594.                                     {
  595.                                         return true;
  596.                                     }
  597.                                     else
  598.                                     {
  599.                                         return false;
  600.                                     }
  601.                                 }
  602.                                 else
  603.                                 {
  604.                                     return false;
  605.                                 }
  606.                             }
  607.                         }
  608.                         else
  609.                         {
  610.                             error = 1;
  611.                             return false;
  612.                         }
  613.                     }
  614.                 }
  615.                 else
  616.                 {
  617.                     return false;
  618.                 }
  619.             }
  620.             else
  621.             {
  622.                 return false;
  623.             }
  624.         }
  625.         #endregion facturacion
  626.  
  627.         public static bool hayInternet(bool mostrarMensaje)
  628.         {
  629.             NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
  630.             foreach (NetworkInterface nic in nics)
  631.             {
  632.                 if (string.Compare(nic.Name, "hamachi", true) == 0)
  633.                 {
  634.                     continue;
  635.                 }
  636.  
  637.                 if (
  638.                     (nic.NetworkInterfaceType != NetworkInterfaceType.Loopback && nic.NetworkInterfaceType != NetworkInterfaceType.Tunnel) &&
  639.                     nic.OperationalStatus == OperationalStatus.Up)
  640.                 {
  641.                     return true;
  642.                 }
  643.             }
  644.             if (mostrarMensaje)
  645.             {
  646.                 MessageBox.Show("Hay una falla en su conexión de internet.\nFavor de verificar su conexión de internet e intente más tarde.", Global.STR_NOMBRE_SISTEMA, MessageBoxButtons.OK, MessageBoxIcon.Stop);
  647.             }
  648.             return false;
  649.            
  650.         }
  651.         public static string CadenaConexion
  652.         {
  653.             get
  654.             {
  655.                 return Properties.Settings.Default.CadenaConexion;
  656.             }
  657.         }
  658.  
  659.         public static string Usuario
  660.         {
  661.             get
  662.             {
  663.                 return Properties.Settings.Default.Usuario;
  664.             }
  665.         }
  666.  
  667.         public static double Descuento
  668.         {
  669.             get
  670.             {
  671.                 return Properties.Settings.Default.Descuento;
  672.             }
  673.         }
  674.  
  675.         //public static DevExpress.Utils.WaitDialogForm WaitDialog { get; set; }
  676.  
  677.         public static DateTime MinDate
  678.         {
  679.             get
  680.             {
  681.                 return new DateTime(1900, 1, 1);
  682.             }
  683.         }
  684.  
  685.         public static bool EsFolioId(object strText, ref string id)
  686.         {
  687.             bool bIsNumber = false;
  688.             if (Convert.ToString(strText).Trim().Length > 0)
  689.             {
  690.                 long lValue = 0;
  691.                 bIsNumber = long.TryParse(Convert.ToString(strText).Trim(), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out lValue);
  692.                 if (lValue > 0)
  693.                 {
  694.                     id = lValue.ToString();
  695.                 }
  696.                 else
  697.                 {
  698.                     id = strText.ToString();
  699.                 }
  700.             }
  701.  
  702.             return bIsNumber;
  703.         }
  704.  
  705.         public static void moveFocusToNextControl(System.Windows.Forms.Keys KeyCode)
  706.         {
  707.             if (KeyCode == System.Windows.Forms.Keys.Enter)
  708.             {
  709.                 System.Windows.Forms.SendKeys.Send("{TAB}");
  710.             }
  711.         }
  712.  
  713.         public static void moveFocusToNextControl()
  714.         {
  715.             System.Windows.Forms.SendKeys.Send("{TAB}");
  716.         }
  717.  
  718.         public static void imgEfects(System.Windows.Forms.PictureBox pb, int option)
  719.         {
  720.             switch (option)
  721.             {
  722.                 case DOWN:
  723.                     pb.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  724.                     break;
  725.                 case UP:
  726.                     pb.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  727.                     break;
  728.                 case HOVER:
  729.                     pb.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  730.                     break;
  731.                 case LEAVE:
  732.                     pb.BorderStyle = System.Windows.Forms.BorderStyle.None;
  733.                     break;
  734.             }
  735.         }
  736.  
  737.         public static string DoubleToString(double dNumero)
  738.         {
  739.             return dNumero.ToString("N2", NumberFormatInfo.InvariantInfo);
  740.         }
  741.  
  742.         public static double StringToDouble(string strNumero)
  743.         {
  744.             double dResultado = 0;
  745.  
  746.             double.TryParse(strNumero.Trim(), NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out dResultado);
  747.  
  748.             return dResultado;
  749.         }
  750.  
  751.         public static bool esPrestamoExcedido(DateTime fecha)
  752.         {
  753.             if (DateTime.Now.Year > fecha.Year)
  754.             {
  755.                 return true;
  756.             }
  757.             else
  758.             {
  759.                 if (DateTime.Now.Month > fecha.Month)
  760.                 {
  761.                     return true;
  762.                 }
  763.                 else
  764.                 {
  765.                     if (DateTime.Now.Day > fecha.Day)
  766.                     {
  767.                         return true;
  768.                     }
  769.                 }
  770.             }            
  771.             return false;
  772.         }
  773.         public static int diasPrestamoExcedido(DateTime fecha)
  774.         {
  775.             return -Convert.ToInt32((fecha - DateTime.Now).TotalDays);
  776.         }
  777.  
  778.         public static int diasPrestamo(DateTime fechaInicio, DateTime fechaFin)
  779.         {
  780.             return Convert.ToInt32((fechaFin - fechaInicio).TotalDays);
  781.         }
  782.  
  783.         public static bool esFechaEntregaValida(DateTime fechaInicio, DateTime fechaEntrega)
  784.         {
  785.             if (fechaInicio > fechaEntrega)
  786.             {
  787.                 return false;
  788.             }
  789.             else
  790.             {
  791.                 return true;
  792.             }
  793.         }
  794.         public static void updateStyleSelectedowsPercentajeGrid(MetroFramework.Controls.MetroGrid grid)
  795.         {
  796.             float _offset = Global.PERCENTAJE;
  797.             grid.DefaultCellStyle.SelectionBackColor = ControlPaint.Light(MetroPaint.GetStyleColor(grid.Style), _offset);
  798.             grid.DefaultCellStyle.SelectionForeColor = Color.FromArgb(17, 17, 17);
  799.  
  800.             grid.DefaultCellStyle.SelectionBackColor = ControlPaint.Light(MetroPaint.GetStyleColor(grid.Style), _offset);
  801.             grid.DefaultCellStyle.SelectionForeColor = Color.FromArgb(17, 17, 17);
  802.  
  803.             grid.RowHeadersDefaultCellStyle.SelectionBackColor = ControlPaint.Light(MetroPaint.GetStyleColor(grid.Style), _offset);
  804.             grid.RowHeadersDefaultCellStyle.SelectionForeColor = Color.FromArgb(17, 17, 17);
  805.  
  806.             grid.ColumnHeadersDefaultCellStyle.SelectionBackColor = ControlPaint.Light(MetroPaint.GetStyleColor(grid.Style), _offset);
  807.             grid.ColumnHeadersDefaultCellStyle.SelectionForeColor = Color.FromArgb(17, 17, 17);
  808.         }
  809.        
  810.         public static string ObtenerImpresoraTickets()
  811.         {
  812.             string strResultado = string.Empty;
  813.  
  814.             long lPreferenciasId = frmPrincipal._BaseDatos.ObtenerPreferenciasId();
  815.             Preferencias preferencias = frmPrincipal._BaseDatos.ObtenerPreferencias(lPreferenciasId);
  816.             if (preferencias != null)
  817.             {
  818.                 strResultado = preferencias.ImpresoraTickets;
  819.             }
  820.  
  821.             bool bSeleccionarImpresora = false;
  822.  
  823.             if (strResultado == string.Empty)
  824.             {
  825.                 bSeleccionarImpresora = true;
  826.             }
  827.             else
  828.             {
  829.                 if (!EsImpresoraValida(strResultado))
  830.                 {
  831.                     bSeleccionarImpresora = true;
  832.                 }
  833.             }
  834.  
  835.             if (bSeleccionarImpresora)
  836.             {
  837.                 FrmImpresora frm = new FrmImpresora();
  838.                 if (frm.ShowDialog() == DialogResult.OK)
  839.                 {
  840.                     strResultado = frm.ObtenerImpresora();
  841.  
  842.                     if (strResultado != string.Empty)
  843.                     {
  844.                         frmPrincipal._BaseDatos.ActualizarPreferenciasImpresoraTickets(lPreferenciasId, strResultado);
  845.                     }
  846.                 }
  847.             }
  848.  
  849.             return strResultado;
  850.         }
  851.  
  852.         public static string ObtenerImpresoraFacturas()
  853.         {
  854.             string strResultado = string.Empty;
  855.  
  856.             long lPreferenciasId = frmPrincipal._BaseDatos.ObtenerPreferenciasId();
  857.             Preferencias preferencias = frmPrincipal._BaseDatos.ObtenerPreferencias(lPreferenciasId);
  858.             if (preferencias != null)
  859.             {
  860.                 strResultado = preferencias.ImpresoraFacturas;
  861.             }
  862.  
  863.             bool bSeleccionarImpresora = false;
  864.  
  865.             if (strResultado == string.Empty)
  866.             {
  867.                 bSeleccionarImpresora = true;
  868.             }
  869.             else
  870.             {
  871.                 if (!EsImpresoraValida(strResultado))
  872.                 {
  873.                     bSeleccionarImpresora = true;
  874.                 }
  875.             }
  876.  
  877.             if (bSeleccionarImpresora)
  878.             {
  879.                 FrmImpresora frm = new FrmImpresora();
  880.                 if (frm.ShowDialog() == DialogResult.OK)
  881.                 {
  882.                     strResultado = frm.ObtenerImpresora();
  883.  
  884.                     if (strResultado != string.Empty)
  885.                     {
  886.                         frmPrincipal._BaseDatos.ActualizarPreferenciasImpresoraFacturas(lPreferenciasId, strResultado);
  887.                     }
  888.                 }
  889.             }
  890.  
  891.             return strResultado;
  892.         }
  893.  
  894.         public static List<string> ObtenerImpresoras()
  895.         {
  896.             List<string> lstImpresoras = new List<string>();
  897.  
  898.             foreach (string strImpresoraInstalada in PrinterSettings.InstalledPrinters)
  899.             {
  900.                 lstImpresoras.Add(strImpresoraInstalada);
  901.             }
  902.  
  903.             return lstImpresoras;
  904.         }
  905.  
  906.         public static bool EsImpresoraValida(string strImpresora)
  907.         {
  908.             List<string> lstImpresoras = ObtenerImpresoras();
  909.  
  910.             if (lstImpresoras.Contains(strImpresora))
  911.             {
  912.                 return true;
  913.             }
  914.  
  915.             return false;
  916.         }
  917.  
  918.         public static bool exportToExcel(MetroFramework.Controls.MetroGrid grid, string tituloReporte, bool corteCaja)
  919.         {
  920.             if (grid.RowCount > 0)
  921.             {
  922.                 SaveFileDialog sfd = new SaveFileDialog();
  923.                 sfd.Title = "Export To Excel";
  924.                 sfd.Filter = "To Excel (Xlsx)|*.xlsx";
  925.                 sfd.FileName = "*.xlsx";
  926.                 bool result = false;
  927.                 if (sfd.ShowDialog() == DialogResult.OK)
  928.                 {                    
  929.                     string filename = sfd.FileName;                    
  930.                     try
  931.                     {
  932.                         string Range_Letter = Alphabit[grid.Columns.Count];
  933.                         string Range_Row = (grid.Rows.Count + 1).ToString();
  934.  
  935.                         if (File.Exists(filename))
  936.                         {
  937.                             File.Delete(filename);
  938.                         }
  939.  
  940.                         Microsoft.Office.Interop.Excel.Application oApp;
  941.                         Microsoft.Office.Interop.Excel.Worksheet oSheet;
  942.                         Microsoft.Office.Interop.Excel.Workbook oBook;
  943.  
  944.                         oApp = new Microsoft.Office.Interop.Excel.Application();
  945.                         oBook = oApp.Workbooks.Add();
  946.                         oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oBook.Worksheets.get_Item(1);                        
  947.                         int columns = grid.Columns.Count;
  948.                         for (int x = 0; x < columns; x++)
  949.                         {
  950.                             // creating Columns :
  951.                             oSheet.Cells[1, x + 2] = grid.Columns[x].HeaderText;
  952.                         }
  953.                         oSheet.Cells.NumberFormat = "@";
  954.                         int rows = grid.Rows.Count;
  955.                         for (int i = 0; i < columns; i++)
  956.                         {
  957.                             for (int j = 0; j < rows; j++)
  958.                             {
  959.                                 // creating rows :
  960.                                 if (grid.Rows[j].Cells[i].Value != null)
  961.                                 {
  962.                                     oSheet.Cells[j + 2, i + 2] = grid.Rows[j].Cells[i].Value.ToString();
  963.                                 }
  964.                             }
  965.                         }                      
  966.                         //Add some formatting
  967.                         Range rng1 = oSheet.get_Range("B1", Range_Letter + "1");
  968.                         rng1.WrapText = true;
  969.                         rng1.Font.Size = 8;
  970.                         rng1.Font.Bold = true;
  971.                         //rng1.Cells.Borders.LineStyle = XlLineStyle.xlDouble;
  972.                         rng1.Cells.Borders.Color = System.Drawing.Color.Black;
  973.                         rng1.Font.Color = System.Drawing.Color.Black;
  974.                         rng1.HorizontalAlignment = XlHAlign.xlHAlignCenter;
  975.                         //rng1.Interior.Color = System.Drawing.Color.LightGray;
  976.                         Range rng2 = null;
  977.                         if (Range_Letter.CompareTo("J") == 0)
  978.                         {
  979.                             rng2 = oSheet.get_Range("B2", "D" + Range_Row);
  980.                             Range rngConcepto = rng2.Columns[8];
  981.                             rng2.WrapText = false;
  982.                             rng2.Font.Size = 6;
  983.                             rng2.Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
  984.                             rng2.Cells.Borders.Color = System.Drawing.Color.Black;
  985.                             rng2.VerticalAlignment = XlVAlign.xlVAlignCenter;
  986.                             rng2.EntireColumn.AutoFit();
  987.                             rng2.EntireRow.AutoFit();
  988.  
  989.                             Range rngconcepto = oSheet.get_Range("E2", "J" + Range_Row);
  990.                             rngconcepto.WrapText = true;
  991.                             rngconcepto.Font.Size = 6;
  992.                             rngconcepto.Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
  993.                             rngconcepto.Cells.Borders.Color = System.Drawing.Color.Black;
  994.                             rngconcepto.VerticalAlignment = XlVAlign.xlVAlignCenter;
  995.                             rngconcepto.EntireColumn.AutoFit();
  996.                             rngconcepto.EntireRow.AutoFit();
  997.                         }
  998.                         else
  999.                         {
  1000.                             rng2 = oSheet.get_Range("B2", Range_Letter + Range_Row);
  1001.                             rng2.WrapText = false;
  1002.                             rng2.Font.Size = 6;
  1003.                             rng2.Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
  1004.                             rng2.Cells.Borders.Color = System.Drawing.Color.Black;
  1005.                             rng2.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1006.                             //rng2.Interior.Color = System.Drawing.Color.Azure;
  1007.                             rng2.EntireColumn.AutoFit();
  1008.                             rng2.EntireRow.AutoFit();
  1009.                         }
  1010.                         //Add a header row
  1011.                         oSheet.get_Range("B1", Range_Letter + "2").EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
  1012.                         if (!corteCaja)
  1013.                         {
  1014.                             oSheet.Cells[1, 2] = "Fecha del Reporte:  " + DateTime.Now.ToString("dd/MM/yyy h:mm tt");
  1015.                         }
  1016.                         else
  1017.                         {
  1018.                             oSheet.Cells[1, 2] = "Fecha del Corte:  " + DateTime.Now.ToString("dd/MMMM/yyy");
  1019.                         }
  1020.                         Range rng3 = oSheet.get_Range("B1", Range_Letter + "2");
  1021.                         rng3.WrapText = true;
  1022.                         rng3.Merge(Missing.Value);
  1023.                         rng3.Font.Size = 11;
  1024.                         rng3.Font.Color = System.Drawing.Color.Black;
  1025.                         rng3.Font.Bold = true;
  1026.                         rng3.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1027.                         rng3.EntireColumn.AutoFit();
  1028.                         rng3.EntireRow.AutoFit();
  1029.                         //SetRowHeight(ref rng3);
  1030.  
  1031.                         oSheet.get_Range("B1", Range_Letter + "2").EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
  1032.                         oSheet.Cells[1, 3] = tituloReporte;
  1033.                         Range rng4 = oSheet.get_Range("B1", Range_Letter + "2");
  1034.                         rng4.WrapText = true;
  1035.                         rng4.Merge(Missing.Value);
  1036.                         rng4.Font.Size = 16;
  1037.                         rng4.Font.Color = System.Drawing.Color.Black;
  1038.                         rng4.Font.Bold = true;
  1039.                         rng4.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1040.                         rng4.HorizontalAlignment = XlVAlign.xlVAlignCenter;
  1041.                         Microsoft.Office.Interop.Excel.Range oRange = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[1, 2];
  1042.                         Bitmap image = new Bitmap((Bitmap)Properties.Resources.uniitec, 150, 40);
  1043.                         System.Windows.Forms.Clipboard.SetDataObject(image, true);
  1044.                         oSheet.Paste(oRange, image);
  1045.  
  1046.                         oBook.SaveAs(filename);
  1047.                         oBook.Close();
  1048.                         oApp.Quit();
  1049.                         result = true;
  1050.                     }
  1051.                     catch (Exception e)
  1052.                     {
  1053.                         System.Diagnostics.Debug.WriteLine("ERROR AL EXPORTAR A EXCEL:" + e.ToString());
  1054.                     }
  1055.                     //sfd.OpenFile();
  1056.                 }                
  1057.                 sfd.Dispose();
  1058.                 return result;
  1059.             }
  1060.             else
  1061.             {
  1062.                 System.Windows.Forms.MessageBox.Show("No hay ningún registro a exportar.", Global.STR_NOMBRE_SISTEMA, MessageBoxButtons.OK, MessageBoxIcon.Information);
  1063.             }
  1064.             return false;
  1065.         }
  1066.  
  1067.         public static bool exportToExcelCorteCaja(MetroFramework.Controls.MetroGrid grid, string tituloReporte, bool complementario)
  1068.         {
  1069.             if (grid.RowCount > 0)
  1070.             {
  1071.                 SaveFileDialog sfd = new SaveFileDialog();
  1072.                 sfd.Title = "Export To Excel";
  1073.                 sfd.Filter = "To Excel (Xlsx)|*.xlsx";
  1074.                 sfd.FileName = "*.xlsx";
  1075.                 bool result = false;
  1076.                 if (sfd.ShowDialog() == DialogResult.OK)
  1077.                 {
  1078.                     string filename = sfd.FileName;
  1079.                     try
  1080.                     {
  1081.                         string Range_Letter = Alphabit[grid.Columns.Count];
  1082.                         string Range_Row = (grid.Rows.Count + 1).ToString();
  1083.  
  1084.                         if (File.Exists(filename))
  1085.                         {
  1086.                             File.Delete(filename);
  1087.                         }
  1088.  
  1089.                         Microsoft.Office.Interop.Excel.Application oApp;
  1090.                         Microsoft.Office.Interop.Excel.Worksheet oSheet;
  1091.                         Microsoft.Office.Interop.Excel.Workbook oBook;
  1092.  
  1093.                         oApp = new Microsoft.Office.Interop.Excel.Application();
  1094.                         oBook = oApp.Workbooks.Add();
  1095.                         oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oBook.Worksheets.get_Item(1);                            
  1096.                         int columns = grid.Columns.Count;
  1097.                         for (int x = 0; x < columns; x++)
  1098.                         {
  1099.                             // creating Columns :
  1100.                             oSheet.Cells[1, x + 2] = grid.Columns[x].HeaderText;
  1101.                         }
  1102.                         oSheet.Cells.NumberFormat = "@";
  1103.                         int rows = grid.Rows.Count;
  1104.                         for (int i = 0; i < columns; i++)
  1105.                         {
  1106.                             for (int j = 0; j < rows; j++)
  1107.                             {
  1108.                                 // creating rows :
  1109.                                 if (grid.Rows[j].Cells[i].Value != null)
  1110.                                 {
  1111.                                     oSheet.Cells[j + 2, i + 2] = grid.Rows[j].Cells[i].Value.ToString();
  1112.                                 }
  1113.                             }
  1114.                         }
  1115.                         //Add some formatting
  1116.                         Range rng1 = oSheet.get_Range("B1", Range_Letter + "1");
  1117.                         rng1.WrapText = true;
  1118.                         rng1.Font.Size = 8;
  1119.                         rng1.Font.Bold = true;
  1120.                         //rng1.Cells.Borders.LineStyle = XlLineStyle.xlDouble;
  1121.                         rng1.Cells.Borders.Color = System.Drawing.Color.Black;
  1122.                         rng1.Font.Color = System.Drawing.Color.Black;
  1123.                         rng1.HorizontalAlignment = XlHAlign.xlHAlignCenter;
  1124.                         //rng1.Interior.Color = System.Drawing.Color.LightGray;
  1125.  
  1126.                         Range rng2 = oSheet.get_Range("B2", Range_Letter + Range_Row);
  1127.                         rng2.WrapText = true;
  1128.                         rng2.WrapText = false;
  1129.                         rng2.Font.Size = 6;
  1130.                         rng2.Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
  1131.                         rng2.Cells.Borders.Color = System.Drawing.Color.Black;
  1132.                         rng2.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1133.                         //rng2.Interior.Color = System.Drawing.Color.Azure;
  1134.                         rng2.EntireColumn.AutoFit();
  1135.                         rng2.EntireRow.AutoFit();
  1136.  
  1137.                         //Add a header row
  1138.                         oSheet.get_Range("B1", Range_Letter + "2").EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
  1139.                         if (complementario)
  1140.                         {
  1141.                             oSheet.Cells[1, 2] = "Fecha del Reporte Complementario:  " + DateTime.Now.ToString("dd/MM/yyy h:mm tt");
  1142.                         }
  1143.                         else
  1144.                         {
  1145.                             oSheet.Cells[1, 2] = "Fecha del Reporte:  " + DateTime.Now.ToString("dd/MM/yyy h:mm tt");
  1146.                         }
  1147.                         Range rng3 = oSheet.get_Range("B1", Range_Letter + "2");
  1148.                         rng3.WrapText = true;
  1149.                         rng3.Merge(Missing.Value);
  1150.                         rng3.Font.Size = 11;
  1151.                         rng3.Font.Color = System.Drawing.Color.Black;
  1152.                         rng3.Font.Bold = true;
  1153.                         rng3.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1154.                         rng3.EntireColumn.AutoFit();
  1155.                         rng3.EntireRow.AutoFit();
  1156.  
  1157.                         oSheet.get_Range("B1", Range_Letter + "2").EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
  1158.                         oSheet.Cells[1, 3] = tituloReporte;
  1159.                         Range rng4 = oSheet.get_Range("B1", Range_Letter + "2");
  1160.                         rng4.WrapText = true;
  1161.                         rng4.Merge(Missing.Value);
  1162.                         rng4.Font.Size = 16;
  1163.                         rng4.Font.Color = System.Drawing.Color.Black;
  1164.                         rng4.Font.Bold = true;
  1165.                         rng4.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1166.                         rng4.HorizontalAlignment = XlVAlign.xlVAlignCenter;
  1167.                         Microsoft.Office.Interop.Excel.Range oRange = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[1, 2];
  1168.                         Bitmap image = new Bitmap((Bitmap)Properties.Resources.uniitec, 150, 80);
  1169.                         System.Windows.Forms.Clipboard.SetDataObject(image, true);
  1170.                         oSheet.Paste(oRange, image);
  1171.  
  1172.                         oBook.SaveAs(filename);
  1173.                         oBook.Close();
  1174.                         oApp.Quit();
  1175.                         result = true;
  1176.                     }
  1177.                     catch (Exception e)
  1178.                     {
  1179.                         System.Diagnostics.Debug.WriteLine("ERROR AL EXPORTAR A EXCEL:" + e.ToString());
  1180.                     }
  1181.                     //sfd.OpenFile();
  1182.                 }
  1183.                 sfd.Dispose();
  1184.                 return result;
  1185.             }
  1186.             else
  1187.             {
  1188.                 System.Windows.Forms.MessageBox.Show("No hay ningún registro a exportar.", Global.STR_NOMBRE_SISTEMA, MessageBoxButtons.OK, MessageBoxIcon.Information);
  1189.             }
  1190.             return false;
  1191.         }
  1192.  
  1193.         public static bool exportToExcelCorteCajaParaCorreo(MetroFramework.Controls.MetroGrid grid, string tituloReporte, bool complementario, ref string path)
  1194.         {
  1195.             if (grid.RowCount > 0)
  1196.             {                
  1197.                 bool result = false;                
  1198.                 {
  1199.                     string filename = string.Empty;
  1200.                     if (complementario)
  1201.                     {
  1202.                         filename = crearDirectorioCortesCaja() + "\\CorteComplementario_" + DateTime.Now.ToString("dd-MM-yyyy") + ".xlsx";
  1203.                     }
  1204.                     else
  1205.                     {
  1206.                         filename = crearDirectorioCortesCaja() + "\\CorteNormal_" + DateTime.Now.ToString("dd-MM-yyyy") + ".xlsx";
  1207.                     }
  1208.                     path = filename;
  1209.                     try
  1210.                     {
  1211.                         string Range_Letter = Alphabit[grid.Columns.Count];
  1212.                         string Range_Row = (grid.Rows.Count + 1).ToString();
  1213.                         if (File.Exists(filename))
  1214.                         {
  1215.                             File.Delete(filename);
  1216.                         }
  1217.  
  1218.                         Microsoft.Office.Interop.Excel.Application oApp;
  1219.                         Microsoft.Office.Interop.Excel.Worksheet oSheet;
  1220.                         Microsoft.Office.Interop.Excel.Workbook oBook;
  1221.  
  1222.                         oApp = new Microsoft.Office.Interop.Excel.Application();
  1223.                         oBook = oApp.Workbooks.Add();
  1224.                         oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oBook.Worksheets.get_Item(1);
  1225.                         int columns = grid.Columns.Count;
  1226.                         for (int x = 0; x < columns; x++)
  1227.                         {
  1228.                             // creating Columns :
  1229.                             oSheet.Cells[1, x + 2] = grid.Columns[x].HeaderText;
  1230.                         }
  1231.                         oSheet.Cells.NumberFormat = "@";
  1232.                         int rows = grid.Rows.Count;
  1233.                         for (int i = 0; i < columns; i++)
  1234.                         {
  1235.                             for (int j = 0; j < rows; j++)
  1236.                             {
  1237.                                 // creating rows :
  1238.                                 if (grid.Rows[j].Cells[i].Value != null)
  1239.                                 {
  1240.                                     oSheet.Cells[j + 2, i + 2] = grid.Rows[j].Cells[i].Value.ToString();
  1241.                                 }
  1242.                             }
  1243.                         }
  1244.                         //Add some formatting
  1245.                         Range rng1 = oSheet.get_Range("B1", Range_Letter + "1");
  1246.                         rng1.Font.Size = 8;
  1247.                         rng1.Font.Bold = true;
  1248.                         //rng1.Cells.Borders.LineStyle = XlLineStyle.xlDouble;
  1249.                         rng1.Cells.Borders.Color = System.Drawing.Color.Black;
  1250.                         rng1.Font.Color = System.Drawing.Color.Black;
  1251.                         rng1.HorizontalAlignment = XlHAlign.xlHAlignCenter;
  1252.                         //rng1.Interior.Color = System.Drawing.Color.LightGray;
  1253.  
  1254.                         Range rng2 = oSheet.get_Range("B2", Range_Letter + Range_Row);
  1255.                         rng2.WrapText = false;
  1256.                         rng2.Font.Size = 6;
  1257.                         rng2.Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
  1258.                         rng2.Cells.Borders.Color = System.Drawing.Color.Black;
  1259.                         rng2.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1260.                         //rng2.Interior.Color = System.Drawing.Color.Azure;
  1261.                         rng2.EntireColumn.AutoFit();
  1262.                         rng2.EntireRow.AutoFit();
  1263.  
  1264.                         //Add a header row
  1265.                         oSheet.get_Range("B1", Range_Letter + "2").EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
  1266.                         oSheet.Cells[1, 2] = "Fecha del Reporte:  " + DateTime.Now.ToString("dd/MM/yyy h:mm tt");
  1267.                         Range rng3 = oSheet.get_Range("B1", Range_Letter + "2");
  1268.                         rng3.WrapText = true;
  1269.                         rng3.Merge(Missing.Value);
  1270.                         rng3.Font.Size = 11;
  1271.                         rng3.Font.Color = System.Drawing.Color.Black;
  1272.                         rng3.Font.Bold = true;
  1273.                         rng3.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1274.                         rng3.EntireColumn.AutoFit();
  1275.                         rng3.EntireRow.AutoFit();
  1276.                         //SetRowHeight(ref rng3);
  1277.  
  1278.                         oSheet.get_Range("B1", Range_Letter + "2").EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
  1279.                         oSheet.Cells[1, 3] = tituloReporte;
  1280.                         Range rng4 = oSheet.get_Range("B1", Range_Letter + "2");
  1281.                         rng4.WrapText = true;
  1282.                         rng4.Merge(Missing.Value);
  1283.                         rng4.Font.Size = 16;
  1284.                         rng4.Font.Color = System.Drawing.Color.Black;
  1285.                         rng4.Font.Bold = true;
  1286.                         rng4.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1287.                         rng4.HorizontalAlignment = XlVAlign.xlVAlignCenter;
  1288.                         //Bitmap image = new Bitmap((Bitmap)Properties.Resources.itecExport, 100, 100);
  1289.                         Microsoft.Office.Interop.Excel.Range oRange = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[1, 2];
  1290.                         Bitmap image = new Bitmap((Bitmap)Properties.Resources.uniitec, 150, 80);
  1291.                         System.Windows.Forms.Clipboard.SetDataObject(image, true);
  1292.                         oSheet.Paste(oRange, image);
  1293.                         //rng4.EntireColumn.AutoFit();
  1294.                         //rng4.EntireRow.AutoFit();
  1295.                         // SetRowHeight(ref rng4);
  1296.                         // oSheet.Cells.Columns.EntireColumn.AutoFit();
  1297.                         //oSheet.Rows.AutoFit();
  1298.                         //oSheet.Range["B1", Range_Letter + "2"].AutoFit();                        
  1299.  
  1300.                         oBook.SaveAs(filename);
  1301.                         oBook.Close();
  1302.                         oApp.Quit();
  1303.                         result = true;
  1304.                     }
  1305.                     catch (Exception e)
  1306.                     {
  1307.                         System.Diagnostics.Debug.WriteLine("ERROR AL EXPORTAR A EXCEL:" + e.ToString());
  1308.                     }
  1309.                     //sfd.OpenFile();
  1310.                 }                
  1311.                 return result;
  1312.             }            
  1313.             return false;
  1314.         }
  1315.         public static bool exportAlumnosToExcel(MetroFramework.Controls.MetroGrid grid, string tituloReporte,string header, bool corteCaja)
  1316.         {
  1317.             if (grid.RowCount > 0)
  1318.             {
  1319.                 SaveFileDialog sfd = new SaveFileDialog();
  1320.                 sfd.Title = "Export To Excel";
  1321.                 sfd.Filter = "To Excel (Xlsx)|*.xlsx";
  1322.                 sfd.FileName = "*.xlsx";
  1323.                 bool result = false;
  1324.                 if (sfd.ShowDialog() == DialogResult.OK)
  1325.                 {
  1326.                     string filename = sfd.FileName;
  1327.                     try
  1328.                     {
  1329.                         string Range_Letter = Alphabit[grid.Columns.Count];
  1330.                         string Range_Row = (grid.Rows.Count + 1).ToString();
  1331.  
  1332.                         if (File.Exists(filename))
  1333.                         {
  1334.                             File.Delete(filename);
  1335.                         }
  1336.  
  1337.                         Microsoft.Office.Interop.Excel.Application oApp;
  1338.                         Microsoft.Office.Interop.Excel.Worksheet oSheet;
  1339.                         Microsoft.Office.Interop.Excel.Workbook oBook;
  1340.  
  1341.                         oApp = new Microsoft.Office.Interop.Excel.Application();
  1342.                         oBook = oApp.Workbooks.Add();
  1343.                         oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oBook.Worksheets.get_Item(1);
  1344.  
  1345.                         int columns = grid.Columns.Count;
  1346.                         for (int x = 0; x < columns; x++)
  1347.                         {
  1348.                             // creating Columns :
  1349.                             oSheet.Cells[1, x + 2] = grid.Columns[x].HeaderText;
  1350.                         }
  1351.                         oSheet.Cells.NumberFormat = "@";
  1352.                         int rows = grid.Rows.Count;
  1353.                         for (int i = 0; i < columns; i++)
  1354.                         {
  1355.                             for (int j = 0; j < rows; j++)
  1356.                             {
  1357.                                 // creating rows :                                
  1358.                                 oSheet.Cells[j + 2, i + 2] = grid.Rows[j].Cells[i].Value.ToString();
  1359.                             }
  1360.                         }
  1361.                         //Add some formatting
  1362.                         Range rng1 = oSheet.get_Range("B1", Range_Letter + "1");
  1363.                         rng1.WrapText = true;
  1364.                         rng1.Font.Size = 8;
  1365.                         rng1.Font.Bold = true;
  1366.                         //rng1.Cells.Borders.LineStyle = XlLineStyle.xlDouble;
  1367.                         rng1.Cells.Borders.Color = System.Drawing.Color.Black;
  1368.                         rng1.Font.Color = System.Drawing.Color.Black;
  1369.                         rng1.HorizontalAlignment = XlHAlign.xlHAlignCenter;
  1370.                         //rng1.Interior.Color = System.Drawing.Color.LightGray;
  1371.  
  1372.                         Range rng2 = oSheet.get_Range("B2", Range_Letter + Range_Row);
  1373.                         rng2.WrapText = true;
  1374.                         rng2.Font.Size = 6;
  1375.                         rng2.Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
  1376.                         rng2.Cells.Borders.Color = System.Drawing.Color.Black;
  1377.                         rng2.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1378.                         //rng2.Interior.Color = System.Drawing.Color.Azure;
  1379.                         rng2.EntireColumn.AutoFit();
  1380.                         rng2.EntireRow.AutoFit();
  1381.                         string range = "2";
  1382.                         if (header.Length > 0)
  1383.                         {
  1384.                             range = "3";
  1385.                         }
  1386.                         //Add a header row
  1387.                         oSheet.get_Range("B1", Range_Letter + range).EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
  1388.                        
  1389.                         if (!corteCaja)
  1390.                         {
  1391.                             if (header.Length > 0)
  1392.                             {
  1393.                                 oSheet.Cells[1, 3] = "Fecha del Reporte:  " + DateTime.Now.ToString("dd/MM/yyy h:mm tt") + "\r\n" + header;                                
  1394.                             }
  1395.                             else
  1396.                             {
  1397.                                 oSheet.Cells[1, 2] = "Fecha del Reporte:  " + DateTime.Now.ToString("dd/MM/yyy h:mm tt");
  1398.                             }
  1399.                         }
  1400.                         else
  1401.                         {
  1402.                             oSheet.Cells[1, 2] = "Fecha del Corte:  " + DateTime.Now.ToString("dd/MMMM/yyy");
  1403.                         }
  1404.                         Range rng3 = oSheet.get_Range("B1", Range_Letter + range);
  1405.                         rng3.WrapText = true;
  1406.                         rng3.Merge(Missing.Value);
  1407.                         rng3.Font.Size = 11;
  1408.                         rng3.Font.Color = System.Drawing.Color.Black;
  1409.                         rng3.Font.Bold = true;
  1410.                         rng3.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1411.                         rng3.EntireColumn.AutoFit();
  1412.                         rng3.EntireRow.AutoFit();
  1413.                         //SetRowHeight(ref rng3);
  1414.  
  1415.                         oSheet.get_Range("B1", Range_Letter + range).EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
  1416.                         oSheet.Cells[1, 3] = tituloReporte;
  1417.                         Range rng4 = oSheet.get_Range("B1", Range_Letter + range);
  1418.                         rng4.WrapText = true;
  1419.                         rng4.Merge(Missing.Value);
  1420.                         rng4.Font.Size = 16;
  1421.                         rng4.Font.Color = System.Drawing.Color.Black;
  1422.                         rng4.Font.Bold = true;
  1423.                         rng4.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1424.                         rng4.HorizontalAlignment = XlVAlign.xlVAlignCenter;
  1425.                         Microsoft.Office.Interop.Excel.Range oRange = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[1, 2];
  1426.                         Bitmap image = new Bitmap((Bitmap)Properties.Resources.uniitec, 150, 80);
  1427.                         System.Windows.Forms.Clipboard.SetDataObject(image, true);
  1428.                         oSheet.Paste(oRange, image);
  1429.                         //rng4.EntireColumn.AutoFit();
  1430.                         //rng4.EntireRow.AutoFit();
  1431.                         // SetRowHeight(ref rng4);
  1432.                         // oSheet.Cells.Columns.EntireColumn.AutoFit();
  1433.                         //oSheet.Rows.AutoFit();
  1434.                         //oSheet.Range["B1", Range_Letter + "2"].AutoFit();                        
  1435.  
  1436.                         oBook.SaveAs(filename);
  1437.                         oBook.Close();
  1438.                         oApp.Quit();
  1439.                         result = true;
  1440.                     }
  1441.                     catch (Exception e)
  1442.                     {
  1443.                         System.Diagnostics.Debug.WriteLine("ERROR AL EXPORTAR A EXCEL:" + e.ToString());
  1444.                     }
  1445.                     //sfd.OpenFile();
  1446.                 }
  1447.                 sfd.Dispose();
  1448.                 return result;
  1449.             }
  1450.             else
  1451.             {
  1452.                 System.Windows.Forms.MessageBox.Show("No hay ningún registro a exportar.", Global.STR_NOMBRE_SISTEMA, MessageBoxButtons.OK, MessageBoxIcon.Information);
  1453.             }
  1454.             return false;
  1455.         }
  1456.  
  1457.         public static bool exportToExcel2(MetroFramework.Controls.MetroGrid grid, string tituloReporte, bool corteCaja)
  1458.         {
  1459.             if (grid.RowCount > 0)
  1460.             {
  1461.                 SaveFileDialog sfd = new SaveFileDialog();
  1462.                 sfd.Title = "Export To Excel";
  1463.                 sfd.Filter = "To Excel (Xlsx)|*.xlsx";
  1464.                 sfd.FileName = "*.xlsx";
  1465.                 bool result = false;
  1466.                 if (sfd.ShowDialog() == DialogResult.OK)
  1467.                 {
  1468.                     string filename = sfd.FileName;
  1469.                     try
  1470.                     {
  1471.                         string Range_Letter = Alphabit[grid.Columns.Count];
  1472.                         string Range_Row = (grid.Rows.Count + 1).ToString();
  1473.  
  1474.                         if (File.Exists(filename))
  1475.                         {
  1476.                             File.Delete(filename);
  1477.                         }
  1478.  
  1479.                         Microsoft.Office.Interop.Excel.Application oApp;
  1480.                         Microsoft.Office.Interop.Excel.Worksheet oSheet;
  1481.                         Microsoft.Office.Interop.Excel.Workbook oBook;
  1482.  
  1483.                         oApp = new Microsoft.Office.Interop.Excel.Application();
  1484.                         oBook = oApp.Workbooks.Add();
  1485.                         oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oBook.Worksheets.get_Item(1);
  1486.  
  1487.                         int columns = grid.Columns.Count;
  1488.                         for (int x = 0; x < columns; x++)
  1489.                         {
  1490.                             // creating Columns :
  1491.                             oSheet.Cells[1, x + 2] = grid.Columns[x].HeaderText;                            
  1492.                         }
  1493.                         oSheet.Cells.NumberFormat = "@";
  1494.                         int rows = grid.Rows.Count;
  1495.                         for (int i = 0; i < columns; i++)
  1496.                         {
  1497.                             for (int j = 0; j < rows; j++)
  1498.                             {
  1499.                                 // creating rows :
  1500.                                 oSheet.Cells[j + 2, i + 2] = grid.Rows[j].Cells[i].Value.ToString();
  1501.                             }
  1502.                         }
  1503.                         //Add some formatting
  1504.                         Range rng1 = oSheet.get_Range("B1", Range_Letter + "1");
  1505.                         rng1.WrapText = true;
  1506.                         rng1.Font.Size = 7;
  1507.                         rng1.Font.Bold = true;
  1508.                         //rng1.Cells.Borders.LineStyle = XlLineStyle.xlDouble;
  1509.                         rng1.Cells.Borders.Color = System.Drawing.Color.Black;
  1510.                         rng1.Font.Color = System.Drawing.Color.Black;
  1511.                         rng1.HorizontalAlignment = XlHAlign.xlHAlignCenter;
  1512.                         //rng1.Interior.Color = System.Drawing.Color.LightGray;
  1513.  
  1514.                         Range rng2 = oSheet.get_Range("B2", Range_Letter + Range_Row);
  1515.                         rng2.WrapText = true;
  1516.                         rng2.Font.Size = 6;
  1517.                         rng2.Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
  1518.                         rng2.Cells.Borders.Color = System.Drawing.Color.Black;
  1519.                         rng2.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1520.                         //rng2.Interior.Color = System.Drawing.Color.Azure;
  1521.                         rng2.EntireColumn.AutoFit();
  1522.                         rng2.EntireRow.AutoFit();
  1523.  
  1524.                         //Add a header row
  1525.                         oSheet.get_Range("B1", Range_Letter + "2").EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
  1526.                         if (!corteCaja)
  1527.                         {
  1528.                             oSheet.Cells[1, 2] = "Fecha del Reporte:  " + DateTime.Now.ToString("dd/MM/yyy h:mm tt");
  1529.                         }
  1530.                         else
  1531.                         {
  1532.                             oSheet.Cells[1, 2] = "Fecha del Corte:  " + DateTime.Now.ToString("dd/MMMM/yyy");
  1533.                         }
  1534.                         Range rng3 = oSheet.get_Range("B1", Range_Letter + "2");
  1535.                         rng3.WrapText = true;
  1536.                         rng3.Merge(Missing.Value);
  1537.                         rng3.Font.Size = 11;
  1538.                         rng3.Font.Color = System.Drawing.Color.Black;
  1539.                         rng3.Font.Bold = true;
  1540.                         rng3.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1541.                         rng3.EntireColumn.AutoFit();
  1542.                         rng3.EntireRow.AutoFit();
  1543.                         //SetRowHeight(ref rng3);
  1544.  
  1545.                         oSheet.get_Range("B1", Range_Letter + "2").EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
  1546.                         oSheet.Cells[1, 3] = tituloReporte;
  1547.                         Range rng4 = oSheet.get_Range("B1", Range_Letter + "2");
  1548.                         rng4.WrapText = true;
  1549.                         rng4.Merge(Missing.Value);
  1550.                         rng4.Font.Size = 16;
  1551.                         rng4.Font.Color = System.Drawing.Color.Black;
  1552.                         rng4.Font.Bold = true;
  1553.                         rng4.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1554.                         rng4.HorizontalAlignment = XlVAlign.xlVAlignCenter;
  1555.                         Microsoft.Office.Interop.Excel.Range oRange = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[1, 2];
  1556.                         System.Windows.Forms.Clipboard.SetDataObject((Bitmap)Properties.Resources.itecExport, true);
  1557.                         oSheet.Paste(oRange, (Bitmap)Properties.Resources.itecExport);
  1558.                         //rng4.EntireColumn.AutoFit();
  1559.                         //rng4.EntireRow.AutoFit();
  1560.                         // SetRowHeight(ref rng4);
  1561.                         // oSheet.Cells.Columns.EntireColumn.AutoFit();
  1562.                         //oSheet.Rows.AutoFit();
  1563.                         //oSheet.Range["B1", Range_Letter + "2"].AutoFit();                        
  1564.  
  1565.                         oBook.SaveAs(filename);
  1566.                         oBook.Close();
  1567.                         oApp.Quit();
  1568.                         result = true;
  1569.                     }
  1570.                     catch (Exception e)
  1571.                     {
  1572.                         System.Diagnostics.Debug.WriteLine("ERROR AL EXPORTAR A EXCEL:" + e.ToString());
  1573.                     }
  1574.                     //sfd.OpenFile();
  1575.                 }
  1576.                 sfd.Dispose();
  1577.                 return result;
  1578.             }
  1579.             else
  1580.             {
  1581.                 System.Windows.Forms.MessageBox.Show("No hay ningún registro a exportar.", Global.STR_NOMBRE_SISTEMA, MessageBoxButtons.OK, MessageBoxIcon.Information);
  1582.             }
  1583.             return false;
  1584.         }
  1585.         private static void SetRowHeight(ref Microsoft.Office.Interop.Excel.Range myMergedCells)
  1586.         {
  1587.             // autofit does not set the row height if the row contains merged cells.
  1588.             // this is adapted from a post by Peer Moretti on MSDN forums.
  1589.             Microsoft.Office.Interop.Excel.Range mySingleCell = myMergedCells.get_Offset(0, 100);
  1590.             double dblPointsWidth;
  1591.             // set our variables to the ranges concerned
  1592.             // and make sure the single cell has exactly the same font style, size, etc.
  1593.             // work out the width of the merged cells in points
  1594.             // this requires myMergedCells to be all the columns
  1595.             dblPointsWidth = 0;
  1596.             mySingleCell.Font.Name = myMergedCells.Font.Name;
  1597.             mySingleCell.Font.Size = myMergedCells.Font.Size;
  1598.             mySingleCell.Font.Bold = myMergedCells.Font.Bold;
  1599.             foreach (Microsoft.Office.Interop.Excel.Range myColumn in myMergedCells.Columns)
  1600.             {
  1601.                 dblPointsWidth = dblPointsWidth + (double)myColumn.Width;
  1602.             }
  1603.             // set the single cell to the same width as the merged cells in points
  1604.            // mySingleCell.ColumnWidth = (double)mySingleCell.ColumnWidth * dblPointsWidth / (double)mySingleCell.Width;
  1605.             // put the information from the merged cell in to the single cell
  1606.             mySingleCell.Value2 = myMergedCells.Value2;
  1607.             // now autofit
  1608.             //mySingleCell.Rows.AutoFit();
  1609.             mySingleCell.Columns.AutoFit();
  1610.             mySingleCell.Columns.Hidden = true;
  1611.             // set the height of the merged cell to the height of the single cell
  1612.             //myMergedCells.RowHeight = mySingleCell.RowHeight;
  1613.             myMergedCells.ColumnWidth = mySingleCell.ColumnWidth;
  1614.         }
  1615.  
  1616.         public static bool imprimirArchivo(MetroFramework.Controls.MetroGrid grid, string impresora, string tituloReporte, bool corteCaja)
  1617.         {
  1618.             if (grid.RowCount > 0)
  1619.             {
  1620.  
  1621.                 if (impresora.Length > 0)
  1622.                 {
  1623.                     try
  1624.                     {
  1625.                         string Range_Letter = Alphabit[grid.Columns.Count];
  1626.                         string Range_Row = (grid.Rows.Count + 1).ToString();
  1627.                         Microsoft.Office.Interop.Excel.Application oApp;
  1628.                         Microsoft.Office.Interop.Excel.Worksheet oSheet;
  1629.                         Microsoft.Office.Interop.Excel.Workbook oBook;
  1630.  
  1631.                         oApp = new Microsoft.Office.Interop.Excel.Application();
  1632.                         oBook = oApp.Workbooks.Add();
  1633.                         oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oBook.Worksheets.get_Item(1);
  1634.  
  1635.                         oSheet.Columns.WrapText = true;
  1636.  
  1637.                         int columns = grid.Columns.Count;
  1638.                         for (int x = 0; x < columns; x++)
  1639.                         {
  1640.                             // creating Columns :
  1641.                             oSheet.Cells[1, x + 2] = grid.Columns[x].HeaderText;
  1642.                         }
  1643.                         oSheet.Cells.NumberFormat = "@";                        
  1644.                         int rows = grid.Rows.Count;
  1645.                         for (int i = 0; i < columns; i++)
  1646.                         {
  1647.                             for (int j = 0; j < rows; j++)
  1648.                             {
  1649.                                 // creating rows :
  1650.                                 oSheet.Cells[j + 2, i + 2] = grid.Rows[j].Cells[i].Value;
  1651.                             }
  1652.                         }
  1653.                         //Add some formatting                        
  1654.                         Range rng1 = oSheet.get_Range("B1", Range_Letter + "1");
  1655.                         rng1.WrapText = true;
  1656.                         rng1.Font.Size = 8;
  1657.                         rng1.Font.Bold = true;
  1658.                         //rng1.Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
  1659.                         rng1.Cells.Borders.Color = System.Drawing.Color.Black;
  1660.                         rng1.Font.Color = System.Drawing.Color.Black;
  1661.                         rng1.HorizontalAlignment = XlHAlign.xlHAlignCenter;
  1662.                        
  1663.                         Range rng2 = null;
  1664.                         if (Range_Letter.CompareTo("J") == 0)
  1665.                         {
  1666.                             rng2 = oSheet.get_Range("B2", "D" + Range_Row);
  1667.                             Range rngConcepto = rng2.Columns[8];
  1668.                             rng2.WrapText = false;
  1669.                             rng2.Font.Size = 6;
  1670.                             rng2.Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
  1671.                             rng2.Cells.Borders.Color = System.Drawing.Color.Black;
  1672.                             rng2.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1673.                             rng2.EntireColumn.AutoFit();
  1674.                             rng2.EntireRow.AutoFit();
  1675.  
  1676.                             Range rngconcepto = oSheet.get_Range("E2", "J" + Range_Row);
  1677.                             rngconcepto.WrapText = true;
  1678.                             rngconcepto.Font.Size = 6;
  1679.                             rngconcepto.Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
  1680.                             rngconcepto.Cells.Borders.Color = System.Drawing.Color.Black;
  1681.                             rngconcepto.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1682.                             rngconcepto.EntireColumn.AutoFit();
  1683.                             rngconcepto.EntireRow.AutoFit();
  1684.                         }
  1685.                         else
  1686.                         {
  1687.                             rng2 = oSheet.get_Range("B2", Range_Letter + Range_Row);
  1688.                             rng2.WrapText = false;
  1689.                             rng2.Font.Size = 6;
  1690.                             rng2.Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
  1691.                             rng2.Cells.Borders.Color = System.Drawing.Color.Black;
  1692.                             rng2.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1693.                             rng2.EntireColumn.AutoFit();
  1694.                             rng2.EntireRow.AutoFit();
  1695.                         }
  1696.                         oSheet.PageSetup.Orientation = XlPageOrientation.xlLandscape;
  1697.                         if (corteCaja)                        
  1698.                         {                            
  1699.                             oSheet.get_Range("B1", Range_Letter + "2").EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
  1700.  
  1701.                             oSheet.Cells[1, 2] = "Fecha del Corte:  " + DateTime.Now.ToString("dd/MMMM/yyy");
  1702.  
  1703.                             Range rng3 = oSheet.get_Range("B1", Range_Letter + "2");
  1704.                             rng3.WrapText = true;
  1705.                             rng3.Merge(Missing.Value);
  1706.                             rng3.Font.Size = 11;
  1707.                             rng3.Font.Color = System.Drawing.Color.Black;
  1708.                             rng3.Font.Bold = true;
  1709.                             rng3.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1710.                             rng3.EntireColumn.AutoFit();
  1711.                             rng3.EntireRow.AutoFit();
  1712.                         }
  1713.                         //Add a header row
  1714.                         oSheet.get_Range("B1", Range_Letter + "2").EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
  1715.                         oSheet.Cells[1, 3] = tituloReporte;
  1716.                         Range rng4 = oSheet.get_Range("B1", Range_Letter + "2");
  1717.                         rng4.Merge(Missing.Value);
  1718.                         rng4.WrapText = true;
  1719.                         rng4.Font.Size = 14;
  1720.                         rng4.Font.Color = System.Drawing.Color.Black;
  1721.                         rng4.Font.Bold = true;
  1722.                         rng4.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1723.                         rng4.HorizontalAlignment = XlVAlign.xlVAlignCenter;
  1724.                         Microsoft.Office.Interop.Excel.Range oRange = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[1, 2];
  1725.                         if (corteCaja)
  1726.                         {
  1727.                             Bitmap image = new Bitmap((Bitmap)Properties.Resources.uniitec, 150, 80);
  1728.                             System.Windows.Forms.Clipboard.SetDataObject(image, true);
  1729.                             oSheet.Paste(oRange, image);
  1730.                         }
  1731.                         else
  1732.                         {
  1733.                             Bitmap image = new Bitmap((Bitmap)Properties.Resources.uniitec, 150, 40);
  1734.                             System.Windows.Forms.Clipboard.SetDataObject(image, true);
  1735.                             oSheet.Paste(oRange, image);
  1736.                         }
  1737.                         //rng3.Interior.Color = System.Drawing.Color.LightSkyBlue;
  1738.                         string filename = crearDirectorioImpresiones() + "\\" + DateTime.Now.Ticks.ToString() + ".xlsx";
  1739.                         if (!File.Exists(filename))
  1740.                         {
  1741.                             oBook.SaveAs(filename);
  1742.                         }
  1743.                         oBook.PrintOutEx(Type.Missing, Type.Missing, Type.Missing, Type.Missing, impresora, Type.Missing, Type.Missing, Type.Missing);
  1744.                         oBook.Close();
  1745.                         oApp.Quit();
  1746.                         try
  1747.                         {
  1748.                             if (File.Exists(filename))
  1749.                             {
  1750.                                 File.Delete(filename);
  1751.                             }
  1752.                         }
  1753.                         catch (Exception) { }
  1754.                         return true;
  1755.                     }
  1756.                     catch (Exception e)
  1757.                     {
  1758.                         System.Diagnostics.Debug.WriteLine("ERROR AL EXPORTAR A EXCEL:" + e.ToString());
  1759.                     }
  1760.                 }
  1761.                 else
  1762.                 {
  1763.                     System.Windows.Forms.MessageBox.Show("No hay ningúna impresora seleccionada.\r\nFavor de seleccionar una impresora.", Global.STR_NOMBRE_SISTEMA, MessageBoxButtons.OK, MessageBoxIcon.Information);
  1764.                 }
  1765.             }
  1766.             else
  1767.             {
  1768.                 System.Windows.Forms.MessageBox.Show("No hay ningún registro para imprimir.", Global.STR_NOMBRE_SISTEMA, MessageBoxButtons.OK, MessageBoxIcon.Information);
  1769.             }
  1770.             return false;
  1771.         }
  1772.  
  1773.         public static bool imprimirCorteCaja(MetroFramework.Controls.MetroGrid grid, string impresora, string tituloReporte, bool complementario)
  1774.         {
  1775.             if (grid.RowCount > 0)
  1776.             {
  1777.                 if (impresora.Length > 0)
  1778.                 {
  1779.                     try
  1780.                     {
  1781.                         string Range_Letter = Alphabit[grid.Columns.Count];
  1782.                         string Range_Row = (grid.Rows.Count + 1).ToString();
  1783.  
  1784.                         Microsoft.Office.Interop.Excel.Application oApp;
  1785.                         Microsoft.Office.Interop.Excel.Worksheet oSheet;
  1786.                         Microsoft.Office.Interop.Excel.Workbook oBook;
  1787.  
  1788.                         oApp = new Microsoft.Office.Interop.Excel.Application();
  1789.                         oBook = oApp.Workbooks.Add();
  1790.                         oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oBook.Worksheets.get_Item(1);
  1791.                         int columns = grid.Columns.Count;
  1792.                         oSheet.Cells.WrapText = true;
  1793.                         for (int x = 0; x < columns; x++)
  1794.                         {
  1795.                             // creating Columns :
  1796.                             oSheet.Cells[1, x + 2] = grid.Columns[x].HeaderText;
  1797.                         }
  1798.                         oSheet.Cells.NumberFormat = "@";
  1799.                         int rows = grid.Rows.Count;
  1800.                         for (int i = 0; i < columns; i++)
  1801.                         {
  1802.                             for (int j = 0; j < rows; j++)
  1803.                             {
  1804.                                 // creating rows :
  1805.                                 if (grid.Rows[j].Cells[i].Value != null)
  1806.                                 {
  1807.                                     oSheet.Cells[j + 2, i + 2] = grid.Rows[j].Cells[i].Value.ToString();                                    
  1808.                                 }
  1809.                             }
  1810.                         }
  1811.                         //Add some formatting
  1812.                         Range rng1 = oSheet.get_Range("B1", Range_Letter + "1");
  1813.                         rng1.WrapText = false;
  1814.                         rng1.Font.Size = 8;
  1815.                         rng1.Font.Bold = true;
  1816.                         //rng1.Cells.Borders.LineStyle = XlLineStyle.xlDouble;
  1817.                         rng1.Cells.Borders.Color = System.Drawing.Color.Black;
  1818.                         rng1.Font.Color = System.Drawing.Color.Black;
  1819.                         rng1.HorizontalAlignment = XlHAlign.xlHAlignCenter;
  1820.                         //rng1.Interior.Color = System.Drawing.Color.LightGray;
  1821.  
  1822.                         Range rng2 = oSheet.get_Range("B2", "G" + Range_Row);
  1823.                         Range rngConcepto = rng2.Columns[8];                        
  1824.                         rng2.WrapText = false;
  1825.                         rng2.Font.Size = 6;
  1826.                         rng2.Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
  1827.                         rng2.Cells.Borders.Color = System.Drawing.Color.Black;                        
  1828.                         rng2.VerticalAlignment = XlVAlign.xlVAlignCenter;                        
  1829.                         rng2.EntireColumn.AutoFit();
  1830.                         rng2.EntireRow.AutoFit();
  1831.                        
  1832.                         Range rngconcepto = oSheet.get_Range("B2", "H" + Range_Row);
  1833.                         rngconcepto.WrapText = true;
  1834.                         rngconcepto.Font.Size = 6;
  1835.                         rngconcepto.Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
  1836.                         rngconcepto.Cells.Borders.Color = System.Drawing.Color.Black;
  1837.                         rngconcepto.VerticalAlignment = XlVAlign.xlVAlignCenter;                        
  1838.                         rngconcepto.EntireColumn.AutoFit();
  1839.                         rngconcepto.EntireRow.AutoFit();
  1840.  
  1841.                         Range rngtotal = oSheet.get_Range("B2", "I" + Range_Row);
  1842.                         rngtotal.WrapText = true;
  1843.                         rngtotal.Font.Size = 6;
  1844.                         rngtotal.Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
  1845.                         rngtotal.Cells.Borders.Color = System.Drawing.Color.Black;
  1846.  
  1847.                         rngtotal.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1848.                         rngtotal.EntireColumn.AutoFit();
  1849.                         rngtotal.EntireRow.AutoFit();
  1850.  
  1851.  
  1852.                         //Add a header row
  1853.                         oSheet.PageSetup.Orientation = XlPageOrientation.xlLandscape;
  1854.                         oSheet.get_Range("B1", Range_Letter + "2").EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);                        
  1855.                         oSheet.Cells[1, 2] = "Fecha:  " + DateTime.Now.ToString("dd/MM/yyy h:mm tt");                                                
  1856.                         Range rng3 = oSheet.get_Range("B1", Range_Letter + "2");
  1857.                         //rng3.WrapText = true;
  1858.                         rng3.Merge(Missing.Value);
  1859.                         rng3.Font.Size = 11;
  1860.                         rng3.Font.Color = System.Drawing.Color.Black;
  1861.                         rng3.Font.Bold = true;
  1862.                         rng3.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1863.                         rng3.HorizontalAlignment = XlVAlign.xlVAlignCenter;
  1864.                         rng3.EntireColumn.AutoFit();
  1865.                         rng3.EntireRow.AutoFit();
  1866.                         //SetRowHeight(ref rng3);
  1867.  
  1868.                         oSheet.get_Range("B1", Range_Letter + "2").EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
  1869.                         oSheet.Cells[1, 3] = tituloReporte;
  1870.                         Range rng4 = oSheet.get_Range("B1", Range_Letter + "2");
  1871.                         //rng4.WrapText = true;
  1872.                         rng4.Merge(Missing.Value);
  1873.                         rng4.Font.Size = 16;
  1874.                         rng4.Font.Color = System.Drawing.Color.Black;
  1875.                         rng4.Font.Bold = true;
  1876.                         rng4.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1877.                         rng4.HorizontalAlignment = XlVAlign.xlVAlignCenter;
  1878.                         //Bitmap image = new Bitmap((Bitmap)Properties.Resources.itecExport, 100, 100);
  1879.                         Microsoft.Office.Interop.Excel.Range oRange = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[1, 2];
  1880.                         Bitmap image = new Bitmap((Bitmap)Properties.Resources.uniitec, 150, 80);
  1881.                         System.Windows.Forms.Clipboard.SetDataObject(image, true);
  1882.                         oSheet.Paste(oRange, image);
  1883.                         string filename = crearDirectorioImpresiones() + "\\" + DateTime.Now.Ticks.ToString() + ".xlsx";
  1884.                         if (!File.Exists(filename))
  1885.                         {
  1886.                             oBook.SaveAs(filename);
  1887.                         }
  1888.                         oBook.PrintOutEx(Type.Missing, Type.Missing, Type.Missing, Type.Missing, impresora, Type.Missing, Type.Missing, Type.Missing);
  1889.                         oBook.Close();
  1890.                         oApp.Quit();
  1891.                         try
  1892.                         {
  1893.                             if (File.Exists(filename))
  1894.                             {
  1895.                                 File.Delete(filename);
  1896.                             }
  1897.                         }
  1898.                         catch (Exception) { }
  1899.                         return true;
  1900.                     }
  1901.                     catch (Exception e)
  1902.                     {
  1903.                         System.Diagnostics.Debug.WriteLine("ERROR AL EXPORTAR A EXCEL:" + e.ToString());
  1904.                     }
  1905.                 }
  1906.                 else
  1907.                 {
  1908.                     System.Windows.Forms.MessageBox.Show("No hay ningúna impresora seleccionada.\r\nFavor de seleccionar una impresora.", Global.STR_NOMBRE_SISTEMA, MessageBoxButtons.OK, MessageBoxIcon.Information);
  1909.                 }
  1910.             }
  1911.             else
  1912.             {
  1913.                 System.Windows.Forms.MessageBox.Show("No hay ningún registro para imprimir.", Global.STR_NOMBRE_SISTEMA, MessageBoxButtons.OK, MessageBoxIcon.Information);
  1914.             }
  1915.             return false;
  1916.         }
  1917.  
  1918.         //public static bool imprimirCorteCaja(MetroFramework.Controls.MetroGrid grid, string impresora, string tituloReporte, bool complementario)
  1919.         //{
  1920.         //    if (grid.RowCount > 0)
  1921.         //    {
  1922.  
  1923.         //        if (impresora.Length > 0)
  1924.         //        {
  1925.         //            try
  1926.         //            {
  1927.         //                string Range_Letter = Alphabit[grid.Columns.Count];
  1928.         //                string Range_Row = (grid.Rows.Count + 1).ToString();
  1929.         //                Microsoft.Office.Interop.Excel.Application oApp;
  1930.         //                Microsoft.Office.Interop.Excel.Worksheet oSheet;
  1931.         //                Microsoft.Office.Interop.Excel.Workbook oBook;
  1932.  
  1933.         //                oApp = new Microsoft.Office.Interop.Excel.Application();
  1934.         //                oBook = oApp.Workbooks.Add();
  1935.         //                oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oBook.Worksheets.get_Item(1);
  1936.  
  1937.         //                oSheet.Columns.WrapText = true;
  1938.  
  1939.         //                int columns = grid.Columns.Count;
  1940.         //                for (int x = 0; x < columns; x++)
  1941.         //                {
  1942.         //                    // creating Columns :
  1943.         //                    oSheet.Cells[1, x + 2] = grid.Columns[x].HeaderText;
  1944.         //                }
  1945.         //                oSheet.Cells.NumberFormat = "@";
  1946.         //                int rows = grid.Rows.Count;
  1947.         //                for (int i = 0; i < columns; i++)
  1948.         //                {
  1949.         //                    for (int j = 0; j < rows; j++)
  1950.         //                    {
  1951.         //                        // creating rows :
  1952.         //                        oSheet.Cells[j + 2, i + 2] = grid.Rows[j].Cells[i].Value;
  1953.         //                    }
  1954.         //                }
  1955.  
  1956.         //                //Add some formatting                        
  1957.         //                Range rng1 = oSheet.get_Range("B1", Range_Letter + "1");
  1958.         //                rng1.WrapText = true;
  1959.         //                rng1.Font.Size = 8;
  1960.         //                rng1.Font.Bold = true;
  1961.         //                //rng1.Cells.Borders.LineStyle = XlLineStyle.xlDouble;
  1962.         //                rng1.Cells.Borders.Color = System.Drawing.Color.Black;
  1963.         //                rng1.Font.Color = System.Drawing.Color.Black;
  1964.         //                rng1.HorizontalAlignment = XlHAlign.xlHAlignCenter;
  1965.         //                //rng1.Interior.Color = System.Drawing.Color.LightGray;
  1966.  
  1967.         //                Range rng2 = oSheet.get_Range("B2", Range_Letter + Range_Row);
  1968.         //                rng2.WrapText = false;
  1969.         //                rng2.Font.Size = 6;
  1970.         //                rng2.Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
  1971.         //                //rng2.Cells.Borders.Color = System.Drawing.Color.DeepSkyBlue;
  1972.         //                rng2.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1973.         //                //rng2.Interior.Color = System.Drawing.Color.Azure;
  1974.         //                rng2.EntireColumn.AutoFit();
  1975.         //                rng2.EntireRow.AutoFit();
  1976.  
  1977.         //                oSheet.PageSetup.Orientation = XlPageOrientation.xlLandscape;
  1978.         //                oSheet.get_Range("B1", Range_Letter + "2").EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
  1979.         //                if (complementario)
  1980.         //                {
  1981.         //                    oSheet.Cells[1, 2] = "Fecha del Corte COmplementario:  " + DateTime.Now.ToString("dd/MMMM/yyy");
  1982.         //                }
  1983.         //                else
  1984.         //                {
  1985.         //                    oSheet.Cells[1, 2] = "Fecha del Corte:  " + DateTime.Now.ToString("dd/MMMM/yyy");
  1986.         //                }
  1987.         //                Range rng3 = oSheet.get_Range("B1", Range_Letter + "2");
  1988.         //                rng3.WrapText = true;
  1989.         //                rng3.Merge(Missing.Value);
  1990.         //                rng3.Font.Size = 11;
  1991.         //                rng3.Font.Color = System.Drawing.Color.Black;
  1992.         //                rng3.Font.Bold = true;
  1993.         //                rng3.VerticalAlignment = XlVAlign.xlVAlignCenter;
  1994.         //                rng3.EntireColumn.AutoFit();
  1995.         //                rng3.EntireRow.AutoFit();
  1996.         //                //Add a header row
  1997.         //                oSheet.get_Range("B1", Range_Letter + "2").EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
  1998.         //                oSheet.Cells[1, 3] = tituloReporte;
  1999.         //                Range rng4 = oSheet.get_Range("B1", Range_Letter + "2");
  2000.         //                rng4.Merge(Missing.Value);
  2001.         //                rng4.WrapText = true;
  2002.         //                rng4.Font.Size = 14;
  2003.         //                rng4.Font.Color = System.Drawing.Color.Black;
  2004.         //                rng4.Font.Bold = true;
  2005.         //                rng4.VerticalAlignment = XlVAlign.xlVAlignCenter;
  2006.         //                Microsoft.Office.Interop.Excel.Range oRange = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[1, 2];
  2007.         //                Bitmap image = new Bitmap((Bitmap)Properties.Resources.itecExport, 84,84);
  2008.         //                System.Windows.Forms.Clipboard.SetDataObject(image, true);
  2009.         //                oSheet.Paste(oRange, image);
  2010.         //                string filename = crearDirectorioImpresiones() + "\\" + DateTime.Now.Ticks.ToString() + ".xlsx";
  2011.         //                if (!File.Exists(filename))
  2012.         //                {
  2013.         //                    oBook.SaveAs(filename);
  2014.         //                }
  2015.         //                oBook.PrintOutEx(Type.Missing, Type.Missing, Type.Missing, Type.Missing, impresora, Type.Missing, Type.Missing, Type.Missing);
  2016.         //                oBook.Close();
  2017.         //                oApp.Quit();
  2018.         //                try
  2019.         //                {
  2020.         //                    if (File.Exists(filename))
  2021.         //                    {
  2022.         //                        File.Delete(filename);
  2023.         //                    }
  2024.         //                }
  2025.         //                catch (Exception) { }
  2026.         //                return true;
  2027.         //            }
  2028.         //            catch (Exception e)
  2029.         //            {
  2030.         //                System.Diagnostics.Debug.WriteLine("ERROR AL EXPORTAR A EXCEL:" + e.ToString());
  2031.         //            }
  2032.         //        }
  2033.         //        else
  2034.         //        {
  2035.         //            System.Windows.Forms.MessageBox.Show("No hay ningúna impresora seleccionada.\r\nFavor de seleccionar una impresora.", Global.STR_NOMBRE_SISTEMA, MessageBoxButtons.OK, MessageBoxIcon.Information);
  2036.         //        }
  2037.         //    }
  2038.         //    else
  2039.         //    {
  2040.         //        System.Windows.Forms.MessageBox.Show("No hay ningún registro para imprimir.", Global.STR_NOMBRE_SISTEMA, MessageBoxButtons.OK, MessageBoxIcon.Information);
  2041.         //    }
  2042.         //    return false;
  2043.         //}
  2044.         public static bool imprimirArchivoAlumnos(MetroFramework.Controls.MetroGrid grid, string impresora, string tituloReporte, string header, bool corteCaja)
  2045.         {
  2046.             if (grid.RowCount > 0)
  2047.             {
  2048.  
  2049.                 if (impresora.Length > 0)
  2050.                 {
  2051.                     try
  2052.                     {
  2053.                         string Range_Letter = Alphabit[grid.Columns.Count];
  2054.                         string Range_Row = (grid.Rows.Count + 1).ToString();
  2055.                         Microsoft.Office.Interop.Excel.Application oApp;
  2056.                         Microsoft.Office.Interop.Excel.Worksheet oSheet;
  2057.                         Microsoft.Office.Interop.Excel.Workbook oBook;
  2058.  
  2059.                         oApp = new Microsoft.Office.Interop.Excel.Application();
  2060.                         oBook = oApp.Workbooks.Add();
  2061.                         oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oBook.Worksheets.get_Item(1);
  2062.  
  2063.                         int columns = grid.Columns.Count;
  2064.                         for (int x = 0; x < columns; x++)
  2065.                         {
  2066.                             // creating Columns :
  2067.                             oSheet.Cells[1, x + 2] = grid.Columns[x].HeaderText;
  2068.                         }
  2069.                         oSheet.Cells.NumberFormat = "@";
  2070.                         int rows = grid.Rows.Count;
  2071.                         for (int i = 0; i < columns; i++)
  2072.                         {
  2073.                             for (int j = 0; j < rows; j++)
  2074.                             {
  2075.                                 // creating rows :
  2076.                                 oSheet.Cells[j + 2, i + 2] = grid.Rows[j].Cells[i].Value;
  2077.                             }
  2078.                         }
  2079.  
  2080.                         //Add some formatting
  2081.                         Range rng1 = oSheet.get_Range("B1", Range_Letter + "1");
  2082.                         rng1.Font.Size = 10;
  2083.                         rng1.Font.Bold = true;
  2084.                         rng1.Cells.Borders.LineStyle = XlLineStyle.xlDouble;
  2085.                         //rng1.Cells.Borders.Color = System.Drawing.Color.DeepSkyBlue;
  2086.                         rng1.Font.Color = System.Drawing.Color.Black;
  2087.                         rng1.HorizontalAlignment = XlHAlign.xlHAlignCenter;
  2088.                         //rng1.Interior.Color = System.Drawing.Color.LightGray;
  2089.  
  2090.                         Range rng2 = oSheet.get_Range("B2", Range_Letter + Range_Row);
  2091.                         rng2.WrapText = false;
  2092.                         rng2.Font.Size = 9;
  2093.                         rng2.Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
  2094.                         //rng2.Cells.Borders.Color = System.Drawing.Color.DeepSkyBlue;
  2095.                         rng2.VerticalAlignment = XlVAlign.xlVAlignCenter;
  2096.                         //rng2.Interior.Color = System.Drawing.Color.Azure;
  2097.                         rng2.EntireColumn.AutoFit();
  2098.                         rng2.EntireRow.AutoFit();
  2099.  
  2100.                         oSheet.PageSetup.Orientation = XlPageOrientation.xlLandscape;
  2101.                         string range = "2";
  2102.                         if (header.Length > 0)
  2103.                         {
  2104.                             range = "3";
  2105.                         }
  2106.                         oSheet.get_Range("B1", Range_Letter + range).EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
  2107.                         if (corteCaja)
  2108.                         {
  2109.                             //Add a header row                            
  2110.                             oSheet.Cells[1, 2] = "Fecha del Corte:  " + DateTime.Now.ToString("dd/MMMM/yyy");                            
  2111.                         }
  2112.                         else
  2113.                         {
  2114.                             if (header.Length > 0)
  2115.                             {
  2116.                                 oSheet.Cells[1, 3] = "Fecha del Reporte:  " + DateTime.Now.ToString("dd/MM/yyy h:mm tt") + "\r\n" + header;
  2117.                             }
  2118.                             else
  2119.                             {
  2120.                                 oSheet.Cells[1, 2] = "Fecha del Reporte:  " + DateTime.Now.ToString("dd/MM/yyy h:mm tt");
  2121.                             }
  2122.                         }
  2123.                         Range rng3 = oSheet.get_Range("B1", Range_Letter + range);
  2124.                         rng3.WrapText = true;
  2125.                         rng3.Merge(Missing.Value);
  2126.                         rng3.Font.Size = 11;
  2127.                         rng3.Font.Color = System.Drawing.Color.Black;
  2128.                         rng3.Font.Bold = true;
  2129.                         rng3.VerticalAlignment = XlVAlign.xlVAlignCenter;
  2130.                         rng3.EntireColumn.AutoFit();
  2131.                         rng3.EntireRow.AutoFit();
  2132.  
  2133.                         //Add a header row
  2134.                         oSheet.get_Range("B1", Range_Letter + range).EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
  2135.                         oSheet.Cells[1, 3] = tituloReporte;
  2136.                         Range rng4 = oSheet.get_Range("B1", Range_Letter + range);
  2137.                         rng4.Merge(Missing.Value);
  2138.                         rng4.WrapText = true;
  2139.                         rng4.Font.Size = 14;
  2140.                         rng4.Font.Color = System.Drawing.Color.Black;
  2141.                         rng4.Font.Bold = true;
  2142.                         rng4.VerticalAlignment = XlVAlign.xlVAlignCenter;
  2143.                         rng4.HorizontalAlignment = XlVAlign.xlVAlignCenter;
  2144.                         Microsoft.Office.Interop.Excel.Range oRange = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[1, 2];
  2145.                         Bitmap image = new Bitmap((Bitmap)Properties.Resources.uniitec, 150, 80);
  2146.                         System.Windows.Forms.Clipboard.SetDataObject(image, true);
  2147.                         oSheet.Paste(oRange, image);
  2148.                         //rng3.Interior.Color = System.Drawing.Color.LightSkyBlue;
  2149.                         string filename = crearDirectorioImpresiones() + "\\" + DateTime.Now.Ticks.ToString() + ".xlsx";
  2150.                         if (!File.Exists(filename))
  2151.                         {
  2152.                             oBook.SaveAs(filename);
  2153.                         }
  2154.                         oBook.PrintOutEx(Type.Missing, Type.Missing, Type.Missing, Type.Missing, impresora, Type.Missing, Type.Missing, Type.Missing);
  2155.                         oBook.Close();
  2156.                         oApp.Quit();
  2157.                         try
  2158.                         {
  2159.                             if (File.Exists(filename))
  2160.                             {
  2161.                                 File.Delete(filename);
  2162.                             }
  2163.                         }
  2164.                         catch (Exception) { }
  2165.                         return true;
  2166.                     }
  2167.                     catch (Exception e)
  2168.                     {
  2169.                         System.Diagnostics.Debug.WriteLine("ERROR AL EXPORTAR A EXCEL:" + e.ToString());
  2170.                     }
  2171.                 }
  2172.                 else
  2173.                 {
  2174.                     System.Windows.Forms.MessageBox.Show("No hay ningúna impresora seleccionada.\r\nFavor de seleccionar una impresora.", Global.STR_NOMBRE_SISTEMA, MessageBoxButtons.OK, MessageBoxIcon.Information);
  2175.                 }
  2176.             }
  2177.             else
  2178.             {
  2179.                 System.Windows.Forms.MessageBox.Show("No hay ningún registro para imprimir.", Global.STR_NOMBRE_SISTEMA, MessageBoxButtons.OK, MessageBoxIcon.Information);
  2180.             }
  2181.             return false;
  2182.         }
  2183.         private static string crearDirectorioImpresiones()
  2184.         {
  2185.             string strFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Universidad_Itec";
  2186.             string strDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Universidad_Itec";
  2187.             if (!System.IO.Directory.Exists(strDirectory))
  2188.             {
  2189.                 System.IO.Directory.CreateDirectory(strDirectory);
  2190.             }
  2191.             strDirectory = strDirectory + "\\Temp";
  2192.             if (!System.IO.Directory.Exists(strDirectory))
  2193.             {
  2194.                 System.IO.Directory.CreateDirectory(strDirectory);
  2195.             }
  2196.             return strDirectory;
  2197.         }
  2198.         private static string crearDirectorioCortesCaja()
  2199.         {
  2200.             string strFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Universidad_Itec";
  2201.             string strDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Universidad_Itec";
  2202.             if (!System.IO.Directory.Exists(strDirectory))
  2203.             {
  2204.                 System.IO.Directory.CreateDirectory(strDirectory);
  2205.             }
  2206.             strDirectory = strDirectory + "\\CortesCaja";
  2207.             if (!System.IO.Directory.Exists(strDirectory))
  2208.             {
  2209.                 System.IO.Directory.CreateDirectory(strDirectory);
  2210.             }
  2211.             return strDirectory;
  2212.         }
  2213.  
  2214.         public static string ObtenerDomicilio(string strCalle, string strNumero, string strNumeroInterior, string strColonia, string strCodigoPostal, string estado, string ciudad, string pais)
  2215.         {
  2216.             string strResultado = strCalle;
  2217.  
  2218.             if (strNumero != string.Empty)
  2219.             {
  2220.                 if (strResultado != string.Empty) { strResultado += " "; }
  2221.  
  2222.                 strResultado += "No. " + strNumero;
  2223.             }
  2224.  
  2225.             if (strNumeroInterior != string.Empty)
  2226.             {
  2227.                 if (strResultado != string.Empty) { strResultado += " "; }
  2228.  
  2229.                 strResultado += "Interior " + strNumeroInterior;
  2230.             }
  2231.  
  2232.             if (strColonia != string.Empty)
  2233.             {
  2234.                 if (strResultado != string.Empty) { strResultado += " "; }
  2235.  
  2236.                 strResultado += strColonia;
  2237.             }
  2238.  
  2239.             if (strCodigoPostal != string.Empty)
  2240.             {
  2241.                 if (strResultado != string.Empty) { strResultado += " "; }
  2242.  
  2243.                 strResultado += "C.P. " + strCodigoPostal;
  2244.             }
  2245.  
  2246.             if (ciudad != string.Empty)
  2247.             {
  2248.                 if (strResultado != string.Empty) { strResultado += " "; }
  2249.  
  2250.                 strResultado += ciudad + ", ";
  2251.             }
  2252.  
  2253.             if (estado != string.Empty)
  2254.             {
  2255.                 if (strResultado != string.Empty) { strResultado += " "; }
  2256.  
  2257.                 strResultado += estado;
  2258.             }
  2259.  
  2260.             if (pais != string.Empty)
  2261.             {
  2262.                 if (strResultado != string.Empty) { strResultado += " "; }
  2263.  
  2264.                 strResultado += pais;
  2265.             }
  2266.  
  2267.             return strResultado;
  2268.         }
  2269.  
  2270.         public static string NumeroALetras(string num)
  2271.         {
  2272.             string res, dec = "";
  2273.             Int64 entero;
  2274.             int decimales;
  2275.             double nro;
  2276.  
  2277.             try
  2278.             {
  2279.                 nro = StringToDouble(num);
  2280.             }
  2281.             catch
  2282.             {
  2283.                 return "";
  2284.             }
  2285.  
  2286.             entero = Convert.ToInt64(Math.Truncate(nro));
  2287.             decimales = Convert.ToInt32(Math.Round((nro - entero) * 100, 2));
  2288.  
  2289.             if (decimales < 10)
  2290.             {
  2291.                 dec = " PESOS CON 0" + decimales.ToString() + "/100 MN";
  2292.             }
  2293.             else
  2294.             {
  2295.                 dec = " PESOS CON " + decimales.ToString() + "/100 MN";
  2296.             }
  2297.  
  2298.             res = toText(Convert.ToDouble(entero)) + dec;
  2299.             return res;
  2300.         }
  2301.  
  2302.         private static string toText(double value)
  2303.         {
  2304.             string Num2Text = "";
  2305.             value = Math.Truncate(value);
  2306.  
  2307.             if (value == 0) Num2Text = "CERO";
  2308.             else if (value == 1) Num2Text = "UNO";
  2309.             else if (value == 2) Num2Text = "DOS";
  2310.             else if (value == 3) Num2Text = "TRES";
  2311.             else if (value == 4) Num2Text = "CUATRO";
  2312.             else if (value == 5) Num2Text = "CINCO";
  2313.             else if (value == 6) Num2Text = "SEIS";
  2314.             else if (value == 7) Num2Text = "SIETE";
  2315.             else if (value == 8) Num2Text = "OCHO";
  2316.             else if (value == 9) Num2Text = "NUEVE";
  2317.             else if (value == 10) Num2Text = "DIEZ";
  2318.             else if (value == 11) Num2Text = "ONCE";
  2319.             else if (value == 12) Num2Text = "DOCE";
  2320.             else if (value == 13) Num2Text = "TRECE";
  2321.             else if (value == 14) Num2Text = "CATORCE";
  2322.             else if (value == 15) Num2Text = "QUINCE";
  2323.             else if (value < 20) Num2Text = "DIECI" + toText(value - 10);
  2324.             else if (value == 20) Num2Text = "VEINTE";
  2325.             else if (value < 30) Num2Text = "VEINTI" + toText(value - 20);
  2326.             else if (value == 30) Num2Text = "TREINTA";
  2327.             else if (value == 40) Num2Text = "CUARENTA";
  2328.             else if (value == 50) Num2Text = "CINCUENTA";
  2329.             else if (value == 60) Num2Text = "SESENTA";
  2330.             else if (value == 70) Num2Text = "SETENTA";
  2331.             else if (value == 80) Num2Text = "OCHENTA";
  2332.             else if (value == 90) Num2Text = "NOVENTA";
  2333.             else if (value < 100) Num2Text = toText(Math.Truncate(value / 10) * 10) + " Y " + toText(value % 10);
  2334.             else if (value == 100) Num2Text = "CIEN";
  2335.             else if (value < 200) Num2Text = "CIENTO " + toText(value - 100);
  2336.             else if ((value == 200) || (value == 300) || (value == 400) || (value == 600) || (value == 800)) Num2Text = toText(Math.Truncate(value / 100)) + "CIENTOS";
  2337.             else if (value == 500) Num2Text = "QUINIENTOS";
  2338.             else if (value == 700) Num2Text = "SETECIENTOS";
  2339.             else if (value == 900) Num2Text = "NOVECIENTOS";
  2340.             else if (value < 1000) Num2Text = toText(Math.Truncate(value / 100) * 100) + " " + toText(value % 100);
  2341.             else if (value == 1000) Num2Text = "MIL";
  2342.             else if (value < 2000) Num2Text = "MIL " + toText(value % 1000);
  2343.             else if (value < 1000000)
  2344.             {
  2345.                 Num2Text = toText(Math.Truncate(value / 1000)) + " MIL";
  2346.                 if ((value % 1000) > 0) Num2Text = Num2Text + " " + toText(value % 1000);
  2347.             }
  2348.             else if (value == 1000000) Num2Text = "UN MILLON";
  2349.             else if (value < 2000000) Num2Text = "UN MILLON " + toText(value % 1000000);
  2350.             else if (value < 1000000000000)
  2351.             {
  2352.                 Num2Text = toText(Math.Truncate(value / 1000000)) + " MILLONES ";
  2353.                 if ((value - Math.Truncate(value / 1000000) * 1000000) > 0) Num2Text = Num2Text + " " + toText(value - Math.Truncate(value / 1000000) * 1000000);
  2354.             }
  2355.             else if (value == 1000000000000) Num2Text = "UN BILLON";
  2356.             else if (value < 2000000000000) Num2Text = "UN BILLON " + toText(value - Math.Truncate(value / 1000000000000) * 1000000000000);
  2357.             else
  2358.             {
  2359.                 Num2Text = toText(Math.Truncate(value / 1000000000000)) + " BILLONES";
  2360.                 if ((value - Math.Truncate(value / 1000000000000) * 1000000000000) > 0) Num2Text = Num2Text + " " + toText(value - Math.Truncate(value / 1000000000000) * 1000000000000);
  2361.             }
  2362.  
  2363.             return Num2Text;
  2364.         }
  2365.         public static byte[] ImageToByte(System.Drawing.Image pImagen)
  2366.         {
  2367.             byte[] mImage = null;
  2368.             System.IO.MemoryStream ms = new System.IO.MemoryStream();
  2369.             pImagen.Save(ms, pImagen.RawFormat);
  2370.             mImage = ms.GetBuffer();
  2371.             ms.Close();
  2372.             return mImage;
  2373.         }
  2374.  
  2375.         public static bool hayInternet()
  2376.         {
  2377.             NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
  2378.             foreach (NetworkInterface nic in nics)
  2379.             {
  2380.                 if (string.Compare(nic.Name, "hamachi", true) == 0)
  2381.                 {
  2382.                     continue;
  2383.                 }
  2384.  
  2385.                 if (
  2386.                     (nic.NetworkInterfaceType != NetworkInterfaceType.Loopback && nic.NetworkInterfaceType != NetworkInterfaceType.Tunnel) &&
  2387.                     nic.OperationalStatus == OperationalStatus.Up)
  2388.                 {
  2389.                     return true;
  2390.                 }
  2391.             }
  2392.             return false;
  2393.  
  2394.         }
  2395.         public static bool refreshToken()
  2396.         {
  2397.             bool result = false;
  2398.             if (hayInternet())
  2399.             {
  2400.                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
  2401.                 request.Method = "POST";
  2402.                 request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*;q=0.8";
  2403.                 request.Timeout = 10000;
  2404.                 request.Headers.Add("Accept-Language", "en-us,en;q=0.5");
  2405.                 request.ContentType = "application/x-www-form-urlencoded";
  2406.                 request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
  2407.                 request.KeepAlive = true;
  2408.  
  2409.                 string strPosData = "client_id=" + clientID + "&client_secret=" + clientSecret + "&refresh_token=" + Refresh_Token + "&grant_type=refresh_token";
  2410.                 request.ContentLength = strPosData.Length;
  2411.                 StreamWriter sw3 = new StreamWriter(request.GetRequestStream());
  2412.                 sw3.Write(strPosData);
  2413.                 sw3.Close();
  2414.                 try
  2415.                 {
  2416.                     using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  2417.                     {
  2418.                         string strHTML = string.Empty;
  2419.                         using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  2420.                         {
  2421.                             strHTML = reader.ReadToEnd();
  2422.                             reader.Close();
  2423.                             string token = Utility.GetRegExParsedValue("access_token\" : \"(?<RetVal>.*?)\"", strHTML);
  2424.                             if (token.Length > 0)
  2425.                             {
  2426.                                 Access_Token = token;
  2427.                                 result = true;
  2428.                                 Tokens t = new Tokens();
  2429.                                 t.AccessToken = token;
  2430.                                 t.RefreshToken = Refresh_Token;
  2431.                                 frmPrincipal._BaseDatos.InsertarTokens(t);
  2432.                                 t = null;
  2433.                             }
  2434.                         }
  2435.                     }
  2436.                 }
  2437.                 catch (Exception e)
  2438.                 {
  2439.                     System.Diagnostics.Debug.WriteLine(e.ToString());
  2440.                 }
  2441.             }
  2442.             return result;
  2443.         }
  2444.  
  2445.         public static bool IsValidToken()
  2446.         {
  2447.             bool result = false;
  2448.             if (hayInternet())
  2449.             {
  2450.                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + Access_Token);
  2451.                 request.Method = "GET";
  2452.                 request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*;q=0.8";
  2453.                 request.Timeout = 10000;
  2454.                 request.Headers.Add("Accept-Language", "en-us,en;q=0.5");
  2455.                 //request.ContentType = "application/x-www-form-urlencoded";
  2456.                 request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
  2457.                 request.KeepAlive = true;
  2458.                 try
  2459.                 {
  2460.                     using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  2461.                     {
  2462.                         string strHTML = string.Empty;
  2463.                         using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  2464.                         {
  2465.                             strHTML = reader.ReadToEnd();
  2466.                             reader.Close();
  2467.                             string email = Utility.GetRegExParsedValue("email\": \"(?<RetVal>.*?)\"", strHTML);
  2468.                             if (email.Length > 0)
  2469.                             {
  2470.                                 result = true;
  2471.                             }
  2472.                         }
  2473.                     }
  2474.                 }
  2475.                 catch (Exception e)
  2476.                 {
  2477.                     System.Diagnostics.Debug.WriteLine(e.ToString());
  2478.                 }
  2479.             }
  2480.             return result;
  2481.         }
  2482.  
  2483.     }
  2484.  
  2485.     public class MetodoPago
  2486.     {
  2487.         private int clave;
  2488.         private string definicion;
  2489.         public MetodoPago(int clave, string definicion)
  2490.         {
  2491.             this.clave = clave;
  2492.             this.definicion = definicion;
  2493.         }
  2494.  
  2495.         public int Clave
  2496.         {
  2497.             get { return clave; }
  2498.             set { clave = value; }
  2499.         }
  2500.  
  2501.         public string Definicion
  2502.         {
  2503.             get { return definicion; }
  2504.             set { definicion = value; }
  2505.         }
  2506.  
  2507.         public override string ToString()
  2508.         {
  2509.             return clave.ToString("D2") + " " + definicion;
  2510.         }
  2511.     }
  2512. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement