From 190de279a4cc8a8c232da18be0b22c34830ea53c Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Thu, 5 Feb 2015 19:13:28 +0100 Subject: [PATCH 01/12] Debugging Mono font metrics, which obviously differ from Microsoft's .Net implementation. GAAAAAAH --- .../TaxForms/SE/MonthlyTaxForm-SE.aspx.cs | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs b/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs index a78d4faa..e1cd1c30 100644 --- a/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs +++ b/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs @@ -38,17 +38,35 @@ namespace Swarmops.Plugins.Stock.TaxForms formVersion = 2010; } + Dictionary> coord = + new Dictionary>(); + + // Coordinates for various elements on the form versions + + coord[2010] = new Dictionary(); + coord[2015] = new Dictionary(); + + Image form = Image.FromFile (MapPath (".") + "/MonthlyTaxForm-SE-" + formVersion + ".png"); // the "." says "in same folder as this file" CultureInfo swedishCulture = CultureInfo.CreateSpecificCulture ("sv-SE"); monthString = new DateTime(year, month, 1).ToString("MMMM yyyy", swedishCulture); + string orgNumber = "xxxxxx-xxxx"; string sansFontName = "Liberation Sans"; string courierFontName = "Liberation Mono"; + int smallSize = 18; + int regularSize = 24; + int handWriteSize = 48; + if (Debugger.IsAttached) { sansFontName = "Arial"; // yeahyeah... courierFontName = "Courier New"; + orgNumber = "DEBUGGER ATTACHED"; + smallSize = 24; + regularSize = 30; + handWriteSize = 64; } using (Graphics graphics = Graphics.FromImage (form)) @@ -56,7 +74,7 @@ namespace Swarmops.Plugins.Stock.TaxForms StringFormat rightAlign = new StringFormat(); rightAlign.Alignment = StringAlignment.Far; - Font fontHandwriting = new Font (courierFontName, 64, FontStyle.Bold); + Font fontHandwriting = new Font (courierFontName, 64, FontStyle.Regular); Font fontPreprinted = new Font (courierFontName, 30, FontStyle.Bold); Font fontPreprintedSmall = new Font (courierFontName, 24, FontStyle.Bold); Font fontPreprintedSans = new Font (sansFontName, 30, FontStyle.Bold); @@ -78,7 +96,7 @@ namespace Swarmops.Plugins.Stock.TaxForms graphics.DrawString (monthString, fontPreprinted, brushPreprinted, 620, 1460); graphics.DrawString (String.Format ("{0,4}-{1:D2}-{2:D2}", year, month + 1, 12), fontPreprinted, brushPreprinted, 820, 160); - graphics.DrawString ("xxxxxx-xxxx", fontPreprinted, brushPreprinted, 1110, 160); + graphics.DrawString (orgNumber, fontPreprinted, brushPreprinted, 1110, 160); } else // the latest revision of the tax form { @@ -86,7 +104,7 @@ namespace Swarmops.Plugins.Stock.TaxForms graphics.DrawString (monthString, fontPreprintedSans, brushPreprinted, 610, 1455); graphics.DrawString (String.Format ("{0,4}-{1:D2}-{2:D2}", year, month + 1, 12), fontPreprinted, brushPreprinted, 820, 160); - graphics.DrawString ("xxxxxx-xxxx", fontPreprinted, brushPreprinted, 1110, 160); + graphics.DrawString (orgNumber, fontPreprinted, brushPreprinted, 1110, 160); } // Draw the years and tax rates and other on-form constants more discreetly @@ -152,6 +170,7 @@ namespace Swarmops.Plugins.Stock.TaxForms { Salary = new double[3]; TaxAdditive = new double[3]; + TaxTotal = 99999; // Prime with something for font metrics debugging } public double[] Salary; @@ -195,5 +214,18 @@ namespace Swarmops.Plugins.Stock.TaxForms return result; } + + private enum GraphicsElement + { + Unknown = 0, + LeftColumnX, + RightColumnX, + AgeBracketMainY, + AgeBracketDistY, + AdditiveTaxTotalY, + SummaryTopLineY, + SummaryBottomLineY, + SummaryTotalY + } } } \ No newline at end of file From 881c3b25e305fafd307f0ba2f65f37870ab0ec80 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Thu, 5 Feb 2015 19:16:39 +0100 Subject: [PATCH 02/12] More debugging --- Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs b/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs index e1cd1c30..ca311c4b 100644 --- a/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs +++ b/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs @@ -74,10 +74,10 @@ namespace Swarmops.Plugins.Stock.TaxForms StringFormat rightAlign = new StringFormat(); rightAlign.Alignment = StringAlignment.Far; - Font fontHandwriting = new Font (courierFontName, 64, FontStyle.Regular); - Font fontPreprinted = new Font (courierFontName, 30, FontStyle.Bold); - Font fontPreprintedSmall = new Font (courierFontName, 24, FontStyle.Bold); - Font fontPreprintedSans = new Font (sansFontName, 30, FontStyle.Bold); + Font fontHandwriting = new Font (courierFontName, handWriteSize, FontStyle.Regular); + Font fontPreprinted = new Font (courierFontName, regularSize, FontStyle.Bold); + Font fontPreprintedSmall = new Font (courierFontName, smallSize, FontStyle.Bold); + Font fontPreprintedSans = new Font (sansFontName, regularSize, FontStyle.Bold); Brush brushHandwriting = Brushes.Blue; Brush brushPreprinted = Brushes.Red; From 929013de38b46096055144a0c4d41351f4054d22 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Thu, 5 Feb 2015 19:51:42 +0100 Subject: [PATCH 03/12] More debugging --- .../TaxForms/SE/MonthlyTaxForm-SE.aspx.cs | 69 ++++++++++++------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs b/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs index ca311c4b..58aa9b16 100644 --- a/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs +++ b/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs @@ -16,6 +16,9 @@ namespace Swarmops.Plugins.Stock.TaxForms { public partial class MonthlyTaxFormSE : DataV5Base { + private Font _fontHandwriting; + private Brush _brushHandwriting; + protected void Page_Load (object sender, EventArgs e) { string monthString = Request.QueryString["YearMonth"]; @@ -57,7 +60,7 @@ namespace Swarmops.Plugins.Stock.TaxForms string courierFontName = "Liberation Mono"; int smallSize = 18; int regularSize = 24; - int handWriteSize = 48; + int handWriteSize = 40; if (Debugger.IsAttached) { @@ -66,7 +69,7 @@ namespace Swarmops.Plugins.Stock.TaxForms orgNumber = "DEBUGGER ATTACHED"; smallSize = 24; regularSize = 30; - handWriteSize = 64; + handWriteSize = 52; } using (Graphics graphics = Graphics.FromImage (form)) @@ -74,12 +77,12 @@ namespace Swarmops.Plugins.Stock.TaxForms StringFormat rightAlign = new StringFormat(); rightAlign.Alignment = StringAlignment.Far; - Font fontHandwriting = new Font (courierFontName, handWriteSize, FontStyle.Regular); + _fontHandwriting = new Font (sansFontName, handWriteSize, FontStyle.Regular); Font fontPreprinted = new Font (courierFontName, regularSize, FontStyle.Bold); Font fontPreprintedSmall = new Font (courierFontName, smallSize, FontStyle.Bold); Font fontPreprintedSans = new Font (sansFontName, regularSize, FontStyle.Bold); - Brush brushHandwriting = Brushes.Blue; + _brushHandwriting = Brushes.Blue; Brush brushPreprinted = Brushes.Red; Brush brushPreprintedDiscreet = Brushes.DarkRed; @@ -110,49 +113,49 @@ namespace Swarmops.Plugins.Stock.TaxForms // Draw the years and tax rates and other on-form constants more discreetly graphics.DrawString (taxRates[2].ToString ("F2", swedishCulture), fontPreprintedSmall, - brushPreprintedDiscreet, 828, 731); + brushPreprintedDiscreet, 840, 729); graphics.DrawString (taxRates[1].ToString ("F2", swedishCulture), fontPreprintedSmall, - brushPreprintedDiscreet, 828, 798); + brushPreprintedDiscreet, 840, 796); graphics.DrawString (taxRates[0].ToString ("F2", swedishCulture), fontPreprintedSmall, - brushPreprintedDiscreet, 828, 865); + brushPreprintedDiscreet, 840, 863); graphics.DrawString (string.Format ("{0} - {1}", yearBreakpoints[1], yearBreakpoints[2] - 1), - fontPreprintedSmall, brushPreprintedDiscreet, 200, 731); + fontPreprintedSmall, brushPreprintedDiscreet, 190, 729); graphics.DrawString (string.Format ("{0} -", yearBreakpoints[2] - 1), - fontPreprintedSmall, brushPreprintedDiscreet, 200, 798); + fontPreprintedSmall, brushPreprintedDiscreet, 190, 796); graphics.DrawString (string.Format ("{0}", yearBreakpoints[1] - 1), - fontPreprintedSmall, brushPreprintedDiscreet, 252, 861); + fontPreprintedSmall, brushPreprintedDiscreet, 250, 859); // Draw the actual numbers - graphics.DrawString (data.SalaryTotal.ToString ("F0"), fontHandwriting, brushHandwriting, 792, 358, + graphics.DrawString (data.SalaryTotal.ToString ("F0"), _fontHandwriting, _brushHandwriting, 792, 358, rightAlign); // Salary total sub - graphics.DrawString (data.SalaryTotal.ToString ("F0"), fontHandwriting, brushHandwriting, 792, 558, + graphics.DrawString (data.SalaryTotal.ToString ("F0"), _fontHandwriting, _brushHandwriting, 792, 558, rightAlign); // Salary total total for (int ageBracket = 0; ageBracket <= 2; ageBracket++) { if (data.Salary[ageBracket] > 0.0) { - graphics.DrawString (data.Salary[ageBracket].ToString ("F0"), fontHandwriting, brushHandwriting, + graphics.DrawString (data.Salary[ageBracket].ToString ("F0"), _fontHandwriting, _brushHandwriting, 792, 692 + ageBracket*67, rightAlign); // Salary - graphics.DrawString (data.TaxAdditive[ageBracket].ToString ("F0"), fontHandwriting, - brushHandwriting, 1510, 692 + ageBracket*67, rightAlign); // Employer's fee + graphics.DrawString (data.TaxAdditive[ageBracket].ToString ("F0"), _fontHandwriting, + _brushHandwriting, 1510, 692 + ageBracket*67, rightAlign); // Employer's fee } } - graphics.DrawString(data.SalaryTotal.ToString("F0"), fontHandwriting, brushHandwriting, 792, 1524, + graphics.DrawString(data.SalaryTotal.ToString("F0"), _fontHandwriting, _brushHandwriting, 792, 1524, rightAlign); // Salary total again - graphics.DrawString (data.SalaryTotal.ToString ("F0"), fontHandwriting, brushHandwriting, 792, 1725, + graphics.DrawString (data.SalaryTotal.ToString ("F0"), _fontHandwriting, _brushHandwriting, 792, 1725, rightAlign); // Salary total again - graphics.DrawString(data.TaxAdditiveTotal.ToString("F0"), fontHandwriting, brushHandwriting, 1510, + graphics.DrawString(data.TaxAdditiveTotal.ToString("F0"), _fontHandwriting, _brushHandwriting, 1510, 1326, rightAlign); // Emp fee total - graphics.DrawString (data.TaxSubtractiveTotal.ToString ("F0"), fontHandwriting, brushHandwriting, 1510, 1525, + graphics.DrawString (data.TaxSubtractiveTotal.ToString ("F0"), _fontHandwriting, _brushHandwriting, 1510, 1525, rightAlign); // Deducted main - graphics.DrawString (data.TaxSubtractiveTotal.ToString ("F0"), fontHandwriting, brushHandwriting, 1510, 1726, + graphics.DrawString (data.TaxSubtractiveTotal.ToString ("F0"), _fontHandwriting, _brushHandwriting, 1510, 1726, rightAlign); // Deducted total - graphics.DrawString (data.TaxTotal.ToString ("F0"), fontHandwriting, brushHandwriting, 1510, 1793, - rightAlign); // Tax cost total + + DrawWrittenNumber (data.TaxTotal, 1510, 1793, graphics); } using (Stream responseStream = Response.OutputStream) @@ -163,6 +166,26 @@ namespace Swarmops.Plugins.Stock.TaxForms } + private void DrawWrittenNumber (double number, int x, int y, Graphics graphics) + { + // This inserts spaces between every other character + string numberString = number.ToString ("F0"); + char thinSpace = ' '; // this is a UNICODE THIN SPACE character (U+2009) + string drawString = string.Empty + numberString[0]; + + for (int loop = 1; loop < numberString.Length; loop++) + { + drawString += thinSpace; + drawString += numberString[loop]; + } + + StringFormat rightAlign = new StringFormat(); + rightAlign.Alignment = StringAlignment.Far; + + graphics.DrawString (drawString, _fontHandwriting, _brushHandwriting, x, y, rightAlign); + + } + private class SalaryTaxData { @@ -170,7 +193,7 @@ namespace Swarmops.Plugins.Stock.TaxForms { Salary = new double[3]; TaxAdditive = new double[3]; - TaxTotal = 99999; // Prime with something for font metrics debugging + TaxTotal = 123456789; // Prime with something for font metrics debugging } public double[] Salary; From 3ea59b0f89527ccc626a05e9000b944f130bfc33 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Thu, 5 Feb 2015 20:07:20 +0100 Subject: [PATCH 04/12] Debugging Mono font metrics --- Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs b/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs index 58aa9b16..71d87fa5 100644 --- a/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs +++ b/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs @@ -155,7 +155,7 @@ namespace Swarmops.Plugins.Stock.TaxForms graphics.DrawString (data.TaxSubtractiveTotal.ToString ("F0"), _fontHandwriting, _brushHandwriting, 1510, 1726, rightAlign); // Deducted total - DrawWrittenNumber (data.TaxTotal, 1510, 1793, graphics); + DrawWrittenNumber (data.TaxTotal, 1485, 1793, graphics); } using (Stream responseStream = Response.OutputStream) @@ -170,12 +170,12 @@ namespace Swarmops.Plugins.Stock.TaxForms { // This inserts spaces between every other character string numberString = number.ToString ("F0"); - char thinSpace = ' '; // this is a UNICODE THIN SPACE character (U+2009) + char space = ' '; string drawString = string.Empty + numberString[0]; for (int loop = 1; loop < numberString.Length; loop++) { - drawString += thinSpace; + drawString += space; drawString += numberString[loop]; } From ae9974a14de2f0598657fa1b39539b107fe3e9a5 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Thu, 5 Feb 2015 20:20:23 +0100 Subject: [PATCH 05/12] More debugging --- Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs b/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs index 71d87fa5..deec15e5 100644 --- a/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs +++ b/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs @@ -60,7 +60,7 @@ namespace Swarmops.Plugins.Stock.TaxForms string courierFontName = "Liberation Mono"; int smallSize = 18; int regularSize = 24; - int handWriteSize = 40; + int handWriteSize = 36; if (Debugger.IsAttached) { @@ -95,8 +95,8 @@ namespace Swarmops.Plugins.Stock.TaxForms if (year <= 2014) { - graphics.DrawString (monthString, fontPreprinted, brushPreprinted, 680, 292); - graphics.DrawString (monthString, fontPreprinted, brushPreprinted, 620, 1460); + graphics.DrawString (monthString, fontPreprinted, brushPreprinted, 676, 288); + graphics.DrawString (monthString, fontPreprinted, brushPreprinted, 616, 1456); graphics.DrawString (String.Format ("{0,4}-{1:D2}-{2:D2}", year, month + 1, 12), fontPreprinted, brushPreprinted, 820, 160); graphics.DrawString (orgNumber, fontPreprinted, brushPreprinted, 1110, 160); @@ -155,7 +155,7 @@ namespace Swarmops.Plugins.Stock.TaxForms graphics.DrawString (data.TaxSubtractiveTotal.ToString ("F0"), _fontHandwriting, _brushHandwriting, 1510, 1726, rightAlign); // Deducted total - DrawWrittenNumber (data.TaxTotal, 1485, 1793, graphics); + DrawWrittenNumber (data.TaxTotal, 1492, 1793, graphics); } using (Stream responseStream = Response.OutputStream) From 9cedc0cb410eca3d8c47340de384b10bce811194 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Thu, 5 Feb 2015 21:34:16 +0100 Subject: [PATCH 06/12] More debugging of Mono metrics --- .../TaxForms/SE/MonthlyTaxForm-SE.aspx.cs | 160 ++++++++++++------ 1 file changed, 112 insertions(+), 48 deletions(-) diff --git a/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs b/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs index deec15e5..4ab79370 100644 --- a/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs +++ b/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs @@ -28,7 +28,7 @@ namespace Swarmops.Plugins.Stock.TaxForms int year = Int32.Parse (monthString.Substring (0, 4)); int month = Int32.Parse (monthString.Substring (4)); // never mind input checking - int[] yearBreakpoints = { 1938, year-65, year-26 }; + int[] yearBreakpoints = {1938, year - 65, year - 26}; double[] taxRates = {10.21, 15.49, 31.42}; // will need to adapt this when some of the rates change SalaryTaxData data = GetSalaryData (year, month, organization, yearBreakpoints); @@ -49,6 +49,33 @@ namespace Swarmops.Plugins.Stock.TaxForms coord[2010] = new Dictionary(); coord[2015] = new Dictionary(); + coord[2010][GraphicsElement.LeftColumnX] = 775; + coord[2010][GraphicsElement.RightColumnX] = 1492; + + coord[2015][GraphicsElement.LeftColumnX] = 800; + coord[2015][GraphicsElement.RightColumnX] = 1510; + + coord[2010][GraphicsElement.AgeBracketMainY] = 694; + coord[2010][GraphicsElement.AgeBracketDistY] = 67; + + coord[2015][GraphicsElement.AgeBracketMainY] = 770; + coord[2015][GraphicsElement.AgeBracketDistY] = 65; + + coord[2010][GraphicsElement.SalaryY] = 355; + coord[2010][GraphicsElement.SalaryTotalY] = 555; + coord[2010][GraphicsElement.AdditiveTaxTotalY] = 1329; + coord[2010][GraphicsElement.DeductedTaxY] = 1529; + coord[2010][GraphicsElement.DeductedTaxTotalY] = 1727; + coord[2010][GraphicsElement.SummaryTotalY] = 1795; + + coord[2015][GraphicsElement.SalaryY] = 440; + coord[2015][GraphicsElement.SalaryTotalY] = 640; + coord[2015][GraphicsElement.AdditiveTaxTotalY] = 1395; + coord[2015][GraphicsElement.DeductedTaxY] = 1560; + coord[2015][GraphicsElement.DeductedTaxTotalY] = 1759; + coord[2015][GraphicsElement.SummaryTotalY] = 1825; + + Image form = Image.FromFile (MapPath (".") + "/MonthlyTaxForm-SE-" + formVersion + ".png"); // the "." says "in same folder as this file" @@ -69,7 +96,7 @@ namespace Swarmops.Plugins.Stock.TaxForms orgNumber = "DEBUGGER ATTACHED"; smallSize = 24; regularSize = 30; - handWriteSize = 52; + handWriteSize = 48; } using (Graphics graphics = Graphics.FromImage (form)) @@ -89,75 +116,102 @@ namespace Swarmops.Plugins.Stock.TaxForms graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.SmoothingMode = SmoothingMode.AntiAlias; - // Draw the header and monthname - things to pay attention to - - graphics.DrawString (organization.Name, fontPreprinted, brushPreprinted, 150, 170); + // Draw the header and monthname - things to pay attention to when looking at cheat sheet if (year <= 2014) { - graphics.DrawString (monthString, fontPreprinted, brushPreprinted, 676, 288); - graphics.DrawString (monthString, fontPreprinted, brushPreprinted, 616, 1456); + graphics.DrawString(organization.Name, fontPreprinted, brushPreprinted, 150, 170); + + graphics.DrawString(monthString, fontPreprinted, brushPreprinted, 676, 288); + graphics.DrawString (monthString, fontPreprinted, brushPreprinted, 610, 1455); graphics.DrawString (String.Format ("{0,4}-{1:D2}-{2:D2}", year, month + 1, 12), fontPreprinted, brushPreprinted, 820, 160); graphics.DrawString (orgNumber, fontPreprinted, brushPreprinted, 1110, 160); + + // Draw the years and tax rates and other on-form constants more discreetly + + graphics.DrawString(taxRates[2].ToString("F2", swedishCulture), fontPreprintedSmall, + brushPreprintedDiscreet, 840, 729); + graphics.DrawString(taxRates[1].ToString("F2", swedishCulture), fontPreprintedSmall, + brushPreprintedDiscreet, 840, 796); + graphics.DrawString(taxRates[0].ToString("F2", swedishCulture), fontPreprintedSmall, + brushPreprintedDiscreet, 840, 863); + + graphics.DrawString(string.Format("{0} - {1}", yearBreakpoints[1], yearBreakpoints[2] - 1), + fontPreprintedSmall, brushPreprintedDiscreet, 190, 729); + graphics.DrawString(string.Format("{0} -", yearBreakpoints[2] - 1), + fontPreprintedSmall, brushPreprintedDiscreet, 190, 796); + graphics.DrawString(string.Format("{0}", yearBreakpoints[1] - 1), + fontPreprintedSmall, brushPreprintedDiscreet, 250, 859); } else // the latest revision of the tax form { - graphics.DrawString (monthString, fontPreprintedSans, brushPreprinted, 670, 287); - graphics.DrawString (monthString, fontPreprintedSans, brushPreprinted, 610, 1455); + graphics.DrawString(organization.Name.ToUpperInvariant(), fontPreprinted, brushPreprinted, 840, 260); + + graphics.DrawString(monthString, fontPreprintedSans, brushPreprinted, 700, 365); + graphics.DrawString (monthString, fontPreprintedSans, brushPreprinted, 637, 1487); graphics.DrawString (String.Format ("{0,4}-{1:D2}-{2:D2}", year, month + 1, 12), fontPreprinted, - brushPreprinted, 820, 160); - graphics.DrawString (orgNumber, fontPreprinted, brushPreprinted, 1110, 160); + brushPreprinted, 840, 180); + graphics.DrawString (orgNumber, fontPreprinted, brushPreprinted, 1125, 180); + + // Draw the years and tax rates and other on-form constants more discreetly + + graphics.DrawString(taxRates[2].ToString("F2", swedishCulture), fontPreprintedSmall, + brushPreprintedDiscreet, 870, 798); + graphics.DrawString(taxRates[1].ToString("F2", swedishCulture), fontPreprintedSmall, + brushPreprintedDiscreet, 870, 865); + graphics.DrawString(taxRates[0].ToString("F2", swedishCulture), fontPreprintedSmall, + brushPreprintedDiscreet, 870, 932); + + graphics.DrawString(string.Format("{0} - {1}", yearBreakpoints[1], yearBreakpoints[2] - 1), + fontPreprintedSmall, brushPreprintedDiscreet, 220, 800); + graphics.DrawString(string.Format("{0} -", yearBreakpoints[2] - 1), + fontPreprintedSmall, brushPreprintedDiscreet, 220, 863); + graphics.DrawString(string.Format("{0}", yearBreakpoints[1] - 1), + fontPreprintedSmall, brushPreprintedDiscreet, 270, 924); } - // Draw the years and tax rates and other on-form constants more discreetly + // Draw the actual numbers: First, salary totals - graphics.DrawString (taxRates[2].ToString ("F2", swedishCulture), fontPreprintedSmall, - brushPreprintedDiscreet, 840, 729); - graphics.DrawString (taxRates[1].ToString ("F2", swedishCulture), fontPreprintedSmall, - brushPreprintedDiscreet, 840, 796); - graphics.DrawString (taxRates[0].ToString ("F2", swedishCulture), fontPreprintedSmall, - brushPreprintedDiscreet, 840, 863); + DrawWrittenNumber(data.SalaryTotal, coord[formVersion][GraphicsElement.LeftColumnX], coord[formVersion][GraphicsElement.SalaryY], graphics); + DrawWrittenNumber(data.SalaryTotal, coord[formVersion][GraphicsElement.LeftColumnX], coord[formVersion][GraphicsElement.SalaryTotalY], graphics); - graphics.DrawString (string.Format ("{0} - {1}", yearBreakpoints[1], yearBreakpoints[2] - 1), - fontPreprintedSmall, brushPreprintedDiscreet, 190, 729); - graphics.DrawString (string.Format ("{0} -", yearBreakpoints[2] - 1), - fontPreprintedSmall, brushPreprintedDiscreet, 190, 796); - graphics.DrawString (string.Format ("{0}", yearBreakpoints[1] - 1), - fontPreprintedSmall, brushPreprintedDiscreet, 250, 859); - - // Draw the actual numbers - - graphics.DrawString (data.SalaryTotal.ToString ("F0"), _fontHandwriting, _brushHandwriting, 792, 358, - rightAlign); // Salary total sub - graphics.DrawString (data.SalaryTotal.ToString ("F0"), _fontHandwriting, _brushHandwriting, 792, 558, - rightAlign); // Salary total total + // Additive tax for (int ageBracket = 0; ageBracket <= 2; ageBracket++) { - if (data.Salary[ageBracket] > 0.0) + if (1.0 > 0.0) { - graphics.DrawString (data.Salary[ageBracket].ToString ("F0"), _fontHandwriting, _brushHandwriting, - 792, 692 + ageBracket*67, rightAlign); // Salary - graphics.DrawString (data.TaxAdditive[ageBracket].ToString ("F0"), _fontHandwriting, - _brushHandwriting, 1510, 692 + ageBracket*67, rightAlign); // Employer's fee + DrawWrittenNumber(data.Salary[ageBracket], coord[formVersion][GraphicsElement.LeftColumnX], + coord[formVersion][GraphicsElement.AgeBracketMainY] + + coord[formVersion][GraphicsElement.AgeBracketDistY] * ageBracket, + graphics); + + DrawWrittenNumber(data.TaxAdditive[ageBracket], coord[formVersion][GraphicsElement.RightColumnX], + coord[formVersion][GraphicsElement.AgeBracketMainY] + + coord[formVersion][GraphicsElement.AgeBracketDistY] * ageBracket, + graphics); + } } - graphics.DrawString(data.SalaryTotal.ToString("F0"), _fontHandwriting, _brushHandwriting, 792, 1524, - rightAlign); // Salary total again - graphics.DrawString (data.SalaryTotal.ToString ("F0"), _fontHandwriting, _brushHandwriting, 792, 1725, - rightAlign); // Salary total again - graphics.DrawString(data.TaxAdditiveTotal.ToString("F0"), _fontHandwriting, _brushHandwriting, 1510, - 1326, rightAlign); // Emp fee total - graphics.DrawString (data.TaxSubtractiveTotal.ToString ("F0"), _fontHandwriting, _brushHandwriting, 1510, 1525, - rightAlign); // Deducted main - graphics.DrawString (data.TaxSubtractiveTotal.ToString ("F0"), _fontHandwriting, _brushHandwriting, 1510, 1726, - rightAlign); // Deducted total + DrawWrittenNumber (data.TaxAdditiveTotal, coord[formVersion][GraphicsElement.RightColumnX], coord[formVersion][GraphicsElement.AdditiveTaxTotalY], graphics); - DrawWrittenNumber (data.TaxTotal, 1492, 1793, graphics); + // Deducted taxes + + DrawWrittenNumber(data.SalaryTotal, coord[formVersion][GraphicsElement.LeftColumnX], coord[formVersion][GraphicsElement.DeductedTaxY], graphics); + DrawWrittenNumber(data.SalaryTotal, coord[formVersion][GraphicsElement.LeftColumnX], coord[formVersion][GraphicsElement.DeductedTaxTotalY], graphics); + + DrawWrittenNumber(data.TaxSubtractiveTotal, coord[formVersion][GraphicsElement.RightColumnX], coord[formVersion][GraphicsElement.DeductedTaxY], graphics); + DrawWrittenNumber(data.TaxSubtractiveTotal, coord[formVersion][GraphicsElement.RightColumnX], coord[formVersion][GraphicsElement.DeductedTaxTotalY], graphics); + + // Grand total + + DrawWrittenNumber (data.TaxTotal, coord[formVersion][GraphicsElement.RightColumnX], coord[formVersion][GraphicsElement.SummaryTotalY], graphics); } + // Write graphics buffer as PNG stream to Response object + using (Stream responseStream = Response.OutputStream) { form.Save (responseStream, ImageFormat.Png); @@ -168,6 +222,13 @@ namespace Swarmops.Plugins.Stock.TaxForms private void DrawWrittenNumber (double number, int x, int y, Graphics graphics) { + if (Debugger.IsAttached) + { + x += 10; // Mono has different metrics because of bloody course it has + } + + y += 3; // global adjustment (ok, this is ugly) + // This inserts spaces between every other character string numberString = number.ToString ("F0"); char space = ' '; @@ -246,8 +307,11 @@ namespace Swarmops.Plugins.Stock.TaxForms AgeBracketMainY, AgeBracketDistY, AdditiveTaxTotalY, + DeductedTaxY, + DeductedTaxTotalY, + SalaryY, + SalaryTotalY, SummaryTopLineY, - SummaryBottomLineY, SummaryTotalY } } From ed2c1d87cbd41846cf3f51c1d621885f5d7969e6 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Thu, 5 Feb 2015 21:46:34 +0100 Subject: [PATCH 07/12] Done debugging Mono metrics --- .../TaxForms/SE/MonthlyTaxForm-SE.aspx.cs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs b/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs index 4ab79370..63b1f012 100644 --- a/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs +++ b/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs @@ -53,7 +53,7 @@ namespace Swarmops.Plugins.Stock.TaxForms coord[2010][GraphicsElement.RightColumnX] = 1492; coord[2015][GraphicsElement.LeftColumnX] = 800; - coord[2015][GraphicsElement.RightColumnX] = 1510; + coord[2015][GraphicsElement.RightColumnX] = 1512; coord[2010][GraphicsElement.AgeBracketMainY] = 694; coord[2010][GraphicsElement.AgeBracketDistY] = 67; @@ -146,10 +146,10 @@ namespace Swarmops.Plugins.Stock.TaxForms } else // the latest revision of the tax form { - graphics.DrawString(organization.Name.ToUpperInvariant(), fontPreprinted, brushPreprinted, 840, 260); + graphics.DrawString(organization.Name.ToUpperInvariant(), fontPreprinted, brushPreprinted, 840, 255); - graphics.DrawString(monthString, fontPreprintedSans, brushPreprinted, 700, 365); - graphics.DrawString (monthString, fontPreprintedSans, brushPreprinted, 637, 1487); + graphics.DrawString(monthString, fontPreprintedSans, brushPreprinted, 702, 363); + graphics.DrawString (monthString, fontPreprintedSans, brushPreprinted, 639, 1485); graphics.DrawString (String.Format ("{0,4}-{1:D2}-{2:D2}", year, month + 1, 12), fontPreprinted, brushPreprinted, 840, 180); graphics.DrawString (orgNumber, fontPreprinted, brushPreprinted, 1125, 180); @@ -180,7 +180,7 @@ namespace Swarmops.Plugins.Stock.TaxForms for (int ageBracket = 0; ageBracket <= 2; ageBracket++) { - if (1.0 > 0.0) + if (data.Salary[ageBracket] > 0.0) { DrawWrittenNumber(data.Salary[ageBracket], coord[formVersion][GraphicsElement.LeftColumnX], coord[formVersion][GraphicsElement.AgeBracketMainY] + @@ -227,8 +227,6 @@ namespace Swarmops.Plugins.Stock.TaxForms x += 10; // Mono has different metrics because of bloody course it has } - y += 3; // global adjustment (ok, this is ugly) - // This inserts spaces between every other character string numberString = number.ToString ("F0"); char space = ' '; @@ -254,7 +252,6 @@ namespace Swarmops.Plugins.Stock.TaxForms { Salary = new double[3]; TaxAdditive = new double[3]; - TaxTotal = 123456789; // Prime with something for font metrics debugging } public double[] Salary; From a9f239ea5b2a90f3a1be8e6b3be1c3fb9ecaa8ca Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Thu, 5 Feb 2015 23:50:25 +0100 Subject: [PATCH 08/12] Changed back to Bold --- Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs b/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs index 63b1f012..5d97142c 100644 --- a/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs +++ b/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs @@ -104,7 +104,7 @@ namespace Swarmops.Plugins.Stock.TaxForms StringFormat rightAlign = new StringFormat(); rightAlign.Alignment = StringAlignment.Far; - _fontHandwriting = new Font (sansFontName, handWriteSize, FontStyle.Regular); + _fontHandwriting = new Font (sansFontName, handWriteSize, FontStyle.Bold); Font fontPreprinted = new Font (courierFontName, regularSize, FontStyle.Bold); Font fontPreprintedSmall = new Font (courierFontName, smallSize, FontStyle.Bold); Font fontPreprintedSans = new Font (sansFontName, regularSize, FontStyle.Bold); From a360db6e549572f5ae8b2d5eb033286acbddafd1 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Thu, 5 Feb 2015 23:51:38 +0100 Subject: [PATCH 09/12] Small adjustment to legacy tax forms --- Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs b/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs index 5d97142c..737d2cdc 100644 --- a/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs +++ b/Site5/Plugins-Stock/TaxForms/SE/MonthlyTaxForm-SE.aspx.cs @@ -61,8 +61,8 @@ namespace Swarmops.Plugins.Stock.TaxForms coord[2015][GraphicsElement.AgeBracketMainY] = 770; coord[2015][GraphicsElement.AgeBracketDistY] = 65; - coord[2010][GraphicsElement.SalaryY] = 355; - coord[2010][GraphicsElement.SalaryTotalY] = 555; + coord[2010][GraphicsElement.SalaryY] = 360; + coord[2010][GraphicsElement.SalaryTotalY] = 560; coord[2010][GraphicsElement.AdditiveTaxTotalY] = 1329; coord[2010][GraphicsElement.DeductedTaxY] = 1529; coord[2010][GraphicsElement.DeductedTaxTotalY] = 1727; From 108cbd4be14517d1b1488e0e28ea25e1f1ff0a0c Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Fri, 6 Feb 2015 12:00:13 +0100 Subject: [PATCH 10/12] Some fundamental work to support RTL --- Site5/Style/style-v5.css | 26 ++++++++++++++++++ Site5/Style/superfish.css | 56 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/Site5/Style/style-v5.css b/Site5/Style/style-v5.css index d11aa10d..9237fc57 100644 --- a/Site5/Style/style-v5.css +++ b/Site5/Style/style-v5.css @@ -80,6 +80,10 @@ div.break width: 400px; } +[style*="rtl"] .logoimage +{ + float: right; +} /* CURRENT USER INFO */ @@ -111,6 +115,9 @@ div.currentuserinfo font-size:14px; } +[style*="rtl"] div.currentuserinfo { + float: left; +} /* MAIN TOP MENU */ @@ -153,6 +160,11 @@ img left:-5px; } +[style*="rtl"] .searchbox { + float: left; + left: 5px; +} + .searchbox input { width:150px; @@ -170,6 +182,12 @@ img font-family: Ubuntu, Liberation Sans, Arial, Helvetica, Sans-Serif; } +[style*="rtl"] .searchbox input { + padding-left: inherit; + padding-right: 26px; + background-position-x: right; +} + #ctl00_MainMenu { font: 14px/20px Ubuntu !important; @@ -191,6 +209,10 @@ img width: 700px; } +[style*="rtl"] .mainbar { + float: right; +} + .sidebar { float: right; @@ -198,6 +220,10 @@ img margin-left: 20px; } +[style*="rtl"] .sidebar { + float: left; +} + .box { width: 100%; diff --git a/Site5/Style/superfish.css b/Site5/Style/superfish.css index c0079fb4..f35c96b7 100644 --- a/Site5/Style/superfish.css +++ b/Site5/Style/superfish.css @@ -7,6 +7,7 @@ .sf-menu li { position: relative; } + .sf-menu ul { position: absolute; display: none; @@ -14,26 +15,45 @@ left: 0; z-index: 9990; } + +[style*="rtl"] .sf-menu ul { /* RTL override */ + left: inherit; + right: 0; +} + .sf-menu > li { float: left; } +[style*="rtl"] .sf-menu > li { /* RTL override */ + float: right; +} + + .sf-menu li:hover > ul, .sf-menu li.sfHover > ul { display: block; - box-shadow: #1C397E 0px 1px 1px 1px, #A2BEFF 0px 1px 5px 1px; + box-shadow: 0px 1px 1px 1px #1C397E, 0px 1px 5px 1px #A2BEFF; } + .sf-menu a { display: block; position: relative; } + .sf-menu ul ul { top: 0; left: 100%; } +[style*="rtl"] .sf-menu ul ul { /* RTL override*/ + left: inherit; + right: 100%; +} + + /*** SWARMOPS SKIN - HEAVILY MODIFIED FROM SUPERFISH DISTRO ***/ @@ -41,6 +61,13 @@ float: left; margin-left: 14px; } + +[style*="rtl"] .sf-menu { + float: right; + margin-left: inherit; + margin-right: 14px; +} + .sf-menu ul { box-shadow: 2px 2px 6px rgba(0,0,0,.2); min-width: 12em; /* allow long menu items to determine submenu width */ @@ -62,6 +89,12 @@ padding: 2px 10px 8px 12px; } +[style*="rtl"] .sf-menu ul a +{ + padding: 2px 12px 8px 10px; +} + + .sf-menu a:hover, .sf-menu li.sfHover > a { color: #7F5500; @@ -92,6 +125,12 @@ left: -4px; } +[style*="rtl"] .sf-menu ul li img +{ + left: 4px; +} + + .sf-menu li { background: transparent; white-space: nowrap; /* no need for Supersubs plugin */ @@ -102,9 +141,22 @@ .sf-menu ul li { background: #E4ECFF; } + +.sf-menu li.Submenu li.Submenu { /* default: LTR website */ + background: #FF00FF url('/Style/submenu-rightarrow.png') no-repeat right top; /* testing selector */ + padding-left: 20px; /* no icon */ +} + +[style*="rtl"] .sf-menu li.Submenu li.Submenu { /* Right-to-left override */ + background: #FF0044 url('/Style/submenu-leftarrow.png') no-repeat right top; /* testing selector */ + padding-left: inherit; + padding-right: 20px; /* no icon */ +} + +/* -- commented out .sf-menu ul ul li { background: #FF00FF; -} +}*/ .sf-menu li:not(.Separator):hover, .sf-menu li.sfHover { From fca7af33aedd94d7705a0db4d860a02a8e961b07 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Fri, 6 Feb 2015 16:41:35 +0100 Subject: [PATCH 11/12] Changed backend to operate on UTC --- Backend/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Backend/Program.cs b/Backend/Program.cs index bcdf3e87..40536d00 100644 --- a/Backend/Program.cs +++ b/Backend/Program.cs @@ -164,7 +164,7 @@ namespace Swarmops.Backend Persistence.Key["SwarmopsInstallationId"] = Guid.NewGuid().ToString(); } - DateTime cycleStartTime = DateTime.Now; + DateTime cycleStartTime = DateTime.UtcNow; int lastSecond = cycleStartTime.Second; int lastMinute = cycleStartTime.Minute; @@ -176,7 +176,7 @@ namespace Swarmops.Backend { BotLog.Write (0, "MainCycle", "Cycle Start"); - cycleStartTime = DateTime.Now; + cycleStartTime = DateTime.UtcNow; try { From 15aee8f8137c3df431528cd3665d67961897eda5 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Fri, 6 Feb 2015 18:05:11 +0100 Subject: [PATCH 12/12] Added currency rate conversions --- Database/Database-ConnectionsCommands.cs | 2 +- Database/Database-Currencies.cs | 1 + Database/Database-ExchangeRates.cs | 26 ++++++++++---- Logic/Financial/Currency.cs | 46 ++++++++++++++++++++++++ Logic/Financial/Money.cs | 25 +++++++++++++ Logic/Logic.csproj | 1 + Site5/Master-v5.master.cs | 3 ++ 7 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 Logic/Financial/Money.cs diff --git a/Database/Database-ConnectionsCommands.cs b/Database/Database-ConnectionsCommands.cs index 93c0f94c..7b7af070 100644 --- a/Database/Database-ConnectionsCommands.cs +++ b/Database/Database-ConnectionsCommands.cs @@ -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 = { diff --git a/Database/Database-Currencies.cs b/Database/Database-Currencies.cs index 875a35fe..ed21c414 100644 --- a/Database/Database-Currencies.cs +++ b/Database/Database-Currencies.cs @@ -100,6 +100,7 @@ namespace Swarmops.Database #endregion + #region Creation and manipulation - stored procedures public int CreateCurrency (string code, string name, string sign) diff --git a/Database/Database-ExchangeRates.cs b/Database/Database-ExchangeRates.cs index 7a598fca..6640f351 100644 --- a/Database/Database-ExchangeRates.cs +++ b/Database/Database-ExchangeRates.cs @@ -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) { diff --git a/Logic/Financial/Currency.cs b/Logic/Financial/Currency.cs index f5312a6b..1033cff0 100644 --- a/Logic/Financial/Currency.cs +++ b/Logic/Financial/Currency.cs @@ -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); diff --git a/Logic/Financial/Money.cs b/Logic/Financial/Money.cs new file mode 100644 index 00000000..548715ac --- /dev/null +++ b/Logic/Financial/Money.cs @@ -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 }; + } + } +} diff --git a/Logic/Logic.csproj b/Logic/Logic.csproj index 95d2c649..47bf9b71 100644 --- a/Logic/Logic.csproj +++ b/Logic/Logic.csproj @@ -138,6 +138,7 @@ + diff --git a/Site5/Master-v5.master.cs b/Site5/Master-v5.master.cs index 2b977470..9d686710 100644 --- a/Site5/Master-v5.master.cs +++ b/Site5/Master-v5.master.cs @@ -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)