<?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\Tuer24OrderRepository;
#[ORM\Entity(repositoryClass: "Roothirsch\Tuer24Bundle\Repository\Tuer24OrderRepository")]
#[ORM\Table(name: 'roothirsch_tuer24_order')]
#[ORM\HasLifecycleCallbacks]
class Tuer24Order
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(nullable: true)]
private ?int $userId = null;
#[ORM\Column(nullable: true)]
private ?int $companyId = null;
#[ORM\Column(type: 'decimal', precision: 15, scale: 2)]
private ?string $totalPrice = '0.00';
#[ORM\Column(length: 20)]
private ?string $status = 'new';
#[ORM\Column(nullable: true)]
private ?int $shippingAddressId = null;
#[ORM\Column(nullable: true)]
private ?int $billingAddressId = null;
#[ORM\Column(length: 100, nullable: true)]
private ?string $shippingMethod = null;
#[ORM\Column(length: 100, nullable: true)]
private ?string $paymentMethod = null;
#[ORM\Column(length: 50)]
private ?string $paymentStatus = 'pending';
#[ORM\Column(length: 255, nullable: true)]
private ?string $paymentReference = null;
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
private ?\DateTimeImmutable $paidAt = null;
#[ORM\OneToMany(mappedBy: 'order', targetEntity: Tuer24OrderItem::class, orphanRemoval: true, cascade: ['persist', 'remove'])]
private Collection $items;
#[ORM\Column]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\Column]
private ?\DateTimeImmutable $updatedAt = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $notes = 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 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;
}
public function getTotalPrice(): ?string
{
return $this->totalPrice;
}
public function getTotalPriceAsFloat(): ?float
{
return $this->totalPrice !== null ? (float)$this->totalPrice : null;
}
public function setTotalPrice(string|float $totalPrice): self
{
$this->totalPrice = (string)$totalPrice;
return $this;
}
public function calculateTotalPrice(): self
{
$total = 0.0;
foreach ($this->items as $item) {
$total += (float)$item->getTotal();
}
$this->totalPrice = (string)$total;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}
public function getShippingAddressId(): ?int
{
return $this->shippingAddressId;
}
public function setShippingAddressId(?int $shippingAddressId): self
{
$this->shippingAddressId = $shippingAddressId;
return $this;
}
public function getBillingAddressId(): ?int
{
return $this->billingAddressId;
}
public function setBillingAddressId(?int $billingAddressId): self
{
$this->billingAddressId = $billingAddressId;
return $this;
}
public function getShippingMethod(): ?string
{
return $this->shippingMethod;
}
public function setShippingMethod(?string $shippingMethod): self
{
$this->shippingMethod = $shippingMethod;
return $this;
}
public function getPaymentMethod(): ?string
{
return $this->paymentMethod;
}
public function setPaymentMethod(?string $paymentMethod): self
{
$this->paymentMethod = $paymentMethod;
return $this;
}
public function getPaymentStatus(): ?string
{
return $this->paymentStatus;
}
public function setPaymentStatus(string $paymentStatus): self
{
$this->paymentStatus = $paymentStatus;
return $this;
}
public function getPaymentReference(): ?string
{
return $this->paymentReference;
}
public function setPaymentReference(?string $paymentReference): self
{
$this->paymentReference = $paymentReference;
return $this;
}
public function getPaidAt(): ?\DateTimeImmutable
{
return $this->paidAt;
}
public function setPaidAt(?\DateTimeImmutable $paidAt): self
{
$this->paidAt = $paidAt;
return $this;
}
public function markAsPaid(?string $paymentReference = null): self
{
$this->paymentStatus = 'paid';
if ($paymentReference) {
$this->paymentReference = $paymentReference;
}
$this->paidAt = new \DateTimeImmutable();
return $this;
}
/**
* @return Collection<int, Tuer24OrderItem>
*/
public function getItems(): Collection
{
return $this->items;
}
public function addItem(Tuer24OrderItem $item): self
{
if (!$this->items->contains($item)) {
$this->items->add($item);
$item->setOrder($this);
}
return $this;
}
public function removeItem(Tuer24OrderItem $item): self
{
if ($this->items->removeElement($item)) {
if ($item->getOrder() === $this) {
$item->setOrder(null);
}
}
return $this;
}
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 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;
}
public function getNotes(): ?string
{
return $this->notes;
}
public function setNotes(?string $notes): self
{
$this->notes = $notes;
return $this;
}
}