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
PHTML

<?php
namespace App\Models;
1 year ago
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Carbon;
class PaylaterHistory extends Model
{
protected $fillable = [
'debit',
'credit',
'description',
'customer_id',
];
1 year ago
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 () {
1 year ago
return Carbon::parse($this->created_at)->translatedFormat('d F Y');
1 year ago
});
}
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');
1 year ago
});
}
public function amount(): Attribute
{
return Attribute::make(get: function () {
if ($this->credit == 0) {
1 year ago
return 'Rp' . number_format($this->debit, is_float($this->debit) ? 2 : 0, ',', '.');
1 year ago
}
1 year ago
return '-Rp' . number_format($this->credit, is_float($this->credit) ? 2 : 0, ',', '.');
1 year ago
});
}
}