<?php
/**
* Created by simpson <simpsonwork@gmail.com>
* Date: 2019-06-26
* Time: 18:00
*/
namespace App\Entity\Sales;
use App\Entity\Location\City;
use App\Repository\PaidPlacementPriceRepository;
use Doctrine\ORM\Mapping as ORM;
use Money\Currency;
use Money\Money;
#[ORM\Table(name: 'paid_service_prices')]
#[ORM\Entity(repositoryClass: PaidPlacementPriceRepository::class)]
#[ORM\InheritanceType('SINGLE_TABLE')]
#[ORM\DiscriminatorColumn(name: 'placement_type', type: 'string', length: 32)]
#[ORM\DiscriminatorMap([
'standard_saloon' => Saloon\StandardPlacementPrice::class, 'vip_saloon' => Saloon\VipPlacementPrice::class,
'ultra_vip_saloon' => Saloon\UltraVipPlacementPrice::class, 'standard_masseur' => Profile\MasseurPlacementPrice::class,
'vip_masseur' => Profile\MasseurVipPlacementPrice::class, 'ultra_vip_masseur' => Profile\MasseurUltraVipPlacementPrice::class,
'standard_profile' => Profile\StandardPlacementPrice::class, 'vip_profile' => Profile\VipPlacementPrice::class,
'ultra_vip_profile' => Profile\UltraVipPlacementPrice::class, 'top_profile' => Profile\TopPlacementPrice::class,
'placement_hiding' => PlacementHidingPrice::class
])]
abstract class PaidPlacementPrice
{
const SECONDS_IN_HOUR = 3600;
const SECONDS_IN_DAY = 86400;
#[ORM\Id]
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\GeneratedValue(strategy: 'AUTO')]
protected int $id;
#[ORM\JoinColumn(name: 'city_id', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: City::class)]
protected ?City $city;
#[ORM\Column(name: 'price_amount', type: 'integer')]
protected string $priceAmount;
#[ORM\Column(name: 'currency', type: 'string', length: 3)]
protected string $currency;
/**
* Кол-во оплаченного времени по ценнику
*
* 3600 - цена в час
* 86400 - цена в сутки
*/
#[ORM\Column(name: 'duration', type: 'integer')]
protected int $duration;
#[ORM\Column(name: 'enabled', type: 'boolean')]
protected bool $enabled = false;
public static function createHourlyPrice(City $city, Money $price): self
{
return new static($city, $price, self::SECONDS_IN_HOUR);
}
public static function createDailyPrice(City $city, Money $price): self
{
return new static($city, $price, self::SECONDS_IN_DAY);
}
protected function __construct(?City $city, Money $basePrice, int $duration)
{
$this->city = $city;
$this->priceAmount = $basePrice->getAmount();
$this->currency = $basePrice->getCurrency()->getCode();
$this->duration = $duration;
}
public function getId(): int
{
return $this->id;
}
public function getCity(): ?City
{
return $this->city;
}
protected function getBasePrice(): Money
{
return new Money($this->priceAmount, new Currency($this->currency));
}
public function getHourlyPrice(): Money
{
$basePrice = $this->getBasePrice();
return new Money(+$basePrice->getAmount() * self::SECONDS_IN_HOUR / $this->duration, $basePrice->getCurrency());
}
public function getDailyPrice(): Money
{
$basePrice = $this->getBasePrice();
return new Money(+$basePrice->getAmount() * self::SECONDS_IN_DAY / $this->duration, $basePrice->getCurrency());
}
public function calculatePriceByTimeRange(\DateTimeInterface $from, \DateTimeInterface $till): Money
{
$diffInSeconds = abs($till->getTimestamp() - $from->getTimestamp());
$basePrice = $this->getBasePrice();
return new Money(+$basePrice->getAmount() * $diffInSeconds / $this->duration, $basePrice->getCurrency());
}
public function isEnabled(): bool
{
return $this->enabled;
}
public function setEnabled(bool $enabled): void
{
$this->enabled = $enabled;
}
abstract static public function getType(): string;
}