<?php
namespace App\Entity\Catalog;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\Api\CreateOrder;
use App\Controller\Api\Gift\GetOrderByUser;
use App\Entity\Purse\ItemPuntos as Item;
use App\Entity\User\Address\Order as AddressOrder;
use App\Entity\User\User;
use App\Repository\Catalog\OrderRepository;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use JMS\Serializer\Annotation as Serializer;
use Ramsey\Uuid\Doctrine\UuidGenerator;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\SerializedName;
/**
* @ApiResource(
* attributes={
* "normalization_context"={
* "groups"={
* "Order:output",
* "Order:io",
* },
* },
* "denormalization_context"={
* "groups"={
* "Order:input",
* "Order:io",
* },
* },
* "order"={"createdAt": "DESC"},
* },
* collectionOperations={
* "get",
* "get-by-user"={
* "method"="GET",
* "path"="/orders/user",
* "access_control"="is_granted('ROLE_USER')",
* "validation_groups"={"get_order_by_user"},
* "controller"=GetOrderByUser::class,
* "defaults"={"_api_receive"=false},
* },
* },
* itemOperations={
* "get",
* "post-order"={
* "method"="POST",
* "path"="/post-order",
* "access_control"="is_granted('ROLE_USER')",
* "validation_groups"={"post_catalog_order"},
* "controller"=CreateOrder::class,
* "defaults"={"_api_receive"=false},
* },
* }
* )
*
* @ORM\Entity(repositoryClass=OrderRepository::class)
* @ORM\Table(name="`order`")
*/
class Order
{
/**
* Hook timestampable behavior
* updates createdAt, updatedAt fields
*/
use TimestampableEntity;
/**
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
* @SerializedName("id")
* @ApiProperty(identifier=true)
* @Groups({
* "Order:output",
* "get_user_order_carts",
* })
*/
private string $id;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="orders")
* @Groups({
* "Order:output",
* "User:io",
* "get_user_order_carts",
* })
*/
private ?User $user;
/**
* @ORM\OneToOne(targetEntity=AddressOrder::class, cascade={"persist", "remove"})
* @Groups({
* "Order:output",
* "get_user_order_carts",
* })
*/
private ?AddressOrder $address;
/**
* @ORM\ManyToMany(targetEntity=Gift::class)
* @Groups({
* "Order:output",
* "get_user_order_carts",
* "get_order_by_user",
* })
*/
private Collection $gifts;
/**
* @ORM\OneToMany(targetEntity=GiftCode::class, mappedBy="order")
* @SerializedName("gift_codes")
* @Groups({
* "Order:output",
* "get_order_by_user",
* })
*/
protected Collection $giftCodes;
/**
* @ORM\OneToMany(targetEntity=Expedition::class, mappedBy="order")
* @Groups({
* "get_order_by_user",
* "Order:output",
* })
*/
private Collection $expeditions;
/**
* @var Collection<Item>
* @ORM\OneToMany(targetEntity=Item::class, mappedBy="order")
* @Groups({
* "Order:output",
* })
*/
private $items;
public function __construct()
{
$this->gifts = new ArrayCollection();
$this->giftCodes = new ArrayCollection();
$this->expeditions = new ArrayCollection();
$this->items = new ArrayCollection();
}
public function getId(): ?string
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getAddress(): ?AddressOrder
{
return $this->address;
}
public function setAddress(?AddressOrder $address): self
{
$this->address = $address;
return $this;
}
public function getTotalPoints(): int
{
$total = 0;
foreach( $this->getGifts() as $gift ) {
$total += $gift->getNbPuntos();
}
return $total;
}
/**
* @return Collection|Gift[]
*/
public function getGifts(): Collection
{
return $this->gifts;
}
public function addGift(Gift $gift): self
{
if (!$this->gifts->contains($gift)) {
$this->gifts[] = $gift;
}
return $this;
}
public function removeGift(Gift $gift): self
{
$this->gifts->removeElement($gift);
return $this;
}
/**
* @return \DateTime
*/
public function getCreatedAt(): \DateTime
{
return $this->createdAt;
}
/**
* @param \DateTime $createdAt
*/
public function setCreatedAt(\DateTime $createdAt): void
{
$this->createdAt = $createdAt;
}
/**
* @Groups({
* "Order:output",
* "get_user_order_carts",
* })
* @SerializedName("created_at")
*/
public function getCreatedAtTimestampable(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function getGiftCodes(): Collection
{
return $this->giftCodes;
}
public function addGiftCode(GiftCode $giftCode): self
{
if (!$this->giftCodes->contains($giftCode)) {
$this->giftCodes[] = $giftCode;
}
return $this;
}
public function removeGiftCode(GiftCode $giftCode): self
{
$this->giftCodes->removeElement($giftCode);
return $this;
}
public function getExpeditions(): Collection
{
return $this->expeditions;
}
public function addExpedition(Expedition $expedition): self
{
if (!$this->expeditions->contains($expedition)) {
$this->expeditions[] = $expedition;
}
return $this;
}
public function removeExpedition(Expedition $expedition): self
{
$this->expeditions->removeElement($expedition);
return $this;
}
/**
* @return Collection
*/
public function getItems(): Collection
{
return $this->items;
}
/**
* @Serializer\VirtualProperty
* @SERIALIZER\Groups({"order-global"})
* @Serializer\SerializedName("action")
* @return string
*/
public function getAction(): string
{
return 'create';
}
/**
* @Serializer\VirtualProperty
* @Serializer\Groups({"order-global"})
* @Serializer\SerializedName("channel")
* @return string
*/
public function getChannel(): string
{
return 'FW';
}
}