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(repositoryClass: PaymentProductRepository::class)]
  12. class PaymentProduct
  13. {
  14.     #[ORM\Id]
  15.     #[ORM\Column(name: 'id', type: 'integer')]
  16.     #[ORM\GeneratedValue(strategy: 'AUTO')]
  17.     protected int $id;
  18.     #[ORM\Column(name: 'product_id', type: 'integer')]
  19.     protected int $productId;
  20.     #[ORM\Column(name: 'payment_amount', type: 'integer')]
  21.     protected string $paymentAmount;
  22.     #[ORM\Column(name: 'currency', type: 'string', length: 3)]
  23.     protected string $currency;
  24.     /**
  25.      * Дополнительный бонус при покупке, в процентах от основной суммы
  26.      */
  27.     #[ORM\Column(name: 'multiplier_percent', type: 'smallint')]
  28.     protected int $multiplierPercent;
  29.     #[ORM\Column(name: 'is_enabled', type: 'boolean')]
  30.     protected bool $enabled;
  31.     public function __construct(int $productId, Money $money, int $multiplierPercent, bool $enabled)
  32.     {
  33.         $this->productId = $productId;
  34.         $this->paymentAmount = $money->getAmount();
  35.         $this->currency = $money->getCurrency()->getCode();
  36.         $this->multiplierPercent = $multiplierPercent;
  37.         $this->enabled = $enabled;
  38.     }
  39.     public function getId(): int
  40.     {
  41.         return $this->id;
  42.     }
  43.     public function getProductId(): int
  44.     {
  45.         return $this->productId;
  46.     }
  47.     public function getMoney(): Money
  48.     {
  49.         return new Money($this->paymentAmount, new Currency($this->currency));
  50.     }
  51.     public function hasBonus(): bool
  52.     {
  53.         return $this->multiplierPercent > 0;
  54.     }
  55.     public function getBonusMoney(): Money
  56.     {
  57.         $money = $this->getMoney();
  58.         return new Money($money->getAmount() * $this->multiplierPercent / 100, $money->getCurrency());
  59.     }
  60.     public function getTotalMoneyToEnroll(): Money
  61.     {
  62.         $money = $this->getMoney();
  63.         return $money->add($this->getBonusMoney());
  64.     }
  65.     public function getMultiplierPercent(): int
  66.     {
  67.         return $this->multiplierPercent;
  68.     }
  69.     public function isEnabled(): bool
  70.     {
  71.         return $this->enabled;
  72.     }
  73.     public function enable(): void
  74.     {
  75.         $this->enabled = true;
  76.     }
  77.     public function disable(): void
  78.     {
  79.         $this->enabled = false;
  80.     }
  81. }