From d5712a79bfe321885d16bd63142a8804246d0f7d Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Thu, 23 Oct 2014 16:02:22 +0200 Subject: [PATCH] Enough code (and hacks) to bring ledgers UTD --- Logic/Financial/FinancialTransactionRows.cs | 2 +- Logic/Financial/PaymentGroup.cs | 13 ++- Logic/Financial/Payroll.cs | 41 ++++++++++ Logic/Logic.csproj | 1 + .../LogEntries/SalaryCreatedLogEntry.cs | 32 ++++++++ .../Public/Handlers/PaysonIpnListener.aspx.cs | 1 + Site5/Pages/v5/Admin/Hacks/TemporaryHack.aspx | 16 ++++ .../v5/Admin/Hacks/TemporaryHack.aspx.cs | 80 +++++++++++++++++++ 8 files changed, 181 insertions(+), 5 deletions(-) create mode 100644 Logic/Support/LogEntries/SalaryCreatedLogEntry.cs create mode 100644 Site5/Pages/v5/Admin/Hacks/TemporaryHack.aspx create mode 100644 Site5/Pages/v5/Admin/Hacks/TemporaryHack.aspx.cs diff --git a/Logic/Financial/FinancialTransactionRows.cs b/Logic/Financial/FinancialTransactionRows.cs index ba885c96..427bdb01 100644 --- a/Logic/Financial/FinancialTransactionRows.cs +++ b/Logic/Financial/FinancialTransactionRows.cs @@ -54,7 +54,7 @@ namespace Swarmops.Logic.Financial } } - public decimal BalanceCentsDelta + public long BalanceCentsDelta { get { diff --git a/Logic/Financial/PaymentGroup.cs b/Logic/Financial/PaymentGroup.cs index f4964e0f..332d8055 100644 --- a/Logic/Financial/PaymentGroup.cs +++ b/Logic/Financial/PaymentGroup.cs @@ -15,12 +15,17 @@ namespace Swarmops.Logic.Financial // empty ctor } - public static PaymentGroup FromIdentity (int paymentGroupId) + public static PaymentGroup FromIdentity(int paymentGroupId) { return FromBasic(SwarmDb.GetDatabaseForReading().GetPaymentGroup(paymentGroupId)); } - public static PaymentGroup FromBasic (BasicPaymentGroup basic) + private static PaymentGroup FromIdentityAggressive(int paymentGroupId) + { + return FromBasic(SwarmDb.GetDatabaseForWriting().GetPaymentGroup(paymentGroupId)); // "For writing" intentional - solves race conditions on create + } + + public static PaymentGroup FromBasic(BasicPaymentGroup basic) { return new PaymentGroup(basic); } @@ -28,9 +33,9 @@ namespace Swarmops.Logic.Financial public static PaymentGroup Create (Organization organization, DateTime timestamp, Currency currency, Person createdByPerson) { return - FromIdentity(SwarmDb.GetDatabaseForWriting().CreatePaymentGroup(organization.Identity, timestamp, + FromIdentityAggressive(SwarmDb.GetDatabaseForWriting().CreatePaymentGroup(organization.Identity, timestamp, currency.Identity, - System.DateTime.Now, createdByPerson.Identity)); + System.DateTime.Now, createdByPerson == null? 0: createdByPerson.Identity)); } public static PaymentGroup FromTag (Organization organization, string tag) diff --git a/Logic/Financial/Payroll.cs b/Logic/Financial/Payroll.cs index 9b306db8..afcb1404 100644 --- a/Logic/Financial/Payroll.cs +++ b/Logic/Financial/Payroll.cs @@ -1,7 +1,12 @@ +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 { @@ -50,5 +55,41 @@ namespace Swarmops.Logic.Financial { 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 + + } + } } } diff --git a/Logic/Logic.csproj b/Logic/Logic.csproj index 35c496a1..684b725d 100644 --- a/Logic/Logic.csproj +++ b/Logic/Logic.csproj @@ -224,6 +224,7 @@ + diff --git a/Logic/Support/LogEntries/SalaryCreatedLogEntry.cs b/Logic/Support/LogEntries/SalaryCreatedLogEntry.cs new file mode 100644 index 00000000..1f52e823 --- /dev/null +++ b/Logic/Support/LogEntries/SalaryCreatedLogEntry.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Swarmops.Logic.Financial; + +namespace Swarmops.Logic.Support.LogEntries +{ + [Serializable] + public class SalaryCreatedLogEntry: FinancialActionBase + { + [Obsolete("Do not call this constructor directly.", true)] + public SalaryCreatedLogEntry() + { + // empty ctor needed for serializability; should not be called + } + + public SalaryCreatedLogEntry(Salary salary) + { + this.ActingPersonId = 0; + this.BeneficiaryPersonId = salary.PayrollItem.PersonId; + this.Amount = salary.NetSalaryCents/100.0; + this.Currency = salary.Budget.Organization.Currency.Code; + this.DateTime = DateTime.UtcNow; + this.FinancialAccountId = salary.Budget.Identity; + this.FinancialAccountName = salary.Budget.Name; + this.OrganizationId = salary.PayrollItem.OrganizationId; + this.OwnerPersonId = salary.PayrollItem.ReportsToPersonId; + this.OwnerPersonName = salary.PayrollItem.ReportsToPerson.Canonical; + } + } +} diff --git a/Site4/Pages/Public/Handlers/PaysonIpnListener.aspx.cs b/Site4/Pages/Public/Handlers/PaysonIpnListener.aspx.cs index ebc3c431..0476e7d8 100644 --- a/Site4/Pages/Public/Handlers/PaysonIpnListener.aspx.cs +++ b/Site4/Pages/Public/Handlers/PaysonIpnListener.aspx.cs @@ -181,6 +181,7 @@ public partial class Pages_Public_Handlers_PaysonIpnListener : System.Web.UI.Pag Payment payment = Payment.CreateSingle(invoice.Organization, DateTime.Now, invoice.Currency, expectedNetCents, invoice, null); // HACK HACK HACK: says we always received the expected amount transaction.Dependency = payment.Group; + payment.Group.Open = false; } else if (fee > 0.0) { diff --git a/Site5/Pages/v5/Admin/Hacks/TemporaryHack.aspx b/Site5/Pages/v5/Admin/Hacks/TemporaryHack.aspx new file mode 100644 index 00000000..373acae4 --- /dev/null +++ b/Site5/Pages/v5/Admin/Hacks/TemporaryHack.aspx @@ -0,0 +1,16 @@ +<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TemporaryHack.aspx.cs" Inherits="Pages_v5_Admin_Hacks_TemporaryHack" %> + + + + + + + + +
+
+ +
+
+ + diff --git a/Site5/Pages/v5/Admin/Hacks/TemporaryHack.aspx.cs b/Site5/Pages/v5/Admin/Hacks/TemporaryHack.aspx.cs new file mode 100644 index 00000000..f73045bc --- /dev/null +++ b/Site5/Pages/v5/Admin/Hacks/TemporaryHack.aspx.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.UI; +using System.Web.UI.WebControls; + +// This page executes code on page load. Used for various tests of logic. Not supposed to ever be linked from something. +using Swarmops.Logic.Financial; +using Swarmops.Logic.Structure; +using Swarmops.Logic.Swarm; + +public partial class Pages_v5_Admin_Hacks_TemporaryHack : System.Web.UI.Page +{ + protected void Page_Load(object sender, EventArgs e) + { + FinancialAccount bankAccount = FinancialAccount.FromIdentity(29); + Organization euroPirates = Organization.FromIdentity(7); + + FinancialTransactions unbalancedTransactions = FinancialTransactions.GetUnbalanced(euroPirates); + + // assume all in bank account + + foreach (FinancialTransaction transaction in unbalancedTransactions) + { + if (transaction.Description[0] == 'H' && Char.IsDigit(transaction.Description[1]) && transaction.Rows.BalanceCentsDelta > 0) + { + // outbound intl invoice. Add an untracked intl invoice 10 days prior, map it to this + + OutboundInvoice newInvoice = OutboundInvoice.Create(euroPirates, Person.FromIdentity(1), + transaction.DateTime, euroPirates.FinancialAccounts.IncomeSales, "Untracked", + "untracked@example.com", string.Empty, euroPirates.Currency, false, string.Empty); + newInvoice.AddItem("Untracked item", transaction.Rows.BalanceCentsDelta); + + // Doesn't close + + // Add transaction + + // Create the financial transaction with rows + + FinancialTransaction invoiceTransaction = + FinancialTransaction.Create(euroPirates.Identity, transaction.DateTime.AddDays(-10), + "Outbound Invoice #" + newInvoice.Identity); + + invoiceTransaction.AddRow(euroPirates.FinancialAccounts.AssetsOutboundInvoices, newInvoice.AmountCents, null); + invoiceTransaction.AddRow(euroPirates.FinancialAccounts.IncomeSales, -newInvoice.AmountCents, null); + invoiceTransaction.Dependency = newInvoice; + + transaction.AddRow(euroPirates.FinancialAccounts.AssetsOutboundInvoices, -newInvoice.AmountCents, null); + + Payment payment = Payment.CreateSingle(euroPirates, transaction.DateTime, euroPirates.Currency, + newInvoice.AmountCents, newInvoice, null); + + transaction.Dependency = payment.Group; + payment.Group.Open = false; + + } + + if (transaction.Description == "Kostnad" && transaction.Rows.BalanceCentsDelta < 0 && + transaction.Rows.BalanceCentsDelta > -10000) + { + // Bank fee. + + transaction.AddRow(euroPirates.FinancialAccounts.CostsBankFees, -transaction.Rows.BalanceCentsDelta, + null); + } + + if (transaction.Description.StartsWith("Paypal ")) + { + // Bank fee. + + transaction.AddRow(euroPirates.FinancialAccounts.CostsBankFees, -transaction.Rows.BalanceCentsDelta, + null); + } + } + + Payouts.AutomatchAgainstUnbalancedTransactions(euroPirates); + + } +} \ No newline at end of file