invoiceninja/app/Ninja/Datatables/InvoiceDatatable.php

236 lines
9.1 KiB
PHP
Raw Permalink 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 15:38:53 +02:00
use App\Models\Invoice;
2017-01-30 21:40:43 +02:00
use Auth;
use URL;
use Utils;
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()->viewModel($model, $entityType)) {
$str = link_to("{$entityType}s/{$model->public_id}/edit", $model->invoice_number, ['class' => Utils::getEntityRowClass($model)])->toHtml();
return $this->addNote($str, $model->private_notes);
2016-05-23 19:52:20 +03:00
}
else
return $model->invoice_number;
2016-05-23 19:52:20 +03:00
2017-01-30 21:40:43 +02:00
},
2016-05-23 19:52:20 +03:00
],
[
'client_name',
function ($model) {
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);
2017-01-30 21:40:43 +02:00
2016-05-23 19:52:20 +03:00
},
2017-01-30 21:40:43 +02:00
! $this->hideClient,
2016-05-23 19:52:20 +03:00
],
[
'date',
2016-05-23 19:52:20 +03:00
function ($model) {
2017-03-28 12:40:53 +03:00
return Utils::fromSqlDate($model->invoice_date);
2017-01-30 21:40:43 +02:00
},
2016-05-23 19:52:20 +03:00
],
[
'amount',
function ($model) {
return Utils::formatMoney($model->amount, $model->currency_id, $model->country_id);
2017-01-30 21:40:43 +02:00
},
2016-05-23 19:52:20 +03:00
],
[
'balance',
function ($model) {
return $model->partial > 0 ?
trans('texts.partial_remaining', [
'partial' => Utils::formatMoney($model->partial, $model->currency_id, $model->country_id),
2017-01-30 21:40:43 +02:00
'balance' => Utils::formatMoney($model->balance, $model->currency_id, $model->country_id), ]
2016-05-23 19:52:20 +03:00
) :
Utils::formatMoney($model->balance, $model->currency_id, $model->country_id);
},
2017-01-30 21:40:43 +02:00
$entityType == ENTITY_INVOICE,
2016-05-23 19:52:20 +03:00
],
[
$entityType == ENTITY_INVOICE ? 'due_date' : 'valid_until',
2016-05-23 19:52:20 +03:00
function ($model) {
$str = '';
if ($model->partial_due_date) {
2017-12-05 17:25:47 +02:00
$str = Utils::fromSqlDate($model->partial_due_date);
if ($model->due_date_sql && $model->due_date_sql != '0000-00-00') {
$str .= ', ';
}
}
return $str . Utils::fromSqlDate($model->due_date_sql);
2016-05-23 19:52:20 +03:00
},
],
[
'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);
2017-01-30 21:40:43 +02:00
},
],
2016-05-23 19:52:20 +03:00
];
}
public function actions()
{
$entityType = $this->entityType;
return [
[
trans("texts.clone_invoice"),
function ($model) {
return URL::to("invoices/{$model->public_id}/clone");
2016-05-23 19:52:20 +03:00
},
function ($model) {
return Auth::user()->can('create', ENTITY_INVOICE);
2017-01-30 21:40:43 +02:00
},
2016-05-23 19:52:20 +03:00
],
[
trans("texts.clone_quote"),
function ($model) {
return URL::to("quotes/{$model->public_id}/clone");
2016-05-23 19:52:20 +03:00
},
2016-07-21 15:35:23 +03:00
function ($model) {
return Auth::user()->can('create', ENTITY_QUOTE);
2017-01-30 21:40:43 +02:00
},
2016-05-23 19:52:20 +03:00
],
[
2017-11-20 15:40:12 +02:00
trans("texts.{$entityType}_history"),
2016-05-23 19:52:20 +03:00
function ($model) use ($entityType) {
return URL::to("{$entityType}s/{$entityType}_history/{$model->public_id}");
2017-01-30 21:40:43 +02:00
},
2016-05-23 19:52:20 +03:00
],
2017-11-20 15:40:12 +02:00
[
trans('texts.delivery_note'),
function ($model) use ($entityType) {
return url("invoices/delivery_note/{$model->public_id}");
},
function ($model) use ($entityType) {
return $entityType == ENTITY_INVOICE;
},
],
2016-05-23 19:52:20 +03:00
[
2017-01-30 18:05:31 +02:00
'--divider--', function () {
return false;
},
2016-05-23 19:52:20 +03:00
function ($model) {
return Auth::user()->canCreateOrEdit(ENTITY_INVOICE);
2017-01-30 21:40:43 +02:00
},
2016-05-23 19:52:20 +03:00
],
[
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->is_public && Auth::user()->can('edit', [ENTITY_INVOICE, $model]);
2017-01-30 21:40:43 +02:00
},
2016-05-23 19:52:20 +03:00
],
2016-12-14 21:47:22 +02:00
[
trans('texts.mark_paid'),
function ($model) use ($entityType) {
return "javascript:submitForm_{$entityType}('markPaid', {$model->public_id})";
},
2017-01-18 10:48:53 +02:00
function ($model) use ($entityType) {
return $entityType == ENTITY_INVOICE && $model->invoice_status_id != INVOICE_STATUS_PAID && Auth::user()->can('edit', [ENTITY_INVOICE, $model]);
2017-01-30 21:40:43 +02:00
},
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) {
2017-08-09 10:57:24 +03:00
return $entityType == ENTITY_INVOICE && $model->invoice_status_id != INVOICE_STATUS_PAID && Auth::user()->can('create', ENTITY_PAYMENT);
2017-01-30 21:40:43 +02:00
},
2016-05-23 19:52:20 +03:00
],
[
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('view', [ENTITY_INVOICE, $model]);
2017-01-30 21:40:43 +02:00
},
2016-05-23 19:52:20 +03:00
],
2018-02-08 13:01:51 +02:00
[
trans('texts.new_proposal'),
function ($model) {
return URL::to("proposals/create/{$model->public_id}");
},
function ($model) use ($entityType) {
return $entityType == ENTITY_QUOTE && ! $model->quote_invoice_id && $model->invoice_status_id < INVOICE_STATUS_APPROVED && Auth::user()->can('create', ENTITY_PROPOSAL);
},
],
2016-05-23 19:52:20 +03:00
[
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('edit', [ENTITY_INVOICE, $model]);
2017-01-30 21:40:43 +02:00
},
],
2016-05-23 19:52:20 +03:00
];
}
private function getStatusLabel($model)
{
$class = Invoice::calcStatusClass($model->invoice_status_id, $model->balance, $model->partial_due_date ?: $model->due_date_sql, $model->is_recurring);
2016-12-26 15:38:53 +02:00
$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()
{
2017-07-19 17:34:09 +03:00
$actions = [];
2016-12-14 21:47:22 +02:00
if ($this->entityType == ENTITY_INVOICE || $this->entityType == ENTITY_QUOTE) {
2017-07-19 12:59:56 +03:00
$actions[] = [
'label' => mtrans($this->entityType, 'download_' . $this->entityType),
'url' => 'javascript:submitForm_'.$this->entityType.'("download")',
];
2018-02-04 10:43:14 +02:00
if (auth()->user()->isTrusted()) {
2018-02-04 10:31:52 +02:00
$actions[] = [
'label' => mtrans($this->entityType, 'email_' . $this->entityType),
'url' => 'javascript:submitForm_'.$this->entityType.'("emailInvoice")',
];
}
2017-07-19 12:59:56 +03: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")',
];
}
2017-07-19 17:34:09 +03:00
$actions[] = \DropdownButton::DIVIDER;
$actions = array_merge($actions, parent::bulkActions());
2016-12-14 21:47:22 +02:00
return $actions;
}
2016-05-23 19:52:20 +03:00
}