-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Woo POS] [Cash & Receipts] Validate cash amount #14811
Open
iamgabrielma
wants to merge
4
commits into
task/14602-handle-cash-payment-success
Choose a base branch
from
task/14749-pos-cash-validation
base: task/14602-handle-cash-payment-success
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import SwiftUI | ||
import class WooFoundation.CurrencyFormatter | ||
|
||
struct PointOfSaleCollectCashView: View { | ||
@Environment(\.colorScheme) var colorScheme | ||
|
@@ -8,20 +9,16 @@ struct PointOfSaleCollectCashView: View { | |
@State private var textFieldAmountInput: String = "" | ||
@State private var isLoading: Bool = false | ||
@State private var errorMessage: String? | ||
@State private var changeDueMessage: String? | ||
|
||
private let currencyFormatter: CurrencyFormatter = WooFoundation.CurrencyFormatter(currencySettings: ServiceLocator.currencySettings) | ||
|
||
let orderTotal: String | ||
|
||
private var formattedOrderTotal: String { | ||
String.localizedStringWithFormat(Localization.backNavigationSubtitle, orderTotal) | ||
} | ||
|
||
private func validateAmount() -> Bool { | ||
// TODO: | ||
// Validate amount entered vs order total | ||
// https://github.com/woocommerce/woocommerce-ios/issues/14749 | ||
return true | ||
} | ||
|
||
@StateObject private var textFieldViewModel = FormattableAmountTextFieldViewModel(size: .extraLarge, | ||
locale: Locale.autoupdatingCurrent, | ||
storeCurrencySettings: ServiceLocator.currencySettings, | ||
|
@@ -54,8 +51,15 @@ struct PointOfSaleCollectCashView: View { | |
FormattableAmountTextField(viewModel: textFieldViewModel, style: .pos) | ||
.onChange(of: textFieldViewModel.amount) { newValue in | ||
textFieldAmountInput = newValue | ||
updateChangeDueMessage() | ||
} | ||
|
||
if let changeDue = changeDueMessage { | ||
Text(changeDue) | ||
.font(.posBodyRegular) | ||
.foregroundColor(.posTextSuccess) | ||
} | ||
|
||
if let errorMessage = errorMessage { | ||
Text(errorMessage) | ||
.font(POSFontStyle.posBodyRegular) | ||
|
@@ -64,7 +68,7 @@ struct PointOfSaleCollectCashView: View { | |
|
||
Button(action: { | ||
Task { @MainActor in | ||
guard validateAmount() else { | ||
guard validateAmountOnSubmit() else { | ||
return | ||
} | ||
isLoading = true | ||
|
@@ -102,6 +106,7 @@ struct PointOfSaleCollectCashView: View { | |
.background(backgroundColor) | ||
.padding() | ||
.animation(.easeInOut, value: errorMessage) | ||
.animation(.easeInOut, value: changeDueMessage) | ||
.onChange(of: textFieldAmountInput) { _ in | ||
errorMessage = nil | ||
} | ||
|
@@ -112,6 +117,46 @@ struct PointOfSaleCollectCashView: View { | |
} | ||
} | ||
|
||
private extension PointOfSaleCollectCashView { | ||
func parseCurrency(_ amountString: String) -> NSDecimalNumber? { | ||
currencyFormatter.convertToDecimal(amountString, locale: .current) | ||
} | ||
|
||
private func formatAsCurrency(_ amount: NSDecimalNumber) -> String { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All these methods could be unit tested (with mocked currency formatter). We have calculations happening within them. These methods would likely need to change into pure functions within a helper and then View would just set appropriate labels based on the result. |
||
currencyFormatter.formatAmount(amount) ?? "$0.00" | ||
} | ||
|
||
private func updateChangeDueMessage() { | ||
guard let orderDecimal = parseCurrency(orderTotal), | ||
let inputDecimal = parseCurrency(textFieldAmountInput) else { | ||
changeDueMessage = nil | ||
return | ||
} | ||
|
||
if inputDecimal.compare(orderDecimal) == .orderedDescending { | ||
let changeDue = inputDecimal.subtracting(orderDecimal) | ||
changeDueMessage = String.localizedStringWithFormat(Localization.changeDueMessage, formatAsCurrency(changeDue)) | ||
} else { | ||
changeDueMessage = nil | ||
} | ||
} | ||
|
||
private func validateAmountOnSubmit() -> Bool { | ||
guard let orderDecimal = parseCurrency(orderTotal), | ||
let inputDecimal = parseCurrency(textFieldAmountInput) else { | ||
errorMessage = Localization.failedToCollectCashPayment | ||
return false | ||
} | ||
|
||
if inputDecimal.compare(orderDecimal) == .orderedAscending { | ||
errorMessage = Localization.cashPaymentAmountNotEnough | ||
return false | ||
} | ||
errorMessage = nil | ||
return true | ||
} | ||
} | ||
|
||
private extension PointOfSaleCollectCashView { | ||
enum Constants { | ||
static let buttonSpacing: CGFloat = 12 | ||
|
@@ -147,10 +192,21 @@ private extension PointOfSaleCollectCashView { | |
comment: "Button to mark a cash payment as completed" | ||
) | ||
static let failedToCollectCashPayment = NSLocalizedString( | ||
"pointOfSale.cashview.failedToCollectCashPayment.draft", | ||
"pointOfSale.cashview.failedtocollectcashpayment.errormessage", | ||
value: "Error trying to process payment. Try again.", | ||
comment: "Error message when the system fails to collect a cash payment." | ||
) | ||
static let cashPaymentAmountNotEnough = NSLocalizedString( | ||
"pointOfSale.cashview.cashpaymentamountnotenough.errormessage", | ||
value: "Amount must be more or equal to total.", | ||
comment: "Error message when the cash amount entered is less than the order total." | ||
) | ||
static let changeDueMessage = NSLocalizedString( | ||
"pointOfSale.cashview.changedue", | ||
value: "Change due: %1$@", | ||
comment: "Change due when the cash amount entered exceeds the order total." + | ||
"Reads as 'Change due: $1.23'" | ||
) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've attempted to implement this from zero using
Decimal
but dealing with currency was anything but fun, so I've relied on our ownCurrencyFormatter
implementation. That said we seem to carry our own downsides (eg: #13829 ) so I'm not sure if there's a better way we could use to handle formatting here, since we use the same currency formatter in other places in POS 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, understandable. It's okay to use what we currently have. We can make improvements internally within the formatters.
The largest issues I've seen were when converting amounts back and forth when creating and then editing Order fields again. It's not the case with this POS cash payment so it should be fine.