invoiceninja/app/Ninja/Datatables/InvoiceDatatable.php

199 lines
7.7 KiB
PHP
Raw Normal View History

2016-07-21 15:35:23 +03:00
<?php namespace App\Ninja\Datatables;
2016-05-23 19:52:20 +03:00
use Utils;
use URL;
use Auth;
2016-12-26 15:38:53 +02:00
use App\Models\Invoice;
2016-05-23 19:52:20 +03:00
class InvoiceDatatable extends EntityDatatable
{
public $entityType = ENTITY_INVOICE;
2016-11-24 11:46:57 +02:00
public $sortCol = 3;
2016-05-23 19:52:20 +03:00
public function columns()
{
$entityType = $this->entityType;
return [
[
$entityType == ENTITY_INVOICE ? 'invoice_number' : 'quote_number',
2016-05-23 19:52:20 +03:00
function ($model) use ($entityType) {
if(!Auth::user()->can('viewByOwner', [ENTITY_INVOICE, $model->user_id])){
2016-05-23 19:52:20 +03:00
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
],
[
'date',
2016-05-23 19:52:20 +03:00
function ($model) {
return Utils::fromSqlDate($model->date);
2016-05-23 19:52:20 +03:00
}
],
[
'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
],
[
$entityType == ENTITY_INVOICE ? 'due_date' : 'valid_until',
2016-05-23 19:52:20 +03:00
function ($model) {
return Utils::fromSqlDate($model->due_date);
},
],
[
'status',
2016-05-23 19:52:20 +03:00
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");
},
2016-07-21 15:35:23 +03:00
function ($model) {
2016-05-23 19:52:20 +03:00
return Auth::user()->can('create', ENTITY_INVOICE);
}
],
[
trans('texts.view_history'),
2016-05-23 19:52:20 +03: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);
}
],
[
trans('texts.mark_sent'),
2016-11-25 12:53:35 +02:00
function ($model) use ($entityType) {
return "javascript:submitForm_{$entityType}('markSent', {$model->public_id})";
2016-05-23 19:52:20 +03:00
},
function ($model) {
return $model->invoice_status_id < INVOICE_STATUS_SENT && Auth::user()->can('editByOwner', [ENTITY_INVOICE, $model->user_id]);
}
],
2016-12-14 21:47:22 +02:00
[
trans('texts.mark_paid'),
function ($model) use ($entityType) {
return "javascript:submitForm_{$entityType}('markPaid', {$model->public_id})";
},
function ($model) {
return $model->balance > 0 && Auth::user()->can('editByOwner', [ENTITY_INVOICE, $model->user_id]);
2016-12-14 21:47:22 +02:00
}
],
2016-05-23 19:52:20 +03:00
[
trans('texts.enter_payment'),
function ($model) {
return URL::to("payments/create/{$model->client_public_id}/{$model->public_id}");
},
function ($model) use ($entityType) {
return $model->is_public && $entityType == ENTITY_INVOICE && $model->balance > 0 && Auth::user()->can('create', ENTITY_PAYMENT);
2016-05-23 19:52:20 +03:00
}
],
[
trans('texts.view_quote'),
2016-05-23 19:52:20 +03: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]);
}
],
[
trans('texts.view_invoice'),
2016-05-23 19:52:20 +03: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]);
}
],
[
trans('texts.convert_to_invoice'),
2016-05-23 19:52:20 +03:00
function ($model) {
2016-11-25 12:53:35 +02:00
return "javascript:submitForm_quote('convert', {$model->public_id})";
2016-05-23 19:52:20 +03:00
},
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)
{
2016-12-26 15:38:53 +02:00
$class = Invoice::calcStatusClass($model->invoice_status_id, $model->balance, $model->due_date);
$label = Invoice::calcStatusLabel($model->invoice_status_name, $class, $this->entityType, $model->quote_invoice_id);
2016-05-23 19:52:20 +03:00
return "<h4><div class=\"label label-{$class}\">$label</div></h4>";
}
2016-12-14 21:47:22 +02:00
public function bulkActions()
{
$actions = parent::bulkActions();
if ($this->entityType == ENTITY_INVOICE || $this->entityType == ENTITY_QUOTE) {
2016-12-15 13:16:19 +02:00
$actions[] = \DropdownButton::DIVIDER;
2016-12-14 21:47:22 +02:00
$actions[] = [
'label' => mtrans($this->entityType, 'mark_sent'),
'url' => 'javascript:submitForm_'.$this->entityType.'("markSent")',
];
}
if ($this->entityType == ENTITY_INVOICE) {
$actions[] = [
'label' => mtrans($this->entityType, 'mark_paid'),
'url' => 'javascript:submitForm_'.$this->entityType.'("markPaid")',
];
}
return $actions;
}
2016-05-23 19:52:20 +03:00
}