<?php
namespace Uniski\ResourceBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Uniski\CommerceBundle\Exception\PriceNotFoundException;
/**
* @ORM\Entity
* @ORM\Table(name="price_rate")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="rate_type", type="string")
* @ORM\DiscriminatorMap({
* "price_rate" = "PriceRate",
* "hotel_rate" = "HotelRate",
* "ski_rate" = "SkiRate",
* "activity_rate" = "ActivityRate",
* "renting_rate" = "RentingRate",
* "insurance_rate" = "InsuranceRate",
* "pack_rate" = "PackRate"
* })
*/
class PriceRate
{
const PRICE_MAIN_KEY = 'precios';
const PRICE_PERSON_KEY = 'persona';
const PRICE_ADULTS_KEY = 'Adultos';
const PRICE_JUNIOR_KEY = 'Junior';
const PRICE_KIDS_KEY = 'NiƱos';
/**
* @var integer
*
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @var array
*
* @ORM\Column(name="prices", type="json")
*/
protected $prices;
/**
* @var string
* @ORM\Column(name="name", type="string")
*/
protected $name;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set prices
*
* @param array $prices
*
* @return PriceRate
*/
public function setPrices($prices)
{
$this->prices = $prices;
return $this;
}
/**
* Get prices
*
* @return array
*/
public function getPrices()
{
return $this->prices;
}
/**
* Gets the value of name.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Sets the value of name.
*
* @param string $name the name
*
* @return self
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
public static function getModel($options = null)
{
return [self::PRICE_MAIN_KEY => [self::PRICE_PERSON_KEY => '']];
}
public function getPersonPrice($gross = true)
{
//Verify base model
if (!isset($this->prices[PriceRate::PRICE_MAIN_KEY]) ||
!isset($this->prices[PriceRate::PRICE_MAIN_KEY][PriceRate::PRICE_PERSON_KEY]) ||
!is_numeric($this->prices[PriceRate::PRICE_MAIN_KEY][PriceRate::PRICE_PERSON_KEY]))
throw new PriceNotFoundException("Invalid model");
$price = floatval($this->prices[self::PRICE_MAIN_KEY][self::PRICE_PERSON_KEY]);
if ($gross) {
$price /= (1 - $this->percentage);
}
return round($price, 3);
}
public function getKeyPrice($key, $gross = true)
{
if ($this->days == null || $this->days < 1) $days = 1;
else $days = $this->days;
if (!is_numeric($this->prices[self::PRICE_MAIN_KEY][$key])) {
throw new PriceNotFoundException("There is no price for key $key");
}
$price = $this->prices[self::PRICE_MAIN_KEY][$key] / $days;
if ($gross) {
$price /= (1 - $this->percentage);
}
return round($price, 3);
}
}