diff --git a/Logic/Financial/FinancialAccountRows.cs b/Logic/Financial/FinancialAccountRows.cs index ba7708a2..3bb14f80 100644 --- a/Logic/Financial/FinancialAccountRows.cs +++ b/Logic/Financial/FinancialAccountRows.cs @@ -1,5 +1,8 @@ +using System; using System.Collections.Generic; using Swarmops.Basic.Types.Financial; +using Swarmops.Database; +using Swarmops.Logic.Structure; namespace Swarmops.Logic.Financial { @@ -16,5 +19,15 @@ namespace Swarmops.Logic.Financial return result; } + + public static FinancialAccountRows ForOrganization(Organization organization, DateTime fromDate, + DateTime toDate) + { + FinancialAccounts orgAccounts = FinancialAccounts.ForOrganization (organization); + orgAccounts.GetRows (fromDate, toDate); + return + FromArray (SwarmDb.GetDatabaseForReading() + .GetFinancialAccountRows (orgAccounts.Identities, fromDate, toDate, false)); + } } } \ No newline at end of file diff --git a/Logic/Financial/FinancialTransaction.cs b/Logic/Financial/FinancialTransaction.cs index 61eaeb08..5ab255ea 100644 --- a/Logic/Financial/FinancialTransaction.cs +++ b/Logic/Financial/FinancialTransaction.cs @@ -327,11 +327,26 @@ namespace Swarmops.Logic.Financial } + public Dictionary GetRecalculationBase() + { + Dictionary currentTransaction = new Dictionary(); + + foreach (FinancialTransactionRow row in Rows) + { + if (!currentTransaction.ContainsKey(row.FinancialAccountId)) + { + currentTransaction[row.FinancialAccountId] = 0; + } + + currentTransaction[row.FinancialAccountId] += row.AmountCents; + } + + return currentTransaction; + } + + public bool RecalculateTransaction (Dictionary nominalTransaction, Person loggingPerson) { - // TODO: Update dimension 2 with dimension 1 P&L accounts as template - - bool changedTransaction = false; // We need to create a delta. This is... somewhat complicated. @@ -340,17 +355,7 @@ namespace Swarmops.Logic.Financial // 2) Create a "should-look-like" transaction record. (done in calling routine, already). // 3) Apply the delta, in two steps. - Dictionary currentTransaction = new Dictionary(); - - foreach (FinancialTransactionRow row in Rows) - { - if (!currentTransaction.ContainsKey (row.FinancialAccountId)) - { - currentTransaction[row.FinancialAccountId] = 0; - } - - currentTransaction[row.FinancialAccountId] += row.AmountCents; - } + Dictionary currentTransaction = GetRecalculationBase(); FinancialTransaction continuedTransaction = ContinuedTransaction; @@ -395,9 +400,6 @@ namespace Swarmops.Logic.Financial } } - // TODO: Dimension 2 - - return changedTransaction; } diff --git a/Site5/App_GlobalResources/Pages.Ledgers.resx b/Site5/App_GlobalResources/Pages.Ledgers.resx index 8d1f09e1..b84176fc 100644 --- a/Site5/App_GlobalResources/Pages.Ledgers.resx +++ b/Site5/App_GlobalResources/Pages.Ledgers.resx @@ -400,6 +400,7 @@ Amount + keep short Description @@ -469,14 +470,10 @@ Daily Operations - - Initial Value - For asset and liability accounts - the value going into Swarmops first ledger year - Parent account or group - + 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. Displayed as a message box @@ -558,4 +555,11 @@ New Account the name of newly-created accounts, before rename + + This is a Liabilities account. Such accounts normally have a balance in the negative, since they represent negative assets in the organization. Your entered initial balance was changed to negative. + + + Initial Balance (inbound into {0}), {1} + {0} is the first ledger year in Swarmops. {1} is the currency code. + \ No newline at end of file diff --git a/Site5/Automation/Json-FinancialAccountsTree.aspx.cs b/Site5/Automation/Json-FinancialAccountsTree.aspx.cs index d955dc19..cc74a6b7 100644 --- a/Site5/Automation/Json-FinancialAccountsTree.aspx.cs +++ b/Site5/Automation/Json-FinancialAccountsTree.aspx.cs @@ -55,28 +55,28 @@ namespace Swarmops.Frontend.Automation if (accountType == FinancialAccountType.Asset || accountType == FinancialAccountType.Balance || accountType == FinancialAccountType.All) { - response += GetAccountGroup (FinancialAccountType.Asset, Ledgers.BalanceSheet_Assets) + + response += GetAccountGroup (FinancialAccountType.Asset, Resources.Global.Financial_Asset) + ","; } if (accountType == FinancialAccountType.Debt || accountType == FinancialAccountType.Balance || accountType == FinancialAccountType.All) { - response += GetAccountGroup (FinancialAccountType.Debt, Ledgers.BalanceSheet_Debt) + ","; + response += GetAccountGroup (FinancialAccountType.Debt, Resources.Global.Financial_Debt) + ","; } if (accountType == FinancialAccountType.Income || accountType == FinancialAccountType.Result || accountType == FinancialAccountType.All) { response += - GetAccountGroup (FinancialAccountType.Income, Ledgers.ProfitLossStatement_Income) + + GetAccountGroup(FinancialAccountType.Income, Resources.Global.Financial_Income) + ","; } if (accountType == FinancialAccountType.Cost || accountType == FinancialAccountType.Result || accountType == FinancialAccountType.All) { - response += GetAccountGroup (FinancialAccountType.Cost, Ledgers.ProfitLossStatement_Costs); + response += GetAccountGroup(FinancialAccountType.Cost, Resources.Global.Financial_Cost); } response = response.TrimEnd (','); diff --git a/Site5/Pages/v5/Ledgers/AccountPlan.aspx b/Site5/Pages/v5/Ledgers/AccountPlan.aspx index 6d4634f9..63f52257 100644 --- a/Site5/Pages/v5/Ledgers/AccountPlan.aspx +++ b/Site5/Pages/v5/Ledgers/AccountPlan.aspx @@ -1,6 +1,8 @@ <%@ Page Title="" Language="C#" MasterPageFile="~/Master-v5.master" AutoEventWireup="true" CodeFile="AccountPlan.aspx.cs" Inherits="Swarmops.Frontend.Pages.v5.Ledgers.AccountPlan" %> +<%@ Import Namespace="Swarmops.Logic.Financial" %> <%@ Register TagPrefix="Swarmops5" TagName="ComboBudgets" Src="~/Controls/v5/Financial/ComboBudgets.ascx" %> <%@ Register TagPrefix="Swarmops5" TagName="ComboPeople" Src="~/Controls/v5/Swarm/ComboPeople.ascx" %> +<%@ Register TagPrefix="Swarmops5" TagName="TextCurrency" Src="~/Controls/v5/Financial/CurrencyTextBox.ascx" %> @@ -147,6 +149,51 @@ }); }); + $('#<%=CurrencyInitialBalance.ClientID%>_Input').blur(function() { + var newAccountInitialBalance = $(this).val(); + + if (modalAccountInitialBalance == newAccountInitialBalance) { + return; // nothing changed, nothing to do + } + + if (accountType == 'D' && newAccountInitialBalance[0] != '-') { + alertify.alert(''); + newAccountInitialBalance = "-" + newAccountInitialBalance; + } + + var jsonData = {}; + jsonData.accountId = accountId; + jsonData.newInitialBalanceString = newAccountInitialBalance; + + $(this).css('background-color', '#FFFFE0'); + $.ajax({ + type: "POST", + url: "AccountPlan.aspx/SetAccountInitialBalance", + data: $.toJSON(jsonData), + contentType: "application/json; charset=utf-8", + dataType: "json", + success: $.proxy(function(msg) { + if (msg.d.Result == 3 || msg.d.Result == 4) { // Invalid or NoPermission + $(this).css('background-color', '#FFA0A0'); + alertify.error("There was an error attempting to set your initial balance."); // TODO: Localize + } else { // msg.d.Result should be Changed here + $(this).css('background-color', '#E0FFE0'); + modalAccountInitialBalance = msg.d.NewData; + accountDirty = true; + } + $(this).val(modalAccountInitialBalance); + $(this).animate({ backgroundColor: "#FFFFFF" }, 250); + }, this), + error: $.proxy(function(msg) { + alertify.error("There was an error calling the server to set your initial balance. Is the server reachable?"); // TODO: Localize + $(this).val(modalAccountInitialBalance); + $(this).css('background-color', '#FFA0A0'); + $(this).animate({ backgroundColor: "#FFFFFF" }, 250); + }, this) + }); + }); + + }); @@ -176,6 +223,14 @@ accountTreeLoaded = false; accountTree.combotree('setText', ''); + if ((accountType == 'A' || accountType == 'D') && firstFiscalYear > ledgersClosedUntil) { + $('#DivEditInitLabels').show(); + $('#DivEditInitControls').show(); + } else { + $('#DivEditInitLabels').hide(); + $('#DivEditInitControls').hide(); + } + if (accountType == 'A') { $('#DivEditAssetLabels').show(); $('#DivEditAssetControls').show(); @@ -207,6 +262,7 @@ $('span#<%= DropOwner.ClientID %>_SpanPeople span input.combo-text').css('background-color', '#DDD'); $('#TextAccountBudget').val('...').css('background-color', '#DDD'); $('#TextAccountName').val('...').css('background-color', '#DDD'); + $('#<%=CurrencyInitialBalance.ClientID%>_Input').val('...').css('background-color', '#DDD'); $('span#<%= DropOwner.ClientID %>_SpanPeople span input.combo-text').attr('placeholder', '...'); accountTree.combotree('setText', '...'); @@ -327,6 +383,9 @@ modalAccountBudget = msg.d.Budget; $('#TextAccountBudget').val(msg.d.Budget).css('background-color', '#FFF'); + modalAccountInitialBalance = msg.d.InitialBalance; + $('#<%=CurrencyInitialBalance.ClientID%>_Input').val(msg.d.InitialBalance).css('background-color', '#FFF'); + $('#SpanTextCurrency').text(msg.d.CurrencyCode); $('#SpanEditBalance').text(msg.d.Balance); parentAccountName = msg.d.ParentAccountName; @@ -432,10 +491,13 @@ } - var currentYear = <%=DateTime.Today.Year %>; + var currentYear = <%=DateTime.Today.Year %>; + var firstFiscalYear = <%=CurrentOrganization.FirstFiscalYear %>; + var ledgersClosedUntil = <%=CurrentOrganization.Parameters.FiscalBooksClosedUntilYear %>; var modalAccountName = ""; var modalAccountBudget = ""; + var modalAccountInitialBalance = ""; var accountId = 0; var accountType = ''; var accountDirty = false; @@ -492,16 +554,20 @@

 
 
-  
-
 
+
 
+  
 
 


 
-
-  
+
 
+
+  
+ +  
+
+


Upload parameter, if any
diff --git a/Site5/Pages/v5/Ledgers/AccountPlan.aspx.cs b/Site5/Pages/v5/Ledgers/AccountPlan.aspx.cs index 7e8196bc..b71a7128 100644 --- a/Site5/Pages/v5/Ledgers/AccountPlan.aspx.cs +++ b/Site5/Pages/v5/Ledgers/AccountPlan.aspx.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Globalization; using System.Web; using System.Web.Services; @@ -7,6 +8,7 @@ using Swarmops.Basic.Enums; using Swarmops.Database; using Swarmops.Logic.Financial; using Swarmops.Logic.Security; +using Swarmops.Logic.Structure; using Swarmops.Logic.Swarm; namespace Swarmops.Frontend.Pages.v5.Ledgers @@ -56,7 +58,9 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers this.BoxTitle.Text = PageTitle = Resources.Pages.Ledgers.AccountPlan_PageTitle; InfoBoxLiteral = Resources.Pages.Ledgers.AccountPlan_Info; this.LiteralExpensesBudgetsAreNegative.Text = - Resources.Pages.Ledgers.AccountPlan_ExpensesBudgetsAreNegaive; + Resources.Pages.Ledgers.AccountPlan_ExpensesBudgetsAreNegative; + this.LiteralDebtBalancesAreNegative.Text = + Resources.Pages.Ledgers.AccountPlan_DebtBalancesAreNegative; this.LiteralHeaderAccountName.Text = Resources.Pages.Ledgers.AccountPlan_Header_AccountName; this.LiteralHeaderBalance.Text = Resources.Pages.Ledgers.AccountPlan_Header_Balance; this.LiteralHeaderBudget.Text = Resources.Pages.Ledgers.AccountPlan_Header_Budget; @@ -79,6 +83,10 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers Resources.Pages.Ledgers.AccountPlan_Edit_HeaderDailyOperations; this.LiteralLabelOwner.Text = Global.Global_Owner; this.LiteralLabelParent.Text = Resources.Pages.Ledgers.AccountPlan_Edit_Parent; + + this.LiteralLabelInitialAmount.Text = + String.Format (Resources.Pages.Ledgers.AccountPlan_Edit_InitialBalance, + CurrentOrganization.FirstFiscalYear, CurrentOrganization.Currency.DisplayCode); } PageAccessRequired = new Access (CurrentOrganization, AccessAspect.Bookkeeping, AccessType.Write); DbVersionRequired = 2; // Account reparenting @@ -127,6 +135,9 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers (accountTree.GetDeltaCents (new DateTime (1900, 1, 1), new DateTime (year + 1, 1, 1))/100L).ToString ( "N0"); + result.InitialBalance = + (accountTree.GetDeltaCents (new DateTime (1900, 1, 1), + new DateTime (authData.CurrentOrganization.FirstFiscalYear, 1, 1)).ToString ("N2")); } else { @@ -134,6 +145,7 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers (-accountTree.GetDeltaCents (new DateTime (year, 1, 1), new DateTime (year + 1, 1, 1))/100L) .ToString ( "N0"); + result.InitialBalance = "N/A"; // unused } result.CurrencyCode = account.Organization.Currency.DisplayCode; @@ -255,6 +267,87 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers return true; } + [WebMethod] + public static ChangeAccountDataResult SetAccountInitialBalance (int accountId, string newInitialBalanceString) + { + try + { + AuthenticationData authData = GetAuthenticationDataAndCulture(); + FinancialAccount account = FinancialAccount.FromIdentity (accountId); + + if (!PrepareAccountChange (account, authData, false) || authData.CurrentOrganization.Parameters.FiscalBooksClosedUntilYear >= authData.CurrentOrganization.FirstFiscalYear) + { + return new ChangeAccountDataResult + { + Result = ChangeAccountDataOperationsResult.NoPermission + }; + } + + Int64 desiredInitialBalanceCents = + (Int64) + (Double.Parse (newInitialBalanceString, + NumberStyles.AllowThousands | NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint, + CultureInfo.CurrentCulture)*100.0); + + Int64 currentInitialBalanceCents = account.GetDeltaCents (new DateTime (1900, 1, 1), + new DateTime (authData.CurrentOrganization.FirstFiscalYear, 1, 1)); + + Int64 deltaCents = desiredInitialBalanceCents - currentInitialBalanceCents; + + // Find or create "Initial Balances" transaction + + FinancialAccountRows testRows = FinancialAccountRows.ForOrganization (authData.CurrentOrganization, + new DateTime (1900, 1, 1), new DateTime (authData.CurrentOrganization.FirstFiscalYear, 1, 1)); + + FinancialTransaction initialBalancesTransaction = null; + + foreach (FinancialAccountRow row in testRows) + { + if (row.Transaction.Description == "Initial Balances") + { + initialBalancesTransaction = row.Transaction; + break; + } + } + + if (initialBalancesTransaction == null) + { + // create transaction + + initialBalancesTransaction = FinancialTransaction.Create (authData.CurrentOrganization.Identity, + new DateTime (authData.CurrentOrganization.FirstFiscalYear - 1, 12, 31), "Initial Balances"); + } + + Dictionary recalcBase = initialBalancesTransaction.GetRecalculationBase(); + int equityAccountId = authData.CurrentOrganization.FinancialAccounts.DebtsEquity.Identity; + + if (!recalcBase.ContainsKey (accountId)) + { + recalcBase[accountId] = 0; + } + if (!recalcBase.ContainsKey (equityAccountId)) + { + recalcBase[equityAccountId] = 0; + } + + recalcBase[accountId] += deltaCents; + recalcBase[equityAccountId] -= deltaCents; + initialBalancesTransaction.RecalculateTransaction (recalcBase, authData.CurrentUser); + return new ChangeAccountDataResult + { + Result = ChangeAccountDataOperationsResult.Changed, + NewData = (desiredInitialBalanceCents / 100.0).ToString("N2", CultureInfo.CurrentCulture) + }; + } + catch (Exception weirdException) + { + SwarmDb.GetDatabaseForWriting() + .CreateExceptionLogEntry(DateTime.UtcNow, "AccountPlan-SetInitBalance", weirdException); + + throw; + } + } + [WebMethod] public static ChangeAccountDataResult SetAccountBudget (int accountId, string budget) { @@ -357,6 +450,7 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers public string AccountOwnerName; public bool Active; public bool Administrative; + public string InitialBalance; public string Balance; public string Budget; public string ClosedYear; diff --git a/Site5/Pages/v5/Ledgers/Json-AccountPlanData.aspx.cs b/Site5/Pages/v5/Ledgers/Json-AccountPlanData.aspx.cs index 17e1de3b..4f4c559b 100644 --- a/Site5/Pages/v5/Ledgers/Json-AccountPlanData.aspx.cs +++ b/Site5/Pages/v5/Ledgers/Json-AccountPlanData.aspx.cs @@ -41,11 +41,11 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers PopulateLookups (FinancialAccounts.ForOrganization (this._authenticationData.CurrentOrganization)); this._hashedAccounts = FinancialAccounts.GetHashedAccounts (this._authenticationData.CurrentOrganization); - response += GetAccountGroup (FinancialAccountType.Asset, Resources.Pages.Ledgers.BalanceSheet_Assets) + ","; - response += GetAccountGroup (FinancialAccountType.Debt, Resources.Pages.Ledgers.BalanceSheet_Debt) + ","; + response += GetAccountGroup (FinancialAccountType.Asset, Resources.Global.Financial_Asset) + ","; + response += GetAccountGroup (FinancialAccountType.Debt, Resources.Global.Financial_Debt) + ","; response += - GetAccountGroup (FinancialAccountType.Income, Resources.Pages.Ledgers.ProfitLossStatement_Income) + ","; - response += GetAccountGroup (FinancialAccountType.Cost, Resources.Pages.Ledgers.ProfitLossStatement_Costs); + GetAccountGroup (FinancialAccountType.Income, Resources.Global.Financial_Income) + ","; + response += GetAccountGroup (FinancialAccountType.Cost, Resources.Global.Financial_Cost); Response.Output.WriteLine ("[" + response + "]"); Response.End();