<?php
namespace App\Entity;
use App\Service\Helper\DateTimeHelper;
use App\Service\Helper\Generator;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Exception;
/**
* @ORM\Table(name="orders__orders")
* @ORM\Entity(repositoryClass="App\Repository\OrderRepository")
*/
class Order
{
const PAYMENT_TYPE_ONLINE = 'online';
const PAYMENT_TYPE_BOX_OFFICE = 'box_office';
use IdTrait;
/**
* @ORM\Column(name="created_at", type="datetime", nullable=false)
*/
private DateTime $createdAt;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
*/
private User $user;
/**
* @ORM\Column(name="crm_order_id", type="integer", nullable=true)
*/
private ?int $crmOrderId;
/**
* @ORM\Column(name="first_name", type="string", nullable=true)
*/
private ?string $firstName = null;
/**
* @ORM\Column(name="last_name", type="string", nullable=true)
*/
private ?string $lastName = null;
/**
* @ORM\Column(name="father_name", type="string", nullable=true)
*/
private ?string $fatherName = null;
/**
* @ORM\Column(name="phone", type="string", nullable=true)
*/
private ?string $phone = null;
/**
* @var Collection|OrderElement[]
*
* @ORM\OneToMany(targetEntity="App\Entity\OrderElement", mappedBy="order", cascade={"persist"})
*/
private $elements;
/**
* @ORM\Column(type="string", name="delivery_type", nullable=true)
*/
private ?string $deliveryType = null;
/**
* @ORM\Column(type="string", name="town", nullable=true)
*/
private ?string $town = null;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private ?int $postcode = null;
/**
* @ORM\Column(type="string", name="street", nullable=true)
*/
private ?string $street = null;
/**
* @ORM\Column(type="string", name="house", nullable=true)
*/
private ?string $house = null;
/**
* @ORM\Column(type="string", name="apartment", nullable=true)
*/
private ?string $apartment = null;
/**
* @ORM\Column(type="float", nullable=true)
*/
private ?float $longitude = null;
/**
* @ORM\Column(type="float", nullable=true)
*/
private ?float $latitude = null;
/**
* @ORM\Column(type="string", name="payment_type", nullable=true)
*/
private ?string $paymentType = null;
/**
* @ORM\Column(name="comment", type="text", nullable=true)
*/
private ?string $comment;
/**
* @ORM\Column(name="hash", type="string", unique=true)
*/
private string $hash;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\TransportCompany")
* @ORM\JoinColumn(name="transport_company_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
private ?TransportCompany $transportCompany = null;
/**
* @var Collection|SberbankAcquiringOrder[]
*
* @ORM\OneToMany(targetEntity="App\Entity\SberbankAcquiringOrder", mappedBy="order", cascade={"persist"})
*/
private $sberbankAcquiringOrders;
/**
* @ORM\Column(name="confirmed_date", type="datetime", nullable=true)
*/
private ?DateTime $confirmedDate = null;
/**
* @ORM\Column(name="collected_date", type="datetime", nullable=true)
*/
private ?DateTime $collectedDate = null;
/**
* @ORM\Column(name="issued_date", type="datetime", nullable=true)
*/
private ?DateTime $issuedDate = null;
/**
* @ORM\Column(name="canceled_date", type="datetime", nullable=true)
*/
private ?DateTime $canceledDate = null;
/**
* @throws Exception
*/
public function __construct(?int $crmOrderId, User $user)
{
$this->crmOrderId = $crmOrderId;
$this->user = $user;
$this->createdAt = DateTimeHelper::getNowDateTime();
$this->hash = Generator::generateOrderHash();
$this->elements = new ArrayCollection();
$this->sberbankAcquiringOrders = new ArrayCollection();
}
public function getCreatedAt(): DateTime
{
return $this->createdAt;
}
public function getCrmOrderId(): ?int
{
return $this->crmOrderId;
}
public function getUser(): User
{
return $this->user;
}
public function setUser(User $user): Order
{
$this->user = $user;
return $this;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(?string $firstName): Order
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(?string $lastName): Order
{
$this->lastName = $lastName;
return $this;
}
public function getFatherName(): ?string
{
return $this->fatherName;
}
public function setFatherName(?string $fatherName): Order
{
$this->fatherName = $fatherName;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(?string $phone): Order
{
$this->phone = $phone;
return $this;
}
/**
* @return Collection|OrderElement[]
*/
public function getElements()
{
return $this->elements;
}
public function addElement(OrderElement $orderElement): Order
{
if(!$this->getElements()->contains($orderElement)) $this->elements->add($orderElement);
return $this;
}
public function getDeliveryType(): ?string
{
return $this->deliveryType;
}
public function setDeliveryType(?string $deliveryType): Order
{
$this->deliveryType = $deliveryType;
return $this;
}
public function getPaymentType(): ?string
{
return $this->paymentType;
}
public function setPaymentType(?string $paymentType): Order
{
$this->paymentType = $paymentType;
return $this;
}
public function getComment(): ?string
{
return $this->comment;
}
public function setComment(?string $comment): Order
{
$this->comment = $comment;
return $this;
}
public function getTown(): ?string
{
return $this->town;
}
public function setTown(?string $town): Order
{
$this->town = $town;
return $this;
}
public function getPostcode(): ?int
{
return $this->postcode;
}
public function setPostcode(?int $postcode): Order
{
$this->postcode = $postcode;
return $this;
}
public function getStreet(): ?string
{
return $this->street;
}
public function setStreet(?string $street): Order
{
$this->street = $street;
return $this;
}
public function getHouse(): ?string
{
return $this->house;
}
public function setHouse(?string $house): Order
{
$this->house = $house;
return $this;
}
public function getApartment(): ?string
{
return $this->apartment;
}
public function setApartment(?string $apartment): Order
{
$this->apartment = $apartment;
return $this;
}
public function getLongitude(): ?float
{
return $this->longitude;
}
public function setLongitude(?float $longitude): Order
{
$this->longitude = $longitude;
return $this;
}
public function getLatitude(): ?float
{
return $this->latitude;
}
public function setLatitude(?float $latitude): Order
{
$this->latitude = $latitude;
return $this;
}
public function getHash(): string
{
return $this->hash;
}
public function getTransportCompany(): ?TransportCompany
{
return $this->transportCompany;
}
public function setTransportCompany(?TransportCompany $transportCompany): Order
{
$this->transportCompany = $transportCompany;
return $this;
}
public function setConfirmedDate(?DateTime $confirmedDate): Order
{
$this->confirmedDate = $confirmedDate;
return $this;
}
public function getConfirmedDate(): ?DateTime
{
return $this->confirmedDate;
}
public function setCollectedDate(?DateTime $collectedDate): Order
{
$this->collectedDate = $collectedDate;
return $this;
}
public function setIssuedDate(?DateTime $issuedDate): Order
{
$this->issuedDate = $issuedDate;
return $this;
}
public function getCanceledDate(): ?DateTime
{
return $this->canceledDate;
}
/**
* @return SberbankAcquiringOrder[]|Collection
*/
public function getSberbankAcquiringOrders(): array|Collection
{
return $this->sberbankAcquiringOrders;
}
public function getElementsPrice(): int
{
$price = 0;
foreach ($this->getElements() as $element) {
$price += $element->getPrice() * $element->getQuantity();
}
return $price;
}
public function getPaymentAmount(): int
{
$paymentAmount = 0;
foreach ($this->getSberbankAcquiringOrders() as $sberbankAcquiringOrder) {
if($sberbankAcquiringOrder->getStatus() !== SberbankAcquiringOrder::STATUS_SUCCESS) continue;
$paymentAmount += $sberbankAcquiringOrder->getAmount();
}
return $paymentAmount;
}
public function isPayed(): bool
{
return $this->getElementsPrice() === $this->getPaymentAmount();
}
}