add payment option
This commit is contained in:
@@ -2,16 +2,76 @@
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user