src/Entity/Catalog/Order.php line 67

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Catalog;
  3. use ApiPlatform\Core\Annotation\ApiProperty;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use App\Controller\Api\CreateOrder;
  6. use App\Controller\Api\Gift\GetOrderByUser;
  7. use App\Entity\Purse\ItemPuntos as Item;
  8. use App\Entity\User\Address\Order as AddressOrder;
  9. use App\Entity\User\User;
  10. use App\Repository\Catalog\OrderRepository;
  11. use DateTime;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Doctrine\ORM\Mapping as ORM;
  15. use Gedmo\Timestampable\Traits\TimestampableEntity;
  16. use JMS\Serializer\Annotation as Serializer;
  17. use Ramsey\Uuid\Doctrine\UuidGenerator;
  18. use Symfony\Component\Serializer\Annotation\Groups;
  19. use Symfony\Component\Serializer\Annotation\SerializedName;
  20. /**
  21.  * @ApiResource(
  22.  *     attributes={
  23.  *          "normalization_context"={
  24.  *              "groups"={
  25.  *                  "Order:output",
  26.  *                  "Order:io",
  27.  *              },
  28.  *          },
  29.  *          "denormalization_context"={
  30.  *              "groups"={
  31.  *                  "Order:input",
  32.  *                  "Order:io",
  33.  *              },
  34.  *          },
  35.  *          "order"={"createdAt": "DESC"},
  36.  *      },
  37.  *     collectionOperations={
  38.  *          "get",
  39.  *          "get-by-user"={
  40.  *              "method"="GET",
  41.  *              "path"="/orders/user",
  42.  *              "access_control"="is_granted('ROLE_USER')",
  43.  *              "validation_groups"={"get_order_by_user"},
  44.  *              "controller"=GetOrderByUser::class,
  45.  *              "defaults"={"_api_receive"=false},
  46.  *          },
  47.  *     },
  48.  *     itemOperations={
  49.  *          "get",
  50.  *          "post-order"={
  51.  *              "method"="POST",
  52.  *              "path"="/post-order",
  53.  *              "access_control"="is_granted('ROLE_USER')",
  54.  *              "validation_groups"={"post_catalog_order"},
  55.  *              "controller"=CreateOrder::class,
  56.  *              "defaults"={"_api_receive"=false},
  57.  *          },
  58.  *     }
  59.  * )
  60.  *
  61.  * @ORM\Entity(repositoryClass=OrderRepository::class)
  62.  * @ORM\Table(name="`order`")
  63.  */
  64. class Order
  65. {
  66.     /**
  67.      * Hook timestampable behavior
  68.      * updates createdAt, updatedAt fields
  69.      */
  70.     use TimestampableEntity;
  71.     /**
  72.      * @ORM\Id
  73.      * @ORM\Column(type="uuid", unique=true)
  74.      * @ORM\GeneratedValue(strategy="CUSTOM")
  75.      * @ORM\CustomIdGenerator(class=UuidGenerator::class)
  76.      * @SerializedName("id")
  77.      * @ApiProperty(identifier=true)
  78.      * @Groups({
  79.      *     "Order:output",
  80.      *     "get_user_order_carts",
  81.      * })
  82.      */
  83.     private string $id;
  84.     /**
  85.      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="orders")
  86.      * @Groups({
  87.      *     "Order:output",
  88.      *     "User:io",
  89.      *     "get_user_order_carts",
  90.      * })
  91.      */
  92.     private ?User $user;
  93.     /**
  94.      * @ORM\OneToOne(targetEntity=AddressOrder::class, cascade={"persist", "remove"})
  95.      * @Groups({
  96.      *     "Order:output",
  97.      *     "get_user_order_carts",
  98.      * })
  99.      */
  100.     private ?AddressOrder $address;
  101.     /**
  102.      * @ORM\ManyToMany(targetEntity=Gift::class)
  103.      * @Groups({
  104.      *     "Order:output",
  105.      *     "get_user_order_carts",
  106.      *     "get_order_by_user",
  107.      * })
  108.      */
  109.     private Collection $gifts;
  110.     /**
  111.      * @ORM\OneToMany(targetEntity=GiftCode::class, mappedBy="order")
  112.      * @SerializedName("gift_codes")
  113.      * @Groups({
  114.      *     "Order:output",
  115.      *     "get_order_by_user",
  116.      * })
  117.      */
  118.     protected Collection $giftCodes;
  119.     /**
  120.      * @ORM\OneToMany(targetEntity=Expedition::class, mappedBy="order")
  121.      * @Groups({
  122.      *     "get_order_by_user",
  123.      *     "Order:output",
  124.      * })
  125.      */
  126.     private Collection $expeditions;
  127.     /**
  128.      * @var Collection<Item>
  129.      * @ORM\OneToMany(targetEntity=Item::class, mappedBy="order")
  130.      * @Groups({
  131.      *     "Order:output",
  132.      * })
  133.      */
  134.     private $items;
  135.     public function __construct()
  136.     {
  137.         $this->gifts = new ArrayCollection();
  138.         $this->giftCodes = new ArrayCollection();
  139.         $this->expeditions = new ArrayCollection();
  140.         $this->items = new ArrayCollection();
  141.     }
  142.     public function getId(): ?string
  143.     {
  144.         return $this->id;
  145.     }
  146.     public function getUser(): ?User
  147.     {
  148.         return $this->user;
  149.     }
  150.     public function setUser(?User $user): self
  151.     {
  152.         $this->user $user;
  153.         return $this;
  154.     }
  155.     public function getAddress(): ?AddressOrder
  156.     {
  157.         return $this->address;
  158.     }
  159.     public function setAddress(?AddressOrder $address): self
  160.     {
  161.         $this->address $address;
  162.         return $this;
  163.     }
  164.     public function getTotalPoints(): int
  165.     {
  166.         $total 0;
  167.         foreach( $this->getGifts() as $gift ) {
  168.             $total += $gift->getNbPuntos();
  169.         }
  170.         return $total;
  171.     }
  172.     /**
  173.      * @return Collection|Gift[]
  174.      */
  175.     public function getGifts(): Collection
  176.     {
  177.         return $this->gifts;
  178.     }
  179.     public function addGift(Gift $gift): self
  180.     {
  181.         if (!$this->gifts->contains($gift)) {
  182.             $this->gifts[] = $gift;
  183.         }
  184.         return $this;
  185.     }
  186.     public function removeGift(Gift $gift): self
  187.     {
  188.         $this->gifts->removeElement($gift);
  189.         return $this;
  190.     }
  191.     /**
  192.      * @return \DateTime
  193.      */
  194.     public function getCreatedAt(): \DateTime
  195.     {
  196.         return $this->createdAt;
  197.     }
  198.     /**
  199.      * @param \DateTime $createdAt
  200.      */
  201.     public function setCreatedAt(\DateTime $createdAt): void
  202.     {
  203.         $this->createdAt $createdAt;
  204.     }
  205.     /**
  206.      * @Groups({
  207.      *     "Order:output",
  208.      *     "get_user_order_carts",
  209.      * })
  210.      * @SerializedName("created_at")
  211.      */
  212.     public function getCreatedAtTimestampable(): ?\DateTimeInterface
  213.     {
  214.         return $this->createdAt;
  215.     }
  216.     public function getGiftCodes(): Collection
  217.     {
  218.         return $this->giftCodes;
  219.     }
  220.     public function addGiftCode(GiftCode $giftCode): self
  221.     {
  222.         if (!$this->giftCodes->contains($giftCode)) {
  223.             $this->giftCodes[] = $giftCode;
  224.         }
  225.         return $this;
  226.     }
  227.     public function removeGiftCode(GiftCode $giftCode): self
  228.     {
  229.         $this->giftCodes->removeElement($giftCode);
  230.         return $this;
  231.     }
  232.     public function getExpeditions(): Collection
  233.     {
  234.         return $this->expeditions;
  235.     }
  236.     public function addExpedition(Expedition $expedition): self
  237.     {
  238.         if (!$this->expeditions->contains($expedition)) {
  239.             $this->expeditions[] = $expedition;
  240.         }
  241.         return $this;
  242.     }
  243.     public function removeExpedition(Expedition $expedition): self
  244.     {
  245.         $this->expeditions->removeElement($expedition);
  246.         return $this;
  247.     }
  248.     /**
  249.      * @return Collection
  250.      */
  251.     public function getItems(): Collection
  252.     {
  253.         return $this->items;
  254.     }
  255.     /**
  256.      * @Serializer\VirtualProperty
  257.      * @SERIALIZER\Groups({"order-global"})
  258.      * @Serializer\SerializedName("action")
  259.      * @return string
  260.      */
  261.     public function getAction(): string
  262.     {
  263.         return 'create';
  264.     }
  265.     /**
  266.      * @Serializer\VirtualProperty
  267.      * @Serializer\Groups({"order-global"})
  268.      * @Serializer\SerializedName("channel")
  269.      * @return string
  270.      */
  271.     public function getChannel(): string
  272.     {
  273.         return 'FW';
  274.     }
  275. }