<?php
namespace App\Entity\Sales;
use App\Repository\PaymentProductRepository;
use Doctrine\ORM\Mapping as ORM;
use Money\Currency;
use Money\Money;
/**
* Опция покупки в личном кабинете
*/
#[ORM\Table(name: 'payment_products')]
#[ORM\Entity(repositoryClass: PaymentProductRepository::class)]
class PaymentProduct
{
#[ORM\Id]
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\GeneratedValue(strategy: 'AUTO')]
protected int $id;
#[ORM\Column(name: 'product_id', type: 'integer')]
protected int $productId;
#[ORM\Column(name: 'payment_amount', type: 'integer')]
protected string $paymentAmount;
#[ORM\Column(name: 'currency', type: 'string', length: 3)]
protected string $currency;
/**
* Дополнительный бонус при покупке, в процентах от основной суммы
*/
#[ORM\Column(name: 'multiplier_percent', type: 'smallint')]
protected int $multiplierPercent;
#[ORM\Column(name: 'is_enabled', type: 'boolean')]
protected bool $enabled;
public function __construct(int $productId, Money $money, int $multiplierPercent, bool $enabled)
{
$this->productId = $productId;
$this->paymentAmount = $money->getAmount();
$this->currency = $money->getCurrency()->getCode();
$this->multiplierPercent = $multiplierPercent;
$this->enabled = $enabled;
}
public function getId(): int
{
return $this->id;
}
public function getProductId(): int
{
return $this->productId;
}
public function getMoney(): Money
{
return new Money($this->paymentAmount, new Currency($this->currency));
}
public function hasBonus(): bool
{
return $this->multiplierPercent > 0;
}
public function getBonusMoney(): Money
{
$money = $this->getMoney();
return new Money($money->getAmount() * $this->multiplierPercent / 100, $money->getCurrency());
}
public function getTotalMoneyToEnroll(): Money
{
$money = $this->getMoney();
return $money->add($this->getBonusMoney());
}
public function getMultiplierPercent(): int
{
return $this->multiplierPercent;
}
public function isEnabled(): bool
{
return $this->enabled;
}
public function enable(): void
{
$this->enabled = true;
}
public function disable(): void
{
$this->enabled = false;
}
}