<?php
namespace App\Entity;
use App\Repository\OrderRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Blameable\Traits\BlameableEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: OrderRepository::class)]
#[ORM\Table(name: '`order`')]
class Order
{
use BlameableEntity; //Hook blameable behaviour. Updates createdBy, updatedBy fields
use TimestampableEntity; //Hook timestampable behaviour. Updates createdAt, updatedAt fields
#[ORM\Id]
#[ORM\GeneratedValue(strategy: "SEQUENCE")]
#[ORM\SequenceGenerator(sequenceName: "order_id_seq", initialValue: 10000000)]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'orders')]
#[ORM\JoinColumn(nullable: false)]
private ?User $buyer = null;
#[ORM\ManyToOne(inversedBy: 'orders')]
#[ORM\JoinColumn(nullable: false)]
private ?OrderStatus $status = null;
#[ORM\OneToMany(mappedBy: 'header', targetEntity: OrderDetail::class, cascade: ['persist', 'remove'])]
private Collection $orderDetails;
#[ORM\ManyToOne(inversedBy: 'orders')]
private ?Card $card = null;
#[ORM\Column(nullable: true)]
private ?bool $saveCard = false;
#[ORM\Column(nullable:true)]
#[Assert\NotNull()]
#[Assert\PositiveOrZero()]
protected ?float $shippingCost = null;
#[ORM\Column(nullable:true)]
#[Assert\NotNull()]
#[Assert\PositiveOrZero()]
protected ?float $finalShippingCost = null;
#[ORM\Column(length: 511, nullable:true)]
#[Assert\NotNull()]
#[Assert\Length(max: 511)]
protected ?string $shippingAddress = null;
#[ORM\Column(type: 'integer', nullable:true)]
private ?int $sharedAddressOrderId = null;
#[ORM\Column(length: 10, nullable:true)]
#[Assert\NotNull()]
#[Assert\Length(max: 10)]
protected ?string $shippingPostalCode = null;
#[ORM\Column(length: 50, nullable:true)]
#[Assert\NotNull()]
#[Assert\Length(max: 50)]
protected ?string $shippingCity = null;
#[ORM\Column(length: 100, nullable:true)]
#[Assert\NotNull()]
#[Assert\Length(max: 100)]
protected ?string $shippingCountry = null;
#[ORM\Column(length: 511, nullable:true)]
#[Assert\NotNull()]
#[Assert\Length(max: 511)]
protected ?string $billingAddress = null;
#[ORM\Column(length: 10, nullable:true)]
#[Assert\NotNull()]
#[Assert\Length(max: 10)]
protected ?string $billingPostalCode = null;
#[ORM\Column(length: 50, nullable:true)]
#[Assert\NotNull()]
#[Assert\Length(max: 50)]
protected ?string $billingCity = null;
#[ORM\Column(length: 100, nullable:true)]
#[Assert\NotNull()]
#[Assert\Length(max: 100)]
protected ?string $billingCountry = null;
#[ORM\Column(length: 2, nullable:true)]
#[Assert\NotNull()]
#[Assert\Length(max: 2)]
protected ?string $billingCountryIsoCode2 = null;
#[ORM\OneToOne(mappedBy: 'spentOrder', cascade: ['persist', 'remove'])]
private ?Wallet $walletSpent = null;
#[ORM\Column]
#[Assert\NotNull()]
#[Assert\PositiveOrZero()]
protected ?float $totalAmount = null;
#[ORM\Column(length: 500, nullable: true)]
private ?string $holdedId = null;
#[ORM\Column(length: 100, nullable: true)]
#[Assert\NotNull()]
#[Assert\Length(max: 100)]
private ?string $phone = null;
#[ORM\Column(nullable: true)]
private ?bool $needBill = false;
#[ORM\Column(length: 100, nullable: true)]
#[Assert\Length(max: 100)]
private ?string $cif = null;
#[ORM\OneToOne(mappedBy: 'spentOrderRefund', cascade: ['persist', 'remove'])]
private ?Wallet $walletSpentRefund = null;
#[ORM\Column(nullable: true)]
private ?string $shipping_province = null;
#[ORM\Column(length: 4, nullable: true)]
private ?string $shipping_country_iso = null;
#[ORM\Column(length: 50, nullable: true)]
#[Assert\Length(max: 50)]
private ?string $deliveryMethod = null;
#[ORM\ManyToOne(inversedBy: 'orders')]
private ?BuyingGroup $buyingGroup = null;
public function __construct()
{
$this->orderDetails = new ArrayCollection();
}
public function __toString()
{
return $this->getId();
}
public function getId(): ?int
{
return $this->id;
}
public function getBuyer(): ?User
{
return $this->buyer;
}
public function setBuyer(?User $buyer): self
{
$this->buyer = $buyer;
return $this;
}
public function getStatus(): ?OrderStatus
{
return $this->status;
}
public function setStatus(?OrderStatus $status): self
{
$this->status = $status;
return $this;
}
/**
* @return Collection<int, OrderDetail>
*/
public function getOrderDetails(): Collection
{
return $this->orderDetails;
}
public function getFirstOrderDetail(): ?OrderDetail
{
$firstOrderDetail = null;
for ($i = 0; $i <= count($this->orderDetails); $i++) {
if($i == 0) {
$firstOrderDetail = $this->orderDetails[$i];
break;
}
}
return $firstOrderDetail;
}
public function addOrderDetail(OrderDetail $orderDetail): self
{
if (!$this->orderDetails->contains($orderDetail)) {
$this->orderDetails->add($orderDetail);
$orderDetail->setHeader($this);
}
return $this;
}
public function removeOrderDetail(OrderDetail $orderDetail): self
{
if ($this->orderDetails->removeElement($orderDetail)) {
// set the owning side to null (unless already changed)
if ($orderDetail->getHeader() === $this) {
$orderDetail->setHeader(null);
}
}
return $this;
}
public function removeAllOrderDetail(): self
{
foreach($this->orderDetails as $orderDetail) {
// set the owning side to null (unless already changed)
if ($orderDetail->getHeader() === $this) {
$orderDetail->setHeader(null);
}
}
$this->orderDetails->clear();
return $this;
}
public function getCard(): ?Card
{
return $this->card;
}
public function setCard(?Card $card): self
{
$this->card = $card;
return $this;
}
public function getSaveCard(): ?bool
{
return $this->saveCard;
}
public function setSaveCard(?bool $saveCard): self
{
$this->saveCard = $saveCard;
return $this;
}
public function getShippingCost(): ?float
{
return $this->shippingCost;
}
public function setShippingCost(float $shippingCost): self
{
$this->shippingCost = $shippingCost;
return $this;
}
public function getFinalShippingCost(): ?float
{
return $this->finalShippingCost;
}
public function setFinalShippingCost(float $finalShippingCost): self
{
$this->finalShippingCost = $finalShippingCost;
return $this;
}
public function setShippingData(Address $shippingAddress): self
{
$this->shippingAddress = $shippingAddress->getAddress();
$this->shippingPostalCode = $shippingAddress->getPostalCode();
$this->shippingCity = $shippingAddress->getCity();
if ($shippingAddress->getCountry()) {
$this->shippingCountry = $shippingAddress->getCountry()->getName();
$this->shipping_country_iso = $shippingAddress->getCountry()->getIsoCode2();
}
if ($shippingAddress->getProvince()) {
$this->shipping_province = $shippingAddress->getProvince()->getName();
}
return $this;
}
public function getShippingAddress(): ?string
{
return $this->shippingAddress;
}
public function setShippingAddress(?string $shippingAddress): self
{
$this->shippingAddress = $shippingAddress;
return $this;
}
public function getSharedAddressOrderId(): ?int
{
return $this->sharedAddressOrderId;
}
public function setSharedAddressOrderId(?int $sharedAddressOrderId): void
{
$this->sharedAddressOrderId = $sharedAddressOrderId;
}
public function getShippingPostalCode(): ?string
{
return $this->shippingPostalCode;
}
public function setShippingPostalCode(?string $shippingPostalCode): self
{
$this->shippingPostalCode = $shippingPostalCode;
return $this;
}
public function getShippingCity(): ?string
{
return $this->shippingCity;
}
public function setShippingCity(?string $shippingCity): self
{
$this->shippingCity = $shippingCity;
return $this;
}
public function getShippingCountry(): ?string
{
return $this->shippingCountry;
}
public function setShippingCountry(?string $shippingCountry): self
{
$this->shippingCountry = $shippingCountry;
return $this;
}
public function setBillingData(Address $billingAddress): self
{
$this->billingAddress = $billingAddress->getAddress();
$this->billingPostalCode = $billingAddress->getPostalCode();
$this->billingCity = $billingAddress->getCity();
$this->billingCountry = $billingAddress->getCountry()->getName();
$this->billingCountryIsoCode2 = $billingAddress->getCountry()->getIsoCode2();
return $this;
}
public function getHoldedBillingData(): array
{
$billAddress = [
'address' => $this->getBillingAddress(),
'city' => $this->getBillingCity(),
'postalCode' => $this->getBillingPostalCode(),
'country' => $this->getBillingCountryIsoCode2(),
];
return $billAddress;
}
public function getBillingAddress(): ?string
{
return $this->billingAddress;
}
public function setBillingAddress(?string $billingAddress): self
{
$this->billingAddress = $billingAddress;
return $this;
}
public function getBillingPostalCode(): ?string
{
return $this->billingPostalCode;
}
public function setBillingPostalCode(?string $billingPostalCode): self
{
$this->billingPostalCode = $billingPostalCode;
return $this;
}
public function getBillingCity(): ?string
{
return $this->billingCity;
}
public function setBillingCity(?string $billingCity): self
{
$this->billingCity = $billingCity;
return $this;
}
public function getBillingCountry(): ?string
{
return $this->billingCountry;
}
public function setBillingCountry(?string $billingCountry): self
{
$this->billingCountry = $billingCountry;
return $this;
}
public function getBillingCountryIsoCode2(): ?string
{
return $this->billingCountryIsoCode2;
}
public function setBillingCountryIsoCode2(string $billingCountryIsoCode2): self
{
$this->billingCountryIsoCode2 = $billingCountryIsoCode2;
return $this;
}
public function getWalletSpent(): ?Wallet
{
return $this->walletSpent;
}
public function setWalletSpent(?Wallet $walletSpent): self
{
// unset the owning side of the relation if necessary
if ($walletSpent === null && $this->walletSpent !== null) {
$this->walletSpent->setSpentOrder(null);
}
// set the owning side of the relation if necessary
if ($walletSpent !== null && $walletSpent->getSpentOrder() !== $this) {
$walletSpent->setSpentOrder($this);
}
$this->walletSpent = $walletSpent;
return $this;
}
public function getTotalAmount(): ?float
{
return $this->totalAmount;
}
public function setTotalAmount(float $totalAmount): self
{
$this->totalAmount = $totalAmount;
return $this;
}
public function getHoldedId(): ?string
{
return $this->holdedId;
}
public function setHoldedId(?string $holdedId): self
{
$this->holdedId = $holdedId;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(?string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getFullShippingAddress(): ?string
{
return $this->shippingAddress . ' ' . $this->shippingPostalCode . ', ' . $this->shippingCity . ' ' . $this->shippingCountry;
}
public function getFullBillingAddress(): ?string
{
return $this->billingAddress . ' ' . $this->billingPostalCode . ', ' . $this->billingCity . ' ' . $this->billingCountry;
}
public function getNeedBill(): ?bool
{
return $this->needBill;
}
public function setNeedBill(?bool $needBill): self
{
$this->needBill = $needBill;
return $this;
}
public function getCif(): ?string
{
return $this->cif;
}
public function setCif(?string $cif): self
{
$this->cif = $cif;
return $this;
}
public function getWalletSpentRefund(): ?Wallet
{
return $this->walletSpentRefund;
}
public function setWalletSpentRefund(?Wallet $walletSpentRefund): self
{
// unset the owning side of the relation if necessary
if ($walletSpentRefund === null && $this->walletSpentRefund !== null) {
$this->walletSpentRefund->setSpentOrderRefund(null);
}
// set the owning side of the relation if necessary
if ($walletSpentRefund !== null && $walletSpentRefund->getSpentOrderRefund() !== $this) {
$walletSpentRefund->setSpentOrderRefund($this);
}
$this->walletSpentRefund = $walletSpentRefund;
return $this;
}
public function getShippingProvince(): ?string
{
return $this->shipping_province;
}
public function setShippingProvince(?string $shipping_province): self
{
$this->shipping_province = $shipping_province;
return $this;
}
public function getShippingCountryIso(): ?string
{
return $this->shipping_country_iso;
}
public function setShippingCountryIso(?string $shipping_country_iso): self
{
$this->shipping_country_iso = $shipping_country_iso;
return $this;
}
public function getDeliveryMethod(): ?string
{
return $this->deliveryMethod;
}
public function setDeliveryMethod(?string $deliveryMethod): self
{
$this->deliveryMethod = $deliveryMethod;
return $this;
}
public function getExportExcelType():?string{
if (empty($this->orderDetails)) {
return "STANDARD";
}
return $this->orderDetails[0]->isIndividualPurchase() ? "INDIVIDUAL" : "STANDARD";
}
public function getExportExcelSource():?string{
return "";
}
public function getExportExcelComments():?string{
return "";
}
public function getBuyingGroup(): ?BuyingGroup
{
return $this->buyingGroup;
}
public function setBuyingGroup(?BuyingGroup $buyingGroup): self
{
$this->buyingGroup = $buyingGroup;
return $this;
}
}