Ejemplo n.º 1
0
 public void SaveEdiPure(ref string _EdiPure, string _FileName, int _CheckSeg)
 {
     LearPureEdiO = new LearPureEdi()
     {
         HashId        = EdiBase.GetHashId(),
         EdiStr        = _EdiPure,
         Fingreso      = DateTime.Now.ToString(ApplicationSettings.DateTimeFormat),
         Reprocesar    = true,
         NombreArchivo = _FileName,
         CheckSeg      = _CheckSeg,
         InOut         = "I"
     };
     DbO.LearPureEdi.Add(LearPureEdiO);
     DbO.SaveChanges();
 }
Ejemplo n.º 2
0
 public static void AddComLog(ref EdiDBContext _DbO, string _Type, string _Log)
 {
     try
     {
         _DbO.EdiComs.Add(new EdiComs()
         {
             Type = _Type,
             Freg = DateTime.Now.ToString(ApplicationSettings.DateTimeFormat),
             Log  = _Log
         });
         _DbO.SaveChanges();
     }
     catch { }
 }
Ejemplo n.º 3
0
        public void SaveAll856(ref EdiDBContext _DbO)
        {
            try
            {
                ApplicationSettings.SavedSegments++;
                Validate();
                switch (GetType().GetField("Init").GetRawConstantValue())
                {
                case ISA856.Init:
                    LearRep856.LearIsa856root = Reflect(new LearIsa856());
                    _DbO.LearIsa856.Add(LearRep856.LearIsa856root);
                    break;

                case BSN856.Init:
                    _DbO.LearBsn856.Add(Reflect(new LearBsn856()));
                    break;

                case CLD856.Init:
                    _DbO.LearCld856.Add(Reflect(new LearCld856()));
                    break;

                case CTT856.Init:
                    _DbO.LearCtt856.Add(Reflect(new LearCtt856()));
                    break;

                case DTM856.Init:
                    _DbO.LearDtm856.Add(Reflect(new LearDtm856()));
                    break;

                case ETD856.Init:
                    _DbO.LearEtd856.Add(Reflect(new LearEtd856()));
                    break;

                //case GE856.Init:
                //    break;
                case GS856.Init:
                    _DbO.LearGs856.Add(Reflect(new LearGs856()));
                    break;

                case HLOL856.Init:
                    if (this.GetType().Name == "HLOL856")
                    {
                        _DbO.LearHlol856.Add(Reflect(new LearHlol856()));
                    }
                    else
                    {
                        _DbO.LearHlsl856.Add(Reflect(new LearHlsl856()));
                    }
                    break;

                //case IEA856.Init:
                //    _DbO.LearI
                //    break;
                case LIN856.Init:
                    _DbO.LearLin856.Add(Reflect(new LearLin856()));
                    break;

                case MEA856.Init:
                    _DbO.LearMea856.Add(Reflect(new LearMea856()));
                    break;

                case N1856.Init:
                    _DbO.LearN1856.Add(Reflect(new LearN1856()));
                    break;

                case PRF856.Init:
                    _DbO.LearPrf856.Add(Reflect(new LearPrf856()));
                    break;

                case REF856.Init:
                    _DbO.LearRef856.Add(Reflect(new LearRef856()));
                    break;

                //case SE856.Init:
                //    _DbO.LearSE
                //    break;
                case SN1856.Init:
                    _DbO.LearSn1856.Add(Reflect(new LearSn1856()));
                    break;

                case ST856.Init:
                    _DbO.LearSt856.Add(Reflect(new LearSt856()));
                    break;

                case TD1856.Init:
                    _DbO.LearTd1856.Add(Reflect(new LearTd1856()));
                    break;

                case TD3856.Init:
                    _DbO.LearTd3856.Add(Reflect(new LearTd3856()));
                    break;

                case TD5856.Init:
                    _DbO.LearTd5856.Add(Reflect(new LearTd5856()));
                    break;

                default:
                    break;
                }
                _DbO.SaveChanges();
            }
            catch (Exception eFb1)
            {
                throw new Exception($"Error al guardar {GetType().GetField("Init").GetRawConstantValue() } {eFb1.ToString()}. Error en linea {EdiStr}");
            }
            foreach (EdiBase ChildO in this.Childs)
            {
                ChildO.SaveAll856(ref _DbO);
            }
        }
