src/Entity/Vista/Order.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity\Vista;
  4. use App\Entity\Local\OrderSeat;
  5. use DateTimeImmutable;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Swagger\Annotations as SWG;
  10. use Symfony\Component\Serializer\Annotation\Groups;
  11. /**
  12.  * @ORM\Entity(repositoryClass="App\Repository\OrderRepositoryORMDecorator")
  13.  * @ORM\HasLifecycleCallbacks()
  14.  * @ORM\Table(name="orders")
  15.  */
  16. class Order implements PayableInterface
  17. {
  18.     use OrderAndBooking;
  19.     const STATE_NEW 'new';
  20.     const STATE_PAYMENT_STARTED 'payment_started';
  21.     const STATE_PAYMENT_FINISHED 'payment_finished';
  22.     const STATE_RESERVED 'reserved';
  23.     const STATE_CANCELED 'canceled';
  24.     /**
  25.      * @var string
  26.      * @ORM\Id()
  27.      * @ORM\Column(type="guid")
  28.      * @ORM\GeneratedValue(strategy="UUID")
  29.      */
  30.     protected $id;
  31.     /**
  32.      * Original
  33.      *
  34.      * @Groups({"new_order"})
  35.      * @var string|null
  36.      * @ORM\Column(type="string", nullable=true)
  37.      */
  38.     protected $cinemaId;
  39.     /**
  40.      * Original
  41.      *
  42.      * @var string
  43.      * @ORM\Column(type="string")
  44.      */
  45.     protected $userSessionId;
  46.     /**
  47.      * Original
  48.      *
  49.      * @var integer|null
  50.      * @ORM\Column(type="integer", nullable=true)
  51.      */
  52.     protected $orderTotalValueInCents;
  53.     /**
  54.      * Original
  55.      *
  56.      * @ORM\ManyToOne(targetEntity="App\Entity\Vista\OrderSession", cascade={"persist", "remove"})
  57.      * @ORM\JoinColumn(name="session_guid", referencedColumnName="uuid")
  58.      * @var OrderSession
  59.      */
  60.     protected $session;
  61.     /**
  62.      * @var Ticket[]|Collection
  63.      * @ORM\OneToMany(
  64.      *     targetEntity="Ticket",
  65.      *     mappedBy="order",
  66.      *     cascade={"persist", "remove"},
  67.      *     orphanRemoval=true)
  68.      */
  69.     protected $tickets;
  70.     /**
  71.      * @var OrderConcessionItem[]|Collection
  72.      * @ORM\OneToMany(
  73.      *     targetEntity="OrderConcessionItem",
  74.      *     mappedBy="order",
  75.      *     cascade={"persist", "remove"},
  76.      *     orphanRemoval=true)
  77.      */
  78.     protected $orderConcessionItem;
  79.     /**
  80.      * @var string
  81.      * @ORM\Column(type="string")
  82.      */
  83.     protected $state self::STATE_NEW;
  84.     /**
  85.      * @var LoyaltyMember|null
  86.      * @ORM\ManyToOne(targetEntity="App\Entity\Vista\LoyaltyMember")
  87.      * @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
  88.      */
  89.     protected $user;
  90.     /**
  91.      * @var \DateTime
  92.      * @ORM\Column(type="date_immutable", nullable=true)
  93.      */
  94.     protected $bookingDate;
  95.     /**
  96.      * @var \DateTimeImmutable
  97.      * @ORM\Column(type="datetime_immutable")
  98.      */
  99.     protected $createdAt;
  100.     /**
  101.      * @var \DateTimeImmutable
  102.      * @ORM\Column(type="datetime_immutable")
  103.      */
  104.     protected $modifiedAt;
  105.     /**
  106.      * @SWG\Property(type="array", @SWG\Items(type="string"))
  107.      * @ORM\Column(type="array", nullable=true)
  108.      * @var Seat[]|null
  109.      */
  110.     protected $selection;
  111.     /**
  112.      * @var ConcessionItem
  113.      * @ORM\Column(type="object", nullable=true)
  114.      */
  115.     protected $concessionItem;
  116.     /**
  117.      * @var string
  118.      * @ORM\Column(type="string", nullable=true)
  119.      */
  120.     protected $cinemaName;
  121.     public function __construct()
  122.     {
  123.         $this->tickets = new ArrayCollection();
  124.         $this->orderConcessionItem = new ArrayCollection();
  125.     }
  126.     /**
  127.      * @return string
  128.      */
  129.     public function getId(): string
  130.     {
  131.         return $this->id;
  132.     }
  133.     /**
  134.      * @param string $id
  135.      *
  136.      * @return Order
  137.      */
  138.     public function setId(string $id): Order
  139.     {
  140.         $this->id $id;
  141.         return $this;
  142.     }
  143.     /**
  144.      * @return string|null
  145.      */
  146.     public function getCinemaId(): ?string
  147.     {
  148.         return $this->cinemaId;
  149.     }
  150.     /**
  151.      * @param string|null $cinemaId
  152.      * @return Order
  153.      */
  154.     public function setCinemaId(?string $cinemaId): Order
  155.     {
  156.         $this->cinemaId $cinemaId;
  157.         return $this;
  158.     }
  159.     /**
  160.      * @return string
  161.      */
  162.     public function getUserSessionId(): string
  163.     {
  164.         return $this->userSessionId;
  165.     }
  166.     /**
  167.      * @param string $userSessionId
  168.      * @return Order
  169.      */
  170.     public function setUserSessionId(string $userSessionId): Order
  171.     {
  172.         $this->userSessionId $userSessionId;
  173.         return $this;
  174.     }
  175.     /**
  176.      * @return int|null
  177.      */
  178.     public function getOrderTotalValueInCents(): ?int
  179.     {
  180.         return $this->orderTotalValueInCents;
  181.     }
  182.     /**
  183.      * @param int|null $orderTotalValueInCents
  184.      * @return Order
  185.      */
  186.     public function setOrderTotalValueInCents(?int $orderTotalValueInCents): Order
  187.     {
  188.         $this->orderTotalValueInCents $orderTotalValueInCents;
  189.         return $this;
  190.     }
  191.     /**
  192.      * @param OrderSession[] $sessions
  193.      * @return Order
  194.      */
  195.     public function setSessions($sessions): Order
  196.     {
  197.         if (empty($sessions)) {
  198.             return $this;
  199.         }
  200.         $session array_shift($sessions);
  201.         return $this->setSession($session);
  202.     }
  203.     /**
  204.      * @return OrderSession
  205.      */
  206.     public function getSession(): ?OrderSession
  207.     {
  208.         return $this->session;
  209.     }
  210.     /**
  211.      * @param OrderSession $session
  212.      *
  213.      * @return Order
  214.      */
  215.     public function setSession(OrderSession $session): Order
  216.     {
  217.         if( $session->getStartTime()){
  218.             $this->setBookingDate(\DateTimeImmutable::createFromMutable($session->getStartTime()));
  219.             $session->setCinemaId($this->cinemaId);
  220.             $this->session $session;
  221.         }
  222.         return $this;
  223.     }
  224.     /**
  225.      * @return string
  226.      */
  227.     public function getState(): ?string
  228.     {
  229.         return $this->state;
  230.     }
  231.     /**
  232.      * @param string $state
  233.      *
  234.      * @return Order
  235.      */
  236.     public function setState(string $state): Order
  237.     {
  238.         $this->state $state;
  239.         return $this;
  240.     }
  241.     /**
  242.      * @return LoyaltyMember|null
  243.      */
  244.     public function getUser(): ?LoyaltyMember
  245.     {
  246.         return $this->user;
  247.     }
  248.     /**
  249.      * @param LoyaltyMember $user
  250.      *
  251.      * @return Order
  252.      */
  253.     public function setUser(LoyaltyMember $user): Order
  254.     {
  255.         $this->user $user;
  256.         return $this;
  257.     }
  258.     /**
  259.      * @return Ticket[]|Collection
  260.      */
  261.     public function getTickets(): Collection
  262.     {
  263.         return $this->tickets;
  264.     }
  265.     /**
  266.      * @param Ticket[]|Collection $tickets
  267.      *
  268.      * @return Order
  269.      */
  270.     public function setTickets($tickets): Order
  271.     {
  272.         if (is_array($tickets)) {
  273.             $this->tickets = new ArrayCollection($tickets);
  274.         } else {
  275.             $this->tickets $tickets;
  276.         }
  277.         return $this;
  278.     }
  279.     /**
  280.      * @return \DateTimeImmutable
  281.      */
  282.     public function getBookingDate(): \DateTimeImmutable
  283.     {
  284.         if ($this->session) {
  285.             return \DateTimeImmutable::createFromMutable($this->session->getStartTime());
  286.         }
  287.         return $this->createdAt;
  288.     }
  289.     /**
  290.      * @param \DateTimeImmutable $bookingDate
  291.      *
  292.      * @return Order
  293.      */
  294.     public function setBookingDate(\DateTimeImmutable $bookingDate): Order
  295.     {
  296.         $this->bookingDate $bookingDate;
  297.         return $this;
  298.     }
  299.     /**
  300.      * @return \DateTimeImmutable
  301.      */
  302.     public function getCreatedAt(): \DateTimeImmutable
  303.     {
  304.         return $this->createdAt;
  305.     }
  306.     /**
  307.      * @param \DateTimeImmutable $createdAt
  308.      *
  309.      * @return Order
  310.      */
  311.     public function setCreatedAt(\DateTimeImmutable $createdAt): Order
  312.     {
  313.         $this->createdAt $createdAt;
  314.         return $this;
  315.     }
  316.     /**
  317.      * @return \DateTimeImmutable
  318.      */
  319.     public function getModifiedAt(): \DateTimeImmutable
  320.     {
  321.         return $this->modifiedAt;
  322.     }
  323.     /**
  324.      * @param \DateTimeImmutable $modifiedAt
  325.      *
  326.      * @return Order
  327.      */
  328.     public function setModifiedAt(\DateTimeImmutable $modifiedAt): Order
  329.     {
  330.         $this->modifiedAt $modifiedAt;
  331.         return $this;
  332.     }
  333.     /**
  334.      * @ORM\PrePersist()
  335.      */
  336.     public function onPrePersist()
  337.     {
  338.         $this->setCreatedAt(new \DateTimeImmutable());
  339.         $this->setModifiedAt(new \DateTimeImmutable());
  340.     }
  341.     /**
  342.      * @ORM\PreUpdate()
  343.      */
  344.     public function onPreUpdate()
  345.     {
  346.         $this->setModifiedAt(new \DateTimeImmutable());
  347.     }
  348.     /**
  349.      * Get tickets count
  350.      *
  351.      * @return int
  352.      */
  353.     public function getTicketsCount(): int
  354.     {
  355.         if ($this->getConcessionItem() instanceof ConcessionItem) {
  356.             return 1;
  357.         }
  358.         $cnt 0;
  359.         foreach ($this->getTickets() as $ticket) {
  360.             $cnt += count($ticket->getSeats());
  361.         }
  362.         return $cnt;
  363.     }
  364.     /**
  365.      * Get bonus tickets count
  366.      *
  367.      * @return int
  368.      */
  369.     public function getBonusTicketsCount(): int
  370.     {
  371.         $cnt 0;
  372.         foreach ($this->getTickets() as $ticket) {
  373.             if ($ticket->isLoyaltyTicket()) {
  374.                 $cnt += count($ticket->getSeats());
  375.             }
  376.         }
  377.         return $cnt;
  378.     }
  379.     /**
  380.      * @inheritdoc
  381.      */
  382.     public function getPaymentId(): string
  383.     {
  384.         return $this->getUserSessionId();
  385.     }
  386.     /**
  387.      * @inheritdoc
  388.      */
  389.     public function getPriceInCents(): int
  390.     {
  391.         return $this->getOrderTotalValueInCents();
  392.     }
  393.     /**
  394.      * @return Seat[]
  395.      */
  396.     public function getSelection(): array
  397.     {
  398.         return $this->selection ?? [];
  399.     }
  400.     /**
  401.      * @param Seat[]|null $selection
  402.      * @return Order
  403.      */
  404.     public function setSelection(?array $selection): Order
  405.     {
  406.         $this->selection $selection;
  407.         return $this;
  408.     }
  409.     /**
  410.      * @return ConcessionItem
  411.      */
  412.     public function getConcessionItem(): ?ConcessionItem
  413.     {
  414.         return $this->concessionItem;
  415.     }
  416.     /**
  417.      * @param ConcessionItem $concessionItem
  418.      *
  419.      * @return Order
  420.      */
  421.     public function setConcessionItem(ConcessionItem $concessionItem): Order
  422.     {
  423.         $this->concessionItem $concessionItem;
  424.         return $this;
  425.     }
  426.     /**
  427.      * @return OrderConcessionItem[]|Collection
  428.      */
  429.     public function getOrderConcessionItem(): ?Collection
  430.     {
  431.         return $this->orderConcessionItem;
  432.     }
  433.     /**
  434.      * @param OrderConcessionItem[]|Collection $orderConcessionItem
  435.      *
  436.      * @return Order
  437.      */
  438.     public function setOrderConcessionItem($orderConcessionItem): Order
  439.     {
  440.         $this->orderConcessionItem $orderConcessionItem;
  441.         return $this;
  442.     }
  443.     /**
  444.      * @return Order
  445.      */
  446.     public function setConcessions($concessions): Order
  447.     {
  448.         if( is_array($concessions)){
  449.             if (!empty($concessions)) {
  450.                 $oci = new ArrayCollection();
  451.                 foreach($concessions as $concession){
  452.                     $oci->add((new OrderConcessionItem())
  453.                         ->setId($concession['id'])
  454.                         ->setItemId($concession['itemId'])
  455.                         ->setDescription($concession['description'])
  456.                         ->setFinalPriceInCents($concession['finalPriceInCents'])
  457.                         ->setOrder($this)
  458.                     );
  459.                 }
  460.                 $this->setOrderConcessionItem($oci);
  461.             } else {
  462.                 // clear concessions
  463.                 $this->setOrderConcessionItem(new ArrayCollection());
  464.             }
  465.         } else {
  466.             $this->setOrderConcessionItem($concessions);
  467.         }
  468.         return $this;
  469.     }
  470.     /**
  471.      * @return string
  472.      */
  473.     public function getCinemaName(): ?string
  474.     {
  475.         return $this->cinemaName;
  476.     }
  477.     /**
  478.      * @param string $cinemaName
  479.      *
  480.      * @return Order
  481.      */
  482.     public function setCinemaName(string $cinemaName): Order
  483.     {
  484.         $this->cinemaName $cinemaName;
  485.         return $this;
  486.     }
  487.     /**
  488.      * Get movie name
  489.      *
  490.      * @return string
  491.      */
  492.     public function getDescription(): string
  493.     {
  494.         if ($this->getSession() instanceof OrderSession) {
  495.             return $this->getSession()->getFilmTitle();
  496.         } elseif ($this->getConcessionItem() instanceof ConcessionItem) {
  497.             return $this->getConcessionItem()->getDescription();
  498.         } else {
  499.             return '';
  500.         }
  501.     }
  502.     protected function getTicketsRowLabel(array $data = [])
  503.     {
  504.         /** @var Ticket $ticket */
  505.         $ticket $data['ticket'];
  506.         $seats array_values($ticket->getSeats() ? $ticket->getSeats()->toArray() : []);
  507.         if (!$seats) {
  508.             return null;
  509.         }
  510.         /** @var OrderSeat $item */
  511.         $item $seats[0];
  512.         return $item->getRowDisplay();
  513.     }
  514.     protected function getTicketsColumnsLabels()
  515.     {
  516.         $result = [];
  517.         foreach ($this->tickets ?: [] as $ticket) {
  518.             foreach ($ticket->getSeats() as $seat) {
  519.                 $result[] = $seat->getColumnDisplay() ?: '';
  520.             }
  521.         }
  522.         return $result;
  523.     }
  524.     public function getSessionDate(): ?DateTimeImmutable
  525.     {
  526.         if ($this->getSession()) {
  527.             return DateTimeImmutable::createFromMutable($this->getSession()->getStartTime());
  528.         } else {
  529.             return null;
  530.         }
  531.     }
  532. }