<?php
namespace App\Entity\Shop\Order;
use App\Entity\BaseEntity;
use App\Entity\Generic\Customer\Address;
use App\Entity\Generic\Customer\Customer;
use App\Entity\Shop\Accounting\Transaction;
use App\Entity\Shop\Shop\Shop;
use App\Repository\Shop\OrderRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
#[ORM\Entity(repositoryClass: OrderRepository::class)]
#[ORM\Table(name: '`order`')]
class Order extends BaseEntity
{
#[ORM\Id]
#[ORM\Column(type: 'guid', unique: true)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: UuidGenerator::class)]
private ?string $id;
#[ORM\ManyToOne(inversedBy: 'orders')]
#[ORM\JoinColumn(nullable: false)]
private ?Customer $customer = null;
#[ORM\Column(type: Types::BIGINT)]
private ?string $shippingCost = null;
#[ORM\Column(type: Types::BIGINT,nullable: false)]
private ?string $taxAmount = null;
#[ORM\Column(type: Types::BIGINT)]
private ?string $totalPrice = null;
#[ORM\ManyToOne(inversedBy: 'orders')]
#[ORM\JoinColumn(nullable: false)]
private ?OrderStatus $status = null;
#[ORM\ManyToOne(inversedBy: 'orders')]
#[ORM\JoinColumn(nullable: true)]
private ?ShippingMethod $shippingMethod = null;
#[ORM\Column(length: 255,nullable: true)]
private ?string $trackingCode = null;
#[ORM\OneToMany(mappedBy: 'mainOrder', targetEntity: OrderItem::class, cascade: ['persist'], orphanRemoval: true)]
private Collection $orderItems;
#[ORM\ManyToOne(inversedBy: 'orders')]
#[ORM\JoinColumn(nullable: false)]
private ?Shop $shop = null;
#[ORM\ManyToOne(inversedBy: 'orders')]
private ?Coupon $coupon = null;
#[ORM\ManyToOne(inversedBy: 'orders')]
private ?Address $address = null;
#[ORM\Column(nullable: true)]
private ?string $paymentGateway = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $note = null;
#[ORM\OneToMany(mappedBy: 'paidOrder', targetEntity: Transaction::class)]
private Collection $transactions;
public function setPaymentGateway(?string $gateway): self
{
$this->paymentGateway = $gateway;
return $this;
}
public function getPaymentGateway(): ?string
{
return $this->paymentGateway;
}
public function __construct()
{
parent::__construct();
$this->setTaxAmount(0);
$this->setShippingCost(0);
$this->setTrackingCode(uniqid('TRK-' , false));
$this->orderItems = new ArrayCollection();
$this->transactions = new ArrayCollection();
}
public function getId(): ?string
{
return $this->id;
}
public function calculateTax(float $rate): void
{
$this->taxAmount = ($this->totalPrice - $this->shippingCost) * $rate / 100;
}
public function getCustomer(): ?Customer
{
return $this->customer;
}
public function setCustomer(?Customer $customer): static
{
$this->customer = $customer;
return $this;
}
public function getShippingCost(): ?string
{
return $this->shippingCost;
}
public function setShippingCost(string $shippingCost): static
{
$this->shippingCost = $shippingCost;
return $this;
}
public function getTaxAmount(): ?string
{
return $this->taxAmount;
}
public function setTaxAmount(string $taxAmount): static
{
$this->taxAmount = $taxAmount;
return $this;
}
public function getTotalPrice(): ?string
{
return $this->totalPrice;
}
public function setTotalPrice(string $totalPrice): static
{
$this->totalPrice = $totalPrice;
return $this;
}
public function getStatus(): ?OrderStatus
{
return $this->status;
}
public function setStatus(?OrderStatus $status): static
{
$this->status = $status;
return $this;
}
public function getShippingMethod(): ?ShippingMethod
{
return $this->shippingMethod;
}
public function setShippingMethod(?ShippingMethod $shippingMethod): static
{
$this->shippingMethod = $shippingMethod;
return $this;
}
/**
* @return Collection<int, OrderItem>
*/
public function getOrderItems(): Collection
{
return $this->orderItems;
}
public function addOrderItem(OrderItem $orderItem): static
{
if (!$this->orderItems->contains($orderItem)) {
$this->orderItems->add($orderItem);
$orderItem->setMainOrder($this);
}
return $this;
}
public function removeOrderItem(OrderItem $orderItem): static
{
if ($this->orderItems->removeElement($orderItem)) {
// set the owning side to null (unless already changed)
if ($orderItem->getMainOrder() === $this) {
$orderItem->setMainOrder(null);
}
}
return $this;
}
public function getTrackingCode(): ?string
{
return $this->trackingCode;
}
public function setTrackingCode(?string $trackingCode): void
{
$this->trackingCode = $trackingCode;
}
public function getShop(): ?Shop
{
return $this->shop;
}
public function setShop(?Shop $shop): static
{
$this->shop = $shop;
return $this;
}
public function getCoupon(): ?Coupon
{
return $this->coupon;
}
public function setCoupon(?Coupon $coupon): static
{
$this->coupon = $coupon;
return $this;
}
public function getAddress(): ?Address
{
return $this->address;
}
public function setAddress(?Address $address): static
{
$this->address = $address;
return $this;
}
public function getNote(): ?string
{
return $this->note;
}
public function setNote(?string $note): static
{
$this->note = $note;
return $this;
}
/**
* @return Collection<int, Transaction>
*/
public function getTransactions(): Collection
{
return $this->transactions;
}
public function addTransaction(Transaction $transaction): static
{
if (!$this->transactions->contains($transaction)) {
$this->transactions->add($transaction);
$transaction->setPaidOrder($this);
}
return $this;
}
public function removeTransaction(Transaction $transaction): static
{
if ($this->transactions->removeElement($transaction)) {
// set the owning side to null (unless already changed)
if ($transaction->getPaidOrder() === $this) {
$transaction->setPaidOrder(null);
}
}
return $this;
}
}