64 lines
1.6 KiB
PHP
64 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\Investment\Entities;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Modules\Auth\Entities\User;
|
|
|
|
class UserInvestmentLimit extends Model
|
|
{
|
|
use HasUuids;
|
|
|
|
protected $table = 'user_investment_limits';
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'investment_type_id',
|
|
'max_investment_amount',
|
|
'max_transaction_amount',
|
|
'effective_date',
|
|
'expiry_date',
|
|
'created_by',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'max_investment_amount' => 'decimal:2',
|
|
'max_transaction_amount' => 'decimal:2',
|
|
'effective_date' => 'date',
|
|
'expiry_date' => 'date',
|
|
];
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
public function investmentType(): BelongsTo
|
|
{
|
|
return $this->belongsTo(InvestmentType::class, 'investment_type_id');
|
|
}
|
|
|
|
public function createdBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by');
|
|
}
|
|
|
|
public function scopeActive(Builder $query, $onDate = null): Builder
|
|
{
|
|
$date = ($onDate ?? now())->toDateString();
|
|
|
|
return $query
|
|
->whereDate('effective_date', '<=', $date)
|
|
->where(function (Builder $builder) use ($date) {
|
|
$builder->whereNull('expiry_date')
|
|
->orWhereDate('expiry_date', '>=', $date);
|
|
});
|
|
}
|
|
}
|