src/Entity/MarkupStrategyExchangeRate.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\MarkupStrategyExchangeRateRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Gedmo\Timestampable\Traits\TimestampableEntity;
  6. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. /**
  9.  * Sale rate of a currency pair for the customers priced by a markup strategy : replaces the default sale rate
  10.  * (CurrencyExchangerate) of the pair in their flight pricing. Only the sale rate : the buy rate is the agency's cost.
  11.  */
  12. #[ORM\Entity(repositoryClass: MarkupStrategyExchangeRateRepository::class)]
  13. #[ORM\UniqueConstraint(columns: ['markupStrategy_id', 'currencyFrom', 'currencyTo'])]
  14. #[UniqueEntity(fields: ['markupStrategy', 'currencyFrom', 'currencyTo'])]
  15. class MarkupStrategyExchangeRate
  16. {
  17.     use TimestampableEntity;
  18.     #[ORM\Id]
  19.     #[ORM\GeneratedValue]
  20.     #[ORM\Column]
  21.     private ?int $id = null;
  22.     #[ORM\ManyToOne(inversedBy: 'exchangeRates')]
  23.     #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
  24.     private ?MarkupStrategy $markupStrategy = null;
  25.     #[ORM\Column(length: 64)]
  26.     private ?string $currencyFrom = null;
  27.     #[ORM\Column(length: 64)]
  28.     private ?string $currencyTo = null;
  29.     // nullable in PHP only : an emptied form field must reach the form's NotBlank instead of a TypeError
  30.     #[ORM\Column(type: 'decimal', precision: 10, scale: CurrencyExchangerate::RATE_SCALE)]
  31.     #[Assert\Positive]
  32.     private ?float $exchangeRateSale = null;
  33.     public function getId(): ?int
  34.     {
  35.         return $this->id;
  36.     }
  37.     public function getMarkupStrategy(): ?MarkupStrategy
  38.     {
  39.         return $this->markupStrategy;
  40.     }
  41.     public function setMarkupStrategy(?MarkupStrategy $markupStrategy): self
  42.     {
  43.         $this->markupStrategy = $markupStrategy;
  44.         return $this;
  45.     }
  46.     public function getCurrencyFrom(): ?string
  47.     {
  48.         return $this->currencyFrom;
  49.     }
  50.     public function setCurrencyFrom(?string $currencyFrom): self
  51.     {
  52.         $this->currencyFrom = $currencyFrom;
  53.         return $this;
  54.     }
  55.     public function getCurrencyTo(): ?string
  56.     {
  57.         return $this->currencyTo;
  58.     }
  59.     public function setCurrencyTo(?string $currencyTo): self
  60.     {
  61.         $this->currencyTo = $currencyTo;
  62.         return $this;
  63.     }
  64.     public function getExchangeRateSale(): ?float
  65.     {
  66.         return $this->exchangeRateSale;
  67.     }
  68.     public function setExchangeRateSale(?float $exchangeRateSale): self
  69.     {
  70.         $this->exchangeRateSale = $exchangeRateSale;
  71.         return $this;
  72.     }
  73.     /**
  74.      * "Auto reverse" : sets this pair (TO_FROM) to the inverse of $rate (FROM_TO), keeping a current value that already
  75.      * gives $rate back at the stored precision (see CurrencyExchangerate::setReverseRatesOf)
  76.      */
  77.     public function setReverseRateOf(MarkupStrategyExchangeRate $rate): void
  78.     {
  79.         $this->exchangeRateSale = CurrencyExchangerate::reciprocal($rate->getExchangeRateSale(), $this->exchangeRateSale);
  80.     }
  81.     #[Assert\IsTrue(message: 'The specified currencies should be different')]
  82.     public function hasDifferentCurrencies(): bool
  83.     {
  84.         return $this->currencyTo != $this->currencyFrom;
  85.     }
  86.     public function __toString(): string
  87.     {
  88.         return $this->currencyFrom.' => '.$this->currencyTo;
  89.     }
  90. }