Prevent negative payment

This commit is contained in:
Hillel Coren 2020-10-16 15:49:02 +03:00
parent 95ffdb778c
commit 2a3018d71c
4 changed files with 20 additions and 4 deletions

View File

@ -20,7 +20,7 @@ class ErrorDialog extends StatelessWidget {
final store = StoreProvider.of<AppState>(context); final store = StoreProvider.of<AppState>(context);
return AlertDialog( return AlertDialog(
title: Text(localization.anErrorOccurred), title: Text(localization.error),
content: error != null ? Text(error.toString()) : SizedBox(), content: error != null ? Text(error.toString()) : SizedBox(),
actions: [ actions: [
if (clearErrorOnDismiss && !Config.DEMO_MODE) if (clearErrorOnDismiss && !Config.DEMO_MODE)

View File

@ -53,11 +53,9 @@ class _ClientEditState extends State<ClientEdit>
onSavePressed: (context) { onSavePressed: (context) {
final bool isValid = _formKey.currentState.validate(); final bool isValid = _formKey.currentState.validate();
/*
setState(() { setState(() {
autoValidate = !isValid ?? false; //autoValidate = !isValid;
}); });
*/
if (!isValid) { if (!isValid) {
return; return;

View File

@ -8,6 +8,7 @@ import 'package:invoiceninja_flutter/redux/ui/pref_state.dart';
import 'package:invoiceninja_flutter/redux/ui/ui_actions.dart'; import 'package:invoiceninja_flutter/redux/ui/ui_actions.dart';
import 'package:invoiceninja_flutter/ui/app/dialogs/error_dialog.dart'; import 'package:invoiceninja_flutter/ui/app/dialogs/error_dialog.dart';
import 'package:invoiceninja_flutter/ui/payment/view/payment_view_vm.dart'; import 'package:invoiceninja_flutter/ui/payment/view/payment_view_vm.dart';
import 'package:invoiceninja_flutter/utils/localization.dart';
import 'package:invoiceninja_flutter/utils/platforms.dart'; import 'package:invoiceninja_flutter/utils/platforms.dart';
import 'package:redux/redux.dart'; import 'package:redux/redux.dart';
import 'package:invoiceninja_flutter/redux/payment/payment_actions.dart'; import 'package:invoiceninja_flutter/redux/payment/payment_actions.dart';
@ -70,6 +71,18 @@ class PaymentEditVM {
store.dispatch(UpdateCurrentRoute(state.uiState.previousRoute)); store.dispatch(UpdateCurrentRoute(state.uiState.previousRoute));
}, },
onSavePressed: (BuildContext context) { onSavePressed: (BuildContext context) {
double amount = 0;
payment.invoices.forEach((invoice) => amount += invoice.amount);
payment.credits.forEach((credit) => amount -= credit.amount);
if (amount < 0) {
showDialog<ErrorDialog>(
context: context,
builder: (BuildContext context) {
return ErrorDialog(
AppLocalization.of(context).negativePaymentError);
});
return null;
}
final Completer<PaymentEntity> completer = Completer<PaymentEntity>(); final Completer<PaymentEntity> completer = Completer<PaymentEntity>();
store.dispatch( store.dispatch(
SavePaymentRequest(completer: completer, payment: payment)); SavePaymentRequest(completer: completer, payment: payment));

View File

@ -15,6 +15,8 @@ mixin LocalizationsProvider on LocaleCodeAware {
static final Map<String, Map<String, String>> _localizedValues = { static final Map<String, Map<String, String>> _localizedValues = {
'en': { 'en': {
// STARTER: lang key - do not remove comment // STARTER: lang key - do not remove comment
'negative_payment_error':
'The credit amount cannot exceed the payment amount',
'view_changes': 'View Changes', 'view_changes': 'View Changes',
'force_update': 'Force Update', 'force_update': 'Force Update',
'force_update_help': 'force_update_help':
@ -4673,6 +4675,9 @@ mixin LocalizationsProvider on LocaleCodeAware {
String get viewChanges => _localizedValues[localeCode]['view_changes'] ?? ''; String get viewChanges => _localizedValues[localeCode]['view_changes'] ?? '';
String get negativePaymentError =>
_localizedValues[localeCode]['negative_payment_error'] ?? '';
String lookup(String key) { String lookup(String key) {
final lookupKey = toSnakeCase(key); final lookupKey = toSnakeCase(key);