2016-05-23 18:52:20 +02:00
|
|
|
<?php namespace App\Ninja\Datatables;
|
|
|
|
|
|
|
|
use Utils;
|
|
|
|
use URL;
|
|
|
|
use Auth;
|
|
|
|
use App\Models\PaymentMethod;
|
|
|
|
|
|
|
|
class PaymentDatatable extends EntityDatatable
|
|
|
|
{
|
|
|
|
public $entityType = ENTITY_PAYMENT;
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
protected static $refundableGateways = [
|
2016-05-23 20:10:40 +02:00
|
|
|
GATEWAY_STRIPE,
|
|
|
|
GATEWAY_BRAINTREE,
|
|
|
|
GATEWAY_WEPAY,
|
2016-07-03 18:11:58 +02:00
|
|
|
];
|
2016-05-23 20:10:40 +02:00
|
|
|
|
2016-05-23 18:52:20 +02:00
|
|
|
public function columns()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'invoice_number',
|
|
|
|
function ($model) {
|
|
|
|
if(!Auth::user()->can('editByOwner', [ENTITY_INVOICE, $model->invoice_user_id])){
|
|
|
|
return $model->invoice_number;
|
|
|
|
}
|
|
|
|
|
|
|
|
return link_to("invoices/{$model->invoice_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 $model->client_public_id ? link_to("clients/{$model->client_public_id}", Utils::getClientDisplayName($model))->toHtml() : '';
|
|
|
|
},
|
|
|
|
! $this->hideClient
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'transaction_reference',
|
|
|
|
function ($model) {
|
2016-06-30 15:23:45 +02:00
|
|
|
return $model->transaction_reference ? $model->transaction_reference : '<i>'.trans('texts.manual_entry').'</i>';
|
2016-05-23 18:52:20 +02:00
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'payment_type',
|
|
|
|
function ($model) {
|
|
|
|
return ($model->payment_type && !$model->last4) ? $model->payment_type : ($model->account_gateway_id ? $model->gateway_name : '');
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'source',
|
|
|
|
function ($model) {
|
|
|
|
$code = str_replace(' ', '', strtolower($model->payment_type));
|
2016-07-03 18:11:58 +02:00
|
|
|
$card_type = trans('texts.card_' . $code);
|
2016-05-23 18:52:20 +02:00
|
|
|
if ($model->payment_type_id != PAYMENT_TYPE_ACH) {
|
|
|
|
if($model->last4) {
|
2016-06-22 11:22:38 +02:00
|
|
|
$expiration = Utils::fromSqlDate($model->expiration, false)->format('m/y');
|
2016-05-23 18:52:20 +02:00
|
|
|
return '<img height="22" src="' . URL::to('/images/credit_cards/' . $code . '.png') . '" alt="' . htmlentities($card_type) . '"> •••' . $model->last4 . ' ' . $expiration;
|
|
|
|
} elseif ($model->email) {
|
|
|
|
return $model->email;
|
|
|
|
}
|
|
|
|
} elseif ($model->last4) {
|
2016-05-25 21:04:58 +02:00
|
|
|
if($model->bank_name) {
|
|
|
|
$bankName = $model->bank_name;
|
|
|
|
} else {
|
|
|
|
$bankData = PaymentMethod::lookupBankData($model->routing_number);
|
|
|
|
if($bankData) {
|
|
|
|
$bankName = $bankData->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($bankName)) {
|
|
|
|
return $bankName.' •••' . $model->last4;
|
2016-05-23 18:52:20 +02:00
|
|
|
} elseif($model->last4) {
|
|
|
|
return '<img height="22" src="' . URL::to('/images/credit_cards/ach.png') . '" alt="' . htmlentities($card_type) . '"> •••' . $model->last4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'amount',
|
|
|
|
function ($model) {
|
|
|
|
return Utils::formatMoney($model->amount, $model->currency_id, $model->country_id);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'payment_date',
|
|
|
|
function ($model) {
|
|
|
|
return Utils::dateToString($model->payment_date);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'payment_status_name',
|
|
|
|
function ($model) {
|
|
|
|
return self::getStatusLabel($model);
|
|
|
|
}
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
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);
|
|
|
|
$symbol = Utils::getFromCache($model->currency_id ? $model->currency_id : 1, 'currencies')->symbol ;
|
|
|
|
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 &&
|
|
|
|
$model->refunded < $model->amount &&
|
|
|
|
(
|
|
|
|
($model->transaction_reference && in_array($model->gateway_id , static::$refundableGateways))
|
|
|
|
|| $model->payment_type_id == PAYMENT_TYPE_CREDIT
|
|
|
|
);
|
|
|
|
}
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getStatusLabel($model)
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
$label = trans('texts.status_' . strtolower($model->payment_status_name));
|
2016-05-23 18:52:20 +02:00
|
|
|
$class = 'default';
|
|
|
|
switch ($model->payment_status_id) {
|
|
|
|
case PAYMENT_STATUS_PENDING:
|
|
|
|
$class = 'info';
|
|
|
|
break;
|
|
|
|
case PAYMENT_STATUS_COMPLETED:
|
|
|
|
$class = 'success';
|
|
|
|
break;
|
|
|
|
case PAYMENT_STATUS_FAILED:
|
|
|
|
$class = 'danger';
|
|
|
|
break;
|
|
|
|
case PAYMENT_STATUS_PARTIALLY_REFUNDED:
|
|
|
|
$label = trans('texts.status_partially_refunded_amount', [
|
|
|
|
'amount' => Utils::formatMoney($model->refunded, $model->currency_id, $model->country_id),
|
|
|
|
]);
|
|
|
|
$class = 'primary';
|
|
|
|
break;
|
|
|
|
case PAYMENT_STATUS_VOIDED:
|
|
|
|
case PAYMENT_STATUS_REFUNDED:
|
|
|
|
$class = 'default';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return "<h4><div class=\"label label-{$class}\">$label</div></h4>";
|
|
|
|
}
|
|
|
|
}
|