2015-10-28 20:22:07 +01:00
|
|
|
<?php namespace App\Services;
|
|
|
|
|
2015-11-05 23:37:04 +01:00
|
|
|
use Utils;
|
|
|
|
use URL;
|
|
|
|
use Auth;
|
2015-10-28 20:22:07 +01:00
|
|
|
use App\Services\BaseService;
|
2016-03-16 00:08:00 +01:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Credit;
|
|
|
|
use App\Models\Expense;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\Task;
|
2015-10-28 20:22:07 +01:00
|
|
|
use App\Ninja\Repositories\ClientRepository;
|
2015-11-09 20:24:22 +01:00
|
|
|
use App\Ninja\Repositories\NinjaRepository;
|
2015-10-28 20:22:07 +01:00
|
|
|
|
|
|
|
class ClientService extends BaseService
|
|
|
|
{
|
|
|
|
protected $clientRepo;
|
2015-11-05 23:37:04 +01:00
|
|
|
protected $datatableService;
|
2015-10-28 20:22:07 +01:00
|
|
|
|
2015-11-09 20:24:22 +01:00
|
|
|
public function __construct(ClientRepository $clientRepo, DatatableService $datatableService, NinjaRepository $ninjaRepo)
|
2015-10-28 20:22:07 +01:00
|
|
|
{
|
|
|
|
$this->clientRepo = $clientRepo;
|
2015-11-09 20:24:22 +01:00
|
|
|
$this->ninjaRepo = $ninjaRepo;
|
2015-11-05 23:37:04 +01:00
|
|
|
$this->datatableService = $datatableService;
|
2015-10-28 20:22:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getRepo()
|
|
|
|
{
|
|
|
|
return $this->clientRepo;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function save($data)
|
|
|
|
{
|
2016-04-19 04:35:18 +02:00
|
|
|
if (Auth::user()->account->isNinjaAccount() && isset($data['plan'])) {
|
|
|
|
$this->ninjaRepo->updatePlanDetails($data['public_id'], $data);
|
2015-11-09 20:24:22 +01:00
|
|
|
}
|
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
return $this->clientRepo->save($data);
|
|
|
|
}
|
2015-11-05 23:37:04 +01:00
|
|
|
|
|
|
|
public function getDatatable($search)
|
|
|
|
{
|
|
|
|
$query = $this->clientRepo->find($search);
|
|
|
|
|
2016-03-16 00:08:00 +01:00
|
|
|
if(!Utils::hasPermission('view_all')){
|
|
|
|
$query->where('clients.user_id', '=', Auth::user()->id);
|
|
|
|
}
|
|
|
|
|
2015-11-05 23:37:04 +01:00
|
|
|
return $this->createDatatable(ENTITY_CLIENT, $query);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getDatatableColumns($entityType, $hideClient)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'name',
|
|
|
|
function ($model) {
|
2016-03-02 14:36:42 +01:00
|
|
|
return link_to("clients/{$model->public_id}", $model->name ?: '')->toHtml();
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'first_name',
|
|
|
|
function ($model) {
|
2016-03-02 14:36:42 +01:00
|
|
|
return link_to("clients/{$model->public_id}", $model->first_name.' '.$model->last_name)->toHtml();
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'email',
|
|
|
|
function ($model) {
|
2016-03-02 14:36:42 +01:00
|
|
|
return link_to("clients/{$model->public_id}", $model->email ?: '')->toHtml();
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'clients.created_at',
|
|
|
|
function ($model) {
|
|
|
|
return Utils::timestampToDateString(strtotime($model->created_at));
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'last_login',
|
|
|
|
function ($model) {
|
|
|
|
return Utils::timestampToDateString(strtotime($model->last_login));
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'balance',
|
|
|
|
function ($model) {
|
2015-12-07 14:34:55 +01:00
|
|
|
return Utils::formatMoney($model->balance, $model->currency_id, $model->country_id);
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getDatatableActions($entityType)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
trans('texts.edit_client'),
|
|
|
|
function ($model) {
|
|
|
|
return URL::to("clients/{$model->public_id}/edit");
|
2016-03-16 00:08:00 +01:00
|
|
|
},
|
|
|
|
function ($model) {
|
|
|
|
return Client::canEditItem($model);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'--divider--', function(){return false;},
|
|
|
|
function ($model) {
|
|
|
|
return Client::canEditItem($model) && (Task::canCreate() || Invoice::canCreate());
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
trans('texts.new_task'),
|
|
|
|
function ($model) {
|
|
|
|
return URL::to("tasks/create/{$model->public_id}");
|
2016-03-16 00:08:00 +01:00
|
|
|
},
|
|
|
|
function ($model) {
|
|
|
|
return Task::canCreate();
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
trans('texts.new_invoice'),
|
|
|
|
function ($model) {
|
|
|
|
return URL::to("invoices/create/{$model->public_id}");
|
2016-03-16 00:08:00 +01:00
|
|
|
},
|
|
|
|
function ($model) {
|
|
|
|
return Invoice::canCreate();
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
trans('texts.new_quote'),
|
|
|
|
function ($model) {
|
|
|
|
return URL::to("quotes/create/{$model->public_id}");
|
|
|
|
},
|
|
|
|
function ($model) {
|
2016-04-19 04:35:18 +02:00
|
|
|
return Auth::user()->hasFeature(FEATURE_QUOTES) && Invoice::canCreate();
|
2016-03-16 00:08:00 +01:00
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'--divider--', function(){return false;},
|
|
|
|
function ($model) {
|
|
|
|
return (Task::canCreate() || Invoice::canCreate()) && (Payment::canCreate() || Credit::canCreate() || Expense::canCreate());
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
trans('texts.enter_payment'),
|
|
|
|
function ($model) {
|
|
|
|
return URL::to("payments/create/{$model->public_id}");
|
2016-03-16 00:08:00 +01:00
|
|
|
},
|
|
|
|
function ($model) {
|
|
|
|
return Payment::canCreate();
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
trans('texts.enter_credit'),
|
|
|
|
function ($model) {
|
|
|
|
return URL::to("credits/create/{$model->public_id}");
|
2016-03-16 00:08:00 +01:00
|
|
|
},
|
|
|
|
function ($model) {
|
|
|
|
return Credit::canCreate();
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
2016-01-21 20:15:30 +01:00
|
|
|
],
|
|
|
|
[
|
|
|
|
trans('texts.enter_expense'),
|
|
|
|
function ($model) {
|
|
|
|
return URL::to("expenses/create/0/{$model->public_id}");
|
2016-03-16 00:08:00 +01:00
|
|
|
},
|
|
|
|
function ($model) {
|
|
|
|
return Expense::canCreate();
|
2016-01-21 20:15:30 +01:00
|
|
|
}
|
2015-11-05 23:37:04 +01:00
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|