using System; using System.Collections.Generic; using System.Globalization; using Swarmops.Basic.Enums; using Swarmops.Basic.Types; using Swarmops.Database; using Swarmops.Logic.Structure; using Swarmops.Logic.Support; using Swarmops.Logic.Support.LogEntries; namespace Swarmops.Logic.Financial { public class Payroll: PluralBase { /// /// Returns all open payroll items for an organization. /// /// The organization. /// The payroll. public static Payroll ForOrganization (Organization organization) { return ForOrganization(organization, false); } /// /// Returns all payroll items for an organization, optionally including closed ones. /// /// The organization to filter for. /// True to include closed records. /// The payroll. public static Payroll ForOrganization (Organization organization, bool includeClosed) { return FromArray(SwarmDb.GetDatabaseForReading().GetPayroll(organization, includeClosed? DatabaseCondition.None : DatabaseCondition.OpenTrue)); } /// /// Returns all open payroll for all organizations. /// /// The payroll. public static Payroll GetAll() { return GetAll(false); } /// /// Returns all payroll for all organizations, optionally including closed ones. /// /// True to include closed records. /// The payroll. public static Payroll GetAll(bool includeClosed) { return FromArray(SwarmDb.GetDatabaseForReading().GetPayroll(includeClosed? DatabaseCondition.None : DatabaseCondition.OpenTrue)); } /// /// Processes all payroll for all organizations system-wide. Should run on the 1st of every month. /// public static void ProcessMonthly() { DateTime today = DateTime.UtcNow; string lastRun = Persistence.Key["LastSalaryRun"]; string expectedLastRun = today.ToString("yyyyMM", CultureInfo.InvariantCulture); if (lastRun != null && String.Compare(lastRun, expectedLastRun, CultureInfo.InvariantCulture, CompareOptions.IgnoreCase) >= 0) { // nothing to do, return return; } Persistence.Key["LastSalaryRun"] = expectedLastRun; // Process the payroll for all organizations. Assume payday is 25th. // TODO: Different payday per organization? Payroll payroll = Payroll.GetAll(); DateTime payday = new DateTime(today.Year, today.Month, 25); foreach (PayrollItem payrollItem in payroll) { Salary salary = Salary.Create(payrollItem, payday); SwarmopsLog.CreateEntry(payrollItem.Person, new SalaryCreatedLogEntry(salary)); // TODO: CREATE SALARY SPECIFICATION, SEND TO PERSON } } } }