<?php
namespace App\Entity;
use App\Repository\OrderRepository;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use JetBrains\PhpStorm\Pure;
use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
use function Symfony\Component\String\u;
#[ORM\Entity(repositoryClass: OrderRepository::class)]
#[ORM\Table(name: '`order`')]
class Order extends EntityStatus implements TimestampableInterface
{
use TimestampableTrait;
public const STATUS_WAITING_PAYMENT = 4;
public const STATUS_PAID = 5;
public const STATUS_CANCELED = 6;
public const STATUS_SHIPPED = 7;
public const STATUS_TREATED = 8;
public const TYPE_SHOP = 1;
public const TYPE_COTISATION = 2;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 32)]
private ?string $number = null;
#[ORM\ManyToOne(inversedBy: 'orders')]
#[ORM\JoinColumn(nullable: false)]
private ?Entreprise $firm = null;
#[ORM\Column(type: 'datetime')]
private ?DateTimeInterface $orderDate = null;
#[ORM\Column]
private ?int $type = self::TYPE_SHOP;
#[ORM\OneToMany(mappedBy: 'parentOrder', targetEntity: OrderLine::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
private Collection $orderLines;
#[ORM\Column(nullable: true)]
private ?float $totalAmount = null;
#[ORM\Column(type: Types::JSON)]
private array $billingAddress = [];
#[ORM\Column(type: Types::JSON)]
private array $deliveryAddress = [];
#[ORM\Column(length: 255, nullable: true)]
private ?string $shippingNumber = null;
#[ORM\OneToOne(mappedBy: 'parentOrder', cascade: ['persist', 'remove'])]
private ?Invoice $invoice = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $invoiceNumber = null;
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
private ?\DateTimeInterface $invoiceDate = null;
#[ORM\OneToOne(mappedBy: 'associetedOrder', cascade: ['persist', 'remove'])]
private ?Cotisation $cotisation = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: false)]
private ?PaymentType $paymentType = null;
#[ORM\Column(nullable: true)]
private ?array $bankRequest = [];
#[ORM\OneToMany(mappedBy: 'associatedOrder', targetEntity: BankResponse::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
private Collection $bankResponses;
#[ORM\ManyToOne(inversedBy: 'orders')]
private ?User $user = null;
#[Pure]
public function __construct()
{
$this->orderLines = new ArrayCollection();
$this->bankResponses = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getNumber(): ?string
{
return $this->number;
}
public function setNumber(string $number): self
{
$this->number = $number;
return $this;
}
public function getFirm(): ?Entreprise
{
return $this->firm;
}
public function setFirm(?Entreprise $firm): self
{
$this->firm = $firm;
return $this;
}
public function getOrderDate(): ?DateTimeInterface
{
return $this->orderDate;
}
public function setOrderDate(?DateTimeInterface $orderDate): self
{
$this->orderDate = $orderDate;
return $this;
}
public function getType(): ?int
{
return $this->type;
}
public function setType(int $type): self
{
$this->type = $type;
return $this;
}
/**
* @return Collection<int, OrderLine>
*/
public function getOrderLines(): Collection
{
return $this->orderLines;
}
public function addOrderLine(OrderLine $orderLine): self
{
if (!$this->orderLines->contains($orderLine)) {
$this->orderLines->add($orderLine);
$orderLine->setParentOrder($this);
}
return $this;
}
public function removeOrderLine(OrderLine $orderLine): self
{
if ($this->orderLines->removeElement($orderLine)) {
// set the owning side to null (unless already changed)
if ($orderLine->getParentOrder() === $this) {
$orderLine->setParentOrder(null);
}
}
return $this;
}
public function getTotalAmount(): ?float
{
if($this->totalAmount === null) {
$this->totalAmount = 0;
/** @var OrderLine $orderLine */
foreach($this->orderLines as $orderLine) {
if($orderLine->getProduct()->getTag() === 'mapa_part') continue;
$this->totalAmount += $orderLine->getTotalPrice();
}
}
return $this->totalAmount;
}
public function setTotalAmount(?float $totalAmount): self
{
$this->totalAmount = $totalAmount;
return $this;
}
public function getBillingAddress(): array
{
return $this->billingAddress;
}
public function setBillingAddress(array $billingAddress): self
{
$this->billingAddress = $billingAddress;
return $this;
}
public function getDeliveryAddress(): array
{
return $this->deliveryAddress;
}
public function setDeliveryAddress(array $deliveryAddress): self
{
$this->deliveryAddress = $deliveryAddress;
return $this;
}
public function getShippingNumber(): ?string
{
return $this->shippingNumber;
}
public function setShippingNumber(?string $shippingNumber): self
{
$this->shippingNumber = $shippingNumber;
return $this;
}
public function getInvoice(): ?Invoice
{
return $this->invoice;
}
public function setInvoice(Invoice $invoice): self
{
// set the owning side of the relation if necessary
if ($invoice->getParentOrder() !== $this) {
$invoice->setParentOrder($this);
}
$this->invoice = $invoice;
return $this;
}
public function generateInvoiceNumber(): string{
return $this->invoiceNumber ?? (Date('Y') . '-' . sprintf('%06d', $this->getId()));
}
public function getInvoiceNumber(): ?string
{
return $this->invoiceNumber;
}
public function setInvoiceNumber(?string $invoiceNumber): self
{
$this->invoiceNumber = $invoiceNumber;
return $this;
}
public function getInvoiceDate(): ?\DateTimeInterface
{
return $this->invoiceDate;
}
public function setInvoiceDate(?\DateTimeInterface $invoiceDate): self
{
$this->invoiceDate = $invoiceDate;
return $this;
}
public function getCotisation(): ?Cotisation
{
return $this->cotisation;
}
public function setCotisation(?Cotisation $cotisation): self
{
// unset the owning side of the relation if necessary
if ($cotisation === null && $this->cotisation !== null) {
$this->cotisation->setAssocietedOrder(null);
}
// set the owning side of the relation if necessary
if ($cotisation !== null && $cotisation->getAssocietedOrder() !== $this) {
$cotisation->setAssocietedOrder($this);
}
$this->cotisation = $cotisation;
return $this;
}
public function getPaymentType(): ?PaymentType
{
return $this->paymentType;
}
public function setPaymentType(?PaymentType $paymentType): self
{
$this->paymentType = $paymentType;
return $this;
}
public function getBankResponse(): ?array
{
/** @var BankResponse|null $bankResponse */
$bankResponse = $this->bankResponses->last();
if (!$bankResponse){
return null;
}
return $bankResponse->getResponse() ?? null;
}
public function setBankResponse(?array $response): ?BankResponse
{
if(null !== $response){
$bankResponse = new BankResponse();
$bankResponse->setResponse($response);
$bankResponse->setAssociatedOrder($this);
return $bankResponse;
}
return null;
}
public function getBankRequest(): array
{
return $this->bankRequest;
}
public function setBankRequest(?array $bankRequest): self
{
$this->bankRequest = $bankRequest;
return $this;
}
/**
* @return Collection<int, BankResponse>
*/
public function getBankResponses(): Collection
{
return $this->bankResponses;
}
public function addBankResponse(BankResponse $bankResponse): self
{
if (!$this->bankResponses->contains($bankResponse)) {
$this->bankResponses->add($bankResponse);
$bankResponse->setAssociatedOrder($this);
}
return $this;
}
public function removeBankResponse(BankResponse $bankResponse): self
{
if ($this->bankResponses->removeElement($bankResponse)) {
// set the owning side to null (unless already changed)
if ($bankResponse->getAssociatedOrder() === $this) {
$bankResponse->setAssociatedOrder(null);
}
}
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
}