<?php
namespace App\AtechNet\CoreBundle\Entity;
use App\AtechNet\CoreBundle\Translation\TranslatorInterface;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\Table as Table;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Album
*
* @Table(name="products__metas")
* @Entity
* @HasLifecycleCallbacks
*/
class ProductMeta extends AbstractEntity
{
/**
* @var Domain
*
* @Assert\NotNull
*
* @ManyToOne(targetEntity="Domain")
* @JoinColumn(name="domain_id", referencedColumnName="id")
*/
private $domain;
/**
* @var Product
*
* @Assert\NotNull
*
* @ManyToOne(targetEntity="Product", inversedBy="metas")
* @JoinColumn(name="product_id", referencedColumnName="id")
*/
private $product;
/**
* @var float
*
* @Column(name="price", type="float", nullable=true)
*/
private $price = 0;
/**
* @var float
*
* @Column(name="suggested_price", type="float", nullable=true)
*/
private $suggestedPrice = 0;
/**
* @var float
*
* @Column(name="current_vat_price", type="float", nullable=true)
*/
private $currentVatPrice = 0;
/**
* @var float
*
* @Column(name="discount", type="float", nullable=true)
*/
private $discount = 0;
/**
* @var bool
*
* @Assert\NotNull
*
* @Column(name="discounted", type="boolean")
*/
private $discounted;
/**
* @var bool
*
* @Assert\NotNull
*
* @Column(name="active", type="boolean")
*/
private $active;
/**
* @var bool
*
* @Assert\NotNull
*
* @Column(name="listed", type="boolean")
*/
private $listed;
/**
* @var bool
*
* @Assert\NotNull
*
* @Column(name="orderable", type="boolean")
*/
private $orderable;
/**
* @var float
*
* @Column(name="vat", type="decimal", precision=10, scale=2)
*/
private $vat;
/**
* @var int
*
* @Assert\NotNull
*
* @Column(name="warranty", type="integer")
*/
private $warranty;
/**
* @var bool
*
* @Assert\NotNull
*
* @Column(name="bazaar", type="boolean")
*/
private $bazaar;
/**
* @var bool
*
* @Assert\NotNull
*
* @Column(name="best_seller", type="boolean")
*/
private $bestSeller;
/**
* @var bool
*
* @Assert\NotNull
*
* @Column(name="re_lenovo", type="boolean")
*/
private $reLenovo;
/**
* @var bool
*
* @Assert\NotNull
*
* @Column(name="hp_renew", type="boolean")
*/
private $hpRenew;
/**
* @var bool
*
* @Assert\NotNull
*
* @Column(name="special_promotion", type="boolean")
*/
private $specialPromotion;
/**
* @var string
*
* @Column(name="detail", type="text", nullable=true)
*/
private $detail = null;
/**
* @var \DateTime
*
* @Column(name="sale_start", type="datetime", nullable=true)
*/
private $saleStart;
/**
* @var \DateTime
*
* @Column(name="sale_end", type="datetime", nullable=true)
*/
private $saleEnd;
/**
* @var \DateTime
*
* @Column(name="activation_date", type="datetime", nullable=true)
*/
private $activationDate;
/**
* @var \DateTime
*
* @Column(name="discounted_date", type="datetime", nullable=true)
*/
private $discountedDate;
/**
* @var \DateTime
*
* @Column(name="flash_sale_date", type="datetime", nullable=true)
*/
private $flashSaleDate;
public function __construct()
{
parent::__construct();
}
/**
* @return float
*/
public function getPrice()
{
if (in_array($this->domain->getId(), [Domain::DOMAIN_ID_RT_HU, Domain::DOMAIN_ID_WS_HU]) && $this->price % 10 != 0) {
return floor($this->price / 10) * 10;
} elseif (in_array($this->domain->getId(), [Domain::DOMAIN_ID_RT_CZ, Domain::DOMAIN_ID_WS_CZ])) {
return ceil($this->price);
}
return $this->price;
}
/**
* @param float $price
*/
public function setPrice($price)
{
$this->price = $price;
}
/**
* @return float
*/
public function getSuggestedPrice()
{
return $this->suggestedPrice;
}
/**
* @param float $suggestedPrice
*/
public function setSuggestedPrice($suggestedPrice)
{
$this->suggestedPrice = $suggestedPrice;
}
/**
* @return float
*/
public function getDomainSuggestedPrice()
{
if ($this->suggestedPriceCache == null) {
$this->suggestedPriceCache = round($this->getDomainVatSuggestedPrice() / (1 + $this->domain->getVat() / 100.), $this->domain->getPricePrecision(), PHP_ROUND_HALF_UP);
}
return $this->suggestedPriceCache;
}
private $suggestedPriceCache = null;
/**
* @return float
*/
public function getDomainVatSuggestedPrice()
{
if ($this->suggestedVatPriceCache == null) {
$this->suggestedVatPriceCache = $this->domain->createVatPrice($this->getSuggestedPrice());
}
return $this->suggestedVatPriceCache;
}
private $suggestedVatPriceCache = null;
/**
* @return float
*/
public function getVatPrice()
{
return $this->price * (1 + $this->vat / 100.);
}
/**
* @return float
*/
public function getVatSuggestedPrice()
{
return $this->suggestedPrice * (1 + $this->vat / 100.);
}
/**
* @param float $discount
*/
public function setDiscount($discount)
{
$this->discount = $discount;
}
/**
* @return float
*/
public function getDiscount()
{
if (in_array($this->domain->getId(), [Domain::DOMAIN_ID_RT_HU, Domain::DOMAIN_ID_WS_HU]) && $this->discount % 10 != 0) {
return floor($this->discount / 10) * 10;
}
return $this->discount;
}
/**
* @return float
*/
public function getVatDiscount()
{
return $this->discount * (1 + $this->vat / 100.);
}
/**
* @Assert\IsTrue
*/
public function isDiscountValid()
{
return $this->discount > 0 && $this->discount < $this->price || $this->discount == 0;
}
/**
* @return float
*/
public function getVat()
{
return $this->vat;
}
/**
* @param float $vat
*/
public function setVat($vat)
{
$this->vat = $vat;
}
/**
* @Assert\IsTrue
*/
public function isVatValid()
{
return $this->vat >= 0;
}
/**
* @return boolean
*/
public function isDiscounted()
{
return $this->discounted;
}
/**
* @param boolean $discounted
*/
public function setDiscounted($discounted)
{
$this->discounted = $discounted;
}
/**
* @return boolean
*/
public function isNormal()
{
return !$this->discounted;
}
/**
* @param boolean $normal
*/
public function setNormal($normal)
{
$this->discounted = !$normal;
}
/**
* @return boolean
*/
public function isActive()
{
return $this->active;
}
/**
* @param boolean $active
*/
public function setActive($active)
{
$this->active = $active;
}
/**
* @return boolean
*/
public function isListed()
{
return $this->listed;
}
/**
* @param boolean $listed
*/
public function setListed($listed)
{
$this->listed = $listed;
}
/**
* @return boolean
*/
public function isOrderable()
{
return $this->orderable;
}
/**
* @param boolean $orderable
*/
public function setOrderable($orderable)
{
$this->orderable = $orderable;
}
/**
* @return Product
*/
public function getProduct()
{
return $this->product;
}
/**
* @param Product $product
*/
public function setProduct($product)
{
$this->product = $product;
$product->addMeta($this);
}
/**
* @return Domain
*/
public function getDomain()
{
return $this->domain;
}
/**
* @param Domain $domain
*/
public function setDomain($domain)
{
$this->domain = $domain;
}
public function isReady()
{
return $this->price > 0 || $this->discount > 0;
}
/**
* @return float
*/
public function getCurrentPrice()
{
if ($this->isDiscounted()) {
return $this->getDiscount();
} else {
return $this->getPrice();
}
}
/**
* @return float
*/
public function getCurrentVatPrice()
{
return $this->currentVatPrice;
}
/**
* @param float $currentVatPrice
*/
public function setCurrentVatPrice($currentVatPrice)
{
$this->currentVatPrice = $currentVatPrice;
}
/**
* @return boolean
*/
public function isDomainDiscounted()
{
return $this->isDiscounted();
}
private $priceCache = null;
/**
* @return float
*/
public function getDomainPrice()
{
if ($this->priceCache == null) {
$this->priceCache = round($this->getDomainVatPrice() / (1 + $this->domain->getVat() / 100.), $this->domain->getPricePrecision(), PHP_ROUND_HALF_UP);
}
if (in_array($this->domain->getId(), [Domain::DOMAIN_ID_RT_HU, Domain::DOMAIN_ID_WS_HU]) && $this->priceCache % 10 != 0) {
return floor($this->priceCache / 10) * 10;
}
return $this->priceCache;
}
private $vatPriceCache = null;
/**
* @return float
*/
public function getDomainVatPrice()
{
if ($this->vatPriceCache == null) {
$this->vatPriceCache = $this->domain->createVatPrice($this->getPrice());
}
return $this->vatPriceCache;
}
private $discountPriceCache = null;
/**
* @return float
*/
public function getDomainDiscount()
{
if ($this->discountPriceCache == null) {
$this->discountPriceCache = round($this->getDomainVatDiscount() / (1 + $this->domain->getVat() / 100.), $this->domain->getPricePrecision(), PHP_ROUND_HALF_UP);
}
if (in_array($this->domain->getId(), [Domain::DOMAIN_ID_RT_HU, Domain::DOMAIN_ID_WS_HU]) && $this->discountPriceCache % 10 != 0) {
return floor($this->discountPriceCache / 10) * 10;
}
return $this->discountPriceCache;
}
private $discountVatPriceCache = null;
/**
* @return float
*/
public function getDomainVatDiscount()
{
if ($this->discountVatPriceCache == null) {
$this->discountVatPriceCache = $this->domain->createVatPrice($this->getDiscount());
}
return $this->discountVatPriceCache;
}
/**
* @return int
*/
public function getWarranty()
{
return $this->warranty;
}
/**
* @param int $warranty
*/
public function setWarranty($warranty)
{
$this->warranty = $warranty;
}
/**
* @return boolean
*/
public function isBazaar()
{
return $this->bazaar;
}
/**
* @param boolean $bazaar
*/
public function setBazaar($bazaar)
{
$this->bazaar = $bazaar;
}
/**
* @return boolean
*/
public function isBestSeller()
{
return $this->bestSeller;
}
/**
* @param boolean $bestSeller
*/
public function setBestSeller($bestSeller)
{
$this->bestSeller = $bestSeller;
}
/**
* @return boolean
*/
public function isReLenovo()
{
return $this->reLenovo;
}
/**
* @param boolean $reLenovo
*/
public function setReLenovo($reLenovo)
{
$this->reLenovo = $reLenovo;
}
/**
* @return boolean
*/
public function isHpRenew()
{
return $this->hpRenew;
}
/**
* @param boolean $hpRenew
*/
public function setHpRenew($hpRenew)
{
$this->hpRenew = $hpRenew;
}
/**
* @return boolean
*/
public function isSpecialPromotion()
{
return $this->specialPromotion;
}
/**
* @param boolean $specialPromotion
*/
public function setSpecialPromotion($specialPromotion)
{
$this->specialPromotion = $specialPromotion;
}
/**
* @return string
*/
public function getDetail()
{
return $this->detail;
}
public function getFormatDetail()
{
return Product::createFormatedDetail($this->detail);
}
/**
* @param string $detail
*/
public function setDetail($detail)
{
$this->detail = $detail;
}
private $fullDescription = [];
/**
* @param TranslatorInterface $translator
* @param bool $fromCache
* @param bool $lpcFpcCondition
* @param bool $displayQuality
* @return string
*/
public function getFullDescription(TranslatorInterface $translator = null, $fromCache = true, $lpcFpcCondition = false, $displayQuality = false)
{
$transName = ($translator == null ? 'default' : $translator->getName());
if (!isset($this->fullDescription[$transName]) || !$fromCache) {
$result = '<div class="product-spec">';
/**
* @var ProductAttribute $option
*/
$prevTitle = '';
foreach ($this->product->getSpecificationProductAttributeOptions() as $option) {
if (is_null($option)) {
continue;
}
/**
* Do not display quality in this list, its displayed already separately
* This will be removed after introduction of stock movements
*/
if ($option->getOption()->getAttribute()->getId() == 101 && !$displayQuality) {
continue;
}
/**
* Do not display COAs on LPC and FPC
*/
$optionId = $option->getOption()->getId();
if ($lpcFpcCondition && in_array($optionId, [479, 480, 481, 537, 538, 849, 684, 702, 785, 789, 790, 845, 849, 1207])) {
continue;
}
$title = htmlspecialchars($option->getOption()->getLongTitle($translator));
if ($title == '') {
$title = htmlspecialchars($option->getOption()->getTitle($translator));
}
$attrTitle = htmlspecialchars($option->getOption()->getAttribute()->getTitle($translator));
$multiplier = '';
if ($option->getMultiplier() != 0) {
$multiplier = $option->getMultiplier() . ' x ';
}
$style = ' style="color:rgba(255, 255, 255, 0) !important;"';
if ($prevTitle != $attrTitle) {
$prevTitle = $attrTitle;
$style = '';
}
$result .= '<dl class="attr-' . $option->getOption()->getAttribute()->getId() . '">';
$result .= '<dt' . $style . '>' . $attrTitle . ':</dt>';
$result .= '<dd>' . $multiplier . $title . '</dd>';
$result .= '</dl>';
if ($prevTitle == '') {
$prevTitle = $attrTitle;
}
}
$title = !is_null($translator) ? $translator->trans('Dimensions') : 'Dimensions';
if ($this->product->dimensions('mm', true) != null) {
$result .= '<dl><dt>' . $title . ':</dt>';
$result .= '<dd>' . $this->product->dimensions('mm', true) . '</dd>';
$result .= '</dl>';
}
$title = !is_null($translator) ? $translator->trans('Weight') : 'Weight';
if ($this->product->getWeight() != 0) {
$result .= '<dl><dt>' . $title . ':</dt>';
$result .= '<dd>' . $this->product->dimensionInUnit($this->product->getWeight(), 'kg', true) . '</dd>';
$result .= '</dl>';
}
$result .= '</div>';
$prodDetail = $this->product->getDetail();
if ($prodDetail !== null && $prodDetail != '') {
$result .= Product::createFormatedDetail($prodDetail);
}
$detail = $this->getDetail();
if ($detail !== null && $detail != '') {
$result .= Product::createFormatedDetail($detail);
}
if ($fromCache) {
$this->fullDescription[$transName] = $result;
} else {
return $result;
}
}
return $this->fullDescription[$transName];
}
/**
* Determine if we can add product into eshop cart
*
* @return bool
**/
public function canAddToCart()
{
if (!$this->isOrderable()) {
return false;
}
if (!$this->product->isAvailable()) {
switch ($this->product->getOutOfStockStatus()) {
case Product::OutOfStockStatus_Empty:
case Product::OutOfStockStatus_NotVisibleDefault:
case Product::OutOfStockStatus_VisibleComingSoon:
case Product::OutOfStockStatus_VisibleSoldOut:
return false;
break;
}
}
return true;
}
/**
* @return \DateTime
*/
public function getSaleStart()
{
return $this->saleStart;
}
/**
* @param \DateTime $saleStart
*/
public function setSaleStart($saleStart)
{
$this->saleStart = $saleStart;
}
/**
* @return \DateTime
*/
public function getSaleEnd()
{
return $this->saleEnd;
}
/**
* @param \DateTime $saleEnd
*/
public function setSaleEnd($saleEnd)
{
$this->saleEnd = $saleEnd;
}
/**
* @param bool $checkOnlyEnding
* @return bool
*/
public function isTimedSaleActive($checkOnlyEnding = false)
{
if (!is_null($this->getSaleStart()) || !is_null($this->getSaleEnd())) {
$currentTime = new \DateTime();
if (!is_null($this->getSaleStart()) && is_null($this->getSaleEnd()) && !$checkOnlyEnding) {
return $currentTime >= $this->getSaleStart();
}
if (is_null($this->getSaleStart()) && !is_null($this->getSaleEnd())) {
return $currentTime <= $this->getSaleEnd();
}
if (!is_null($this->getSaleStart()) && !is_null($this->getSaleEnd())) {
return $this->getSaleStart() <= $currentTime && $currentTime <= $this->getSaleEnd();
}
}
return false;
}
/**
* @return \DateTime|null
*/
public function getActivationDate(): ?\DateTime
{
return $this->activationDate;
}
/**
* @param \DateTime|null $activationDate
*/
public function setActivationDate(?\DateTime $activationDate)
{
$this->activationDate = $activationDate;
}
/**
* @return \DateTime|null
*/
public function getDiscountedDate(): ?\DateTime
{
return $this->discountedDate;
}
/**
* @param \DateTime|null $discountedDate
*/
public function setDiscountedDate(?\DateTime $discountedDate)
{
$this->discountedDate = $discountedDate;
}
/**
* @return \DateTime|null
*/
public function getFlashSaleDate(): ?\DateTime
{
return $this->flashSaleDate;
}
/**
* @param \DateTime|null $flashSaleDate
*/
public function setFlashSaleDate(?\DateTime $flashSaleDate)
{
$this->flashSaleDate = $flashSaleDate;
}
}