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.

55 lines
1.4 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Carbon;
class PaylaterHistory extends Model
{
protected $fillable = [
'debit',
'credit',
'description',
'customer_id',
];
protected $appends = [
'format_human_created_at',
'format_created_at',
'amount',
];
public function update_customer_paylater()
{
$customer = Customer::find($this->customer_id);
$paylater = $customer->paylater;
$paylater->update(['usage' => $paylater->usage + $this->debit - $this->credit]);
}
public function formatHumanCreatedAt(): Attribute
{
return Attribute::make(get: function () {
return Carbon::parse($this->created_at)->translatedFormat('d F Y');
});
}
public function formatCreatedAt(): Attribute
{
return Attribute::make(get: function () {
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 'Rp' . number_format($this->debit, is_float($this->debit) ? 2 : 0, ',', '.');
}
return '-Rp' . number_format($this->credit, is_float($this->credit) ? 2 : 0, ',', '.');
});
}
}