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) { %>
<% } %> - + <% if (this.Layout == LayoutDirection.Vertical) { %>
<% } %> @@ -28,10 +28,37 @@ // <%=this.ClientID%>_enable(); } - $(document).ready(function() { - $('#<%=this.ClientID%>_TextInput').blur(function() { - alert('currency blur'); - }); + $(document).ready(function () { + var currencyText = $('#<%=this.ClientID%>_TextInput').val(); + + var jsonData = {}; + jsonData.input = currencyText; + + if (currencyText.trim().indexOf(' ') >= 0) { + // the text contains at least one space, so try to interpret it and convert it to presentation currency + + $('#<%=this.ClientID%>_TextInput').blur(function() { + SwarmopsJS.ajaxCall('/Automation/FinancialFunctions.aspx/InterpretCurrency', + jsonData, + function(data) { + if (data.Success) { + alert("PresentationAmount:" + data.NewText + "\r\nUsed Currency:" + data.EnteredCurrency + "\r\nEntered Amount:" + data.EnteredAmount); + } + }); + }); + } else { + // the text does NOT contain at least one space, so we should format it for presentation currency + $('#<%=this.ClientID%>_TextInput').blur(function() { + SwarmopsJS.ajaxCall('/Automation/Formatting.aspx/FormatCurrencyString', + jsonData, + function(data) { + if (data.Success) { + $('#<%=this.ClientID%>_TextInput').val(data.Result); + } + }); + }); + } + }); diff --git a/Site/Controls/v5/Financial/CurrencyTextBox.ascx.designer.cs b/Site/Controls/v5/Financial/CurrencyTextBox.ascx.designer.cs index 8d306db8..ad602d2c 100644 --- a/Site/Controls/v5/Financial/CurrencyTextBox.ascx.designer.cs +++ b/Site/Controls/v5/Financial/CurrencyTextBox.ascx.designer.cs @@ -22,21 +22,21 @@ namespace Swarmops.Frontend.Controls.Financial { protected global::System.Web.UI.WebControls.TextBox TextInput; /// - /// NativeCurrency control. + /// EnteredCurrency control. /// /// /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// - protected global::System.Web.UI.WebControls.HiddenField NativeCurrency; + protected global::System.Web.UI.WebControls.HiddenField EnteredCurrency; /// - /// NativeAmount control. + /// EnteredAmount control. /// /// /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// - protected global::System.Web.UI.WebControls.HiddenField NativeAmount; + protected global::System.Web.UI.WebControls.HiddenField EnteredAmount; } }