File size: 9,642 Bytes
cf14c12 | 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | <?php
declare(strict_types=1);
namespace Billing\Domain\Entities;
use Billing\Domain\Exceptions\NotSubscriptionPlanException;
use Billing\Domain\ValueObjects\Count;
use Billing\Domain\ValueObjects\ExternalId;
use Billing\Domain\ValueObjects\PaymentGateway;
use Billing\Domain\ValueObjects\SubscriptionStatus;
use Billing\Domain\ValueObjects\TrialPeriodDays;
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
use Shared\Domain\ValueObjects\Id;
use Workspace\Domain\Entities\WorkspaceEntity;
#[ORM\Entity]
#[ORM\Table(name: 'subscription')]
#[ORM\UniqueConstraint(columns: ['payment_gateway', 'external_id'])]
#[ORM\HasLifecycleCallbacks]
class SubscriptionEntity
{
/** A unique numeric identifier of the entity. */
#[ORM\Embedded(class: Id::class, columnPrefix: false)]
private Id $id;
#[ORM\Embedded(class: TrialPeriodDays::class, columnPrefix: false)]
private TrialPeriodDays $trialPeriodDays;
#[ORM\Embedded(class: Count::class, columnPrefix: 'usage_')]
private Count $usageCount;
#[ORM\Embedded(class: PaymentGateway::class, columnPrefix: false)]
private PaymentGateway $paymentGateway;
#[ORM\Embedded(class: ExternalId::class, columnPrefix: false)]
private ExternalId $externalId;
/** Creation date and time of the entity */
#[ORM\Column(type: 'datetime', name: 'created_at')]
private DateTimeInterface $createdAt;
/** The date and time when the entity was last modified. */
#[ORM\Column(type: 'datetime', name: 'updated_at', nullable: true)]
private ?DateTimeInterface $updatedAt = null;
/** The date and time when the cancellation request received */
#[ORM\Column(type: 'datetime', name: 'canceled_at', nullable: true)]
private ?DateTimeInterface $canceledAt = null;
/** Cancel subscription at this datetime */
#[ORM\Column(type: 'datetime', name: 'cancel_at', nullable: true)]
private ?DateTimeInterface $cancelAt = null;
/** Datetime when the subscription is actually cancelled/ended */
#[ORM\Column(type: 'datetime', name: 'ended_at', nullable: true)]
private ?DateTimeInterface $endedAt = null;
/**
* The date and time to reset the credit usage next time.
* For one time payments, this value is always null.
*
* For recurring payments, this value is set to the next current
* reset_at plus 30 days.
*
* First reset date is set at the time of the activation to the
* trials_period_days later (if trial_days > 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;
}
}
|