0), otherwise it is set to the * current date plus 30 days . */ #[ORM\Column(type: 'datetime', name: 'renew_at', nullable: true)] private ?DateTimeInterface $renewAt = null; #[ORM\OneToOne(targetEntity: OrderEntity::class, inversedBy: 'subscription')] private ?OrderEntity $order = null; #[ORM\ManyToOne(targetEntity: PlanSnapshotEntity::class)] #[ORM\JoinColumn(nullable: false)] private PlanSnapshotEntity $plan; #[ORM\ManyToOne(targetEntity: WorkspaceEntity::class, inversedBy: 'subscriptions')] #[ORM\JoinColumn(nullable: false)] private WorkspaceEntity $workspace; public static function createFromOrder(OrderEntity $order): self { $subs = new self( $order->getWorkspace(), $order->getPlan(), $order->getTrialPeriodDays() ); $subs->order = $order; $subs->externalId = $order->getExternalId(); $subs->paymentGateway = $order->getPaymentGateway(); if ($order->isPaid()) { $subs->cancelAt = null; $subs->canceledAt = null; $renewIn = $subs->trialPeriodDays->value > 0 ? $subs->trialPeriodDays->value : 30; $subs->renewAt = new DateTime( $subs->createdAt->format('Y-m-d H:i:s') . " +{$renewIn} days" ); } return $subs; } /** * @throws NotSubscriptionPlanException */ public function __construct( WorkspaceEntity $workspace, PlanSnapshotEntity $plan, ?TrialPeriodDays $trialPeriodDays = null, ) { if (!$plan->getBillingCycle()->isRenewable()) { throw new NotSubscriptionPlanException($plan); } $this->id = new Id(); $this->trialPeriodDays = $trialPeriodDays ?: new TrialPeriodDays(0); $this->usageCount = new Count(0); $this->paymentGateway = new PaymentGateway(); $this->externalId = new ExternalId(); $this->createdAt = new DateTimeImmutable(); $this->plan = $plan; $renewIn = $this->trialPeriodDays->value > 0 ? $this->trialPeriodDays->value : 30; $this->renewAt = new DateTime( $this->createdAt->format('Y-m-d H:i:s') . " +{$renewIn} days" ); if ($this->trialPeriodDays->value > 0) { $this->cancel(); } $this->workspace = $workspace; $workspace->addSubscription($this); } public function getId(): Id { return $this->id; } public function getTrialPeriodDays(): TrialPeriodDays { return $this->trialPeriodDays; } public function getUsageCount(): Count { return $this->usageCount; } public function getPaymentGateway(): PaymentGateway { return $this->paymentGateway; } public function getExternalId(): ExternalId { return $this->externalId; } /** @return DateTimeInterface */ public function getCreatedAt(): DateTimeInterface { return $this->createdAt; } /** @return null|DateTimeInterface */ public function getUpdatedAt(): ?DateTimeInterface { return $this->updatedAt; } public function getCanceledAt(): ?DateTimeInterface { return $this->canceledAt; } public function getCancelAt(): ?DateTimeInterface { return $this->cancelAt; } public function getEndedAt(): ?DateTimeInterface { return $this->endedAt; } public function getRenewAt(): ?DateTimeInterface { return $this->renewAt; } public function getOrder(): ?OrderEntity { return $this->order; } public function getPlan(): PlanSnapshotEntity { return $this->plan; } /** * Get remaining credit * * @return Count */ public function getCredit(): Count { $plan = $this->plan; if ($plan->getCreditCount()->value === null) { // Unlimited token credit return new Count(); } if ($this->usageCount->value === null) { // No usage yet return $plan->getCreditCount(); } $credit = (float) $plan->getCreditCount()->value - (float) $this->usageCount->value; return new Count($credit > 0 ? $credit : 0); } /** * Deduct credit from the subscription * * @param Count $count * @return Count The remaining credit after deduction */ public function deductCredit(Count $count): Count { $credit = $this->getCredit(); if ((float) $credit->value <= 0) { return $count; } if ( $credit->value === null || (float)$credit->value > (float)$count->value ) { $this->usageCount = new Count( (float) $this->usageCount->value + (float) $count->value ); return new Count(0); } $this->usageCount = new Count( (float) $this->usageCount->value + (float) $credit->value ); return new Count((float) $count->value - (float) $credit->value); } public function getStatus(): SubscriptionStatus { if ($this->endedAt) { return SubscriptionStatus::ENDED; } if ($this->canceledAt) { return SubscriptionStatus::CANCELED; } if ($this->renewAt) { if ( $this->trialPeriodDays->value > 0 && $this->createdAt > new DateTime('-' . $this->trialPeriodDays->value . ' days') ) { return SubscriptionStatus::TRIALING; } return SubscriptionStatus::ACTIVE; } return SubscriptionStatus::UNKNOWN; } public function cancel(): void { if ($this->canceledAt) { return; } $this->canceledAt = new DateTime(); if ($this->renewAt) { $this->cancelAt = $this->renewAt; $this->renewAt = null; return; } $this->cancelAt = new DateTime(); } public function end(): void { $this->cancel(); $this->endedAt = new DateTime(); } public function isExpired(): bool { if (!$this->cancelAt) { return false; } return $this->cancelAt < new DateTime(); } public function getWorkspace(): WorkspaceEntity { return $this->workspace; } #[ORM\PreUpdate] public function preUpdate(): void { $this->updatedAt = new DateTime(); } /** @return SubscriptionEntity */ public function renew(): self { if ( !$this->renewAt || $this->renewAt > new DateTime() ) { return $this; } // Reset usage count $this->usageCount = new Count(0); // Set next reset date $this->renewAt = new DateTime( $this->renewAt->format('Y-m-d H:i:s') . " +30 days" ); return $this; } }