src/Entity/Transaction.php line 23

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\Form\Admin\Payment\TransactionCashType;
  7. use App\Form\Admin\Payment\TransactionCheckCouponType;
  8. use App\Form\Admin\Payment\TransactionCheckType;
  9. use App\Form\Admin\Payment\TransactionRefundType;
  10. use App\Form\Admin\Payment\TransactionWireType;
  11. use App\Repository\TransactionRepository;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Doctrine\ORM\Mapping as ORM;
  15. use InvalidArgumentException;
  16. #[ORM\Entity(repositoryClassTransactionRepository::class)]
  17. #[ORM\InheritanceType('SINGLE_TABLE')]
  18. #[ORM\DiscriminatorColumn('type''string')]
  19. #[ORM\DiscriminatorMap(self::TYPE_CLASSES)]
  20. class Transaction implements CancellableInterface
  21. {
  22.     use Identifiable;
  23.     use Timestampable;
  24.     public const TYPE_DEFAULT                'default';
  25.     public const TYPE_CARD                   'card';
  26.     public const TYPE_CARD_SINGLE            'card_single';
  27.     public const TYPE_CARD_MULTI             'card_multi';
  28.     public const TYPE_CHECK                  'check';
  29.     public const TYPE_CHECK_COUPON           'check_coupon';
  30.     public const TYPE_CASH                   'cash';
  31.     public const TYPE_WIRE                   'wire';
  32.     public const TYPE_DEPOSIT_CHECK          'deposit_check';
  33.     public const TYPE_RENTAL_DEPOSIT_CHECK   'rental_deposit_check';
  34.     public const TYPE_DISCOUNT_DEPOSIT_CHECK 'discount_deposit_check';
  35.     public const TYPE_DISCOUNT_RECEIPT       'discount_receipt';
  36.     public const TYPE_TERMINAL               'terminal';
  37.     public const TYPE_REFUND                 'refund';
  38.     public const TYPE_CLASSES = [
  39.         self::TYPE_DEFAULT                => Transaction::class,
  40.         self::TYPE_CARD                   => TransactionCard::class,
  41.         self::TYPE_CARD_SINGLE            => TransactionCardSingle::class,
  42.         self::TYPE_CARD_MULTI             => TransactionCardMulti::class,
  43.         self::TYPE_CHECK                  => TransactionCheck::class,
  44.         self::TYPE_CHECK_COUPON           => TransactionCheckCoupon::class,
  45.         self::TYPE_CASH                   => TransactionCash::class,
  46.         self::TYPE_WIRE                   => TransactionWire::class,
  47.         self::TYPE_DEPOSIT_CHECK          => TransactionDepositCheck::class,
  48.         self::TYPE_RENTAL_DEPOSIT_CHECK   => TransactionRentalDepositCheck::class,
  49.         self::TYPE_DISCOUNT_DEPOSIT_CHECK => TransactionDiscountDepositCheck::class,
  50.         self::TYPE_DISCOUNT_RECEIPT       => TransactionDiscountReceipt::class,
  51.         self::TYPE_TERMINAL               => TransactionTerminal::class,
  52.         self::TYPE_REFUND                 => TransactionRefund::class,
  53.     ];
  54.     public const TYPE_NAMES = [
  55.         self::TYPE_DEFAULT                => 'Paiement',
  56.         self::TYPE_CARD_SINGLE            => 'Par carte',
  57.         self::TYPE_CARD_MULTI             => 'Par carte en 3x',
  58.         self::TYPE_CHECK                  => 'Par chèques',
  59.         self::TYPE_CHECK_COUPON           => 'Par chèques vacances ou coupons sport ANCV',
  60.         self::TYPE_CASH                   => 'En espèces',
  61.         self::TYPE_WIRE                   => 'Par virement',
  62.         self::TYPE_DEPOSIT_CHECK          => 'Chèque de caution',
  63.         self::TYPE_RENTAL_DEPOSIT_CHECK   => 'Chèque de caution (location)',
  64.         self::TYPE_DISCOUNT_DEPOSIT_CHECK => 'Chèque de caution (aides à la pratique du sport)',
  65.         self::TYPE_DISCOUNT_RECEIPT       => 'Justificatif (aides à la pratique du sport)',
  66.         self::TYPE_TERMINAL               => 'TPE',
  67.         self::TYPE_REFUND                 => 'Remboursement',
  68.     ];
  69.     public const PUBLIC_TYPES = [
  70.         self::TYPE_CARD_SINGLE,
  71.         self::TYPE_CARD_MULTI,
  72.         self::TYPE_CHECK,
  73.         self::TYPE_CHECK_COUPON,
  74.         self::TYPE_CASH,
  75.         self::TYPE_WIRE,
  76.     ];
  77.     public const MANUAL_TYPES = [
  78.         self::TYPE_CASH,
  79.         self::TYPE_TERMINAL,
  80.         self::TYPE_WIRE,
  81.         self::TYPE_CHECK,
  82.         self::TYPE_CHECK_COUPON,
  83.         self::TYPE_DEPOSIT_CHECK,
  84.         self::TYPE_RENTAL_DEPOSIT_CHECK,
  85.         self::TYPE_DISCOUNT_DEPOSIT_CHECK,
  86.         self::TYPE_REFUND,
  87.     ];
  88.     public const FORM_CLASSES = [
  89.         self::TYPE_CHECK                  => TransactionCheckType::class,
  90.         self::TYPE_CHECK_COUPON           => TransactionCheckCouponType::class,
  91.         self::TYPE_CASH                   => TransactionCashType::class,
  92.         self::TYPE_TERMINAL               => TransactionCashType::class,
  93.         self::TYPE_WIRE                   => TransactionWireType::class,
  94.         self::TYPE_DEPOSIT_CHECK          => TransactionCheckType::class,
  95.         self::TYPE_RENTAL_DEPOSIT_CHECK   => TransactionCheckType::class,
  96.         self::TYPE_DISCOUNT_DEPOSIT_CHECK => TransactionCheckType::class,
  97.         self::TYPE_REFUND                 => TransactionRefundType::class,
  98.     ];
  99.     #[ORM\Column(type'float')]
  100.     private $amount;
  101.     #[ORM\Column(type'float')]
  102.     private $amountPayed 0;
  103.     #[ORM\Column(type'string'length20enumTypeTransactionStatus::class)]
  104.     private $status TransactionStatus::Created;
  105.     #[ORM\Column(type'string'length255nullabletrue)]
  106.     private $comment;
  107.     #[ORM\ManyToOne(targetEntityOrder::class, inversedBy'transactions')]
  108.     #[ORM\JoinColumn(nullablefalse)]
  109.     private $order;
  110.     #[ORM\ManyToOne(targetEntityself::class, inversedBy'children')]
  111.     private $parent;
  112.     #[ORM\OneToMany(mappedBy'parent'targetEntityself::class)]
  113.     private $children;
  114.     #[ORM\Column(type'string'length255nullabletrue)]
  115.     private $cancelReason;
  116.     public function __construct()
  117.     {
  118.         $this->children = new ArrayCollection();
  119.     }
  120.     public function getAmount(): ?float
  121.     {
  122.         return $this->amount;
  123.     }
  124.     public function setAmount(float $amount): self
  125.     {
  126.         $this->amount $amount;
  127.         return $this;
  128.     }
  129.     public function getAmountPayed(bool $checkMultiCardWaiting false): ?float
  130.     {
  131.         if ($checkMultiCardWaiting &&
  132.             $this instanceof TransactionCardMulti &&
  133.             $this->getStatus() === TransactionStatus::Waiting &&
  134.             $this->getCycle() > 1) {
  135.             return $this->amount;
  136.         }
  137.         return $this->amountPayed;
  138.     }
  139.     public function setAmountPayed(float $amountPayed): self
  140.     {
  141.         $this->amountPayed $amountPayed;
  142.         return $this;
  143.     }
  144.     public function getStatus(): ?TransactionStatus
  145.     {
  146.         return $this->status;
  147.     }
  148.     public function setStatus(TransactionStatus $status): self
  149.     {
  150.         $this->status $status;
  151.         return $this;
  152.     }
  153.     public function getComment(): ?string
  154.     {
  155.         return $this->comment;
  156.     }
  157.     public function setComment(?string $comment): self
  158.     {
  159.         $this->comment $comment;
  160.         return $this;
  161.     }
  162.     public function getOrder(): ?Order
  163.     {
  164.         return $this->order;
  165.     }
  166.     public function setOrder(?Order $order): self
  167.     {
  168.         $this->order $order;
  169.         return $this;
  170.     }
  171.     public static function createFromType(string $type): self
  172.     {
  173.         if (!array_key_exists($typeself::TYPE_CLASSES)) {
  174.             throw new InvalidArgumentException(
  175.                 sprintf('type must be one of "%s"'implode(', 'array_keys(self::TYPE_CLASSES)))
  176.             );
  177.         }
  178.         return new (self::TYPE_CLASSES[$type]);
  179.     }
  180.     public function getType(?bool $slug false): string
  181.     {
  182.         if ($slug) {
  183.             return array_search(get_class($this), self::TYPE_CLASSES);
  184.         }
  185.         return self::TYPE_NAMES[array_search(get_class($this), self::TYPE_CLASSES)];
  186.     }
  187.     public function getParent(): ?self
  188.     {
  189.         return $this->parent;
  190.     }
  191.     public function setParent(?self $parent): self
  192.     {
  193.         $this->parent $parent;
  194.         return $this;
  195.     }
  196.     /**
  197.      * @return Collection<int, self>
  198.      */
  199.     public function getChildren(): Collection
  200.     {
  201.         return $this->children;
  202.     }
  203.     public function addChild(self $child): self
  204.     {
  205.         if (!$this->children->contains($child)) {
  206.             $this->children[] = $child;
  207.             $child->setParent($this);
  208.         }
  209.         return $this;
  210.     }
  211.     public function removeChild(self $child): self
  212.     {
  213.         if ($this->children->removeElement($child)) {
  214.             // set the owning side to null (unless already changed)
  215.             if ($child->getParent() === $this) {
  216.                 $child->setParent(null);
  217.             }
  218.         }
  219.         return $this;
  220.     }
  221.     public function getName(): string
  222.     {
  223.         return self::getNameByClass(static::class) ?? '';
  224.     }
  225.     public static function getNameByClass($class): ?string
  226.     {
  227.         $key array_search($classself::TYPE_CLASSEStrue);
  228.         if ($key && (self::TYPE_NAMES[$key] ?? null)) {
  229.             return self::TYPE_NAMES[$key];
  230.         }
  231.         return null;
  232.     }
  233.     public function getCancelReason(): ?string
  234.     {
  235.         return $this->cancelReason;
  236.     }
  237.     public function setCancelReason(?string $cancelReason): self
  238.     {
  239.         $this->cancelReason $cancelReason;
  240.         return $this;
  241.     }
  242.     public static function getPublicClasses(): array
  243.     {
  244.         $publicTypes self::PUBLIC_TYPES;
  245.         return array_filter(self::TYPE_CLASSES, static fn($key) => in_array($key$publicTypes), ARRAY_FILTER_USE_KEY);
  246.     }
  247. }