src/Entity/Gauge/Gauge.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Gauge;
  3. use App\Entity\Building\Building;
  4. use App\Entity\Building\Sector;
  5. use App\Entity\ContractPrice\Contract;
  6. use App\Entity\ContractPrice\ContractItemType;
  7. use App\Entity\Event\Event;
  8. use App\Entity\Extension\BlameableTrait;
  9. use App\Entity\Extension\DisabledReasonTrait;
  10. use App\Entity\Extension\DisabledTrait;
  11. use App\Entity\Extension\HistoricalInterface;
  12. use App\Entity\Extension\ItemsWithHistoryTrait;
  13. use App\Entity\Extension\TimestampableTrait;
  14. use App\Entity\File;
  15. use App\Entity\Import\DataImport;
  16. use App\Entity\PriceDecision\Distributor;
  17. use App\Repository\Gauge\GaugeRepository;
  18. use App\Utils\Dataset;
  19. use App\Utils\GaugeType;
  20. use App\Utils\Period;
  21. use App\Utils\Units;
  22. use Doctrine\Common\Collections\ArrayCollection;
  23. use Doctrine\Common\Collections\Collection;
  24. use Doctrine\ORM\Mapping as ORM;
  25. /**
  26. * @ORM\Entity(repositoryClass=GaugeRepository::class)
  27. * @ORM\Table(name="gauges")
  28. */
  29. class Gauge
  30. {
  31. /**
  32. * @var int|null
  33. * @ORM\Id()
  34. * @ORM\GeneratedValue()
  35. * @ORM\Column(type="integer")
  36. */
  37. private ?int $id = null;
  38. /**
  39. * values are defined above in class definition
  40. * @var int
  41. * @ORM\Column(type="smallint", nullable=false)
  42. */
  43. private int $level;
  44. /**
  45. * values are defined in App\Utils\GaugeType::TYPE_...
  46. * @var int
  47. * @ORM\Column(type="smallint", nullable=false)
  48. */
  49. private int $type;
  50. /**
  51. * values are defined in App\Utils\GaugeType::SOURCE...
  52. * @var int|null
  53. * @ORM\Column(type="smallint", nullable=true)
  54. */
  55. private ?int $primarySource = null;
  56. /**
  57. * @var string|null
  58. * @ORM\Column(type="string", nullable=true)
  59. */
  60. private ?string $primarySourceOther = null;
  61. /**
  62. * @var Building
  63. * @ORM\ManyToOne(targetEntity="App\Entity\Building\Building", inversedBy="gauges")
  64. * @ORM\JoinColumn(name="building_id", referencedColumnName="id", nullable=false)
  65. */
  66. private Building $building;
  67. /**
  68. * @var Collection|Gauge[]
  69. * @ORM\OneToMany(targetEntity="App\Entity\Gauge\Gauge", mappedBy="parent", cascade={"persist"})
  70. */
  71. protected $children;
  72. /**
  73. * @var Collection|File[]
  74. *
  75. * @ORM\ManyToMany(targetEntity="App\Entity\File", mappedBy="gauges")
  76. */
  77. private $files;
  78. /**
  79. * @var Gauge|null
  80. * @ORM\ManyToOne(targetEntity="App\Entity\Gauge\Gauge", inversedBy="children")
  81. * @ORM\JoinColumn(name="parent", referencedColumnName="id", nullable=true)
  82. */
  83. protected ?Gauge $parent = null;
  84. /**
  85. * @var string
  86. * @ORM\Column(type="string", nullable=false)
  87. */
  88. private string $name = '';
  89. /**
  90. * Path to generated QR code image linking to gauge readings
  91. * @var string|null
  92. * @ORM\Column(type="string", name="qr_code_path", nullable=true)
  93. */
  94. private ?string $qrCodePath = null;
  95. /**
  96. * values are defined in App\Utils\Dataset::MEASUREMENT_PARAM...
  97. * @var int
  98. * @ORM\Column(type="smallint", nullable=false)
  99. */
  100. private int $measurementTarget = GaugeType::MEASUREMENT_TARGET_CONSUMPTION;
  101. /**
  102. * indicator if is allowed remote readings
  103. * @var bool
  104. * @ORM\Column(type="boolean")
  105. */
  106. private bool $remote = false;
  107. /**
  108. * @var bool
  109. * @ORM\Column(type="boolean", nullable=false, options={"default" : 0})
  110. */
  111. private bool $remoteAsPrimary = false;
  112. /**
  113. * Php stan exception: this is right entity mapping by documentation. In constructor is solved not-nullable properties.
  114. * https://github.com/phpstan/phpstan-doctrine/issues/97
  115. *
  116. * @ORM\OneToOne(targetEntity="App\Entity\Gauge\GaugeProperties", mappedBy="gauge", cascade={"all"})
  117. */
  118. private GaugeProperties $properties; // @phpstan-ignore-line
  119. /**
  120. * @var Collection|GaugeRate[]
  121. * @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugeRate", mappedBy="gauge", cascade={"all"})
  122. */
  123. private $rates;
  124. /**
  125. * @var Collection|ShareOfGauges[]
  126. * @ORM\OneToMany(targetEntity="App\Entity\Gauge\ShareOfGauges", mappedBy="gauge", cascade={"all"})
  127. */
  128. private $shares;
  129. /**
  130. * @var Collection|GaugePhaseAndBreaker[]
  131. * @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugePhaseAndBreaker", mappedBy="gauge", cascade={"all"})
  132. */
  133. private $phaseAndBreaker;
  134. /**
  135. * @var Collection|GaugeHeatingPower[]
  136. * @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugeHeatingPower", mappedBy="gauge", cascade={"all"})
  137. */
  138. private $heatingPower;
  139. /**
  140. * @var Collection|GaugeUnitPrice[]
  141. * @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugeUnitPrice", mappedBy="gauge", cascade={"all"})
  142. * @ORM\OrderBy({"validFrom" = "ASC"})
  143. */
  144. private $unitPrice;
  145. /**
  146. * @var Collection|GaugePriceSupplyPoint[]
  147. * @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugePriceSupplyPoint", mappedBy="gauge", cascade={"all"})
  148. */
  149. private $priceSupplyPoint;
  150. /**
  151. * @var GaugeRemoteProperties|null
  152. * @ORM\OneToOne(targetEntity="App\Entity\Gauge\GaugeRemoteProperties", mappedBy="gauge", cascade={"all"})
  153. */
  154. private ?GaugeRemoteProperties $remoteProperties;
  155. /**
  156. * @var File|null
  157. * @ORM\ManyToOne(targetEntity="App\Entity\File", inversedBy="gaugePhotos")
  158. * @ORM\JoinColumn(nullable=true)
  159. */
  160. private ?File $photo = null;
  161. /**
  162. * @var Distributor|null
  163. * @ORM\ManyToOne(targetEntity="App\Entity\PriceDecision\Distributor")
  164. * @ORM\JoinColumn(name="el_dictributor_id", referencedColumnName="id", nullable=true)
  165. */
  166. private ?Distributor $electricityDistributor = null;
  167. /**
  168. * @var Collection|GaugePurpose[]
  169. * @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugePurpose", mappedBy="gauge", cascade={"all"})
  170. */
  171. private $purposes;
  172. /**
  173. * @var Collection|GaugeReading[]
  174. *
  175. * @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugeReading", mappedBy="gauge")
  176. */
  177. private $readings;
  178. /**
  179. * @var Collection|Invoice[]
  180. *
  181. * @ORM\OneToMany(targetEntity="App\Entity\Gauge\Invoice", mappedBy="gauge")
  182. */
  183. private $invoices;
  184. /**
  185. * @var Collection|Event[]
  186. *
  187. * @ORM\ManyToMany(targetEntity="App\Entity\Event\Event", mappedBy="gauges", cascade={"persist"})
  188. */
  189. private $events;
  190. /**
  191. * @var Collection|GaugeInvoiceUnit[]
  192. * @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugeInvoiceUnit", mappedBy="gauge", cascade={"all"})
  193. */
  194. private $invoiceUnits;
  195. /**
  196. * this attribute sets if data can be showed in output after gauge is disabled
  197. * @var \bool
  198. *
  199. * @ORM\Column(name="show_in_output", type="boolean", nullable=false, options={"default": 1})
  200. */
  201. protected bool $showInOutput = true;
  202. /**
  203. * values are defined in App\Utils\Dataset::CONSUMPTION_IN...
  204. * @var int|null
  205. * @ORM\Column(type="smallint", nullable=true)
  206. */
  207. private ?int $dataInResults = Dataset::CONSUMPTION_IN_RESULTS_ON;
  208. /**
  209. * @var Collection|ConsumptionLimit[]
  210. * @ORM\ManyToMany(targetEntity="App\Entity\Gauge\ConsumptionLimit", mappedBy="gauges", cascade={"persist"})
  211. */
  212. private $consumptionLimits;
  213. /**
  214. * @var \DateTime|null
  215. *
  216. * @ORM\Column(type="datetime", nullable=true)
  217. */
  218. private ?\DateTime $lastSentNotificationAt;
  219. /**
  220. * @var DataImport|null
  221. * @ORM\ManyToOne(targetEntity="App\Entity\Import\DataImport", inversedBy="gauges")
  222. * @ORM\JoinColumn(nullable=true)
  223. */
  224. private ?DataImport $import = null;
  225. /**
  226. * @var Collection|Contract[]|null
  227. * @ORM\ManyToMany(targetEntity=Contract::class, mappedBy="gauges")
  228. */
  229. private $contracts;
  230. use BlameableTrait;
  231. use TimestampableTrait;
  232. use ItemsWithHistoryTrait;
  233. use DisabledTrait;
  234. use DisabledReasonTrait;
  235. public function __construct()
  236. {
  237. $this->type = GaugeType::TYPE_ELECTRICITY_METER;
  238. $this->level = GaugeType::LEVEL_MAIN;
  239. $this->setProperties(new GaugeProperties($this));
  240. $this->remoteProperties = null;
  241. $this->children = new ArrayCollection();
  242. $this->files = new ArrayCollection();
  243. $this->rates = new ArrayCollection();
  244. $this->phaseAndBreaker = new ArrayCollection();
  245. $this->heatingPower = new ArrayCollection();
  246. $this->unitPrice = new ArrayCollection();
  247. $this->priceSupplyPoint = new ArrayCollection();
  248. $this->readings = new ArrayCollection();
  249. $this->purposes = new ArrayCollection();
  250. $this->invoices = new ArrayCollection();
  251. $this->invoiceUnits = new ArrayCollection();
  252. $this->contracts = new ArrayCollection();
  253. }
  254. /**
  255. * @return int|null
  256. */
  257. public function getId(): ?int
  258. {
  259. return $this->id;
  260. }
  261. /**
  262. * @param int|null $id
  263. * @return Gauge
  264. */
  265. public function setId(?int $id): Gauge
  266. {
  267. $this->id = $id;
  268. return $this;
  269. }
  270. /**
  271. * @return int
  272. */
  273. public function getLevel(): int
  274. {
  275. return $this->level;
  276. }
  277. /**
  278. * @param int $level
  279. * @return Gauge
  280. */
  281. public function setLevel(int $level): Gauge
  282. {
  283. $this->level = $level;
  284. return $this;
  285. }
  286. /**
  287. * @return int
  288. */
  289. public function getType(): int
  290. {
  291. return $this->type;
  292. }
  293. /**
  294. * @param int $type
  295. * @return Gauge
  296. */
  297. public function setType(int $type): Gauge
  298. {
  299. $this->type = $type;
  300. return $this;
  301. }
  302. /**
  303. * @return Building
  304. */
  305. public function getBuilding(): Building
  306. {
  307. return $this->building;
  308. }
  309. /**
  310. * @param Building $building
  311. * @return Gauge
  312. */
  313. public function setBuilding(Building $building): Gauge
  314. {
  315. $this->building = $building;
  316. return $this;
  317. }
  318. /**
  319. * @return Gauge[]|Collection
  320. */
  321. public function getChildren()
  322. {
  323. return $this->children;
  324. }
  325. /**
  326. * @return Gauge|null
  327. */
  328. public function getParent(): ?Gauge
  329. {
  330. return $this->parent;
  331. }
  332. /**
  333. * @param Gauge|null $parent
  334. * @return Gauge
  335. */
  336. public function setParent(?Gauge $parent): Gauge
  337. {
  338. $this->parent = $parent;
  339. return $this;
  340. }
  341. /**
  342. * @return string
  343. */
  344. public function getName(): string
  345. {
  346. return $this->name;
  347. }
  348. /**
  349. * @param string $name
  350. * @return Gauge
  351. */
  352. public function setName(string $name): Gauge
  353. {
  354. $this->name = $name;
  355. return $this;
  356. }
  357. /**
  358. * @return string|null
  359. */
  360. public function getQrCodePath(): ?string
  361. {
  362. return $this->qrCodePath;
  363. }
  364. /**
  365. * @param string|null $qrCodePath
  366. * @return $this
  367. */
  368. public function setQrCodePath(?string $qrCodePath): self
  369. {
  370. $this->qrCodePath = $qrCodePath;
  371. return $this;
  372. }
  373. /**
  374. * @return GaugeProperties
  375. */
  376. public function getProperties(): GaugeProperties
  377. {
  378. return $this->properties;
  379. }
  380. /**
  381. * @param GaugeProperties $properties
  382. * @return Gauge
  383. */
  384. public function setProperties(GaugeProperties $properties): Gauge
  385. {
  386. $this->properties = $properties;
  387. return $this;
  388. }
  389. /**
  390. * @return Collection|File[]
  391. */
  392. public function getFiles(): Collection
  393. {
  394. return $this->files;
  395. }
  396. /**
  397. * @param File $file
  398. * @return $this
  399. */
  400. public function addFile(File $file): self
  401. {
  402. if (!$this->files->contains($file)) {
  403. $file->addGauge($this);
  404. $this->files->add($file);
  405. }
  406. return $this;
  407. }
  408. /**
  409. * @return int|null
  410. */
  411. public function getPrimarySource(): ?int
  412. {
  413. return $this->primarySource;
  414. }
  415. /**
  416. * @param int|null $primarySource
  417. * @return Gauge
  418. */
  419. public function setPrimarySource(?int $primarySource): Gauge
  420. {
  421. $this->primarySource = $primarySource;
  422. return $this;
  423. }
  424. /**
  425. * @return string|null
  426. */
  427. public function getPrimarySourceOther(): ?string
  428. {
  429. return $this->primarySourceOther;
  430. }
  431. /**
  432. * @param string|null $primarySourceOther
  433. * @return Gauge
  434. */
  435. public function setPrimarySourceOther(?string $primarySourceOther): Gauge
  436. {
  437. $this->primarySourceOther = $primarySourceOther;
  438. return $this;
  439. }
  440. /**
  441. * @param File $file
  442. * @return $this
  443. */
  444. public function removeFile(File $file): self
  445. {
  446. if ($this->files->contains($file)) {
  447. $this->files->removeElement($file);
  448. $file->removeGauge($this);
  449. }
  450. return $this;
  451. }
  452. /**
  453. * kontrola zda je měřidlo podružné
  454. * @return bool
  455. */
  456. public function isSecondary(): bool
  457. {
  458. if ($this->getLevel() === GaugeType::LEVEL_SECONDARY && $this->getParent() !== null) {
  459. return true;
  460. }
  461. return false;
  462. }
  463. /**
  464. * kontrola zda je měřidlo hlavní
  465. * @return bool
  466. */
  467. public function isMain(): bool
  468. {
  469. return $this->getLevel() === GaugeType::LEVEL_MAIN;
  470. }
  471. /**
  472. * @return string
  473. */
  474. public function __toString(): string
  475. {
  476. return $this->getName();
  477. }
  478. /**
  479. * @return File|null
  480. */
  481. public function getPhoto(): ?File
  482. {
  483. return $this->photo;
  484. }
  485. /**
  486. * @param File|null $photo
  487. * @return Gauge
  488. */
  489. public function setPhoto(?File $photo): Gauge
  490. {
  491. $this->photo = $photo;
  492. if ($photo !== null) {
  493. $photo->addGauge($this);
  494. }
  495. return $this;
  496. }
  497. /**
  498. * @return GaugeRate[]|Collection
  499. */
  500. public function getRates()
  501. {
  502. return $this->rates;
  503. }
  504. /**
  505. * @param GaugeRate[]|Collection $rates
  506. * @return Gauge
  507. */
  508. public function setRates($rates)
  509. {
  510. $this->rates = $rates;
  511. return $this;
  512. }
  513. /**
  514. * @param GaugeRate $rate
  515. * @return Gauge
  516. */
  517. public function addRate(GaugeRate $rate): Gauge
  518. {
  519. $rate->setGauge($this);
  520. $this->rates->add($rate);
  521. return $this;
  522. }
  523. /**
  524. * @return GaugePhaseAndBreaker[]|Collection
  525. */
  526. public function getPhaseAndBreaker(): Collection
  527. {
  528. return $this->phaseAndBreaker;
  529. }
  530. /**
  531. * @param GaugePhaseAndBreaker[]|Collection $phaseAndBreaker
  532. * @return Gauge
  533. */
  534. public function setPhaseAndBreaker($phaseAndBreaker): Gauge
  535. {
  536. $this->phaseAndBreaker = $phaseAndBreaker;
  537. return $this;
  538. }
  539. /**
  540. * @param GaugePhaseAndBreaker $phaseAndBreaker
  541. * @return Gauge
  542. */
  543. public function addPhaseAndBreaker(GaugePhaseAndBreaker $phaseAndBreaker): Gauge
  544. {
  545. $phaseAndBreaker->setGauge($this);
  546. $this->phaseAndBreaker->add($phaseAndBreaker);
  547. return $this;
  548. }
  549. /**
  550. * @return GaugeHeatingPower[]|Collection
  551. */
  552. public function getHeatingPower()
  553. {
  554. return $this->heatingPower;
  555. }
  556. /**
  557. * @param GaugeHeatingPower[]|Collection $heatingPower
  558. * @return Gauge
  559. */
  560. public function setHeatingPower($heatingPower): Gauge
  561. {
  562. $this->heatingPower = $heatingPower;
  563. return $this;
  564. }
  565. /**
  566. * @param GaugeHeatingPower $heatingPower
  567. * @return Gauge
  568. */
  569. public function addHeatingPower(GaugeHeatingPower $heatingPower): Gauge
  570. {
  571. $heatingPower->setGauge($this);
  572. $this->heatingPower->add($heatingPower);
  573. return $this;
  574. }
  575. /**
  576. * @return GaugeUnitPrice[]|Collection
  577. */
  578. public function getUnitPrice()
  579. {
  580. return $this->unitPrice;
  581. }
  582. /**
  583. * @param GaugeUnitPrice[]|Collection $unitPrice
  584. * @return Gauge
  585. */
  586. public function setUnitPrice($unitPrice): Gauge
  587. {
  588. $this->unitPrice = $unitPrice;
  589. return $this;
  590. }
  591. /**
  592. * @param GaugeUnitPrice $unitPrice
  593. * @return $this
  594. */
  595. public function addUnitPrice(GaugeUnitPrice $unitPrice): Gauge
  596. {
  597. $unitPrice->setGauge($this);
  598. $this->unitPrice->add($unitPrice);
  599. return $this;
  600. }
  601. /**
  602. * @return GaugePriceSupplyPoint[]|Collection
  603. */
  604. public function getPriceSupplyPoint(): Collection
  605. {
  606. return $this->priceSupplyPoint;
  607. }
  608. /**
  609. * @param GaugePriceSupplyPoint[]|Collection $priceSupplyPoint
  610. * @return Gauge
  611. */
  612. public function setPriceSupplyPoint($priceSupplyPoint): Gauge
  613. {
  614. $this->priceSupplyPoint = $priceSupplyPoint;
  615. return $this;
  616. }
  617. /**
  618. * @param GaugePriceSupplyPoint $priceSupplyPoint
  619. * @return $this
  620. */
  621. public function addPriceSupplyPoint(GaugePriceSupplyPoint $priceSupplyPoint): Gauge
  622. {
  623. $priceSupplyPoint->setGauge($this);
  624. $this->priceSupplyPoint->add($priceSupplyPoint);
  625. return $this;
  626. }
  627. /**
  628. * @return GaugeRemoteProperties|null
  629. */
  630. public function getRemoteProperties(): ?GaugeRemoteProperties
  631. {
  632. return $this->remoteProperties;
  633. }
  634. /**
  635. * @param GaugeRemoteProperties|null $remoteProperties
  636. * @return Gauge
  637. */
  638. public function setRemoteProperties(?GaugeRemoteProperties $remoteProperties): Gauge
  639. {
  640. $this->remoteProperties = $remoteProperties;
  641. return $this;
  642. }
  643. /**
  644. * @param int $mediumType
  645. * @param \DateTime|null $date
  646. * @return HistoricalInterface|GaugeUnitPrice|mixed|null
  647. */
  648. public function getCurrentUnitPrice(int $mediumType, ?\DateTime $date = null)
  649. {
  650. if ($date === null) {
  651. $date = new \DateTime();
  652. }
  653. $current = null;
  654. foreach ($this->getUnitPrice() as $item) {
  655. if ($item->getValidFrom() <= $date
  656. && $mediumType === $item->getMedium()
  657. && (
  658. !$current instanceof GaugeUnitPrice
  659. || $item->getValidFrom() > $current->getValidFrom()
  660. )
  661. ) {
  662. $current = $item;
  663. }
  664. }
  665. return $current;
  666. }
  667. /**
  668. * @return bool
  669. */
  670. public function isRemote(): bool
  671. {
  672. return $this->remote;
  673. }
  674. /**
  675. * @param bool $remote
  676. * @return Gauge
  677. */
  678. public function setRemote(bool $remote): Gauge
  679. {
  680. $this->remote = $remote;
  681. return $this;
  682. }
  683. /**
  684. * @return GaugeReading[]|Collection
  685. */
  686. public function getReadings()
  687. {
  688. return $this->readings;
  689. }
  690. /**
  691. * @return bool
  692. */
  693. public function isElectricityMeter(): bool
  694. {
  695. return $this->isType(GaugeType::TYPE_ELECTRICITY_METER);
  696. }
  697. /**
  698. * @return bool
  699. */
  700. public function isGasometter(): bool
  701. {
  702. return $this->isType(GaugeType::TYPE_GASOMETER);
  703. }
  704. /**
  705. * @param int $type
  706. * @return bool
  707. */
  708. public function isType(int $type): bool
  709. {
  710. return $this->getType() === $type;
  711. }
  712. /**
  713. * @return bool
  714. */
  715. public function canHaveReadings(): bool
  716. {
  717. return $this->getLevel() !== GaugeType::LEVEL_FICTIVE && $this->getType() !== GaugeType::TYPE_OTHER_FUELS;
  718. }
  719. /**
  720. * @return bool
  721. */
  722. public function canHaveInvoices(): bool
  723. {
  724. return true;
  725. }
  726. public function hasProduction(): bool
  727. {
  728. return in_array($this->getMeasurementTarget(), [GaugeType::MEASUREMENT_TARGET_PRODUCTION, GaugeType::MEASUREMENT_TARGET_CONSUMPTION_DELIVERY], true);
  729. }
  730. /**
  731. * @return GaugePurpose[]|Collection
  732. */
  733. public function getPurposes(): Collection
  734. {
  735. return $this->purposes;
  736. }
  737. /**
  738. * @param GaugePurpose[]|Collection $purposes
  739. * @return Gauge
  740. */
  741. public function setPurposes($purposes): Gauge
  742. {
  743. $this->purposes = $purposes;
  744. return $this;
  745. }
  746. /**
  747. * @param GaugePurpose $gaugePurpose
  748. * @return Gauge
  749. */
  750. public function addPurpose(GaugePurpose $gaugePurpose): Gauge
  751. {
  752. if (!$this->purposes->contains($gaugePurpose)) {
  753. $gaugePurpose->setGauge($this);
  754. $this->purposes->add($gaugePurpose);
  755. }
  756. return $this;
  757. }
  758. /**
  759. * @return int
  760. */
  761. public function getMeasurementTarget(): int
  762. {
  763. return $this->measurementTarget;
  764. }
  765. /**
  766. * @param int $measurementTarget
  767. * @return Gauge
  768. */
  769. public function setMeasurementTarget(int $measurementTarget): Gauge
  770. {
  771. $this->measurementTarget = $measurementTarget;
  772. return $this;
  773. }
  774. /**
  775. * @param int $purposeConst
  776. * @return GaugePurpose|null
  777. */
  778. public function getPurposeObject(int $purposeConst): ?GaugePurpose
  779. {
  780. foreach ($this->getPurposes() as $gaugePurpose) {
  781. if ($gaugePurpose->getPurpose() === $purposeConst) {
  782. return $gaugePurpose;
  783. }
  784. }
  785. return null;
  786. }
  787. /**
  788. * @return GaugePurpose|null
  789. */
  790. public function getPurposeFirst(): ?GaugePurpose
  791. {
  792. if ($this->purposes->count() > 0) {
  793. return $this->purposes->first();
  794. }
  795. return null;
  796. }
  797. /**
  798. * @param int $purposeConst
  799. * @return GaugePurpose
  800. */
  801. public function getPurposeNewOrExistObject(int $purposeConst): GaugePurpose
  802. {
  803. $gaugePurpose = $this->getPurposeObject($purposeConst);
  804. if ($gaugePurpose === null) {
  805. $gaugePurpose = new GaugePurpose();
  806. $gaugePurpose->setPurpose($purposeConst);
  807. $gaugePurpose->setGauge($this);
  808. }
  809. return $gaugePurpose;
  810. }
  811. /**
  812. * @return Invoice[]|Collection
  813. */
  814. public function getInvoices(): Collection
  815. {
  816. return $this->invoices;
  817. }
  818. /**
  819. * @param Invoice[]|Collection $invoices
  820. * @return Gauge
  821. */
  822. public function setInvoices($invoices): Gauge
  823. {
  824. $this->invoices = $invoices;
  825. return $this;
  826. }
  827. /**
  828. * @param Invoice $invoice
  829. * @return Gauge
  830. */
  831. public function addInvoice(Invoice $invoice): Gauge
  832. {
  833. $this->invoices[] = $invoice;
  834. return $this;
  835. }
  836. /**
  837. * @param \DateTime|null $date
  838. * @return int
  839. */
  840. public function getInvoiceUnit(?\DateTime $date = null): int
  841. {
  842. $invUnitObj = $this->getCurrent('invoiceUnits', $date, $this->getInvoiceUnits());
  843. if ($invUnitObj instanceof GaugeInvoiceUnit) {
  844. return $invUnitObj->getUnit();
  845. }
  846. return Units::getDefaultUnitByGaugeType($this->getType());
  847. }
  848. /**
  849. * @return Event[]|Collection
  850. */
  851. public function getEvents()
  852. {
  853. return $this->events;
  854. }
  855. /**
  856. * @param Event[]|Collection $events
  857. * @return Gauge
  858. */
  859. public function setEvents($events): Gauge
  860. {
  861. $this->events = $events;
  862. return $this;
  863. }
  864. public function getSector(): Sector
  865. {
  866. return $this->getBuilding()->getSector();
  867. }
  868. /**
  869. * @return ShareOfGauges[]|Collection
  870. */
  871. public function getShares()
  872. {
  873. return $this->shares;
  874. }
  875. /**
  876. * @param ShareOfGauges[]|Collection $shares
  877. * @return Gauge
  878. */
  879. public function setShares($shares): Gauge
  880. {
  881. $this->shares = $shares;
  882. return $this;
  883. }
  884. /**
  885. * @return array
  886. */
  887. public function toArray(): array
  888. {
  889. return [
  890. 'id' => $this->getId(),
  891. 'name' => $this->getName()
  892. ];
  893. }
  894. /**
  895. * @return bool
  896. */
  897. public function isElectricityWholesale():bool
  898. {
  899. if (
  900. $this->getType() === GaugeType::TYPE_ELECTRICITY_METER &&
  901. in_array($this->getProperties()->getConsumptionType(), [Dataset::CONSUMPTION_TYPE_WHOLESALE_VN, Dataset::CONSUMPTION_TYPE_WHOLESALE_VVN], true)
  902. ) {
  903. return true;
  904. }
  905. return false;
  906. }
  907. /**
  908. * @return bool
  909. */
  910. public function isForceLowTariff():bool
  911. {
  912. return $this->getProperties()->getForceLowTariff() === true;
  913. }
  914. /**
  915. * @return bool
  916. */
  917. public function isForceSpecialTariff():bool
  918. {
  919. return $this->getProperties()->getForceSpecialTariff() === true;
  920. }
  921. /**
  922. * @return GaugeInvoiceUnit[]|Collection
  923. */
  924. public function getInvoiceUnits(): Collection
  925. {
  926. return $this->invoiceUnits;
  927. }
  928. /**
  929. * @param GaugeInvoiceUnit $item
  930. * @return $this
  931. */
  932. public function addInvoiceUnit(GaugeInvoiceUnit $item): self
  933. {
  934. $item->setGauge($this);
  935. $this->invoiceUnits->add($item);
  936. return $this;
  937. }
  938. /**
  939. * @return Distributor|null
  940. */
  941. public function getElectricityDistributor(): ?Distributor
  942. {
  943. return $this->electricityDistributor;
  944. }
  945. /**
  946. * @param Distributor|null $electricityDistributor
  947. * @return Gauge
  948. */
  949. public function setElectricityDistributor(?Distributor $electricityDistributor): Gauge
  950. {
  951. $this->electricityDistributor = $electricityDistributor;
  952. return $this;
  953. }
  954. /**
  955. * @return string|null
  956. */
  957. public function getCustomerName() :?string
  958. {
  959. if ($this->properties->getCustomerName() === null) {
  960. return $this->building->getOwner();
  961. }
  962. return $this->properties->getCustomerName();
  963. }
  964. /**
  965. * @return GaugePurpose|null
  966. */
  967. public function getConsumptionPurpose(): ?GaugePurpose
  968. {
  969. foreach ($this->purposes as $gPurpose) {
  970. if ($gPurpose->getPurpose() === GaugePurpose::PURPOSE_CONSUMPTION) {
  971. return $gPurpose;
  972. }
  973. }
  974. return null;
  975. }
  976. /**
  977. * @return bool|null
  978. */
  979. public function getShowInOutput(): ?bool
  980. {
  981. return $this->showInOutput;
  982. }
  983. /**
  984. * @param bool|null $showInOutput
  985. * @return self
  986. */
  987. public function setShowInOutput(?bool $showInOutput): self
  988. {
  989. $this->showInOutput = $showInOutput;
  990. return $this;
  991. }
  992. public function getImport(): ?DataImport
  993. {
  994. return $this->import;
  995. }
  996. public function setImport(?DataImport $import): Gauge
  997. {
  998. $this->import = $import;
  999. return $this;
  1000. }
  1001. /**
  1002. * @return int
  1003. */
  1004. public function getConsumptionInResultsRatio(): int
  1005. {
  1006. $inResultsSettings = $this->getDataInResults();
  1007. if ($inResultsSettings === Dataset::CONSUMPTION_IN_RESULTS_OFF) {
  1008. return 0;
  1009. }
  1010. if ($inResultsSettings === Dataset::CONSUMPTION_IN_RESULTS_TAKE_OFF) {
  1011. return - 1;
  1012. }
  1013. return 1;
  1014. }
  1015. /**
  1016. * @return int
  1017. */
  1018. public function getCostInResultsRatio(): int
  1019. {
  1020. $costInResults = $this->getProperties()->getCostsInResults();
  1021. if ($costInResults === Dataset::COSTS_IN_RESULTS_OFF) {
  1022. return 0;
  1023. }
  1024. if ($costInResults === Dataset::COSTS_IN_RESULTS_TAKE_OFF) {
  1025. return - 1;
  1026. }
  1027. return 1;
  1028. }
  1029. /**
  1030. * @return bool
  1031. */
  1032. public function hasParentFromSameBuilding():bool
  1033. {
  1034. if ($this->getParent() === null) {
  1035. return false;
  1036. }
  1037. return $this->getParent()->getBuilding()->getId() === $this->getBuilding()->getId();
  1038. }
  1039. /**
  1040. * @param bool|null $client
  1041. * @return string
  1042. */
  1043. public function getNameWithBuilding(?bool $client = null): string
  1044. {
  1045. if ($client === true) {
  1046. return sprintf('%s - %s', $this->getBuilding()->getClient()->getName(), $this->getNameWithBuilding());
  1047. }
  1048. return sprintf('%s - %s', $this->getBuilding()->getName(), $this->getName());
  1049. }
  1050. /**
  1051. * @return int|string|null
  1052. */
  1053. public function getTdd()
  1054. {
  1055. if ($this->isElectricityMeter()) {
  1056. return $this->getProperties()->getTddEl();
  1057. }
  1058. if ($this->isGasometter()) {
  1059. return $this->getProperties()->getTdd();
  1060. }
  1061. return null;
  1062. }
  1063. /**
  1064. * method returns all allowed segments by all gauge purposes
  1065. * @return void
  1066. */
  1067. public function getSegments(): array
  1068. {
  1069. $segments = [];
  1070. foreach ($this->purposes as $gPurpose) {
  1071. $segments = array_merge($segments, $gPurpose->getSegments());
  1072. }
  1073. return $segments;
  1074. }
  1075. public function getDataInResults(): ?int
  1076. {
  1077. return $this->dataInResults;
  1078. }
  1079. public function setDataInResults(?int $dataInResults): Gauge
  1080. {
  1081. $this->dataInResults = $dataInResults;
  1082. return $this;
  1083. }
  1084. public function hasConsumptionUsage(int $usage): bool
  1085. {
  1086. $consumptionPurpose = $this->getConsumptionPurpose();
  1087. if ($consumptionPurpose === null) {
  1088. return false;
  1089. }
  1090. foreach ($consumptionPurpose->getConsumptionUsage() as $cUsageObj) {
  1091. if ($cUsageObj->getConsumptionUsage() === $usage) {
  1092. return true;
  1093. }
  1094. }
  1095. return false;
  1096. }
  1097. /**
  1098. * @return ConsumptionLimit[]|Collection
  1099. */
  1100. public function getConsumptionLimits()
  1101. {
  1102. return $this->consumptionLimits;
  1103. }
  1104. /**
  1105. * @param ConsumptionLimit[]|Collection $consumptionLimits
  1106. * @return Gauge
  1107. */
  1108. public function setConsumptionLimits($consumptionLimits)
  1109. {
  1110. $this->consumptionLimits = $consumptionLimits;
  1111. return $this;
  1112. }
  1113. /**
  1114. * @param ConsumptionLimit $gaugeLimitObj
  1115. * @return ConsumptionLimit|null
  1116. */
  1117. public function getOtherLimitControl(ConsumptionLimit $gaugeLimitObj): ?ConsumptionLimit
  1118. {
  1119. $otherControls = $this->consumptionLimits->filter(
  1120. fn (ConsumptionLimit $limitItem) => $limitItem->getId() !== $gaugeLimitObj->getId()
  1121. );
  1122. return $otherControls->count() > 0 ? $otherControls->first() : null;
  1123. }
  1124. public function getLastSentNotificationAt(): ?\DateTime
  1125. {
  1126. return $this->lastSentNotificationAt;
  1127. }
  1128. public function setLastSentNotificationAt(?\DateTime $lastSentNotificationAt): Gauge
  1129. {
  1130. $this->lastSentNotificationAt = $lastSentNotificationAt;
  1131. return $this;
  1132. }
  1133. public function isRemoteAsPrimary(): bool
  1134. {
  1135. return $this->remoteAsPrimary;
  1136. }
  1137. public function setRemoteAsPrimary(bool $remoteAsPrimary): Gauge
  1138. {
  1139. $this->remoteAsPrimary = $remoteAsPrimary;
  1140. return $this;
  1141. }
  1142. public function getPrimaryMode(): int
  1143. {
  1144. if ($this->remote && $this->isRemoteAsPrimary()) {
  1145. return GaugeType::MODE_REMOTE;
  1146. }
  1147. return GaugeType::MODE_MANUAL;
  1148. }
  1149. public function getSecondaryMode(): ?int
  1150. {
  1151. if (!$this->remote) {
  1152. return null;
  1153. }
  1154. if (!$this->isRemoteAsPrimary()) {
  1155. return GaugeType::MODE_REMOTE;
  1156. }
  1157. return GaugeType::MODE_MANUAL;
  1158. }
  1159. public function isRemoteOnly(): bool
  1160. {
  1161. $fullRemote = $this->remote && $this->getPrimaryMode() === GaugeType::MODE_REMOTE;
  1162. $noManualReadingsPeriod = true;
  1163. foreach ($this->purposes as $purpose) {
  1164. if ($purpose->getReadingsPeriod() !== Period::NONE) {
  1165. $noManualReadingsPeriod = false;
  1166. }
  1167. }
  1168. return $fullRemote && $noManualReadingsPeriod;
  1169. }
  1170. public function getPurposesForManualReadings()
  1171. {
  1172. $allowedPurposes = $this->getAllowedPurposesByMeasurementTarget();
  1173. return $this->purposes->filter(function (GaugePurpose $gp) use ($allowedPurposes) {
  1174. if (!empty($allowedPurposes) && !in_array($gp->getPurpose(), $allowedPurposes, true)) {
  1175. return false;
  1176. }
  1177. return $this->isPurposeManualEnabled($gp);
  1178. });
  1179. }
  1180. public function getPurposesForRemoteReadings()
  1181. {
  1182. $allowedPurposes = $this->getAllowedPurposesByMeasurementTarget();
  1183. return $this->purposes->filter(function (GaugePurpose $gp) use ($allowedPurposes) {
  1184. if (!empty($allowedPurposes) && !in_array($gp->getPurpose(), $allowedPurposes, true)) {
  1185. return false;
  1186. }
  1187. return $this->isPurposeRemoteEnabled($gp);
  1188. });
  1189. }
  1190. /**
  1191. * Returns purposes configured for remote readings only.
  1192. * Unlike getPurposesForRemoteReadings(), this is not filtered by measurement target.
  1193. */
  1194. public function getPurposesForConfiguredRemoteReadings()
  1195. {
  1196. return $this->purposes->filter(function (GaugePurpose $gp) {
  1197. return $this->isPurposeRemoteEnabled($gp);
  1198. });
  1199. }
  1200. private function getAllowedPurposesByMeasurementTarget(): array
  1201. {
  1202. $measurementTarget = $this->getMeasurementTarget();
  1203. $map = GaugeType::getPurposesByMeasurementTarget();
  1204. if ($measurementTarget === null) {
  1205. return [];
  1206. }
  1207. return $map[$measurementTarget] ?? [];
  1208. }
  1209. private function isPurposeManualEnabled(GaugePurpose $gp): bool
  1210. {
  1211. $readingsPeriod = $gp->getReadingsPeriod();
  1212. if ($readingsPeriod === null || $readingsPeriod === Period::NONE) {
  1213. return false;
  1214. }
  1215. return $this->getPrimaryMode() === GaugeType::MODE_MANUAL
  1216. || $this->getSecondaryMode() === GaugeType::MODE_MANUAL;
  1217. }
  1218. private function isPurposeRemoteEnabled(GaugePurpose $gp): bool
  1219. {
  1220. if (!$this->isRemote()) {
  1221. return false;
  1222. }
  1223. if ($this->getPrimaryMode() !== GaugeType::MODE_REMOTE && $this->getSecondaryMode() !== GaugeType::MODE_REMOTE) {
  1224. return false;
  1225. }
  1226. if ($gp->getRemoteReadingsMinuteFrequency() !== null || $gp->getRemoteReadingsPeriod() !== null) {
  1227. return true;
  1228. }
  1229. return $gp->getRemoteUnits()->count() > 0;
  1230. }
  1231. public function getContracts(): array|Collection|null
  1232. {
  1233. return $this->contracts;
  1234. }
  1235. /**
  1236. * @param Contract[]|Collection $contracts
  1237. * @return Gauge
  1238. */
  1239. public function setContracts($contracts): Gauge
  1240. {
  1241. $this->contracts = $contracts;
  1242. return $this;
  1243. }
  1244. public function addContract(Contract $contract): Gauge
  1245. {
  1246. if ($this->contracts === null) {
  1247. $this->contracts = new ArrayCollection();
  1248. }
  1249. if (!$this->contracts->contains($contract)) {
  1250. $this->contracts->add($contract);
  1251. }
  1252. return $this;
  1253. }
  1254. public function removeContract(Contract $contract): Gauge
  1255. {
  1256. if ($this->contracts === null) {
  1257. $this->contracts = new ArrayCollection();
  1258. }
  1259. if ($this->contracts->contains($contract)) {
  1260. $this->contracts->removeElement($contract);
  1261. $contract->removeGauge($this);
  1262. }
  1263. return $this;
  1264. }
  1265. }