<?php
namespace App\Entity\Building;
use App\Entity\Client\Client;
use App\Entity\Event\Event;
use App\Entity\Extension\BlameableTrait;
use App\Entity\Extension\DisabledReasonTrait;
use App\Entity\Extension\DisabledTrait;
use App\Entity\Extension\HistoricalInterface;
use App\Entity\Extension\ItemsWithHistoryTrait;
use App\Entity\Extension\TimestampableTrait;
use App\Entity\File;
use App\Entity\Gauge\Gauge;
use App\Entity\Role;
use App\Repository\Building\BuildingRepository;
use App\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=BuildingRepository::class)
* @ORM\Table(name="buildings")
*/
class Building
{
public const TYPE_BUILDING = 1;
public const TYPE_PUBLIC_LIGHTNING = 2;
public const TYPE_OTHER = 3;
use BlameableTrait;
use TimestampableTrait;
use ItemsWithHistoryTrait;
use DisabledTrait;
use DisabledReasonTrait;
/**
* @var null|int
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* @var Client
* @ORM\ManyToOne(targetEntity="App\Entity\Client\Client", inversedBy="buildings")
* @ORM\JoinColumn(name="client_id", referencedColumnName="id", nullable=false)
*/
private Client $client;
/**
* @var Collection|User[]
* @ORM\ManyToMany(targetEntity="App\Entity\User", mappedBy="buildings")
*/
protected $users;
/**
* @var Collection|File[]
*
* @ORM\ManyToMany(targetEntity="App\Entity\File", mappedBy="buildings", cascade={"persist"})
*/
private $files;
/**
* @var File|null
* @ORM\ManyToOne(targetEntity="App\Entity\File", inversedBy="buildingPhotos")
* @ORM\JoinColumn(nullable=true)
*/
private ?File $photo = null;
/**
* @var int
* @ORM\Column(type="smallint")
*
* @Assert\NotBlank
* @Assert\Type(type="int")
*/
private int $type;
/**
* @var Sector
* @ORM\ManyToOne(targetEntity="App\Entity\Building\Sector", inversedBy="buildings")
* @ORM\JoinColumn(name="sector_id", referencedColumnName="id", nullable=false)
* @Assert\NotBlank
*/
private Sector $sector;
/**
* @var string
* @ORM\Column(type="string")
*
* @Assert\NotBlank
* @Assert\Type(type="string")
*/
private string $name;
/**
* @var string|null
* @ORM\Column(type="string", nullable=true)
* @Assert\Type(type="string")
*/
private ?string $ico;
/**
* @var string|null
* @ORM\Column(type="string", nullable=true)
* @Assert\Type(type="string")
*/
private ?string $owner;
/**
* GID - Global Identification for Prague region
* @var string|null
* @ORM\Column(type="string", nullable=true)
*/
private ?string $gid = null;
/**
* @var string|null
* @ORM\Column(type="string", nullable=true)
* @Assert\Type(type="string")
*/
private ?string $delegatedManagerIco;
/**
* @var string|null
* @ORM\Column(type="string", nullable=true)
* @Assert\Type(type="string")
*/
private ?string $delegatedManager;
/**
* @var string|null
* @ORM\Column(type="string", nullable=true)
* @Assert\Type(type="string")
*/
private ?string $managerIco;
/**
* @var string|null
* @ORM\Column(type="string", nullable=true)
* @Assert\Type(type="string")
*/
private ?string $manager;
/**
* @var int|null
* @ORM\Column(type="integer", nullable=true)
* @Assert\Type(type="integer")
* @Assert\GreaterThan(0)
*/
private ?int $builtYear;
/**
* @var string|null
* @ORM\Column(type="text", nullable=true)
* @Assert\Type(type="string")
*/
private ?string $description;
/**
* @var string|null
* @ORM\Column(type="string", nullable=true)
* @Assert\Type(type="string")
*/
private ?string $street = null;
/**
* @var string|null
* @ORM\Column(type="string", nullable=true)
* @Assert\Type(type="string")
*/
private ?string $houseNumber = null;
/**
* @var string|null
* @ORM\Column(type="string", nullable=true)
* @Assert\Type(type="string")
*/
private ?string $referenceNumber = null;
/**
* @var int|null
* @ORM\Column(type="integer", nullable=true)
* @Assert\Type(type="integer")
* @Assert\Length(
* min = 5,
* max = 5
* )
*/
private ?int $zip = null;
/**
* @var string|null
* @ORM\Column(type="string", nullable=true)
* @Assert\Type(type="string")
*/
private ?string $city = null;
/**
* @var BuildingProperties
* @ORM\OneToOne(targetEntity="App\Entity\Building\BuildingProperties", mappedBy="building", cascade={"all"})
*/
private BuildingProperties $properties; // @phpstan-ignore-line
/**
* @var BuildingEnergyManagement
* @ORM\OneToOne(targetEntity="App\Entity\Building\BuildingEnergyManagement", mappedBy="building", cascade={"all"})
*/
private BuildingEnergyManagement $energyManagement; // @phpstan-ignore-line
/**
* @var Collection|BuildingUsersQuantity[]
* @ORM\OneToMany(targetEntity="App\Entity\Building\BuildingUsersQuantity", mappedBy="building", cascade={"all"})
*/
private $buildingUsersQuantity;
/**
* @var Collection|BuildingHeatedArea[]
* @ORM\OneToMany(targetEntity="App\Entity\Building\BuildingHeatedArea", mappedBy="building", cascade={"all"})
*/
private $buildingHeatedArea;
/**
* @var Collection|BuildingUsableArea[]
* @ORM\OneToMany(targetEntity="App\Entity\Building\BuildingUsableArea", mappedBy="building", cascade={"all"})
*/
private $buildingUsableArea;
/**
* @var Collection|BuiltUpArea[]
* @ORM\OneToMany(targetEntity="App\Entity\Building\BuiltUpArea", mappedBy="building", cascade={"all"})
*/
private $builtUpArea;
/**
* @var Collection|BuildingTemperature[]
* @ORM\OneToMany(targetEntity="App\Entity\Building\BuildingTemperature", mappedBy="building", cascade={"all"})
*/
private $temperature;
/**
* @var Collection|PublicLightPoints[]
* @ORM\OneToMany(targetEntity="App\Entity\Building\PublicLightPoints", mappedBy="building", cascade={"all"})
*/
private $publicLightPoints;
/**
* @var Collection|PublicLightPointsFVE[]
* @ORM\OneToMany(targetEntity="App\Entity\Building\PublicLightPointsFVE", mappedBy="building", cascade={"all"})
*/
private $publicLightPointsFVE;
/**
* @var Collection|Gauge[]
* @ORM\OneToMany(targetEntity="App\Entity\Gauge\Gauge", mappedBy="building")
*/
private $gauges;
/**
* @var Collection|CalculatedConsumption[]
* @ORM\OneToMany(targetEntity="App\Entity\Building\CalculatedConsumption", mappedBy="building", cascade={"all"})
*/
private $calculatedConsumption;
/**
* @var Collection|Event[]
*
* @ORM\ManyToMany(targetEntity="App\Entity\Event\Event", mappedBy="buildings", cascade={"persist"})
*/
private $events;
/**
* @var float|null
* @ORM\Column(type="float", nullable=true)
*/
private ?float $mapLat = null;
/**
* @var float|null
* @ORM\Column(type="float", nullable=true)
*/
private ?float $mapLng = null;
/**
* @var float|null
* @ORM\Column(type="float", nullable=true)
*/
private ?float $mapLatCustom = null;
/**
* @var float|null
* @ORM\Column(type="float", nullable=true)
*/
private ?float $mapLngCustom = null;
/**
* @var string|null
* @ORM\Column(type="string", nullable=true)
*/
private ?string $mapyCzUrl = null;
/**
* @var bool
* @ORM\Column(type="boolean", options={"default": true})
*/
private bool $showOnMap = true;
/**
* Building constructor.
*/
public function __construct()
{
$this->type = self::TYPE_BUILDING;
$this->properties = new BuildingProperties($this);
$this->energyManagement = new BuildingEnergyManagement($this);
$this->users = new ArrayCollection();
$this->buildingUsersQuantity = new ArrayCollection();
$this->buildingHeatedArea = new ArrayCollection();
$this->buildingUsableArea = new ArrayCollection();
$this->builtUpArea = new ArrayCollection();
$this->temperature = new ArrayCollection();
$this->publicLightPoints = new ArrayCollection();
$this->publicLightPointsFVE = new ArrayCollection();
$this->files = new ArrayCollection();
$this->gauges = new ArrayCollection();
$this->calculatedConsumption = new ArrayCollection();
$this->showOnMap = true;
}
public function __toString(): string
{
return $this->getName();
}
/**
* @param BuildingUsersQuantity $newItem
* @return $this
*/
public function addBuildingUsersQuantity(BuildingUsersQuantity $newItem): Building
{
$newItem->setBuilding($this);
$this->buildingUsersQuantity->add($newItem);
return $this;
}
/**
* @param bool $fullVersion
* @return string
*/
public function getAddress($fullVersion = false) : string
{
$address = '';
if ($this->getStreet() !== null) {
$address .= $this->getStreet();
}
if ($this->getHouseNumber() !== null) {
$address .= ' '.$this->getHouseNumber();
}
if ($this->getReferenceNumber() !== null) {
$address .= '/'.$this->getReferenceNumber();
}
if ($fullVersion) {
if ($this->getZip() !== null) {
$address .= ', '.$this->getZip();
}
if ($this->getCity() !== null) {
$address .= ' '.$this->getCity();
}
}
return $address;
}
/**
* @param File $file
* @return $this
*/
public function addFile(File $file): self
{
if (!$this->files->contains($file)) {
$this->files[] = $file;
$file->addBuilding($this);
}
return $this;
}
/**
* @param File $file
* @return $this
*/
public function removeFile(File $file): self
{
if ($this->files->contains($file)) {
$this->files->removeElement($file);
$file->removeBuilding($this);
}
return $this;
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @param int|null $id
* @return Building
*/
public function setId(?int $id): Building
{
$this->id = $id;
return $this;
}
/**
* @return Client
*/
public function getClient(): Client
{
return $this->client;
}
/**
* @param Client $client
* @return Building
*/
public function setClient(Client $client): Building
{
$this->client = $client;
return $this;
}
/**
* @return User[]|Collection
*/
public function getUsers()
{
return $this->users;
}
/**
* @param User[]|Collection $users
* @return Building
*/
public function setUsers($users)
{
$this->users = $users;
return $this;
}
/**
* @return File[]|Collection
*/
public function getFiles()
{
return $this->files;
}
/**
* @param File[]|Collection $files
* @return Building
*/
public function setFiles($files)
{
$this->files = $files;
return $this;
}
/**
* @return File|null
*/
public function getPhoto(): ?File
{
return $this->photo;
}
/**
* @param File|null $photo
* @return Building
*/
public function setPhoto(?File $photo): Building
{
$this->photo = $photo;
if ($photo !== null) {
$photo->addBuilding($this);
}
return $this;
}
/**
* @return int
*/
public function getType(): int
{
return $this->type;
}
/**
* @param int $type
* @return Building
*/
public function setType(int $type): Building
{
$this->type = $type;
return $this;
}
/**
* @return Sector
*/
public function getSector(): Sector
{
return $this->sector;
}
/**
* @param Sector $sector
* @return Building
*/
public function setSector(Sector $sector): Building
{
$this->sector = $sector;
return $this;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @param string $name
* @return Building
*/
public function setName(string $name): Building
{
$this->name = $name;
return $this;
}
/**
* @return string|null
*/
public function getIco(): ?string
{
return $this->ico;
}
/**
* @param string|null $ico
* @return Building
*/
public function setIco(?string $ico): Building
{
$this->ico = $ico;
return $this;
}
public function getGid(): ?string
{
return $this->gid;
}
public function setGid(?string $gid): Building
{
$this->gid = $gid;
return $this;
}
/**
* @return string|null
*/
public function getOwner(): ?string
{
return $this->owner;
}
/**
* @param string|null $owner
* @return Building
*/
public function setOwner(?string $owner): Building
{
$this->owner = $owner;
return $this;
}
/**
* @return string|null
*/
public function getManagerIco(): ?string
{
return $this->managerIco;
}
/**
* @param string|null $managerIco
* @return Building
*/
public function setManagerIco(?string $managerIco): Building
{
$this->managerIco = $managerIco;
return $this;
}
/**
* @return string|null
*/
public function getManager(): ?string
{
return $this->manager;
}
/**
* @param string|null $manager
* @return Building
*/
public function setManager(?string $manager): Building
{
$this->manager = $manager;
return $this;
}
/**
* @return int|null
*/
public function getBuiltYear(): ?int
{
return $this->builtYear;
}
/**
* @param int|null $builtYear
* @return Building
*/
public function setBuiltYear(?int $builtYear): Building
{
$this->builtYear = $builtYear;
return $this;
}
/**
* @return string|null
*/
public function getDescription(): ?string
{
return $this->description;
}
/**
* @param string|null $description
* @return Building
*/
public function setDescription(?string $description): Building
{
$this->description = $description;
return $this;
}
/**
* @return string|null
*/
public function getStreet(): ?string
{
return $this->street;
}
/**
* @param string|null $street
* @return Building
*/
public function setStreet(?string $street): Building
{
$this->street = $street;
return $this;
}
/**
* @return string|null
*/
public function getHouseNumber(): ?string
{
return $this->houseNumber;
}
/**
* @param string|null $houseNumber
* @return Building
*/
public function setHouseNumber(?string $houseNumber): Building
{
$this->houseNumber = $houseNumber;
return $this;
}
/**
* @return string|null
*/
public function getReferenceNumber(): ?string
{
return $this->referenceNumber;
}
/**
* @param string|null $referenceNumber
* @return Building
*/
public function setReferenceNumber(?string $referenceNumber): Building
{
$this->referenceNumber = $referenceNumber;
return $this;
}
/**
* @return int|null
*/
public function getZip(): ?int
{
return $this->zip;
}
/**
* @param int|null $zip
* @return Building
*/
public function setZip(?int $zip): Building
{
$this->zip = $zip;
return $this;
}
/**
* @return string|null
*/
public function getCity(): ?string
{
return $this->city;
}
/**
* @param string|null $city
* @return Building
*/
public function setCity(?string $city): Building
{
$this->city = $city;
return $this;
}
/**
* @return BuildingProperties
*/
public function getProperties(): BuildingProperties
{
return $this->properties;
}
/**
* @param BuildingProperties $properties
* @return Building
*/
public function setProperties(BuildingProperties $properties): Building
{
$this->properties = $properties;
return $this;
}
/**
* @return BuildingEnergyManagement
*/
public function getEnergyManagement(): BuildingEnergyManagement
{
return $this->energyManagement;
}
/**
* @param BuildingEnergyManagement $energyManagement
* @return Building
*/
public function setEnergyManagement(BuildingEnergyManagement $energyManagement): Building
{
$this->energyManagement = $energyManagement;
return $this;
}
/**
* @return BuildingUsersQuantity[]|Collection
*/
public function getBuildingUsersQuantity()
{
return $this->buildingUsersQuantity;
}
/**
* @param BuildingUsersQuantity[]|Collection $buildingUsersQuantity
* @return Building
*/
public function setBuildingUsersQuantity($buildingUsersQuantity)
{
$this->buildingUsersQuantity = $buildingUsersQuantity;
return $this;
}
/**
* @return BuildingHeatedArea[]|Collection
*/
public function getBuildingHeatedArea()
{
return $this->buildingHeatedArea;
}
/**
* @param BuildingHeatedArea[]|Collection $buildingHeatedArea
* @return Building
*/
public function setBuildingHeatedArea($buildingHeatedArea)
{
$this->buildingHeatedArea = $buildingHeatedArea;
return $this;
}
/**
* @param BuildingHeatedArea $newItem
* @return $this
*/
public function addBuildingHeatedArea(BuildingHeatedArea $newItem): Building
{
$newItem->setBuilding($this);
$this->buildingHeatedArea->add($newItem);
return $this;
}
/**
* @return BuildingUsableArea[]|Collection
*/
public function getBuildingUsableArea()
{
return $this->buildingUsableArea;
}
/**
* @param BuildingUsableArea[]|Collection $buildingUsableArea
* @return Building
*/
public function setBuildingUsableArea($buildingUsableArea)
{
$this->buildingUsableArea = $buildingUsableArea;
return $this;
}
/**
* @param BuildingUsableArea $newItem
* @return $this
*/
public function addBuildingUsableArea(BuildingUsableArea $newItem): Building
{
$newItem->setBuilding($this);
$this->buildingUsableArea->add($newItem);
return $this;
}
/**
* @return BuiltUpArea[]|Collection
*/
public function getBuiltUpArea()
{
return $this->builtUpArea;
}
/**
* @param BuiltUpArea[]|Collection $builtUpArea
* @return Building
*/
public function setBuiltUpArea($builtUpArea)
{
$this->builtUpArea = $builtUpArea;
return $this;
}
/**
* @param BuiltUpArea $newItem
* @return $this
*/
public function addBuiltUpArea(BuiltUpArea $newItem): Building
{
$newItem->setBuilding($this);
$this->builtUpArea->add($newItem);
return $this;
}
/**
* @return BuildingTemperature[]|Collection
*/
public function getTemperature()
{
return $this->temperature;
}
/**
* @param BuildingTemperature[]|Collection $temperature
* @return Building
*/
public function setTemperature($temperature)
{
$this->temperature = $temperature;
return $this;
}
/**
* @param BuildingTemperature $newItem
* @return $this
*/
public function addTemperature(BuildingTemperature $newItem): Building
{
$newItem->setBuilding($this);
$this->temperature->add($newItem);
return $this;
}
/**
* @return PublicLightPoints[]|Collection
*/
public function getPublicLightPoints()
{
return $this->publicLightPoints;
}
/**
* @param PublicLightPoints[]|Collection $publicLightPoints
* @return Building
*/
public function setPublicLightPoints($publicLightPoints)
{
$this->publicLightPoints = $publicLightPoints;
return $this;
}
/**
* @param PublicLightPoints $newItem
* @return $this
*/
public function addPublicLightPoints(PublicLightPoints $newItem): Building
{
$newItem->setBuilding($this);
$this->publicLightPoints->add($newItem);
return $this;
}
/**
* @return PublicLightPointsFVE[]|Collection
*/
public function getPublicLightPointsFVE()
{
return $this->publicLightPointsFVE;
}
/**
* @param PublicLightPointsFVE[]|Collection $publicLightPointsFVE
* @return Building
*/
public function setPublicLightPointsFVE($publicLightPointsFVE)
{
$this->publicLightPointsFVE = $publicLightPointsFVE;
return $this;
}
/**
* @param PublicLightPointsFVE $newItem
* @return $this
*/
public function addPublicLightPointsFVE(PublicLightPointsFVE $newItem): Building
{
$newItem->setBuilding($this);
$this->publicLightPointsFVE->add($newItem);
return $this;
}
/**
* @return Gauge[]|Collection
*/
public function getGauges()
{
return $this->gauges;
}
/**
* @param Gauge[]|Collection $gauges
* @return Building
*/
public function setGauges($gauges)
{
$this->gauges = $gauges;
return $this;
}
/**
* @return CalculatedConsumption[]|Collection
*/
public function getCalculatedConsumption()
{
return $this->calculatedConsumption;
}
/**
* @param CalculatedConsumption[]|Collection $calculatedConsumption
* @return Building
*/
public function setCalculatedConsumption($calculatedConsumption)
{
$this->calculatedConsumption = $calculatedConsumption;
return $this;
}
/**
* @param CalculatedConsumption $calculatedConsumption
* @return Building
*/
public function addCalculatedConsumption(CalculatedConsumption $calculatedConsumption)
{
$calculatedConsumption->setBuilding($this);
if ($calculatedConsumption->getMonthValues()->count() === 0) {
// add month values by ratios
$calculatedConsumption->addMonthValuesByRatios();
}
$this->calculatedConsumption->add($calculatedConsumption);
return $this;
}
/**
* @param int $usage
* @param \DateTime|null $date
* @return HistoricalInterface|CalculatedConsumption|mixed|null
*/
public function getCurrentCalculatedConsumption(int $usage, ?\DateTime $date = null)
{
if ($date === null) {
$date = new \DateTime();
}
$current = null;
foreach ($this->getCalculatedConsumption() as $item) {
if ($item->getValidFrom() <= $date
&& $usage === $item->getUsage()
&& (
!$current instanceof CalculatedConsumption
|| $item->getValidFrom() > $current->getValidFrom()
)
) {
$current = $item;
}
}
return $current;
}
/**
* @return Event[]|Collection
*/
public function getEvents()
{
return $this->events;
}
/**
* @param Event[]|Collection $events
* @return Building
*/
public function setEvents($events)
{
$this->events = $events;
return $this;
}
/**
* @return string|null
*/
public function getDelegatedManagerIco(): ?string
{
return $this->delegatedManagerIco;
}
/**
* @param string|null $delegatedManagerIco
* @return Building
*/
public function setDelegatedManagerIco(?string $delegatedManagerIco): Building
{
$this->delegatedManagerIco = $delegatedManagerIco;
return $this;
}
/**
* @return string|null
*/
public function getDelegatedManager(): ?string
{
return $this->delegatedManager;
}
/**
* @param string|null $delegatedManager
* @return Building
*/
public function setDelegatedManager(?string $delegatedManager): Building
{
$this->delegatedManager = $delegatedManager;
return $this;
}
/**
* Uživatel Pracovník u daného objektu
* @return User|null
*/
public function getResponsibleUser(): ?User
{
foreach ($this->getUsers() as $user) {
if ($user->getRole()->getName() === Role::WORKER) {
return $user;
}
}
return null;
}
/**
* @return float|null
*/
public function getMapLat(): ?float
{
return $this->mapLat;
}
/**
* @param float|null $mapLat
* @return $this
*/
public function setMapLat(?float $mapLat): self
{
$this->mapLat = $mapLat;
return $this;
}
/**
* @return float|null
*/
public function getMapLng(): ?float
{
return $this->mapLng;
}
/**
* @param float|null $mapLng
* @return $this
*/
public function setMapLng(?float $mapLng): self
{
$this->mapLng = $mapLng;
return $this;
}
/**
* @return float|null
*/
public function getMapLatCustom(): ?float
{
return $this->mapLatCustom;
}
/**
* @param float|null $mapLatCustom
* @return $this
*/
public function setMapLatCustom(?float $mapLatCustom): self
{
$this->mapLatCustom = $mapLatCustom;
return $this;
}
/**
* @return float|null
*/
public function getMapLngCustom(): ?float
{
return $this->mapLngCustom;
}
/**
* @param float|null $mapLngCustom
* @return $this
*/
public function setMapLngCustom(?float $mapLngCustom): self
{
$this->mapLngCustom = $mapLngCustom;
return $this;
}
public function getMapLatEffective(): ?float
{
if ($this->hasCustomMapCoordinates()) {
return $this->mapLatCustom;
}
return $this->mapLat;
}
public function getMapLngEffective(): ?float
{
if ($this->hasCustomMapCoordinates()) {
return $this->mapLngCustom;
}
return $this->mapLng;
}
private function hasCustomMapCoordinates(): bool
{
return $this->mapLatCustom !== null && $this->mapLngCustom !== null;
}
/**
* @return string|null
*/
public function getMapyCzUrl(): ?string
{
return $this->mapyCzUrl;
}
/**
* @param string|null $mapyCzUrl
* @return $this
*/
public function setMapyCzUrl(?string $mapyCzUrl): self
{
$this->mapyCzUrl = $mapyCzUrl;
return $this;
}
/**
* @return bool
*/
public function isShowOnMap(): bool
{
return $this->showOnMap;
}
/**
* @param bool $showOnMap
* @return $this
*/
public function setShowOnMap(bool $showOnMap): self
{
$this->showOnMap = $showOnMap;
return $this;
}
}