CurrencyTextBox should handle currencies now

This commit is contained in:
Rick Falkvinge
2017-09-25 20:16:13 +02:00
parent 0379dba2b3
commit d50a39eb11
5 changed files with 84 additions and 16 deletions
+12 -2
View File
@@ -234,7 +234,7 @@ namespace Swarmops.Common.Enums
FinancialAccountEnabledForInvoicing,
/// <summary>
/// Organization's operations currency, stored as currency code
/// Organization's presentation currency, stored as currency code
/// </summary>
OrgCurrency,
@@ -396,6 +396,16 @@ namespace Swarmops.Common.Enums
/// <summary>
/// Prohibit all active sessions until this time (UTC).
/// </summary>
QuarantineSessionsUntil
QuarantineSessionsUntil,
/// <summary>
/// If nonpresentation currency, which currency was used (the code).
/// </summary>
NativeCurrencyCode,
/// <summary>
/// If nonpresentation currency, which currency was used (Int64 cents).
/// </summary>
NativeCurrencyAmountCents
}
}
+28
View File
@@ -300,5 +300,33 @@ namespace Swarmops.Logic.Financial
throw new InvalidOperationException("Can't set PaidOut if Attested is false");
}
}
public bool HasNativeCurrency
{
get
{
return
ObjectOptionalData.ForObject(this)
.GetOptionalDataString(ObjectOptionalDataType.NativeCurrencyCode)
.Length > 0;
}
}
public Money NativeCurrencyAmount
{
get
{
ObjectOptionalData optionalData = ObjectOptionalData.ForObject(this);
return new Money(optionalData.GetOptionalDataInt64(ObjectOptionalDataType.NativeCurrencyAmountCents), Currency.FromCode(optionalData.GetOptionalDataString(ObjectOptionalDataType.NativeCurrencyCode)));
}
set
{
ObjectOptionalData optionalData = ObjectOptionalData.ForObject(this);
optionalData.SetOptionalDataInt64(ObjectOptionalDataType.NativeCurrencyAmountCents, value.Cents);
optionalData.SetOptionalDataString(ObjectOptionalDataType.NativeCurrencyCode, value.Currency.Code);
}
}
}
}
+10 -13
View File
@@ -30,12 +30,9 @@
$(document).ready(function () {
alertify.success("doc.ready - <%=this.ClientID%>");
$('#<%=this.ClientID%>_TextInput').blur(function () {
var currencyText = $('#<%=this.ClientID%>_TextInput').val();
alertify.success("blur - <%=this.ClientID%> - " + currencyText);
var jsonData = {};
jsonData.input = currencyText;
@@ -47,20 +44,20 @@
jsonData,
function(data) {
if (data.Success) {
alert("PresentationAmount:" + data.DisplayAmount + "\r\nUsed Currency:" + data.CurrencyCode + "\r\nEntered Amount:" + data.EnteredAmount);
$('#<%=this.ClientID%>_TextInput').val(data.DisplayAmount);
$('#<%=this.ClientID%>_EnteredCurrency').val(data.CurrencyCode);
$('#<%=this.ClientID%>_EnteredAmount').val(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.NewValue);
}
});
});
SwarmopsJS.ajaxCall('/Automation/Formatting.aspx/FormatCurrencyString',
jsonData,
function(data) {
if (data.Success) {
$('#<%=this.ClientID%>_TextInput').val(data.NewValue);
}
});
}
});
@@ -2,6 +2,7 @@
using System.Globalization;
using Swarmops.Common.Enums;
using System.Web.UI;
using Swarmops.Logic.Financial;
namespace Swarmops.Frontend.Controls.Financial
{
@@ -31,7 +32,16 @@ namespace Swarmops.Frontend.Controls.Financial
{
return (Int64) (InternalValue * 100.0 + 0.5); // the +0.5 is to compensate for floating point errors. And yes, THEY HAPPEN.
}
set { this.TextInput.Text = (value / 100.0).ToString("N2", CultureInfo.CurrentCulture); }
set
{
this.TextInput.Text = (value / 100.0).ToString("N2", CultureInfo.CurrentCulture);
if (value == 0)
{
// if zeroing, also clear the foreign currency
this.EnteredCurrency.Value = string.Empty;
this.EnteredAmount.Value = string.Empty;
}
}
}
public Int64 Metacents // Metacents are cents of cents, so four decimals to the currency unit.
@@ -82,5 +92,18 @@ namespace Swarmops.Frontend.Controls.Financial
{
this.TextInput.Focus();
}
public bool NonPresentationCurrencyUsed
{
get { return this.EnteredCurrency.Value.Length > 0; }
}
public Money NonPresentationCurrencyAmount
{
get
{
return new Money((long) (Double.Parse(this.EnteredAmount.Value, NumberStyles.AllowThousands | NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture) * 100.0 + 0.5), Currency.FromCode(this.EnteredCurrency.Value));
}
}
}
}
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Web;
using Resources;
@@ -183,6 +184,15 @@ namespace Swarmops.Frontend.Pages.v5.Financial
documents.SetForeignObjectForAll (invoice);
// If amounts were in a different currency, record the native values for proper payment
if (this.CurrencyAmount.NonPresentationCurrencyUsed)
{
Money currencyEntered = this.CurrencyAmount.NonPresentationCurrencyAmount;
invoice.NativeCurrencyAmount = currencyEntered;
}
// Display success message
this._invoiceId = invoice.Identity; // a property returns the localized string