<?php
namespace Uniski\ResourceBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="Uniski\ResourceBundle\Repository\HotelRateRepository")
* @ORM\HasLifecycleCallbacks()
**/
class HotelRate extends PriceRate
{
const PRICE_KEY_GROUP = 'group';
const PRICE_KEY_PAX = 'pax';
const PRICE_TYPE_PAX = "pax";
const PRICE_TYPE_GROUP = "group";
const PRICE_TYPE_ROOM = "room";
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime")
*/
protected $date;
/**
* @var float
*
* @ORM\Column(name="percentage", type="decimal", precision=3, scale=2)
*/
protected $percentage;
/**
* @var float
*
* @ORM\Column(name="min_price", type="decimal", precision=8, scale=2)
*/
protected $minPrice;
/**
* @var integer
* @ORM\Column(type="smallint")
*/
protected $days;
function __construct()
{
$this->days = 1;
}
/**
* Gets the value of date.
*
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Sets the value of date.
*
* @param \DateTime $date the date
*
* @return self
*/
public function setDate(\DateTime $date)
{
$this->date = $date;
return $this;
}
/**
* Gets the value of percentage.
*
* @return integer
*/
public function getPercentage()
{
return $this->percentage;
}
/**
* Sets the value of percentage.
*
* @param integer $percentage the percentage
*
* @return self
*/
public function setPercentage($percentage)
{
$this->percentage = $percentage;
return $this;
}
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function calculateMinPrice()
{
$this->minPrice = null;
foreach ($this->prices as $roomPrice) {
foreach ($roomPrice as $boardTypePrice) {
if (is_numeric($boardTypePrice) &&
( $this->minPrice == null ||
($boardTypePrice / $this->days) < $this->minPrice) ) {
$this->minPrice = $boardTypePrice /$this->days;
}
}
}
}
/**
* Set minPrice
*
* @param string $minPrice
*
* @return self
*/
public function setMinPrice($minPrice)
{
$this->minPrice = $minPrice;
return $this;
}
/**
* Get minPrice
*
* @return string
*/
public function getMinPrice()
{
return $this->minPrice;
}
public static function getModel($options = null)
{
if ($options === null) throw new Exception("Options cannot be null");
$hotel = $options['hotel'];
$boardPrices = [
Hotel::BOARD_ACCOMODATION_ONLY => '',
Hotel::BOARD_BREAKFAST => '',
Hotel::BOARD_HALF => '',
Hotel::BOARD_FULL => '',
];
//We set a place for per person prices
$model = [];
$model[self::PRICE_KEY_PAX] = $boardPrices;
foreach ($hotel->getRooms() as $key => $room) {
$roomType = isset($room["type"]) ? $room["type"] : $key;
$model[$roomType] = $boardPrices;
}
$model[self::PRICE_KEY_GROUP] = $boardPrices;
return $model;
}
/**
* Gets the value of days.
*
* @return integer
*/
public function getDays()
{
return $this->days;
}
/**
* Sets the value of days.
*
* @param integer $days the days
*
* @return self
*/
public function setDays($days)
{
$this->days = $days;
return $this;
}
public function type()
{
if (!$this->prices) return null;
$paxPrices = isset($this->prices[self::PRICE_KEY_PAX]) ? $this->prices[self::PRICE_KEY_PAX]: [];
$groupPrices = isset($this->prices[self::PRICE_KEY_GROUP]) ? $this->prices[self::PRICE_KEY_GROUP]: [];
foreach ($paxPrices as $price) {
if (intval($price) > 0) return self::PRICE_TYPE_PAX;
}
foreach ($groupPrices as $price) {
if (intval($price) > 0) return self::PRICE_TYPE_GROUP;
}
return self::PRICE_TYPE_ROOM;
}
}