2016-05-23 18:52:20 +02:00
|
|
|
<?php namespace App\Ninja\Datatables;
|
|
|
|
|
|
|
|
use Utils;
|
|
|
|
use URL;
|
|
|
|
use Auth;
|
|
|
|
|
|
|
|
class InvoiceDatatable extends EntityDatatable
|
|
|
|
{
|
|
|
|
public $entityType = ENTITY_INVOICE;
|
|
|
|
|
|
|
|
public function columns()
|
|
|
|
{
|
|
|
|
$entityType = $this->entityType;
|
|
|
|
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'invoice_number',
|
|
|
|
function ($model) use ($entityType) {
|
|
|
|
if(!Auth::user()->can('editByOwner', [ENTITY_INVOICE, $model->user_id])){
|
|
|
|
return $model->invoice_number;
|
|
|
|
}
|
|
|
|
|
|
|
|
return link_to("{$entityType}s/{$model->public_id}/edit", $model->invoice_number, ['class' => Utils::getEntityRowClass($model)])->toHtml();
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'client_name',
|
|
|
|
function ($model) {
|
|
|
|
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();
|
|
|
|
},
|
|
|
|
! $this->hideClient
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'invoice_date',
|
|
|
|
function ($model) {
|
|
|
|
return Utils::fromSqlDate($model->invoice_date);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'amount',
|
|
|
|
function ($model) {
|
|
|
|
return Utils::formatMoney($model->amount, $model->currency_id, $model->country_id);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'balance',
|
|
|
|
function ($model) {
|
|
|
|
return $model->partial > 0 ?
|
|
|
|
trans('texts.partial_remaining', [
|
|
|
|
'partial' => Utils::formatMoney($model->partial, $model->currency_id, $model->country_id),
|
|
|
|
'balance' => Utils::formatMoney($model->balance, $model->currency_id, $model->country_id)]
|
|
|
|
) :
|
|
|
|
Utils::formatMoney($model->balance, $model->currency_id, $model->country_id);
|
|
|
|
},
|
|
|
|
$entityType == ENTITY_INVOICE
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'due_date',
|
|
|
|
function ($model) {
|
|
|
|
return Utils::fromSqlDate($model->due_date);
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'invoice_status_name',
|
|
|
|
function ($model) use ($entityType) {
|
|
|
|
return $model->quote_invoice_id ? link_to("invoices/{$model->quote_invoice_id}/edit", trans('texts.converted'))->toHtml() : self::getStatusLabel($model);
|
|
|
|
}
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actions()
|
|
|
|
{
|
|
|
|
$entityType = $this->entityType;
|
|
|
|
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
trans("texts.edit_{$entityType}"),
|
|
|
|
function ($model) use ($entityType) {
|
|
|
|
return URL::to("{$entityType}s/{$model->public_id}/edit");
|
|
|
|
},
|
|
|
|
function ($model) {
|
|
|
|
return Auth::user()->can('editByOwner', [ENTITY_INVOICE, $model->user_id]);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
trans("texts.clone_{$entityType}"),
|
|
|
|
function ($model) use ($entityType) {
|
|
|
|
return URL::to("{$entityType}s/{$model->public_id}/clone");
|
|
|
|
},
|
|
|
|
function ($model) {
|
|
|
|
return Auth::user()->can('create', ENTITY_INVOICE);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
2016-07-03 18:11:58 +02:00
|
|
|
trans('texts.view_history'),
|
2016-05-23 18:52:20 +02:00
|
|
|
function ($model) use ($entityType) {
|
|
|
|
return URL::to("{$entityType}s/{$entityType}_history/{$model->public_id}");
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'--divider--', function(){return false;},
|
|
|
|
function ($model) {
|
|
|
|
return Auth::user()->can('editByOwner', [ENTITY_INVOICE, $model->user_id]) || Auth::user()->can('create', ENTITY_PAYMENT);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
2016-07-03 18:11:58 +02:00
|
|
|
trans('texts.mark_sent'),
|
2016-05-23 18:52:20 +02:00
|
|
|
function ($model) {
|
|
|
|
return "javascript:markEntity({$model->public_id})";
|
|
|
|
},
|
|
|
|
function ($model) {
|
|
|
|
return $model->invoice_status_id < INVOICE_STATUS_SENT && Auth::user()->can('editByOwner', [ENTITY_INVOICE, $model->user_id]);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
trans('texts.enter_payment'),
|
|
|
|
function ($model) {
|
|
|
|
return URL::to("payments/create/{$model->client_public_id}/{$model->public_id}");
|
|
|
|
},
|
|
|
|
function ($model) use ($entityType) {
|
|
|
|
return $entityType == ENTITY_INVOICE && $model->balance > 0 && Auth::user()->can('create', ENTITY_PAYMENT);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
2016-07-03 18:11:58 +02:00
|
|
|
trans('texts.view_quote'),
|
2016-05-23 18:52:20 +02:00
|
|
|
function ($model) {
|
|
|
|
return URL::to("quotes/{$model->quote_id}/edit");
|
|
|
|
},
|
|
|
|
function ($model) use ($entityType) {
|
|
|
|
return $entityType == ENTITY_INVOICE && $model->quote_id && Auth::user()->can('editByOwner', [ENTITY_INVOICE, $model->user_id]);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
2016-07-03 18:11:58 +02:00
|
|
|
trans('texts.view_invoice'),
|
2016-05-23 18:52:20 +02:00
|
|
|
function ($model) {
|
|
|
|
return URL::to("invoices/{$model->quote_invoice_id}/edit");
|
|
|
|
},
|
|
|
|
function ($model) use ($entityType) {
|
|
|
|
return $entityType == ENTITY_QUOTE && $model->quote_invoice_id && Auth::user()->can('editByOwner', [ENTITY_INVOICE, $model->user_id]);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
2016-07-03 18:11:58 +02:00
|
|
|
trans('texts.convert_to_invoice'),
|
2016-05-23 18:52:20 +02:00
|
|
|
function ($model) {
|
|
|
|
return "javascript:convertEntity({$model->public_id})";
|
|
|
|
},
|
|
|
|
function ($model) use ($entityType) {
|
|
|
|
return $entityType == ENTITY_QUOTE && ! $model->quote_invoice_id && Auth::user()->can('editByOwner', [ENTITY_INVOICE, $model->user_id]);
|
|
|
|
}
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getStatusLabel($model)
|
|
|
|
{
|
|
|
|
$entityType = $this->entityType;
|
|
|
|
|
|
|
|
// check if invoice is overdue
|
|
|
|
if (Utils::parseFloat($model->balance) && $model->due_date && $model->due_date != '0000-00-00') {
|
2016-07-03 18:11:58 +02:00
|
|
|
if (\DateTime::createFromFormat('Y-m-d', $model->due_date) < new \DateTime('now')) {
|
2016-05-23 18:52:20 +02:00
|
|
|
$label = $entityType == ENTITY_INVOICE ? trans('texts.overdue') : trans('texts.expired');
|
2016-07-03 18:11:58 +02:00
|
|
|
return '<h4><div class="label label-danger">' . $label . '</div></h4>';
|
2016-05-23 18:52:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
$label = trans('texts.status_' . strtolower($model->invoice_status_name));
|
2016-05-23 18:52:20 +02:00
|
|
|
$class = 'default';
|
|
|
|
switch ($model->invoice_status_id) {
|
|
|
|
case INVOICE_STATUS_SENT:
|
|
|
|
$class = 'info';
|
|
|
|
break;
|
|
|
|
case INVOICE_STATUS_VIEWED:
|
|
|
|
$class = 'warning';
|
|
|
|
break;
|
|
|
|
case INVOICE_STATUS_APPROVED:
|
|
|
|
$class = 'success';
|
|
|
|
break;
|
|
|
|
case INVOICE_STATUS_PARTIAL:
|
|
|
|
$class = 'primary';
|
|
|
|
break;
|
|
|
|
case INVOICE_STATUS_PAID:
|
|
|
|
$class = 'success';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "<h4><div class=\"label label-{$class}\">$label</div></h4>";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|