invoiceninja/app/Ninja/Datatables/PaymentDatatable.php

172 lines
6.9 KiB
PHP
Raw 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 19:45:38 +02:00
use App\Models\Payment;
2016-05-23 19:52:20 +03:00
use App\Models\PaymentMethod;
2017-01-30 21:40:43 +02:00
use Auth;
use URL;
use Utils;
2016-05-23 19:52:20 +03:00
class PaymentDatatable extends EntityDatatable
{
public $entityType = ENTITY_PAYMENT;
2016-11-24 11:46:57 +02:00
public $sortCol = 7;
2016-05-23 19:52:20 +03:00
protected static $refundableGateways = [
2016-05-23 21:10:40 +03:00
GATEWAY_STRIPE,
GATEWAY_BRAINTREE,
GATEWAY_WEPAY,
];
2016-05-23 21:10:40 +03:00
2016-05-23 19:52:20 +03:00
public function columns()
{
return [
[
2016-12-15 15:20:36 +02:00
'invoice_name',
2016-05-23 19:52:20 +03:00
function ($model) {
if (Auth::user()->can('view', [ENTITY_INVOICE, $model->invoice_user_id]))
return link_to("invoices/{$model->invoice_public_id}/edit", $model->invoice_number, ['class' => Utils::getEntityRowClass($model)])->toHtml();
else
2016-05-23 19:52:20 +03:00
return $model->invoice_number;
},
2016-05-23 19:52:20 +03:00
],
[
'client_name',
function ($model) {
if(Auth::user()->can('view', [ENTITY_CLIENT, ENTITY_CLIENT]))
return $model->client_public_id ? link_to("clients/{$model->client_public_id}", Utils::getClientDisplayName($model))->toHtml() : '';
else
2016-05-23 19:52:20 +03:00
return Utils::getClientDisplayName($model);
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
],
[
'transaction_reference',
function ($model) {
2017-10-14 20:55:37 +03: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 21:40:43 +02:00
},
2016-05-23 19:52:20 +03:00
],
[
2016-12-15 15:20:36 +02:00
'method',
2016-05-23 19:52:20 +03:00
function ($model) {
2017-09-06 11:45:50 +03:00
return $model->account_gateway_id ? $model->gateway_name : ($model->payment_type ? trans('texts.payment_type_' . $model->payment_type) : '');
2017-01-30 21:40:43 +02:00
},
2016-05-23 19:52:20 +03:00
],
[
2016-12-15 15:20:36 +02:00
'source',
2016-05-23 19:52:20 +03:00
function ($model) {
$code = str_replace(' ', '', strtolower($model->payment_type));
$card_type = trans('texts.card_' . $code);
2016-05-23 19:52:20 +03:00
if ($model->payment_type_id != PAYMENT_TYPE_ACH) {
2017-01-30 18:05:31 +02:00
if ($model->last4) {
2016-06-22 12:22:38 +03:00
$expiration = Utils::fromSqlDate($model->expiration, false)->format('m/y');
2017-01-30 21:40:43 +02:00
2016-05-23 19:52:20 +03: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 18:17:58 +03:00
} elseif ($model->payment_type) {
2017-11-10 12:07:06 +02:00
return trans('texts.payment_type_' . $model->payment_type);
2016-05-23 19:52:20 +03:00
}
} elseif ($model->last4) {
2017-01-30 18:05:31 +02:00
if ($model->bank_name) {
2016-05-25 15:04:58 -04:00
$bankName = $model->bank_name;
} else {
$bankData = PaymentMethod::lookupBankData($model->routing_number);
2017-01-30 18:05:31 +02:00
if ($bankData) {
2016-05-25 15:04:58 -04:00
$bankName = $bankData->name;
}
}
2017-01-30 21:40:43 +02:00
if (! empty($bankName)) {
2016-05-25 15:04:58 -04:00
return $bankName.'&nbsp; &bull;&bull;&bull;' . $model->last4;
2017-01-30 18:05:31 +02:00
} elseif ($model->last4) {
2016-05-23 19:52:20 +03: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 21:40:43 +02:00
},
2016-05-23 19:52:20 +03:00
],
[
'amount',
function ($model) {
2017-11-19 11:55:01 +02: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 21:40:43 +02:00
},
2016-05-23 19:52:20 +03:00
],
[
2017-03-28 12:40:53 +03:00
'date',
2016-05-23 19:52:20 +03:00
function ($model) {
2016-10-10 11:40:04 +03: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 21:40:43 +02:00
},
2016-05-23 19:52:20 +03:00
],
[
'status',
2016-05-23 19:52:20 +03:00
function ($model) {
return self::getStatusLabel($model);
2017-01-30 21:40:43 +02:00
},
],
2016-05-23 19:52:20 +03: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('view', [ENTITY_PAYMENT, $model]);
2017-01-30 21:40:43 +02:00
},
2016-05-23 19:52:20 +03:00
],
2017-05-16 13:13:53 +03:00
[
trans('texts.email_payment'),
function ($model) {
return "javascript:submitForm_payment('email', {$model->public_id})";
},
function ($model) {
return Auth::user()->can('edit', [ENTITY_PAYMENT, $model]);
2017-05-16 13:13:53 +03:00
},
],
2016-05-23 19:52:20 +03:00
[
trans('texts.refund_payment'),
function ($model) {
2018-03-23 14:21:21 +03:00
$max_refund = $model->amount - $model->refunded;
2016-05-23 19:52:20 +03:00
$formatted = Utils::formatMoney($max_refund, $model->currency_id, $model->country_id);
2017-01-30 21:40:43 +02:00
$symbol = Utils::getFromCache($model->currency_id ? $model->currency_id : 1, 'currencies')->symbol;
2018-02-16 14:55:02 +02:00
$local = in_array($model->gateway_id, [GATEWAY_BRAINTREE, GATEWAY_STRIPE, GATEWAY_WEPAY]) || ! $model->gateway_id ? 0 : 1;
2017-01-30 21:40:43 +02:00
2018-02-16 14:55:02 +02:00
return "javascript:showRefundModal({$model->public_id}, '{$max_refund}', '{$formatted}', '{$symbol}', {$local})";
2016-05-23 19:52:20 +03:00
},
function ($model) {
return Auth::user()->can('edit', [ENTITY_PAYMENT, $model])
&& $model->payment_status_id >= PAYMENT_STATUS_COMPLETED
2017-01-16 14:51:43 +02:00
&& $model->refunded < $model->amount;
2017-01-30 21:40:43 +02:00
},
],
2016-05-23 19:52:20 +03:00
];
}
2016-07-21 15:35:23 +03:00
private function getStatusLabel($model)
2016-05-23 19:52:20 +03:00
{
2016-12-26 19:45:38 +02: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 19:52:20 +03:00
return "<h4><div class=\"label label-{$class}\">$label</div></h4>";
}
}