invoiceninja/app/Services/CreditService.php

99 lines
2.8 KiB
PHP
Raw Normal View History

2015-10-28 21:22:07 +02:00
<?php namespace App\Services;
2015-11-06 00:37:04 +02:00
use Utils;
use URL;
2016-03-17 10:11:14 -04:00
use Auth;
2015-10-28 21:22:07 +02:00
use App\Services\BaseService;
use App\Models\Client;
use App\Models\Payment;
2015-10-28 21:22:07 +02:00
use App\Ninja\Repositories\CreditRepository;
class CreditService extends BaseService
{
protected $creditRepo;
2015-11-06 00:37:04 +02:00
protected $datatableService;
2015-10-28 21:22:07 +02:00
2015-11-06 00:37:04 +02:00
public function __construct(CreditRepository $creditRepo, DatatableService $datatableService)
2015-10-28 21:22:07 +02:00
{
$this->creditRepo = $creditRepo;
2015-11-06 00:37:04 +02:00
$this->datatableService = $datatableService;
2015-10-28 21:22:07 +02:00
}
protected function getRepo()
{
return $this->creditRepo;
}
public function save($data)
{
return $this->creditRepo->save($data);
}
2015-11-06 00:37:04 +02:00
public function getDatatable($clientPublicId, $search)
{
$query = $this->creditRepo->find($clientPublicId, $search);
if(!Utils::hasPermission('view_all')){
2016-03-17 10:11:14 -04:00
$query->where('credits.user_id', '=', Auth::user()->id);
}
2015-11-06 00:37:04 +02:00
return $this->createDatatable(ENTITY_CREDIT, $query, !$clientPublicId);
}
protected function getDatatableColumns($entityType, $hideClient)
{
return [
[
'client_name',
function ($model) {
2016-04-25 21:53:39 -04:00
if(!Auth::user()->can('viewByOwner', [ENTITY_CLIENT, $model->client_user_id])){
return Utils::getClientDisplayName($model);
}
2016-03-02 15:36:42 +02:00
return $model->client_public_id ? link_to("clients/{$model->client_public_id}", Utils::getClientDisplayName($model))->toHtml() : '';
2015-11-06 00:37:04 +02:00
},
! $hideClient
],
[
'amount',
function ($model) {
return Utils::formatMoney($model->amount, $model->currency_id, $model->country_id) . '<span '.Utils::getEntityRowClass($model).'/>';
2015-11-06 00:37:04 +02:00
}
],
[
'balance',
function ($model) {
return Utils::formatMoney($model->balance, $model->currency_id, $model->country_id);
2015-11-06 00:37:04 +02:00
}
],
[
'credit_date',
function ($model) {
return Utils::fromSqlDate($model->credit_date);
}
],
[
'private_notes',
function ($model) {
return $model->private_notes;
}
]
];
}
protected function getDatatableActions($entityType)
{
return [
[
trans('texts.apply_credit'),
function ($model) {
return URL::to("payments/create/{$model->client_public_id}") . '?paymentTypeId=1';
},
function ($model) {
2016-04-25 21:53:39 -04:00
return Auth::user()->can('create', ENTITY_PAYMENT);
2015-11-06 00:37:04 +02:00
}
]
];
}
2015-10-28 21:22:07 +02:00
}