src/Entity/Sales/PaidPlacementPrice.php line 28

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-06-26
  5.  * Time: 18:00
  6.  */
  7. namespace App\Entity\Sales;
  8. use App\Entity\Location\City;
  9. use App\Repository\PaidPlacementPriceRepository;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use Money\Currency;
  12. use Money\Money;
  13. #[ORM\Table(name'paid_service_prices')]
  14. #[ORM\Entity(repositoryClassPaidPlacementPriceRepository::class)]
  15. #[ORM\InheritanceType('SINGLE_TABLE')]
  16. #[ORM\DiscriminatorColumn(name'placement_type'type'string'length32)]
  17. #[ORM\DiscriminatorMap([
  18.     'standard_saloon' => Saloon\StandardPlacementPrice::class, 'vip_saloon' => Saloon\VipPlacementPrice::class,
  19.     'ultra_vip_saloon' => Saloon\UltraVipPlacementPrice::class, 'standard_masseur' => Profile\MasseurPlacementPrice::class,
  20.     'vip_masseur' => Profile\MasseurVipPlacementPrice::class, 'ultra_vip_masseur' => Profile\MasseurUltraVipPlacementPrice::class,
  21.     'standard_profile' => Profile\StandardPlacementPrice::class, 'vip_profile' => Profile\VipPlacementPrice::class,
  22.     'ultra_vip_profile' => Profile\UltraVipPlacementPrice::class, 'top_profile' => Profile\TopPlacementPrice::class,
  23.     'placement_hiding' => PlacementHidingPrice::class
  24. ])]
  25. abstract class PaidPlacementPrice
  26. {
  27.     const SECONDS_IN_HOUR 3600;
  28.     const SECONDS_IN_DAY 86400;
  29.     #[ORM\Id]
  30.     #[ORM\Column(name'id'type'integer')]
  31.     #[ORM\GeneratedValue(strategy'AUTO')]
  32.     protected int $id;
  33.     #[ORM\JoinColumn(name'city_id'referencedColumnName'id')]
  34.     #[ORM\ManyToOne(targetEntityCity::class)]
  35.     protected ?City $city;
  36.     #[ORM\Column(name'price_amount'type'integer')]
  37.     protected string $priceAmount;
  38.     #[ORM\Column(name'currency'type'string'length3)]
  39.     protected string $currency;
  40.     /**
  41.      * Кол-во оплаченного времени по ценнику
  42.      *
  43.      * 3600 - цена в час
  44.      * 86400 - цена в сутки
  45.      */
  46.     #[ORM\Column(name'duration'type'integer')]
  47.     protected int $duration;
  48.     #[ORM\Column(name'enabled'type'boolean')]
  49.     protected bool $enabled false;
  50.     public static function createHourlyPrice(City $cityMoney $price): self
  51.     {
  52.         return new static($city$priceself::SECONDS_IN_HOUR);
  53.     }
  54.     public static function createDailyPrice(City $cityMoney $price): self
  55.     {
  56.         return new static($city$priceself::SECONDS_IN_DAY);
  57.     }
  58.     protected function __construct(?City $cityMoney $basePriceint $duration)
  59.     {
  60.         $this->city $city;
  61.         $this->priceAmount $basePrice->getAmount();
  62.         $this->currency $basePrice->getCurrency()->getCode();
  63.         $this->duration $duration;
  64.     }
  65.     public function getId(): int
  66.     {
  67.         return $this->id;
  68.     }
  69.     public function getCity(): ?City
  70.     {
  71.         return $this->city;
  72.     }
  73.     protected function getBasePrice(): Money
  74.     {
  75.         return new Money($this->priceAmount, new Currency($this->currency));
  76.     }
  77.     public function getHourlyPrice(): Money
  78.     {
  79.         $basePrice $this->getBasePrice();
  80.         return new Money(+$basePrice->getAmount() * self::SECONDS_IN_HOUR $this->duration$basePrice->getCurrency());
  81.     }
  82.     public function getDailyPrice(): Money
  83.     {
  84.         $basePrice $this->getBasePrice();
  85.         return new Money(+$basePrice->getAmount() * self::SECONDS_IN_DAY $this->duration$basePrice->getCurrency());
  86.     }
  87.     public function calculatePriceByTimeRange(\DateTimeInterface $from\DateTimeInterface $till): Money
  88.     {
  89.         $diffInSeconds abs($till->getTimestamp() - $from->getTimestamp());
  90.         $basePrice $this->getBasePrice();
  91.         return new Money(+$basePrice->getAmount() * $diffInSeconds $this->duration$basePrice->getCurrency());
  92.     }
  93.     public function isEnabled(): bool
  94.     {
  95.         return $this->enabled;
  96.     }
  97.     public function setEnabled(bool $enabled): void
  98.     {
  99.         $this->enabled $enabled;
  100.     }
  101.     abstract static public function getType(): string;
  102. }