示例#1
0
        private bool CompileFdlTable()
        {
            bool result = false;

            if (stopImport)
            {
                return(result);
            }

            StatusChanged("Importing PDF files...");

            try
            {
                IEnumerable <DataRow> sentFiles = dtSentFiles.Rows.Cast <DataRow>();

                foreach (FileInfo file in new DirectoryInfo(_sourceFdlPath).GetFiles("*.pdf", SearchOption.AllDirectories))
                {
                    if (stopImport)
                    {
                        break;
                    }

                    FDLEVM fdl = null;

                    try
                    {
                        fdl = FDLManager.ImportFDLFromFile(file.FullName, false, false, false, true, true);

                        // try with XFA format
                        if (fdl == null)
                        {
                            fdl = FDLManager.ImportFDLFromFile(file.FullName, true, false, false, true, true);
                        }

                        if (fdl != null)
                        {
                            File.Copy(file.FullName, Path.Combine(ApplicationSettings.Directories.FDL, file.Name), true);

                            DataRow sent = sentFiles.Where(f => !string.IsNullOrEmpty(f.Field <string>("Dbf_Foglio")) && FormatFDL(f.Field <string>("Dbf_Foglio")) == fdl.Id && (f.Field <int>("dbf_TipoInvio") == 2 || f.Field <int>("dbf_TipoInvio") == 4)).Select(f => f)
                                           .OrderBy(x => x.Field <int>("Dbf_NumeroInviiPrima") == 0)
                                           .ThenBy(x => string.IsNullOrEmpty(x.Field <string>("Dbf_Impianto")))
                                           .ThenBy(x => string.IsNullOrEmpty(x.Field <string>("Dbf_Commessa")))
                                           .FirstOrDefault();

                            if (sent != null)
                            {
                                using (DBArchive db = new DBArchive())
                                {
                                    // we must override recived fdl with the same of current dbcontext istance
                                    FDL currentFdl = db.FDLs.SingleOrDefault(f => f.Id == fdl.Id);

                                    if (currentFdl != null)
                                    {
                                        if (sent.Field <int>("Dbf_NumeroInviiPrima") == 0)
                                        {
                                            currentFdl.Status = (long)EFDLStatus.Waiting;
                                        }
                                        else if (sent.Field <string>("Dbf_Impianto") != string.Empty && sent.Field <string>("Dbf_Commessa") != string.Empty)
                                        {
                                            currentFdl.Status = (long)EFDLStatus.Accepted;
                                        }
                                        else
                                        {
                                            currentFdl.Status = (long)EFDLStatus.Cancelled;
                                        }

                                        if (currentFdl.Status != (long)EFDLStatus.New)
                                        {
                                            currentFdl.IsReadOnly = true;
                                        }

                                        db.FDLs.AddOrUpdate(currentFdl);
                                        db.SaveChanges();
                                        Message($"FDL {currentFdl.Id} OK");
                                    }
                                    else
                                    {
                                        Error("Missing FDL on database. Should never happen.");
                                    }
                                }
                            }
                            else
                            {
                                Error("Missing FDL sent status!");
                            }
                        }
                        else
                        {
                            Error($"Failed to import FDL from file: {file.FullName}");
                        }
                    }
                    catch (Exception ex)
                    {
                        Error($"Failed importing FDL {fdl?.Id}. {ex}", ex);
                    }
                }

                result = true;
            }
            catch (Exception ex)
            {
                Error($"Failed importing FDLs. {ex}", ex);
            }

            return(result);
        }
示例#2
0
        private bool ImportFDLFiles()
        {
            bool result = false;

            if (stopImport)
            {
                return(result);
            }

            StatusChanged("Importing FDL files...");

            try
            {
                foreach (FileInfo file in new DirectoryInfo(FDLFolder).GetFiles("*.pdf", SearchOption.AllDirectories))
                {
                    if (stopImport)
                    {
                        break;
                    }

                    if (FDLManager.GetFileType(file.Name) != EFileType.FDL)
                    {
                        continue;
                    }

                    FDLEVM fdl = null;

                    try
                    {
                        fdl = FDLManager.ImportFDLFromFile(file.FullName, false, false, false, false, true);

                        // try with XFA format
                        if (fdl == null)
                        {
                            fdl = FDLManager.ImportFDLFromFile(file.FullName, true, false, false, false, true);
                        }

                        if (fdl != null)
                        {
                            File.Copy(file.FullName, Path.Combine(ApplicationSettings.Directories.FDL, file.Name), true);

                            if (UserSettings.Advanced.ExcelExpenseAccount)
                            {
                                FDLManager.CreateExcelEA(fdl);
                            }

                            using (DBArchive db = new DBArchive())
                            {
                                // we must override received fdl with the same of current dbcontext istance
                                FDL currentFdl = db.FDLs.SingleOrDefault(f => f.Id == fdl.Id);

                                if (currentFdl != null)
                                {
                                    FDLEVM tmpFdl = new FDLEVM(currentFdl);

                                    if (tmpFdl.StartDayDate.Year < DateTime.Now.Year || (tmpFdl.StartDayDate.Year == DateTime.Now.Year && tmpFdl.WeekNr < DateTime.Now.WeekNr()))
                                    {
                                        tmpFdl.EStatus = EFDLStatus.Accepted;
                                        tmpFdl.Save(db);
                                    }
                                }
                                else
                                {
                                    Error("Missing FDL on database. Should never happen.");
                                }
                            }

                            Message($"FDL {fdl.Id} OK");
                        }
                        else
                        {
                            Error($"Failed to import FDL from file: {file.FullName}");
                        }
                    }
                    catch (Exception ex)
                    {
                        Error($"Failed importing FDL {fdl?.Id}. {ex}", ex);
                    }
                }

                result = true;
            }
            catch (Exception ex)
            {
                Error($"Failed importing FDLs. {ex}", ex);
            }

            return(result);
        }