diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index 67bc81238b5a..97b46a90c60a 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -134,6 +134,11 @@ class PaymentController extends BaseController $actions[] = ['url' => url("/invoices/invoice_history/{$payment->invoice->public_id}?payment_id={$payment->public_id}"), 'label' => trans('texts.view_invoice')]; } $actions[] = ['url' => url("/invoices/{$payment->invoice->public_id}/edit"), 'label' => trans('texts.edit_invoice')]; + + if ($payment->canBeRefunded()) { + $actions[] = ['url' => "javascript:showRefundModal({$payment->public_id}, \"{$payment->getCompletedAmount()}\", \"{$payment->present()->amount}\", \"{$payment->present()->currencySymbol}\")", 'label' => trans('texts.refund_payment')]; + } + $actions[] = DropdownButton::DIVIDER; if (! $payment->trashed()) { $actions[] = ['url' => 'javascript:submitAction("archive")', 'label' => trans('texts.archive_payment')]; @@ -195,7 +200,7 @@ class PaymentController extends BaseController */ public function update(UpdatePaymentRequest $request) { - if (in_array($request->action, ['archive', 'delete', 'restore'])) { + if (in_array($request->action, ['archive', 'delete', 'restore', 'refund'])) { return self::bulk(); } diff --git a/app/Models/Payment.php b/app/Models/Payment.php index 6819e31cc1f1..b836bacba449 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -281,6 +281,11 @@ class Payment extends EntityModel return $this->amount - $this->refunded; } + public function canBeRefunded() + { + return $this->getCompletedAmount() > 0 && ($this->isCompleted() || $this->isPartiallyRefunded()); + } + /** * @return mixed|null|\stdClass|string */ diff --git a/app/Ninja/Presenters/PaymentPresenter.php b/app/Ninja/Presenters/PaymentPresenter.php index b7d6ba8f3fe2..1b7396bbbd2c 100644 --- a/app/Ninja/Presenters/PaymentPresenter.php +++ b/app/Ninja/Presenters/PaymentPresenter.php @@ -12,6 +12,11 @@ class PaymentPresenter extends EntityPresenter return Utils::formatMoney($this->entity->amount, $this->entity->client->currency_id); } + public function currencySymbol() + { + return Utils::getFromCache($this->entity->currency_id ? $this->entity->currency_id : DEFAULT_CURRENCY, 'currencies')->symbol; + } + public function client() { return $this->entity->client ? $this->entity->client->getDisplayName() : ''; diff --git a/resources/views/list.blade.php b/resources/views/list.blade.php index ebb90060534b..4897fc81fd46 100644 --- a/resources/views/list.blade.php +++ b/resources/views/list.blade.php @@ -73,37 +73,7 @@ ->render('datatable') !!} @if ($entityType == ENTITY_PAYMENT) - + @include('partials/refund_payment') @endif {!! Former::close() !!} @@ -143,21 +113,6 @@ } } - @if ($entityType == ENTITY_PAYMENT) - var paymentId = null; - function showRefundModal(id, amount, formatted, symbol){ - paymentId = id; - $('#refundCurrencySymbol').text(symbol); - $('#refundMax').text(formatted); - $('#refundAmount').val(amount).attr('max', amount); - $('#paymentRefundModal').modal('show'); - } - - function handleRefundClicked(){ - submitForm_{{ $entityType }}('refund', paymentId); - } - @endif - $(function() { // Handle datatable filtering @@ -210,10 +165,6 @@ actionListHandler(); } - @if ($entityType == ENTITY_PAYMENT) - $('#completeRefundButton').click(handleRefundClicked) - @endif - $('.listForm_{{ $entityType }} .archive, .invoice').prop('disabled', true); $('.listForm_{{ $entityType }} .archive:not(.dropdown-toggle)').click(function() { submitForm_{{ $entityType }}('archive'); diff --git a/resources/views/partials/refund_payment.blade.php b/resources/views/partials/refund_payment.blade.php new file mode 100644 index 000000000000..4420ebdb10be --- /dev/null +++ b/resources/views/partials/refund_payment.blade.php @@ -0,0 +1,52 @@ + + + + diff --git a/resources/views/payments/edit.blade.php b/resources/views/payments/edit.blade.php index 004f439057e9..6647f5f54599 100644 --- a/resources/views/payments/edit.blade.php +++ b/resources/views/payments/edit.blade.php @@ -92,6 +92,8 @@ + @include('partials/refund_payment') + {!! Former::close() !!}