From 7c1b451ae3c2bc29873198ed17abc17220eb03a5 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Tue, 8 May 2018 22:54:12 +0300 Subject: [PATCH] Fix expense datatable on client overview --- app/Http/Controllers/ExpenseController.php | 5 +++++ app/Ninja/Repositories/ExpenseRepository.php | 10 ++++++++++ app/Services/ExpenseService.php | 18 ++++++++++++++++++ resources/views/clients/show.blade.php | 7 +++++++ resources/views/list.blade.php | 2 +- resources/views/projects/show.blade.php | 1 + resources/views/vendors/show.blade.php | 1 + routes/web.php | 3 ++- 8 files changed, 45 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/ExpenseController.php b/app/Http/Controllers/ExpenseController.php index b02d6d9786..6edff813f0 100644 --- a/app/Http/Controllers/ExpenseController.php +++ b/app/Http/Controllers/ExpenseController.php @@ -69,6 +69,11 @@ class ExpenseController extends BaseController return $this->expenseService->getDatatableVendor($vendorPublicId); } + public function getDatatableClient($clientPublicId = null) + { + return $this->expenseService->getDatatableClient($clientPublicId); + } + public function create(ExpenseRequest $request) { if ($request->vendor_id != 0) { diff --git a/app/Ninja/Repositories/ExpenseRepository.php b/app/Ninja/Repositories/ExpenseRepository.php index 62505625f1..65cc4d0c6e 100644 --- a/app/Ninja/Repositories/ExpenseRepository.php +++ b/app/Ninja/Repositories/ExpenseRepository.php @@ -5,6 +5,7 @@ namespace App\Ninja\Repositories; use App\Models\Document; use App\Models\Expense; use App\Models\Vendor; +use App\Models\Client; use Auth; use DB; use Utils; @@ -42,6 +43,15 @@ class ExpenseRepository extends BaseRepository return $query; } + public function findClient($clientPublicId) + { + $clientId = Client::getPrivateId($clientPublicId); + + $query = $this->find()->where('expenses.client_id', '=', $clientId); + + return $query; + } + public function find($filter = null) { $accountid = \Auth::user()->account_id; diff --git a/app/Services/ExpenseService.php b/app/Services/ExpenseService.php index 1e80986e3d..58eec85510 100644 --- a/app/Services/ExpenseService.php +++ b/app/Services/ExpenseService.php @@ -96,4 +96,22 @@ class ExpenseService extends BaseService return $this->datatableService->createDatatable($datatable, $query); } + + /** + * @param $clientPublicId + * + * @return \Illuminate\Http\JsonResponse + */ + public function getDatatableClient($clientPublicId) + { + $datatable = new ExpenseDatatable(true, true); + + $query = $this->expenseRepo->findClient($clientPublicId); + + if (! Utils::hasPermission('view_all')) { + $query->where('expenses.user_id', '=', Auth::user()->id); + } + + return $this->datatableService->createDatatable($datatable, $query); + } } diff --git a/resources/views/clients/show.blade.php b/resources/views/clients/show.blade.php index 257582fdbf..920ade1efe 100644 --- a/resources/views/clients/show.blade.php +++ b/resources/views/clients/show.blade.php @@ -283,6 +283,7 @@ 'entityType' => ENTITY_TASK, 'datatable' => new \App\Ninja\Datatables\TaskDatatable(true, true), 'clientId' => $client->public_id, + 'url' => url('api/tasks/' . $client->public_id), ]) @endif @@ -293,6 +294,7 @@ 'entityType' => ENTITY_EXPENSE, 'datatable' => new \App\Ninja\Datatables\ExpenseDatatable(true, true), 'clientId' => $client->public_id, + 'url' => url('api/client_expenses/' . $client->public_id), ]) @endif @@ -303,6 +305,7 @@ 'entityType' => ENTITY_QUOTE, 'datatable' => new \App\Ninja\Datatables\InvoiceDatatable(true, true, ENTITY_QUOTE), 'clientId' => $client->public_id, + 'url' => url('api/quotes/' . $client->public_id), ]) @endif @@ -313,6 +316,7 @@ 'entityType' => ENTITY_RECURRING_INVOICE, 'datatable' => new \App\Ninja\Datatables\RecurringInvoiceDatatable(true, true), 'clientId' => $client->public_id, + 'url' => url('api/recurring_invoices/' . $client->public_id), ]) @endif @@ -322,6 +326,7 @@ 'entityType' => ENTITY_INVOICE, 'datatable' => new \App\Ninja\Datatables\InvoiceDatatable(true, true), 'clientId' => $client->public_id, + 'url' => url('api/invoices/' . $client->public_id), ]) @@ -330,6 +335,7 @@ 'entityType' => ENTITY_PAYMENT, 'datatable' => new \App\Ninja\Datatables\PaymentDatatable(true, true), 'clientId' => $client->public_id, + 'url' => url('api/payments/' . $client->public_id), ]) @@ -339,6 +345,7 @@ 'entityType' => ENTITY_CREDIT, 'datatable' => new \App\Ninja\Datatables\CreditDatatable(true, true), 'clientId' => $client->public_id, + 'url' => url('api/credits/' . $client->public_id), ]) @endif diff --git a/resources/views/list.blade.php b/resources/views/list.blade.php index 7198595787..8924ebc4e7 100644 --- a/resources/views/list.blade.php +++ b/resources/views/list.blade.php @@ -131,7 +131,7 @@ {!! Datatable::table() ->addColumn(Utils::trans($datatable->columnFields(), $datatable->entityType)) - ->setUrl(url('api/' . Utils::pluralizeEntityType($entityType) . '/' . (isset($clientId) ? (isset($projectId) ? ($clientId . '/' . $projectId) : $clientId) : (isset($vendorId) ? $vendorId : '')))) + ->setUrl(empty($url) ? url('api/' . Utils::pluralizeEntityType($entityType)) : $url) ->setCustomValues('entityType', Utils::pluralizeEntityType($entityType)) ->setCustomValues('clientId', isset($clientId) && $clientId && empty($projectId)) ->setOptions('sPaginationType', 'bootstrap') diff --git a/resources/views/projects/show.blade.php b/resources/views/projects/show.blade.php index 45fa8971c4..cf87f69e99 100644 --- a/resources/views/projects/show.blade.php +++ b/resources/views/projects/show.blade.php @@ -126,6 +126,7 @@ 'datatable' => new \App\Ninja\Datatables\ProjectTaskDatatable(true, true), 'projectId' => $project->public_id, 'clientId' => $project->client->public_id, + 'url' => url('api/tasks/' . $project->client->public_id . '/' . $project->public_id), ]) diff --git a/resources/views/vendors/show.blade.php b/resources/views/vendors/show.blade.php index 6a4ef2dbfe..7bb0f63ed7 100644 --- a/resources/views/vendors/show.blade.php +++ b/resources/views/vendors/show.blade.php @@ -190,6 +190,7 @@ 'entityType' => ENTITY_EXPENSE, 'datatable' => new \App\Ninja\Datatables\ExpenseDatatable(true, true), 'vendorId' => $vendor->public_id, + 'url' => url('api/vendor_expenses/' . $vendor->public_id), ]) diff --git a/routes/web.php b/routes/web.php index d7b4a10181..b8d76db876 100644 --- a/routes/web.php +++ b/routes/web.php @@ -270,7 +270,8 @@ Route::group(['middleware' => ['lookup:user', 'auth:user']], function () { Route::get('expenses/create/{vendor_id?}/{client_id?}/{category_id?}', 'ExpenseController@create'); Route::get('expenses/{expenses}/clone', 'ExpenseController@cloneExpense'); Route::get('api/expenses', 'ExpenseController@getDatatable'); - Route::get('api/expenses/{id}', 'ExpenseController@getDatatableVendor'); + Route::get('api/vendor_expenses/{id}', 'ExpenseController@getDatatableVendor'); + Route::get('api/client_expenses/{id}', 'ExpenseController@getDatatableClient'); Route::post('expenses/bulk', 'ExpenseController@bulk'); Route::get('expense_categories', 'ExpenseCategoryController@index'); Route::get('api/expense_categories', 'ExpenseCategoryController@getDatatable');