2016-05-23 18:52:20 +02:00
|
|
|
<?php namespace App\Ninja\Datatables;
|
|
|
|
|
|
|
|
use Utils;
|
|
|
|
use URL;
|
|
|
|
use Auth;
|
|
|
|
|
|
|
|
class ExpenseDatatable extends EntityDatatable
|
|
|
|
{
|
|
|
|
public $entityType = ENTITY_EXPENSE;
|
|
|
|
|
|
|
|
public function columns()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'vendor_name',
|
|
|
|
function ($model)
|
|
|
|
{
|
|
|
|
if ($model->vendor_public_id) {
|
|
|
|
if(!Auth::user()->can('viewByOwner', [ENTITY_VENDOR, $model->vendor_user_id])){
|
|
|
|
return $model->vendor_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return link_to("vendors/{$model->vendor_public_id}", $model->vendor_name)->toHtml();
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
2016-05-23 20:03:01 +02:00
|
|
|
},
|
|
|
|
! $this->hideClient
|
2016-05-23 18:52:20 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'client_name',
|
|
|
|
function ($model)
|
|
|
|
{
|
|
|
|
if ($model->client_public_id) {
|
|
|
|
if(!Auth::user()->can('viewByOwner', [ENTITY_CLIENT, $model->client_user_id])){
|
|
|
|
return Utils::getClientDisplayName($model);
|
|
|
|
}
|
|
|
|
|
|
|
|
return link_to("clients/{$model->client_public_id}", Utils::getClientDisplayName($model))->toHtml();
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
2016-05-23 20:03:01 +02:00
|
|
|
},
|
|
|
|
! $this->hideClient
|
2016-05-23 18:52:20 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'expense_date',
|
|
|
|
function ($model) {
|
|
|
|
if(!Auth::user()->can('editByOwner', [ENTITY_EXPENSE, $model->user_id])){
|
|
|
|
return Utils::fromSqlDate($model->expense_date);
|
|
|
|
}
|
|
|
|
|
|
|
|
return link_to("expenses/{$model->public_id}/edit", Utils::fromSqlDate($model->expense_date))->toHtml();
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'amount',
|
|
|
|
function ($model) {
|
|
|
|
// show both the amount and the converted amount
|
|
|
|
if ($model->exchange_rate != 1) {
|
|
|
|
$converted = round($model->amount * $model->exchange_rate, 2);
|
|
|
|
return Utils::formatMoney($model->amount, $model->expense_currency_id) . ' | ' .
|
|
|
|
Utils::formatMoney($converted, $model->invoice_currency_id);
|
|
|
|
} else {
|
|
|
|
return Utils::formatMoney($model->amount, $model->expense_currency_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
2016-07-06 20:35:16 +02:00
|
|
|
[
|
|
|
|
'category',
|
|
|
|
function ($model) {
|
|
|
|
return $model->category != null ? substr($model->category, 0, 100) : '';
|
|
|
|
}
|
|
|
|
],
|
2016-05-23 18:52:20 +02:00
|
|
|
[
|
|
|
|
'public_notes',
|
|
|
|
function ($model) {
|
|
|
|
return $model->public_notes != null ? substr($model->public_notes, 0, 100) : '';
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'expense_status_id',
|
|
|
|
function ($model) {
|
2016-07-03 11:33:35 +02:00
|
|
|
return self::getStatusLabel($model->invoice_id, $model->should_be_invoiced, $model->balance);
|
2016-05-23 18:52:20 +02:00
|
|
|
}
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actions()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
trans('texts.edit_expense'),
|
|
|
|
function ($model) {
|
|
|
|
return URL::to("expenses/{$model->public_id}/edit") ;
|
|
|
|
},
|
|
|
|
function ($model) {
|
|
|
|
return Auth::user()->can('editByOwner', [ENTITY_EXPENSE, $model->user_id]);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
trans('texts.view_invoice'),
|
|
|
|
function ($model) {
|
|
|
|
return URL::to("/invoices/{$model->invoice_public_id}/edit");
|
|
|
|
},
|
|
|
|
function ($model) {
|
|
|
|
return $model->invoice_public_id && Auth::user()->can('editByOwner', [ENTITY_INVOICE, $model->invoice_user_id]);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
trans('texts.invoice_expense'),
|
|
|
|
function ($model) {
|
|
|
|
return "javascript:invoiceEntity({$model->public_id})";
|
|
|
|
},
|
|
|
|
function ($model) {
|
|
|
|
return ! $model->invoice_id && (!$model->deleted_at || $model->deleted_at == '0000-00-00') && Auth::user()->can('create', ENTITY_INVOICE);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-03 11:33:35 +02:00
|
|
|
private function getStatusLabel($invoiceId, $shouldBeInvoiced, $balance)
|
2016-05-23 18:52:20 +02:00
|
|
|
{
|
|
|
|
if ($invoiceId) {
|
2016-07-03 11:33:35 +02:00
|
|
|
if (floatval($balance)) {
|
|
|
|
$label = trans('texts.invoiced');
|
|
|
|
$class = 'default';
|
|
|
|
} else {
|
|
|
|
$label = trans('texts.paid');
|
2016-07-06 20:35:16 +02:00
|
|
|
$class = 'success';
|
2016-07-03 11:33:35 +02:00
|
|
|
}
|
2016-05-23 18:52:20 +02:00
|
|
|
} elseif ($shouldBeInvoiced) {
|
|
|
|
$label = trans('texts.pending');
|
|
|
|
$class = 'warning';
|
|
|
|
} else {
|
|
|
|
$label = trans('texts.logged');
|
|
|
|
$class = 'primary';
|
|
|
|
}
|
|
|
|
|
|
|
|
return "<h4><div class=\"label label-{$class}\">$label</div></h4>";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|