Teren-app/app/Models/Payment.php
2025-10-02 18:35:02 +02:00

78 lines
1.7 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Activity;
class Payment extends Model
{
use HasFactory;
use SoftDeletes;
protected $fillable = [
'account_id',
'amount_cents',
'currency',
'reference',
'paid_at',
'meta',
'created_by',
'activity_id',
];
protected function casts(): array
{
return [
'paid_at' => 'datetime',
'meta' => 'array',
'amount_cents' => 'integer',
];
}
public function account(): BelongsTo
{
return $this->belongsTo(Account::class);
}
public function bookings(): HasMany
{
return $this->hasMany(Booking::class);
}
public function activity(): BelongsTo
{
return $this->belongsTo(Activity::class);
}
public function type(): BelongsTo
{
return $this->belongsTo(\App\Models\PaymentType::class);
}
/**
* Accessor to expose decimal amount for JSON serialization and UI convenience.
*/
protected function amount(): Attribute
{
return Attribute::get(function () {
$cents = (int) ($this->attributes['amount_cents'] ?? 0);
return $cents / 100;
});
}
/**
* Mutator to set amount via decimal; stores in cents.
*/
public function setAmountAttribute($value): void
{
$this->attributes['amount_cents'] = (int) round(((float) $value) * 100);
}
}