src/Entity/Sales/PaymentProduct.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Sales;
  3. use App\Repository\PaymentProductRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Money\Currency;
  6. use Money\Money;
  7. /**
  8.  * Опция покупки в личном кабинете
  9.  */
  10. #[ORM\Table(name'payment_products')]
  11. #[ORM\Entity(repositoryClassPaymentProductRepository::class)]
  12. class PaymentProduct
  13. {
  14.     #[ORM\Id]
  15.     #[ORM\Column(name'id'type'integer')]
  16.     #[ORM\GeneratedValue(strategy'AUTO')]
  17.     protected ?int $id null;
  18.     #[ORM\Column(name'product_id'type'integer')]
  19.     protected int $productId 0;
  20.     #[ORM\Column(name'payment_amount'type'integer')]
  21.     protected string $paymentAmount '0';
  22.     #[ORM\Column(name'currency'type'string'length3)]
  23.     protected string $currency 'RUB';
  24.     /**
  25.      * Дополнительный бонус при покупке, в процентах от основной суммы
  26.      */
  27.     #[ORM\Column(name'multiplier_percent'type'smallint')]
  28.     protected int $multiplierPercent 0;
  29.     #[ORM\Column(name'is_enabled'type'boolean')]
  30.     protected bool $enabled true;
  31.     public function __construct(int $productId 0, ?Money $money nullint $multiplierPercent 0bool $enabled true)
  32.     {
  33.         $this->productId $productId;
  34.         if (null !== $money) {
  35.             $this->setMoney($money);
  36.         }
  37.         $this->multiplierPercent $multiplierPercent;
  38.         $this->enabled $enabled;
  39.     }
  40.     public function getId(): ?int
  41.     {
  42.         return $this->id;
  43.     }
  44.     public function getProductId(): int
  45.     {
  46.         return $this->productId;
  47.     }
  48.     public function setProductId(int $productId): void
  49.     {
  50.         $this->productId $productId;
  51.     }
  52.     public function getMoney(): Money
  53.     {
  54.         return new Money($this->paymentAmount, new Currency($this->currency));
  55.     }
  56.     public function setMoney(Money $money): void
  57.     {
  58.         $this->paymentAmount $money->getAmount();
  59.         $this->currency $money->getCurrency()->getCode();
  60.     }
  61.     public function getCurrency(): string
  62.     {
  63.         return $this->currency;
  64.     }
  65.     public function setCurrency(string $currency): void
  66.     {
  67.         $this->currency $currency;
  68.     }
  69.     public function hasBonus(): bool
  70.     {
  71.         return $this->multiplierPercent 0;
  72.     }
  73.     public function getBonusMoney(): Money
  74.     {
  75.         return $this->calculateBonusMoney($this->multiplierPercent);
  76.     }
  77.     public function getBonusMoneyForPercent(int $multiplierPercent): Money
  78.     {
  79.         return $this->calculateBonusMoney($multiplierPercent);
  80.     }
  81.     public function getTotalMoneyToEnroll(): Money
  82.     {
  83.         return $this->getMoney()->add($this->getBonusMoney());
  84.     }
  85.     public function getTotalMoneyToEnrollForPercent(int $multiplierPercent): Money
  86.     {
  87.         return $this->getMoney()->add($this->getBonusMoneyForPercent($multiplierPercent));
  88.     }
  89.     public function getMultiplierPercent(): int
  90.     {
  91.         return $this->multiplierPercent;
  92.     }
  93.     public function setMultiplierPercent(int $multiplierPercent): void
  94.     {
  95.         $this->multiplierPercent $multiplierPercent;
  96.     }
  97.     public function isEnabled(): bool
  98.     {
  99.         return $this->enabled;
  100.     }
  101.     public function enable(): void
  102.     {
  103.         $this->enabled true;
  104.     }
  105.     public function disable(): void
  106.     {
  107.         $this->enabled false;
  108.     }
  109.     public function setEnabled(bool $enabled): void
  110.     {
  111.         $this->enabled $enabled;
  112.     }
  113.     public function __toString(): string
  114.     {
  115.         $money $this->getMoney();
  116.         return sprintf(
  117.             '%d (%s %s, %d%%)',
  118.             $this->productId,
  119.             number_format((int) $money->getAmount() / 1002'.'''),
  120.             $money->getCurrency()->getCode(),
  121.             $this->multiplierPercent
  122.         );
  123.     }
  124.     private function calculateBonusMoney(int $multiplierPercent): Money
  125.     {
  126.         $money $this->getMoney();
  127.         return new Money($money->getAmount() * $multiplierPercent 100$money->getCurrency());
  128.     }
  129. }