<?php
namespace App\AtechNet\CoreBundle\Entity;
use App\AtechNet\CoreBundle\Helper\VAT;
use Doctrine\Common\Collections\ArrayCollection;
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\OneToMany;
use Doctrine\ORM\Mapping\OrderBy;
use Doctrine\ORM\Mapping\Table as Table;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Domain
*
* @Table(name="domains")
* @Entity(repositoryClass="App\AtechNet\CoreBundle\Repository\DomainRepository")
* @HasLifecycleCallbacks
*/
class Domain extends AbstractSortableEntity
{
public const string TYPE_RETAIL = 'retail';
public const string TYPE_WHOLESALE = 'wholesale';
public const string TYPE_OTHER = 'other';
public const string TYPE_EXPORT = 'export';
public const string WARRANTY_12 = '12';
public const string WARRANTY_24 = '24';
public const string WARRANTY_36 = '36';
public const int HU_VAT_CHANGE_DATE = 201711051720; // Date format: YmdHi
public const int HU_VAT_CHANGE_BACK_DATE = 202501010000; // Date format: YmdHi
public const int CZ_VAT_CHANGE_DATE = 202107010000;
public const int SK_VAT_CHANGE_DATE = 202501010000;
public const DOMAIN_IDS_FOR_ACTIVE_CAMPAIGN = [1, 2, 3, 4, 5, 9, 8, 7];
public const DOMAINS_WITH_WEBSITE = [
1, 2, 3, 4, 5, 7, 9, 11
];
public const DOMAINS_FOR_QUALITY_RESULT_FILTER = [
1, 2, 3, 4, 5, 7, 9, 10, 11
];
public const WS_DOMAINS_WITH_WEBSITE = [
2,
4,
5,
7,
];
public const DOMAINS_WITH_WEBSITE_WITHOUT_WS = [
1,
3,
9,
11
];
public const int DOMAIN_ID_RT_SK = 1;
public const int DOMAIN_ID_WS_SK = 2;
public const int DOMAIN_ID_RT_HU = 3;
public const int DOMAIN_ID_WS_HU = 4;
public const int DOMAIN_ID_WS_CZ = 5;
public const int DOMAIN_ID_RT_CZ = 9;
public const int DOMAIN_ID_PC_SHOP = 6;
public const int DOMAIN_ID_WS_EU = 7;
public const int DOMAIN_ID_OTHER = 8;
public const int DOMAIN_ID_B2C_EU = 10;
public const int DOMAIN_ID_RT_EU = 11;
public const int DOMAIN_ID_B2C_CH = 12;
public const int DOMAIN_ID_B2C_GB = 13;
public const string CURRENCY_EUR = '€';
public const string CURRENCY_HUF = 'Ft';
public const string CURRENCY_CZK = 'Kč';
public const string CURRENCY_CHF = 'CHF';
public const string CURRENCY_GBP = '£';
public static function getWarrantyChoices(): array
{
return [
self::WARRANTY_24 => self::WARRANTY_24,
self::WARRANTY_12 => self::WARRANTY_12,
self::WARRANTY_36 => self::WARRANTY_36,
];
}
/**
* @return int[]
*/
public static function getRetailWholesaleDomainPairs(): array
{
return [
1 => 2,
2 => 1,
3 => 4,
4 => 3,
5 => 9,
9 => 5,
7 => 11,
11 => 7,
];
}
/**
* @return string[]
*/
public static function getCurrencyChoices(): array
{
return [
self::CURRENCY_EUR,
self::CURRENCY_HUF,
self::CURRENCY_CZK,
self::CURRENCY_CHF,
self::CURRENCY_GBP,
];
}
public static function getDomainCodeByDomainId()
{
return [
self::DOMAIN_ID_RT_SK => 'sk',
self::DOMAIN_ID_WS_SK => 'sk',
self::DOMAIN_ID_RT_HU => 'hu',
self::DOMAIN_ID_WS_HU => 'hu',
self::DOMAIN_ID_RT_CZ => 'cz',
self::DOMAIN_ID_WS_CZ => 'cz',
self::DOMAIN_ID_RT_EU => 'eu',
self::DOMAIN_ID_WS_EU => 'eu',
];
}
/**
* @param int $rtDomain
* @return int
*/
public static function getWSdomainForRt(int $rtDomain): int
{
return match ($rtDomain) {
self::DOMAIN_ID_RT_SK => self::DOMAIN_ID_WS_SK,
self::DOMAIN_ID_RT_HU => self::DOMAIN_ID_WS_HU,
self::DOMAIN_ID_RT_CZ => self::DOMAIN_ID_WS_CZ,
self::DOMAIN_ID_RT_EU => self::DOMAIN_ID_WS_EU,
};
}
/**
* @var Language
*
* @Assert\NotNull
*
* @ManyToOne(targetEntity="Language")
* @JoinColumn(name="language_id", referencedColumnName="id")
*/
private $language;
/**
* @var Company
*
* @Assert\NotNull
*
* @ManyToOne(targetEntity="Company")
* @JoinColumn(name="company_id", referencedColumnName="id")
*/
private $company;
/**
* @var string
*
* @Assert\NotNull
*
* @Column(name="type", type="text")
*/
private $type;
/**
* @var string
*
* @Assert\NotNull
*
* @Column(name="name", type="text")
*/
private $name;
/**
* @var string
*
* @Assert\NotNull
*
* @Column(name="currency", type="text")
*/
private $currency;
/**
* @var float
*
* @Assert\NotNull
*
* @Column(name="price_factor", type="float")
*/
private $priceFactor = 1;
/**
* @var int
*
* @Assert\NotNull
*
* @Column(name="price_precision", type="decimal")
*/
private $pricePrecision = 2;
/**
* @var float
*
* @Assert\NotNull
*
* @Column(name="price_end", type="float")
*/
private $priceEnd;
/**
* @var float
*
* @Assert\NotNull
*
* @Column(name="price_end2", type="float")
*/
private $priceEnd2;
/**
* @var float
*
* @Assert\NotNull
*
* @Column(name="price_end2_under", type="float")
*/
private $priceEnd2Under;
/**
* @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 string
*
* @Assert\NotNull
*
* @Column(name="url", type="text")
*/
private $url;
/**
* @var User
*
* @ManyToOne(targetEntity="User")
* @JoinColumn(name="default_sales_manager_id", referencedColumnName="id", nullable=true)
*/
private $defaultSalesManager;
/**
* @var ArrayCollection
*
* @Assert\Valid
*
* @OneToMany(targetEntity="Customer", mappedBy="domain")
**/
private $customers;
/**
* @var ArrayCollection
*
* @Assert\Valid
*
* @OneToMany(targetEntity="DomainShippingType", mappedBy="domain")
* @OrderBy({"type" = "ASC"})
**/
private $domainShippingTypes;
/**
* @var ArrayCollection
*
* @Assert\Valid
*
* @OneToMany(targetEntity="DomainPaymentType", mappedBy="domain")
* @OrderBy({"type" = "ASC"})
**/
private $domainPaymentTypes;
/**
* @var ArrayCollection
*
* @Assert\Valid
*
* @OneToMany(targetEntity="Order", mappedBy="domain")
* @OrderBy({"created" = "DESC"})
**/
private $orders;
/**
* @var string
*
* @Assert\NotNull
*
* @Column(name="title", type="text")
*/
private $title;
/**
* @var string
*
* @Assert\NotNull
*
* @Column(name="short_title", type="text", length=8)
*/
private $shortTitle;
/**
* @var string
*
* @Column(name="ga_id", type="text", nullable=true)
*/
private $gaId = null;
/**
* @var string
*
* @Column(name="blog_url", type="text", nullable=true)
*/
private $blogUrl = null;
/**
* @var string
*
* @Column(name="facebook_url", type="text", nullable=true)
*/
private $facebookUrl = null;
/**
* @var string
*
* @Column(name="twitter_url", type="text", nullable=true)
*/
private $twitterUrl = null;
/**
* @var string
*
* @Column(name="youtube_url", type="text", nullable=true)
*/
private $youtubeUrl = null;
/**
* @var string
*
* @Column(name="gplus_url", type="text", nullable=true)
*/
private $GPlusUrl = null;
/**
* @var string
*
* @Column(name="livechat_username", type="text", nullable=true)
*/
private $liveChatUsername = null;
/**
* @var string
*
* @Column(name="javascripts", type="text", nullable=true)
*/
private $javascripts = null;
/**
* @var ArrayCollection
*
* @Assert\Valid
*
* @OneToMany(targetEntity="DocumentGroup", mappedBy="domain")
* @OrderBy({"prefix" = "DESC"})
**/
private $documentGroups;
/**
* @var ArrayCollection
*
* @Assert\Valid
*
* @OneToMany(targetEntity="Banner", mappedBy="domain")
* @OrderBy({"id" = "DESC"})
**/
private $banners;
/**
* @var ArrayCollection
*
* @Assert\Valid
*
* @OneToMany(targetEntity="Nanobar", mappedBy="domain")
**/
private $nanobars;
public function __construct()
{
parent::__construct();
$this->customers = new ArrayCollection();
$this->nanobars = new ArrayCollection();
}
/**
* @return Language
*/
public function getLanguage()
{
return $this->language;
}
/**
* @param Language $language
*/
public function setLanguage($language)
{
$this->language = $language;
}
/**
* @return Company
*/
public function getCompany()
{
return $this->company;
}
/**
* @param Company $company
*/
public function setCompany($company)
{
$this->company = $company;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return string
*/
public function getCurrency()
{
return $this->currency;
}
/**
* @param string $currency
*/
public function setCurrency($currency)
{
$this->currency = $currency;
}
/**
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* @param string $type
*/
public function setType($type)
{
if (!in_array($type, [self::TYPE_RETAIL, self::TYPE_WHOLESALE, self::TYPE_EXPORT, self::TYPE_OTHER])) {
throw new \LogicException('Invalid Domain Type.');
}
$this->type = $type;
}
/**
* @param null|float $date
* @return float
* date format: YmdHi
*/
public function getVat($date = null): float
{
$vat = $this->vat;
if ((in_array($this->getId(), [3, 4]) && !is_null($date) && $date < self::HU_VAT_CHANGE_DATE)
|| (in_array($this->getId(), [5, 9]) && !is_null($date) && $date < self::CZ_VAT_CHANGE_DATE)) {
if ($date < self::SK_VAT_CHANGE_DATE) {
$vat = 20.00;
} else {
$vat = 23.00;
}
}
return $vat;
}
/**
* @param float $vat
*/
public function setVat($vat)
{
$this->vat = $vat;
}
/**
* @Assert\IsTrue
*/
public function isVatValid()
{
return $this->vat >= 0;
}
/**
* @return float
*/
public function getPriceFactor()
{
return $this->priceFactor;
}
/**
* @param float $priceFactor
*/
public function setPriceFactor($priceFactor)
{
$this->priceFactor = $priceFactor;
}
/**
* @return int
*/
public function getPricePrecision()
{
return $this->pricePrecision;
}
/**
* @param int $pricePrecision
*/
public function setPricePrecision($pricePrecision)
{
$this->pricePrecision = $pricePrecision;
}
/**
* @return float
*/
public function getPriceEnd()
{
return $this->priceEnd;
}
/**
* @param float $priceEnd
*/
public function setPriceEnd($priceEnd)
{
$this->priceEnd = $priceEnd;
}
public function toDomainPrice($price)
{
return $this->getPriceFactor() * $price;
}
public function toDomainVatPrice($price)
{
return $this->toDomainPrice($price) * self::getVatMultiplier($this->vat);
}
public static function getVatMultiplier($vat)
{
return 1. + $vat / 100.;
}
/**
* this function exist at JavaScript version, file: product-list.js (with the same function name)
* @param $priceSrc
* @param float $vatMultiplier
* @return float convert the price based on the domain settings
*/
public function createVatPrice($priceSrc, float $vatMultiplier = 0)
{
if ($priceSrc <= 0) {
return 0;
}
if ($vatMultiplier > 0) {
$price = $priceSrc * $vatMultiplier;
} else {
$price = $priceSrc * self::getVatMultiplier($this->vat);
}
//round price
$price = round($price, $this->getPricePrecision(), PHP_ROUND_HALF_EVEN);
if (!in_array($priceSrc, ['11811.02', '39370.08', '78740.16', '196850.39'])) {
//setup ending
if ($priceSrc < $this->getPriceEnd2Under()) {
if (($priceEnd = $this->getPriceEnd2()) > 0) {
$price = $this->createPriceWithEnding($price, $priceEnd);
}
} else {
if (($priceEnd = $this->getPriceEnd()) > 0) {
$price = $this->createPriceWithEnding($price, $priceEnd);
}
}
}
if ($this->hasLastTwoDigits($price, '01')) {
$price = floor($price);
}
return $price;
}
/**
* @param $priceSrc
* @param string $country
* @return float convert the price based on the domain settings
*/
public function createWithoutVatPriceFromVatPrice($priceSrc, string $country = '')
{
if ($priceSrc <= 0) {
return 0;
}
$vatForCountry = VAT::getVatPercentageByCountry($country);
$vat = $country == '' ? $this->vat : $vatForCountry;
$price = $priceSrc / self::getVatMultiplier($vat);
//round price
$price = round($price, $this->getPricePrecision(), PHP_ROUND_HALF_EVEN);
//setup ending
if ($priceSrc < $this->getPriceEnd2Under()) {
if (($priceEnd = $this->getPriceEnd2()) > 0) {
$price = $this->createPriceWithEnding($price, $priceEnd);
}
} else {
if (($priceEnd = $this->getPriceEnd()) > 0) {
$price = $this->createPriceWithEnding($price, $priceEnd);
}
}
return $price;
}
// this function exist at JavaScript version, file: product-list.js (with the same function name)
private function createPriceWithEnding($price, $priceEnd)
{
$split = preg_split('/[.,]/', '' . $priceEnd);
$wholePlaces = strlen($split[0]);
if (count($split) > 1) {
$decimalPlaces = strlen($split[1]);
} else {
$decimalPlaces = 0;
}
//remove the ending
//ending: 990
//12888 => 12000
//12000+990 => 12990
if ($wholePlaces > 0) {
$rounder = pow(10, $wholePlaces);
$price1 = $price / $rounder;
$price1 = (int)($price1);
$price1 *= $rounder;
if ($price - $price1 > $priceEnd) {
$price = $price1 + $rounder;
} else {
$price = $price1;
}
} elseif ($decimalPlaces > 0) {
$price = (int)($price);
}
$price += $priceEnd;
return $price;
}
/**
* @return int
*/
public function getWarranty()
{
return $this->warranty;
}
/**
* @param int $warranty
*/
public function setWarranty($warranty)
{
$this->warranty = $warranty;
}
/**
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* @param string $url
*/
public function setUrl($url)
{
$this->url = $url;
}
/**
* @return ArrayCollection
*/
public function getCustomers()
{
return $this->customers;
}
/**
* @return ArrayCollection
*/
public function getDomainShippingTypes()
{
return $this->domainShippingTypes;
}
/**
* @param ArrayCollection $domainShippingTypes
*/
public function setDomainShippingTypes($domainShippingTypes)
{
$this->domainShippingTypes = $domainShippingTypes;
}
/**
* @return ArrayCollection
*/
public function getDomainPaymentTypes()
{
return $this->domainPaymentTypes;
}
/**
* @param ArrayCollection $domainPaymentTypes
*/
public function setDomainPaymentTypes($domainPaymentTypes)
{
$this->domainPaymentTypes = $domainPaymentTypes;
}
/**
* @return ArrayCollection
*/
public function getOrders()
{
return $this->orders;
}
/**
* @return float
*/
public function getPriceEnd2()
{
return $this->priceEnd2;
}
/**
* @param float $priceEnd2
*/
public function setPriceEnd2($priceEnd2)
{
$this->priceEnd2 = $priceEnd2;
}
/**
* @return float
*/
public function getPriceEnd2Under()
{
return $this->priceEnd2Under;
}
/**
* @param float $priceEnd2Under
*/
public function setPriceEnd2Under($priceEnd2Under)
{
$this->priceEnd2Under = $priceEnd2Under;
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* @return string
*/
public function getShortTitle()
{
return $this->shortTitle;
}
/**
* @param string $shortTitle
*/
public function setShortTitle($shortTitle)
{
$this->shortTitle = $shortTitle;
}
/**
* @return bool
*/
public function hasGaId()
{
return !is_null($this->gaId) && strlen($this->gaId) > 0;
}
/**
* @return string
*/
public function getGaId()
{
return $this->gaId;
}
/**
* @param string $gaId
*/
public function setGaId($gaId)
{
$this->gaId = $gaId;
}
/**
* @return bool
*/
public function hasBlogUrl()
{
return !is_null($this->blogUrl) && strlen($this->blogUrl) > 0;
}
/**
* @return string
*/
public function getBlogUrl()
{
return $this->blogUrl;
}
/**
* @param string $blogUrl
*/
public function setBlogUrl($blogUrl)
{
$this->blogUrl = $blogUrl;
}
/**
* @return bool
*/
public function hasFacebookUrl()
{
return !is_null($this->facebookUrl) && strlen($this->facebookUrl) > 0;
}
/**
* @return string
*/
public function getFacebookUrl()
{
return $this->facebookUrl;
}
/**
* @param string $facebookUrl
*/
public function setFacebookUrl($facebookUrl)
{
$this->facebookUrl = $facebookUrl;
}
/**
* @return bool
*/
public function hasTwitterUrl()
{
return !is_null($this->twitterUrl) && strlen($this->twitterUrl) > 0;
}
/**
* @return string
*/
public function getTwitterUrl()
{
return $this->twitterUrl;
}
/**
* @param string $twitterUrl
*/
public function setTwitterUrl($twitterUrl)
{
$this->twitterUrl = $twitterUrl;
}
/**
* @return bool
*/
public function hasYoutubeUrl()
{
return !is_null($this->youtubeUrl) && strlen($this->youtubeUrl) > 0;
}
/**
* @return string
*/
public function getYoutubeUrl()
{
return $this->youtubeUrl;
}
/**
* @param string $youtubeUrl
*/
public function setYoutubeUrl($youtubeUrl)
{
$this->youtubeUrl = $youtubeUrl;
}
/**
* @return bool
*/
public function hasGPlusUrl()
{
return !is_null($this->GPlusUrl) && strlen($this->GPlusUrl) > 0;
}
/**
* @return string
*/
public function getGPlusUrl()
{
return $this->GPlusUrl;
}
/**
* @param string $GPlusUrl
*/
public function setGPlusUrl($GPlusUrl)
{
$this->GPlusUrl = $GPlusUrl;
}
/**
* @return bool
*/
public function hasLiveChatUsername()
{
return !is_null($this->liveChatUsername) && strlen($this->liveChatUsername) > 0;
}
/**
* @return string
*/
public function getLiveChatUsername()
{
return $this->liveChatUsername;
}
/**
* @param string $liveChatUsername
*/
public function setLiveChatUsername($liveChatUsername)
{
$this->liveChatUsername = $liveChatUsername;
}
/**
* @return bool
*/
public function hasJavascripts()
{
return !is_null($this->javascripts) && strlen($this->javascripts) > 0;
}
/**
* @return string
*/
public function getJavascripts()
{
return $this->javascripts;
}
/**
* @param string $javaScripts
*/
public function setJavascripts($javaScripts)
{
$this->javascripts = $javaScripts;
}
/**
* @return ArrayCollection
*/
public function getDocumentGroups()
{
return $this->documentGroups;
}
/**
* @param ArrayCollection $documentGroups
*/
public function setDocumentGroups($documentGroups)
{
$this->documentGroups = $documentGroups;
}
/**
* @return User
*/
public function getDefaultSalesManager()
{
return $this->defaultSalesManager;
}
/**
* @param User $defaultSalesManager
*/
public function setDefaultSalesManager($defaultSalesManager)
{
$this->defaultSalesManager = $defaultSalesManager;
}
/**
* @return ArrayCollection
*/
public function getBanners()
{
return $this->banners;
}
/**
* @return array
*/
public function getActiveBanners()
{
$activeBanners = [];
foreach ($this->getBanners() as $banner) {
if ($banner->isCurrentlyActive()) {
$activeBanners[] = $banner;
}
}
return $activeBanners;
}
/**
* @return string
*/
public function getCurrencyTextCode()
{
switch (trim($this->getCurrency())) {
case '€':
$currencyTextCode = 'EUR';
break;
case 'Ft':
$currencyTextCode = 'HUF';
break;
case 'Kč':
$currencyTextCode = 'CZK';
break;
case 'CHF':
$currencyTextCode = 'CHF';
break;
default:
$currencyTextCode = '';
}
return $currencyTextCode;
}
public function addNanobar(Nanobar $nanobar): Domain
{
$this->nanobars[] = $nanobar;
return $this;
}
public function removeNanobar(Nanobar $nanobar)
{
return $this->nanobars->removeElement($nanobar);
}
public function getNanobars(): ArrayCollection
{
return $this->nanobars;
}
public function hasLastTwoDigits($number, $lastTwoDigitsToCheck): bool
{
$formatted = number_format($number, 2, '.', ''); // Ensure two decimal places
$lastTwo = substr($formatted, -2); // Get last two characters
return $lastTwo === $lastTwoDigitsToCheck;
}
}