Ejemplo n.º 4
0
        //De modo que se expone https://localhost:44373/Edi/TranslateForms830
        public ActionResult <RetReporte> TranslateForms830()
        {
            //System.IO.StreamWriter Sw2 = new System.IO.StreamWriter(@"c:\temp\EdiLog.txt", true);
            //Sw2.WriteLine("TranslateForms830 init" + DateTime.Now.ToString() + Environment.NewLine);
            //Sw2.Close();
            DateTime   StartTime   = DateTime.Now;
            LearRep830 LearRep830O = new LearRep830(ref DbO);

            try
            {
                int           CodError2 = 0;
                string        MessageSubject = string.Empty, FileName = string.Empty, EdiPureStr = string.Empty;
                List <string> ListEdiPure = new List <string>();
                try
                {
                    //StreamReader Rep830File = ComRepoMail.GetEdi830File(IMapHost, IMapPortIn, IMapPortOut, IMapUser, IMapPassword, IMapSSL, ref CodError, ref MessageSubject, ref FileName, ref DbO, Config.GetSection("MaxEdiComs").GetValue(typeof(string), "Value"));
                    ComRepoFtp ComRepoFtpO = new ComRepoFtp(
                        (string)IEdiFtpConfig.GetValue(typeof(string), "Host"),
                        (string)IEdiFtpConfig.GetValue(typeof(string), "HostFailover"),
                        (string)IEdiFtpConfig.GetValue(typeof(string), "EdiUser"),
                        (string)IEdiFtpConfig.GetValue(typeof(string), "EdiPassword"),
                        (string)IEdiFtpConfig.GetValue(typeof(string), "DirIn"),
                        (string)IEdiFtpConfig.GetValue(typeof(string), "DirOut"),
                        (string)IEdiFtpConfig.GetValue(typeof(string), "DirChecked"),
                        Config.GetSection("MaxEdiComs").GetValue(typeof(string), "Value")
                        );
                    if (!ComRepoFtpO.Ping(ref DbO))
                    {
                        ComRepoFtpO.UseHost2 = true;
                        if (!ComRepoFtpO.Ping(ref DbO))
                        {
                            return(new RetReporte()
                            {
                                Info = new RetInfo()
                                {
                                    CodError = -3,
                                    Mensaje = $"Error, no se puede conectar con el servidor FTP primario o secundario",
                                    ResponseTimeSeconds = (DateTime.Now - StartTime).TotalSeconds
                                }
                            });
                        }
                    }
                    ComRepoFtpO.Get(ref DbO, ref CodError2, ref MessageSubject, ref FileName, ref ListEdiPure);
                    //ComRepoMail.GetEdi830File(IMapHost, IMapPortIn, IMapPortOut, IMapUser, IMapPassword, IMapSSL, ref CodError2, ref MessageSubject, ref FileName, ref DbO, Config.GetSection("MaxEdiComs").GetValue(typeof(string), "Value"), ref ListEdiPure);
                    switch (CodError2)
                    {
                    case -1:
                        return(new RetReporte()
                        {
                            Info = new RetInfo()
                            {
                                CodError = -1,
                                Mensaje = $"Error, el correo verificado no contiene ningún archivo. Subject = {MessageSubject}.",
                                ResponseTimeSeconds = (DateTime.Now - StartTime).TotalSeconds
                            }
                        });

                    case -2:
                        return(new RetReporte()
                        {
                            Info = new RetInfo()
                            {
                                CodError = -2,
                                Mensaje = $"Error, no hay nada a verificar.",
                                ResponseTimeSeconds = (DateTime.Now - StartTime).TotalSeconds
                            }
                        });

                    case -4:
                    case -5:
                    case -6:
                    case -7:
                    case -8:
                    case -9:
                    case -10:
                    case -11:
                        return(new RetReporte()
                        {
                            Info = new RetInfo()
                            {
                                CodError = CodError2,
                                Mensaje = MessageSubject,
                                ResponseTimeSeconds = (DateTime.Now - StartTime).TotalSeconds
                            }
                        });
                    }
                    for (int Ifn = 0; Ifn < ListEdiPure.Count; Ifn++)
                    {
                        ListEdiPure[Ifn] = ListEdiPure[Ifn].Replace(Environment.NewLine, "");
                    }
                    if (ListEdiPure.Count > 1)
                    {
                        ListEdiPure.ForEach(E => EdiPureStr += E);
                        if (ListEdiPure[1].Contains(EdiBase.SegmentTerminator))
                        {
                            LearRep830O.EdiFile = EdiPureStr.Split(EdiBase.SegmentTerminator).ToList();
                        }
                        else
                        {
                            LearRep830O.EdiFile = ListEdiPure;
                        }
                    }
                    else if (ListEdiPure.Count == 1)
                    {
                        if (ListEdiPure.FirstOrDefault().Contains(EdiBase.SegmentTerminator))
                        {
                            LearRep830O.EdiFile = ListEdiPure.FirstOrDefault().Split(EdiBase.SegmentTerminator).ToList();
                        }
                        else
                        {
                            return(new RetReporte()
                            {
                                Info = new RetInfo()
                                {
                                    CodError = -13,
                                    Mensaje = $"El archivo Edi {FileName} no contiene un separador de segmento válido, no se puede procesar.",
                                    ResponseTimeSeconds = (DateTime.Now - StartTime).TotalSeconds
                                }
                            });
                        }
                    }
                    else
                    {
                        return(new RetReporte()
                        {
                            Info = new RetInfo()
                            {
                                CodError = -14,
                                Mensaje = $"El archivo Edi {FileName} no contiene ninguna linea de contenido.",
                                ResponseTimeSeconds = (DateTime.Now - StartTime).TotalSeconds
                            }
                        });
                    }
                    if (LearRep830O.EdiFile.LastOrDefault() == "")
                    {
                        LearRep830O.EdiFile.RemoveAt(LearRep830O.EdiFile.Count - 1);
                    }
                }
                catch (Exception ExMail)
                {
                    return(new RetReporte()
                    {
                        Info = new RetInfo()
                        {
                            CodError = 1,
                            Mensaje = ExMail.ToString(),
                            ResponseTimeSeconds = (DateTime.Now - StartTime).TotalSeconds
                        }
                    });
                }
                LearRep830O.SaveEdiPure(ref EdiPureStr, FileName, LearRep830O.EdiFile.Count);
                string ParseRet = LearRep830O.Parse();
                if (!string.IsNullOrEmpty(ParseRet))
                {
                    LearRep830O.LearPureEdiO.Reprocesar = false;
                    LearRep830O.LearPureEdiO.Fprocesado = DateTime.Now.ToString(ApplicationSettings.DateTimeFormat);
                    LearRep830O.LearPureEdiO.Log        = ParseRet;
                    LearRep830O.LearPureEdiO.InOut      = "I";
                    DbO.LearPureEdi.Update(LearRep830O.LearPureEdiO);
                    DbO.SaveChanges();
                    return(new RetReporte()
                    {
                        Info = new RetInfo()
                        {
                            CodError = -12,
                            Mensaje = ParseRet,
                            ResponseTimeSeconds = (DateTime.Now - StartTime).TotalSeconds
                        }
                    });
                }
                LearRep830O.SaveAll();
                if (LearRep830.LearIsa830root != null)
                {
                    LearRep830.LearIsa830root = DbO.LearIsa830.Where(L => L.HashId == LearRep830.LearIsa830root.HashId).FirstOrDefault();
                    LearRep830.LearIsa830root.ParentHashId = LearRep830O.LearPureEdiO.HashId;
                    DbO.LearIsa830.Update(LearRep830.LearIsa830root);
                }
                LearRep830O.UpdateEdiPure();
                try
                {
                    List <Tuple <LIN830, FST830> > ListProductsQtys = new List <Tuple <LIN830, FST830> >();
                    List <LIN830> ListProducts = new List <LIN830>();
                    List <SHP830> ListShp      = new List <SHP830>();
                    LearRep830O.ISAO.Childs.ForEach(ObjSt =>
                    {
                        if (ObjSt.GetType().Name == "ST830")
                        {
                            ObjSt.Childs.ForEach(ObjLin =>
                            {
                                if (ObjLin.GetType().Name == "LIN830")
                                {
                                    ObjLin.Childs.ForEach(ObjSdp =>
                                    {
                                        if (ObjSdp.GetType().Name.Equals("SDP830"))
                                        {
                                            ObjSdp.Childs.ForEach(ObjFst =>
                                            {
                                                if (ObjFst.GetType().Name.Equals("FST830"))
                                                {
                                                    if (!ListProducts.Exists(P => P.HashId == ((LIN830)ObjLin).HashId))
                                                    {
                                                        ListProducts.Add((LIN830)ObjLin);
                                                    }
                                                    ListProductsQtys.Add(new Tuple <LIN830, FST830>((LIN830)ObjLin, (FST830)ObjFst));
                                                }
                                            });
                                        }
                                        else if (ObjSdp.GetType().Name.Equals("SHP830"))
                                        {
                                            ListShp.Add((SHP830)ObjSdp);
                                        }
                                    });
                                }
                            });
                        }
                    });
                    ListProductsQtys = ListProductsQtys
                                       .OrderBy(Pq => Pq.Item1.ProductId)
                                       .ThenBy(Pq2 => Pq2.Item2.FstDate.ToShortDate())
                                       .ToList();
                    ListProducts.ForEach(P =>
                    {
                        DateTime?FirstDate = null;
                        ListProductsQtys
                        .Where(Pq => Pq.Item1.HashId == P.HashId)
                        .ToList().ForEach(Pq =>
                        {
                            if (!FirstDate.HasValue)
                            {
                                FirstDate = Pq.Item2.FstDate.ToShortDate();
                            }
                            if ((Pq.Item2.FstDate.ToShortDate() - FirstDate.Value).TotalDays > 6)
                            {
                                return;
                            }
                            switch (Pq.Item2.FstDate.ToShortDate().DayOfWeek)
                            {
                            case DayOfWeek.Monday:
                                SHP830 Shp = ListShp.Where(S => S.ParentHashId == P.HashId && S.QuantityQualifier.Equals("02")).Fod();
                                if (Shp != null)
                                {
                                    double ShpQty    = Convert.ToDouble(Shp.Quantity);
                                    double Qty       = Convert.ToDouble(Pq.Item2.Quantity);
                                    double QtyRes    = (Qty - ShpQty);
                                    Pq.Item2.RealQty = QtyRes.ToString("N0");
                                }
                                break;

                            case DayOfWeek.Wednesday:
                            case DayOfWeek.Friday:
                                FST830 FstLast   = ListProductsQtys.Where(Pq2 => Pq2.Item1.HashId == P.HashId && Pq2.Item2.FstDate.ToShortDate() == Pq.Item2.FstDate.ToShortDate().AddDays(-2)).Fod().Item2;
                                Pq.Item2.RealQty = (Convert.ToDouble(Pq.Item2.Quantity) - Convert.ToDouble(FstLast.Quantity)).ToString("N0");
                                break;
                            }
                        });
                    });
                    ListProductsQtys = ListProductsQtys.Where(Pq => !string.IsNullOrEmpty(Pq.Item2.RealQty)).ToList();
                    ListProductsQtys.ForEach(Pq => {
                        LearFst830 Fst = DbO.LearFst830.Where(F => F.HashId == Pq.Item2.HashId).Fod();
                        Fst.RealQty    = Pq.Item2.RealQty;
                        DbO.LearFst830.Update(Fst);
                    });
                    DbO.SaveChanges();
                }
                catch
                {
                }
                return(new RetReporte()
                {
                    EdiFile = string.Join(EdiBase.SegmentTerminator, LearRep830O.EdiFile),
                    Info = new RetInfo()
                    {
                        CodError = 0,
                        Mensaje = "Todo OK",
                        ResponseTimeSeconds = (DateTime.Now - StartTime).TotalSeconds
                    }
                });
            }
            catch (Exception e1)
            {
                try
                {
                    LearRep830O.LearPureEdiO.Log = e1.ToString();
                    DbContextOptionsBuilder <EdiDBContext> optionsBuilder = new DbContextOptionsBuilder <EdiDBContext>();
                    optionsBuilder.UseSqlServer(Config.GetConnectionString("EdiDB"));
                    DbOEx = new EdiDBContext(optionsBuilder.Options);
                    DbOEx.LearPureEdi.Update(LearRep830O.LearPureEdiO);
                    DbOEx.SaveChanges();
                }
                catch (Exception SevereEx)
                {
                    return(new RetReporte()
                    {
                        Info = new RetInfo()
                        {
                            CodError = 1,
                            Mensaje = "ERROR GRAVE DE BASE DE DATOS. " + SevereEx.ToString(),
                            ResponseTimeSeconds = (DateTime.Now - StartTime).TotalSeconds
                        }
                    });
                }
                return(new RetReporte()
                {
                    Info = new RetInfo()
                    {
                        CodError = 1,
                        Mensaje = e1.ToString(),
                        ResponseTimeSeconds = (DateTime.Now - StartTime).TotalSeconds
                    }
                });
            }
        }
