src/Entity/Installment.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Doctrine\Types\PlanDurationType;
  4. use App\Entity\SuperClass\BaseEntity;
  5. use App\Repository\InstallmentRepository;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. /**
  10. * @ORM\Entity(repositoryClass=InstallmentRepository::class)
  11. */
  12. class Installment extends BaseEntity
  13. {
  14. /**
  15. * @ORM\Column(type="plan_duration_type")
  16. */
  17. private $duration;
  18. /**
  19. * @ORM\Column(type="integer")
  20. */
  21. private $amount;
  22. /**
  23. * @ORM\ManyToOne(targetEntity=Product::class, inversedBy="installments")
  24. */
  25. private $item;
  26. /**
  27. * @ORM\Column(type="boolean")
  28. */
  29. private $active;
  30. /**
  31. * @ORM\OneToMany(targetEntity=OrderItem::class, mappedBy="payment_option")
  32. */
  33. private $orderItems;
  34. public function __construct()
  35. {
  36. $this->orderItems = new ArrayCollection();
  37. }
  38. public function getDuration(): ?string
  39. {
  40. return $this->duration;
  41. }
  42. public function setDuration(string $duration): self
  43. {
  44. $this->duration = $duration;
  45. if ($this->getItem()) {
  46. $price = $this->getItem()->getPrice();
  47. if ($duration != PlanDurationType::PAY_IN_FULL) {
  48. $interval = \DateInterval::createFromDateString($duration);
  49. $this->setAmount($price / $interval->m);
  50. } else {
  51. $this->setAmount($price);
  52. }
  53. }
  54. return $this;
  55. }
  56. public function getAmount(): ?int
  57. {
  58. return $this->amount;
  59. }
  60. public function setAmount(?int $amount): self
  61. {
  62. $this->amount = $amount;
  63. return $this;
  64. }
  65. public function getItem(): ?Product
  66. {
  67. return $this->item;
  68. }
  69. public function setItem(?Product $item): self
  70. {
  71. $price = $item->getPrice();
  72. if ($this->getDuration() != PlanDurationType::PAY_IN_FULL) {
  73. $interval = \DateInterval::createFromDateString($this->getDuration());
  74. $this->setAmount($price / $interval->m);
  75. } else {
  76. $this->setAmount($price);
  77. }
  78. $this->item = $item;
  79. return $this;
  80. }
  81. public function getActive(): ?bool
  82. {
  83. return $this->active;
  84. }
  85. public function setActive(bool $active): self
  86. {
  87. $this->active = $active;
  88. return $this;
  89. }
  90. /**
  91. * @return Collection<int, OrderItem>
  92. */
  93. public function getOrderItems(): Collection
  94. {
  95. return $this->orderItems;
  96. }
  97. public function addOrderItem(OrderItem $orderItem): self
  98. {
  99. if (!$this->orderItems->contains($orderItem)) {
  100. $this->orderItems[] = $orderItem;
  101. $orderItem->setPaymentOption($this);
  102. }
  103. return $this;
  104. }
  105. public function removeOrderItem(OrderItem $orderItem): self
  106. {
  107. if ($this->orderItems->removeElement($orderItem)) {
  108. // set the owning side to null (unless already changed)
  109. if ($orderItem->getPaymentOption() === $this) {
  110. $orderItem->setPaymentOption(null);
  111. }
  112. }
  113. return $this;
  114. }
  115. public function __toString()
  116. {
  117. return $this->getDuration();
  118. }
  119. }