Work on Account Plan; needs Mono test

This commit is contained in:
Rick Falkvinge
2014-09-13 14:53:20 +02:00
parent 60fac749f0
commit 021e1066fb
3 changed files with 133 additions and 12 deletions
+49 -5
View File
@@ -38,7 +38,7 @@
$(".IconEdit").click(function() {
accountId = $(this).attr("accountId");
var accountType = accountId.substring(0,1); // A, D, I, C
accountType = accountId.substring(0,1); // A, D, I, C
accountId = accountId.substring(1);
var accountTree = $('#<%=DropParents.ClientID %>_DropBudgets');
@@ -117,15 +117,27 @@
$("#IconCloseEdit").click(function() {
$('#divModalCover').fadeOut();
if (accountDirty) {
$('#tableAccountPlan').treegrid('reload');
accountDirty = false;
}
});
$('#TextAccountName').blur(function() {
var newAccountName = $('#TextAccountName').val();
var newAccountName = $('#TextAccountName').val().trim();
if (modalAccountName == newAccountName) {
return; // nothing changed, nothing to do
}
if (newAccountName == "") { // no, we're not changing to an empty name
$('#TextAccountName').css('background-color', '#FFA0A0');
$('#TextAccountName').val(modalAccountName);
$('#TextAccountName').animate({ backgroundColor: "#FFFFFF" }, 250);
return;
}
$('#TextAccountName').css('background-color', '#FFFFE0');
$.ajax({
type: "POST",
@@ -133,14 +145,15 @@
data: "{'accountId': '" + escape(accountId) + "', 'name':'" + escape(newAccountName) + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
success: function(msg) { // TODO: This needs three return values - changed, broken and changed, or not changed
if (msg.d) { // saved
$('#TextAccountName').css('background-color', '#E0FFE0');
modalAccountName = $('#TextAccountName').val(); // race condition because async. Matters?
accountDirty = true;
} else {
$('#TextAccountName').css('background-color', '#FFA0A0');
$('#TextAccountName').val(modalAccountName);
alertify.error("Cannot change accounts that have transactions in closed books - yet.");
alertify.error("Cannot change accounts that have transactions in closed ledgers - yet."); // TODO: Localize
}
$('#TextAccountName').animate({ backgroundColor: "#FFFFFF" }, 250);
}
@@ -148,7 +161,36 @@
});
$('#TextAccountBudget').blur(function() {
alert('foo');
var newAccountBudget = $('#TextAccountBudget').val();
if (modalAccountBudget == newAccountBudget) {
return; // nothing changed, nothing to do
}
if (accountType == 'C' && newAccountBudget[0] != '-') {
alertify.alert("This is an Expenses account. Such accounts normally have a budget in the negative, since they drain assets from the organization. Your proposed budget was changed to negative.");
newAccountBudget = "-" + newAccountBudget;
}
$('#TextAccountBudget').css('background-color', '#FFFFE0');
$.ajax({
type: "POST",
url: "AccountPlan.aspx/SetAccountBudget",
data: "{'accountId': '" + escape(accountId) + "', 'budget':'" + escape(newAccountBudget) + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) { // TODO: This needs four return values - changed, broken and changed, invalid, or not changed. Also, formatted number
if (msg.d.Result == "Invalid" || msg.d.Result == "NoPermission") {
$('#TextAccountBudget').css('background-color', '#FFA0A0');
alertify.error("There was an error attempting to set your proposed budget."); // TODO: Localize
} else { // msg.d.Result should be Changed here
$('#TextAccountBudget').css('background-color', '#E0FFE0');
modalAccountBudget = msg.d.NewData;
accountDirty = true;
}
$('#TextAccountBudget').val(modalAccountBudget);
$('#TextAccountBudget').animate({ backgroundColor: "#FFFFFF" }, 250);
}
});
});
$('.EditCheck').switchButton(
@@ -171,6 +213,8 @@
var modalAccountName = "";
var modalAccountBudget = "";
var accountId = 0;
var accountType = '';
var accountDirty = false;
</script>
<style>
+82 -5
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Services;
@@ -58,7 +59,7 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
result.AccountOwnerAvatarUrl = account.OwnerPersonId != 0
? account.Owner.GetSecureAvatarLink(24)
: "/Images/Icons/iconshock-warning-24px.png";
result.Budget = (accountTree.GetBudgetSumCents(year)/100L).ToString("N0");
result.Budget = (accountTree.GetBudgetSumCents(year)/100L).ToString("N0", CultureInfo.CurrentCulture);
if (account.AccountType == FinancialAccountType.Asset || account.AccountType == FinancialAccountType.Debt)
{
@@ -73,7 +74,7 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
}
private static bool PrepareAccountChange(FinancialAccount account, AuthenticationData authData)
private static bool PrepareAccountChange(FinancialAccount account, AuthenticationData authData, bool checkOpenedYear)
{
// TODO: Check permissions, too (may be read-only)
@@ -84,7 +85,7 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
try
{
if (account.OpenedYear <= authData.CurrentOrganization.Parameters.FiscalBooksClosedUntilYear)
if (checkOpenedYear && account.OpenedYear <= authData.CurrentOrganization.Parameters.FiscalBooksClosedUntilYear)
{
// This require breaking the account, which we can't do yet (in this sprint, will come next sprint).
return false;
@@ -103,10 +104,9 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
public static bool SetAccountName(int accountId, string name)
{
AuthenticationData authData = GetAuthenticationDataAndCulture();
FinancialAccount account = FinancialAccount.FromIdentity(accountId);
if (!PrepareAccountChange(account, authData))
if (!PrepareAccountChange(account, authData, true))
{
return false;
}
@@ -115,6 +115,83 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
return true;
}
[WebMethod]
public static ChangeAccountDataResult SetAccountBudget(int accountId, string budget)
{
AuthenticationData authData = GetAuthenticationDataAndCulture();
FinancialAccount account = FinancialAccount.FromIdentity(accountId);
if (!PrepareAccountChange(account, authData, false))
{
return new ChangeAccountDataResult
{
Result = ChangeAccountDataOperationsResult.NoPermission
};
}
Int64 newTreeBudget;
budget = budget.Replace("%A0", "%20"); // some very weird browser space-to-otherspace translation weirds out number parsing
budget = HttpContext.Current.Server.UrlDecode(budget);
if (budget.Trim().Length > 0 && Int64.TryParse(budget, NumberStyles.Currency, CultureInfo.CurrentCulture, out newTreeBudget))
{
newTreeBudget *= 100; // convert to cents
int year = DateTime.Today.Year;
FinancialAccounts accountTree = account.GetTree();
Int64 currentTreeBudget = accountTree.GetBudgetSumCents(year);
Int64 currentSingleBudget = account.GetBudgetCents(year);
Int64 suballocatedBudget = currentTreeBudget - currentSingleBudget;
Int64 newSingleBudget = newTreeBudget - suballocatedBudget;
account.SetBudgetCents(DateTime.Today.Year, newSingleBudget);
return new ChangeAccountDataResult
{
Result = ChangeAccountDataOperationsResult.Changed,
NewData = (newTreeBudget/100).ToString("N0", CultureInfo.CurrentCulture)
};
}
else
{
return new ChangeAccountDataResult
{
Result = ChangeAccountDataOperationsResult.Invalid
};
}
}
public class ChangeAccountDataResult
{
public ChangeAccountDataOperationsResult Result { set; get; }
public string NewData { set; get; }
}
public enum ChangeAccountDataOperationsResult
{
Unknown = 0,
/// <summary>
/// The account data was changed and nothing more.
/// </summary>
Changed,
/// <summary>
/// The account has transactions in closed ledgers, so the account was broken in two -
/// the closed ledgers were kept, and all open ledgers were moved into a new account
/// with the new data.
/// </summary>
ChangedBroken,
/// <summary>
/// The user doesn't have write permissions.
/// </summary>
NoPermission,
/// <summary>
/// The data submitted was invalid (for example an unparsable number for budget).
/// </summary>
Invalid
}
public struct JsonAccountData
{
public string AccountName;
@@ -113,10 +113,10 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
account.Identity,
Server.HtmlEncode(JsonSanitize(account.Name)),
ownerString,
_hashedAccounts[account.Identity].Count > 1 ? JsonDualString(account.Identity, _treeBalanceLookup[account.Identity], _singleBalanceLookup[account.Identity]) : (_singleBalanceLookup [account.Identity] / 100.0).ToString("N0"),
_hashedAccounts[account.Identity].Count > 1 ? JsonDualString(account.Identity, _treeBalanceLookup[account.Identity], _singleBalanceLookup[account.Identity]) : (_singleBalanceLookup [account.Identity] / 100.0).ToString("N0", CultureInfo.CurrentCulture),
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") :
(_singleBudgetLookup[account.Identity] / 100.0).ToString("N0", CultureInfo.CurrentCulture) :
string.Empty,
editString
) + grandChildren + '}');