<?php
namespace App\Entity;
use App\Doctrine\Types\PlanDurationType;
use App\Entity\SuperClass\BaseEntity;
use App\Repository\InstallmentRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=InstallmentRepository::class)
*/
class Installment extends BaseEntity
{
/**
* @ORM\Column(type="plan_duration_type")
*/
private $duration;
/**
* @ORM\Column(type="integer")
*/
private $amount;
/**
* @ORM\ManyToOne(targetEntity=Product::class, inversedBy="installments")
*/
private $item;
/**
* @ORM\Column(type="boolean")
*/
private $active;
/**
* @ORM\OneToMany(targetEntity=OrderItem::class, mappedBy="payment_option")
*/
private $orderItems;
public function __construct()
{
$this->orderItems = new ArrayCollection();
}
public function getDuration(): ?string
{
return $this->duration;
}
public function setDuration(string $duration): self
{
$this->duration = $duration;
if ($this->getItem()) {
$price = $this->getItem()->getPrice();
if ($duration != PlanDurationType::PAY_IN_FULL) {
$interval = \DateInterval::createFromDateString($duration);
$this->setAmount($price / $interval->m);
} else {
$this->setAmount($price);
}
}
return $this;
}
public function getAmount(): ?int
{
return $this->amount;
}
public function setAmount(?int $amount): self
{
$this->amount = $amount;
return $this;
}
public function getItem(): ?Product
{
return $this->item;
}
public function setItem(?Product $item): self
{
$price = $item->getPrice();
if ($this->getDuration() != PlanDurationType::PAY_IN_FULL) {
$interval = \DateInterval::createFromDateString($this->getDuration());
$this->setAmount($price / $interval->m);
} else {
$this->setAmount($price);
}
$this->item = $item;
return $this;
}
public function getActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): self
{
$this->active = $active;
return $this;
}
/**
* @return Collection<int, OrderItem>
*/
public function getOrderItems(): Collection
{
return $this->orderItems;
}
public function addOrderItem(OrderItem $orderItem): self
{
if (!$this->orderItems->contains($orderItem)) {
$this->orderItems[] = $orderItem;
$orderItem->setPaymentOption($this);
}
return $this;
}
public function removeOrderItem(OrderItem $orderItem): self
{
if ($this->orderItems->removeElement($orderItem)) {
// set the owning side to null (unless already changed)
if ($orderItem->getPaymentOption() === $this) {
$orderItem->setPaymentOption(null);
}
}
return $this;
}
public function __toString()
{
return $this->getDuration();
}
}