src/App/Entity/Membership/BillwerkContract.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Membership;
  3. use App\Entity\User;
  4. use App\Utility\DateTimeUtility;
  5. use DateTime;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use InvalidArgumentException;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. /**
  10. * Please keep direct usage of this class as low as possible. Instead,
  11. * use BillwerkSynchronizationService and MembershipService as facades.
  12. *
  13. * @ORM\Entity(repositoryClass="App\Repository\BillwerkContractRepository")
  14. *
  15. * @ORM\Table(
  16. * name="billwerk_contracts",
  17. * indexes={
  18. *
  19. * @ORM\Index(name="users_id_updated_at_idx", columns={"customer_id", "updated_at"})
  20. * }
  21. * )
  22. *
  23. * @ORM\HasLifecycleCallbacks()
  24. */
  25. class BillwerkContract
  26. {
  27. public const ACTIVE_LIFECYCLE_STATUS = [
  28. self::STATUS_ACTIVE,
  29. self::STATUS_TRIAL,
  30. self::STATUS_IN_ORDER_PROCESS,
  31. ];
  32. public const STATUS_ACTIVE = 'Active';
  33. public const STATUS_TRIAL = 'InTrial';
  34. public const STATUS_IN_ORDER_PROCESS = 'InOrderProcess';
  35. public const STATUS_CANCELLED = 'Cancelled';
  36. public const STATUS_PAUSED = 'Paused';
  37. /**
  38. * @var string
  39. *
  40. * @ORM\Column(name="id", type="string")
  41. *
  42. * @ORM\Id
  43. */
  44. private $id;
  45. public function setId(string $id)
  46. {
  47. $this->id = $id;
  48. }
  49. public function getId()
  50. {
  51. return $this->id;
  52. }
  53. /**
  54. * @var string
  55. *
  56. * @ORM\Column(name="customer_id", type="string", length=255, nullable=false)
  57. *
  58. * @Assert\NotBlank()
  59. *
  60. * @Assert\Length(
  61. * min = 24,
  62. * max = 255,
  63. * )
  64. */
  65. private $customerId;
  66. public function setCustomerId(string $customerId)
  67. {
  68. $this->customerId = $customerId;
  69. }
  70. public function getCustomerId()
  71. {
  72. return $this->customerId;
  73. }
  74. /**
  75. * @var string
  76. *
  77. * @ORM\Column(name="lifecycle_status", type="string", length=255, nullable=false)
  78. *
  79. * @Assert\NotBlank()
  80. */
  81. private $lifecycleStatus;
  82. public function setLifecycleStatus(string $lifecycleStatus): BillwerkContract
  83. {
  84. $this->lifecycleStatus = $lifecycleStatus;
  85. return $this;
  86. }
  87. public function getLifecycleStatus()
  88. {
  89. return $this->lifecycleStatus;
  90. }
  91. /**
  92. * @ORM\Column(name="plan_variant_id", type="string", length=255, nullable=true)
  93. */
  94. private ?string $planVariantId;
  95. public function setPlanVariantId(?string $planVariantId = null): void
  96. {
  97. $this->planVariantId = $planVariantId;
  98. }
  99. public function getPlanVariantId(): ?string
  100. {
  101. return $this->planVariantId;
  102. }
  103. /**
  104. * @var string
  105. *
  106. * @ORM\Column(name="recurring_fee", type="float", nullable=true)
  107. */
  108. private $recurringFee;
  109. public function setRecurringFee(?float $recurringFee = null)
  110. {
  111. $this->recurringFee = $recurringFee;
  112. }
  113. public function getRecurringFee()
  114. {
  115. return $this->recurringFee;
  116. }
  117. /**
  118. * @var string
  119. *
  120. * @ORM\Column(name="currency", type="string", nullable=false)
  121. */
  122. private $currency;
  123. public function setCurrency(string $currency)
  124. {
  125. $this->currency = $currency;
  126. }
  127. public function getCurrency()
  128. {
  129. return $this->currency;
  130. }
  131. /**
  132. * @var string
  133. *
  134. * @ORM\Column(name="payload", type="text", nullable=false)
  135. *
  136. * @Assert\NotBlank()
  137. */
  138. private $payload;
  139. public function setPayload(string $payload)
  140. {
  141. $this->payload = $payload;
  142. }
  143. public function getPayload()
  144. {
  145. return $this->payload;
  146. }
  147. /**
  148. * @ORM\Column(name="next_billing_at", type="datetime_microsecond", nullable=true)
  149. */
  150. private ?DateTime $nextBillingAt = null;
  151. public function getNextBillingAt(): ?DateTime
  152. {
  153. return $this->nextBillingAt;
  154. }
  155. public function setNextBillingAt(?DateTime $nextBillingAt = null): void
  156. {
  157. $this->nextBillingAt = $nextBillingAt;
  158. }
  159. /**
  160. * @ORM\Column(name="is_payment_escalated", type="boolean", nullable=false)
  161. */
  162. private $isPaymentEscalated = false;
  163. public function isPaymentEscalated(): bool
  164. {
  165. return $this->isPaymentEscalated;
  166. }
  167. public function setIsPaymentEscalated(bool $isPaymentEscalated)
  168. {
  169. $this->isPaymentEscalated = $isPaymentEscalated;
  170. }
  171. /**
  172. * @var DateTime
  173. *
  174. * @ORM\Column(name="created_at", type="datetime")
  175. */
  176. private $createdAt;
  177. /**
  178. * @ORM\PrePersist()
  179. */
  180. public function onCreate()
  181. {
  182. $this->setCreatedAt(DateTimeUtility::createDateTimeUtc());
  183. $this->setUpdatedAt(DateTimeUtility::createDateTimeUtc());
  184. }
  185. public function getCreatedAt(): DateTime
  186. {
  187. return $this->createdAt;
  188. }
  189. public function setCreatedAt(DateTime $createdAt): void
  190. {
  191. $this->createdAt = $createdAt;
  192. }
  193. /**
  194. * @var DateTime
  195. *
  196. * @ORM\Column(name="updated_at", type="datetime", nullable=false)
  197. */
  198. private $updatedAt;
  199. /**
  200. * @return DateTime
  201. */
  202. public function getUpdatedAt()
  203. {
  204. return $this->updatedAt;
  205. }
  206. public function setUpdatedAt(DateTime $updatedAt)
  207. {
  208. $this->updatedAt = $updatedAt;
  209. }
  210. /**
  211. * @ORM\PreUpdate()
  212. */
  213. public function onUpdate()
  214. {
  215. $this->setUpdatedAt(DateTimeUtility::createDateTimeUtc());
  216. }
  217. /**
  218. * @ORM\Column(name="last_synchronized_at", type="datetime", nullable=true)
  219. */
  220. private ?DateTime $lastSynchronizedAt;
  221. public function getLastSynchronizedAt(): ?DateTime
  222. {
  223. return $this->lastSynchronizedAt;
  224. }
  225. public function setLastSynchronizedAt(DateTime $lastSynchronizedAt): void
  226. {
  227. $this->lastSynchronizedAt = $lastSynchronizedAt;
  228. }
  229. /**
  230. * @ORM\Column(name="ongoing_proactive_sync_initiated_at", type="datetime", nullable=true)
  231. */
  232. private ?DateTime $ongoingProactiveSyncInitiatedAt;
  233. public function getOngoingProactiveSyncInitiatedAt(): ?DateTime
  234. {
  235. return $this->ongoingProactiveSyncInitiatedAt;
  236. }
  237. public function setOngoingProactiveSyncInitiatedAt(?DateTime $at): void
  238. {
  239. $this->ongoingProactiveSyncInitiatedAt = $at;
  240. }
  241. /**
  242. * @ORM\Column(name="number_of_consecutive_proactive_syncs_with_timeout", type="integer", nullable=false)
  243. */
  244. private int $numberOfProactiveSyncsWithTimeout = 0;
  245. public function getNumberOfConsecutiveProactiveSyncsWithTimeout(): int
  246. {
  247. return $this->numberOfProactiveSyncsWithTimeout;
  248. }
  249. public function incrementNumberOfConsecutiveProactiveSyncsWithTimeout(): void
  250. {
  251. ++$this->numberOfProactiveSyncsWithTimeout;
  252. }
  253. public function resetNumberOfConsecutiveProactiveSyncsWithTimeout(): void
  254. {
  255. $this->numberOfProactiveSyncsWithTimeout = 0;
  256. }
  257. /**
  258. * @var User
  259. *
  260. * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="billwerkContracts")
  261. *
  262. * @ORM\JoinColumn(name="users_id", onDelete="CASCADE")
  263. */
  264. private $user;
  265. public function getUser(): User
  266. {
  267. return $this->user;
  268. }
  269. public function setUser(User $user): void
  270. {
  271. $this->user = $user;
  272. }
  273. /**
  274. * @var bool
  275. *
  276. * @ORM\Column(type="boolean")
  277. */
  278. private $cancelled = false;
  279. public function isCancelled(): bool
  280. {
  281. return $this->cancelled;
  282. }
  283. public function setCancelled(bool $cancelled)
  284. {
  285. $this->cancelled = $cancelled;
  286. }
  287. public function isActive(): bool
  288. {
  289. return in_array($this->lifecycleStatus, self::ACTIVE_LIFECYCLE_STATUS);
  290. }
  291. public function isPaused(): bool
  292. {
  293. return $this->lifecycleStatus === self::STATUS_PAUSED;
  294. }
  295. public function __toString()
  296. {
  297. return (string)$this->getId();
  298. }
  299. /**
  300. * The follow up billwerk contract is the specific billwerk contract which is booked when the user enters into another
  301. * contract while having another currently active billwerk contract.
  302. * For example if the user upgrades its basic membership to a premium membership the premium membership billwerk contract
  303. * is considered to be the follow up billwerk contract for the previous active basic membership billwerk contract.
  304. * On the other hand, if a user ends its current billwerk contract with a membership cancellation and then, a few weeks or
  305. * even a minute later signs up for a new membership, the new billwerk contract is not considered a follow up billwerk contract.
  306. *
  307. * @ORM\ManyToOne(targetEntity="App\Entity\Membership\BillwerkContract", cascade={"persist"})
  308. *
  309. * @ORM\JoinColumn(name="follow_up_billwerk_contracts_id", referencedColumnName="id", onDelete="SET NULL")
  310. */
  311. private ?BillwerkContract $followUpBillwerkContract = null;
  312. public function getFollowUpBillwerkContract(): ?BillwerkContract
  313. {
  314. return $this->followUpBillwerkContract;
  315. }
  316. public function setFollowUpBillwerkContract(?BillwerkContract $followUpBillwerkContract)
  317. {
  318. if (!is_null($followUpBillwerkContract)) {
  319. if ($followUpBillwerkContract->getId() === $this->id) {
  320. throw new InvalidArgumentException("Cannot set follow up billwerk contract for billwerk contract {$this->id}, because the follow up billwerk contract and this billwerk contract are identical.");
  321. }
  322. }
  323. $this->followUpBillwerkContract = $followUpBillwerkContract;
  324. }
  325. }