Added currency rate conversions

This commit is contained in:
Rick Falkvinge
2015-02-06 18:05:11 +01:00
parent fca7af33ae
commit 15aee8f813
7 changed files with 97 additions and 7 deletions
+1 -1
View File
@@ -229,7 +229,7 @@ namespace Swarmops.Database
}
private string SqlSanitize (string input)
private string SqlSanitize (string input) // TODO: Replace with parameterized queries everywhere.
{
string[] forbiddenArray =
{
+1
View File
@@ -100,6 +100,7 @@ namespace Swarmops.Database
#endregion
#region Creation and manipulation - stored procedures
public int CreateCurrency (string code, string name, string sign)
+20 -6
View File
@@ -31,28 +31,42 @@ namespace Swarmops.Database
#endregion
#region Record reading - SELECT statements
/*
public BasicCurrency GetCurrency(int currencyId)
public double GetCurrencyExchangeRate (int fromCurrencyId, int toCurrencyId, DateTime valuationDateTime)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command =
GetDbCommand(
"SELECT" + currencyFieldSequence + "WHERE CurrencyId=" + currencyId + ";", connection);
GetDbCommand(String.Format (
"SELECT CurrencyExchangeRateSnapshotData.CurrencyAId AS CurrencyAId, " +
" CurrencyExchangeRateSnapshotData.CurrencyBId AS CurrencyBId, " +
" CurrencyExchangeRateSnapshotData.APerB AS Rate, " +
" CurrencyExchangeRateSnapshots.DateTime AS RateTime " +
" FROM CurrencyExchangeRateSnapshotData " +
" JOIN CurrencyExchangeRateSnapshots USING (CurrencyExchangeRateSnapshotId)" +
" WHERE CurrencyAId = {0} AND CurrencyBId = {1} AND CurrencyExchangeRateSnapshots.DateTime < @valuationDateTime " +
" ORDER BY CurrencyExchangeRateSnapshots.DateTime DESC " +
" LIMIT 1;", fromCurrencyId, toCurrencyId), connection);
AddParameterWithName (command, "valuationDateTime", valuationDateTime);
using (DbDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
return ReadCurrencyFromDataReader(reader);
DateTime testDate = reader.GetDateTime (3);
return reader.GetDouble(2);
}
throw new ArgumentException("Unknown Currency Id: " + currencyId);
throw new ArgumentException("Unknown Exchange Rate");
}
}
}
/*
public BasicCurrency GetCurrency(string currencyCode)
{
+46
View File
@@ -1,3 +1,5 @@
using System;
using NBitcoin;
using Swarmops.Basic.Types;
using Swarmops.Database;
@@ -23,6 +25,50 @@ namespace Swarmops.Logic.Financial
}
}
public bool IsBitcoin
{
get { return (base.Code == "BTC"); }
}
public static Currency Bitcoin
{
get { return Currency.FromCode ("BTC"); }
}
public double GetConversionRate (Currency otherCurrency)
{
return GetConversionRate (otherCurrency, DateTime.UtcNow);
}
public double GetConversionRate (Currency otherCurrency, DateTime valuationDateTime)
{
if (this.IsBitcoin)
{
return SwarmDb.GetDatabaseForReading()
.GetCurrencyExchangeRate (otherCurrency.Identity, this.Identity, valuationDateTime);
}
else if (otherCurrency.IsBitcoin)
{
return 1.0 / SwarmDb.GetDatabaseForReading()
.GetCurrencyExchangeRate (otherCurrency.Identity, this.Identity, valuationDateTime);
}
else
{
// Neither is bitcoin, so go via bitcoin
int bitcoinCurrencyId = Currency.Bitcoin.Identity;
double thisPerBitcoin = SwarmDb.GetDatabaseForReading()
.GetCurrencyExchangeRate (this.Identity, bitcoinCurrencyId, valuationDateTime);
double otherPerBitcoin = SwarmDb.GetDatabaseForReading()
.GetCurrencyExchangeRate (otherCurrency.Identity, bitcoinCurrencyId, valuationDateTime);
return otherPerBitcoin/thisPerBitcoin; // returns other-per-this, allowing multiplication with exchange rate
}
}
public static Currency FromBasic (BasicCurrency basic)
{
return new Currency (basic);
+25
View File
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Swarmops.Logic.Financial
{
public class Money
{
public Currency Currency { get; set; }
public Int64 Cents { get; set; }
public DateTime ValuationDateTime { get; set; }
public Money ToCurrency (Currency newCurrency)
{
double conversionRate = this.Currency.GetConversionRate (newCurrency, this.ValuationDateTime);
return new Money {
Currency = newCurrency,
Cents = (Int64) (this.Cents * conversionRate),
ValuationDateTime = this.ValuationDateTime };
}
}
}
+1
View File
@@ -138,6 +138,7 @@
<Compile Include="Financial\FinancialTransactionTagTypes.cs" />
<Compile Include="Financial\ImportedBankData.cs" />
<Compile Include="Financial\ISummable.cs" />
<Compile Include="Financial\Money.cs" />
<Compile Include="Financial\StockBankDataReaders.cs" />
<Compile Include="Financial\YearlyReport.cs" />
<Compile Include="Security\Accesses.cs" />
+3
View File
@@ -2,11 +2,13 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using Resources;
using Swarmops.Logic.Financial;
using Swarmops.Logic.Security;
using Swarmops.Logic.Structure;
using Swarmops.Logic.Support;
@@ -44,6 +46,7 @@ namespace Swarmops
_currentOrganization = Organization.Sandbox;
}
}
}
protected void Page_Load (object sender, EventArgs e)