diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index 69383e337194..e0e92015920e 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -236,7 +236,6 @@ class PaymentController extends BaseController public function bulk() { $action = Input::get('action'); - $amount = Input::get('refund_amount'); $ids = Input::get('public_id') ? Input::get('public_id') : Input::get('ids'); if ($action === 'email') { @@ -244,7 +243,10 @@ class PaymentController extends BaseController $this->contactMailer->sendPaymentConfirmation($payment); Session::flash('message', trans('texts.emailed_payment')); } else { - $count = $this->paymentService->bulk($ids, $action, ['refund_amount' => $amount]); + $count = $this->paymentService->bulk($ids, $action, [ + 'refund_amount' => Input::get('refund_amount'), + 'refund_email' => Input::get('refund_email'), + ]); if ($count > 0) { $message = Utils::pluralize($action == 'refund' ? 'refunded_payment' : $action.'d_payment', $count); Session::flash('message', $message); diff --git a/app/Ninja/Mailers/ContactMailer.php b/app/Ninja/Mailers/ContactMailer.php index 90b51b76c66c..5507ef2ccfce 100644 --- a/app/Ninja/Mailers/ContactMailer.php +++ b/app/Ninja/Mailers/ContactMailer.php @@ -231,17 +231,25 @@ class ContactMailer extends Mailer /** * @param Payment $payment */ - public function sendPaymentConfirmation(Payment $payment) + public function sendPaymentConfirmation(Payment $payment, $refunded = 0) { $account = $payment->account; $client = $payment->client; $account->loadLocalizationSettings($client); - $invoice = $payment->invoice; $accountName = $account->getDisplayName(); - $emailTemplate = $account->getEmailTemplate(ENTITY_PAYMENT); - $emailSubject = $invoice->account->getEmailSubject(ENTITY_PAYMENT); + + if ($refunded > 0) { + $emailSubject = trans('texts.refund_subject'); + $emailTemplate = trans('texts.refund_body', [ + 'amount' => $account->formatMoney($refunded, $client), + 'invoice_number' => $invoice->invoice_number, + ]); + } else { + $emailSubject = $invoice->account->getEmailSubject(ENTITY_PAYMENT); + $emailTemplate = $account->getEmailTemplate(ENTITY_PAYMENT); + } if ($payment->invitation) { $user = $payment->invitation->user; @@ -270,9 +278,10 @@ class ContactMailer extends Mailer 'entityType' => ENTITY_INVOICE, 'bccEmail' => $account->getBccEmail(), 'fromEmail' => $account->getFromEmail(), + 'isRefund' => $refunded > 0, ]; - if ($account->attachPDF()) { + if (! $refunded && $account->attachPDF()) { $data['pdfString'] = $invoice->getPDFString(); $data['pdfFileName'] = $invoice->getFileName(); } diff --git a/app/Services/PaymentService.php b/app/Services/PaymentService.php index 1c8f5b97fe5d..7e12baaeec59 100644 --- a/app/Services/PaymentService.php +++ b/app/Services/PaymentService.php @@ -180,17 +180,28 @@ class PaymentService extends BaseService foreach ($payments as $payment) { if (Auth::user()->can('edit', $payment)) { $amount = ! empty($params['refund_amount']) ? floatval($params['refund_amount']) : null; + $sendEmail = ! empty($params['refund_email']) ? boolval($params['refund_email']) : false; $paymentDriver = false; + $refunded = false; + if ($accountGateway = $payment->account_gateway) { $paymentDriver = $accountGateway->paymentDriver(); } + if ($paymentDriver && $paymentDriver->canRefundPayments) { if ($paymentDriver->refundPayment($payment, $amount)) { $successful++; + $refunded = true; } } else { $payment->recordRefund($amount); $successful++; + $refunded = true; + } + + if ($refunded && $sendEmail) { + $mailer = app('App\Ninja\Mailers\ContactMailer'); + $mailer->sendPaymentConfirmation($payment, $amount); } } } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 15a9c76fbc33..78311607aa88 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2317,6 +2317,9 @@ $LANG = array( 'downloaded_quotes' => 'An email will be sent with the quote PDFs', 'clone_expense' => 'Clone Expense', 'default_documents' => 'Default Documents', + 'send_email_to_client' => 'Send email to the client', + 'refund_subject' => 'Refund Processed', + 'refund_body' => 'You have been processed a refund of :amount for invoice :invoice_number.', ); diff --git a/resources/views/emails/design2_html.blade.php b/resources/views/emails/design2_html.blade.php index b203784ecdfc..e707c21a8822 100644 --- a/resources/views/emails/design2_html.blade.php +++ b/resources/views/emails/design2_html.blade.php @@ -40,12 +40,14 @@

- - {{ trans('texts.' . $invoice->present()->balanceDueLabel) }}: -
- - {{ $account->formatMoney($invoice->getRequestedAmount(), $client) }} - + @if (! isset($isRefund) || ! $isRefund) + + {{ trans('texts.' . $invoice->present()->balanceDueLabel) }}: +
+ + {{ $account->formatMoney($invoice->getRequestedAmount(), $client) }} + + @endif

diff --git a/resources/views/emails/design3_html.blade.php b/resources/views/emails/design3_html.blade.php index 7ccba719cca0..ab1b0f33ca2c 100644 --- a/resources/views/emails/design3_html.blade.php +++ b/resources/views/emails/design3_html.blade.php @@ -40,12 +40,14 @@

- - {{ strtoupper(trans('texts.' . $invoice->present()->balanceDueLabel)) }}: -
- - {{ $account->formatMoney($invoice->getRequestedAmount(), $client) }} - + @if (! isset($isRefund) || ! $isRefund) + + {{ strtoupper(trans('texts.' . $invoice->present()->balanceDueLabel)) }}: +
+ + {{ $account->formatMoney($invoice->getRequestedAmount(), $client) }} + + @endif

diff --git a/resources/views/partials/refund_payment.blade.php b/resources/views/partials/refund_payment.blade.php index 8a56437aaa20..aef759b32160 100644 --- a/resources/views/partials/refund_payment.blade.php +++ b/resources/views/partials/refund_payment.blade.php @@ -20,6 +20,17 @@
{{ trans('texts.refund_max') }}
+
+ +
+
+ {!! Former::checkbox('refund_email') + ->text('send_email_to_client') + ->raw() !!} +
+
+
+ @@ -45,12 +56,28 @@ $('#paymentRefundModal').modal('show'); } - function handleRefundClicked(){ + function onRefundClicked(){ + $('#completeRefundButton').prop('disabled', true); submitForm_payment('refund', paymentId); } + function onRefundEmailChange() { + if (! isStorageSupported()) { + return; + } + var checked = $('#refund_email').is(':checked'); + localStorage.setItem('last:send_refund_email', checked ? true : ''); + } + $(function() { - $('#completeRefundButton').click(handleRefundClicked); + $('#completeRefundButton').click(onRefundClicked); + $('#refund_email').click(onRefundEmailChange); + + if (isStorageSupported()) { + if (localStorage.getItem('last:send_refund_email')) { + $('#refund_email').prop('checked', true); + } + } })