From 9721491072c634e45c77df0a7ddc353b06826c6d Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 15 Feb 2018 11:53:51 +0200 Subject: [PATCH] Show credit amount on new payment form --- app/Http/Controllers/PaymentController.php | 21 ++++++++++++++++++--- resources/views/payments/edit.blade.php | 17 +++++++++++------ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index 7b8ad70817..e7da8b5367 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -89,16 +89,29 @@ class PaymentController extends BaseController */ public function create(PaymentRequest $request) { + $user = auth()->user(); + $account = $user->account; + $invoices = Invoice::scope() ->invoices() ->where('invoices.invoice_status_id', '!=', INVOICE_STATUS_PAID) ->with('client', 'invoice_status') ->orderBy('invoice_number')->get(); + $clientPublicId = Input::old('client') ? Input::old('client') : ($request->client_id ?: 0); + $invoicePublicId = Input::old('invoice') ? Input::old('invoice') : ($request->invoice_id ?: 0); + + $totalCredit = false; + if ($clientPublicId && $client = Client::scope($clientPublicId)->first()) { + $totalCredit = $account->formatMoney($client->getTotalCredit(), $client); + } elseif ($invoicePublicId && $invoice = Invoice::scope($invoicePublicId)->first()) { + $totalCredit = $account->formatMoney($invoice->client->getTotalCredit(), $client); + } + $data = [ 'account' => Auth::user()->account, - 'clientPublicId' => Input::old('client') ? Input::old('client') : ($request->client_id ?: 0), - 'invoicePublicId' => Input::old('invoice') ? Input::old('invoice') : ($request->invoice_id ?: 0), + 'clientPublicId' => $clientPublicId, + 'invoicePublicId' => $invoicePublicId, 'invoice' => null, 'invoices' => $invoices, 'payment' => null, @@ -106,7 +119,9 @@ class PaymentController extends BaseController 'url' => 'payments', 'title' => trans('texts.new_payment'), 'paymentTypeId' => Input::get('paymentTypeId'), - 'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), ]; + 'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), + 'totalCredit' => $totalCredit, + ]; return View::make('payments.edit', $data); } diff --git a/resources/views/payments/edit.blade.php b/resources/views/payments/edit.blade.php index 50638acda1..483ec017ef 100644 --- a/resources/views/payments/edit.blade.php +++ b/resources/views/payments/edit.blade.php @@ -162,6 +162,9 @@ } $(function() { + @if ($totalCredit) + $('#payment_type_id option:contains("{{ trans('texts.apply_credit') }}")').text("{{ trans('texts.apply_credit') }} | {{ $totalCredit}}"); + @endif @if (Input::old('data')) // this means we failed so we'll reload the previous state @@ -408,12 +411,14 @@ if (invoiceId) { var invoice = invoiceMap[invoiceId]; - var client = clientMap[invoice.client.public_id]; - invoice.client = client; - setComboboxValue($('.invoice-select'), invoice.public_id, (invoice.invoice_number + ' - ' + - invoice.invoice_status.name + ' - ' + getClientDisplayName(client) + ' - ' + - formatMoneyInvoice(invoice.amount, invoice) + ' | ' + formatMoneyInvoice(invoice.balance, invoice))); - $invoiceSelect.trigger('change'); + if (invoice) { + var client = clientMap[invoice.client.public_id]; + invoice.client = client; + setComboboxValue($('.invoice-select'), invoice.public_id, (invoice.invoice_number + ' - ' + + invoice.invoice_status.name + ' - ' + getClientDisplayName(client) + ' - ' + + formatMoneyInvoice(invoice.amount, invoice) + ' | ' + formatMoneyInvoice(invoice.balance, invoice))); + $invoiceSelect.trigger('change'); + } } else if (clientId) { var client = clientMap[clientId]; setComboboxValue($('.client-select'), client.public_id, getClientDisplayName(client));