From d50a39eb118d4966e95313176de1f7df8c7886d3 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Mon, 25 Sep 2017 20:16:13 +0200 Subject: [PATCH] CurrencyTextBox should handle currencies now --- Common/Enums/ObjectOptionalDataType.cs | 14 ++++++++-- Logic/Financial/InboundInvoice.cs | 28 +++++++++++++++++++ .../v5/Financial/CurrencyTextBox.ascx | 23 +++++++-------- .../v5/Financial/CurrencyTextBox.ascx.cs | 25 ++++++++++++++++- .../v5/Financial/CreateInboundInvoice.aspx.cs | 10 +++++++ 5 files changed, 84 insertions(+), 16 deletions(-) diff --git a/Common/Enums/ObjectOptionalDataType.cs b/Common/Enums/ObjectOptionalDataType.cs index a2dd3b9d..b3e48457 100644 --- a/Common/Enums/ObjectOptionalDataType.cs +++ b/Common/Enums/ObjectOptionalDataType.cs @@ -234,7 +234,7 @@ namespace Swarmops.Common.Enums FinancialAccountEnabledForInvoicing, /// - /// Organization's operations currency, stored as currency code + /// Organization's presentation currency, stored as currency code /// OrgCurrency, @@ -396,6 +396,16 @@ namespace Swarmops.Common.Enums /// /// Prohibit all active sessions until this time (UTC). /// - QuarantineSessionsUntil + QuarantineSessionsUntil, + + /// + /// If nonpresentation currency, which currency was used (the code). + /// + NativeCurrencyCode, + + /// + /// If nonpresentation currency, which currency was used (Int64 cents). + /// + NativeCurrencyAmountCents } } \ No newline at end of file diff --git a/Logic/Financial/InboundInvoice.cs b/Logic/Financial/InboundInvoice.cs index 0a89baab..8fe59fa4 100644 --- a/Logic/Financial/InboundInvoice.cs +++ b/Logic/Financial/InboundInvoice.cs @@ -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); + } + } } } \ No newline at end of file diff --git a/Site/Controls/v5/Financial/CurrencyTextBox.ascx b/Site/Controls/v5/Financial/CurrencyTextBox.ascx index 09401e17..bfa26f51 100644 --- a/Site/Controls/v5/Financial/CurrencyTextBox.ascx +++ b/Site/Controls/v5/Financial/CurrencyTextBox.ascx @@ -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); + } + }); } }); diff --git a/Site/Controls/v5/Financial/CurrencyTextBox.ascx.cs b/Site/Controls/v5/Financial/CurrencyTextBox.ascx.cs index 4cb04b2b..936b0772 100644 --- a/Site/Controls/v5/Financial/CurrencyTextBox.ascx.cs +++ b/Site/Controls/v5/Financial/CurrencyTextBox.ascx.cs @@ -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)); + } + } } } \ No newline at end of file diff --git a/Site/Pages/v5/Financial/CreateInboundInvoice.aspx.cs b/Site/Pages/v5/Financial/CreateInboundInvoice.aspx.cs index 73d5629e..8ca2b9e4 100644 --- a/Site/Pages/v5/Financial/CreateInboundInvoice.aspx.cs +++ b/Site/Pages/v5/Financial/CreateInboundInvoice.aspx.cs @@ -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