<?php
namespace App\Entity\Gauge;
use App\Entity\Building\Building;
use App\Entity\Building\Sector;
use App\Entity\ContractPrice\Contract;
use App\Entity\ContractPrice\ContractItemType;
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\Import\DataImport;
use App\Entity\PriceDecision\Distributor;
use App\Repository\Gauge\GaugeRepository;
use App\Utils\Dataset;
use App\Utils\GaugeType;
use App\Utils\Period;
use App\Utils\Units;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=GaugeRepository::class)
* @ORM\Table(name="gauges")
*/
class Gauge
{
/**
* @var int|null
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* values are defined above in class definition
* @var int
* @ORM\Column(type="smallint", nullable=false)
*/
private int $level;
/**
* values are defined in App\Utils\GaugeType::TYPE_...
* @var int
* @ORM\Column(type="smallint", nullable=false)
*/
private int $type;
/**
* values are defined in App\Utils\GaugeType::SOURCE...
* @var int|null
* @ORM\Column(type="smallint", nullable=true)
*/
private ?int $primarySource = null;
/**
* @var string|null
* @ORM\Column(type="string", nullable=true)
*/
private ?string $primarySourceOther = null;
/**
* @var Building
* @ORM\ManyToOne(targetEntity="App\Entity\Building\Building", inversedBy="gauges")
* @ORM\JoinColumn(name="building_id", referencedColumnName="id", nullable=false)
*/
private Building $building;
/**
* @var Collection|Gauge[]
* @ORM\OneToMany(targetEntity="App\Entity\Gauge\Gauge", mappedBy="parent", cascade={"persist"})
*/
protected $children;
/**
* @var Collection|File[]
*
* @ORM\ManyToMany(targetEntity="App\Entity\File", mappedBy="gauges")
*/
private $files;
/**
* @var Gauge|null
* @ORM\ManyToOne(targetEntity="App\Entity\Gauge\Gauge", inversedBy="children")
* @ORM\JoinColumn(name="parent", referencedColumnName="id", nullable=true)
*/
protected ?Gauge $parent = null;
/**
* @var string
* @ORM\Column(type="string", nullable=false)
*/
private string $name = '';
/**
* Path to generated QR code image linking to gauge readings
* @var string|null
* @ORM\Column(type="string", name="qr_code_path", nullable=true)
*/
private ?string $qrCodePath = null;
/**
* values are defined in App\Utils\Dataset::MEASUREMENT_PARAM...
* @var int
* @ORM\Column(type="smallint", nullable=false)
*/
private int $measurementTarget = GaugeType::MEASUREMENT_TARGET_CONSUMPTION;
/**
* indicator if is allowed remote readings
* @var bool
* @ORM\Column(type="boolean")
*/
private bool $remote = false;
/**
* @var bool
* @ORM\Column(type="boolean", nullable=false, options={"default" : 0})
*/
private bool $remoteAsPrimary = false;
/**
* Php stan exception: this is right entity mapping by documentation. In constructor is solved not-nullable properties.
* https://github.com/phpstan/phpstan-doctrine/issues/97
*
* @ORM\OneToOne(targetEntity="App\Entity\Gauge\GaugeProperties", mappedBy="gauge", cascade={"all"})
*/
private GaugeProperties $properties; // @phpstan-ignore-line
/**
* @var Collection|GaugeRate[]
* @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugeRate", mappedBy="gauge", cascade={"all"})
*/
private $rates;
/**
* @var Collection|ShareOfGauges[]
* @ORM\OneToMany(targetEntity="App\Entity\Gauge\ShareOfGauges", mappedBy="gauge", cascade={"all"})
*/
private $shares;
/**
* @var Collection|GaugePhaseAndBreaker[]
* @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugePhaseAndBreaker", mappedBy="gauge", cascade={"all"})
*/
private $phaseAndBreaker;
/**
* @var Collection|GaugeHeatingPower[]
* @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugeHeatingPower", mappedBy="gauge", cascade={"all"})
*/
private $heatingPower;
/**
* @var Collection|GaugeUnitPrice[]
* @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugeUnitPrice", mappedBy="gauge", cascade={"all"})
* @ORM\OrderBy({"validFrom" = "ASC"})
*/
private $unitPrice;
/**
* @var Collection|GaugePriceSupplyPoint[]
* @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugePriceSupplyPoint", mappedBy="gauge", cascade={"all"})
*/
private $priceSupplyPoint;
/**
* @var GaugeRemoteProperties|null
* @ORM\OneToOne(targetEntity="App\Entity\Gauge\GaugeRemoteProperties", mappedBy="gauge", cascade={"all"})
*/
private ?GaugeRemoteProperties $remoteProperties;
/**
* @var File|null
* @ORM\ManyToOne(targetEntity="App\Entity\File", inversedBy="gaugePhotos")
* @ORM\JoinColumn(nullable=true)
*/
private ?File $photo = null;
/**
* @var Distributor|null
* @ORM\ManyToOne(targetEntity="App\Entity\PriceDecision\Distributor")
* @ORM\JoinColumn(name="el_dictributor_id", referencedColumnName="id", nullable=true)
*/
private ?Distributor $electricityDistributor = null;
/**
* @var Collection|GaugePurpose[]
* @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugePurpose", mappedBy="gauge", cascade={"all"})
*/
private $purposes;
/**
* @var Collection|GaugeReading[]
*
* @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugeReading", mappedBy="gauge")
*/
private $readings;
/**
* @var Collection|Invoice[]
*
* @ORM\OneToMany(targetEntity="App\Entity\Gauge\Invoice", mappedBy="gauge")
*/
private $invoices;
/**
* @var Collection|Event[]
*
* @ORM\ManyToMany(targetEntity="App\Entity\Event\Event", mappedBy="gauges", cascade={"persist"})
*/
private $events;
/**
* @var Collection|GaugeInvoiceUnit[]
* @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugeInvoiceUnit", mappedBy="gauge", cascade={"all"})
*/
private $invoiceUnits;
/**
* this attribute sets if data can be showed in output after gauge is disabled
* @var \bool
*
* @ORM\Column(name="show_in_output", type="boolean", nullable=false, options={"default": 1})
*/
protected bool $showInOutput = true;
/**
* values are defined in App\Utils\Dataset::CONSUMPTION_IN...
* @var int|null
* @ORM\Column(type="smallint", nullable=true)
*/
private ?int $dataInResults = Dataset::CONSUMPTION_IN_RESULTS_ON;
/**
* @var Collection|ConsumptionLimit[]
* @ORM\ManyToMany(targetEntity="App\Entity\Gauge\ConsumptionLimit", mappedBy="gauges", cascade={"persist"})
*/
private $consumptionLimits;
/**
* @var \DateTime|null
*
* @ORM\Column(type="datetime", nullable=true)
*/
private ?\DateTime $lastSentNotificationAt;
/**
* @var DataImport|null
* @ORM\ManyToOne(targetEntity="App\Entity\Import\DataImport", inversedBy="gauges")
* @ORM\JoinColumn(nullable=true)
*/
private ?DataImport $import = null;
/**
* @var Collection|Contract[]|null
* @ORM\ManyToMany(targetEntity=Contract::class, mappedBy="gauges")
*/
private $contracts;
use BlameableTrait;
use TimestampableTrait;
use ItemsWithHistoryTrait;
use DisabledTrait;
use DisabledReasonTrait;
public function __construct()
{
$this->type = GaugeType::TYPE_ELECTRICITY_METER;
$this->level = GaugeType::LEVEL_MAIN;
$this->setProperties(new GaugeProperties($this));
$this->remoteProperties = null;
$this->children = new ArrayCollection();
$this->files = new ArrayCollection();
$this->rates = new ArrayCollection();
$this->phaseAndBreaker = new ArrayCollection();
$this->heatingPower = new ArrayCollection();
$this->unitPrice = new ArrayCollection();
$this->priceSupplyPoint = new ArrayCollection();
$this->readings = new ArrayCollection();
$this->purposes = new ArrayCollection();
$this->invoiceUnits = new ArrayCollection();
$this->contracts = new ArrayCollection();
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @param int|null $id
* @return Gauge
*/
public function setId(?int $id): Gauge
{
$this->id = $id;
return $this;
}
/**
* @return int
*/
public function getLevel(): int
{
return $this->level;
}
/**
* @param int $level
* @return Gauge
*/
public function setLevel(int $level): Gauge
{
$this->level = $level;
return $this;
}
/**
* @return int
*/
public function getType(): int
{
return $this->type;
}
/**
* @param int $type
* @return Gauge
*/
public function setType(int $type): Gauge
{
$this->type = $type;
return $this;
}
/**
* @return Building
*/
public function getBuilding(): Building
{
return $this->building;
}
/**
* @param Building $building
* @return Gauge
*/
public function setBuilding(Building $building): Gauge
{
$this->building = $building;
return $this;
}
/**
* @return Gauge[]|Collection
*/
public function getChildren()
{
return $this->children;
}
/**
* @return Gauge|null
*/
public function getParent(): ?Gauge
{
return $this->parent;
}
/**
* @param Gauge|null $parent
* @return Gauge
*/
public function setParent(?Gauge $parent): Gauge
{
$this->parent = $parent;
return $this;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @param string $name
* @return Gauge
*/
public function setName(string $name): Gauge
{
$this->name = $name;
return $this;
}
/**
* @return string|null
*/
public function getQrCodePath(): ?string
{
return $this->qrCodePath;
}
/**
* @param string|null $qrCodePath
* @return $this
*/
public function setQrCodePath(?string $qrCodePath): self
{
$this->qrCodePath = $qrCodePath;
return $this;
}
/**
* @return GaugeProperties
*/
public function getProperties(): GaugeProperties
{
return $this->properties;
}
/**
* @param GaugeProperties $properties
* @return Gauge
*/
public function setProperties(GaugeProperties $properties): Gauge
{
$this->properties = $properties;
return $this;
}
/**
* @return Collection|File[]
*/
public function getFiles(): Collection
{
return $this->files;
}
/**
* @param File $file
* @return $this
*/
public function addFile(File $file): self
{
if (!$this->files->contains($file)) {
$file->addGauge($this);
$this->files->add($file);
}
return $this;
}
/**
* @return int|null
*/
public function getPrimarySource(): ?int
{
return $this->primarySource;
}
/**
* @param int|null $primarySource
* @return Gauge
*/
public function setPrimarySource(?int $primarySource): Gauge
{
$this->primarySource = $primarySource;
return $this;
}
/**
* @return string|null
*/
public function getPrimarySourceOther(): ?string
{
return $this->primarySourceOther;
}
/**
* @param string|null $primarySourceOther
* @return Gauge
*/
public function setPrimarySourceOther(?string $primarySourceOther): Gauge
{
$this->primarySourceOther = $primarySourceOther;
return $this;
}
/**
* @param File $file
* @return $this
*/
public function removeFile(File $file): self
{
if ($this->files->contains($file)) {
$this->files->removeElement($file);
$file->removeGauge($this);
}
return $this;
}
/**
* kontrola zda je měřidlo podružné
* @return bool
*/
public function isSecondary(): bool
{
if ($this->getLevel() === GaugeType::LEVEL_SECONDARY && $this->getParent() !== null) {
return true;
}
return false;
}
/**
* kontrola zda je měřidlo hlavnÃ
* @return bool
*/
public function isMain(): bool
{
return $this->getLevel() === GaugeType::LEVEL_MAIN;
}
/**
* @return string
*/
public function __toString(): string
{
return $this->getName();
}
/**
* @return File|null
*/
public function getPhoto(): ?File
{
return $this->photo;
}
/**
* @param File|null $photo
* @return Gauge
*/
public function setPhoto(?File $photo): Gauge
{
$this->photo = $photo;
if ($photo !== null) {
$photo->addGauge($this);
}
return $this;
}
/**
* @return GaugeRate[]|Collection
*/
public function getRates()
{
return $this->rates;
}
/**
* @param GaugeRate[]|Collection $rates
* @return Gauge
*/
public function setRates($rates)
{
$this->rates = $rates;
return $this;
}
/**
* @param GaugeRate $rate
* @return Gauge
*/
public function addRate(GaugeRate $rate): Gauge
{
$rate->setGauge($this);
$this->rates->add($rate);
return $this;
}
/**
* @return GaugePhaseAndBreaker[]|Collection
*/
public function getPhaseAndBreaker(): Collection
{
return $this->phaseAndBreaker;
}
/**
* @param GaugePhaseAndBreaker[]|Collection $phaseAndBreaker
* @return Gauge
*/
public function setPhaseAndBreaker($phaseAndBreaker): Gauge
{
$this->phaseAndBreaker = $phaseAndBreaker;
return $this;
}
/**
* @param GaugePhaseAndBreaker $phaseAndBreaker
* @return Gauge
*/
public function addPhaseAndBreaker(GaugePhaseAndBreaker $phaseAndBreaker): Gauge
{
$phaseAndBreaker->setGauge($this);
$this->phaseAndBreaker->add($phaseAndBreaker);
return $this;
}
/**
* @return GaugeHeatingPower[]|Collection
*/
public function getHeatingPower()
{
return $this->heatingPower;
}
/**
* @param GaugeHeatingPower[]|Collection $heatingPower
* @return Gauge
*/
public function setHeatingPower($heatingPower): Gauge
{
$this->heatingPower = $heatingPower;
return $this;
}
/**
* @param GaugeHeatingPower $heatingPower
* @return Gauge
*/
public function addHeatingPower(GaugeHeatingPower $heatingPower): Gauge
{
$heatingPower->setGauge($this);
$this->heatingPower->add($heatingPower);
return $this;
}
/**
* @return GaugeUnitPrice[]|Collection
*/
public function getUnitPrice()
{
return $this->unitPrice;
}
/**
* @param GaugeUnitPrice[]|Collection $unitPrice
* @return Gauge
*/
public function setUnitPrice($unitPrice): Gauge
{
$this->unitPrice = $unitPrice;
return $this;
}
/**
* @param GaugeUnitPrice $unitPrice
* @return $this
*/
public function addUnitPrice(GaugeUnitPrice $unitPrice): Gauge
{
$unitPrice->setGauge($this);
$this->unitPrice->add($unitPrice);
return $this;
}
/**
* @return GaugePriceSupplyPoint[]|Collection
*/
public function getPriceSupplyPoint(): Collection
{
return $this->priceSupplyPoint;
}
/**
* @param GaugePriceSupplyPoint[]|Collection $priceSupplyPoint
* @return Gauge
*/
public function setPriceSupplyPoint($priceSupplyPoint): Gauge
{
$this->priceSupplyPoint = $priceSupplyPoint;
return $this;
}
/**
* @param GaugePriceSupplyPoint $priceSupplyPoint
* @return $this
*/
public function addPriceSupplyPoint(GaugePriceSupplyPoint $priceSupplyPoint): Gauge
{
$priceSupplyPoint->setGauge($this);
$this->priceSupplyPoint->add($priceSupplyPoint);
return $this;
}
/**
* @return GaugeRemoteProperties|null
*/
public function getRemoteProperties(): ?GaugeRemoteProperties
{
return $this->remoteProperties;
}
/**
* @param GaugeRemoteProperties|null $remoteProperties
* @return Gauge
*/
public function setRemoteProperties(?GaugeRemoteProperties $remoteProperties): Gauge
{
$this->remoteProperties = $remoteProperties;
return $this;
}
/**
* @param int $mediumType
* @param \DateTime|null $date
* @return HistoricalInterface|GaugeUnitPrice|mixed|null
*/
public function getCurrentUnitPrice(int $mediumType, ?\DateTime $date = null)
{
if ($date === null) {
$date = new \DateTime();
}
$current = null;
foreach ($this->getUnitPrice() as $item) {
if ($item->getValidFrom() <= $date
&& $mediumType === $item->getMedium()
&& (
!$current instanceof GaugeUnitPrice
|| $item->getValidFrom() > $current->getValidFrom()
)
) {
$current = $item;
}
}
return $current;
}
/**
* @return bool
*/
public function isRemote(): bool
{
return $this->remote;
}
/**
* @param bool $remote
* @return Gauge
*/
public function setRemote(bool $remote): Gauge
{
$this->remote = $remote;
return $this;
}
/**
* @return GaugeReading[]|Collection
*/
public function getReadings()
{
return $this->readings;
}
/**
* @return bool
*/
public function isElectricityMeter(): bool
{
return $this->isType(GaugeType::TYPE_ELECTRICITY_METER);
}
/**
* @return bool
*/
public function isGasometter(): bool
{
return $this->isType(GaugeType::TYPE_GASOMETER);
}
/**
* @param int $type
* @return bool
*/
public function isType(int $type): bool
{
return $this->getType() === $type;
}
/**
* @return bool
*/
public function canHaveReadings(): bool
{
return $this->getLevel() !== GaugeType::LEVEL_FICTIVE && $this->getType() !== GaugeType::TYPE_OTHER_FUELS;
}
/**
* @return bool
*/
public function canHaveInvoices(): bool
{
return true;
}
public function hasProduction(): bool
{
return in_array($this->getMeasurementTarget(), [GaugeType::MEASUREMENT_TARGET_PRODUCTION, GaugeType::MEASUREMENT_TARGET_CONSUMPTION_DELIVERY], true);
}
/**
* @return GaugePurpose[]|Collection
*/
public function getPurposes(): Collection
{
return $this->purposes;
}
/**
* @param GaugePurpose[]|Collection $purposes
* @return Gauge
*/
public function setPurposes($purposes): Gauge
{
$this->purposes = $purposes;
return $this;
}
/**
* @param GaugePurpose $gaugePurpose
* @return Gauge
*/
public function addPurpose(GaugePurpose $gaugePurpose): Gauge
{
if (!$this->purposes->contains($gaugePurpose)) {
$gaugePurpose->setGauge($this);
$this->purposes->add($gaugePurpose);
}
return $this;
}
/**
* @return int
*/
public function getMeasurementTarget(): int
{
return $this->measurementTarget;
}
/**
* @param int $measurementTarget
* @return Gauge
*/
public function setMeasurementTarget(int $measurementTarget): Gauge
{
$this->measurementTarget = $measurementTarget;
return $this;
}
/**
* @param int $purposeConst
* @return GaugePurpose|null
*/
public function getPurposeObject(int $purposeConst): ?GaugePurpose
{
foreach ($this->getPurposes() as $gaugePurpose) {
if ($gaugePurpose->getPurpose() === $purposeConst) {
return $gaugePurpose;
}
}
return null;
}
/**
* @return GaugePurpose|null
*/
public function getPurposeFirst(): ?GaugePurpose
{
if ($this->purposes->count() > 0) {
return $this->purposes->first();
}
return null;
}
/**
* @param int $purposeConst
* @return GaugePurpose
*/
public function getPurposeNewOrExistObject(int $purposeConst): GaugePurpose
{
$gaugePurpose = $this->getPurposeObject($purposeConst);
if ($gaugePurpose === null) {
$gaugePurpose = new GaugePurpose();
$gaugePurpose->setPurpose($purposeConst);
$gaugePurpose->setGauge($this);
}
return $gaugePurpose;
}
/**
* @return Invoice[]|Collection
*/
public function getInvoices(): Collection
{
return $this->invoices;
}
/**
* @param Invoice[]|Collection $invoices
* @return Gauge
*/
public function setInvoices($invoices): Gauge
{
$this->invoices = $invoices;
return $this;
}
/**
* @param Invoice $invoice
* @return Gauge
*/
public function addInvoice(Invoice $invoice): Gauge
{
$this->invoices[] = $invoice;
return $this;
}
/**
* @param \DateTime|null $date
* @return int
*/
public function getInvoiceUnit(?\DateTime $date = null): int
{
$invUnitObj = $this->getCurrent('invoiceUnits', $date, $this->getInvoiceUnits());
if ($invUnitObj instanceof GaugeInvoiceUnit) {
return $invUnitObj->getUnit();
}
return Units::getDefaultUnitByGaugeType($this->getType());
}
/**
* @return Event[]|Collection
*/
public function getEvents()
{
return $this->events;
}
/**
* @param Event[]|Collection $events
* @return Gauge
*/
public function setEvents($events): Gauge
{
$this->events = $events;
return $this;
}
public function getSector(): Sector
{
return $this->getBuilding()->getSector();
}
/**
* @return ShareOfGauges[]|Collection
*/
public function getShares()
{
return $this->shares;
}
/**
* @param ShareOfGauges[]|Collection $shares
* @return Gauge
*/
public function setShares($shares): Gauge
{
$this->shares = $shares;
return $this;
}
/**
* @return array
*/
public function toArray(): array
{
return [
'id' => $this->getId(),
'name' => $this->getName()
];
}
/**
* @return bool
*/
public function isElectricityWholesale():bool
{
if (
$this->getType() === GaugeType::TYPE_ELECTRICITY_METER &&
in_array($this->getProperties()->getConsumptionType(), [Dataset::CONSUMPTION_TYPE_WHOLESALE_VN, Dataset::CONSUMPTION_TYPE_WHOLESALE_VVN], true)
) {
return true;
}
return false;
}
/**
* @return bool
*/
public function isForceLowTariff():bool
{
return $this->getProperties()->getForceLowTariff() === true;
}
/**
* @return bool
*/
public function isForceSpecialTariff():bool
{
return $this->getProperties()->getForceSpecialTariff() === true;
}
/**
* @return GaugeInvoiceUnit[]|Collection
*/
public function getInvoiceUnits(): Collection
{
return $this->invoiceUnits;
}
/**
* @param GaugeInvoiceUnit $item
* @return $this
*/
public function addInvoiceUnit(GaugeInvoiceUnit $item): self
{
$item->setGauge($this);
$this->invoiceUnits->add($item);
return $this;
}
/**
* @return Distributor|null
*/
public function getElectricityDistributor(): ?Distributor
{
return $this->electricityDistributor;
}
/**
* @param Distributor|null $electricityDistributor
* @return Gauge
*/
public function setElectricityDistributor(?Distributor $electricityDistributor): Gauge
{
$this->electricityDistributor = $electricityDistributor;
return $this;
}
/**
* @return string|null
*/
public function getCustomerName() :?string
{
if ($this->properties->getCustomerName() === null) {
return $this->building->getOwner();
}
return $this->properties->getCustomerName();
}
/**
* @return GaugePurpose|null
*/
public function getConsumptionPurpose(): ?GaugePurpose
{
foreach ($this->purposes as $gPurpose) {
if ($gPurpose->getPurpose() === GaugePurpose::PURPOSE_CONSUMPTION) {
return $gPurpose;
}
}
return null;
}
/**
* @return bool|null
*/
public function getShowInOutput(): ?bool
{
return $this->showInOutput;
}
/**
* @param bool|null $showInOutput
* @return self
*/
public function setShowInOutput(?bool $showInOutput): self
{
$this->showInOutput = $showInOutput;
return $this;
}
public function getImport(): ?DataImport
{
return $this->import;
}
public function setImport(?DataImport $import): Gauge
{
$this->import = $import;
return $this;
}
/**
* @return int
*/
public function getConsumptionInResultsRatio(): int
{
$inResultsSettings = $this->getDataInResults();
if ($inResultsSettings === Dataset::CONSUMPTION_IN_RESULTS_OFF) {
return 0;
}
if ($inResultsSettings === Dataset::CONSUMPTION_IN_RESULTS_TAKE_OFF) {
return - 1;
}
return 1;
}
/**
* @return int
*/
public function getCostInResultsRatio(): int
{
$costInResults = $this->getProperties()->getCostsInResults();
if ($costInResults === Dataset::COSTS_IN_RESULTS_OFF) {
return 0;
}
if ($costInResults === Dataset::COSTS_IN_RESULTS_TAKE_OFF) {
return - 1;
}
return 1;
}
/**
* @return bool
*/
public function hasParentFromSameBuilding():bool
{
if ($this->getParent() === null) {
return false;
}
return $this->getParent()->getBuilding()->getId() === $this->getBuilding()->getId();
}
/**
* @param bool|null $client
* @return string
*/
public function getNameWithBuilding(?bool $client = null): string
{
if ($client === true) {
return sprintf('%s - %s', $this->getBuilding()->getClient()->getName(), $this->getNameWithBuilding());
}
return sprintf('%s - %s', $this->getBuilding()->getName(), $this->getName());
}
/**
* @return int|string|null
*/
public function getTdd()
{
if ($this->isElectricityMeter()) {
return $this->getProperties()->getTddEl();
}
if ($this->isGasometter()) {
return $this->getProperties()->getTdd();
}
return null;
}
/**
* method returns all allowed segments by all gauge purposes
* @return void
*/
public function getSegments(): array
{
$segments = [];
foreach ($this->purposes as $gPurpose) {
$segments = array_merge($segments, $gPurpose->getSegments());
}
return $segments;
}
public function getDataInResults(): ?int
{
return $this->dataInResults;
}
public function setDataInResults(?int $dataInResults): Gauge
{
$this->dataInResults = $dataInResults;
return $this;
}
public function hasConsumptionUsage(int $usage): bool
{
$consumptionPurpose = $this->getConsumptionPurpose();
if ($consumptionPurpose === null) {
return false;
}
foreach ($consumptionPurpose->getConsumptionUsage() as $cUsageObj) {
if ($cUsageObj->getConsumptionUsage() === $usage) {
return true;
}
}
return false;
}
/**
* @return ConsumptionLimit[]|Collection
*/
public function getConsumptionLimits()
{
return $this->consumptionLimits;
}
/**
* @param ConsumptionLimit[]|Collection $consumptionLimits
* @return Gauge
*/
public function setConsumptionLimits($consumptionLimits)
{
$this->consumptionLimits = $consumptionLimits;
return $this;
}
/**
* @param ConsumptionLimit $gaugeLimitObj
* @return ConsumptionLimit|null
*/
public function getOtherLimitControl(ConsumptionLimit $gaugeLimitObj): ?ConsumptionLimit
{
$otherControls = $this->consumptionLimits->filter(
fn (ConsumptionLimit $limitItem) => $limitItem->getId() !== $gaugeLimitObj->getId()
);
return $otherControls->count() > 0 ? $otherControls->first() : null;
}
public function getLastSentNotificationAt(): ?\DateTime
{
return $this->lastSentNotificationAt;
}
public function setLastSentNotificationAt(?\DateTime $lastSentNotificationAt): Gauge
{
$this->lastSentNotificationAt = $lastSentNotificationAt;
return $this;
}
public function isRemoteAsPrimary(): bool
{
return $this->remoteAsPrimary;
}
public function setRemoteAsPrimary(bool $remoteAsPrimary): Gauge
{
$this->remoteAsPrimary = $remoteAsPrimary;
return $this;
}
public function getPrimaryMode(): int
{
if ($this->remote && $this->isRemoteAsPrimary()) {
return GaugeType::MODE_REMOTE;
}
return GaugeType::MODE_MANUAL;
}
public function getSecondaryMode(): ?int
{
if (!$this->remote) {
return null;
}
if (!$this->isRemoteAsPrimary()) {
return GaugeType::MODE_REMOTE;
}
return GaugeType::MODE_MANUAL;
}
public function isRemoteOnly(): bool
{
$fullRemote = $this->remote && $this->getPrimaryMode() === GaugeType::MODE_REMOTE;
$noManualReadingsPeriod = true;
foreach ($this->purposes as $purpose) {
if ($purpose->getReadingsPeriod() !== Period::NONE) {
$noManualReadingsPeriod = false;
}
}
return $fullRemote && $noManualReadingsPeriod;
}
public function getPurposesForManualReadings()
{
$allowedPurposes = $this->getAllowedPurposesByMeasurementTarget();
return $this->purposes->filter(function (GaugePurpose $gp) use ($allowedPurposes) {
if (!empty($allowedPurposes) && !in_array($gp->getPurpose(), $allowedPurposes, true)) {
return false;
}
return $this->isPurposeManualEnabled($gp);
});
}
public function getPurposesForRemoteReadings()
{
$allowedPurposes = $this->getAllowedPurposesByMeasurementTarget();
return $this->purposes->filter(function (GaugePurpose $gp) use ($allowedPurposes) {
if (!empty($allowedPurposes) && !in_array($gp->getPurpose(), $allowedPurposes, true)) {
return false;
}
return $this->isPurposeRemoteEnabled($gp);
});
}
/**
* Returns purposes configured for remote readings only.
* Unlike getPurposesForRemoteReadings(), this is not filtered by measurement target.
*/
public function getPurposesForConfiguredRemoteReadings()
{
return $this->purposes->filter(function (GaugePurpose $gp) {
return $this->isPurposeRemoteEnabled($gp);
});
}
private function getAllowedPurposesByMeasurementTarget(): array
{
$measurementTarget = $this->getMeasurementTarget();
$map = GaugeType::getPurposesByMeasurementTarget();
if ($measurementTarget === null) {
return [];
}
return $map[$measurementTarget] ?? [];
}
private function isPurposeManualEnabled(GaugePurpose $gp): bool
{
$readingsPeriod = $gp->getReadingsPeriod();
if ($readingsPeriod === null || $readingsPeriod === Period::NONE) {
return false;
}
return $this->getPrimaryMode() === GaugeType::MODE_MANUAL
|| $this->getSecondaryMode() === GaugeType::MODE_MANUAL;
}
private function isPurposeRemoteEnabled(GaugePurpose $gp): bool
{
if (!$this->isRemote()) {
return false;
}
if ($this->getPrimaryMode() !== GaugeType::MODE_REMOTE && $this->getSecondaryMode() !== GaugeType::MODE_REMOTE) {
return false;
}
if ($gp->getRemoteReadingsMinuteFrequency() !== null || $gp->getRemoteReadingsPeriod() !== null) {
return true;
}
return $gp->getRemoteUnits()->count() > 0;
}
public function getContracts(): array|Collection|null
{
return $this->contracts;
}
/**
* @param Contract[]|Collection $contracts
* @return Gauge
*/
public function setContracts($contracts): Gauge
{
$this->contracts = $contracts;
return $this;
}
public function addContract(Contract $contract): Gauge
{
if ($this->contracts === null) {
$this->contracts = new ArrayCollection();
}
if (!$this->contracts->contains($contract)) {
$this->contracts->add($contract);
}
return $this;
}
public function removeContract(Contract $contract): Gauge
{
if ($this->contracts === null) {
$this->contracts = new ArrayCollection();
}
if ($this->contracts->contains($contract)) {
$this->contracts->removeElement($contract);
$contract->removeGauge($this);
}
return $this;
}
}