Exemplo n.º 1
0
        //TODO: Repetido
        private string ObtenerNroComprobante()
        {
            string            ultimoComprobante = string.Empty;
            TiposComprobantes tipo = comprobante.Tipo;

            try
            {
                if (tipo.Codigo == ConfigurationManager.AppSettings["tipoComprobante.FacturaA"])
                {
                    ultimoComprobante = comprobante.Empresa.UltimaFacturaA;
                }
                else if (tipo.Codigo == ConfigurationManager.AppSettings["tipoComprobante.FacturaB"])
                {
                    ultimoComprobante = comprobante.Empresa.UltimaFacturaB;
                }
                else if (tipo.Codigo == ConfigurationManager.AppSettings["tipoComprobante.FacturaC"])
                {
                    ultimoComprobante = comprobante.Empresa.UltimaFacturaC;
                }
                else if (tipo.Codigo == ConfigurationManager.AppSettings["tipoComprobante.FacturaE"])
                {
                    ultimoComprobante = comprobante.Empresa.UltimaFacturaE;
                }
                else if (tipo.Codigo == ConfigurationManager.AppSettings["tipoComprobante.NotaDebitoA"])
                {
                    ultimoComprobante = comprobante.Empresa.UltimaNotaDebitoA;
                }
                else if (tipo.Codigo == ConfigurationManager.AppSettings["tipoComprobante.NotaDebitoB"])
                {
                    ultimoComprobante = comprobante.Empresa.UltimaNotaDebitoB;
                }
                else if (tipo.Codigo == ConfigurationManager.AppSettings["tipoComprobante.NotaDebitoC"])
                {
                    ultimoComprobante = comprobante.Empresa.UltimaNotaDebitoC;
                }
                else if (tipo.Codigo == ConfigurationManager.AppSettings["tipoComprobante.NotaDebitoE"])
                {
                    ultimoComprobante = comprobante.Empresa.UltimaNotaDebitoE;
                }
                else if (tipo.Codigo == ConfigurationManager.AppSettings["tipoComprobante.NotaCreditoA"])
                {
                    ultimoComprobante = comprobante.Empresa.UltimaNotaCreditoA;
                }
                else if (tipo.Codigo == ConfigurationManager.AppSettings["tipoComprobante.NotaCreditoB"])
                {
                    ultimoComprobante = comprobante.Empresa.UltimaNotaCreditoB;
                }
                else if (tipo.Codigo == ConfigurationManager.AppSettings["tipoComprobante.NotaCreditoC"])
                {
                    ultimoComprobante = comprobante.Empresa.UltimaNotaCreditoC;
                }
                else if (tipo.Codigo == ConfigurationManager.AppSettings["tipoComprobante.NotaCreditoE"])
                {
                    ultimoComprobante = comprobante.Empresa.UltimaNotaCreditoE;
                }

                string[] partes = ultimoComprobante.Split(new char[] { '-' });

                int?nroC;
                if (partes.Length >= 2)
                {
                    int nro = Convert.ToInt32(partes[1]);
                    nro++;
                    comprobante.NroCbante = partes[0] + "-" + nro.ToString().PadLeft(8, '0');
                    nroC = MFDService.GetUltimoNroComprobante(comprobante);
                    if (nroC.HasValue)
                    {
                        nroC++;
                    }
                    else
                    {
                        nroC = 1;
                    }
                }
                else
                {
                    throw new Exception("Error al intentar obtener el último nro de comprobante: " + tipo.Nombre);
                }

                return(partes[0] + "-" + nroC.ToString().PadLeft(8, '0'));
            }
            catch
            {
                throw new Exception("Error al intentar obtener el último nro de comprobante: " + tipo.Nombre);
            }
        }
        public static void ReceiveOldMessage(List<MFD> MFDList)
        {
            #if DEBUG_MGS
            string output = string.Format("Enter ReceiveOldMessage");
            lock (_locker)
            {
                Console.WriteLine(output);
                Debug.Print(output + string.Format("\r\n"));
            }
            #endif

            // Navico NSS 2.0: Multicast 239.2.1.1, port 2050
            UdpClient client = new UdpClient();
            client.Client.ReceiveTimeout = 2000;

            IPEndPoint localEp = new IPEndPoint(IPAddress.Any, 2050);
            client.Client.Bind(localEp);

            IPAddress multicastaddress = IPAddress.Parse("239.2.1.1");
            client.JoinMulticastGroup(multicastaddress);

            DateTime timeLastNewFound = DateTime.Now;
            while (timeLastNewFound.AddSeconds(2) > DateTime.Now)
            {
                Byte[] data = null;
                try
                {
                    data = client.Receive(ref localEp);
                }
                catch (Exception ex)
                {
                    break;
                }
                if (data.Length > 0)
                {
                    string strData = Encoding.ASCII.GetString(data);
                    char[] lineEnd = { '\r', '\n' };
                    strData = strData.TrimEnd(lineEnd);
                    string[] strParams = strData.Split(',');
                    if (strParams.Count() >= 4)
                    {
                        bool foundMFD = false;
                        bool foundService = false;
                        foreach (MFD mfd in MFDList)
                        {
                            if (strParams[2] == mfd.IP)
                            {
                                foundMFD = true;
                                foreach (MFDService service in mfd.Services)
                                {
                                    if (strParams[0] == service.Service)
                                    {
                                        foundService = true;
                                    }
                                }
                                break;
                            }
                        }

                        if (foundMFD)
                        {
                            if (!foundService)
                            {
                                timeLastNewFound = DateTime.Now;
                                foreach (MFD mfd in MFDList)
                                {
                                    if (strParams[2] == mfd.IP)
                                    {
                                        lock (_locker)
                                        {
                                            MFDService[] newList = new MFDService[mfd.Services.Count() + 1];
                                            for (int service = 0; service < mfd.Services.Count(); service++)
                                            {
                                                newList[service] = mfd.Services[service];
                                            }
                                            newList[mfd.Services.Count()].Service = strParams[0];
                                            newList[mfd.Services.Count()].Port = Convert.ToUInt32(strParams[3]);
                                            mfd.Services = newList;
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            timeLastNewFound = DateTime.Now;
                            MFD mfd = new MFD();
                            mfd.IP = strParams[2];
                            mfd.Services = new MFDService[1];
                            mfd.Services[0] = new MFDService(strParams[0], 0, Convert.ToUInt32(strParams[3]));
            #if DEBUG_MGS
                            output = string.Format("Service: {0}\tVersion: {1}\tIP: {2}\tPort: {3}", strParams[0], strParams[1], strParams[2], strParams[3]);
            #endif
                            lock (_locker)
                            {
                                MFDList.Add(mfd);

            #if DEBUG_MGS
                                Console.WriteLine(output);
                                Debug.Print(output + string.Format("\r\n"));
            #endif
                            }
                        }
                    }
                }
            }

            #if DEBUG_MGS
            output = string.Format("Exit ReceiveOldMessage");
            lock (_locker)
            {
                Console.WriteLine(output);
                Debug.Print(output + string.Format("\r\n"));
            }
            #endif
        }
        public static void ReceiveOldMessage(List<MFD> MFDList)
        {
#if DEBUG_MGS
            string output = string.Format("Enter ReceiveOldMessage");
            lock (_locker)
            {
                Console.WriteLine(output);
                Debug.Print(output + string.Format("\r\n"));
            }
#endif

            // Navico NSS 2.0: Multicast 239.2.1.1, port 2050
            UdpClient client = new UdpClient();
            client.Client.ReceiveTimeout = 2000;

            IPEndPoint localEp = new IPEndPoint(IPAddress.Any, 2050);
            client.Client.Bind(localEp);

            IPAddress multicastaddress = IPAddress.Parse("239.2.1.1");
            client.JoinMulticastGroup(multicastaddress);

            DateTime timeLastNewFound = DateTime.Now;
            while (timeLastNewFound.AddSeconds(2) > DateTime.Now)
            {
                Byte[] data = null;
                try
                {
                    data = client.Receive(ref localEp);
                }
                catch (Exception ex)
                {
                    break;
                }
                if (data.Length > 0)
                {
                    string strData = Encoding.ASCII.GetString(data);
                    char[] lineEnd = { '\r', '\n' };
                    strData = strData.TrimEnd(lineEnd);
                    string[] strParams = strData.Split(',');
                    if (strParams.Count() >= 4)
                    {
                        bool foundMFD = false;
                        bool foundService = false;
                        foreach (MFD mfd in MFDList)
                        {
                            if (strParams[2] == mfd.IP)
                            {
                                foundMFD = true;
                                foreach (MFDService service in mfd.Services)
                                {
                                    if (strParams[0] == service.Service)
                                    {
                                        foundService = true;
                                    }
                                }
                                break;
                            }
                        }

                        if (foundMFD)
                        {
                            if (!foundService)
                            {
                                timeLastNewFound = DateTime.Now;
                                foreach (MFD mfd in MFDList)
                                {
                                    if (strParams[2] == mfd.IP)
                                    {
                                        lock (_locker)
                                        {
                                            MFDService[] newList = new MFDService[mfd.Services.Count() + 1];
                                            for (int service = 0; service < mfd.Services.Count(); service++)
                                            {
                                                newList[service] = mfd.Services[service];
                                            }
                                            newList[mfd.Services.Count()].Service = strParams[0];
                                            newList[mfd.Services.Count()].Port = Convert.ToUInt32(strParams[3]);
                                            mfd.Services = newList;
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            timeLastNewFound = DateTime.Now;
                            MFD mfd = new MFD();
                            mfd.IP = strParams[2];
                            mfd.Services = new MFDService[1];
                            mfd.Services[0] = new MFDService(strParams[0], 0, Convert.ToUInt32(strParams[3]));
#if DEBUG_MGS
                            output = string.Format("Service: {0}\tVersion: {1}\tIP: {2}\tPort: {3}", strParams[0], strParams[1], strParams[2], strParams[3]);
#endif
                            lock (_locker)
                            {
                                MFDList.Add(mfd);

#if DEBUG_MGS
                                Console.WriteLine(output);
                                Debug.Print(output + string.Format("\r\n"));
#endif
                            }
                        }
                    }
                }
            }

#if DEBUG_MGS
            output = string.Format("Exit ReceiveOldMessage");
            lock (_locker)
            {
                Console.WriteLine(output);
                Debug.Print(output + string.Format("\r\n"));
            }
#endif
        }