private void PayrollRevertExport() { lock (payrollExportLock) { logger.LogDebug("Restoring exported items to unexported state."); try { // Allow only one export at a time to run. payrollExport = new PayrollExport(logger, IntegrationTroToPayrollFolder, database, config); payrollExport.PayrollRevert(Message); } catch (Exception ex) { logger.LogError("Failed to revert payroll document export.", ex); remoteConnection.ReportError(originalMessage.Value, originalResponse.Value, ex.Message); } } }
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."); }
/// <summary> /// Appends entrytype hours or amounts to a worksheet /// </summary> /// <typeparam name="EntryType">Abcence, Day or Timesheet</typeparam> /// <param name="worksheet">Worksheet to append work hours or amounts</param> /// <param name="row">Row position where to start</param> /// <returns>Row position where to start next data to be written</returns> private int AppendTimesheetAndAbcenceAndDayDataToWorksheet <EntryType>(ExcelWorksheet worksheet, int row) where EntryType : EntryToPayroll { var collection = this as EntriesToPayroll <EntryType>; // sortti by asentajan nimi, päivämäärä, jip: collection.Sort(delegate(EntryType x, EntryType y) { string name_x = (string)x.Document.GetValue("__UserSortName", ""); string name_y = (string)y.Document.GetValue("__UserSortName", ""); DateTime pvm_x = Convert.ToDateTime(x.Document.GetValue("date", DateTime.MinValue)); DateTime pvm_y = Convert.ToDateTime(y.Document.GetValue("date", DateTime.MinValue)); if ((name_x == null && name_y == null) || name_x == name_y) { return(pvm_x.CompareTo(pvm_y)); } if (name_x == null) { return(-1); } if (name_y == null) { return(1); } return(name_x.CompareTo(name_y)); }); // collection --> excel foreach (EntryType item in collection) { //If no dayentry then its timesheet or abcense if (typeof(EntryType) == typeof(Day) == false) { if (item.PayType == null || !Convert.ToBoolean(item.PayType.GetValue("countsasregularhours", false))) { continue; } } if ((exportTask.onlyExportEntriesNotAcceptedByManager && exportTask.onlyExportEntriesNotAcceptedByWorker) || exportTask.onlyExportEntriesNotAcceptedByWorker) { if (Convert.ToBoolean(item.Document.GetValue("approvedbyworker", false))) { continue; } } else if (exportTask.onlyExportEntriesNotAcceptedByManager && (Convert.ToBoolean(item.Document.GetValue("approvedbymanager", false)) || !Convert.ToBoolean(item.Document.GetValue("approvedbyworker", false)))) { continue; } if (exportTask.onlyExportExportedEntries && !Convert.ToBoolean(item.Document.GetValue("exported_visma", false))) { continue; } // If no special selectors are selected, only take unexported entries accepted by user and manager. if ((!exportTask.onlyExportEntriesNotAcceptedByManager && !exportTask.onlyExportEntriesNotAcceptedByWorker && !exportTask.onlyExportExportedEntries) && (Convert.ToBoolean(item.Document.GetValue("exported_visma", false)) || !Convert.ToBoolean(item.Document.GetValue("approvedbymanager", false)) || !Convert.ToBoolean(item.Document.GetValue("approvedbyworker", false)))) { continue; } worksheet.Cells[row, DateExcelColumn].Value = Convert.ToDateTime(item.Document.GetValue("date", DateTime.MinValue)).ToString(ExcelDateFormat); //worksheet.Cells[row, NameExcelColumn].Value = Convert.ToString(item.Document.GetValue("__user__displayname", "")); worksheet.Cells[row, NameExcelColumn].Value = Convert.ToString(item.Document.GetValue("__UserSortName", "")); worksheet.Cells[row, UserIdExcelColumn].Value = item.UserIdentifier; if (typeof(EntryType) == typeof(Day)) { worksheet.Cells[row, AmountExcelColumn].Value = Convert.ToInt32(item.Document.GetValue("amount", 0)); } else { worksheet.Cells[row, HoursExcelColumn].Value = PayrollExport.MillisecondsToHours(Convert.ToInt32(item.Document.GetValue("duration", 0))); } worksheet.Cells[row, PayTypeNameExcelColumn].Value = Convert.ToString(item.PayType.GetValue("name", "")); worksheet.Cells[row, PayTypeExcelColumn].Value = item.PayTypeId; worksheet.Cells[row, ProfitcenterExcelColumn].Value = Convert.ToString(item.Document.GetValue("__profitcenter__displayname", "")); worksheet.Cells[row, SupervisorExcelColumn].Value = Convert.ToString(item.Document.GetValue("__supervisor__displayname", "")); worksheet.Cells[row, UniqueIdExcelColumn].Value = item.ObjectId; row++; } return(row); }