1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Ninja/Datatables/PaymentDatatable.php

170 lines
6.8 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Ninja\Datatables;
2016-05-23 18:52:20 +02:00
2016-12-26 18:45:38 +01:00
use App\Models\Payment;
2016-05-23 18:52:20 +02:00
use App\Models\PaymentMethod;
2017-01-30 20:40:43 +01:00
use Auth;
use URL;
use Utils;
2016-05-23 18:52:20 +02:00
class PaymentDatatable extends EntityDatatable
{
public $entityType = ENTITY_PAYMENT;
2016-11-24 10:46:57 +01:00
public $sortCol = 7;
2016-05-23 18:52:20 +02:00
protected static $refundableGateways = [
2016-05-23 20:10:40 +02:00
GATEWAY_STRIPE,
GATEWAY_BRAINTREE,
GATEWAY_WEPAY,
];
2016-05-23 20:10:40 +02:00
2016-05-23 18:52:20 +02:00
public function columns()
{
return [
[
2016-12-15 14:20:36 +01:00
'invoice_name',
2016-05-23 18:52:20 +02:00
function ($model) {
2017-01-30 20:40:43 +01:00
if (! Auth::user()->can('viewByOwner', [ENTITY_INVOICE, $model->invoice_user_id])) {
2016-05-23 18:52:20 +02:00
return $model->invoice_number;
}
return link_to("invoices/{$model->invoice_public_id}/edit", $model->invoice_number, ['class' => Utils::getEntityRowClass($model)])->toHtml();
2017-01-30 20:40:43 +01:00
},
2016-05-23 18:52:20 +02:00
],
[
'client_name',
function ($model) {
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 $model->client_public_id ? link_to("clients/{$model->client_public_id}", Utils::getClientDisplayName($model))->toHtml() : '';
},
2017-01-30 20:40:43 +01:00
! $this->hideClient,
2016-05-23 18:52:20 +02:00
],
[
'transaction_reference',
function ($model) {
2017-10-14 19:55:37 +02:00
$str = $model->transaction_reference ? e($model->transaction_reference) : '<i>'.trans('texts.manual_entry').'</i>';
return $this->addNote($str, $model->private_notes);
2017-01-30 20:40:43 +01:00
},
2016-05-23 18:52:20 +02:00
],
[
2016-12-15 14:20:36 +01:00
'method',
2016-05-23 18:52:20 +02:00
function ($model) {
2017-09-06 10:45:50 +02:00
return $model->account_gateway_id ? $model->gateway_name : ($model->payment_type ? trans('texts.payment_type_' . $model->payment_type) : '');
2017-01-30 20:40:43 +01:00
},
2016-05-23 18:52:20 +02:00
],
[
2016-12-15 14:20:36 +01:00
'source',
2016-05-23 18:52:20 +02:00
function ($model) {
$code = str_replace(' ', '', strtolower($model->payment_type));
$card_type = trans('texts.card_' . $code);
2016-05-23 18:52:20 +02:00
if ($model->payment_type_id != PAYMENT_TYPE_ACH) {
2017-01-30 17:05:31 +01:00
if ($model->last4) {
2016-06-22 11:22:38 +02:00
$expiration = Utils::fromSqlDate($model->expiration, false)->format('m/y');
2017-01-30 20:40:43 +01:00
2016-05-23 18:52:20 +02:00
return '<img height="22" src="' . URL::to('/images/credit_cards/' . $code . '.png') . '" alt="' . htmlentities($card_type) . '">&nbsp; &bull;&bull;&bull;' . $model->last4 . ' ' . $expiration;
} elseif ($model->email) {
return $model->email;
2017-09-05 17:17:58 +02:00
} elseif ($model->payment_type) {
2017-11-10 11:07:06 +01:00
return trans('texts.payment_type_' . $model->payment_type);
2016-05-23 18:52:20 +02:00
}
} elseif ($model->last4) {
2017-01-30 17:05:31 +01:00
if ($model->bank_name) {
2016-05-25 21:04:58 +02:00
$bankName = $model->bank_name;
} else {
$bankData = PaymentMethod::lookupBankData($model->routing_number);
2017-01-30 17:05:31 +01:00
if ($bankData) {
2016-05-25 21:04:58 +02:00
$bankName = $bankData->name;
}
}
2017-01-30 20:40:43 +01:00
if (! empty($bankName)) {
2016-05-25 21:04:58 +02:00
return $bankName.'&nbsp; &bull;&bull;&bull;' . $model->last4;
2017-01-30 17:05:31 +01:00
} elseif ($model->last4) {
2016-05-23 18:52:20 +02:00
return '<img height="22" src="' . URL::to('/images/credit_cards/ach.png') . '" alt="' . htmlentities($card_type) . '">&nbsp; &bull;&bull;&bull;' . $model->last4;
}
}
2017-01-30 20:40:43 +01:00
},
2016-05-23 18:52:20 +02:00
],
[
'amount',
function ($model) {
2017-11-19 10:55:01 +01:00
$amount = Utils::formatMoney($model->amount, $model->currency_id, $model->country_id);
if ($model->exchange_currency_id && $model->exchange_rate != 1) {
$amount .= ' | ' . Utils::formatMoney($model->amount * $model->exchange_rate, $model->exchange_currency_id, $model->country_id);
}
return $amount;
2017-01-30 20:40:43 +01:00
},
2016-05-23 18:52:20 +02:00
],
[
2017-03-28 11:40:53 +02:00
'date',
2016-05-23 18:52:20 +02:00
function ($model) {
2016-10-10 10:40:04 +02:00
if ($model->is_deleted) {
return Utils::dateToString($model->payment_date);
} else {
return link_to("payments/{$model->public_id}/edit", Utils::dateToString($model->payment_date))->toHtml();
}
2017-01-30 20:40:43 +01:00
},
2016-05-23 18:52:20 +02:00
],
[
'status',
2016-05-23 18:52:20 +02:00
function ($model) {
return self::getStatusLabel($model);
2017-01-30 20:40:43 +01:00
},
],
2016-05-23 18:52:20 +02:00
];
}
public function actions()
{
return [
[
trans('texts.edit_payment'),
function ($model) {
return URL::to("payments/{$model->public_id}/edit");
},
function ($model) {
return Auth::user()->can('editByOwner', [ENTITY_PAYMENT, $model->user_id]);
2017-01-30 20:40:43 +01:00
},
2016-05-23 18:52:20 +02:00
],
2017-05-16 12:13:53 +02:00
[
trans('texts.email_payment'),
function ($model) {
return "javascript:submitForm_payment('email', {$model->public_id})";
},
function ($model) {
return Auth::user()->can('editByOwner', [ENTITY_PAYMENT, $model->user_id]);
},
],
2016-05-23 18:52:20 +02:00
[
trans('texts.refund_payment'),
function ($model) {
$max_refund = number_format($model->amount - $model->refunded, 2);
$formatted = Utils::formatMoney($max_refund, $model->currency_id, $model->country_id);
2017-01-30 20:40:43 +01:00
$symbol = Utils::getFromCache($model->currency_id ? $model->currency_id : 1, 'currencies')->symbol;
2016-05-23 18:52:20 +02:00
return "javascript:showRefundModal({$model->public_id}, '{$max_refund}', '{$formatted}', '{$symbol}')";
},
function ($model) {
return Auth::user()->can('editByOwner', [ENTITY_PAYMENT, $model->user_id])
&& $model->payment_status_id >= PAYMENT_STATUS_COMPLETED
2017-01-16 13:51:43 +01:00
&& $model->refunded < $model->amount;
2017-01-30 20:40:43 +01:00
},
],
2016-05-23 18:52:20 +02:00
];
}
2016-07-21 14:35:23 +02:00
private function getStatusLabel($model)
2016-05-23 18:52:20 +02:00
{
2016-12-26 18:45:38 +01:00
$amount = Utils::formatMoney($model->refunded, $model->currency_id, $model->country_id);
$label = Payment::calcStatusLabel($model->payment_status_id, $model->status, $amount);
$class = Payment::calcStatusClass($model->payment_status_id);
2016-05-23 18:52:20 +02:00
return "<h4><div class=\"label label-{$class}\">$label</div></h4>";
}
}