Checkpoint commit for Inspect Ledgers

This commit is contained in:
Rick Falkvinge
2014-10-24 22:13:55 +02:00
parent 13537a80e9
commit 574525219d
11 changed files with 280 additions and 112 deletions
+5 -1
View File
@@ -29,6 +29,10 @@ namespace Swarmops.Basic.Enums
/// <summary>
/// Incomes and costs - accounts that are zeroed at year end. Sign reversed.
/// </summary>
Result = 6
Result = 6,
/// <summary>
/// Meta-type which includes all accounts and group labels where applicable
/// </summary>
All = 7
}
}
+6
View File
@@ -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<FinancialAccountType, bool> lookup = new Dictionary<FinancialAccountType, bool>();
@@ -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;
}
}
}
}
+1
View File
@@ -0,0 +1 @@
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Json-BudgetsTree.aspx.cs" Inherits="Swarmops.Frontend.Automation.Json_BudgetsTree" %>
@@ -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<int, List<FinancialAccount>> treeMap = new Dictionary<int, List<FinancialAccount>>();
foreach (FinancialAccount account in accounts)
{
if (!treeMap.ContainsKey(account.ParentIdentity))
{
treeMap[account.ParentIdentity] = new List<FinancialAccount>();
}
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<int, List<FinancialAccount>> treeMap, int node)
{
List<string> elements = new List<string>();
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<int, List<FinancialAccount>> treeMap = new Dictionary<int, List<FinancialAccount>>();
foreach (FinancialAccount account in accounts)
{
if (!treeMap.ContainsKey(account.ParentIdentity))
{
treeMap[account.ParentIdentity] = new List<FinancialAccount>();
}
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<int, List<FinancialAccount>> treeMap, int node)
{
List<string> elements = new List<string>();
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
}
}
}
@@ -1 +0,0 @@
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Json-ExpensableBudgetsTree.aspx.cs" Inherits="Swarmops.Frontend.Automation.Json_ExpensableBudgetsTree" %>
@@ -1 +1 @@
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Json-FinancialAccountsTree.aspx.cs" Inherits="Swarmops.Frontend.Pages.v5.Financial.Json_FinancialAccountsTree" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Json-FinancialAccountsTree.aspx.cs" Inherits="Swarmops.Frontend.Automation.Json_FinancialAccountsTree" %>
@@ -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<int, List<FinancialAccount>> _hashedAccounts = null;
private string GetAccountsRecurse(FinancialAccountType accountType, int rootNodeId)
{
List<string> childStrings = new List<string>();
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<int, List<FinancialAccount>> treeMap, int node)
{
List<string> elements = new List<string>();
@@ -34,4 +34,4 @@
});
</script>
<span id="<%=this.ClientID %>_SpanBudgets"><select class="easyui-combotree" url="/Automation/Json-ExpensableBudgetsTree.aspx" name="DropBudgets" id="<%=this.ClientID %>_DropBudgets" animate="true" style="width:300px"></select></span>
<span id="<%=this.ClientID %>_SpanBudgets"><select class="easyui-combotree" url="<%=this.DataUrl %>" name="DropBudgets" id="<%=this.ClientID %>_DropBudgets" animate="true" style="width:300px"></select></span>
@@ -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
}
}
}
+1 -1
View File
@@ -10,7 +10,7 @@
<asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderMain" Runat="Server">
<h2>Inspect <Swarmops5:ComboBudgets ID="DropBudgets" AccountListType="All" runat="server" /> for <asp:DropDownList runat="server" ID="DropYears"/> <asp:DropDownList runat="server" ID="Months"/></h2>
<h2>Inspect <Swarmops5:ComboBudgets ID="DropBudgets" ListType="All" runat="server" /> for <asp:DropDownList runat="server" ID="DropYears"/> <asp:DropDownList runat="server" ID="Months"/></h2>
</asp:Content>