Inbound Balances in Account Plan works. This completes the Swarmops Red feature set.

This commit is contained in:
Rick Falkvinge
2014-12-28 19:50:14 +01:00
parent 4627715cdd
commit 47157ca7ee
7 changed files with 216 additions and 36 deletions
+13
View File
@@ -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));
}
}
}
+19 -17
View File
@@ -327,11 +327,26 @@ namespace Swarmops.Logic.Financial
}
public Dictionary<int, Int64> GetRecalculationBase()
{
Dictionary<int, Int64> currentTransaction = new Dictionary<int, Int64>();
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<int, Int64> 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<int, Int64> currentTransaction = new Dictionary<int, Int64>();
foreach (FinancialTransactionRow row in Rows)
{
if (!currentTransaction.ContainsKey (row.FinancialAccountId))
{
currentTransaction[row.FinancialAccountId] = 0;
}
currentTransaction[row.FinancialAccountId] += row.AmountCents;
}
Dictionary<int, Int64> currentTransaction = GetRecalculationBase();
FinancialTransaction continuedTransaction = ContinuedTransaction;
@@ -395,9 +400,6 @@ namespace Swarmops.Logic.Financial
}
}
// TODO: Dimension 2
return changedTransaction;
}
+9 -5
View File
@@ -400,6 +400,7 @@
</data>
<data name="ViewOutstandingAccounts_GridHeaderAmount" xml:space="preserve">
<value>Amount</value>
<comment>keep short</comment>
</data>
<data name="ViewOutstandingAccounts_GridHeaderDescription" xml:space="preserve">
<value>Description</value>
@@ -469,14 +470,10 @@
<data name="AccountPlan_Edit_HeaderDailyOperations" xml:space="preserve">
<value>Daily Operations</value>
</data>
<data name="AccountPlan_Edit_InitialValue" xml:space="preserve">
<value>Initial Value</value>
<comment>For asset and liability accounts - the value going into Swarmops first ledger year</comment>
</data>
<data name="AccountPlan_Edit_Parent" xml:space="preserve">
<value>Parent account or group</value>
</data>
<data name="AccountPlan_ExpensesBudgetsAreNegaive" xml:space="preserve">
<data name="AccountPlan_ExpensesBudgetsAreNegative" xml:space="preserve">
<value>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.</value>
<comment>Displayed as a message box</comment>
</data>
@@ -558,4 +555,11 @@
<value>New Account</value>
<comment>the name of newly-created accounts, before rename</comment>
</data>
<data name="AccountPlan_DebtBalancesAreNegative" xml:space="preserve">
<value>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.</value>
</data>
<data name="AccountPlan_Edit_InitialBalance" xml:space="preserve">
<value>Initial Balance (inbound into {0}), {1}</value>
<comment>{0} is the first ledger year in Swarmops. {1} is the currency code.</comment>
</data>
</root>
@@ -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 (',');
+72 -5
View File
@@ -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" %>
<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderHead" Runat="Server">
<script src="/Scripts/jquery.switchButton.js" language="javascript" type="text/javascript"></script>
@@ -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('<asp:Literal ID="LiteralDebtBalancesAreNegative" runat="server" />');
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 @@
<div style="float:right;margin-top: 2px;margin-right: -5px"><img id="IconCloseEdit" src="/Images/Icons/iconshock-cross-16px.png" /></div><h2 id="HeaderModal"><asp:Literal ID="LiteralHeaderEditingAccount" runat="server"/></h2>
<div id="DivModalFields" class="entryFields"><input type="text" id="TextAccountName" />&nbsp;<br />
<Swarmops5:ComboBudgets ID="DropParents" runat="server" OnClientLoaded="onAccountTreeLoaded" OnClientSelect="onAccountTreeSelect" />&nbsp;<br/>
&nbsp;<br/>
<div id="DivEditProfitLossControls"><Swarmops5:ComboPeople ID="DropOwner" OnClientSelect="onOwnerChange" runat="server" />&nbsp;<br/>
<div id="DivEditProfitLossControls">&nbsp;<br/>
<Swarmops5:ComboPeople ID="DropOwner" OnClientSelect="onOwnerChange" runat="server" />&nbsp;<br/>
<input type="text" id="TextAccountBudget" style="text-align: right"/>&nbsp;<br/>
&nbsp;<br/>
<label for="CheckAccountActive"><asp:Literal ID="LiteralLabelActiveShort" runat="server"/></label><div class="CheckboxContainer"><input type="checkbox" rel="Active" class="EditCheck" id="CheckAccountActive"/></div><br/>
<label for="CheckAccountExpensable"><asp:Literal ID="LiteralLabelExpensableShort" runat="server"/></label><div class="CheckboxContainer"><input type="checkbox" rel="Expensable" class="EditCheck" id="CheckAccountExpensable"/></div><br/>
<label for="CheckAccountAdministrative"><asp:Literal ID="LiteralLabelAdministrativeShort" runat="server"/></label><div class="CheckboxContainer"><input type="checkbox" rel="Administrative" class="EditCheck" id="CheckAccountAdministrative"/></div>
&nbsp;<br/></div>
<div id="DivEditAssetControls"><asp:DropDownList runat="server" ID="DropAccountUploadFormats"/>
<input type="text" id="TextAutomationPaymentTag" readonly="readonly"/>&nbsp;<br/></div>
<div id="DivEditInitControls"><Swarmops5:TextCurrency ID="CurrencyInitialBalance" runat="server" />&nbsp;<br/></div>
<div id="DivEditAssetControls">
&nbsp;<br/>
<asp:DropDownList runat="server" ID="DropAccountUploadFormats"/>
<input type="text" id="TextAutomationPaymentTag" readonly="readonly"/>&nbsp;<br/>
</div>
</div>
<div class="entryLabels"><asp:Literal ID="LiteralLabelAccountName" runat="server"/><br/>
<asp:Literal ID="LiteralLabelParent" runat="server"/><br/>
@@ -512,6 +578,7 @@
<asp:Literal ID="LiteralLabelActiveLong" runat="server"/><br/>
<asp:Literal ID="LiteralLabelExpensableLong" runat="server"/><br/>
<asp:Literal ID="LiteralLabelAdministrativeLong" runat="server"/><br/></div>
<div id="DivEditInitLabels"><asp:Literal ID="LiteralLabelInitialAmount" runat="server"/></div>
<div id="DivEditAssetLabels"><h2><asp:Literal ID="LiteralLabelHeaderAutomation" runat="server"/></h2>
<asp:Literal ID="LiteralLabelFileUploadProfile" runat="server"/><br/>
<span id="SpanUploadParameterName">Upload parameter, if any</span></div>
+95 -1
View File
@@ -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<int, Int64> 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;
@@ -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();