More work on account plan. Committing to test.

This commit is contained in:
Rick Falkvinge
2014-02-18 11:21:35 +00:00
parent c10a0517e9
commit e5e0d5d756
5 changed files with 124 additions and 7 deletions
+3 -3
View File
@@ -1078,12 +1078,12 @@ namespace Swarmops.Database
}*/
private BasicFinancialAccount[] GetFinancialAccountTree(Dictionary<int, List<BasicFinancialAccount>> FinancialAccounts, int startNodeId,
private BasicFinancialAccount[] GetFinancialAccountTree(Dictionary<int, List<BasicFinancialAccount>> financialAccounts, int startNodeId,
int generation)
{
List<BasicFinancialAccount> result = new List<BasicFinancialAccount>();
List<BasicFinancialAccount> thisList = FinancialAccounts[startNodeId];
List<BasicFinancialAccount> 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)
{
+15 -3
View File
@@ -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">
<thead>
<tr>
<th field="accountName" width="240"><asp:Literal ID="LiteralHeaderAccountName" runat="server"/>Account Name</th>
<th field="budget" width="160" align="left">Owner</th>
<th field="owner" width="160" align="left">Owner</th>
<th field="balance" width="80" align="right">Balance</th>
<th field="budget" width="80" align="right">Budget</th>
<th field="class" width="80" align="center">Flags</th>
<th field="class" width="65" align="center">Flags</th>
<th field="action" width="40" align="center">Edit</th>
</tr>
</thead>
@@ -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);
}
}
@@ -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? "<span style=\\\"padding-left:18px;background-repeat:no-repeat;background-image:url('" + account.Owner.GetSecureAvatarLink(16) + "')\\\">" + JsonSanitize(Server.HtmlEncode(account.Owner.Canonical)) + "</span>" : 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<int, List<FinancialAccount>> _treeMap;
private Dictionary<int, Int64> _singleBalanceLookup;
private Dictionary<int, Int64> _treeBalanceLookup;
private Dictionary<int, Int64> _singleBudgetLookup;
private Dictionary<int, Int64> _treeBudgetLookup;
private void PopulateLookups(FinancialAccounts accounts)
{
_singleBalanceLookup = new Dictionary<int, Int64>();
_treeBalanceLookup = new Dictionary<int, Int64>();
_singleBudgetLookup = new Dictionary<int, Int64>();
_treeBudgetLookup = new Dictionary<int, Int64>();
// 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<int, Int64> 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,
"<span class=\\\"accountplandata-collapsed-{0}\\\"><strong>&Sigma;</strong> {1:N0}</span><span class=\\\"accountplandata-expanded-{0}\\\" style=\\\"display:none\\\">&nbsp;</span>",
accountId, treeValue / 100.00);
}
return string.Format(_renderCulture,
"<span class=\\\"accountplandata-collapsed-{0}\\\"><strong>&Sigma;</strong> {1:N0}</span><span class=\\\"accountplandata-expanded-{0}\\\" style=\\\"display:none\\\">{2:N0}</span>",
accountId, treeValue / 100.0, singleValue / 100.0);
}
}
}
@@ -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;