You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.2 KiB
PHTML

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Carbon;
class DepositHistory extends Model
{
const STATUS_VALID = 0;
const STATUS_WAIT_UPLOAD = 1;
const STATUS_WAIT_APPROVE = 2;
const STATUS_WAIT_PAYMENT = 3;
const STATUS_INVALID = 4;
const STATUS_REJECT = 5;
protected $fillable = [
'debit',
'credit',
'description',
'customer_id',
'related_type',
'related_id',
'is_valid',
'image_prove',
'account_id',
'payment_token',
'payment_status',
'payment_response',
'payment_channel',
'payment_type',
];
protected $appends = [
'status',
'format_human_created_at',
'format_created_at',
'amount',
'image_prove_url',
];
public function status(): Attribute
{
return Attribute::make(get: function () {
return [
self::STATUS_VALID => ['text' => 'Success', 'color' => 'bg-green-600', 'text_color' => 'text-green-600'],
self::STATUS_WAIT_UPLOAD => ['text' => 'Upload bukti transfer', 'color' => 'bg-red-600', 'text_color' => 'text-red-600'],
self::STATUS_WAIT_APPROVE => ['text' => 'Menunggu Approve', 'color' => 'bg-green-600', 'text_color' => 'text-green-600'],
self::STATUS_WAIT_PAYMENT => ['text' => 'Menunggu Pembayaran', 'color' => 'bg-green-600', 'text_color' => 'text-green-600'],
self::STATUS_INVALID => ['text' => 'Error', 'color' => 'bg-red-600', 'text_color' => 'text-red-600'],
self::STATUS_REJECT => ['text' => 'Reject', 'color' => 'bg-red-600', 'text_color' => 'text-red-600'],
][$this->is_valid];
});
}
public function formatHumanCreatedAt(): Attribute
{
return Attribute::make(get: function () {
return Carbon::parse($this->created_at)->locale('id')->translatedFormat('d F Y');
});
}
public function formatCreatedAt(): Attribute
{
return Attribute::make(get: function () {
return Carbon::parse($this->created_at)->locale('id')->translatedFormat('d F Y H:i:s');
});
}
public function amount(): Attribute
{
return Attribute::make(get: function () {
if ($this->credit == 0) {
return 'Rp'.number_format($this->debit, is_float($this->debit) ? 2 : 0, ',', '.');
}
return '-Rp'.number_format($this->credit, is_float($this->credit) ? 2 : 0, ',', '.');
});
}
public function imageProveUrl(): Attribute
{
return Attribute::make(get: function () {
return $this->image_prove == null ? '' : asset($this->image_prove);
});
}
public function customer()
{
return $this->belongsTo(Customer::class);
}
public function account()
{
return $this->belongsTo(Account::class);
}
public function update_customer_balance()
{
$customer = Customer::find($this->customer_id);
$customer->update(['deposit_balance' => $customer->deposit_balance + $this->debit - $this->credit]);
}
public function create_notification()
{
if ($this->payment_channel == Setting::PAYMENT_MANUAL) {
$status = '';
if ($this->is_valid == self::STATUS_WAIT_APPROVE) {
$status = ' (bukti bayar di upload, membutuhkan konfirmasi)';
}
Notification::create([
'entity_type' => User::class,
'description' => $this->customer->fullname.' melakukan deposit manual sebesar : '.$this->amount.$status,
]);
}
if ($this->payment_channel == Setting::PAYMENT_MIDTRANS) {
Notification::create([
'entity_type' => User::class,
'description' => $this->customer->fullname.' melakukan deposit via midtrans sebesar : '.$this->amount,
]);
}
}
public function create_notification_user()
{
Notification::create([
'entity_id' => $this->customer_id,
'description' => 'Deposit '.$this->description.' sebesar '.$this->amount.' sudah sukses diterima',
]);
}
}