diff --git a/Site/Automation/FinancialFunctions.aspx.cs b/Site/Automation/FinancialFunctions.aspx.cs index 7571c28c..1547f012 100644 --- a/Site/Automation/FinancialFunctions.aspx.cs +++ b/Site/Automation/FinancialFunctions.aspx.cs @@ -66,5 +66,63 @@ namespace Swarmops.Frontend.Automation return new AjaxCallResult {Success = false, DisplayMessage = "Address already set"}; } } + + [WebMethod] + public static InterpretedCurrencyResult InterpretCurrency(string input) + { + AuthenticationData authData = GetAuthenticationDataAndCulture(); + + string[] currencyStrings = input.Split(' '); + if (currencyStrings.Length != 2) + { + return new InterpretedCurrencyResult {Success = false, DisplayMessage="Cannot interpret field"}; + } + + Currency currencyUsed = null; + + try + { + currencyUsed = Currency.FromCode(currencyStrings[0].ToUpperInvariant()); + } + catch (Exception) + { + return new InterpretedCurrencyResult { Success = false, DisplayMessage = "Cannot interpret currency" }; + } + + Int64 amountCents = 0; + double amountDouble = 0.0; + bool amountInterpreted = false; + + if (Double.TryParse(currencyStrings[1], NumberStyles.AllowThousands | NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out amountDouble)) + { + amountInterpreted = true; + } + else if (Double.TryParse(currencyStrings[1], NumberStyles.AllowThousands | NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out amountDouble)) + { + amountInterpreted = true; + } + + if (!amountInterpreted) + { + return new InterpretedCurrencyResult { Success = false, DisplayMessage = "Cannot interpret amount" }; + } + + Money money = new Money((long) (amountDouble * 100.0 + 0.5), currencyUsed); + + return new InterpretedCurrencyResult + { + Success = true, + CurrencyCode = money.Currency.Code, + DisplayAmount = (money.ToCurrency(authData.CurrentOrganization.Currency).Cents/100.0).ToString("N2") + }; + } + + + public class InterpretedCurrencyResult : AjaxCallResult + { + public string CurrencyCode { get; set; } + public string EnteredAmount { get; set; } + public string DisplayAmount { get; set; } + } } } \ No newline at end of file diff --git a/Site/Automation/Formatting.aspx.cs b/Site/Automation/Formatting.aspx.cs index 2f216829..b5a29746 100644 --- a/Site/Automation/Formatting.aspx.cs +++ b/Site/Automation/Formatting.aspx.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Web; using System.Web.Services; @@ -39,5 +40,27 @@ namespace Swarmops.Frontend.Automation return input.ToString("N2"); } + + public static AjaxInputCallResult FormatCurrencyString(string input) + { + double outParse = 0.0; + bool success = false; + + if (Double.TryParse(input, NumberStyles.AllowThousands | NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out outParse)) + { + success = true; + } + else if (Double.TryParse(input, NumberStyles.AllowThousands | NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out outParse)) + { + success = true; + } + + if (!success) + { + return new AjaxInputCallResult {Success = false}; + } + + return new AjaxInputCallResult {Success = true, NewValue = outParse.ToString("N2")}; + } } } \ No newline at end of file diff --git a/Site/Controls/v5/Financial/CurrencyTextBox.ascx b/Site/Controls/v5/Financial/CurrencyTextBox.ascx index e0bf139f..7dcabd1e 100644 --- a/Site/Controls/v5/Financial/CurrencyTextBox.ascx +++ b/Site/Controls/v5/Financial/CurrencyTextBox.ascx @@ -4,7 +4,7 @@ <% if (this.Layout == LayoutDirection.Vertical) { %>