using System; using System.Collections.Generic; using Swarmops.Basic.Enums; using Swarmops.Logic.Financial; namespace Swarmops.Frontend.Automation { public partial class Json_FinancialAccountsTree : DataV5Base { protected void Page_Load(object sender, EventArgs e) { Response.ContentType = "application/json"; FinancialAccountType accountType = (FinancialAccountType) (Enum.Parse(typeof (FinancialAccountType), Request.QueryString["AccountType"])); string response = string.Empty; int excludeId = -1; string excludeIdString = Request.QueryString["ExcludeId"]; if (!String.IsNullOrEmpty(excludeIdString)) { excludeId = Int32.Parse(excludeIdString); } // Get accounts FinancialAccounts accounts = FinancialAccounts.ForOrganization(this.CurrentOrganization, accountType); // Build tree (there should be a template for this) Dictionary> treeMap = new Dictionary>(); foreach (FinancialAccount account in accounts) { if (account.Identity == excludeId) { continue; } if (!treeMap.ContainsKey(account.ParentIdentity)) { treeMap[account.ParentIdentity] = new List(); } treeMap[account.ParentIdentity].Add(account); } _hashedAccounts = treeMap; if (accountType == FinancialAccountType.Asset || accountType == FinancialAccountType.Balance || accountType == FinancialAccountType.All) { response += GetAccountGroup(FinancialAccountType.Asset, Resources.Pages_Ledgers.BalanceSheet_Assets) + ","; } 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(); 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()) + "]"; } } }