2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Ninja\Datatables;
|
2016-05-23 18:52:20 +02:00
|
|
|
|
2016-12-26 19:09:49 +01:00
|
|
|
use App\Models\Expense;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Auth;
|
|
|
|
use URL;
|
|
|
|
use Utils;
|
2016-05-23 18:52:20 +02:00
|
|
|
|
|
|
|
class ExpenseDatatable extends EntityDatatable
|
|
|
|
{
|
|
|
|
public $entityType = ENTITY_EXPENSE;
|
2016-11-24 10:46:57 +01:00
|
|
|
public $sortCol = 3;
|
2016-05-23 18:52:20 +02:00
|
|
|
|
|
|
|
public function columns()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'vendor_name',
|
2017-01-30 17:05:31 +01:00
|
|
|
function ($model) {
|
2016-05-23 18:52:20 +02:00
|
|
|
if ($model->vendor_public_id) {
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! Auth::user()->can('viewByOwner', [ENTITY_VENDOR, $model->vendor_user_id])) {
|
2016-05-23 18:52:20 +02:00
|
|
|
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
|
|
|
},
|
2017-01-30 20:40:43 +01:00
|
|
|
! $this->hideClient,
|
2016-05-23 18:52:20 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'client_name',
|
2017-01-30 17:05:31 +01:00
|
|
|
function ($model) {
|
2016-05-23 18:52:20 +02:00
|
|
|
if ($model->client_public_id) {
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! Auth::user()->can('viewByOwner', [ENTITY_CLIENT, $model->client_user_id])) {
|
2016-05-23 18:52:20 +02:00
|
|
|
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
|
|
|
},
|
2017-01-30 20:40:43 +01:00
|
|
|
! $this->hideClient,
|
2016-05-23 18:52:20 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'expense_date',
|
|
|
|
function ($model) {
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! Auth::user()->can('viewByOwner', [ENTITY_EXPENSE, $model->user_id])) {
|
2017-03-28 11:40:53 +02:00
|
|
|
return Utils::fromSqlDate($model->expense_date_sql);
|
2016-05-23 18:52:20 +02:00
|
|
|
}
|
|
|
|
|
2017-03-28 11:40:53 +02:00
|
|
|
return link_to("expenses/{$model->public_id}/edit", Utils::fromSqlDate($model->expense_date_sql))->toHtml();
|
2017-01-30 20:40:43 +01:00
|
|
|
},
|
2016-05-23 18:52:20 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'amount',
|
|
|
|
function ($model) {
|
2016-10-27 16:26:42 +02:00
|
|
|
$amount = Utils::calculateTaxes($model->amount, $model->tax_rate1, $model->tax_rate2);
|
|
|
|
$str = Utils::formatMoney($amount, $model->expense_currency_id);
|
|
|
|
|
2016-05-23 18:52:20 +02:00
|
|
|
// show both the amount and the converted amount
|
|
|
|
if ($model->exchange_rate != 1) {
|
2016-10-27 16:26:42 +02:00
|
|
|
$converted = round($amount * $model->exchange_rate, 2);
|
|
|
|
$str .= ' | ' . Utils::formatMoney($converted, $model->invoice_currency_id);
|
2016-05-23 18:52:20 +02:00
|
|
|
}
|
2016-10-27 16:26:42 +02:00
|
|
|
|
|
|
|
return $str;
|
2017-01-30 20:40:43 +01:00
|
|
|
},
|
2016-05-23 18:52:20 +02:00
|
|
|
],
|
2016-07-06 20:35:16 +02:00
|
|
|
[
|
|
|
|
'category',
|
|
|
|
function ($model) {
|
2017-03-25 20:01:35 +01:00
|
|
|
$category = $model->category != null ? substr($model->category, 0, 100) : '';
|
|
|
|
if (! Auth::user()->can('editByOwner', [ENTITY_EXPENSE_CATEGORY, $model->category_user_id])) {
|
|
|
|
return $category;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $model->category_public_id ? link_to("expense_categories/{$model->category_public_id}/edit", $category)->toHtml() : '';
|
2017-01-30 20:40:43 +01:00
|
|
|
},
|
2016-07-06 20:35:16 +02:00
|
|
|
],
|
2016-05-23 18:52:20 +02:00
|
|
|
[
|
|
|
|
'public_notes',
|
|
|
|
function ($model) {
|
|
|
|
return $model->public_notes != null ? substr($model->public_notes, 0, 100) : '';
|
2017-01-30 20:40:43 +01:00
|
|
|
},
|
2016-05-23 18:52:20 +02:00
|
|
|
],
|
|
|
|
[
|
2016-11-24 10:22:37 +01:00
|
|
|
'status',
|
2016-05-23 18:52:20 +02:00
|
|
|
function ($model) {
|
2016-07-03 11:33:35 +02:00
|
|
|
return self::getStatusLabel($model->invoice_id, $model->should_be_invoiced, $model->balance);
|
2017-01-30 20:40:43 +01:00
|
|
|
},
|
2016-05-23 18:52:20 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actions()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
trans('texts.edit_expense'),
|
|
|
|
function ($model) {
|
2017-01-30 20:40:43 +01:00
|
|
|
return URL::to("expenses/{$model->public_id}/edit");
|
2016-05-23 18:52:20 +02:00
|
|
|
},
|
|
|
|
function ($model) {
|
|
|
|
return Auth::user()->can('editByOwner', [ENTITY_EXPENSE, $model->user_id]);
|
2017-01-30 20:40:43 +01:00
|
|
|
},
|
2016-05-23 18:52:20 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
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]);
|
2017-01-30 20:40:43 +01:00
|
|
|
},
|
2016-05-23 18:52:20 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
trans('texts.invoice_expense'),
|
|
|
|
function ($model) {
|
2016-11-25 11:53:35 +01:00
|
|
|
return "javascript:submitForm_expense('invoice', {$model->public_id})";
|
2016-05-23 18:52:20 +02:00
|
|
|
},
|
|
|
|
function ($model) {
|
2017-01-30 20:40:43 +01:00
|
|
|
return ! $model->invoice_id && (! $model->deleted_at || $model->deleted_at == '0000-00-00') && Auth::user()->can('create', ENTITY_INVOICE);
|
|
|
|
},
|
2016-05-23 18:52:20 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-07-03 11:33:35 +02:00
|
|
|
private function getStatusLabel($invoiceId, $shouldBeInvoiced, $balance)
|
2016-05-23 18:52:20 +02:00
|
|
|
{
|
2016-12-26 19:09:49 +01:00
|
|
|
$label = Expense::calcStatusLabel($shouldBeInvoiced, $invoiceId, $balance);
|
|
|
|
$class = Expense::calcStatusClass($shouldBeInvoiced, $invoiceId, $balance);
|
2016-05-23 18:52:20 +02:00
|
|
|
|
|
|
|
return "<h4><div class=\"label label-{$class}\">$label</div></h4>";
|
|
|
|
}
|
|
|
|
}
|