A lot more work on payouts

This commit is contained in:
Rick Falkvinge
2013-02-11 21:36:33 +00:00
parent cca21ac544
commit 6ac8fc219f
10 changed files with 415 additions and 28 deletions
+30
View File
@@ -77,5 +77,35 @@ namespace Swarmops.Logic.Financial
{
get { return FinancialAccount.FromIdentity(this.BudgetId); }
}
public new bool Open
{
get { return base.Open; }
set
{
if (base.Open == value)
{
return;
}
SwarmDb.GetDatabaseForWriting().SetCashAdvanceOpen(this.Identity, value);
base.Open = value;
}
}
public new bool PaidOut
{
get { return base.PaidOut; }
set
{
if (base.PaidOut == value)
{
return;
}
SwarmDb.GetDatabaseForWriting().SetCashAdvancePaidOut(this.Identity, value);
base.PaidOut = value;
}
}
}
}
+12 -1
View File
@@ -27,7 +27,18 @@ namespace Swarmops.Logic.Financial
get
{
CashAdvances result = new CashAdvances();
result.AddRange(this.Where(cashAdvance => cashAdvance.Attested == false));
result.AddRange(this.Where(cashAdvance => !cashAdvance.Attested));
return result;
}
}
public CashAdvances WhereAttested
{
get
{
CashAdvances result = new CashAdvances();
result.AddRange(this.Where(cashAdvance => cashAdvance.Attested));
return result;
}
+93 -4
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Swarmops.Basic.Enums;
using Swarmops.Basic.Types;
@@ -23,6 +24,8 @@ namespace Swarmops.Logic.Financial
public static Payout Create (Payout payout)
{
throw new NotImplementedException(); // Seems not to be needed
// This is quite unique in PW4 -- this is the only class which is pre-constructed and
// can take an instance of its own kind as parameter in creation
@@ -43,6 +46,7 @@ namespace Swarmops.Logic.Financial
DependentInvoices = new InboundInvoices();
DependentSalariesNet = new Salaries();
DependentSalariesTax = new Salaries();
DependentCashAdvances = new CashAdvances();
BasicFinancialDependency[] dependencies = SwarmDb.GetDatabaseForReading().GetPayoutDependencies(this.Identity);
@@ -68,6 +72,10 @@ namespace Swarmops.Logic.Financial
}
break;
case FinancialDependencyType.CashAdvance:
DependentCashAdvances.Add(CashAdvance.FromIdentity(dependency.ForeignId));
break;
default:
throw new NotImplementedException("Unknown financial dependency type in Payout.LoadDependencies(): " + dependency.ToString());
}
@@ -91,6 +99,7 @@ namespace Swarmops.Logic.Financial
public ExpenseClaims DependentExpenseClaims;
public Salaries DependentSalariesNet;
public Salaries DependentSalariesTax;
public CashAdvances DependentCashAdvances;
public decimal Amount
{
@@ -168,7 +177,60 @@ namespace Swarmops.Logic.Financial
return null;
}
if (components [0][0] == 'A')
{
// Cash advance(s). This could be joined with expense claims but isn't as of now.
string bank = string.Empty;
string account = string.Empty;
List<int> identityList = new List<int>();
Int64 amountCents = 0;
int organizationId = 0;
foreach (string component in components)
{
int advanceId = Int32.Parse(component.Substring(1));
CashAdvance advance = CashAdvance.FromIdentity(advanceId);
identityList.Add(advanceId);
if (bank.Length < 1)
{
Person asker = advance.Person;
bank = asker.BankName;
account = asker.BankAccount;
organizationId = advance.OrganizationId;
}
amountCents += advance.AmountCents;
advance.PaidOut = true;
advance.Open = false;
}
string referenceString = string.Empty;
if (identityList.Count == 1)
{
referenceString = "Cash Advance #" + identityList[0].ToString(CultureInfo.InvariantCulture);
}
else
{
identityList.Sort();
referenceString = "Cash Advances " + Formatting.GenerateRangeString(identityList);
}
int payoutId = SwarmDb.GetDatabaseForWriting().CreatePayout(organizationId, bank, account,
referenceString, amountCents, DateTime.Today.AddDays(1),
creator.Identity);
foreach (int advanceId in identityList)
{
SwarmDb.GetDatabaseForWriting().CreatePayoutDependency(payoutId, FinancialDependencyType.CashAdvance,
advanceId);
}
return FromIdentity(payoutId);
}
if (components [0][0] == 'C')
{
// Expense claims.
@@ -334,26 +396,36 @@ namespace Swarmops.Logic.Financial
}
string result = string.Empty;
// ReSharper disable LoopCanBeConvertedToQuery
foreach (InboundInvoice invoice in DependentInvoices)
{
result += "|I" + invoice.Identity.ToString();
result += "|I" + invoice.Identity.ToString(CultureInfo.InvariantCulture);
}
foreach (ExpenseClaim claim in DependentExpenseClaims)
{
result += "|C" + claim.Identity.ToString();
result += "|C" + claim.Identity.ToString(CultureInfo.InvariantCulture);
}
foreach (CashAdvance advance in DependentCashAdvances)
{
result += "|A" + advance.Identity.ToString(CultureInfo.InvariantCulture);
}
foreach (Salary salary in DependentSalariesNet)
{
result += "|S" + salary.Identity.ToString();
result += "|S" + salary.Identity.ToString(CultureInfo.InvariantCulture);
}
foreach (Salary salary in DependentSalariesTax)
{
result += "|T" + salary.Identity.ToString();
result += "|T" + salary.Identity.ToString(CultureInfo.InvariantCulture);
}
// ReSharper restore LoopCanBeConvertedToQuery
if (result.Length > 0)
{
return result.Substring(1);
@@ -382,6 +454,10 @@ namespace Swarmops.Logic.Financial
{
return this.DependentExpenseClaims[0].ClaimerCanonical;
}
else if (this.DependentCashAdvances.Count > 0)
{
return this.DependentCashAdvances[0].Person.Canonical;
}
else if (this.DependentSalariesNet.Count > 0)
{
return this.DependentSalariesNet[0].PayrollItem.PersonCanonical;
@@ -481,12 +557,25 @@ namespace Swarmops.Logic.Financial
public void UndoPayout()
{
// This only works on still-open payouts
if (!this.Open)
{
throw new InvalidOperationException("Payout is already closed - cannot undo");
}
foreach (ExpenseClaim claim in this.DependentExpenseClaims)
{
claim.Repaid = false;
claim.Open = true;
}
foreach (CashAdvance advance in this.DependentCashAdvances)
{
advance.PaidOut = false;
advance.Open = true;
}
foreach (InboundInvoice invoice in this.DependentInvoices)
{
invoice.Open = true;
+77 -22
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Swarmops.Basic.Types;
using Swarmops.Database;
@@ -43,6 +44,7 @@ namespace Swarmops.Logic.Financial
AddUnpaidExpenseClaims(result, organizationId);
AddUnpaidInboundInvoices(result, organizationId);
AddUnpaidSalaries(result, organizationId);
AddUnpaidCashAdvances(result, organizationId);
return result;
}
@@ -75,7 +77,7 @@ namespace Swarmops.Logic.Financial
// No. Create a new payout for this person.
BasicPayout basicPayout = new BasicPayout(0, organizationId, claim.Claimer.BankName,
claim.Claimer.BankClearing+" / "+claim.Claimer.BankAccount, string.Empty, 0,
claim.Claimer.BankClearing + " / " + claim.Claimer.BankAccount, string.Empty, 0,
DateTime.MinValue, false, DateTime.Now, 0);
Payout payout = Payout.FromBasic(basicPayout);
@@ -105,12 +107,12 @@ namespace Swarmops.Logic.Financial
if (claimIds.Count == 1)
{
payout.Reference = String.Format("Expense #{0}", claimIds[0]);
payout.Reference = "[Loc]Financial_ExpenseClaimSpecification|" + claimIds[0].ToString(CultureInfo.InvariantCulture);
}
else
{
claimIds.Sort();
payout.Reference = String.Format("Expenses {0}", Formatting.GenerateRangeString(claimIds));
payout.Reference = "[Loc]Financial_ExpenseClaimsSpecification|" + Formatting.GenerateRangeString(claimIds);
}
if (newAmountCents > 0)
@@ -119,30 +121,83 @@ namespace Swarmops.Logic.Financial
}
}
// Lastly, add the keep-separate claims.
}
foreach (ExpenseClaim claim in claims)
private static void AddUnpaidCashAdvances(Payouts payoutList, int organizationId)
{
CashAdvances advances = CashAdvances.ForOrganization(Organization.FromIdentity(organizationId));
advances = advances.WhereAttested;
Dictionary<int, Payout> payoutLookup = new Dictionary<int, Payout>();
foreach (CashAdvance advance in advances)
{
if (claim.Open)
{
if (claim.Attested && claim.Validated && !claim.Repaid && claim.KeepSeparate)
{
BasicPayout basicPayout = new BasicPayout(0, organizationId, claim.Claimer.BankName,
claim.Claimer.BankClearing + " / " + claim.Claimer.BankAccount, string.Empty, claim.AmountCents,
DateTime.MinValue, false, DateTime.Now, 0);
Payout payout = Payout.FromBasic(basicPayout);
payout.Reference = "Cash Advance #" + claim.Identity;
payout.DependentExpenseClaims.Add(claim);
// If ready for payout, add to list.
payoutList.Add(payout);
}
if (!advance.Open || !advance.Attested || advance.PaidOut)
{
throw new InvalidOperationException("Got into loop with closed/unattested/paid-out cash advances - this is not a possible state");
}
if (payoutLookup.ContainsKey(advance.PersonId))
{
// Yes. Add claim to list.
payoutLookup[advance.PersonId].DependentCashAdvances.Add(advance);
}
else
{
// No. Create a new payout for this person.
BasicPayout basicPayout = new BasicPayout(0, organizationId, advance.Person.BankName,
advance.Person.BankClearing + " / " + advance.Person.BankAccount, string.Empty, 0,
DateTime.MinValue, false, DateTime.Now, 0);
Payout payout = Payout.FromBasic(basicPayout);
payout.DependentCashAdvances.Add(advance);
payoutLookup[advance.PersonId] = payout;
}
}
// We now have the list of payouts and the associated claims, but the amounts aren't set on the
// payouts. This will be the next step, as we assemble the list.
foreach (Payout payout in payoutLookup.Values)
{
Int64 newAmountCents = 0;
List<int> advanceIds = new List<int>();
foreach (CashAdvance advance in payout.DependentCashAdvances)
{
newAmountCents += advance.AmountCents;
advanceIds.Add(advance.Identity);
}
payout.AmountCents = newAmountCents;
if (advanceIds.Count == 1)
{
payout.Reference = "[Loc]Financial_CashAdvanceSpecification|" + advanceIds[0].ToString(CultureInfo.InvariantCulture);
}
else
{
advanceIds.Sort();
payout.Reference = "[Loc]Financial_CashAdvancesSpecification|" + Formatting.GenerateRangeString(advanceIds);
}
if (newAmountCents > 0)
{
payoutList.Add(payout);
}
}
}
private static void AddUnpaidInboundInvoices (Payouts payoutList, int organizationId)
private static void AddUnpaidInboundInvoices(Payouts payoutList, int organizationId)
{
InboundInvoices invoices = InboundInvoices.ForOrganization(Organization.PPSE);
@@ -183,7 +238,7 @@ namespace Swarmops.Logic.Financial
Person employee = payrollItem.Person;
BasicPayout basicPayout = new BasicPayout(0, organizationId, employee.BankName,
employee.BankAccount, "Salary " + salary.PayoutDate.ToString("yyyy-MMM"),
employee.BankAccount, "[Loc]Financial_SalarySpecification|[Date]" + salary.PayoutDate.ToString(CultureInfo.InvariantCulture),
salary.NetSalaryCents, salary.PayoutDate, false, DateTime.Now, 0);
Payout payout = Payout.FromBasic(basicPayout);
@@ -217,16 +272,16 @@ namespace Swarmops.Logic.Financial
if (identityList.Count == 1)
{
referenceString = "Tax for salary #" + identityList[0].ToString();
referenceString = "[Loc]Financial_TaxSpecification|" + identityList[0].ToString();
}
else
{
identityList.Sort();
referenceString = "Tax for salaries " + Formatting.GenerateRangeString(identityList);
referenceString = "[Loc]Financial_TaxesSpecification|" + Formatting.GenerateRangeString(identityList);
}
BasicPayout basicPayout = new BasicPayout(0, organizationId, "The Tax Man",
BasicPayout basicPayout = new BasicPayout(0, organizationId, "[Loc]Financial_TheTaxMan",
string.Empty, referenceString,
taxTotalCents, payDay, false, DateTime.Now, 0);
Payout payout = Payout.FromBasic(basicPayout);
+30
View File
@@ -162,9 +162,21 @@
<data name="Financial_CashAdvance" xml:space="preserve">
<value>Advance</value>
</data>
<data name="Financial_CashAdvanceSpecification" xml:space="preserve">
<value>Cash Advance #{0}</value>
</data>
<data name="Financial_CashAdvancesSpecification" xml:space="preserve">
<value>Cash Advances {0}</value>
</data>
<data name="Financial_ExpenseClaim" xml:space="preserve">
<value>Expense</value>
</data>
<data name="Financial_ExpenseClaimSpecification" xml:space="preserve">
<value>Expense #{0}</value>
</data>
<data name="Financial_ExpenseClaimsSpecification" xml:space="preserve">
<value>Expenses {0}</value>
</data>
<data name="Financial_InvoiceInbound" xml:space="preserve">
<value>Invoice</value>
</data>
@@ -177,9 +189,27 @@
<data name="Financial_Salary" xml:space="preserve">
<value>Salary</value>
</data>
<data name="Financial_SalarySpecification" xml:space="preserve">
<value>Salary for {0:yyyy-MMM}</value>
<comment>The payout date is inserted for date, like "Salary for 2012-Aug".</comment>
</data>
<data name="Financial_TaxesSpecification" xml:space="preserve">
<value>Tax for salaries {0}</value>
</data>
<data name="Financial_TaxSpecification" xml:space="preserve">
<value>Tax for salary #{0}</value>
</data>
<data name="Financial_TheTaxMan" xml:space="preserve">
<value>The Tax Man</value>
<comment>Used as recipient for taxes.</comment>
</data>
<data name="Financial_YearlyResult" xml:space="preserve">
<value>Yearly result</value>
</data>
<data name="Global_ASAP" xml:space="preserve">
<value>ASAP</value>
<comment>Used as time indicator for "right away" (as soon as possible) in place of dates where in a hurry</comment>
</data>
<data name="Global_Cancel" xml:space="preserve">
<value>Cancel</value>
<comment>Displayed as cancellation link on changing something</comment>
@@ -190,6 +190,32 @@
<value>Salary #{0} for {1} was DE-attested. It will not be paid out unless attested again.</value>
<comment>When somebody undoes a previous attestation.</comment>
</data>
<data name="PayOutMoney_GridHeader_Amount" xml:space="preserve">
<value>Amount</value>
<comment>Amount to be paid out</comment>
</data>
<data name="PayOutMoney_GridHeader_BankAccount" xml:space="preserve">
<value>Clearing / Account</value>
<comment>Will display as "XXXX / YYYYYYYY" where XXXX is the clearing number</comment>
</data>
<data name="PayOutMoney_GridHeader_BankName" xml:space="preserve">
<value>Bank</value>
</data>
<data name="PayOutMoney_GridHeader_DueDate" xml:space="preserve">
<value>Due Date</value>
</data>
<data name="PayOutMoney_GridHeader_PaidOut" xml:space="preserve">
<value>Paid</value>
<comment>Possibly in imperative; a click in this column indicates payment has been done. It is an action column, not an information column.</comment>
</data>
<data name="PayOutMoney_GridHeader_Recipient" xml:space="preserve">
<value>Recipient</value>
<comment>Can be org (for invoice) or person (for salary, expense, advance)</comment>
</data>
<data name="PayOutMoney_GridHeader_Reference" xml:space="preserve">
<value>Reference</value>
<comment>Depends on cost type - can be an OCR number for an invoice, a cash advance number, a salary spec, etc.</comment>
</data>
<data name="PayOutMoney_Header" xml:space="preserve">
<value>Costs Ready For Payout</value>
</data>
@@ -0,0 +1 @@
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Json-PayableCosts.aspx.cs" Inherits="Swarmops.Frontend.Pages.Financial.JsonPayableCosts" %>
@@ -0,0 +1,137 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Swarmops.Basic.Interfaces;
using Swarmops.Logic.Financial;
using Swarmops.Logic.Security;
using Swarmops.Logic.Structure;
using Swarmops.Logic.Support;
using Swarmops.Logic.Swarm;
namespace Swarmops.Frontend.Pages.Financial
{
public partial class JsonPayableCosts : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Current authentication
string identity = HttpContext.Current.User.Identity.Name;
string[] identityTokens = identity.Split(',');
string userIdentityString = identityTokens[0];
string organizationIdentityString = identityTokens[1];
int currentUserId = Convert.ToInt32(userIdentityString);
int currentOrganizationId = Convert.ToInt32(organizationIdentityString);
_currentUser = Person.FromIdentity(currentUserId);
Authority authority = _currentUser.GetAuthority();
_currentOrganization = Organization.FromIdentity(currentOrganizationId);
Response.ContentType = "application/json";
// Assert economic access
// TODO: Set language for localization
// Get all payable items
Payouts payouts = Payouts.Construct(_currentOrganization);
// Format as JSON and return
string json = FormatAsJson(payouts);
Response.Output.WriteLine(json);
Response.End();
}
private string FormatAsJson(Payouts payouts)
{
StringBuilder result = new StringBuilder(16384);
string hasDoxString =
"<img src=\\\"/Images/Icons/iconshock-glass-16px.png\\\" onmouseover=\\\"this.src='/Images/Icons/iconshock-glass-16px-hot.png';\\\" onmouseout=\\\"this.src='/Images/Icons/iconshock-glass-16px.png';\\\" onclick=\\\"alert('foo');\\\" style=\\\"cursor:pointer\\\" />";
result.Append("{\"rows\":[");
DateTime today = DateTime.Today;
foreach (Payout payout in payouts)
{
result.Append("{");
result.AppendFormat(
"\"itemId\":\"{0}\"," +
"\"due\":\"{1}\"," +
"\"recipient\":\"{2}\"," +
"\"bank\":\"{3}\"," +
"\"account\":\"{4}\"," +
"\"reference\":\"{5}\"," +
"\"amount\":\"{6:N2}\"," +
"\"action\":\"<span style=\\\"position:relative;top:3px\\\">" +
"<img id=\\\"IconApproval{0}\\\" class=\\\"LocalIconApproval\\\" baseid=\\\"{0}\\\" height=\\\"16\\\" width=\\\"16\\\" />" +
"<img id=\\\"IconApproved{0}\\\" class=\\\"LocalIconApproved\\\" baseid=\\\"{0}\\\" height=\\\"16\\\" width=\\\"16\\\" />\"",
payout.ProtoIdentity,
(payout.ExpectedTransactionDate <= today? Resources.Global.Global_ASAP: payout.ExpectedTransactionDate.ToShortDateString()),
TryLocalize(payout.Recipient),
payout.Bank,
payout.Account,
TryLocalize(payout.Reference),
payout.AmountCents/100.0);
result.Append("},");
}
result.Remove(result.Length - 1, 1); // remove last comma
result.Append("]}");
return result.ToString();
}
private string TryLocalize(string input)
{
if (!input.StartsWith("[Loc]"))
{
return input;
}
string[] inputParts = input.Split('|');
string resourceKey = inputParts[0].Substring(5);
string translatedResource = GetGlobalResourceObject("Global", resourceKey).ToString();
if (inputParts.Length == 1)
{
return translatedResource;
}
else
{
object argument = null;
if (inputParts[1].StartsWith("[Date]"))
{
argument = DateTime.Parse(inputParts[1].Substring(6), CultureInfo.InvariantCulture);
}
else
{
argument = inputParts[1];
}
return String.Format(translatedResource, argument);
}
}
private Dictionary<int, bool> _attestationRights;
private Person _currentUser;
private Organization _currentOrganization;
}
}
+1 -1
View File
@@ -41,7 +41,7 @@
<th data-options="field:'account',width:160,sortable:true"><asp:Label ID="LabelGridHeaderAccount" runat="server" Text="XYZ Account" /></th>
<th data-options="field:'reference',width:80,sortable:true,order:'asc'"><asp:Label ID="LabelGridHeaderReference" runat="server" Text="XYZ Reference" /></th>
<th data-options="field:'amount',width:40,align:'center'"><asp:Label ID="LabelGridHeaderAmount" runat="server" Text="XYZ Amount" /></th>
<th data-options="field:'paid',width:53,align:'center'"><asp:Label ID="LabelGridHeaderPaid" runat="server" Text="XYZPaid" /></th>
<th data-options="field:'action',width:53,align:'center'"><asp:Label ID="LabelGridHeaderPaid" runat="server" Text="XYZPaid" /></th>
</tr>
</thead>
</table>
@@ -29,6 +29,14 @@ namespace Swarmops.Frontend.Pages.Financial
{
this.PageTitle = Resources.Pages.Financial.PayOutMoney_PageTitle;
this.InfoBoxLiteral = Resources.Pages.Financial.PayOutMoney_Info;
this.LabelGridHeaderAccount.Text = Resources.Pages.Financial.PayOutMoney_GridHeader_BankAccount;
this.LabelGridHeaderAmount.Text = Resources.Pages.Financial.PayOutMoney_GridHeader_Amount;
this.LabelGridHeaderBank.Text = Resources.Pages.Financial.PayOutMoney_GridHeader_BankName;
this.LabelGridHeaderDue.Text = Resources.Pages.Financial.PayOutMoney_GridHeader_DueDate;
this.LabelGridHeaderPaid.Text = Resources.Pages.Financial.PayOutMoney_GridHeader_PaidOut;
this.LabelGridHeaderRecipient.Text = Resources.Pages.Financial.PayOutMoney_GridHeader_Recipient;
this.LabelGridHeaderReference.Text = Resources.Pages.Financial.PayOutMoney_GridHeader_Reference;
}
}
}