| @@ -3,8 +3,9 @@ | ||
| 3 | 3 | namespace Give\Subscriptions\Models; |
| 4 | 4 | |
| 5 | 5 | use DateTime; |
| 6 | 6 | use Exception; |
| 7 | +use Give\Campaigns\Models\Campaign; | |
| 7 | 8 | use Give\Donations\Models\Donation; |
| 8 | 9 | use Give\Donors\Models\Donor; |
| 9 | 10 | use Give\Framework\Models\Contracts\ModelCrud; |
| 10 | 11 | use Give\Framework\Models\Contracts\ModelHasFactory; |
| @@ -13,8 +14,9 @@ | ||
| 13 | 14 | use Give\Framework\Models\ValueObjects\Relationship; |
| 14 | 15 | use Give\Framework\PaymentGateways\PaymentGateway; |
| 15 | 16 | use Give\Framework\Support\ValueObjects\Money; |
| 16 | 17 | use Give\Subscriptions\Actions\GenerateNextRenewalForSubscription; |
| 18 | +use Give\Subscriptions\Actions\CalculateProjectedAnnualRevenue; | |
| 17 | 19 | use Give\Subscriptions\DataTransferObjects\SubscriptionQueryData; |
| 18 | 20 | use Give\Subscriptions\Factories\SubscriptionFactory; |
| 19 | 21 | use Give\Subscriptions\ValueObjects\SubscriptionMode; |
| 20 | 22 | use Give\Subscriptions\ValueObjects\SubscriptionPeriod; |
| @@ -19,16 +21,20 @@ | ||
| 19 | 21 | use Give\Subscriptions\ValueObjects\SubscriptionMode; |
| 20 | 22 | use Give\Subscriptions\ValueObjects\SubscriptionPeriod; |
| 21 | 23 | use Give\Subscriptions\ValueObjects\SubscriptionStatus; |
| 22 | 24 | |
| 25 | + | |
| 23 | 26 | /** |
| 24 | 27 | * Class Subscription |
| 25 | 28 | * |
| 29 | + * @since 4.11.0 added campaign ID property | |
| 30 | + * @since 4.10.0 added campaign relationship | |
| 26 | 31 | * @since 2.23.0 added the renewsAt property |
| 27 | 32 | * @since 2.19.6 |
| 28 | 33 | * |
| 29 | 34 | * @property int $id |
| 30 | 35 | * @property int $donationFormId |
| 36 | + * @property int $campaignId | |
| 31 | 37 | * @property DateTime $createdAt |
| 32 | 38 | * @property DateTime $renewsAt The date the subscription will renew next |
| 33 | 39 | * @property int $donorId |
| 34 | 40 | * @property SubscriptionPeriod $period |
| @@ -42,8 +48,10 @@ | ||
| 42 | 48 | * @property string $gatewayId |
| 43 | 49 | * @property string $gatewaySubscriptionId |
| 44 | 50 | * @property Donor $donor |
| 45 | 51 | * @property Donation[] $donations |
| 52 | + * @property float $projectedAnnualRevenue | |
| 53 | + * @property ?Campaign $campaign | |
| 46 | 54 | */ |
| 47 | 55 | class Subscription extends Model implements ModelCrud, ModelHasFactory |
| 48 | 56 | { |
| 49 | 57 | /** |
| @@ -51,8 +59,9 @@ | ||
| 51 | 59 | */ |
| 52 | 60 | protected $properties = [ |
| 53 | 61 | 'id' => 'int', |
| 54 | 62 | 'donationFormId' => 'int', |
| 63 | + 'campaignId' => 'int', | |
| 55 | 64 | 'createdAt' => DateTime::class, |
| 56 | 65 | 'renewsAt' => DateTime::class, |
| 57 | 66 | 'donorId' => 'int', |
| 58 | 67 | 'period' => SubscriptionPeriod::class, |
| @@ -64,8 +73,9 @@ | ||
| 64 | 73 | 'feeAmountRecovered' => Money::class, |
| 65 | 74 | 'status' => SubscriptionStatus::class, |
| 66 | 75 | 'gatewaySubscriptionId' => ['string', ''], |
| 67 | 76 | 'gatewayId' => 'string', |
| 77 | + 'projectedAnnualRevenue' => Money::class, | |
| 68 | 78 | ]; |
| 69 | 79 | |
| 70 | 80 | /** |
| 71 | 81 | * @inheritdoc |
| @@ -72,8 +82,10 @@ | ||
| 72 | 82 | */ |
| 73 | 83 | protected $relationships = [ |
| 74 | 84 | 'donor' => Relationship::BELONGS_TO, |
| 75 | 85 | 'donations' => Relationship::HAS_MANY, |
| 86 | + 'notes' => Relationship::HAS_MANY, | |
| 87 | + 'campaign' => Relationship::BELONGS_TO, | |
| 76 | 88 | ]; |
| 77 | 89 | |
| 78 | 90 | /** |
| 79 | 91 | * Find subscription by ID |
| @@ -109,10 +121,21 @@ | ||
| 109 | 121 | return give()->donations->queryBySubscriptionId($this->id); |
| 110 | 122 | } |
| 111 | 123 | |
| 112 | 124 | /** |
| 125 | + * @since 4.8.0 | |
| 126 | + * | |
| 127 | + * @return ModelQueryBuilder<SubscriptionNote> | |
| 128 | + */ | |
| 129 | + public function notes(): ModelQueryBuilder | |
| 130 | + { | |
| 131 | + return give()->subscriptions->notes->queryBySubscriptionId($this->id); | |
| 132 | + } | |
| 133 | + | |
| 134 | + /** | |
| 113 | 135 | * Get Subscription notes |
| 114 | 136 | * |
| 137 | + * @deprecated Access notes via $subscription->notes()->getAll() instead. | |
| 115 | 138 | * @since 2.19.6 |
| 116 | 139 | * |
| 117 | 140 | * @return object[] |
| 118 | 141 | */ |
| @@ -117,8 +140,10 @@ | ||
| 117 | 140 | * @return object[] |
| 118 | 141 | */ |
| 119 | 142 | public function getNotes(): array |
| 120 | 143 | { |
| 144 | + _give_deprecated_function(__METHOD__, '4.6.0', '$subscription->notes()->getAll()'); | |
| 145 | + | |
| 121 | 146 | return give()->subscriptions->getNotesBySubscriptionId($this->id); |
| 122 | 147 | } |
| 123 | 148 | |
| 124 | 149 | /** |
| @@ -150,12 +175,19 @@ | ||
| 150 | 175 | |
| 151 | 176 | /** |
| 152 | 177 | * Returns the donation that began the subscription. |
| 153 | 178 | * |
| 179 | + * @since 4.8.0 Returns null if no initial donation found. | |
| 154 | 180 | * @since 2.23.0 |
| 155 | 181 | */ |
| 156 | - public function initialDonation(): Donation | |
| 182 | + public function initialDonation(): ?Donation | |
| 157 | 183 | { |
| 184 | + $initialDonationId = give()->subscriptions->getInitialDonationId($this->id); | |
| 185 | + | |
| 186 | + if (!$initialDonationId) { | |
| 187 | + return null; | |
| 188 | + } | |
| 189 | + | |
| 158 | 190 | return Donation::find(give()->subscriptions->getInitialDonationId($this->id)); |
| 159 | 191 | } |
| 160 | 192 | |
| 161 | 193 | /** |
| @@ -163,9 +195,9 @@ | ||
| 163 | 195 | * @since 2.19.6 |
| 164 | 196 | * |
| 165 | 197 | * @throws Exception |
| 166 | 198 | */ |
| 167 | - public static function create(array $attributes) | |
| 199 | + public static function create(array $attributes): Subscription | |
| 168 | 200 | { |
| 169 | 201 | $subscription = new static($attributes); |
| 170 | 202 | $subscription->save(); |
| 171 | 203 | |
| @@ -172,8 +204,49 @@ | ||
| 172 | 204 | return $subscription; |
| 173 | 205 | } |
| 174 | 206 | |
| 175 | 207 | /** |
| 208 | + * @since 3.20.0 | |
| 209 | + * @throws Exception | |
| 210 | + */ | |
| 211 | + public function createRenewal(array $attributes = []): Donation | |
| 212 | + { | |
| 213 | + return give()->subscriptions->createRenewal($this, $attributes); | |
| 214 | + } | |
| 215 | + | |
| 216 | + /** | |
| 217 | + * @since 3.20.0 | |
| 218 | + */ | |
| 219 | + public function shouldCreateRenewal(): bool | |
| 220 | + { | |
| 221 | + if (!$this->status->isActive()) { | |
| 222 | + return false; | |
| 223 | + } | |
| 224 | + | |
| 225 | + return $this->isIndefinite() || $this->totalDonations() < $this->installments; | |
| 226 | + } | |
| 227 | + | |
| 228 | + /** | |
| 229 | + * @since 3.20.0 | |
| 230 | + */ | |
| 231 | + public function totalDonations(): int | |
| 232 | + { | |
| 233 | + return $this->donations()->count(); | |
| 234 | + } | |
| 235 | + | |
| 236 | + /** | |
| 237 | + * @since 3.20.0 | |
| 238 | + */ | |
| 239 | + public function shouldEndSubscription(): bool | |
| 240 | + { | |
| 241 | + if ($this->isIndefinite()) { | |
| 242 | + return false; | |
| 243 | + } | |
| 244 | + | |
| 245 | + return $this->totalDonations() >= $this->installments; | |
| 246 | + } | |
| 247 | + | |
| 248 | + /** | |
| 176 | 249 | * @since 2.20.0 mutate model in repository and return void |
| 177 | 250 | * @since 2.19.6 |
| 178 | 251 | * |
| 179 | 252 | * @return void |
| @@ -198,8 +271,24 @@ | ||
| 198 | 271 | return give()->subscriptions->delete($this); |
| 199 | 272 | } |
| 200 | 273 | |
| 201 | 274 | /** |
| 275 | + * @since 4.8.0 | |
| 276 | + */ | |
| 277 | + public function trash(): bool | |
| 278 | + { | |
| 279 | + return give()->subscriptions->trash($this); | |
| 280 | + } | |
| 281 | + | |
| 282 | + /** | |
| 283 | + * @since 4.12.0 | |
| 284 | + */ | |
| 285 | + public function unTrash(): bool | |
| 286 | + { | |
| 287 | + return give()->subscriptions->unTrash($this); | |
| 288 | + } | |
| 289 | + | |
| 290 | + /** | |
| 202 | 291 | * @since 2.20.0 |
| 203 | 292 | * |
| 204 | 293 | * @param bool $force Set to true to ignore the status of the subscription |
| 205 | 294 | * |
| @@ -275,6 +364,24 @@ | ||
| 275 | 364 | */ |
| 276 | 365 | public static function factory(): SubscriptionFactory |
| 277 | 366 | { |
| 278 | 367 | return new SubscriptionFactory(static::class); |
| 368 | + } | |
| 369 | + | |
| 370 | + /** | |
| 371 | + * @since 4.8.0 | |
| 372 | + */ | |
| 373 | + public function projectedAnnualRevenue(): Money | |
| 374 | + { | |
| 375 | + return give(CalculateProjectedAnnualRevenue::class)($this); | |
| 376 | + } | |
| 377 | + | |
| 378 | + /** | |
| 379 | + * @since 4.10.0 | |
| 380 | + * | |
| 381 | + * @return ModelQueryBuilder<Campaign> | |
| 382 | + */ | |
| 383 | + public function campaign(): ModelQueryBuilder | |
| 384 | + { | |
| 385 | + return give()->campaigns->queryByFormId($this->donationFormId); | |
| 279 | 386 | } |
| 280 | 387 | } |