<?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 = null;
#[ORM\Column(name: 'product_id', type: 'integer')]
protected int $productId = 0;
#[ORM\Column(name: 'payment_amount', type: 'integer')]
protected string $paymentAmount = '0';
#[ORM\Column(name: 'currency', type: 'string', length: 3)]
protected string $currency = 'RUB';
/**
* Дополнительный бонус при покупке, в процентах от основной суммы
*/
#[ORM\Column(name: 'multiplier_percent', type: 'smallint')]
protected int $multiplierPercent = 0;
#[ORM\Column(name: 'is_enabled', type: 'boolean')]
protected bool $enabled = true;
public function __construct(int $productId = 0, ?Money $money = null, int $multiplierPercent = 0, bool $enabled = true)
{
$this->productId = $productId;
if (null !== $money) {
$this->setMoney($money);
}
$this->multiplierPercent = $multiplierPercent;
$this->enabled = $enabled;
}
public function getId(): ?int
{
return $this->id;
}
public function getProductId(): int
{
return $this->productId;
}
public function setProductId(int $productId): void
{
$this->productId = $productId;
}
public function getMoney(): Money
{
return new Money($this->paymentAmount, new Currency($this->currency));
}
public function setMoney(Money $money): void
{
$this->paymentAmount = $money->getAmount();
$this->currency = $money->getCurrency()->getCode();
}
public function getCurrency(): string
{
return $this->currency;
}
public function setCurrency(string $currency): void
{
$this->currency = $currency;
}
public function hasBonus(): bool
{
return $this->multiplierPercent > 0;
}
public function getBonusMoney(): Money
{
return $this->calculateBonusMoney($this->multiplierPercent);
}
public function getBonusMoneyForPercent(int $multiplierPercent): Money
{
return $this->calculateBonusMoney($multiplierPercent);
}
public function getTotalMoneyToEnroll(): Money
{
return $this->getMoney()->add($this->getBonusMoney());
}
public function getTotalMoneyToEnrollForPercent(int $multiplierPercent): Money
{
return $this->getMoney()->add($this->getBonusMoneyForPercent($multiplierPercent));
}
public function getMultiplierPercent(): int
{
return $this->multiplierPercent;
}
public function setMultiplierPercent(int $multiplierPercent): void
{
$this->multiplierPercent = $multiplierPercent;
}
public function isEnabled(): bool
{
return $this->enabled;
}
public function enable(): void
{
$this->enabled = true;
}
public function disable(): void
{
$this->enabled = false;
}
public function setEnabled(bool $enabled): void
{
$this->enabled = $enabled;
}
public function __toString(): string
{
$money = $this->getMoney();
return sprintf(
'%d (%s %s, %d%%)',
$this->productId,
number_format((int) $money->getAmount() / 100, 2, '.', ''),
$money->getCurrency()->getCode(),
$this->multiplierPercent
);
}
private function calculateBonusMoney(int $multiplierPercent): Money
{
$money = $this->getMoney();
return new Money($money->getAmount() * $multiplierPercent / 100, $money->getCurrency());
}
}