invoiceninja/app/Ninja/Datatables/ExpenseDatatable.php

149 lines
5.3 KiB
PHP
Raw Normal View History

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