adrema/app/Payment/Payment.php

88 lines
2.0 KiB
PHP
Raw Normal View History

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;
2023-11-30 23:54:16 +01:00
/**
* @var array<int, string>
*/
public $fillable = ['member_id', 'invoice_data', 'subscription_id', 'nr', 'status_id', 'last_remembered_at'];
/**
* @var array<string, string>
*/
public $casts = [
'invoice_data' => 'json',
'last_remembered_at' => 'date',
];
2021-07-04 16:56:07 +02:00
2023-02-17 18:57:11 +01:00
/**
* @return BelongsTo<Member, self>
*/
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);
}
2023-02-17 18:57:11 +01:00
/**
* @return BelongsTo<Subscription, self>
*/
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);
}
2023-02-17 18:57:11 +01:00
/**
* @return BelongsTo<Status, self>
*/
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
2023-02-17 18:57:11 +01:00
/**
* @param Builder<self> $query
*
* @return Builder<self>
*/
public function scopeWhereNeedsPayment(Builder $query): Builder
2022-01-02 11:42:32 +01:00
{
2023-02-17 18:57:11 +01:00
return $query->whereHas('status', function ($q) {
2021-07-04 21:47:20 +02:00
return $q->needsPayment();
});
}
2023-02-17 18:57:11 +01:00
/**
* @param Builder<self> $query
*
* @return Builder<self>
*/
public function scopeWhereNeedsBill(Builder $query): Builder
2022-01-02 11:42:32 +01:00
{
2023-02-17 18:57:11 +01:00
return $query->whereHas('status', function ($q) {
return $q->where('is_bill', true);
});
}
2023-02-17 18:57:11 +01:00
/**
* @param Builder<self> $query
*
* @return Builder<self>
*/
public function scopeWhereNeedsRemember(Builder $query): Builder
2022-01-02 11:42:32 +01:00
{
2023-02-17 18:57:11 +01:00
return $query->whereHas('status', function ($q) {
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-04 16:56:07 +02:00
}