2021-07-04 16:56:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Payment;
|
|
|
|
|
|
|
|
use App\Member\Member;
|
2022-01-02 11:42:32 +01:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2021-10-29 21:44:23 +02:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-01-02 11:42:32 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2021-07-04 16:56:07 +02:00
|
|
|
|
|
|
|
class Payment extends Model
|
|
|
|
{
|
|
|
|
use HasFactory;
|
|
|
|
|
2021-10-29 21:44:23 +02:00
|
|
|
public $fillable = ['member_id', 'subscription_id', 'nr', 'status_id', 'last_remembered_at'];
|
2021-07-04 16:56:07 +02:00
|
|
|
|
2022-01-02 11:42:32 +01:00
|
|
|
public function member(): BelongsTo
|
|
|
|
{
|
2021-07-04 16:56:07 +02:00
|
|
|
return $this->belongsTo(Member::class);
|
|
|
|
}
|
|
|
|
|
2022-01-02 11:42:32 +01:00
|
|
|
public function subscription(): BelongsTo
|
|
|
|
{
|
2021-07-04 16:56:07 +02:00
|
|
|
return $this->belongsTo(Subscription::class);
|
|
|
|
}
|
|
|
|
|
2022-01-02 11:42:32 +01:00
|
|
|
public function status(): BelongsTo
|
|
|
|
{
|
2021-07-04 16:56:07 +02:00
|
|
|
return $this->belongsTo(Status::class);
|
|
|
|
}
|
2021-07-04 21:47:20 +02:00
|
|
|
|
2022-01-02 11:42:32 +01:00
|
|
|
public function scopeWhereNeedsPayment(Builder $q): Builder
|
|
|
|
{
|
2022-03-11 20:19:17 +01:00
|
|
|
return $q->whereHas('status', function ($q) {
|
2021-07-04 21:47:20 +02:00
|
|
|
return $q->needsPayment();
|
|
|
|
});
|
|
|
|
}
|
2021-07-15 21:19:08 +02:00
|
|
|
|
2022-01-02 11:42:32 +01:00
|
|
|
public function scopeWhereNeedsBill(Builder $q): Builder
|
|
|
|
{
|
2022-03-11 20:19:17 +01:00
|
|
|
return $q->whereHas('status', function ($q) {
|
2021-07-15 21:19:08 +02:00
|
|
|
return $q->where('is_bill', true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-02 11:42:32 +01:00
|
|
|
public function scopeWhereNeedsRemember(Builder $q): Builder
|
|
|
|
{
|
2022-03-11 20:19:17 +01:00
|
|
|
return $q->whereHas('status', function ($q) {
|
2021-07-15 21:19:08 +02:00
|
|
|
return $q->where('is_remember', true);
|
2022-02-10 01:05:37 +01:00
|
|
|
})->where(fn ($query) => $query->whereNull('last_remembered_at')->orWhere('last_remembered_at', '<=', now()->subMonths(3)));
|
2021-07-15 21:19:08 +02:00
|
|
|
}
|
2021-07-04 16:56:07 +02:00
|
|
|
}
|