From e5e0d5d756fde1bb6b3304eb417472ccf42d73f3 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Tue, 18 Feb 2014 11:21:35 +0000 Subject: [PATCH] More work on account plan. Committing to test. --- Database/Database-Financials.cs | 6 +- Site5/Pages/v5/Ledgers/AccountPlan.aspx | 18 +++- Site5/Pages/v5/Ledgers/AccountPlan.aspx.cs | 2 + .../v5/Ledgers/Json-AccountPlanData.aspx.cs | 102 +++++++++++++++++- .../v5/Security/ChangeOrganizations.aspx.cs | 3 + 5 files changed, 124 insertions(+), 7 deletions(-) diff --git a/Database/Database-Financials.cs b/Database/Database-Financials.cs index 49063d42..94d1fb80 100644 --- a/Database/Database-Financials.cs +++ b/Database/Database-Financials.cs @@ -1078,12 +1078,12 @@ namespace Swarmops.Database }*/ - private BasicFinancialAccount[] GetFinancialAccountTree(Dictionary> FinancialAccounts, int startNodeId, + private BasicFinancialAccount[] GetFinancialAccountTree(Dictionary> financialAccounts, int startNodeId, int generation) { List result = new List(); - List thisList = FinancialAccounts[startNodeId]; + List thisList = financialAccounts[startNodeId]; foreach (BasicFinancialAccount node in thisList) { @@ -1093,7 +1093,7 @@ namespace Swarmops.Database // Add recursively - BasicFinancialAccount[] children = GetFinancialAccountTree(FinancialAccounts, node.FinancialAccountId, generation + 1); + BasicFinancialAccount[] children = GetFinancialAccountTree(financialAccounts, node.FinancialAccountId, generation + 1); if (children.Length > 0) { diff --git a/Site5/Pages/v5/Ledgers/AccountPlan.aspx b/Site5/Pages/v5/Ledgers/AccountPlan.aspx index 6e9f7b21..aa7c6839 100644 --- a/Site5/Pages/v5/Ledgers/AccountPlan.aspx +++ b/Site5/Pages/v5/Ledgers/AccountPlan.aspx @@ -10,6 +10,18 @@ $('#tableAccountPlan').treegrid( { + onBeforeExpand: function (foo) { + $('span.accountplandata-collapsed-' + foo.id).fadeOut('fast', function () { + $('span.accountplandata-expanded-' + foo.id).fadeIn('slow'); + }); + }, + + onBeforeCollapse: function (foo) { + $('span.accountplandata-expanded-' + foo.id).fadeOut('fast', function () { + $('span.accountplandata-collapsed-' + foo.id).fadeIn('slow'); + }); + }, + onLoadSuccess: function () { $('div.datagrid').css('opacity', 1); } @@ -29,15 +41,15 @@ url="Json-AccountPlanData.aspx" rownumbers="false" animate="true" - fitColumns="false" + fitColumns="true" idField="id" treeField="accountName"> Account Name - Owner + Owner Balance Budget - Flags + Flags Edit diff --git a/Site5/Pages/v5/Ledgers/AccountPlan.aspx.cs b/Site5/Pages/v5/Ledgers/AccountPlan.aspx.cs index ce6c1b0c..1dd4e5a2 100644 --- a/Site5/Pages/v5/Ledgers/AccountPlan.aspx.cs +++ b/Site5/Pages/v5/Ledgers/AccountPlan.aspx.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; +using Swarmops.Logic.Security; namespace Swarmops.Frontend.Pages.v5.Ledgers { @@ -20,6 +21,7 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers this.PageIcon = "iconshock-openbook"; this.PageTitle = Resources.Pages.Ledgers.AccountPlan_PageTitle; this.InfoBoxLiteral = Resources.Pages.Ledgers.AccountPlan_Info; + this.PageAccessRequired = new Access(this.CurrentOrganization, AccessAspect.Bookkeeping, AccessType.Read); } } diff --git a/Site5/Pages/v5/Ledgers/Json-AccountPlanData.aspx.cs b/Site5/Pages/v5/Ledgers/Json-AccountPlanData.aspx.cs index 93ee2327..a7b4c99e 100644 --- a/Site5/Pages/v5/Ledgers/Json-AccountPlanData.aspx.cs +++ b/Site5/Pages/v5/Ledgers/Json-AccountPlanData.aspx.cs @@ -29,6 +29,7 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers string response = string.Empty; + PopulateLookups(FinancialAccounts.ForOrganization(_authenticationData.CurrentOrganization)); _hashedAccounts = FinancialAccounts.GetHashedAccounts(_authenticationData.CurrentOrganization); response += GetAccountGroup(FinancialAccountType.Asset, Resources.Pages.Ledgers.BalanceSheet_Assets) + ","; @@ -84,7 +85,16 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers } - childStrings.Add('{' + String.Format("\"id\":\"{0}\",\"accountName\":\"{1}\"", account.Identity, JsonSanitize(account.Name)) + grandChildren + '}'); + childStrings.Add('{' + String.Format("\"id\":\"{0}\",\"accountName\":\"{1}\",\"owner\":\"{2}\",\"balance\":\"{3}\",\"budget\":\"{4}\"", + account.Identity, + Server.HtmlEncode(JsonSanitize(account.Name)), + account.OwnerPersonId != 0? "" + JsonSanitize(Server.HtmlEncode(account.Owner.Canonical)) + "" : JsonSanitize(Resources.Global.Global_NoOwner), + _hashedAccounts[account.Identity].Count > 1 ? JsonDualString(account.Identity, _treeBalanceLookup[account.Identity], _singleBalanceLookup[account.Identity]) : (_singleBalanceLookup [account.Identity] / 100.0).ToString("N0"), + account.AccountType == FinancialAccountType.Income || account.AccountType == FinancialAccountType.Cost? + _hashedAccounts[account.Identity].Count > 1 ? (JsonDualString(account.Identity, _treeBudgetLookup[account.Identity], _singleBudgetLookup[account.Identity])) : + (_singleBudgetLookup[account.Identity] / 100.0).ToString("N0") : + string.Empty + ) + grandChildren + '}'); } return String.Join(",", childStrings.ToArray()); @@ -100,5 +110,95 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers { // typedef }; + + private Dictionary> _treeMap; + private Dictionary _singleBalanceLookup; + private Dictionary _treeBalanceLookup; + private Dictionary _singleBudgetLookup; + private Dictionary _treeBudgetLookup; + + + private void PopulateLookups(FinancialAccounts accounts) + { + _singleBalanceLookup = new Dictionary(); + _treeBalanceLookup = new Dictionary(); + _singleBudgetLookup = new Dictionary(); + _treeBudgetLookup = new Dictionary(); + + // 1) Actually, the accounts are already sorted. Or are supposed to be, anyway, + // since FinancialAccounts.ForOrganization gets the _tree_ rather than the flat list. + + // 2) Add all values to the accounts. + + foreach (FinancialAccount account in accounts) + { + // Get current balances + + // TODO: There must be a more optimized way to do this, like with a database optimization. This + // is a HORRIBLY expensive operation, as it performs one complex database query PER ACCOUNT. + + if (account.AccountType == FinancialAccountType.Cost || account.AccountType == FinancialAccountType.Income) + { + _singleBalanceLookup[account.Identity] = account.GetDeltaCents(new DateTime(_year, 1, 1), + new DateTime(_year + 1, 1, 1)); + _singleBudgetLookup[account.Identity] = account.GetBudgetCents(_year); + } + else if (account.AccountType == FinancialAccountType.Asset || account.AccountType == FinancialAccountType.Debt) + { + _singleBalanceLookup[account.Identity] = account.GetDeltaCents(new DateTime(1900, 1, 1), + new DateTime(_year + 1, 1, 1)); + _singleBudgetLookup[account.Identity] = 0; // balance accounts don't have budgets + } + else + { + throw new InvalidOperationException("Account with invalid type encountered - " + account.AccountType.ToString()); + } + + // copy to treeLookups + + _treeBalanceLookup[account.Identity] = _singleBalanceLookup[account.Identity]; + _treeBudgetLookup[account.Identity] = _singleBudgetLookup[account.Identity]; + } + + // 3) Add all children's values to parents + + AddChildrenValuesToParents(_treeBalanceLookup, accounts); + AddChildrenValuesToParents(_treeBudgetLookup, accounts); + + // Done. + } + + + private void AddChildrenValuesToParents(Dictionary lookup, FinancialAccounts accounts) + { + // Iterate backwards and add any value to its parent's value, as they are sorted in tree order. + + for (int index = accounts.Count - 1; index >= 0; index--) + { + int parentFinancialAccountId = accounts[index].ParentFinancialAccountId; + int accountId = accounts[index].Identity; + + if (parentFinancialAccountId != 0) + { + lookup[parentFinancialAccountId] += lookup[accountId]; + } + } + } + + + + private string JsonDualString(int accountId, Int64 treeValue, Int64 singleValue) + { + if (treeValue != 0 && singleValue == 0) + { + return string.Format(_renderCulture, + "Σ {1:N0} ", + accountId, treeValue / 100.00); + } + return string.Format(_renderCulture, + "Σ {1:N0}{2:N0}", + accountId, treeValue / 100.0, singleValue / 100.0); + } + } } \ No newline at end of file diff --git a/Site5/Pages/v5/Security/ChangeOrganizations.aspx.cs b/Site5/Pages/v5/Security/ChangeOrganizations.aspx.cs index bcf920c5..18d893e8 100644 --- a/Site5/Pages/v5/Security/ChangeOrganizations.aspx.cs +++ b/Site5/Pages/v5/Security/ChangeOrganizations.aspx.cs @@ -6,6 +6,7 @@ using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; +using Swarmops.Logic.Security; namespace Swarmops.Frontend.Pages.Security { @@ -18,6 +19,8 @@ namespace Swarmops.Frontend.Pages.Security Localize(); } + this.PageAccessRequired = new Access(AccessAspect.Bookkeeping, AccessType.Write); // bogus, but will prevent bad ppl from entering until real security done + this.PageTitle = Resources.Pages.Security.ChangeOrganizations_PageTitle; this.PageIcon = "iconshock-organizations"; this.InfoBoxLiteral = Resources.Pages.Security.ChangeOrganizations_Info;