Esempio n. 1
0
        private bool CompileEATable()
        {
            bool result = false;

            if (stopImport)
            {
                return(result);
            }

            StatusChanged("Importing Expense Account files...");

            try
            {
                IEnumerable <DataRow> expenses = dtExpenseReview.Rows.Cast <DataRow>();

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

                    ExpenseAccountEVM ea = null;

                    try
                    {
                        ea = FDLManager.ImportEAFromFile(file.FullName, false, false, true);

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

                            using (DBArchive db = new DBArchive())
                            {
                                // we must override recived EA with the same of current dbcontext istance
                                ExpenseAccount currentEA = db.ExpenseAccounts.SingleOrDefault(e => e.Id == ea.Id);

                                if (currentEA != null)
                                {
                                    FDL fdl = db.FDLs.SingleOrDefault(f => f.Id == currentEA.FDL);

                                    if (fdl != null)
                                    {
                                        currentEA.Status = fdl.Status;
                                    }
                                    else
                                    {
                                        currentEA.Status = (long)EFDLStatus.Accepted;
                                    }

                                    var expense = expenses.SingleOrDefault(e => !string.IsNullOrEmpty(e.Field <string>("Dbf_Foglio")) && FormatFDL(e.Field <string>("Dbf_Foglio")) == fdl.Id);
                                    currentEA.IsRefunded = expense != null && expense.Field <bool>("Dbf_Restituito");

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

                                    db.ExpenseAccounts.AddOrUpdate(currentEA);
                                    db.SaveChanges();
                                    Message($"Expense Account {currentEA.FDL} OK");
                                }
                                else
                                {
                                    Error("Missing EA on database. Should never happen.");
                                }
                            }
                        }
                        else
                        {
                            Error($"Failed to import EA from file: {file.FullName}");
                        }
                    }
                    catch (Exception ex)
                    {
                        Error($"Failed importing EA {ea?.FDL}. {ex}", ex);
                    }
                }

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

            return(result);
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        private bool CompileHourTable()
        {
            bool result = false;

            if (stopImport)
            {
                return(result);
            }

            StatusChanged("Importing Hours...");

            try
            {
                using (DBArchive db = new DBArchive())
                {
                    //Get enumerable rows fron datatable
                    IEnumerable <DataRow> collection = dtHours.Rows.Cast <DataRow>();

                    foreach (DataRow r in collection)
                    {
                        if (stopImport)
                        {
                            break;
                        }

                        DayEVM d = new DayEVM();

                        try
                        {
                            d.Timestamp = r.Field <DateTime>("Dbf_Data").ToUnixTimestamp();

                            d.Type = r.Field <byte>("dbf_Tipo_Giorno");

                            if (d.Type != 3 && d.Type != 6)
                            {
                                d.Type = 0;
                            }
                            else if (d.Type == 3)
                            {
                                d.Type = 1;
                            }
                            else if (d.Type == 6)
                            {
                                d.Type = 2;
                            }

                            d.Save(db);

                            //t = new Timesheet();
                            long timestamp = r.Field <DateTime>("Dbf_Data").ToUnixTimestamp();

                            //Add office hours
                            if (
                                r.Field <Int16>("Dbf_Uff_Inizio_AM") != 0 |
                                r.Field <Int16>("Dbf_Uff_Fine_AM") != 0 |
                                r.Field <Int16>("Dbf_Uff_Inizio_PM") != 0 |
                                r.Field <Int16>("Dbf_Uff_Fine_PM") != 0)
                            {
                                TimesheetEVM office = new TimesheetEVM();
                                office.Timestamp = timestamp;

                                office.TravelStartTimeAM = null;
                                office.WorkStartTimeAM   = r.Field <Int16>("Dbf_Uff_Inizio_AM") > 0 ? (long?)TimeSpan.FromMinutes(r.Field <Int16>("Dbf_Uff_Inizio_AM")).TotalSeconds : null;
                                office.WorkEndTimeAM     = r.Field <Int16>("Dbf_Uff_Fine_AM") > 0 ? (long?)TimeSpan.FromMinutes(r.Field <Int16>("Dbf_Uff_Fine_AM")).TotalSeconds : null;
                                office.TravelEndTimeAM   = null;
                                office.TravelStartTimePM = null;
                                office.WorkStartTimePM   = r.Field <Int16>("Dbf_Uff_Inizio_PM") > 0 ? (long?)TimeSpan.FromMinutes(r.Field <Int16>("Dbf_Uff_Inizio_PM")).TotalSeconds : null;
                                office.WorkEndTimePM     = r.Field <Int16>("Dbf_Uff_Fine_PM") > 0 ? (long?)TimeSpan.FromMinutes(r.Field <Int16>("Dbf_Uff_Fine_PM")).TotalSeconds : null;
                                office.TravelEndTimePM   = null;

                                if (db.Timesheets.Where(x => x.Timestamp == office.Timestamp && office.FDL == string.Empty).Count() == 0)
                                {
                                    office.Save(db);
                                }
                            }

                            // Factory association
                            short? factoryId = r.Field <short?>("Dbf_Impianto");
                            string fdlId     = FormatFDL(r.Field <string>("Dbf_Foglio"));

                            if (factoryId.HasValue && _factories.ContainsKey(factoryId.Value) && !string.IsNullOrEmpty(fdlId))
                            {
                                FDL fdl = db.FDLs.SingleOrDefault(f => f.Id == fdlId);

                                if (fdl != null)
                                {
                                    if (!fdl.Factory.HasValue)
                                    {
                                        fdl.Factory = _factories[factoryId.Value];
                                        db.FDLs.AddOrUpdate(fdl);
                                        db.SaveChanges();
                                    }
                                }
                                else
                                {
                                    Warning($"The FDL {fdlId} is missing on database. Impossible to assign the factory to the current timesheet. Day: {d.Date.ToShortDateString()}");
                                }
                            }

                            short? factory2Id = r.Field <short?>("Dbf_SecondoImpianto");
                            string fdl2Id     = FormatFDL(r.Field <string>("Dbf_SecondoFoglio"));

                            if (factory2Id.HasValue && _factories.ContainsKey(factory2Id.Value) && !string.IsNullOrEmpty(fdl2Id))
                            {
                                FDL fdl = db.FDLs.SingleOrDefault(f => f.Id == fdl2Id);

                                if (fdl != null && !fdl.Factory.HasValue)
                                {
                                    fdl.Factory = _factories[factory2Id.Value];
                                    db.FDLs.AddOrUpdate(fdl);
                                    db.SaveChanges();
                                }
                                else
                                {
                                    Warning($"The second FDL {fdlId} is missing on database. Impossible to assign the factory to the current timesheet. Day: {d.Date.ToShortDateString()}");
                                }
                            }

                            Message($"Day {d.Date.ToShortDateString()} OK");
                        }
                        catch (Exception ex)
                        {
                            Error($"Failed to import the Timesheet {d.Date.ToShortDateString()}. {ex}", ex);
                        }
                    }

                    db.SaveChanges();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                Error($"Failed importing the timesheets. {ex}", ex);
            }

            return(result);
        }
Esempio n. 4
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);
        }
Esempio n. 5
0
 public FDLDTO(FDL fdl)
 {
     Auto.Mapper.Map(fdl, this);
 }
Esempio n. 6
0
 public FDLDTO(FDL fdl)
 {
     Global.Mapper.Map(fdl, this);
 }