diff --git a/Logic/Financial/CashAdvance.cs b/Logic/Financial/CashAdvance.cs index 4f38889e..e2e80bcb 100644 --- a/Logic/Financial/CashAdvance.cs +++ b/Logic/Financial/CashAdvance.cs @@ -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; + } + } } } diff --git a/Logic/Financial/CashAdvances.cs b/Logic/Financial/CashAdvances.cs index 6f28676e..c15e5679 100644 --- a/Logic/Financial/CashAdvances.cs +++ b/Logic/Financial/CashAdvances.cs @@ -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; } diff --git a/Logic/Financial/Payout.cs b/Logic/Financial/Payout.cs index 4e63b7e5..dded0269 100644 --- a/Logic/Financial/Payout.cs +++ b/Logic/Financial/Payout.cs @@ -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 identityList = new List(); + 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; diff --git a/Logic/Financial/Payouts.cs b/Logic/Financial/Payouts.cs index 06434686..1272d193 100644 --- a/Logic/Financial/Payouts.cs +++ b/Logic/Financial/Payouts.cs @@ -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 payoutLookup = new Dictionary(); + + 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 advanceIds = new List(); + + 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); diff --git a/Site5/App_GlobalResources/Global.resx b/Site5/App_GlobalResources/Global.resx index 09a1e472..074c55fd 100644 --- a/Site5/App_GlobalResources/Global.resx +++ b/Site5/App_GlobalResources/Global.resx @@ -162,9 +162,21 @@ Advance + + Cash Advance #{0} + + + Cash Advances {0} + Expense + + Expense #{0} + + + Expenses {0} + Invoice @@ -177,9 +189,27 @@ Salary + + Salary for {0:yyyy-MMM} + The payout date is inserted for date, like "Salary for 2012-Aug". + + + Tax for salaries {0} + + + Tax for salary #{0} + + + The Tax Man + Used as recipient for taxes. + Yearly result + + ASAP + Used as time indicator for "right away" (as soon as possible) in place of dates where in a hurry + Cancel Displayed as cancellation link on changing something diff --git a/Site5/App_GlobalResources/Pages.Financial.resx b/Site5/App_GlobalResources/Pages.Financial.resx index 04bfa3fa..ec5c11a1 100644 --- a/Site5/App_GlobalResources/Pages.Financial.resx +++ b/Site5/App_GlobalResources/Pages.Financial.resx @@ -190,6 +190,32 @@ Salary #{0} for {1} was DE-attested. It will not be paid out unless attested again. When somebody undoes a previous attestation. + + Amount + Amount to be paid out + + + Clearing / Account + Will display as "XXXX / YYYYYYYY" where XXXX is the clearing number + + + Bank + + + Due Date + + + Paid + Possibly in imperative; a click in this column indicates payment has been done. It is an action column, not an information column. + + + Recipient + Can be org (for invoice) or person (for salary, expense, advance) + + + Reference + Depends on cost type - can be an OCR number for an invoice, a cash advance number, a salary spec, etc. + Costs Ready For Payout diff --git a/Site5/Pages/v5/Financial/Json-PayableCosts.aspx b/Site5/Pages/v5/Financial/Json-PayableCosts.aspx new file mode 100644 index 00000000..8f78c042 --- /dev/null +++ b/Site5/Pages/v5/Financial/Json-PayableCosts.aspx @@ -0,0 +1 @@ +<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Json-PayableCosts.aspx.cs" Inherits="Swarmops.Frontend.Pages.Financial.JsonPayableCosts" %> diff --git a/Site5/Pages/v5/Financial/Json-PayableCosts.aspx.cs b/Site5/Pages/v5/Financial/Json-PayableCosts.aspx.cs new file mode 100644 index 00000000..031feecd --- /dev/null +++ b/Site5/Pages/v5/Financial/Json-PayableCosts.aspx.cs @@ -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 = + ""; + + 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\":\"" + + "" + + "\"", + 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 _attestationRights; + private Person _currentUser; + private Organization _currentOrganization; + + } + +} \ No newline at end of file diff --git a/Site5/Pages/v5/Financial/PayOutMoney.aspx b/Site5/Pages/v5/Financial/PayOutMoney.aspx index 72a4c38d..48a51eb6 100644 --- a/Site5/Pages/v5/Financial/PayOutMoney.aspx +++ b/Site5/Pages/v5/Financial/PayOutMoney.aspx @@ -41,7 +41,7 @@ - + diff --git a/Site5/Pages/v5/Financial/PayOutMoney.aspx.cs b/Site5/Pages/v5/Financial/PayOutMoney.aspx.cs index 91130a46..46f7d116 100644 --- a/Site5/Pages/v5/Financial/PayOutMoney.aspx.cs +++ b/Site5/Pages/v5/Financial/PayOutMoney.aspx.cs @@ -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; + } } } \ No newline at end of file