This commit is contained in:
@@ -1 +1 @@
|
||||
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Json-ExpensableBudgetsTree.aspx.cs" Inherits="Pages_v5_Finance_Json_ExpensableBudgetsTree" %>
|
||||
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Json-ExpensableBudgetsTree.aspx.cs" Inherits="Swarmops.Frontend.Pages.v5.Financial.Json_ExpensableBudgetsTree" %>
|
||||
@@ -10,96 +10,101 @@ using Swarmops.Logic.Swarm;
|
||||
using Swarmops.Logic.Security;
|
||||
using Swarmops.Logic.Structure;
|
||||
|
||||
public partial class Pages_v5_Finance_Json_ExpensableBudgetsTree : System.Web.UI.Page
|
||||
namespace Swarmops.Frontend.Pages.v5.Financial
|
||||
{
|
||||
protected void Page_Load(object sender, EventArgs e)
|
||||
public partial class Json_ExpensableBudgetsTree : System.Web.UI.Page
|
||||
{
|
||||
// Current authentication
|
||||
|
||||
string identity = HttpContext.Current.User.Identity.Name;
|
||||
string[] identityTokens = identity.Split(',');
|
||||
|
||||
string userIdentityString = identityTokens[0];
|
||||
string organizationIdentityString = identityTokens[1];
|
||||
|
||||
int currentUserId = Convert.ToInt32(userIdentityString);
|
||||
int currentOrganizationId = Convert.ToInt32(organizationIdentityString);
|
||||
|
||||
Person currentUser = Person.FromIdentity(currentUserId);
|
||||
Authority authority = currentUser.GetAuthority();
|
||||
Organization currentOrganization = Organization.FromIdentity(currentOrganizationId);
|
||||
|
||||
Response.ContentType = "application/json";
|
||||
|
||||
// Is this stuff in cache already?
|
||||
|
||||
string cacheKey = "ExpensableAccounts-Json-" + currentOrganizationId.ToString((CultureInfo.InvariantCulture));
|
||||
|
||||
string accountsJson =
|
||||
(string) Cache[cacheKey];
|
||||
|
||||
if (accountsJson != null)
|
||||
protected void Page_Load(object sender, EventArgs e)
|
||||
{
|
||||
// Current authentication
|
||||
|
||||
string identity = HttpContext.Current.User.Identity.Name;
|
||||
string[] identityTokens = identity.Split(',');
|
||||
|
||||
string userIdentityString = identityTokens[0];
|
||||
string organizationIdentityString = identityTokens[1];
|
||||
|
||||
int currentUserId = Convert.ToInt32(userIdentityString);
|
||||
int currentOrganizationId = Convert.ToInt32(organizationIdentityString);
|
||||
|
||||
Person currentUser = Person.FromIdentity(currentUserId);
|
||||
Authority authority = currentUser.GetAuthority();
|
||||
Organization currentOrganization = Organization.FromIdentity(currentOrganizationId);
|
||||
|
||||
Response.ContentType = "application/json";
|
||||
|
||||
// Is this stuff in cache already?
|
||||
|
||||
string cacheKey = "ExpensableAccounts-Json-" +
|
||||
currentOrganizationId.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 = currentOrganization.FinancialAccounts.ExpensableAccounts;
|
||||
|
||||
// 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();
|
||||
return;
|
||||
}
|
||||
|
||||
// Not in cache. Construct.
|
||||
|
||||
// Get accounts
|
||||
|
||||
FinancialAccounts accounts = currentOrganization.FinancialAccounts.ExpensableAccounts;
|
||||
|
||||
// Build tree (there should be a template for this)
|
||||
|
||||
Dictionary<int, List<FinancialAccount>> treeMap = new Dictionary<int, List<FinancialAccount>>();
|
||||
|
||||
foreach (FinancialAccount account in accounts)
|
||||
private string RecurseTreeMap(Dictionary<int, List<FinancialAccount>> treeMap, int node)
|
||||
{
|
||||
if (!treeMap.ContainsKey(account.ParentIdentity))
|
||||
List<string> elements = new List<string>();
|
||||
|
||||
foreach (FinancialAccount account in treeMap[node])
|
||||
{
|
||||
treeMap[account.ParentIdentity] = new List<FinancialAccount>();
|
||||
string element = string.Format("\"id\":{0},\"text\":\"{1}\"", account.Identity,
|
||||
account.Name.Replace("\"", "'"));
|
||||
|
||||
if (treeMap.ContainsKey(account.Identity))
|
||||
{
|
||||
element += ",\"state\":\"closed\",\"children\":" + RecurseTreeMap(treeMap, account.Identity);
|
||||
}
|
||||
|
||||
elements.Add("{" + element + "}");
|
||||
}
|
||||
|
||||
treeMap[account.ParentIdentity].Add(account);
|
||||
return "[" + String.Join(",", elements.ToArray()) + "]";
|
||||
|
||||
}
|
||||
|
||||
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,
|
||||
account.Name.Replace("\"", "'"));
|
||||
|
||||
if (treeMap.ContainsKey(account.Identity))
|
||||
{
|
||||
element += ",\"state\":\"closed\",\"children\":" + RecurseTreeMap(treeMap, account.Identity);
|
||||
}
|
||||
|
||||
elements.Add("{" + element + "}");
|
||||
}
|
||||
|
||||
return "[" + String.Join(",", elements.ToArray()) + "]";
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
<%@ Page Title="" Language="C#" MasterPageFile="~/Master-v5.master" AutoEventWireup="true" CodeFile="RequestCashAdvance.aspx.cs" Inherits="Pages_v5_Finance_RequestCashAdvance" %>
|
||||
<%@ Page Title="" Language="C#" MasterPageFile="~/Master-v5.master" AutoEventWireup="true" CodeFile="RequestCashAdvance.aspx.cs" Inherits="Swarmops.Frontend.Pages.v5.Financial.RequestCashAdvance" %>
|
||||
|
||||
<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderHead" Runat="Server">
|
||||
<script src="https://hostedscripts.falkvinge.net/easyui/jquery.easyui.min.js" type="text/javascript"></script>
|
||||
|
||||
@@ -7,106 +7,113 @@ using System.Web.UI;
|
||||
using System.Web.UI.WebControls;
|
||||
using Swarmops.Logic.Financial;
|
||||
|
||||
public partial class Pages_v5_Finance_RequestCashAdvance : PageV5Base
|
||||
namespace Swarmops.Frontend.Pages.v5.Financial
|
||||
{
|
||||
protected void Page_Load(object sender, EventArgs e)
|
||||
public partial class RequestCashAdvance : PageV5Base
|
||||
{
|
||||
if (!this.CurrentOrganization.IsEconomyEnabled)
|
||||
protected void Page_Load(object sender, EventArgs e)
|
||||
{
|
||||
Response.Redirect("/Pages/v5/Financial/EconomyNotEnabled.aspx", true);
|
||||
return;
|
||||
if (!this.CurrentOrganization.IsEconomyEnabled)
|
||||
{
|
||||
Response.Redirect("/Pages/v5/Financial/EconomyNotEnabled.aspx", true);
|
||||
return;
|
||||
}
|
||||
|
||||
this.PageTitle = Resources.Pages.Finance.RequestCashAdvance_PageTitle;
|
||||
this.PageIcon = "iconshock-walletmoney";
|
||||
this.InfoBoxLiteral = Resources.Pages.Finance.RequestCashAdvance_Info;
|
||||
|
||||
if (!Page.IsPostBack)
|
||||
{
|
||||
// Prime bank details
|
||||
|
||||
this.TextBank.Text = this.CurrentUser.BankName;
|
||||
this.TextClearing.Text = this.CurrentUser.BankClearing;
|
||||
this.TextAccount.Text = this.CurrentUser.BankAccount;
|
||||
this.TextAmount.Text = 0.ToString("N2");
|
||||
this.TextAmount.Focus();
|
||||
|
||||
Localize();
|
||||
}
|
||||
}
|
||||
|
||||
this.PageTitle = Resources.Pages.Finance.RequestCashAdvance_PageTitle;
|
||||
this.PageIcon = "iconshock-walletmoney";
|
||||
this.InfoBoxLiteral = Resources.Pages.Finance.RequestCashAdvance_Info;
|
||||
|
||||
if (!Page.IsPostBack)
|
||||
private void Localize()
|
||||
{
|
||||
// Prime bank details
|
||||
this.LabelAmount.Text = string.Format(Resources.Pages.Finance.RequestCashAdvance_Amount,
|
||||
CurrentOrganization.Currency.Code);
|
||||
this.LabelPurpose.Text = Resources.Pages.Finance.RequestCashAdvance_Purpose;
|
||||
this.LabelBudget.Text = Resources.Pages.Finance.RequestCashAdvance_Budget;
|
||||
this.LabelHeaderBankDetails.Text = Resources.Pages.Finance.RequestCashAdvance_HeaderBankDetails;
|
||||
this.LabelBankName.Text = Resources.Pages.Finance.RequestCashAdvance_BankName;
|
||||
this.LabelBankClearing.Text = Resources.Pages.Finance.RequestCashAdvance_BankClearing;
|
||||
this.LabelBankAccount.Text = Resources.Pages.Finance.RequestCashAdvance_BankAccount;
|
||||
|
||||
this.TextBank.Text = this.CurrentUser.BankName;
|
||||
this.TextClearing.Text = this.CurrentUser.BankClearing;
|
||||
this.TextAccount.Text = this.CurrentUser.BankAccount;
|
||||
this.TextAmount.Text = 0.ToString("N2");
|
||||
this.TextAmount.Focus();
|
||||
|
||||
Localize();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void Localize()
|
||||
{
|
||||
this.LabelAmount.Text = string.Format(Resources.Pages.Finance.RequestCashAdvance_Amount,
|
||||
CurrentOrganization.Currency.Code);
|
||||
this.LabelPurpose.Text = Resources.Pages.Finance.RequestCashAdvance_Purpose;
|
||||
this.LabelBudget.Text = Resources.Pages.Finance.RequestCashAdvance_Budget;
|
||||
this.LabelHeaderBankDetails.Text = Resources.Pages.Finance.RequestCashAdvance_HeaderBankDetails;
|
||||
this.LabelBankName.Text = Resources.Pages.Finance.RequestCashAdvance_BankName;
|
||||
this.LabelBankClearing.Text = Resources.Pages.Finance.RequestCashAdvance_BankClearing;
|
||||
this.LabelBankAccount.Text = Resources.Pages.Finance.RequestCashAdvance_BankAccount;
|
||||
|
||||
this.LiteralErrorAmount.Text = Resources.Pages.Finance.RequestCashAdvance_ValidationError_Amount;
|
||||
this.LiteralErrorPurpose.Text = Resources.Pages.Finance.RequestCashAdvance_ValidationError_Purpose;
|
||||
this.LiteralErrorBudget.Text = Resources.Pages.Finance.RequestCashAdvance_ValidationError_Budget;
|
||||
this.LiteralErrorBankName.Text = Resources.Pages.Finance.RequestCashAdvance_ValidationError_BankName;
|
||||
this.LiteralErrorBankClearing.Text = Resources.Pages.Finance.RequestCashAdvance_ValidationError_BankClearing;
|
||||
this.LiteralErrorBankAccount.Text = Resources.Pages.Finance.RequestCashAdvance_ValidationError_BankAccount;
|
||||
}
|
||||
|
||||
|
||||
protected void ButtonRequest_Click(object sender, EventArgs e)
|
||||
{
|
||||
// The data has been validated client-side already. We'll throw unfriendly exceptions if invalid data is passed here.
|
||||
|
||||
double amount = Double.Parse(this.TextAmount.Text, NumberStyles.Number); // parses in current culture - intentional
|
||||
Int64 amountCents = (Int64) amount*100;
|
||||
|
||||
string description = this.TextPurpose.Text;
|
||||
|
||||
FinancialAccount budget = FinancialAccount.FromIdentity(Int32.Parse(this.Request.Form["DropBudgets"]));
|
||||
|
||||
// sanity check
|
||||
|
||||
if (budget.Organization.Identity != CurrentOrganization.Identity)
|
||||
{
|
||||
throw new InvalidOperationException("Budget-organization mismatch; won't file cash advance");
|
||||
this.LiteralErrorAmount.Text = Resources.Pages.Finance.RequestCashAdvance_ValidationError_Amount;
|
||||
this.LiteralErrorPurpose.Text = Resources.Pages.Finance.RequestCashAdvance_ValidationError_Purpose;
|
||||
this.LiteralErrorBudget.Text = Resources.Pages.Finance.RequestCashAdvance_ValidationError_Budget;
|
||||
this.LiteralErrorBankName.Text = Resources.Pages.Finance.RequestCashAdvance_ValidationError_BankName;
|
||||
this.LiteralErrorBankClearing.Text = Resources.Pages.Finance.RequestCashAdvance_ValidationError_BankClearing;
|
||||
this.LiteralErrorBankAccount.Text = Resources.Pages.Finance.RequestCashAdvance_ValidationError_BankAccount;
|
||||
}
|
||||
|
||||
// Store bank details for current user
|
||||
|
||||
CurrentUser.BankName = this.TextBank.Text;
|
||||
CurrentUser.BankClearing = this.TextClearing.Text;
|
||||
CurrentUser.BankAccount = this.TextAccount.Text;
|
||||
|
||||
// Create cash advance
|
||||
|
||||
CashAdvance cashAdvance = CashAdvance.Create(CurrentOrganization, CurrentUser, CurrentUser, amountCents, budget,
|
||||
description);
|
||||
|
||||
// Create success message
|
||||
|
||||
string successMessage = string.Format(Resources.Pages.Finance.RequestCashAdvance_SuccessMessagePartOne,
|
||||
HttpUtility.HtmlEncode(CurrentUser.Name),
|
||||
HttpUtility.HtmlEncode(description), CurrentOrganization.Currency.Code,
|
||||
(double) (amountCents/100.0));
|
||||
|
||||
if (budget.OwnerPersonId != CurrentUser.Identity)
|
||||
protected void ButtonRequest_Click(object sender, EventArgs e)
|
||||
{
|
||||
successMessage += "<br/><br/>" + Resources.Pages.Finance.RequestCashAdvance_SuccessMessagePartTwo + "<br/>";
|
||||
// The data has been validated client-side already. We'll throw unfriendly exceptions if invalid data is passed here.
|
||||
|
||||
double amount = Double.Parse(this.TextAmount.Text, NumberStyles.Number);
|
||||
// parses in current culture - intentional
|
||||
Int64 amountCents = (Int64) amount*100;
|
||||
|
||||
string description = this.TextPurpose.Text;
|
||||
|
||||
FinancialAccount budget = FinancialAccount.FromIdentity(Int32.Parse(this.Request.Form["DropBudgets"]));
|
||||
|
||||
// sanity check
|
||||
|
||||
if (budget.Organization.Identity != CurrentOrganization.Identity)
|
||||
{
|
||||
throw new InvalidOperationException("Budget-organization mismatch; won't file cash advance");
|
||||
}
|
||||
|
||||
// Store bank details for current user
|
||||
|
||||
CurrentUser.BankName = this.TextBank.Text;
|
||||
CurrentUser.BankClearing = this.TextClearing.Text;
|
||||
CurrentUser.BankAccount = this.TextAccount.Text;
|
||||
|
||||
// Create cash advance
|
||||
|
||||
CashAdvance cashAdvance = CashAdvance.Create(CurrentOrganization, CurrentUser, CurrentUser, amountCents,
|
||||
budget,
|
||||
description);
|
||||
|
||||
// Create success message
|
||||
|
||||
string successMessage = string.Format(Resources.Pages.Finance.RequestCashAdvance_SuccessMessagePartOne,
|
||||
HttpUtility.HtmlEncode(CurrentUser.Name),
|
||||
HttpUtility.HtmlEncode(description), CurrentOrganization.Currency.Code,
|
||||
(double) (amountCents/100.0));
|
||||
|
||||
if (budget.OwnerPersonId != CurrentUser.Identity)
|
||||
{
|
||||
successMessage += "<br/><br/>" + Resources.Pages.Finance.RequestCashAdvance_SuccessMessagePartTwo +
|
||||
"<br/>";
|
||||
}
|
||||
else
|
||||
{
|
||||
successMessage += "<br/><br/>" +
|
||||
Resources.Pages.Finance.RequestCashAdvance_SuccessMessagePartTwoOwnBudget +
|
||||
"<br/>";
|
||||
// TODO: Auto-attest
|
||||
}
|
||||
|
||||
Response.AppendCookie(new HttpCookie("DashboardMessage", HttpUtility.UrlEncode(successMessage)));
|
||||
|
||||
// Redirect to dashboard
|
||||
|
||||
Response.Redirect("/", true);
|
||||
}
|
||||
else
|
||||
{
|
||||
successMessage += "<br/><br/>" + Resources.Pages.Finance.RequestCashAdvance_SuccessMessagePartTwoOwnBudget +
|
||||
"<br/>";
|
||||
// TODO: Auto-attest
|
||||
}
|
||||
|
||||
Response.AppendCookie(new HttpCookie("DashboardMessage", HttpUtility.UrlEncode(successMessage)));
|
||||
|
||||
// Redirect to dashboard
|
||||
|
||||
Response.Redirect("/", true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user