예제 #1
0
        private void RunTroToPayrollExport()
        {
            logger.LogDebug("Starting TRO to payroll export thread.");

            {
                var sw = new Stopwatch();

                DateTime lockDayOffset;
                DateTime exportDayOffset;
                long     payrollLockOffset   = 0; // = 32400000;  //9h
                long     payrollExportOffset = 0;;
                long     stopwatchInterval   = (long)config["automaticexportinterval"].GetValueOrDefault(1800000);

                bool lockAutomatically = (bool)config["lockautomatically"].GetValueOrDefault(false);
                if (lockAutomatically)
                {
                    //Default 3h -> ms
                    payrollLockOffset = (long)config["payrolllockoffset"].GetValueOrDefault(10800000);
                    sw.Start();
                }
                bool exportAutomatically = (bool)config["exportautomatically"].GetValueOrDefault(false);
                if (exportAutomatically)
                {
                    //Default 1d -> ms
                    payrollExportOffset = (long)config["payrollexportoffset"].GetValueOrDefault(86400000);
                    if (!sw.IsRunning)
                    {
                        sw.Start();
                    }
                }

                while (!stopping.WaitOne(5021))
                {
                    try
                    {
                        lock (payrollExportLock)
                        {
                            exportTask = PayrollExport.GetNextPayrollExportTask(database);
                            if (exportTask != null)
                            {
                                logger.LogInfo("Handling next payroll export task");
                                payrollExport = new PayrollExport(logger, IntegrationTroToPayrollFolder, database, config);
                                payrollExport.ExportDocuments(exportTask);
                            }
                            //Check for locking payroll
                            if (lockAutomatically || exportAutomatically)
                            {
                                if (sw.Elapsed.TotalMilliseconds > stopwatchInterval)
                                {
                                    if (lockAutomatically)
                                    {
                                        lockDayOffset = DateTime.Today + TimeSpan.FromMilliseconds(payrollLockOffset);
                                        if (lockDayOffset < DateTime.Now)
                                        {
                                            var andQueries = new List <IMongoQuery>();
                                            andQueries.Add(Query.LTE("enddate", lockDayOffset));
                                            andQueries.Add(Query.EQ("locked", false));
                                            andQueries.Add(Query.EQ("automaticallylocked", false));

                                            MongoCollection <BsonDocument> collection = database.GetCollection("payrollperiod");
                                            MongoCursor <BsonDocument>     cursor     = collection.Find(Query.And(andQueries));
                                            if (cursor.Count() > 0)
                                            {
                                                foreach (var item in cursor)
                                                {
                                                    if ((DateTime)item["enddate"] < lockDayOffset)
                                                    {
                                                        logger.LogInfo("Automatically locking payroll period", item[DBDocument.Id]);
                                                        item["automaticallylocked"] = true;
                                                        item["locked"] = true;
                                                        item["automaticallylocked_time"] = DateTime.UtcNow;
                                                        collection.Save(item, WriteConcern.Acknowledged);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    if (exportAutomatically)
                                    {
                                        var andQueries = new List <IMongoQuery>();
                                        exportDayOffset = DateTime.Today + TimeSpan.FromMilliseconds(payrollExportOffset);
                                        andQueries.Add(Query.LTE("enddate", exportDayOffset));
                                        andQueries.Add(Query.EQ("locked", true));
                                        andQueries.Add(Query.EQ("automaticallyexported", false));

                                        MongoCollection <BsonDocument> collection = database.GetCollection("payrollperiod");
                                        MongoCursor <BsonDocument>     cursor     = collection.Find(Query.And(andQueries));
                                        if (cursor.Count() > 0)
                                        {
                                            if (exportDayOffset < DateTime.Now)
                                            {
                                                foreach (var item in cursor)
                                                {
                                                    logger.LogInfo("Add automatical export task for payroll", item[DBDocument.Id]);
                                                    exportTask    = PayrollExport.GetNextPayrollExportTask(database, true, item);
                                                    payrollExport = new PayrollExport(logger, IntegrationTroToPayrollFolder, database, config);
                                                    payrollExport.ExportDocuments(exportTask);
                                                    item["automaticallyexported"] = true;
                                                    collection.Save(item, WriteConcern.Acknowledged);
                                                }
                                            }
                                        }
                                    }
                                    //Reset stopwatch for next interval
                                    sw.Reset();
                                    sw.Start();
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.LogError("Failed import entries to payroll.", ex);
                    }

                    stopping.WaitOne(payrollExportPollingInterval);
                }
            }

            logger.LogError("Tro to payroll export thread stopping.");
        }
예제 #2
0
 /// <summary>
 /// Creates instance of EntriesToPayroll
 /// </summary>
 /// <param name="list">Helper list to create CSV and Audit files</param>
 /// <param name="exportTask">The export task object with info about current export</param>
 public EntriesToPayroll(List <FieldDetails> list, PayrollExportTask exportTask)
 {
     ParsedConfig    = list;
     this.exportTask = exportTask;
 }