Testing currency box blur II

This commit is contained in:
Rick Falkvinge
2017-09-25 18:46:19 +02:00
parent 103f6f0d0e
commit c3b6765e7b
4 changed files with 117 additions and 9 deletions
@@ -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; }
}
}
}
+23
View File
@@ -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")};
}
}
}
@@ -4,7 +4,7 @@
<!-- TODO: Add nice autocomplete stuff -->
<% if (this.Layout == LayoutDirection.Vertical) { %><div class="stacked-input-control"><% } %>
<asp:TextBox ID="TextInput" runat="server" CssClass="alignRight" /><asp:HiddenField ID="NativeCurrency" runat="server"/><asp:HiddenField ID="NativeAmount" runat="server" />
<asp:TextBox ID="TextInput" runat="server" CssClass="alignRight" /><asp:HiddenField ID="EnteredCurrency" runat="server"/><asp:HiddenField ID="EnteredAmount" runat="server" />
<% if (this.Layout == LayoutDirection.Vertical) { %></div><% } %>
@@ -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);
}
});
});
}
});
+4 -4
View File
@@ -22,21 +22,21 @@ namespace Swarmops.Frontend.Controls.Financial {
protected global::System.Web.UI.WebControls.TextBox TextInput;
/// <summary>
/// NativeCurrency control.
/// EnteredCurrency control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.HiddenField NativeCurrency;
protected global::System.Web.UI.WebControls.HiddenField EnteredCurrency;
/// <summary>
/// NativeAmount control.
/// EnteredAmount control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.HiddenField NativeAmount;
protected global::System.Web.UI.WebControls.HiddenField EnteredAmount;
}
}