diff --git a/Basic/Basic.csproj b/Basic/Basic.csproj index 96e78554..7c76b7f8 100644 --- a/Basic/Basic.csproj +++ b/Basic/Basic.csproj @@ -68,6 +68,7 @@ + diff --git a/Basic/Types/Financial/BasicExchangeRateSnapshot.cs b/Basic/Types/Financial/BasicExchangeRateSnapshot.cs new file mode 100644 index 00000000..a8e7ca38 --- /dev/null +++ b/Basic/Types/Financial/BasicExchangeRateSnapshot.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Swarmops.Basic.Interfaces; + +namespace Swarmops.Basic.Types.Financial +{ + [Serializable] + public class BasicExchangeRateSnapshot: IHasIdentity + { + [Obsolete("Do not call the default constructor directly. It is reserved for serialization.", true)] + public BasicExchangeRateSnapshot() + { + // do not call + } + + public BasicExchangeRateSnapshot (int identity, DateTime dateTime) + { + this.ExchangeRateSnapshotId = identity; + this.DateTime = dateTime; + } + + public BasicExchangeRateSnapshot (BasicExchangeRateSnapshot original) : + this (original.Identity, original.DateTime) + { + // empty copy ctor + } + + public int ExchangeRateSnapshotId { get; private set; } + public DateTime DateTime { get; private set; } + public List Datapoints { get; protected set; } + + public int Identity { get { return ExchangeRateSnapshotId; }} + } + + [Serializable] + public class BasicExchangeRateSnapshotDatapoint + { + public string CurrencyCodeA { get; set; } + public string CurrencyCodeB { get; set; } + public double APerB { get; set; } + } +} diff --git a/Database/Configuration.cs b/Database/Configuration.cs index 820ce141..c6d001d9 100644 --- a/Database/Configuration.cs +++ b/Database/Configuration.cs @@ -14,7 +14,7 @@ namespace Swarmops.Database /// public static int DbVersionExpected { - get { return 8; } + get { return 9; } } diff --git a/Database/Schemata/revisions.txt b/Database/Schemata/revisions.txt index b0a3ef81..9b8a0953 100644 --- a/Database/Schemata/revisions.txt +++ b/Database/Schemata/revisions.txt @@ -6,3 +6,4 @@ 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] \ No newline at end of file diff --git a/Database/Schemata/upgrade-0009.sql b/Database/Schemata/upgrade-0009.sql new file mode 100644 index 00000000..de993b1a --- /dev/null +++ b/Database/Schemata/upgrade-0009.sql @@ -0,0 +1,26 @@ +ALTER TABLE `SalaryTaxLevels` +DROP PRIMARY KEY, +ADD PRIMARY KEY (`TaxLevelId`, `BracketLow`, `CountryId`, `Year`) + + +# + + +DROP PROCEDURE `InsertExchangeRateSnapshotData` + + +# + + +CREATE `CreateExchangeRateDatapoint`( + IN currencyExchangeRateSnapshotId INTEGER, + IN currencyAId INTEGER, + IN currencyBId INTEGER, + IN aPerB DOUBLE +) +BEGIN + + INSERT INTO CurrencyExchangeRateSnapshotData (CurrencyExchangeRateSnapshotId,CurrencyAId,CurrencyBId,APerB) + VALUES (currencyExchangeRateSnapshotId,currencyAId,currencyBId,aPerB); + +END \ No newline at end of file diff --git a/Logic/Financial/ExchangeRateSnapshot.cs b/Logic/Financial/ExchangeRateSnapshot.cs new file mode 100644 index 00000000..b4d3f154 --- /dev/null +++ b/Logic/Financial/ExchangeRateSnapshot.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Net; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using System.Web.Script.Serialization; +using System.Xml.Serialization; +using Swarmops.Basic.Types.Financial; + +namespace Swarmops.Logic.Financial +{ + [Serializable] + public class ExchangeRateSnapshot: BasicExchangeRateSnapshot + { + [Obsolete ("Do not call this ctor directly. Intended for serialization only.", true)] + public ExchangeRateSnapshot() + { + // do not call + } + + private ExchangeRateSnapshot (BasicExchangeRateSnapshot basic) + : base (basic) + { + // private ctor + + // TODO: Verify that data is in, or load it + } + + public static ExchangeRateSnapshot FromBasic (BasicExchangeRateSnapshot basic) + { + return new ExchangeRateSnapshot (basic); + } + + + public static void Create() + { + using(WebClient client = new WebClient()) + { + client.Encoding = Encoding.UTF8; + string rateDataRaw = client.DownloadString("https://bitpay.com/api/rates"); + + // BitPay doesn't provide valid JSON - the rate field isn't enclosed in quotes - so we can't use JSON Deserialization; we'll + // have to use Regex matching instead. + + string regexPattern = @"\{\""code\"":\""([A-Z]+)\"",\""name\"":\""([^\""]+)\"",\""rate\"":([0-9\.]+)\}"; + Regex regex = new Regex (regexPattern); + Match match = regex.Match (rateDataRaw); + + if (match.Success) + { + // We have at least one match, so prepare a new ExchangeRate snapshot + } + + while (match.Success) + { + string currencyCode = match.Groups[0].Value; + string currencyName = match.Groups[1].Value; + double btcRate = Double.Parse (match.Groups[2].Value, NumberStyles.AllowDecimalPoint, + CultureInfo.InvariantCulture); + + + match = match.NextMatch(); + } + + JavaScriptSerializer serializer = new JavaScriptSerializer(); + BitpayRateDatapoint[] rates = (BitpayRateDatapoint[]) serializer.Deserialize (rateDataRaw); + } + } + + } + + [Serializable] + public class BitpayRateDatapoint + { + string code { get; set; } + string name { get; set; } + string rate { get; set; } + } +} diff --git a/Logic/Financial/ExchangeRateSnapshots.cs b/Logic/Financial/ExchangeRateSnapshots.cs new file mode 100644 index 00000000..5d5f4b24 --- /dev/null +++ b/Logic/Financial/ExchangeRateSnapshots.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Swarmops.Basic.Types.Financial; +using Swarmops.Logic.Support; + +namespace Swarmops.Logic.Financial +{ + public class ExchangeRateSnapshots: PluralBase + { + // the templated base takes care of most stuff + } +} diff --git a/Logic/Logic.csproj b/Logic/Logic.csproj index 3ec05bf3..c22c8963 100644 --- a/Logic/Logic.csproj +++ b/Logic/Logic.csproj @@ -67,6 +67,7 @@ + @@ -119,6 +120,8 @@ + + diff --git a/Site5/Automation/GetFinancialData.asmx b/Site5/Automation/GetFinancialData.asmx new file mode 100644 index 00000000..4e4e445d --- /dev/null +++ b/Site5/Automation/GetFinancialData.asmx @@ -0,0 +1,44 @@ +<%@ WebService Language="C#" Class="GetFinancialData" %> + +using System; +using System.Web; +using System.Web.Services; +using System.Web.Services.Protocols; +using Swarmops.Logic.Financial; + +[WebService(Namespace = "http://swarmops.com/")] +[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] +// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. +// [System.Web.Script.Services.ScriptService] +public class GetFinancialData : System.Web.Services.WebService { + + /* + [WebMethod] + public ExchangeRateSnapshots GetExhangeRateDataSince(DateTime dateTimeSince) + { + return ExchangeRateSnapshots.GetSince (dateTimeSince); + } + + [WebMethod] + public double GetExchangeRate (string currencyCodeA, string currencyCodeB) + { + return ExchangeRateSnapshot.Latest.GetExchangeRate (currencyCodeA, currencyCodeB); + } + + [WebMethod] + public double GetExchangeRate (string currencyCodeA, string currencyCodeB, DateTime onDate) + { + return ExchangeRateSnapshot.FromDate (onDate).GetExchangeRate (currencyCodeA, currencyCodeB); + }*/ + + [WebMethod] + public Currency GetCurrency (string currencyCode) + { + return Currency.FromCode (currencyCode); + } + + public Currencies GetCurrencies() + { + return Currencies.GetAll(); + } +} \ No newline at end of file diff --git a/Site5/Automation/GetGeographyData.asmx b/Site5/Automation/GetGeographyData.asmx index 938aed00..f08466b8 100644 --- a/Site5/Automation/GetGeographyData.asmx +++ b/Site5/Automation/GetGeographyData.asmx @@ -1,5 +1,6 @@ <%@ WebService Language="C#" Class="GetGeographyData" %> +using System; using System.Web.Services; using Swarmops.Logic.Structure; @@ -44,4 +45,16 @@ public class GetGeographyData : System.Web.Services.WebService { return Country.FromCode(countryCode).Geography; } + + [WebMethod] + public GeographyUpdates GetGeographyUpdatesSince (DateTime dateTime) + { + return GeographyUpdates.GetCreatedSince (dateTime); + } + + [WebMethod] + public GeographyUpdates GetGeographyUpdates() + { + return GeographyUpdates.GetCreatedSince (new DateTime (1900, 1, 1)); + } } \ No newline at end of file diff --git a/Site5/Default.aspx.cs b/Site5/Default.aspx.cs index 18921d9d..c0577f4c 100644 --- a/Site5/Default.aspx.cs +++ b/Site5/Default.aspx.cs @@ -1,5 +1,6 @@ using System; using Resources; +using Swarmops.Logic.Financial; public partial class Default : PageV5Base { @@ -14,5 +15,7 @@ public partial class Default : PageV5Base InfoBoxLiteral = "This is a Dashboard placeholder. It will contain a snapshot of the state of things as soon as the basic functions are re-implemented in the new interface."; + + ExchangeRateSnapshot.Create(); } } \ No newline at end of file