diff --git a/Basic/Enums/FinancialDependencyType.cs b/Basic/Enums/FinancialDependencyType.cs index 7950e282..02be686b 100644 --- a/Basic/Enums/FinancialDependencyType.cs +++ b/Basic/Enums/FinancialDependencyType.cs @@ -41,6 +41,10 @@ namespace Swarmops.Basic.Enums /// /// A cash advance (prior payment, rather than doing an expense claim post purchase) /// - CashAdvance + CashAdvance, + /// + /// A payback for a previous cash advance + /// + CashAdvancePayback } } \ No newline at end of file diff --git a/Logic/Financial/CashAdvances.cs b/Logic/Financial/CashAdvances.cs index c15e5679..fc79a60a 100644 --- a/Logic/Financial/CashAdvances.cs +++ b/Logic/Financial/CashAdvances.cs @@ -44,5 +44,31 @@ namespace Swarmops.Logic.Financial } } + public CashAdvances WherePaid + { + get + { + CashAdvances result = new CashAdvances(); + result.AddRange(this.Where(cashAdvance => cashAdvance.PaidOut)); + + return result; + } + } + + public CashAdvances WhereUnpaid + { + get + { + CashAdvances result = new CashAdvances(); + result.AddRange(this.Where(cashAdvance => !cashAdvance.PaidOut)); + + return result; + } + } + + public Int64 TotalAmountCents + { + get { return this.Sum(cashAdvance => cashAdvance.AmountCents); } + } } } diff --git a/Logic/Financial/ExpenseClaim.cs b/Logic/Financial/ExpenseClaim.cs index 1be05e52..affb9f8d 100644 --- a/Logic/Financial/ExpenseClaim.cs +++ b/Logic/Financial/ExpenseClaim.cs @@ -368,8 +368,6 @@ namespace Swarmops.Logic.Financial nominalTransaction[debtAccountId] = -AmountCents; nominalTransaction[BudgetId] = AmountCents; - - // TODO: Balance cost account, too } this.FinancialTransaction.RecalculateTransaction(nominalTransaction, updatingPerson); diff --git a/Logic/Financial/Payout.cs b/Logic/Financial/Payout.cs index 8c32e564..060fc79c 100644 --- a/Logic/Financial/Payout.cs +++ b/Logic/Financial/Payout.cs @@ -47,7 +47,8 @@ namespace Swarmops.Logic.Financial DependentInvoices = new InboundInvoices(); DependentSalariesNet = new Salaries(); DependentSalariesTax = new Salaries(); - DependentCashAdvances = new CashAdvances(); + DependentCashAdvancesPayout = new CashAdvances(); + DependentCashAdvancesPayback = new CashAdvances(); BasicFinancialDependency[] dependencies = SwarmDb.GetDatabaseForReading().GetPayoutDependencies(this.Identity); @@ -74,7 +75,11 @@ namespace Swarmops.Logic.Financial break; case FinancialDependencyType.CashAdvance: - DependentCashAdvances.Add(CashAdvance.FromIdentity(dependency.ForeignId)); + DependentCashAdvancesPayout.Add(CashAdvance.FromIdentity(dependency.ForeignId)); + break; + + case FinancialDependencyType.CashAdvancePayback: + DependentCashAdvancesPayback.Add(CashAdvance.FromIdentity(dependency.ForeignId)); break; default: @@ -100,7 +105,8 @@ namespace Swarmops.Logic.Financial public ExpenseClaims DependentExpenseClaims; public Salaries DependentSalariesNet; public Salaries DependentSalariesTax; - public CashAdvances DependentCashAdvances; + public CashAdvances DependentCashAdvancesPayout; + public CashAdvances DependentCashAdvancesPayback; public decimal Amount { @@ -206,7 +212,7 @@ namespace Swarmops.Logic.Financial amountCents += advance.AmountCents; advance.PaidOut = true; - advance.Open = false; + //advance.Open = false; // isn't this closed only when settling the debt? Serious bug here? } string referenceString = string.Empty; @@ -233,51 +239,64 @@ namespace Swarmops.Logic.Financial } else if (components [0][0] == 'C') { - // Expense claims. + // Expense claims, possibly followed up by cash advance paybacks string bank = string.Empty; string account = string.Empty; - List identityList = new List(); + List claimIdentityList = new List(); + List advancePaybackIdentityList = new List(); Int64 amountCents = 0; int organizationId = 0; foreach (string component in components) { - int claimId = Int32.Parse(component.Substring(1)); - ExpenseClaim claim = ExpenseClaim.FromIdentity(claimId); - identityList.Add(claimId); + int foreignId = Int32.Parse(component.Substring(1)); - if (bank.Length < 1) + if (component[0] == 'C') { - Person claimer = claim.Claimer; - bank = claimer.BankName; - account = claimer.BankAccount; - organizationId = claim.OrganizationId; + ExpenseClaim claim = ExpenseClaim.FromIdentity(foreignId); + claimIdentityList.Add(foreignId); + + if (bank.Length < 1) + { + Person claimer = claim.Claimer; + bank = claimer.BankName; + account = claimer.BankAccount; + organizationId = claim.OrganizationId; + } + + amountCents += claim.AmountCents; + + claim.Repaid = true; + claim.Close(); } + else if (component[0] == 'a') + { + CashAdvance advancePayback = CashAdvance.FromIdentity(foreignId); + advancePaybackIdentityList.Add(foreignId); - amountCents += claim.AmountCents; - - claim.Repaid = true; - claim.Close(); + amountCents -= advancePayback.AmountCents; + advancePayback.Open = false; + } } string referenceString = string.Empty; - if (identityList.Count == 1) + if (claimIdentityList.Count == 1) { - referenceString = "Expense Claim #" + identityList[0].ToString(CultureInfo.InvariantCulture); + referenceString = "Expense Claim #" + claimIdentityList[0].ToString(CultureInfo.InvariantCulture); } else { - identityList.Sort(); - referenceString = "Expense Claims " + Formatting.GenerateRangeString(identityList); + claimIdentityList.Sort(); + referenceString = "Expense Claims " + Formatting.GenerateRangeString(claimIdentityList); } payoutId = SwarmDb.GetDatabaseForWriting().CreatePayout(organizationId, bank, account, referenceString, amountCents, DateTime.Today.AddDays(1), creator.Identity); - foreach (int claimId in identityList) + foreach (int claimId in claimIdentityList) { SwarmDb.GetDatabaseForWriting().CreatePayoutDependency(payoutId, FinancialDependencyType.ExpenseClaim, claimId); @@ -403,7 +422,12 @@ namespace Swarmops.Logic.Financial result += "|C" + claim.Identity.ToString(CultureInfo.InvariantCulture); } - foreach (CashAdvance advance in DependentCashAdvances) + foreach (CashAdvance advancePayback in DependentCashAdvancesPayback) + { + result += "|a" + advancePayback.Identity.ToString(CultureInfo.InvariantCulture); + } + + foreach (CashAdvance advance in DependentCashAdvancesPayout) { result += "|A" + advance.Identity.ToString(CultureInfo.InvariantCulture); } @@ -448,9 +472,9 @@ namespace Swarmops.Logic.Financial { return this.DependentExpenseClaims[0].ClaimerCanonical; } - else if (this.DependentCashAdvances.Count > 0) + else if (this.DependentCashAdvancesPayout.Count > 0) { - return this.DependentCashAdvances[0].Person.Canonical; + return this.DependentCashAdvancesPayout[0].Person.Canonical; } else if (this.DependentSalariesNet.Count > 0) { @@ -483,6 +507,7 @@ namespace Swarmops.Logic.Financial accountDebitLookup[organization.FinancialAccounts.DebtsInboundInvoices.Identity] = 0; accountDebitLookup[organization.FinancialAccounts.DebtsSalary.Identity] = 0; accountDebitLookup[organization.FinancialAccounts.DebtsTax.Identity] = 0; + accountDebitLookup[organization.FinancialAccounts.AssetsOutstandingCashAdvances.Identity] = 0; if (this.DependentExpenseClaims.Count > 0) { @@ -504,6 +529,16 @@ namespace Swarmops.Logic.Financial accountDebitLookup[organization.FinancialAccounts.DebtsTax.Identity] += this.DependentSalariesTax.TotalAmountCentsTax; } + if (this.DependentCashAdvancesPayout.Count > 0) + { + accountDebitLookup[organization.FinancialAccounts.AssetsOutstandingCashAdvances.Identity] += + this.DependentCashAdvancesPayout.TotalAmountCents; + } + if (this.DependentCashAdvancesPayback.Count > 0) + { + accountDebitLookup[organization.FinancialAccounts.AssetsOutstandingCashAdvances.Identity] -= // observe the minus + this.DependentCashAdvancesPayback.TotalAmountCents; + } foreach (int financialAccountId in accountDebitLookup.Keys) { @@ -570,7 +605,7 @@ namespace Swarmops.Logic.Financial claim.Open = true; } - foreach (CashAdvance advance in this.DependentCashAdvances) + foreach (CashAdvance advance in this.DependentCashAdvancesPayout) { advance.PaidOut = false; advance.Open = true; diff --git a/Logic/Financial/Payouts.cs b/Logic/Financial/Payouts.cs index 1272d193..4fe80639 100644 --- a/Logic/Financial/Payouts.cs +++ b/Logic/Financial/Payouts.cs @@ -89,6 +89,24 @@ namespace Swarmops.Logic.Financial } } + // At this point, all the expense claims have been added - but we need to add the open + // cash advances and deduct them. + + CashAdvances cashAdvances = CashAdvances.ForOrganization(Organization.FromIdentity(organizationId)); + cashAdvances = cashAdvances.WherePaid; + + // At this point, only open and paid cash advances are in the list: they're debts to the org + + foreach (CashAdvance cashAdvance in cashAdvances) + { + if (payoutLookup.ContainsKey(cashAdvance.PersonId)) + { + // there's a payout prepared to this person - we need to deduct the cash advance from it. + + payoutLookup[cashAdvance.PersonId].DependentCashAdvancesPayback.Add(cashAdvance); + } + } + // 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. @@ -103,16 +121,25 @@ namespace Swarmops.Logic.Financial claimIds.Add(claim.Identity); } + foreach (CashAdvance previousAdvance in payout.DependentCashAdvancesPayback) + { + newAmountCents -= previousAdvance.AmountCents; + } + + string lessAdvancesIndicator = payout.DependentCashAdvancesPayback.Count > 0 + ? "LessAdvances" + : string.Empty; + payout.AmountCents = newAmountCents; if (claimIds.Count == 1) { - payout.Reference = "[Loc]Financial_ExpenseClaimSpecification|" + claimIds[0].ToString(CultureInfo.InvariantCulture); + payout.Reference = "[Loc]Financial_ExpenseClaimSpecification" + lessAdvancesIndicator + "|" + claimIds[0].ToString(CultureInfo.InvariantCulture); } else { claimIds.Sort(); - payout.Reference = "[Loc]Financial_ExpenseClaimsSpecification|" + Formatting.GenerateRangeString(claimIds); + payout.Reference = "[Loc]Financial_ExpenseClaimsSpecification" + lessAdvancesIndicator + "|" + Formatting.GenerateRangeString(claimIds); } if (newAmountCents > 0) @@ -129,6 +156,7 @@ namespace Swarmops.Logic.Financial { CashAdvances advances = CashAdvances.ForOrganization(Organization.FromIdentity(organizationId)); advances = advances.WhereAttested; + advances = advances.WhereUnpaid; Dictionary payoutLookup = new Dictionary(); @@ -145,7 +173,7 @@ namespace Swarmops.Logic.Financial { // Yes. Add claim to list. - payoutLookup[advance.PersonId].DependentCashAdvances.Add(advance); + payoutLookup[advance.PersonId].DependentCashAdvancesPayout.Add(advance); } else { @@ -156,7 +184,7 @@ namespace Swarmops.Logic.Financial DateTime.MinValue, false, DateTime.Now, 0); Payout payout = Payout.FromBasic(basicPayout); - payout.DependentCashAdvances.Add(advance); + payout.DependentCashAdvancesPayout.Add(advance); payoutLookup[advance.PersonId] = payout; } @@ -170,7 +198,7 @@ namespace Swarmops.Logic.Financial Int64 newAmountCents = 0; List advanceIds = new List(); - foreach (CashAdvance advance in payout.DependentCashAdvances) + foreach (CashAdvance advance in payout.DependentCashAdvancesPayout) { newAmountCents += advance.AmountCents; advanceIds.Add(advance.Identity); diff --git a/Site5/App_GlobalResources/Global.resx b/Site5/App_GlobalResources/Global.resx index a5a5636b..ac62926b 100644 --- a/Site5/App_GlobalResources/Global.resx +++ b/Site5/App_GlobalResources/Global.resx @@ -336,4 +336,16 @@ Related List of shortcuts to related actions to current one + + Expense #{0} (minus advance #{1}) + + + Expense #{0} (minus advances) + + + Expenses {0} (minus advance #{1}) + + + Expenses {0} (minus advances) + \ No newline at end of file diff --git a/Site5/Pages/v5/Financial/AttestCosts.aspx b/Site5/Pages/v5/Financial/AttestCosts.aspx index 192f0d68..25c6ca94 100644 --- a/Site5/Pages/v5/Financial/AttestCosts.aspx +++ b/Site5/Pages/v5/Financial/AttestCosts.aspx @@ -5,7 +5,8 @@ - + +