-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubscription.php
More file actions
171 lines (154 loc) · 4.65 KB
/
Copy pathSubscription.php
File metadata and controls
171 lines (154 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
declare(strict_types=1);
namespace App\Models;
use App\Enums\BillingCycle;
use App\Enums\SubscriptionStatus;
use Database\Factories\SubscriptionFactory;
use Illuminate\Database\Connection;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Builder;
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 Illuminate\Support\Carbon;
/**
* @use HasFactory<SubscriptionFactory>
*
* @property BillingCycle $billing_cycle
* @property SubscriptionStatus $status
* @property Carbon $next_billing_date
* @property-read float $monthly_cost
* @property-read float $yearly_cost
*
* @method static Builder<self> forUser(User $user)
* @method static Builder<self> active()
* @method static Builder<self> dueWithin(int $days)
* @method static Builder<self> dueForRenewal()
* @method static Builder<self> dueForReminder()
*/
#[Fillable([
'user_id',
'category_id',
'name',
'description',
'price',
'currency',
'billing_cycle',
'status',
'started_at',
'next_billing_date',
'cancelled_at',
'notify_days_before',
])]
class Subscription extends Model
{
use HasFactory;
use SoftDeletes;
/** @return array<string, string> */
protected function casts(): array
{
return [
'billing_cycle' => BillingCycle::class,
'status' => SubscriptionStatus::class,
'price' => 'decimal:2',
'started_at' => 'date',
'next_billing_date' => 'date',
'cancelled_at' => 'date',
];
}
/** @return BelongsTo<User, $this> */
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/** @return BelongsTo<Category, $this> */
public function category(): BelongsTo
{
return $this->belongsTo(Category::class);
}
/** @return HasMany<Payment, $this> */
public function payments(): HasMany
{
return $this->hasMany(Payment::class);
}
/**
* @param Builder<self> $query
* @return Builder<self>
*/
public function scopeForUser(Builder $query, User $user): Builder
{
return $query->where('user_id', $user->getKey());
}
/**
* @param Builder<self> $query
* @return Builder<self>
*/
public function scopeActive(Builder $query): Builder
{
return $query->where('status', SubscriptionStatus::Active);
}
/**
* @param Builder<self> $query
* @return Builder<self>
*
* Subscriptions whose next_billing_date falls between today and today+$days (inclusive).
*/
public function scopeDueWithin(Builder $query, int $days): Builder
{
return $query->whereBetween('next_billing_date', [
now()->toDateString(),
now()->addDays($days)->toDateString(),
]);
}
/**
* @param Builder<self> $query
* @return Builder<self>
*/
public function scopeDueForRenewal(Builder $query): Builder
{
return $query->whereDate('next_billing_date', '<=', now()->toDateString());
}
/**
* @param Builder<self> $query
* @return Builder<self>
*
* Matches each subscription's own notify_days_before window using a column expression.
*/
public function scopeDueForReminder(Builder $query): Builder
{
/** @var Connection $connection */
$connection = $query->getConnection();
$today = now()->toDateString();
if ($connection->getDriverName() === 'sqlite') {
return $query
->where('next_billing_date', '>=', $today)
->whereRaw(
"date(next_billing_date, '-' || notify_days_before || ' days') <= ?",
[$today],
);
}
return $query
->where('next_billing_date', '>=', $today)
->whereRaw(
'next_billing_date - notify_days_before <= CAST(? AS date)',
[$today],
);
}
/** @return Attribute<float, never> */
public function monthlyCost(): Attribute
{
return Attribute::make(
get: fn (): float => round((float) $this->price * $this->billing_cycle->perYear() / 12, 2),
);
}
/** @return Attribute<float, never> */
public function yearlyCost(): Attribute
{
return Attribute::make(
get: fn (): float => round((float) $this->price * $this->billing_cycle->perYear(), 2),
);
}
}