From f3370e521d67b3331ca51203d96571b09a6747ee Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Tue, 4 Sep 2018 21:16:01 -0700 Subject: [PATCH] Payments --- lib/data/repositories/payment_repository.dart | 7 +++- lib/redux/payment/payment_actions.dart | 40 ++++++++++++++++--- lib/redux/payment/payment_middleware.dart | 21 ++++++++++ lib/ui/payment/payment_list_vm.dart | 5 +++ lib/utils/localization.dart | 4 ++ 5 files changed, 70 insertions(+), 7 deletions(-) diff --git a/lib/data/repositories/payment_repository.dart b/lib/data/repositories/payment_repository.dart index 6d69612f0..13c78d89c 100644 --- a/lib/data/repositories/payment_repository.dart +++ b/lib/data/repositories/payment_repository.dart @@ -45,9 +45,12 @@ class PaymentRepository { } response = await webClient.post(url, company.token, json.encode(data)); } else { - var url = auth.url + '/payments/' + payment.id.toString(); + var url = '${auth.url}/payments/${payment.id}?'; + if (sendEmail) { + url += '&email_receipt=true'; + } if (action != null) { - url += '?action=' + action.toString(); + url += '&action=' + action.toString(); } response = await webClient.put(url, company.token, json.encode(data)); } diff --git a/lib/redux/payment/payment_actions.dart b/lib/redux/payment/payment_actions.dart index a6c885390..4c9b8bdae 100644 --- a/lib/redux/payment/payment_actions.dart +++ b/lib/redux/payment/payment_actions.dart @@ -6,12 +6,14 @@ import 'package:invoiceninja_flutter/redux/app/app_actions.dart'; class ViewPaymentList implements PersistUI { final BuildContext context; + ViewPaymentList(this.context); } class ViewPayment implements PersistUI { final int paymentId; final BuildContext context; + ViewPayment({this.paymentId, this.context}); } @@ -20,11 +22,14 @@ class EditPayment implements PersistUI { final BuildContext context; final Completer completer; final bool trackRoute; - EditPayment({this.payment, this.context, this.completer, this.trackRoute = true}); + + EditPayment( + {this.payment, this.context, this.completer, this.trackRoute = true}); } class UpdatePayment implements PersistUI { final PaymentEntity payment; + UpdatePayment(this.payment); } @@ -54,6 +59,7 @@ class LoadPaymentRequest implements StartLoading {} class LoadPaymentFailure implements StopLoading { final dynamic error; + LoadPaymentFailure(this.error); @override @@ -64,6 +70,7 @@ class LoadPaymentFailure implements StopLoading { class LoadPaymentSuccess implements StopLoading, PersistData { final PaymentEntity payment; + LoadPaymentSuccess(this.payment); @override @@ -76,6 +83,7 @@ class LoadPaymentsRequest implements StartLoading {} class LoadPaymentsFailure implements StopLoading { final dynamic error; + LoadPaymentsFailure(this.error); @override @@ -86,6 +94,7 @@ class LoadPaymentsFailure implements StopLoading { class LoadPaymentsSuccess implements StopLoading, PersistData { final BuiltList payments; + LoadPaymentsSuccess(this.payments); @override @@ -94,11 +103,10 @@ class LoadPaymentsSuccess implements StopLoading, PersistData { } } - - class SavePaymentRequest implements StartSaving { final Completer completer; final PaymentEntity payment; + SavePaymentRequest({this.completer, this.payment}); } @@ -110,12 +118,14 @@ class SavePaymentSuccess implements StopSaving, PersistData, PersistUI { class AddPaymentSuccess implements StopSaving, PersistData, PersistUI { final PaymentEntity payment; + AddPaymentSuccess(this.payment); } class SavePaymentFailure implements StopSaving { final Object error; - SavePaymentFailure (this.error); + + SavePaymentFailure(this.error); } class ArchivePaymentRequest implements StartSaving { @@ -127,11 +137,13 @@ class ArchivePaymentRequest implements StartSaving { class ArchivePaymentSuccess implements StopSaving, PersistData { final PaymentEntity payment; + ArchivePaymentSuccess(this.payment); } class ArchivePaymentFailure implements StopSaving { final PaymentEntity payment; + ArchivePaymentFailure(this.payment); } @@ -144,40 +156,59 @@ class DeletePaymentRequest implements StartSaving { class DeletePaymentSuccess implements StopSaving, PersistData { final PaymentEntity payment; + DeletePaymentSuccess(this.payment); } class DeletePaymentFailure implements StopSaving { final PaymentEntity payment; + DeletePaymentFailure(this.payment); } class RestorePaymentRequest implements StartSaving { final Completer completer; final int paymentId; + RestorePaymentRequest(this.completer, this.paymentId); } class RestorePaymentSuccess implements StopSaving, PersistData { final PaymentEntity payment; + RestorePaymentSuccess(this.payment); } class RestorePaymentFailure implements StopSaving { final PaymentEntity payment; + RestorePaymentFailure(this.payment); } +class EmailPaymentRequest implements StartSaving { + final Completer completer; + final PaymentEntity payment; + EmailPaymentRequest(this.completer, this.payment); +} +class EmailPaymentSuccess implements StopSaving, PersistData {} + +class EmailPaymentFailure implements StopSaving { + final dynamic error; + + EmailPaymentFailure(this.error); +} class FilterPayments { final String filter; + FilterPayments(this.filter); } class SortPayments implements PersistUI { final String field; + SortPayments(this.field); } @@ -205,4 +236,3 @@ class FilterPaymentsByEntity implements PersistUI { FilterPaymentsByEntity({this.entityId, this.entityType}); } - diff --git a/lib/redux/payment/payment_middleware.dart b/lib/redux/payment/payment_middleware.dart index 96190230d..b02b5737a 100644 --- a/lib/redux/payment/payment_middleware.dart +++ b/lib/redux/payment/payment_middleware.dart @@ -24,6 +24,7 @@ List> createStorePaymentsMiddleware([ final archivePayment = _archivePayment(repository); final deletePayment = _deletePayment(repository); final restorePayment = _restorePayment(repository); + final emailPayment = _emailPayment(repository); return [ TypedMiddleware(viewPaymentList), @@ -35,6 +36,7 @@ List> createStorePaymentsMiddleware([ TypedMiddleware(archivePayment), TypedMiddleware(deletePayment), TypedMiddleware(restorePayment), + TypedMiddleware(emailPayment), ]; } @@ -169,6 +171,25 @@ Middleware _savePayment(PaymentRepository repository) { }; } +Middleware _emailPayment(PaymentRepository repository) { + return (Store store, dynamic action, NextDispatcher next) { + repository + .saveData( + store.state.selectedCompany, store.state.authState, action.payment, sendEmail: true) + .then((PaymentEntity payment) { + store.dispatch(SavePaymentSuccess(payment)); + action.completer.complete(null); + }).catchError((Object error) { + print(error); + store.dispatch(SavePaymentFailure(error)); + action.completer.completeError(error); + }); + + next(action); + }; +} + + /* Middleware _loadPayment(PaymentRepository repository) { return (Store store, dynamic action, NextDispatcher next) { diff --git a/lib/ui/payment/payment_list_vm.dart b/lib/ui/payment/payment_list_vm.dart index 72052ed13..7205e7b22 100644 --- a/lib/ui/payment/payment_list_vm.dart +++ b/lib/ui/payment/payment_list_vm.dart @@ -96,6 +96,11 @@ class PaymentListVM { }, onEntityAction: (context, payment, action) { switch (action) { + case EntityAction.email: + store.dispatch(EmailPaymentRequest( + popCompleter(context, AppLocalization.of(context).emailedPayment), payment + )); + break; case EntityAction.restore: store.dispatch(RestorePaymentRequest( popCompleter( diff --git a/lib/utils/localization.dart b/lib/utils/localization.dart index 9ff1302d7..91a8fcbf7 100644 --- a/lib/utils/localization.dart +++ b/lib/utils/localization.dart @@ -118,6 +118,7 @@ class AppLocalization { 'deleted_invoice': 'Successfully deleted invoice', 'restored_invoice': 'Successfully restored invoice', 'emailed_invoice': 'Successfully emailed invoice', + 'emailed_payment': 'Successfully emailed payment', 'amount': 'Amount', 'invoice_number': 'Invoice Number', 'invoice_date': 'Invoice Date', @@ -7353,6 +7354,9 @@ class AppLocalization { String get emailedInvoice => _localizedValues[locale.languageCode]['emailed_invoice']; + String get emailedPayment => + _localizedValues[locale.languageCode]['emailed_payment']; + String get amount => _localizedValues[locale.languageCode]['amount']; String get invoiceNumber =>