diff --git a/app/Models/Payment.php b/app/Models/Payment.php index aa98e24d7960..3518e345632d 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -17,6 +17,15 @@ class Payment extends EntityModel use PresentableTrait; use SoftDeletes; + public static $statusClasses = [ + PAYMENT_STATUS_PENDING => 'info', + PAYMENT_STATUS_COMPLETED => 'success', + PAYMENT_STATUS_FAILED => 'danger', + PAYMENT_STATUS_PARTIALLY_REFUNDED => 'primary', + PAYMENT_STATUS_VOIDED => 'default', + PAYMENT_STATUS_REFUNDED => 'default', + ]; + /** * @var array */ @@ -302,6 +311,35 @@ class Payment extends EntityModel { return $value ? str_pad($value, 4, '0', STR_PAD_LEFT) : null; } + + public static function calcStatusLabel($statusId, $statusName, $amount) + { + if ($statusId == PAYMENT_STATUS_PARTIALLY_REFUNDED) { + return trans('texts.status_partially_refunded_amount', [ + 'amount' => $amount, + ]); + } else { + return trans('texts.status_' . strtolower($statusName)); + } + } + + public static function calcStatusClass($statusId) + { + return static::$statusClasses[$statusId]; + } + + + public function statusClass() + { + return static::calcStatusClass($this->payment_status_id); + } + + public function statusLabel() + { + $amount = $this->account->formatMoney($this->refunded, $this->client); + return static::calcStatusLabel($this->payment_status_id, $this->payment_status->name, $amount); + } + } Payment::creating(function ($payment) { diff --git a/app/Ninja/Datatables/PaymentDatatable.php b/app/Ninja/Datatables/PaymentDatatable.php index 4524337919d9..8af805a4aaed 100644 --- a/app/Ninja/Datatables/PaymentDatatable.php +++ b/app/Ninja/Datatables/PaymentDatatable.php @@ -3,6 +3,7 @@ use Utils; use URL; use Auth; +use App\Models\Payment; use App\Models\PaymentMethod; class PaymentDatatable extends EntityDatatable @@ -140,29 +141,10 @@ class PaymentDatatable extends EntityDatatable private function getStatusLabel($model) { - $label = trans('texts.status_' . strtolower($model->status)); - $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; - } + $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); + return "

$label

"; } } diff --git a/app/Ninja/Presenters/PaymentPresenter.php b/app/Ninja/Presenters/PaymentPresenter.php index 3c8be72da282..5d6bb25efeb1 100644 --- a/app/Ninja/Presenters/PaymentPresenter.php +++ b/app/Ninja/Presenters/PaymentPresenter.php @@ -28,4 +28,15 @@ class PaymentPresenter extends EntityPresenter { } } + public function statusLabel() + { + if ($label = parent::statusLabel()) { + return $label; + } + + $class = $this->entity->statusClass(); + $label = $this->entity->statusLabel(); + + return "{$label}"; + } }