src/Entity/Order.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Enum\TransactionStatus;
  4. use App\Entity\Traits\Identifiable;
  5. use App\Entity\Traits\Timestampable;
  6. use App\Repository\OrderRepository;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. #[ORM\Entity(repositoryClassOrderRepository::class)]
  11. #[ORM\Table(name'`order`')]
  12. class Order
  13. {
  14.     use Identifiable;
  15.     use Timestampable;
  16.     #[ORM\Column(type'float')]
  17.     private $amount 0;
  18.     #[ORM\Column(type'string'length10nullabletrue)]
  19.     private $type;
  20.     #[ORM\OneToOne(inversedBy'order'targetEntityRegistration::class, cascade: ['persist''remove'])]
  21.     private $registration;
  22.     #[ORM\OneToMany(mappedBy'order'targetEntityCart::class, cascade: ['persist''remove'])]
  23.     private $carts;
  24.     #[ORM\OneToMany(mappedBy'order'targetEntityTransaction::class, cascade: ['persist''remove'])]
  25.     private $transactions;
  26.     public function __construct()
  27.     {
  28.         $this->carts = new ArrayCollection();
  29.         $this->transactions = new ArrayCollection();
  30.         $this->addCart(new Cart());
  31.     }
  32.     public function getAmount(): ?float
  33.     {
  34.         return $this->amount;
  35.     }
  36.     public function getDiscountAmount(?bool $aids null): float
  37.     {
  38.         $total 0;
  39.         foreach ($this->getCart()?->getDiscounts($aids) ?? [] as $cartItemDiscount) {
  40.             if ($discount $cartItemDiscount->getDiscount()) {
  41.                 $total += ($discount->getRemainingToPay() * -1);
  42.             } else {
  43.                 $total += $cartItemDiscount->getPrice();
  44.             }
  45.         }
  46.         return $total * -1;
  47.     }
  48.     public function getRemainingAmount($includeDiscounts true): float
  49.     {
  50.         $amount $this->getAmount();
  51.         $statuses = [TransactionStatus::WaitingTransactionStatus::Successful];
  52.         foreach ($this->getTransactions(statuses$statuses) as $transaction) {
  53.             if ($transaction->getStatus() === TransactionStatus::Successful) {
  54.                 $amount -= $transaction->getAmountPayed();
  55.             } elseif ($transaction instanceof TransactionCardMulti
  56.                 && $transaction->getStatus() === TransactionStatus::Waiting) {
  57.                 $amount -= $transaction->getAmount();
  58.             }
  59.         }
  60.         if ($includeDiscounts) {
  61.             $amount -= $this->getDiscountAmount(true);
  62.         }
  63.         return $amount;
  64.     }
  65.     public function getPayedAmount(): float
  66.     {
  67.         $amount 0;
  68.         foreach ($this->getTransactions() as $transaction) {
  69.             if ($transaction->getStatus() === TransactionStatus::Successful) {
  70.                 $amount += $transaction->getAmountPayed();
  71.             } elseif ($transaction instanceof TransactionCardMulti &&
  72.                 $transaction->getStatus() === TransactionStatus::Waiting &&
  73.                 $transaction->getCycle() > 1) {
  74.                 $amount += $transaction->getAmount();
  75.             }
  76.         }
  77.         return $amount;
  78.     }
  79.     public function setAmount(float $amount): self
  80.     {
  81.         $this->amount $amount;
  82.         return $this;
  83.     }
  84.     public function getType(): ?string
  85.     {
  86.         return $this->type;
  87.     }
  88.     public function setType(?string $type): self
  89.     {
  90.         $this->type $type;
  91.         return $this;
  92.     }
  93.     public function getRegistration(): ?Registration
  94.     {
  95.         return $this->registration;
  96.     }
  97.     public function setRegistration(?Registration $registration): self
  98.     {
  99.         $this->registration $registration;
  100.         return $this;
  101.     }
  102.     /**
  103.      * @return Collection<int, Cart>
  104.      */
  105.     public function getCarts(): Collection
  106.     {
  107.         return $this->carts;
  108.     }
  109.     public function addCart(Cart $cart): self
  110.     {
  111.         if (!$this->carts->contains($cart)) {
  112.             $this->carts[] = $cart;
  113.             $cart->setOrder($this);
  114.         }
  115.         return $this;
  116.     }
  117.     public function removeCart(Cart $cart): self
  118.     {
  119.         if ($this->carts->removeElement($cart)) {
  120.             // set the owning side to null (unless already changed)
  121.             if ($cart->getOrder() === $this) {
  122.                 $cart->setOrder(null);
  123.             }
  124.         }
  125.         return $this;
  126.     }
  127.     public function getCart(): ?Cart
  128.     {
  129.         $cart $this->carts->first();
  130.         return $cart === false null $cart;
  131.     }
  132.     /**
  133.      * @param string|null $type
  134.      * @param TransactionStatus|array<int, TransactionStatus>|null $statuses
  135.      * @return Collection<int, Transaction>
  136.      */
  137.     public function getTransactions(?string $type null, array|TransactionStatus $statuses null): Collection
  138.     {
  139.         $transactions $this->transactions;
  140.         if ($type) {
  141.             $transactions $transactions->filter(fn(Transaction $t) => ($t instanceof $type));
  142.         }
  143.         if ($statuses) {
  144.             if (!is_array($statuses)) {
  145.                 $statuses = [$statuses];
  146.             }
  147.             $transactions $transactions->filter(fn(Transaction $t) => (in_array($t->getStatus(), $statusestrue)));
  148.         }
  149.         return $transactions;
  150.     }
  151.     public function addTransaction(Transaction $transaction): self
  152.     {
  153.         if (!$this->transactions->contains($transaction)) {
  154.             $this->transactions[] = $transaction;
  155.             $transaction->setOrder($this);
  156.         }
  157.         return $this;
  158.     }
  159.     public function removeTransaction(Transaction $transaction): self
  160.     {
  161.         if ($this->transactions->removeElement($transaction)) {
  162.             // set the owning side to null (unless already changed)
  163.             if ($transaction->getOrder() === $this) {
  164.                 $transaction->setOrder(null);
  165.             }
  166.         }
  167.         return $this;
  168.     }
  169.     public function getExpectedTransactionType(): ?string
  170.     {
  171.         foreach ($this->getTransactions() as $transaction) {
  172.             if ($transaction->getStatus() === TransactionStatus::Waiting) {
  173.                 return get_class($transaction);
  174.             }
  175.         }
  176.         return null;
  177.     }
  178.     public function hasSuccessfulTransaction(?array $types = []): bool
  179.     {
  180.         $transactions $this->getTransactions()
  181.             ->filter(fn(Transaction $t) => $t->getStatus() === TransactionStatus::Successful);
  182.         if (count($types) > 0) {
  183.             $transactions $transactions->filter(fn(Transaction $t) => in_array(get_class($t), $typestrue));
  184.         }
  185.         return $transactions->count() > 0;
  186.     }
  187. }