diff --git a/app/Http/Controllers/ClientPortalController.php b/app/Http/Controllers/ClientPortalController.php index e17d1eaafd44..96a3ec2835f9 100644 --- a/app/Http/Controllers/ClientPortalController.php +++ b/app/Http/Controllers/ClientPortalController.php @@ -22,6 +22,7 @@ use App\Ninja\Repositories\InvoiceRepository; use App\Ninja\Repositories\PaymentRepository; use App\Ninja\Repositories\ActivityRepository; use App\Ninja\Repositories\DocumentRepository; +use App\Ninja\Repositories\CreditRepository; use App\Events\InvoiceInvitationWasViewed; use App\Events\QuoteInvitationWasViewed; use App\Services\PaymentService; @@ -33,13 +34,14 @@ class ClientPortalController extends BaseController private $paymentRepo; private $documentRepo; - public function __construct(InvoiceRepository $invoiceRepo, PaymentRepository $paymentRepo, ActivityRepository $activityRepo, DocumentRepository $documentRepo, PaymentService $paymentService) + public function __construct(InvoiceRepository $invoiceRepo, PaymentRepository $paymentRepo, ActivityRepository $activityRepo, DocumentRepository $documentRepo, PaymentService $paymentService, CreditRepository $creditRepo) { $this->invoiceRepo = $invoiceRepo; $this->paymentRepo = $paymentRepo; $this->activityRepo = $activityRepo; $this->documentRepo = $documentRepo; $this->paymentService = $paymentService; + $this->creditRepo = $creditRepo; } public function view($invitationKey) @@ -439,6 +441,40 @@ class ClientPortalController extends BaseController return $this->invoiceRepo->getClientDatatable($contact->id, ENTITY_QUOTE, Input::get('sSearch')); } + public function creditIndex() + { + if (!$contact = $this->getContact()) { + return $this->returnError(); + } + + $account = $contact->account; + + if (!$account->enable_client_portal) { + return $this->returnError(); + } + + $color = $account->primary_color ? $account->primary_color : '#0b4d78'; + $data = [ + 'color' => $color, + 'account' => $account, + 'clientFontUrl' => $account->getFontsUrl(), + 'title' => trans('texts.credits'), + 'entityType' => ENTITY_CREDIT, + 'columns' => Utils::trans(['credit_date', 'credit_amount', 'credit_balance']), + ]; + + return response()->view('public_list', $data); + } + + public function creditDatatable() + { + if (!$contact = $this->getContact()) { + return false; + } + + return $this->creditRepo->getClientDatatable($contact->client_id); + } + public function documentIndex() { if (!$contact = $this->getContact()) { diff --git a/app/Http/ViewComposers/ClientPortalHeaderComposer.php b/app/Http/ViewComposers/ClientPortalHeaderComposer.php index 1d21070fe75a..ac0d6ca68618 100644 --- a/app/Http/ViewComposers/ClientPortalHeaderComposer.php +++ b/app/Http/ViewComposers/ClientPortalHeaderComposer.php @@ -2,8 +2,10 @@ namespace App\Http\ViewComposers; +use DB; use Cache; use Illuminate\View\View; +use App\Models\Contact; /** * ClientPortalHeaderComposer.php. @@ -21,6 +23,30 @@ class ClientPortalHeaderComposer */ public function compose(View $view) { - $view->with('testing', 'value'); + $contactKey = session('contact_key'); + + if ( ! $contactKey) { + return false; + } + + $contact = Contact::where('contact_key', '=', $contactKey) + ->with('client') + ->first(); + + if ( ! $contact || $contact->is_deleted) { + return false; + } + + $client = $contact->client; + + $hasDocuments = DB::table('invoices') + ->where('invoices.client_id', '=', $client->id) + ->whereNull('invoices.deleted_at') + ->join('documents', 'documents.invoice_id', '=', 'invoices.id') + ->count(); + + $view->with('hasQuotes', $client->quotes->count()); + $view->with('hasCredits', $client->creditsWithBalance->count()); + $view->with('hasDocuments', $hasDocuments); } } diff --git a/app/Http/routes.php b/app/Http/routes.php index 162e30267de1..42bfe2c44f80 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -52,6 +52,7 @@ Route::group(['middleware' => 'auth:client'], function() { Route::post('client/payment_methods/default', 'ClientPortalController@setDefaultPaymentMethod'); Route::post('client/payment_methods/{source_id}/remove', 'ClientPortalController@removePaymentMethod'); Route::get('client/quotes', 'ClientPortalController@quoteIndex'); + Route::get('client/credits', 'ClientPortalController@creditIndex'); Route::get('client/invoices', 'ClientPortalController@invoiceIndex'); Route::get('client/invoices/recurring', 'ClientPortalController@recurringInvoiceIndex'); Route::post('client/invoices/auto_bill', 'ClientPortalController@setAutoBill'); @@ -63,6 +64,7 @@ Route::group(['middleware' => 'auth:client'], function() { Route::get('client/documents/{invitation_key}/{filename?}', 'ClientPortalController@getInvoiceDocumentsZip'); Route::get('api/client.quotes', ['as'=>'api.client.quotes', 'uses'=>'ClientPortalController@quoteDatatable']); + Route::get('api/client.credits', ['as'=>'api.client.credits', 'uses'=>'ClientPortalController@creditDatatable']); Route::get('api/client.invoices', ['as'=>'api.client.invoices', 'uses'=>'ClientPortalController@invoiceDatatable']); Route::get('api/client.recurring_invoices', ['as'=>'api.client.recurring_invoices', 'uses'=>'ClientPortalController@recurringInvoiceDatatable']); Route::get('api/client.documents', ['as'=>'api.client.documents', 'uses'=>'ClientPortalController@documentDatatable']); diff --git a/app/Models/Client.php b/app/Models/Client.php index feef11877984..7ab4ecac0a4c 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -159,6 +159,14 @@ class Client extends EntityModel return $this->hasMany('App\Models\Invoice'); } + /** + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function quotes() + { + return $this->hasMany('App\Models\Invoice')->where('invoice_type_id', '=', INVOICE_TYPE_QUOTE); + } + /** * @return \Illuminate\Database\Eloquent\Relations\HasMany */ @@ -223,6 +231,14 @@ class Client extends EntityModel return $this->hasMany('App\Models\Credit'); } + /** + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function creditsWithBalance() + { + return $this->hasMany('App\Models\Credit')->where('balance', '>', 0); + } + /** * @return mixed */ diff --git a/app/Ninja/Repositories/CreditRepository.php b/app/Ninja/Repositories/CreditRepository.php index c295f56d8c85..e20be664eae5 100644 --- a/app/Ninja/Repositories/CreditRepository.php +++ b/app/Ninja/Repositories/CreditRepository.php @@ -58,10 +58,36 @@ class CreditRepository extends BaseRepository return $query; } + public function getClientDatatable($clientId) + { + $query = DB::table('credits') + ->join('accounts', 'accounts.id', '=', 'credits.account_id') + ->join('clients', 'clients.id', '=', 'credits.client_id') + ->where('credits.client_id', '=', $clientId) + ->where('clients.deleted_at', '=', null) + ->where('credits.deleted_at', '=', null) + ->where('credits.balance', '>', 0) + ->select( + DB::raw('COALESCE(clients.currency_id, accounts.currency_id) currency_id'), + DB::raw('COALESCE(clients.country_id, accounts.country_id) country_id'), + 'credits.amount', + 'credits.balance', + 'credits.credit_date' + ); + + $table = \Datatable::query($query) + ->addColumn('credit_date', function ($model) { return Utils::fromSqlDate($model->credit_date); }) + ->addColumn('amount', function ($model) { return Utils::formatMoney($model->amount, $model->currency_id, $model->country_id); }) + ->addColumn('balance', function ($model) { return Utils::formatMoney($model->balance, $model->currency_id, $model->country_id); }) + ->make(); + + return $table; + } + public function save($input, $credit = null) { $publicId = isset($data['public_id']) ? $data['public_id'] : false; - + if ($credit) { // do nothing } elseif ($publicId) { diff --git a/app/Providers/ComposerServiceProvider.php b/app/Providers/ComposerServiceProvider.php index 972e690a395d..17aeef6caf60 100644 --- a/app/Providers/ComposerServiceProvider.php +++ b/app/Providers/ComposerServiceProvider.php @@ -34,7 +34,7 @@ class ComposerServiceProvider extends ServiceProvider view()->composer( [ - 'invited.dashboard', + 'public.header' ], 'App\Http\ViewComposers\ClientPortalHeaderComposer' ); diff --git a/resources/views/public/header.blade.php b/resources/views/public/header.blade.php index e0213abc7186..8d376429a0bb 100644 --- a/resources/views/public/header.blade.php +++ b/resources/views/public/header.blade.php @@ -76,13 +76,17 @@ {!! link_to('/client/dashboard', trans('texts.dashboard') ) !!} @endif -