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.

71 lines
1.8 KiB
PHTML

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Carbon;
class PoinHistory extends Model
{
protected $fillable = [
'debit',
'credit',
'description',
'narration',
'customer_id',
'related_type',
'related_id',
];
protected $appends = [
'amount',
'format_human_created_at',
'format_created_at',
];
public function formatHumanCreatedAt(): Attribute
{
return Attribute::make(get: function () {
1 year ago
return Carbon::parse($this->created_at)->translatedFormat('d F Y');
});
}
public function formatCreatedAt(): Attribute
{
return Attribute::make(get: function () {
1 year ago
return Carbon::parse($this->created_at)->translatedFormat('d F Y H:i:s');
});
}
public function amount(): Attribute
{
return Attribute::make(get: function () {
if ($this->credit == 0) {
return number_format($this->debit, is_float($this->debit) ? 2 : 0, ',', '.');
}
return number_format($this->credit, is_float($this->credit) ? 2 : 0, ',', '.');
});
}
public function customer()
{
return $this->belongsTo(Customer::class);
}
public function update_customer_balance($updateExpired = false)
{
$customer = Customer::find($this->customer_id);
$maxExpired = Setting::getByKey('MAX_POINT_EXPIRED');
if ($customer->poin_expired_at == null || $updateExpired) {
$customer->poin_expired_at = now()->addDays($maxExpired);
}
$customer->update([
'poin_balance' => $customer->poin_balance + $this->debit - $this->credit,
1 year ago
'poin_expired_at' => $customer->poin_expired_at,
]);
}
}