From 574525219da507ae8902dc18013f1785476f60b1 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Fri, 24 Oct 2014 22:13:55 +0200 Subject: [PATCH] Checkpoint commit for Inspect Ledgers --- Basic/Enums/FinancialAccountType.cs | 6 +- Logic/Financial/FinancialAccounts.cs | 6 + .../OrganizationFinancialAccounts.cs | 30 ++- Site5/Automation/Json-BudgetsTree.aspx | 1 + ...sTree.aspx.cs => Json-BudgetsTree.aspx.cs} | 215 +++++++++++------- .../Json-ExpensableBudgetsTree.aspx | 1 - .../Json-FinancialAccountsTree.aspx | 2 +- .../Json-FinancialAccountsTree.aspx.cs | 92 +++++++- Site5/Controls/v5/Financial/ComboBudgets.ascx | 2 +- .../v5/Financial/ComboBudgets.ascx.cs | 35 ++- Site5/Pages/v5/Ledgers/InspectLedgers.aspx | 2 +- 11 files changed, 280 insertions(+), 112 deletions(-) create mode 100644 Site5/Automation/Json-BudgetsTree.aspx rename Site5/Automation/{Json-ExpensableBudgetsTree.aspx.cs => Json-BudgetsTree.aspx.cs} (60%) delete mode 100644 Site5/Automation/Json-ExpensableBudgetsTree.aspx diff --git a/Basic/Enums/FinancialAccountType.cs b/Basic/Enums/FinancialAccountType.cs index e143c5c0..ed35c211 100644 --- a/Basic/Enums/FinancialAccountType.cs +++ b/Basic/Enums/FinancialAccountType.cs @@ -29,6 +29,10 @@ namespace Swarmops.Basic.Enums /// /// Incomes and costs - accounts that are zeroed at year end. Sign reversed. /// - Result = 6 + Result = 6, + /// + /// Meta-type which includes all accounts and group labels where applicable + /// + All = 7 } } \ No newline at end of file diff --git a/Logic/Financial/FinancialAccounts.cs b/Logic/Financial/FinancialAccounts.cs index a4c9690a..964d837d 100644 --- a/Logic/Financial/FinancialAccounts.cs +++ b/Logic/Financial/FinancialAccounts.cs @@ -26,6 +26,12 @@ namespace Swarmops.Logic.Financial public static FinancialAccounts ForOrganization (Organization organization, FinancialAccountType accountType) { FinancialAccounts allAccounts = ForOrganization(organization); + + if (accountType == FinancialAccountType.All) + { + return allAccounts; + } + FinancialAccounts result = new FinancialAccounts(); Dictionary lookup = new Dictionary(); diff --git a/Logic/Financial/OrganizationFinancialAccounts.cs b/Logic/Financial/OrganizationFinancialAccounts.cs index b2a2e855..121d4974 100644 --- a/Logic/Financial/OrganizationFinancialAccounts.cs +++ b/Logic/Financial/OrganizationFinancialAccounts.cs @@ -230,8 +230,8 @@ namespace Swarmops.Logic.Financial { // TODO: This needs to return a tree, not a flat list. - get - { + get + { FinancialAccounts result = new FinancialAccounts(); int yearlyResultId = this.CostsYearlyResult.Identity; @@ -252,5 +252,31 @@ namespace Swarmops.Logic.Financial } } + public FinancialAccounts InvoiceableBudgetsOutbound + { + // TODO: This needs to return a tree, not a flat list. + + get + { + FinancialAccounts result = new FinancialAccounts(); + + int yearlyResultId = this.CostsYearlyResult.Identity; + + FinancialAccounts costAccounts = + FinancialAccounts.ForOrganization(Organization.FromIdentity(_organizationId), + FinancialAccountType.Income); + + foreach (FinancialAccount account in costAccounts) + { + if (account.Identity != yearlyResultId && account.Expensable) // really should be redundant, but still... + { + result.Add(account); + } + } + + return result; + } + } + } } diff --git a/Site5/Automation/Json-BudgetsTree.aspx b/Site5/Automation/Json-BudgetsTree.aspx new file mode 100644 index 00000000..18062fa8 --- /dev/null +++ b/Site5/Automation/Json-BudgetsTree.aspx @@ -0,0 +1 @@ +<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Json-BudgetsTree.aspx.cs" Inherits="Swarmops.Frontend.Automation.Json_BudgetsTree" %> \ No newline at end of file diff --git a/Site5/Automation/Json-ExpensableBudgetsTree.aspx.cs b/Site5/Automation/Json-BudgetsTree.aspx.cs similarity index 60% rename from Site5/Automation/Json-ExpensableBudgetsTree.aspx.cs rename to Site5/Automation/Json-BudgetsTree.aspx.cs index 429efea2..76c00a04 100644 --- a/Site5/Automation/Json-ExpensableBudgetsTree.aspx.cs +++ b/Site5/Automation/Json-BudgetsTree.aspx.cs @@ -1,88 +1,129 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using Swarmops.Logic.Financial; - -namespace Swarmops.Frontend.Automation -{ - public partial class Json_ExpensableBudgetsTree : DataV5Base - { - protected void Page_Load(object sender, EventArgs e) - { - Response.ContentType = "application/json"; - - // Is this stuff in cache already? - - string cacheKey = "ExpensableBudgets-Json-" + - this.CurrentOrganization.Identity.ToString((CultureInfo.InvariantCulture)); - - string accountsJson = - (string) Cache[cacheKey]; - - if (accountsJson != null) - { - Response.Output.WriteLine(accountsJson); - Response.End(); - return; - } - - // Not in cache. Construct. - - // Get accounts - - FinancialAccounts accounts = this.CurrentOrganization.FinancialAccounts.ExpensableBudgets; - - // Build tree (there should be a template for this) - - Dictionary> treeMap = new Dictionary>(); - - foreach (FinancialAccount account in accounts) - { - if (!treeMap.ContainsKey(account.ParentIdentity)) - { - treeMap[account.ParentIdentity] = new List(); - } - - treeMap[account.ParentIdentity].Add(account); - } - - int renderRootNodeId = 0; - - if (treeMap[0].Count == 1) - { - // assume there's a master root like "Costs"; bypass it - - renderRootNodeId = treeMap[0][0].Identity; - } - - accountsJson = RecurseTreeMap(treeMap, renderRootNodeId); - - Cache.Insert(cacheKey, accountsJson, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero); - // cache lasts for five minutes, no sliding expiration - Response.Output.WriteLine(accountsJson); - - Response.End(); - } - - private string RecurseTreeMap(Dictionary> treeMap, int node) - { - List elements = new List(); - - foreach (FinancialAccount account in treeMap[node]) - { - string element = string.Format("\"id\":{0},\"text\":\"{1}\"", account.Identity, - JsonSanitize(account.Name)); - - if (treeMap.ContainsKey(account.Identity)) - { - element += ",\"state\":\"closed\",\"children\":" + RecurseTreeMap(treeMap, account.Identity); - } - - elements.Add("{" + element + "}"); - } - - return "[" + String.Join(",", elements.ToArray()) + "]"; - - } - } +using System; +using System.Collections.Generic; +using System.Globalization; +using Swarmops.Logic.Financial; + +namespace Swarmops.Frontend.Automation +{ + public partial class Json_BudgetsTree : DataV5Base + { + protected void Page_Load(object sender, EventArgs e) + { + Response.ContentType = "application/json"; + + // What type of budgets do we want? + // Allowed values are "Expensable", "InvoicableOut", or "InvoiceableIn". Default is "Expensable". + + string accountTypeString = Request.QueryString["AccountType"]; + AccountTypes accountType = AccountTypes.Unknown; + + switch (accountTypeString.ToLowerInvariant()) + { + case "invoicableout": + accountType = AccountTypes.InvoicablesOut; + break; + case "invoicablein": + accountType = AccountTypes.InvoicablesIn; + break; + case "expensable": + default: + accountType = AccountTypes.Expensables; + break; + } + + // Is this stuff in cache already? + + string cacheKey = "ExpensableBudgets-Json-" + accountType.ToString() + // use the sanitized input to prevent cache overload + this.CurrentOrganization.Identity.ToString((CultureInfo.InvariantCulture)); + + string accountsJson = + (string) Cache[cacheKey]; + + if (accountsJson != null) + { + Response.Output.WriteLine(accountsJson); + Response.End(); + return; + } + + // Not in cache. Construct. + + // Get accounts + + FinancialAccounts accounts = null; + + switch (accountType) + { + case AccountTypes.InvoicablesOut: + accounts = CurrentOrganization.FinancialAccounts.InvoiceableBudgetsOutbound; + break; + case AccountTypes.Expensables: + case AccountTypes.InvoicablesIn: + accounts = CurrentOrganization.FinancialAccounts.ExpensableBudgets; + break; + default: + throw new NotImplementedException(); + } + + // Build tree (there should be a template for this) + + Dictionary> treeMap = new Dictionary>(); + + foreach (FinancialAccount account in accounts) + { + if (!treeMap.ContainsKey(account.ParentIdentity)) + { + treeMap[account.ParentIdentity] = new List(); + } + + treeMap[account.ParentIdentity].Add(account); + } + + int renderRootNodeId = 0; + + if (treeMap[0].Count == 1) + { + // assume there's a master root like "Costs"; bypass it + + renderRootNodeId = treeMap[0][0].Identity; + } + + accountsJson = RecurseTreeMap(treeMap, renderRootNodeId); + + Cache.Insert(cacheKey, accountsJson, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero); + // cache lasts for five minutes, no sliding expiration + Response.Output.WriteLine(accountsJson); + + Response.End(); + } + + private string RecurseTreeMap(Dictionary> treeMap, int node) + { + List elements = new List(); + + foreach (FinancialAccount account in treeMap[node]) + { + string element = string.Format("\"id\":{0},\"text\":\"{1}\"", account.Identity, + JsonSanitize(account.Name)); + + if (treeMap.ContainsKey(account.Identity)) + { + element += ",\"state\":\"closed\",\"children\":" + RecurseTreeMap(treeMap, account.Identity); + } + + elements.Add("{" + element + "}"); + } + + return "[" + String.Join(",", elements.ToArray()) + "]"; + + } + + private enum AccountTypes + { + Unknown = 0, + InvoicablesOut, + InvoicablesIn, + Expensables + } + } } \ No newline at end of file diff --git a/Site5/Automation/Json-ExpensableBudgetsTree.aspx b/Site5/Automation/Json-ExpensableBudgetsTree.aspx deleted file mode 100644 index 8e1b0d5b..00000000 --- a/Site5/Automation/Json-ExpensableBudgetsTree.aspx +++ /dev/null @@ -1 +0,0 @@ -<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Json-ExpensableBudgetsTree.aspx.cs" Inherits="Swarmops.Frontend.Automation.Json_ExpensableBudgetsTree" %> \ No newline at end of file diff --git a/Site5/Automation/Json-FinancialAccountsTree.aspx b/Site5/Automation/Json-FinancialAccountsTree.aspx index 284bfe9e..edfd2a6d 100644 --- a/Site5/Automation/Json-FinancialAccountsTree.aspx +++ b/Site5/Automation/Json-FinancialAccountsTree.aspx @@ -1 +1 @@ -<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Json-FinancialAccountsTree.aspx.cs" Inherits="Swarmops.Frontend.Pages.v5.Financial.Json_FinancialAccountsTree" %> \ No newline at end of file +<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Json-FinancialAccountsTree.aspx.cs" Inherits="Swarmops.Frontend.Automation.Json_FinancialAccountsTree" %> \ No newline at end of file diff --git a/Site5/Automation/Json-FinancialAccountsTree.aspx.cs b/Site5/Automation/Json-FinancialAccountsTree.aspx.cs index a674e9cb..05a379aa 100644 --- a/Site5/Automation/Json-FinancialAccountsTree.aspx.cs +++ b/Site5/Automation/Json-FinancialAccountsTree.aspx.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using Swarmops.Basic.Enums; using Swarmops.Logic.Financial; -namespace Swarmops.Frontend.Pages.v5.Financial +namespace Swarmops.Frontend.Automation { public partial class Json_FinancialAccountsTree : DataV5Base { @@ -13,7 +13,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial FinancialAccountType accountType = (FinancialAccountType) (Enum.Parse(typeof (FinancialAccountType), Request.QueryString["AccountType"])); - string accountsJson = string.Empty; + string response = string.Empty; int excludeId = -1; @@ -47,22 +47,92 @@ namespace Swarmops.Frontend.Pages.v5.Financial treeMap[account.ParentIdentity].Add(account); } - if (treeMap.ContainsKey(0)) + _hashedAccounts = treeMap; + + if (accountType == FinancialAccountType.Asset || accountType == FinancialAccountType.Balance || + accountType == FinancialAccountType.All) { - accountsJson = RecurseTreeMap(treeMap, 0); + response += GetAccountGroup(FinancialAccountType.Asset, Resources.Pages.Ledgers.BalanceSheet_Assets) + ","; } - accountsJson = "[{\"id\":\"0\",\"text\":\"" + - JsonSanitize(Resources.Global.ResourceManager.GetString("Financial_" + accountType.ToString())) + - "\",\"state\":\"open\",\"children\":" + - accountsJson + - "}]"; - - Response.Output.WriteLine(accountsJson); + if (accountType == FinancialAccountType.Debt || accountType == FinancialAccountType.Balance || + accountType == FinancialAccountType.All) + { + response += GetAccountGroup(FinancialAccountType.Debt, Resources.Pages.Ledgers.BalanceSheet_Debt) + ","; + } + if (accountType == FinancialAccountType.Income || accountType == FinancialAccountType.Result || + accountType == FinancialAccountType.All) + { + response += + GetAccountGroup(FinancialAccountType.Income, Resources.Pages.Ledgers.ProfitLossStatement_Income) + ","; + } + + if (accountType == FinancialAccountType.Cost || accountType == FinancialAccountType.Result || + accountType == FinancialAccountType.All) + { + response += GetAccountGroup(FinancialAccountType.Cost, Resources.Pages.Ledgers.ProfitLossStatement_Costs); + } + + response = response.TrimEnd(','); // removes last comma regardless of where it came from or if it's even there + + Response.Output.WriteLine("[" + response + "]"); Response.End(); } + private string GetAccountGroup(FinancialAccountType type, string localizedGroupName) + { + string childrenString = GetAccountsRecurse(type, 0); + + return "{\"id\":\"" + -((int)type) + "\",\"text\":\"" + JsonSanitize(localizedGroupName) + + "\",\"state\":\"open\",\"children\":[" + childrenString + "]}"; + } + + private Dictionary> _hashedAccounts = null; + + + private string GetAccountsRecurse(FinancialAccountType accountType, int rootNodeId) + { + List childStrings = new List(); + + if (!_hashedAccounts.ContainsKey(rootNodeId)) + { + return string.Empty; + } + + if (_hashedAccounts[rootNodeId].Count == 1) + { + // No children + + return string.Empty; + } + + foreach (FinancialAccount account in _hashedAccounts[rootNodeId]) + { + if (account.Identity == rootNodeId || account.AccountType != accountType) + { + continue; + } + + string grandChildren = GetAccountsRecurse(accountType, account.Identity); + if (!string.IsNullOrEmpty(grandChildren)) + { + grandChildren = ",\"state\":\"closed\",\"children\":[" + grandChildren + "]"; + } + + childStrings.Add('{' + String.Format( + "\"id\":\"{0}\",\"text\":\"{1}\"", + account.Identity, + Server.HtmlEncode(JsonSanitize(account.Name)) + ) + grandChildren + '}'); + } + + return String.Join(",", childStrings.ToArray()); + } + + + + private string RecurseTreeMap(Dictionary> treeMap, int node) { List elements = new List(); diff --git a/Site5/Controls/v5/Financial/ComboBudgets.ascx b/Site5/Controls/v5/Financial/ComboBudgets.ascx index 3ce76d3d..0d1f3aae 100644 --- a/Site5/Controls/v5/Financial/ComboBudgets.ascx +++ b/Site5/Controls/v5/Financial/ComboBudgets.ascx @@ -34,4 +34,4 @@ }); - \ No newline at end of file + \ No newline at end of file diff --git a/Site5/Controls/v5/Financial/ComboBudgets.ascx.cs b/Site5/Controls/v5/Financial/ComboBudgets.ascx.cs index 2bd77d76..9bd49bd9 100644 --- a/Site5/Controls/v5/Financial/ComboBudgets.ascx.cs +++ b/Site5/Controls/v5/Financial/ComboBudgets.ascx.cs @@ -13,13 +13,34 @@ namespace Swarmops.Controls.Financial public string OnClientSelect { get; set; } public AccountListType ListType { get; set; } - } - public enum AccountListType - { - Unknown = 0, - Expensables, - Invoiceables, - All + protected string DataUrl + { + get + { + switch (this.ListType) + { + case AccountListType.Unknown: + case AccountListType.Expensable: + default: + return "/Automation/Json-BudgetsTree.aspx?AccountType=Expensable"; + break; + case AccountListType.InvoiceableOut: + return "/Automation/Json-BudgetsTree.aspx?AccountType=InvoiceableOut"; + break; + case AccountListType.All: + return "/Automation/Json-FinancialAccountsTree.aspx?AccountType=All"; + } + } + } + + public enum AccountListType + { + Unknown = 0, + Expensable, + InvoiceableOut, + InvoiceableIn, + All + } } } \ No newline at end of file diff --git a/Site5/Pages/v5/Ledgers/InspectLedgers.aspx b/Site5/Pages/v5/Ledgers/InspectLedgers.aspx index cab4e8b8..8319d72a 100644 --- a/Site5/Pages/v5/Ledgers/InspectLedgers.aspx +++ b/Site5/Pages/v5/Ledgers/InspectLedgers.aspx @@ -10,7 +10,7 @@ -

Inspect for

+

Inspect for