<?php
namespace App\Entity;
use App\Repository\OrderRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use \Ramsey\Uuid\UuidInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=OrderRepository::class)
* @ORM\Table(name="`order`")
*/
class Order
{
const STATUSES = [
'Не определен' => 'undefined',
'Создан' => 'created',
'Успешно' => 'succeeded',
'Отменен' => 'canceled',
];
public const STATUS_UNDEFINED = 'undefined';
public const STATUS_CREATED = 'created';
public const STATUS_SUCCEEDED = 'succeeded';
public const STATUS_CANCELED = 'canceled';
public const FIELDS = [
'Виджет' => 'widgetName',
'Создан' => 'createdAt',
'Дата изменения' => 'updatedAt',
'Кому' => 'recipientType',
'Способ Отправки' => 'deliveryType',
'Имя получателя' => 'recipientName',
'Имя отправителя' => 'senderName',
'№ заказа Тибериума' => 'tiberiumOrderId',
'№ заказа' => 'trackingNumber',
'№ заказа Виджета' => 'widgetOrderId',
'Статус' => 'getStatusText',
'E-mail Отправителя' => 'senderEmail',
'E-mail/Телефон Получателя' => 'recipientEmail',
'ID Платежа' => 'paymentDataId',
'Кол-во' => 'quantity',
'Стоимость руб.' => 'amount',
'Сумма руб.' => 'summ',
'Тип создания' => 'type'
];
public const TYPE_MANUAL = 'manual';
public const TYPE_SENDING = 'sending';
public const TYPES = [
'Вручную' => self::TYPE_MANUAL,
'Рассылка' => self::TYPE_SENDING,
];
/**
* @var UuidInterface $id
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="\Ramsey\Uuid\Doctrine\UuidGenerator")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Widget::class, inversedBy="orders", fetch="EAGER", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
private $widget;
/**
* Человекочитаемый номер заказа. Используется для передачи в Кассу при оплате. Передается клиентам в отбивках по заказу
* формат хххх-хххх-хххх
*
* @ORM\Column(type="string", length=50)
*/
private ?string $trackingNumber;
/**
* @ORM\Column(type="datetime_immutable")
*/
private $createdAt;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Choice(callback="getStatuses", message="Choose a valid status.")
*/
private $paymentStatus;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $updatedAt;
/**
* @ORM\OneToMany(targetEntity=OrderItem::class, mappedBy="widgetOrder")
*/
private $orderItems;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $paymentToken;
/**
* @ORM\Column(type="json", nullable=true)
*/
private $paymentData = [];
/**
* @ORM\OneToMany(targetEntity=HistorySend::class, mappedBy="orderId", orphanRemoval=true)
*/
private $historySends;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $type;
public function __toString(): string
{
return $this->id->toString();
}
public function __construct()
{
$this->orderItems = new ArrayCollection();
$this->historySends = new ArrayCollection();
}
/**
* @return string[]
*/
public function getStatuses(): array
{
return self::STATUSES;
}
public function getId(): ?UuidInterface
{
return $this->id;
}
public function getWidget(): ?Widget
{
return $this->widget;
}
public function setWidget(?Widget $widget): self
{
$this->widget = $widget;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getPaymentStatus(): ?string
{
return $this->paymentStatus;
}
public function setPaymentStatus(string $paymentStatus): self
{
$this->paymentStatus = $paymentStatus;
return $this;
}
public function getStatusText(): ? string
{
return array_search($this->paymentStatus, Order::STATUSES)??self::STATUSES['Не определен'];
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeImmutable $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return Collection|OrderItem[]
*/
public function getOrderItems(): Collection
{
return $this->orderItems;
}
public function addOrderItem(OrderItem $orderItem): self
{
if (!$this->orderItems->contains($orderItem)) {
$this->orderItems[] = $orderItem;
$orderItem->setWidgetOrder($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->getWidgetOrder() === $this) {
$orderItem->setWidgetOrder(null);
}
}
return $this;
}
public function getPaymentToken(): ?string
{
return $this->paymentToken;
}
public function setPaymentToken(?string $paymentToken): self
{
$this->paymentToken = $paymentToken;
return $this;
}
/**
* @return string[]|null
*/
public function getPaymentData(): ?array
{
return $this->paymentData;
}
public function setPaymentData(?array $paymentData): self
{
$this->paymentData = $paymentData;
return $this;
}
/**
* @return Collection|HistorySend[]
*/
public function getHistorySends(): Collection
{
return $this->historySends;
}
public function addHistorySend(HistorySend $historySend): self
{
if (!$this->historySends->contains($historySend)) {
$this->historySends[] = $historySend;
$historySend->setOrder($this);
}
return $this;
}
public function removeHistorySend(HistorySend $historySend): self
{
if ($this->historySends->removeElement($historySend)) {
// set the owning side to null (unless already changed)
if ($historySend->getOrder() === $this) {
$historySend->setOrder(null);
}
}
return $this;
}
/**
* @return string|null
*/
public function getType(): ?string
{
return $this->type;
}
/**
* @param string|null $type
* @return $this
*/
public function setType(?string $type): self
{
if (in_array($type, [self::TYPE_MANUAL, self::TYPE_SENDING, null])) {
$this->type = $type;
}
return $this;
}
/**
* @return bool
*/
public function isPaymentSucceeded(): bool
{
return $this->getPaymentStatus() === self::STATUS_SUCCEEDED;
}
/**
* @return string|null
*/
public function getTrackingNumber(): ?string
{
return $this->trackingNumber;
}
/**
* @param string|null $trackingNumber
* @return $this
*/
public function setTrackingNumber(?string $trackingNumber): self
{
$this->trackingNumber = $trackingNumber;
return $this;
}
}