2
2
3
3
namespace LucasDotDev \Soulbscription \Models \Concerns ;
4
4
5
- use Illuminate \Database \Eloquent \Builder ;
6
5
use LucasDotDev \Soulbscription \Models \Feature ;
7
6
use LucasDotDev \Soulbscription \Models \FeatureConsumption ;
8
7
use LucasDotDev \Soulbscription \Models \Plan ;
13
12
14
13
trait HasSubscriptions
15
14
{
16
- public function activePlans ()
17
- {
18
- return $ this ->plans ()
19
- ->wherePivot ('expires_at ' , '> ' , now ());
20
- }
21
-
22
15
public function featureConsumptions ()
23
16
{
24
17
return $ this ->morphMany (FeatureConsumption::class, 'subscriber ' );
25
18
}
26
19
27
- public function plans ()
28
- {
29
- return $ this ->belongsToMany (Plan::class, 'subscriptions ' , 'subscriber_id ' )
30
- ->as ('subscription ' )
31
- ->withPivot ([
32
- 'canceled_at ' ,
33
- 'expires_at ' ,
34
- 'started_at ' ,
35
- 'suppressed_at ' ,
36
- 'was_switched ' ,
37
- ])
38
- ->withTimestamps ();
39
- }
40
-
41
20
public function renewals ()
42
21
{
43
22
return $ this ->hasManyThrough (SubscriptionRenewal::class, Subscription::class, 'subscriber_id ' );
44
23
}
45
24
46
25
public function subscription ()
47
26
{
48
- return $ this ->morphOne (Subscription::class, 'subscriber ' )->ofMany (
49
- [
50
- 'started_at ' => 'max ' ,
51
- ],
52
- fn (Builder $ query ) => $ query ->started (),
53
- );
27
+ return $ this ->morphOne (Subscription::class, 'subscriber ' )->ofMany ('started_at ' , 'MAX ' );
54
28
}
55
29
56
30
public function canConsume ($ featureName , ?float $ consumption = null ): bool
@@ -65,7 +39,6 @@ public function canConsume($featureName, ?float $consumption = null): bool
65
39
66
40
$ currentConsumption = $ this ->featureConsumptions ()
67
41
->whereBelongsTo ($ feature )
68
- ->unexpired ()
69
42
->sum ('consumption ' );
70
43
71
44
return ($ currentConsumption + $ consumption ) <= $ feature ->pivot ->charges ;
@@ -100,17 +73,16 @@ public function consume($featureName, ?float $consumption = null)
100
73
'The feature has no enough charges to this consumption. ' ,
101
74
));
102
75
103
- $ consumedPlan = $ this ->activePlans ->first (fn (Plan $ plan ) => $ plan ->features ->firstWhere ('name ' , $ featureName ));
104
- $ feature = $ consumedPlan ->features ->firstWhere ('name ' , $ featureName );
76
+ $ feature = $ this ->subscription ->plan ->features ->firstWhere ('name ' , $ featureName );
105
77
106
78
$ consumptionExpiration = $ feature ->consumable
107
- ? $ feature ->calculateNextRecurrenceEnd ($ consumedPlan ->subscription ->started_at )
79
+ ? $ feature ->calculateNextRecurrenceEnd ($ this ->subscription ->started_at )
108
80
: null ;
109
81
110
82
$ this ->featureConsumptions ()
111
83
->make ([
112
84
'consumption ' => $ consumption ,
113
- 'expires_at ' => $ consumptionExpiration ,
85
+ 'expired_at ' => $ consumptionExpiration ,
114
86
])
115
87
->feature ()
116
88
->associate ($ feature )
@@ -123,12 +95,10 @@ public function subscribeTo(Plan $plan, $expiration = null, $startDate = null):
123
95
124
96
return tap (
125
97
$ this ->subscription ()
126
- ->make ([
127
- 'expires_at ' => $ expiration ,
128
- ])
98
+ ->make (['expired_at ' => $ expiration ])
129
99
->start ($ startDate )
130
100
->plan ()
131
- ->associate ($ plan ),
101
+ ->associate ($ plan )
132
102
)->save ();
133
103
}
134
104
@@ -147,17 +117,20 @@ public function switchTo(Plan $plan, $expiration = null, $immediately = true): S
147
117
->markAsSwitched ()
148
118
->save ();
149
119
150
- $ startDate = $ this ->subscription ->expires_at ;
120
+ $ startDate = $ this ->subscription ->expired_at ;
151
121
152
122
return $ this ->subscribeTo ($ plan , startDate: $ startDate );
153
123
}
154
124
155
125
private function getAvailableFeature (string $ featureName ): ?Feature
156
126
{
157
- $ this ->loadMissing ('activePlans.features ' );
127
+ $ this ->loadMissing ('subscription.plan.features ' );
128
+
129
+ if (empty ($ this ->subscription )) {
130
+ return null ;
131
+ }
158
132
159
- $ availableFeatures = $ this ->activePlans ->flatMap (fn (Plan $ plan ) => $ plan ->features );
160
- $ feature = $ availableFeatures ->firstWhere ('name ' , $ featureName );
133
+ $ feature = $ this ->subscription ->plan ->features ->firstWhere ('name ' , $ featureName );
161
134
162
135
return $ feature ;
163
136
}
0 commit comments