DbVersion 0010; exchange rates

This commit is contained in:
Rick Falkvinge
2014-12-28 03:05:51 +01:00
parent a3d23af1f0
commit a944b1c8f4
7 changed files with 259 additions and 23 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ namespace Swarmops.Database
/// </summary>
public static int DbVersionExpected
{
get { return 9; }
get { return 10; }
}
-20
View File
@@ -121,26 +121,6 @@ namespace Swarmops.Database
}
}
public int SetCurrencyExchangeRate (DateTime date, int currencyId, double eur100)
{
DateTime now = DateTime.Now;
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand ("SetCurrencyExchangeRate", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName (command, "currencyId", currencyId);
AddParameterWithName (command, "date", date.Date);
AddParameterWithName (command, "eur100", eur100);
return Convert.ToInt32 (command.ExecuteScalar());
}
}
/*
public void SetSalaryNetPaid(int salaryId, bool netPaid)
+226
View File
@@ -0,0 +1,226 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Swarmops.Basic.Types;
namespace Swarmops.Database
{
public partial class SwarmDb
{
#region Field reading code
private const string exchangeRateSnapshotFieldSequence =
" CurrencyId,Code,Name,Sign " + // 0-3
"FROM Currencies ";
private const string exchangeRateDatapointSequence =
" ...";
/*
private static BasicCurrency ReadCurrencyFromDataReader(IDataRecord reader)
{
int currencyId = reader.GetInt32(0);
string code = reader.GetString(1);
string name = reader.GetString(2);
string sign = reader.GetString(3);
return new BasicCurrency(currencyId, code, name, sign);
}*/
#endregion
#region Record reading - SELECT statements
/*
public BasicCurrency GetCurrency(int currencyId)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command =
GetDbCommand(
"SELECT" + currencyFieldSequence + "WHERE CurrencyId=" + currencyId + ";", connection);
using (DbDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
return ReadCurrencyFromDataReader(reader);
}
throw new ArgumentException("Unknown Currency Id: " + currencyId);
}
}
}
public BasicCurrency GetCurrency(string currencyCode)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command =
GetDbCommand(
"SELECT" + currencyFieldSequence + "WHERE Code='" +
currencyCode.Replace("'", "''").ToUpperInvariant() + "';", connection);
using (DbDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
return ReadCurrencyFromDataReader(reader);
}
throw new ArgumentException("Unknown Currency Code: " + currencyCode);
}
}
}
public BasicCurrency[] GetCurrencies(params object[] conditions)
{
List<BasicCurrency> result = new List<BasicCurrency>();
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command =
GetDbCommand(
"SELECT" + currencyFieldSequence + ConstructWhereClause("Currencies", conditions), connection);
using (DbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
result.Add(ReadCurrencyFromDataReader(reader));
}
return result.ToArray();
}
}
}*/
#endregion
#region Creation and manipulation - stored procedures
public int CreateExchangeRateSnapshot()
{
DateTime now = DateTime.UtcNow;
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand("CreateExchangeRateSnapshot", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName(command, "dateTimeUtc", now);
return Convert.ToInt32(command.ExecuteScalar());
}
}
public int CreateExchangeRateDatapoint (int exchangeRateSnapshotId, int currencyAId, int currencyBId, double aPerB)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand("CreateExchangeRateDatapoint", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName (command, "exchangeRateSnapshotId", exchangeRateSnapshotId);
AddParameterWithName (command, "currencyAId", currencyAId);
AddParameterWithName (command, "currencyBId", currencyBId);
AddParameterWithName (command, "aPerB", aPerB);
return Convert.ToInt32(command.ExecuteScalar());
}
}
/*
public void SetSalaryNetPaid(int salaryId, bool netPaid)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand("SetSalaryNetPaid", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName(command, "salaryId", salaryId);
AddParameterWithName(command, "netPaid", netPaid);
command.ExecuteNonQuery();
// "Open" is set in the stored procedure to "NOT (TaxPaid AND NetPaid)".
// So if both are paid, Open is set to false.
}
}
public void SetSalaryNetSalary(int salaryId, double netSalary)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand("SetSalaryNetSalary", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName(command, "salaryId", salaryId);
AddParameterWithName(command, "netSalary", netSalary);
command.ExecuteNonQuery();
// "Open" is set in the stored procedure to "NOT (TaxPaid AND NetPaid)".
// So if both are paid, Open is set to false.
}
}
public void SetSalaryTaxPaid(int salaryId, bool taxPaid)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand("SetSalaryTaxPaid", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName(command, "salaryId", salaryId);
AddParameterWithName(command, "taxPaid", taxPaid);
command.ExecuteNonQuery();
// "Open" is set in the stored procedure to "NOT (TaxPaid AND NetPaid)".
// So if both are paid, Open is set to false.
}
}
public void SetSalaryAttested(int salaryId, bool attested)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand("SetSalaryAttested", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName(command, "salaryId", salaryId);
AddParameterWithName(command, "attested", attested);
command.ExecuteNonQuery();
}
}
*/
#endregion
}
}
+1
View File
@@ -58,6 +58,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Configuration.cs" />
<Compile Include="Database-ExchangeRates.cs" />
<Compile Include="Database-GeographyUpdates.cs" />
<Compile Include="Database-CashAdvances.cs" />
<Compile Include="Database-Currencies.cs" />
+3 -2
View File
@@ -1,4 +1,4 @@
0001 - Initial baseline. [Rick Falkvinge]
0001 - Initial baseline, in 'initialize.sql' [Rick Falkvinge]
0002 - Added functions for account plan maintenance and back/forward links to FinancialAccounts. [Rick Falkvinge]
0003 - Added 'Year' field for salary tax data. [Rick Falkvinge]
0004 - Added BankProfile tables and stored procs to insert/update. [Rick Falkvinge]
@@ -6,4 +6,5 @@
0006 - Added Memberships.LocalNumber and .FamilyNumber; added CurrencyExchangeRate tables/procs. [Rick Falkvinge]
0007 - NL update; added Lat/Long to PostalCodes and to People, indexed [RF]
0008 - GeographyUpdate updates ahead of deploying: added country, Guid [RF]
0009 - Fixed SalaryTaxLevels and CreateExchangeRateDataPoint [RF]
0009 - Fixed SalaryTaxLevels and CreateExchangeRateDataPoint [RF]
0010 - Fix for CurrencyExchangeRateSnapshotData [RF]
+6
View File
@@ -0,0 +1,6 @@
ALTER TABLE `CurrencyExchangeRateSnapshotData`
DROP PRIMARY KEY,
ADD PRIMARY KEY (`CurrencyExchangeRateSnapshotId`, `CurrencyBId`, `CurrencyAId`),
ADD INDEX `Index_SnapshotId` (`CurrencyExchangeRateSnapshotId` ASC),
ADD INDEX `Index_CurrencyA` (`CurrencyAId` ASC),
ADD INDEX `Index_CurrencyB` (`CurrencyBId` ASC)
+22
View File
@@ -9,6 +9,7 @@ using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Xml.Serialization;
using Swarmops.Basic.Types.Financial;
using Swarmops.Database;
using Swarmops.Logic.Support;
namespace Swarmops.Logic.Financial
@@ -52,9 +53,14 @@ namespace Swarmops.Logic.Financial
Regex regex = new Regex (regexPattern);
Match match = regex.Match (rateDataRaw);
int exchangeRateSnapshotId = 0;
int bitcoinId = GetOrCreateCurrency ("BTC", "Bitcoin");
if (match.Success)
{
// We have at least one match, so prepare a new ExchangeRate snapshot
exchangeRateSnapshotId = SwarmDb.GetDatabaseForWriting().CreateExchangeRateSnapshot();
}
while (match.Success)
@@ -64,6 +70,10 @@ namespace Swarmops.Logic.Financial
double btcRate = Double.Parse (match.Groups[3].Value, NumberStyles.AllowDecimalPoint,
CultureInfo.InvariantCulture);
int currencyId = GetOrCreateCurrency (currencyCode, currencyName);
SwarmDb.GetDatabaseForWriting()
.CreateExchangeRateDatapoint (exchangeRateSnapshotId, currencyId, bitcoinId, btcRate);
match = match.NextMatch();
}
@@ -73,6 +83,18 @@ namespace Swarmops.Logic.Financial
}
}
private static int GetOrCreateCurrency (string currencyCode, string currencyName)
{
try
{
return Currency.FromCode (currencyCode).Identity;
}
catch (ArgumentException)
{
return Currency.Create (currencyCode, currencyName, string.Empty).Identity;
}
}
}
[Serializable]