Ejemplo n.º 5
0
 public void Get(ref EdiDBContext _DbO, ref int _CodError, ref string _MessageSubject, ref string _FileName, ref List <string> _EdiPure)
 {
     try
     {
         GetInStack(ref _DbO, ref _CodError, ref _MessageSubject);
         if (Files.Length == 0)
         {
             return;
         }
         if (string.IsNullOrEmpty(Files.LastOrDefault()))
         {
             return;
         }
         List <string>      ListFiles        = Files.ToList();
         List <LearPureEdi> ListLearPureEdiO = (
             from Pe in _DbO.LearPureEdi
             from Lf in ListFiles
             where Pe.NombreArchivo == Lf
             select Pe).ToList();
         for (int Ci = 0; Ci < ListLearPureEdiO.Count; Ci++)
         {
             if (ListLearPureEdiO[Ci].Reprocesar)
             {
                 ListLearPureEdiO[Ci].Reprocesar = false;
                 _DbO.LearPureEdi.Update(ListLearPureEdiO[Ci]);
                 _DbO.SaveChanges();
                 _FileName = ListLearPureEdiO[Ci].NombreArchivo;
                 break;
             }
             ListFiles.Remove(ListLearPureEdiO[Ci].NombreArchivo);
         }
         if (string.IsNullOrEmpty(_FileName))
         {
             if (ListFiles.Count == 0)
             {
                 _CodError = -2;
                 return;
             }
             else
             {
                 _FileName = ListFiles.Fod();
             }
         }
         ftpRequest             = UseHost2 ? (FtpWebRequest)WebRequest.Create($"{host2}/{DirIn}/{_FileName}") : (FtpWebRequest)WebRequest.Create($"{host}/{DirIn}/{_FileName}");
         ftpRequest.Credentials = new NetworkCredential(user, pass);
         ftpRequest.UseBinary   = true;
         ftpRequest.UsePassive  = true;
         ftpRequest.KeepAlive   = true;
         ftpRequest.Method      = WebRequestMethods.Ftp.DownloadFile;
         ftpResponse            = (FtpWebResponse)ftpRequest.GetResponse();
         ftpStream = ftpResponse.GetResponseStream();
         MemoryStream localFileStream = new MemoryStream();
         byte[]       byteBuffer      = new byte[bufferSize];
         int          bytesRead       = ftpStream.Read(byteBuffer, 0, bufferSize);
         try
         {
             while (bytesRead > 0)
             {
                 localFileStream.Write(byteBuffer, 0, bytesRead);
                 bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
             }
         }
         catch (Exception ex)
         {
             _CodError       = -9;
             _MessageSubject = $"Error, al escribir el archivo en stream de memoria {_FileName} en ftp {(UseHost2 ? host2 : host)}. Info: {ex.ToString()}";
             AddComLog(ref _DbO, Id, _MessageSubject);
             return;
         }
         localFileStream.Close();
         ftpStream.Close();
         ftpResponse.Close();
         ftpRequest = null;
         string EdiPure2 = System.Text.Encoding.UTF8.GetString(localFileStream.ToArray());
         _EdiPure = EdiPure2.Split(EdiBase.SegmentTerminator).ToList();
         if (_EdiPure.Count == 1)
         {
             _EdiPure = EdiPure2.Split('\n').ToList();
         }
         AddComLog(ref _DbO, Id, $"Se obtuvo el archivo {_FileName} de ftp {(UseHost2 ? host2 : host)}");
         return;
     }
     catch (Exception ex)
     {
         _CodError       = -10;
         _MessageSubject = $"Error en método GET en ftp {(UseHost2 ? host2 : host)}. Info: {ex.ToString()}";
         AddComLog(ref _DbO, Id, _MessageSubject);
         return;
     }
 }