diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index 766f8cdb2615..e590bae9eba7 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -224,25 +224,51 @@ class ClientController extends BaseController return $this->returnBulk(ENTITY_CLIENT, $action, $ids); } - public function statement() + public function statement($clientPublicId, $statusId = false, $startDate = false, $endDate = false) { $account = Auth::user()->account; + $statusId = intval($statusId); $client = Client::scope(request()->client_id)->with('contacts')->firstOrFail(); + + if (! $startDate) { + $startDate = Utils::today(false)->modify('-6 month')->format('Y-m-d'); + $endDate = Utils::today(false)->format('Y-m-d'); + } + $invoice = $account->createInvoice(ENTITY_INVOICE); $invoice->client = $client; $invoice->date_format = $account->date_format ? $account->date_format->format_moment : 'MMM D, YYYY'; - $invoice->invoice_items = Invoice::scope() + + $invoices = Invoice::scope() ->with(['client']) - ->whereClientId($client->id) ->invoices() + ->whereClientId($client->id) ->whereIsPublic(true) - ->where('balance', '>', 0) - ->get(); + ->orderBy('invoice_date', 'asc'); + + if ($statusId == INVOICE_STATUS_PAID) { + $invoices->where('invoice_status_id', '=', INVOICE_STATUS_PAID); + } elseif ($statusId == INVOICE_STATUS_UNPAID) { + $invoices->where('invoice_status_id', '!=', INVOICE_STATUS_PAID); + } + + if ($statusId == INVOICE_STATUS_PAID || ! $statusId) { + $invoices->where('invoice_date', '>=', $startDate) + ->where('invoice_date', '<=', $endDate); + } + + $invoice->invoice_items = $invoices->get(); + + if (request()->json) { + return json_encode($invoice); + } $data = [ 'showBreadcrumbs' => false, 'client' => $client, - 'invoice' => $invoice, + 'account' => $account, + 'startDate' => $startDate, + 'endDate' => $endDate, ]; return view('clients.statement', $data); diff --git a/app/Http/routes.php b/app/Http/routes.php index b6e6ca69e443..98f1b869c10c 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -144,7 +144,7 @@ Route::group(['middleware' => ['lookup:user', 'auth:user']], function () { Route::get('api/clients', 'ClientController@getDatatable'); Route::get('api/activities/{client_id?}', 'ActivityController@getDatatable'); Route::post('clients/bulk', 'ClientController@bulk'); - Route::get('clients/statement/{client_id}', 'ClientController@statement'); + Route::get('clients/statement/{client_id}/{status_id?}/{start_date?}/{end_date?}', 'ClientController@statement'); Route::resource('tasks', 'TaskController'); Route::get('api/tasks/{client_id?}', 'TaskController@getDatatable'); diff --git a/resources/views/clients/statement.blade.php b/resources/views/clients/statement.blade.php index 54aa8d17ca31..5f48db36780e 100644 --- a/resources/views/clients/statement.blade.php +++ b/resources/views/clients/statement.blade.php @@ -3,6 +3,9 @@ @section('head') @parent + + + @include('money_script') @foreach (Auth::user()->account->getFontFolders() as $font) @@ -13,8 +16,10 @@ var invoiceDesigns = {!! \App\Models\InvoiceDesign::getDesigns() !!}; var invoiceFonts = {!! Cache::get('fonts') !!}; - var currentInvoice = {!! $invoice !!}; - var invoice = {!! $invoice !!}; + + var statementStartDate = moment("{{ $startDate }}"); + var statementEndDate = moment("{{ $endDate }}"); + var dateRanges = {!! $account->present()->dateRangeOptions !!}; function getPDFString(cb) { @@ -40,9 +45,73 @@ } $(function() { - refreshPDF(); + if (isStorageSupported()) { + var lastRange = localStorage.getItem('last:statement_range'); + var lastStatusId = localStorage.getItem('last:statement_status_id'); + lastRange = dateRanges[lastRange]; + if (lastRange) { + statementStartDate = lastRange[0]; + statementEndDate = lastRange[1]; + } + if (lastStatusId) { + $('#status_id').val(lastStatusId); + } + } + + // Initialize date range selector + function cb(start, end, label) { + statementStartDate = start; + statementEndDate = end; + $('#reportrange span').html(start.format('{{ $account->getMomentDateFormat() }}') + ' - ' + end.format('{{ $account->getMomentDateFormat() }}')); + $('#start_date').val(start.format('YYYY-MM-DD')); + $('#end_date').val(end.format('YYYY-MM-DD')); + + if (isStorageSupported() && label && label != "{{ trans('texts.custom_range') }}") { + localStorage.setItem('last:statement_range', label); + } + + refreshData(); + } + + $('#reportrange').daterangepicker({ + locale: { + format: "{{ $account->getMomentDateFormat() }}", + customRangeLabel: "{{ trans('texts.custom_range') }}", + }, + startDate: statementStartDate, + endDate: statementEndDate, + linkedCalendars: false, + ranges: dateRanges, + }, cb); + + cb(statementStartDate, statementEndDate); }); + function refreshData() { + var statusId = $('#status_id').val(); + if (statusId == {{ INVOICE_STATUS_UNPAID }}) { + $('#reportrange').css('color', '#AAA'); + $('#reportrange').css('pointer-events', 'none'); + } else { + $('#reportrange').css('color', '#000'); + $('#reportrange').css('pointer-events', 'auto'); + } + var url = "{!! url('/clients/statement/' . $client->public_id) !!}/" + statusId + '/' + + statementStartDate.format('YYYY-MM-DD') + '/' + statementEndDate.format('YYYY-MM-DD') + '?json=true'; + $.get(url, function(response) { + invoice = currentInvoice = JSON.parse(response); + refreshPDF(); + }); + } + + function onStatusChange() { + if (isStorageSupported()) { + localStorage.setItem('last:statement_status_id', $('#status_id').val()); + } + + refreshData(); + } + function onDownloadClick() { var doc = generatePDF(invoice, invoiceDesigns[0].javascript, true); doc.save("{{ str_replace(' ', '_', trim($client->getDisplayName())) . '-' . trans('texts.statement') }}" + '.pdf'); @@ -70,6 +139,39 @@

 

 

+
+ {!! Former::inline_open() !!} + + {{ trans('texts.status') }} + +    + + {!! Former::select('status_id') + ->onchange('onStatusChange()') + ->label('status') + ->addOption(trans('texts.unpaid'), INVOICE_STATUS_UNPAID) + ->addOption(trans('texts.paid'), INVOICE_STATUS_PAID) + ->addOption(trans('texts.all'), 'false') !!} + +      + + {{ trans('texts.date_range') }} + +    + + +   + + + +
+ {!! Former::text('start_date') !!} + {!! Former::text('end_date') !!} +
+ + {!! Former::close() !!} +
+ @include('invoices.pdf', ['account' => Auth::user()->account, 'pdfHeight' => 800]) @stop