Some work on nonpresentation currency in database

This commit is contained in:
Rick Falkvinge
2018-05-04 15:10:26 +02:00
parent 10afc88ddf
commit b70e727535
3 changed files with 122 additions and 18 deletions
+61 -16
View File
@@ -5,6 +5,7 @@ using System.Data.Common;
using System.Globalization;
using System.Xml;
using Swarmops.Basic.Types.Financial;
using Swarmops.Common;
// This is the first part of Database to fully use MySql.
@@ -1195,10 +1196,13 @@ namespace Swarmops.Database
public Int64 GetFinancialAccountForeignCentsBalance (int financialAccountId)
public Int64 GetFinancialAccountForeignCentsBalance(int financialAccountId)
{
// This gets the balance across the entire ledger history, so it's only for asset or liability accounts
// (not for P&L).
// This gets the non-presentation balance across the entire ledger history, so it's only
// for asset or liability accounts (not for P&L).
// This function joins one less table than the ForeignDeltaCents, so it's kept despite being redundant
// because it's far more optimized for this special case.
using (DbConnection connection = GetMySqlDbConnection())
{
@@ -1208,25 +1212,66 @@ namespace Swarmops.Database
GetDbCommand(
"SELECT SUM(NativeAmountCents) FROM FinancialTransactionRowsNativeCurrency " +
" JOIN FinancialTransactionRows USING (FinancialTransactionRowId) " +
" WHERE FinancialAccountId =" + financialAccountId + ";", connection))
using (DbDataReader reader = command.ExecuteReader())
" WHERE FinancialAccountId=@financialAccountId;", connection))
{
if (reader.Read())
{
if (!reader.IsDBNull(0))
{
return reader.GetInt64(0);
}
}
AddParameterWithName(command, "financialAccountId", financialAccountId);
return 0; // zero balance, apparently.
using (DbDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
if (!reader.IsDBNull(0))
{
return reader.GetInt64(0);
}
}
return 0; // zero balance, apparently.
}
}
}
}
public Int64 GetFinancialAccountForeignDeltaCents(int financialAccountId, DateTime lowerBoundInclusive, DateTime upperBoundExclusive)
{
// This gets the balance across the specified part of ledger history for non-presentation currency.
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
using (DbCommand command =
GetDbCommand(
"SELECT SUM(NativeAmountCents) FROM FinancialTransactionRowsNativeCurrency " +
" JOIN FinancialTransactionRows USING (FinancialTransactionRowId) " +
" JOIN FinancialTransactions USING (FinancialTransactionId) " +
" WHERE FinancialTransactionRows.FinancialAccountId=@financialAccountId " +
" AND FinancialTransactions.DateTime >= @lowerBoundInclusive " +
" AND FinancialTransactions.DateTime < @upperBoundExclusive " +
";", connection))
{
AddParameterWithName(command, "financialAccountId", financialAccountId);
AddParameterWithName(command, "lowerBoundInclusive", lowerBoundInclusive);
AddParameterWithName(command, "upperBoundExclusive", upperBoundExclusive);
using (DbDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
if (!reader.IsDBNull(0))
{
return reader.GetInt64(0);
}
}
return 0; // zero delta for this time span: no rows returned at all.
}
}
}
}
public DateTime GetFinancialAccountFirstTransactionDate(int financialAccountId)
+59 -2
View File
@@ -143,6 +143,19 @@ namespace Swarmops.Logic.Financial
}
public Money GetForeignBalanceDeltaCents(DateTime lowerBoundinclusive, DateTime upperBoundExclusive)
{
if (this.ForeignCurrency == null)
{
throw new InvalidOperationException("Account does not have a non-presentation currency");
}
Int64 foreignCents =
SwarmDb.GetDatabaseForReading().GetFinancialAccountForeignDeltaCents(this.Identity, lowerBoundinclusive, upperBoundExclusive);
return new Money(foreignCents, ForeignCurrency); // default to now
}
public string BitcoinAddress
{
get
@@ -200,10 +213,24 @@ namespace Swarmops.Logic.Financial
Int64 currentInitialBalanceCents = this.GetDeltaCents(new DateTime(1900, 1, 1),
new DateTime(this.Organization.FirstFiscalYear, 1, 1));
// Test: Assert that the currency set equals the currency for the account
if (this.ForeignCurrency != null)
{
}
else
{
if (value.Currency.Identity != this.Organization.Currency.Identity)
{
throw new ArgumentException("Currency mismatch");
}
}
// Find or create "Initial Balances" transaction
FinancialAccountRows testRows = FinancialAccountRows.ForOrganization(this.Organization,
new DateTime(1900, 1, 1), new DateTime(this.Organization.FirstFiscalYear, 1, 1));
Constants.DateTimeLow, new DateTime(this.Organization.FirstFiscalYear, 1, 1));
FinancialTransaction initialBalancesTransaction = null;
@@ -227,7 +254,7 @@ namespace Swarmops.Logic.Financial
// reconstructing the R-value now that we have the valuation date, which the setter didn't
Money setValue = new Money(value.Cents, value.Currency, initialBalancesTransaction.DateTime);
Money setValue = new Money(value.Cents, value.Currency, initialBalancesTransaction.DateTime);
Int64 deltaCents = setValue.ToCurrency(Organization.Currency).Cents - currentInitialBalanceCents;
Dictionary<int, Int64> recalcBase = initialBalancesTransaction.GetRecalculationBase();
@@ -245,6 +272,36 @@ namespace Swarmops.Logic.Financial
recalcBase[this.Identity] += deltaCents;
recalcBase[equityAccountId] -= deltaCents;
initialBalancesTransaction.RecalculateTransaction(recalcBase, null);
if (this.ForeignCurrency != null)
{
// We need to also set the initial balance in foreign currency.
// First, get the current initial balance in account nonpresentation currency:
Money currentInitialBalanceCents = ForeignBalanceTotalCents
// Find the newly added transaction row
// (WARNING / RACE: This may return an incorrect row, if there's a lag betweeen
// reading and writing of the database instances, such as a replication scenario
// with one write and many read dbs)
testRows = FinancialAccountRows.ForOrganization(this.Organization,
Constants.DateTimeLow, new DateTime(this.Organization.FirstFiscalYear, 1, 1));
FinancialAccountRow lastFoundRow = null;
foreach (FinancialAccountRow row in testRows)
{
if (row.Transaction.Description == initialBalanceTransactionTitle)
{
lastFoundRow = row;
}
}
// Amend the last found row, which is assumed to be a result of the RecalculateTx
// call above, to reflect the new initial balance in foreign currency.
}
}
catch (Exception weirdException)
{
@@ -1031,6 +1031,8 @@ namespace Swarmops.Site.Pages.Ledgers
public DateTime EarliestTransaction;
public DateTime LatestTransaction;
public int TransactionsImported;
public string InitialBalanceCents;
public string InitialBalanceCurrency;
public ImportResults()
{