<?php
namespace Roothirsch\Tuer24Bundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Roothirsch\Tuer24Bundle\Repository\Tuer24CartRepository;
#[ORM\Entity(repositoryClass: "Roothirsch\Tuer24Bundle\Repository\Tuer24CartRepository")]
#[ORM\Table(name: 'roothirsch_tuer24_cart')]
#[ORM\HasLifecycleCallbacks]
class Tuer24Cart
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $sessionId = null;
#[ORM\Column(nullable: true)]
private ?int $userId = null;
#[ORM\Column(nullable: true)]
private ?int $companyId = null;
#[ORM\OneToMany(mappedBy: 'cart', targetEntity: Tuer24CartItem::class, orphanRemoval: true, cascade: ['persist', 'remove'])]
private Collection $items;
#[ORM\Column]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\Column]
private ?\DateTimeImmutable $updatedAt = null;
public function __construct()
{
$this->items = new ArrayCollection();
$this->createdAt = new \DateTimeImmutable();
$this->updatedAt = new \DateTimeImmutable();
}
#[ORM\PreUpdate]
public function preUpdate(): void
{
$this->updatedAt = new \DateTimeImmutable();
}
public function getId(): ?int
{
return $this->id;
}
public function getSessionId(): ?string
{
return $this->sessionId;
}
public function setSessionId(string $sessionId): self
{
$this->sessionId = $sessionId;
return $this;
}
public function getUserId(): ?int
{
return $this->userId;
}
public function setUserId(?int $userId): self
{
$this->userId = $userId;
return $this;
}
public function getCompanyId(): ?int
{
return $this->companyId;
}
public function setCompanyId(?int $companyId): self
{
$this->companyId = $companyId;
return $this;
}
/**
* @return Collection<int, Tuer24CartItem>
*/
public function getItems(): Collection
{
return $this->items;
}
public function addItem(Tuer24CartItem $item): self
{
if (!$this->items->contains($item)) {
$this->items->add($item);
$item->setCart($this);
}
return $this;
}
public function removeItem(Tuer24CartItem $item): self
{
if ($this->items->removeElement($item)) {
if ($item->getCart() === $this) {
$item->setCart(null);
}
}
return $this;
}
public function hasItem(int $productId): bool
{
foreach ($this->items as $item) {
if ($item->getProductId() === $productId) {
return true;
}
}
return false;
}
public function getItem(int $productId): ?Tuer24CartItem
{
foreach ($this->items as $item) {
if ($item->getProductId() === $productId) {
return $item;
}
}
return null;
}
public function clearItems(): self
{
$this->items->clear();
return $this;
}
public function countItems(): int
{
return $this->items->count();
}
public function getTotalQuantity(): int
{
$quantity = 0;
foreach ($this->items as $item) {
$quantity += $item->getQuantity();
}
return $quantity;
}
public function getTotalPrice(): float
{
$total = 0.0;
foreach ($this->items as $item) {
$total += $item->getTotal();
}
return $total;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeImmutable $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
}