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->invoiceUnits = new ArrayCollection();
  251. $this->contracts = new ArrayCollection();
  252. }
  253. /**
  254. * @return int|null
  255. */
  256. public function getId(): ?int
  257. {
  258. return $this->id;
  259. }
  260. /**
  261. * @param int|null $id
  262. * @return Gauge
  263. */
  264. public function setId(?int $id): Gauge
  265. {
  266. $this->id = $id;
  267. return $this;
  268. }
  269. /**
  270. * @return int
  271. */
  272. public function getLevel(): int
  273. {
  274. return $this->level;
  275. }
  276. /**
  277. * @param int $level
  278. * @return Gauge
  279. */
  280. public function setLevel(int $level): Gauge
  281. {
  282. $this->level = $level;
  283. return $this;
  284. }
  285. /**
  286. * @return int
  287. */
  288. public function getType(): int
  289. {
  290. return $this->type;
  291. }
  292. /**
  293. * @param int $type
  294. * @return Gauge
  295. */
  296. public function setType(int $type): Gauge
  297. {
  298. $this->type = $type;
  299. return $this;
  300. }
  301. /**
  302. * @return Building
  303. */
  304. public function getBuilding(): Building
  305. {
  306. return $this->building;
  307. }
  308. /**
  309. * @param Building $building
  310. * @return Gauge
  311. */
  312. public function setBuilding(Building $building): Gauge
  313. {
  314. $this->building = $building;
  315. return $this;
  316. }
  317. /**
  318. * @return Gauge[]|Collection
  319. */
  320. public function getChildren()
  321. {
  322. return $this->children;
  323. }
  324. /**
  325. * @return Gauge|null
  326. */
  327. public function getParent(): ?Gauge
  328. {
  329. return $this->parent;
  330. }
  331. /**
  332. * @param Gauge|null $parent
  333. * @return Gauge
  334. */
  335. public function setParent(?Gauge $parent): Gauge
  336. {
  337. $this->parent = $parent;
  338. return $this;
  339. }
  340. /**
  341. * @return string
  342. */
  343. public function getName(): string
  344. {
  345. return $this->name;
  346. }
  347. /**
  348. * @param string $name
  349. * @return Gauge
  350. */
  351. public function setName(string $name): Gauge
  352. {
  353. $this->name = $name;
  354. return $this;
  355. }
  356. /**
  357. * @return string|null
  358. */
  359. public function getQrCodePath(): ?string
  360. {
  361. return $this->qrCodePath;
  362. }
  363. /**
  364. * @param string|null $qrCodePath
  365. * @return $this
  366. */
  367. public function setQrCodePath(?string $qrCodePath): self
  368. {
  369. $this->qrCodePath = $qrCodePath;
  370. return $this;
  371. }
  372. /**
  373. * @return GaugeProperties
  374. */
  375. public function getProperties(): GaugeProperties
  376. {
  377. return $this->properties;
  378. }
  379. /**
  380. * @param GaugeProperties $properties
  381. * @return Gauge
  382. */
  383. public function setProperties(GaugeProperties $properties): Gauge
  384. {
  385. $this->properties = $properties;
  386. return $this;
  387. }
  388. /**
  389. * @return Collection|File[]
  390. */
  391. public function getFiles(): Collection
  392. {
  393. return $this->files;
  394. }
  395. /**
  396. * @param File $file
  397. * @return $this
  398. */
  399. public function addFile(File $file): self
  400. {
  401. if (!$this->files->contains($file)) {
  402. $file->addGauge($this);
  403. $this->files->add($file);
  404. }
  405. return $this;
  406. }
  407. /**
  408. * @return int|null
  409. */
  410. public function getPrimarySource(): ?int
  411. {
  412. return $this->primarySource;
  413. }
  414. /**
  415. * @param int|null $primarySource
  416. * @return Gauge
  417. */
  418. public function setPrimarySource(?int $primarySource): Gauge
  419. {
  420. $this->primarySource = $primarySource;
  421. return $this;
  422. }
  423. /**
  424. * @return string|null
  425. */
  426. public function getPrimarySourceOther(): ?string
  427. {
  428. return $this->primarySourceOther;
  429. }
  430. /**
  431. * @param string|null $primarySourceOther
  432. * @return Gauge
  433. */
  434. public function setPrimarySourceOther(?string $primarySourceOther): Gauge
  435. {
  436. $this->primarySourceOther = $primarySourceOther;
  437. return $this;
  438. }
  439. /**
  440. * @param File $file
  441. * @return $this
  442. */
  443. public function removeFile(File $file): self
  444. {
  445. if ($this->files->contains($file)) {
  446. $this->files->removeElement($file);
  447. $file->removeGauge($this);
  448. }
  449. return $this;
  450. }
  451. /**
  452. * kontrola zda je měřidlo podružné
  453. * @return bool
  454. */
  455. public function isSecondary(): bool
  456. {
  457. if ($this->getLevel() === GaugeType::LEVEL_SECONDARY && $this->getParent() !== null) {
  458. return true;
  459. }
  460. return false;
  461. }
  462. /**
  463. * kontrola zda je měřidlo hlavní
  464. * @return bool
  465. */
  466. public function isMain(): bool
  467. {
  468. return $this->getLevel() === GaugeType::LEVEL_MAIN;
  469. }
  470. /**
  471. * @return string
  472. */
  473. public function __toString(): string
  474. {
  475. return $this->getName();
  476. }
  477. /**
  478. * @return File|null
  479. */
  480. public function getPhoto(): ?File
  481. {
  482. return $this->photo;
  483. }
  484. /**
  485. * @param File|null $photo
  486. * @return Gauge
  487. */
  488. public function setPhoto(?File $photo): Gauge
  489. {
  490. $this->photo = $photo;
  491. if ($photo !== null) {
  492. $photo->addGauge($this);
  493. }
  494. return $this;
  495. }
  496. /**
  497. * @return GaugeRate[]|Collection
  498. */
  499. public function getRates()
  500. {
  501. return $this->rates;
  502. }
  503. /**
  504. * @param GaugeRate[]|Collection $rates
  505. * @return Gauge
  506. */
  507. public function setRates($rates)
  508. {
  509. $this->rates = $rates;
  510. return $this;
  511. }
  512. /**
  513. * @param GaugeRate $rate
  514. * @return Gauge
  515. */
  516. public function addRate(GaugeRate $rate): Gauge
  517. {
  518. $rate->setGauge($this);
  519. $this->rates->add($rate);
  520. return $this;
  521. }
  522. /**
  523. * @return GaugePhaseAndBreaker[]|Collection
  524. */
  525. public function getPhaseAndBreaker(): Collection
  526. {
  527. return $this->phaseAndBreaker;
  528. }
  529. /**
  530. * @param GaugePhaseAndBreaker[]|Collection $phaseAndBreaker
  531. * @return Gauge
  532. */
  533. public function setPhaseAndBreaker($phaseAndBreaker): Gauge
  534. {
  535. $this->phaseAndBreaker = $phaseAndBreaker;
  536. return $this;
  537. }
  538. /**
  539. * @param GaugePhaseAndBreaker $phaseAndBreaker
  540. * @return Gauge
  541. */
  542. public function addPhaseAndBreaker(GaugePhaseAndBreaker $phaseAndBreaker): Gauge
  543. {
  544. $phaseAndBreaker->setGauge($this);
  545. $this->phaseAndBreaker->add($phaseAndBreaker);
  546. return $this;
  547. }
  548. /**
  549. * @return GaugeHeatingPower[]|Collection
  550. */
  551. public function getHeatingPower()
  552. {
  553. return $this->heatingPower;
  554. }
  555. /**
  556. * @param GaugeHeatingPower[]|Collection $heatingPower
  557. * @return Gauge
  558. */
  559. public function setHeatingPower($heatingPower): Gauge
  560. {
  561. $this->heatingPower = $heatingPower;
  562. return $this;
  563. }
  564. /**
  565. * @param GaugeHeatingPower $heatingPower
  566. * @return Gauge
  567. */
  568. public function addHeatingPower(GaugeHeatingPower $heatingPower): Gauge
  569. {
  570. $heatingPower->setGauge($this);
  571. $this->heatingPower->add($heatingPower);
  572. return $this;
  573. }
  574. /**
  575. * @return GaugeUnitPrice[]|Collection
  576. */
  577. public function getUnitPrice()
  578. {
  579. return $this->unitPrice;
  580. }
  581. /**
  582. * @param GaugeUnitPrice[]|Collection $unitPrice
  583. * @return Gauge
  584. */
  585. public function setUnitPrice($unitPrice): Gauge
  586. {
  587. $this->unitPrice = $unitPrice;
  588. return $this;
  589. }
  590. /**
  591. * @param GaugeUnitPrice $unitPrice
  592. * @return $this
  593. */
  594. public function addUnitPrice(GaugeUnitPrice $unitPrice): Gauge
  595. {
  596. $unitPrice->setGauge($this);
  597. $this->unitPrice->add($unitPrice);
  598. return $this;
  599. }
  600. /**
  601. * @return GaugePriceSupplyPoint[]|Collection
  602. */
  603. public function getPriceSupplyPoint(): Collection
  604. {
  605. return $this->priceSupplyPoint;
  606. }
  607. /**
  608. * @param GaugePriceSupplyPoint[]|Collection $priceSupplyPoint
  609. * @return Gauge
  610. */
  611. public function setPriceSupplyPoint($priceSupplyPoint): Gauge
  612. {
  613. $this->priceSupplyPoint = $priceSupplyPoint;
  614. return $this;
  615. }
  616. /**
  617. * @param GaugePriceSupplyPoint $priceSupplyPoint
  618. * @return $this
  619. */
  620. public function addPriceSupplyPoint(GaugePriceSupplyPoint $priceSupplyPoint): Gauge
  621. {
  622. $priceSupplyPoint->setGauge($this);
  623. $this->priceSupplyPoint->add($priceSupplyPoint);
  624. return $this;
  625. }
  626. /**
  627. * @return GaugeRemoteProperties|null
  628. */
  629. public function getRemoteProperties(): ?GaugeRemoteProperties
  630. {
  631. return $this->remoteProperties;
  632. }
  633. /**
  634. * @param GaugeRemoteProperties|null $remoteProperties
  635. * @return Gauge
  636. */
  637. public function setRemoteProperties(?GaugeRemoteProperties $remoteProperties): Gauge
  638. {
  639. $this->remoteProperties = $remoteProperties;
  640. return $this;
  641. }
  642. /**
  643. * @param int $mediumType
  644. * @param \DateTime|null $date
  645. * @return HistoricalInterface|GaugeUnitPrice|mixed|null
  646. */
  647. public function getCurrentUnitPrice(int $mediumType, ?\DateTime $date = null)
  648. {
  649. if ($date === null) {
  650. $date = new \DateTime();
  651. }
  652. $current = null;
  653. foreach ($this->getUnitPrice() as $item) {
  654. if ($item->getValidFrom() <= $date
  655. && $mediumType === $item->getMedium()
  656. && (
  657. !$current instanceof GaugeUnitPrice
  658. || $item->getValidFrom() > $current->getValidFrom()
  659. )
  660. ) {
  661. $current = $item;
  662. }
  663. }
  664. return $current;
  665. }
  666. /**
  667. * @return bool
  668. */
  669. public function isRemote(): bool
  670. {
  671. return $this->remote;
  672. }
  673. /**
  674. * @param bool $remote
  675. * @return Gauge
  676. */
  677. public function setRemote(bool $remote): Gauge
  678. {
  679. $this->remote = $remote;
  680. return $this;
  681. }
  682. /**
  683. * @return GaugeReading[]|Collection
  684. */
  685. public function getReadings()
  686. {
  687. return $this->readings;
  688. }
  689. /**
  690. * @return bool
  691. */
  692. public function isElectricityMeter(): bool
  693. {
  694. return $this->isType(GaugeType::TYPE_ELECTRICITY_METER);
  695. }
  696. /**
  697. * @return bool
  698. */
  699. public function isGasometter(): bool
  700. {
  701. return $this->isType(GaugeType::TYPE_GASOMETER);
  702. }
  703. /**
  704. * @param int $type
  705. * @return bool
  706. */
  707. public function isType(int $type): bool
  708. {
  709. return $this->getType() === $type;
  710. }
  711. /**
  712. * @return bool
  713. */
  714. public function canHaveReadings(): bool
  715. {
  716. return $this->getLevel() !== GaugeType::LEVEL_FICTIVE && $this->getType() !== GaugeType::TYPE_OTHER_FUELS;
  717. }
  718. /**
  719. * @return bool
  720. */
  721. public function canHaveInvoices(): bool
  722. {
  723. return true;
  724. }
  725. public function hasProduction(): bool
  726. {
  727. return in_array($this->getMeasurementTarget(), [GaugeType::MEASUREMENT_TARGET_PRODUCTION, GaugeType::MEASUREMENT_TARGET_CONSUMPTION_DELIVERY], true);
  728. }
  729. /**
  730. * @return GaugePurpose[]|Collection
  731. */
  732. public function getPurposes(): Collection
  733. {
  734. return $this->purposes;
  735. }
  736. /**
  737. * @param GaugePurpose[]|Collection $purposes
  738. * @return Gauge
  739. */
  740. public function setPurposes($purposes): Gauge
  741. {
  742. $this->purposes = $purposes;
  743. return $this;
  744. }
  745. /**
  746. * @param GaugePurpose $gaugePurpose
  747. * @return Gauge
  748. */
  749. public function addPurpose(GaugePurpose $gaugePurpose): Gauge
  750. {
  751. if (!$this->purposes->contains($gaugePurpose)) {
  752. $gaugePurpose->setGauge($this);
  753. $this->purposes->add($gaugePurpose);
  754. }
  755. return $this;
  756. }
  757. /**
  758. * @return int
  759. */
  760. public function getMeasurementTarget(): int
  761. {
  762. return $this->measurementTarget;
  763. }
  764. /**
  765. * @param int $measurementTarget
  766. * @return Gauge
  767. */
  768. public function setMeasurementTarget(int $measurementTarget): Gauge
  769. {
  770. $this->measurementTarget = $measurementTarget;
  771. return $this;
  772. }
  773. /**
  774. * @param int $purposeConst
  775. * @return GaugePurpose|null
  776. */
  777. public function getPurposeObject(int $purposeConst): ?GaugePurpose
  778. {
  779. foreach ($this->getPurposes() as $gaugePurpose) {
  780. if ($gaugePurpose->getPurpose() === $purposeConst) {
  781. return $gaugePurpose;
  782. }
  783. }
  784. return null;
  785. }
  786. /**
  787. * @return GaugePurpose|null
  788. */
  789. public function getPurposeFirst(): ?GaugePurpose
  790. {
  791. if ($this->purposes->count() > 0) {
  792. return $this->purposes->first();
  793. }
  794. return null;
  795. }
  796. /**
  797. * @param int $purposeConst
  798. * @return GaugePurpose
  799. */
  800. public function getPurposeNewOrExistObject(int $purposeConst): GaugePurpose
  801. {
  802. $gaugePurpose = $this->getPurposeObject($purposeConst);
  803. if ($gaugePurpose === null) {
  804. $gaugePurpose = new GaugePurpose();
  805. $gaugePurpose->setPurpose($purposeConst);
  806. $gaugePurpose->setGauge($this);
  807. }
  808. return $gaugePurpose;
  809. }
  810. /**
  811. * @return Invoice[]|Collection
  812. */
  813. public function getInvoices(): Collection
  814. {
  815. return $this->invoices;
  816. }
  817. /**
  818. * @param Invoice[]|Collection $invoices
  819. * @return Gauge
  820. */
  821. public function setInvoices($invoices): Gauge
  822. {
  823. $this->invoices = $invoices;
  824. return $this;
  825. }
  826. /**
  827. * @param Invoice $invoice
  828. * @return Gauge
  829. */
  830. public function addInvoice(Invoice $invoice): Gauge
  831. {
  832. $this->invoices[] = $invoice;
  833. return $this;
  834. }
  835. /**
  836. * @param \DateTime|null $date
  837. * @return int
  838. */
  839. public function getInvoiceUnit(?\DateTime $date = null): int
  840. {
  841. $invUnitObj = $this->getCurrent('invoiceUnits', $date, $this->getInvoiceUnits());
  842. if ($invUnitObj instanceof GaugeInvoiceUnit) {
  843. return $invUnitObj->getUnit();
  844. }
  845. return Units::getDefaultUnitByGaugeType($this->getType());
  846. }
  847. /**
  848. * @return Event[]|Collection
  849. */
  850. public function getEvents()
  851. {
  852. return $this->events;
  853. }
  854. /**
  855. * @param Event[]|Collection $events
  856. * @return Gauge
  857. */
  858. public function setEvents($events): Gauge
  859. {
  860. $this->events = $events;
  861. return $this;
  862. }
  863. public function getSector(): Sector
  864. {
  865. return $this->getBuilding()->getSector();
  866. }
  867. /**
  868. * @return ShareOfGauges[]|Collection
  869. */
  870. public function getShares()
  871. {
  872. return $this->shares;
  873. }
  874. /**
  875. * @param ShareOfGauges[]|Collection $shares
  876. * @return Gauge
  877. */
  878. public function setShares($shares): Gauge
  879. {
  880. $this->shares = $shares;
  881. return $this;
  882. }
  883. /**
  884. * @return array
  885. */
  886. public function toArray(): array
  887. {
  888. return [
  889. 'id' => $this->getId(),
  890. 'name' => $this->getName()
  891. ];
  892. }
  893. /**
  894. * @return bool
  895. */
  896. public function isElectricityWholesale():bool
  897. {
  898. if (
  899. $this->getType() === GaugeType::TYPE_ELECTRICITY_METER &&
  900. in_array($this->getProperties()->getConsumptionType(), [Dataset::CONSUMPTION_TYPE_WHOLESALE_VN, Dataset::CONSUMPTION_TYPE_WHOLESALE_VVN], true)
  901. ) {
  902. return true;
  903. }
  904. return false;
  905. }
  906. /**
  907. * @return bool
  908. */
  909. public function isForceLowTariff():bool
  910. {
  911. return $this->getProperties()->getForceLowTariff() === true;
  912. }
  913. /**
  914. * @return bool
  915. */
  916. public function isForceSpecialTariff():bool
  917. {
  918. return $this->getProperties()->getForceSpecialTariff() === true;
  919. }
  920. /**
  921. * @return GaugeInvoiceUnit[]|Collection
  922. */
  923. public function getInvoiceUnits(): Collection
  924. {
  925. return $this->invoiceUnits;
  926. }
  927. /**
  928. * @param GaugeInvoiceUnit $item
  929. * @return $this
  930. */
  931. public function addInvoiceUnit(GaugeInvoiceUnit $item): self
  932. {
  933. $item->setGauge($this);
  934. $this->invoiceUnits->add($item);
  935. return $this;
  936. }
  937. /**
  938. * @return Distributor|null
  939. */
  940. public function getElectricityDistributor(): ?Distributor
  941. {
  942. return $this->electricityDistributor;
  943. }
  944. /**
  945. * @param Distributor|null $electricityDistributor
  946. * @return Gauge
  947. */
  948. public function setElectricityDistributor(?Distributor $electricityDistributor): Gauge
  949. {
  950. $this->electricityDistributor = $electricityDistributor;
  951. return $this;
  952. }
  953. /**
  954. * @return string|null
  955. */
  956. public function getCustomerName() :?string
  957. {
  958. if ($this->properties->getCustomerName() === null) {
  959. return $this->building->getOwner();
  960. }
  961. return $this->properties->getCustomerName();
  962. }
  963. /**
  964. * @return GaugePurpose|null
  965. */
  966. public function getConsumptionPurpose(): ?GaugePurpose
  967. {
  968. foreach ($this->purposes as $gPurpose) {
  969. if ($gPurpose->getPurpose() === GaugePurpose::PURPOSE_CONSUMPTION) {
  970. return $gPurpose;
  971. }
  972. }
  973. return null;
  974. }
  975. /**
  976. * @return bool|null
  977. */
  978. public function getShowInOutput(): ?bool
  979. {
  980. return $this->showInOutput;
  981. }
  982. /**
  983. * @param bool|null $showInOutput
  984. * @return self
  985. */
  986. public function setShowInOutput(?bool $showInOutput): self
  987. {
  988. $this->showInOutput = $showInOutput;
  989. return $this;
  990. }
  991. public function getImport(): ?DataImport
  992. {
  993. return $this->import;
  994. }
  995. public function setImport(?DataImport $import): Gauge
  996. {
  997. $this->import = $import;
  998. return $this;
  999. }
  1000. /**
  1001. * @return int
  1002. */
  1003. public function getConsumptionInResultsRatio(): int
  1004. {
  1005. $inResultsSettings = $this->getDataInResults();
  1006. if ($inResultsSettings === Dataset::CONSUMPTION_IN_RESULTS_OFF) {
  1007. return 0;
  1008. }
  1009. if ($inResultsSettings === Dataset::CONSUMPTION_IN_RESULTS_TAKE_OFF) {
  1010. return - 1;
  1011. }
  1012. return 1;
  1013. }
  1014. /**
  1015. * @return int
  1016. */
  1017. public function getCostInResultsRatio(): int
  1018. {
  1019. $costInResults = $this->getProperties()->getCostsInResults();
  1020. if ($costInResults === Dataset::COSTS_IN_RESULTS_OFF) {
  1021. return 0;
  1022. }
  1023. if ($costInResults === Dataset::COSTS_IN_RESULTS_TAKE_OFF) {
  1024. return - 1;
  1025. }
  1026. return 1;
  1027. }
  1028. /**
  1029. * @return bool
  1030. */
  1031. public function hasParentFromSameBuilding():bool
  1032. {
  1033. if ($this->getParent() === null) {
  1034. return false;
  1035. }
  1036. return $this->getParent()->getBuilding()->getId() === $this->getBuilding()->getId();
  1037. }
  1038. /**
  1039. * @param bool|null $client
  1040. * @return string
  1041. */
  1042. public function getNameWithBuilding(?bool $client = null): string
  1043. {
  1044. if ($client === true) {
  1045. return sprintf('%s - %s', $this->getBuilding()->getClient()->getName(), $this->getNameWithBuilding());
  1046. }
  1047. return sprintf('%s - %s', $this->getBuilding()->getName(), $this->getName());
  1048. }
  1049. /**
  1050. * @return int|string|null
  1051. */
  1052. public function getTdd()
  1053. {
  1054. if ($this->isElectricityMeter()) {
  1055. return $this->getProperties()->getTddEl();
  1056. }
  1057. if ($this->isGasometter()) {
  1058. return $this->getProperties()->getTdd();
  1059. }
  1060. return null;
  1061. }
  1062. /**
  1063. * method returns all allowed segments by all gauge purposes
  1064. * @return void
  1065. */
  1066. public function getSegments(): array
  1067. {
  1068. $segments = [];
  1069. foreach ($this->purposes as $gPurpose) {
  1070. $segments = array_merge($segments, $gPurpose->getSegments());
  1071. }
  1072. return $segments;
  1073. }
  1074. public function getDataInResults(): ?int
  1075. {
  1076. return $this->dataInResults;
  1077. }
  1078. public function setDataInResults(?int $dataInResults): Gauge
  1079. {
  1080. $this->dataInResults = $dataInResults;
  1081. return $this;
  1082. }
  1083. public function hasConsumptionUsage(int $usage): bool
  1084. {
  1085. $consumptionPurpose = $this->getConsumptionPurpose();
  1086. if ($consumptionPurpose === null) {
  1087. return false;
  1088. }
  1089. foreach ($consumptionPurpose->getConsumptionUsage() as $cUsageObj) {
  1090. if ($cUsageObj->getConsumptionUsage() === $usage) {
  1091. return true;
  1092. }
  1093. }
  1094. return false;
  1095. }
  1096. /**
  1097. * @return ConsumptionLimit[]|Collection
  1098. */
  1099. public function getConsumptionLimits()
  1100. {
  1101. return $this->consumptionLimits;
  1102. }
  1103. /**
  1104. * @param ConsumptionLimit[]|Collection $consumptionLimits
  1105. * @return Gauge
  1106. */
  1107. public function setConsumptionLimits($consumptionLimits)
  1108. {
  1109. $this->consumptionLimits = $consumptionLimits;
  1110. return $this;
  1111. }
  1112. /**
  1113. * @param ConsumptionLimit $gaugeLimitObj
  1114. * @return ConsumptionLimit|null
  1115. */
  1116. public function getOtherLimitControl(ConsumptionLimit $gaugeLimitObj): ?ConsumptionLimit
  1117. {
  1118. $otherControls = $this->consumptionLimits->filter(
  1119. fn (ConsumptionLimit $limitItem) => $limitItem->getId() !== $gaugeLimitObj->getId()
  1120. );
  1121. return $otherControls->count() > 0 ? $otherControls->first() : null;
  1122. }
  1123. public function getLastSentNotificationAt(): ?\DateTime
  1124. {
  1125. return $this->lastSentNotificationAt;
  1126. }
  1127. public function setLastSentNotificationAt(?\DateTime $lastSentNotificationAt): Gauge
  1128. {
  1129. $this->lastSentNotificationAt = $lastSentNotificationAt;
  1130. return $this;
  1131. }
  1132. public function isRemoteAsPrimary(): bool
  1133. {
  1134. return $this->remoteAsPrimary;
  1135. }
  1136. public function setRemoteAsPrimary(bool $remoteAsPrimary): Gauge
  1137. {
  1138. $this->remoteAsPrimary = $remoteAsPrimary;
  1139. return $this;
  1140. }
  1141. public function getPrimaryMode(): int
  1142. {
  1143. if ($this->remote && $this->isRemoteAsPrimary()) {
  1144. return GaugeType::MODE_REMOTE;
  1145. }
  1146. return GaugeType::MODE_MANUAL;
  1147. }
  1148. public function getSecondaryMode(): ?int
  1149. {
  1150. if (!$this->remote) {
  1151. return null;
  1152. }
  1153. if (!$this->isRemoteAsPrimary()) {
  1154. return GaugeType::MODE_REMOTE;
  1155. }
  1156. return GaugeType::MODE_MANUAL;
  1157. }
  1158. public function isRemoteOnly(): bool
  1159. {
  1160. $fullRemote = $this->remote && $this->getPrimaryMode() === GaugeType::MODE_REMOTE;
  1161. $noManualReadingsPeriod = true;
  1162. foreach ($this->purposes as $purpose) {
  1163. if ($purpose->getReadingsPeriod() !== Period::NONE) {
  1164. $noManualReadingsPeriod = false;
  1165. }
  1166. }
  1167. return $fullRemote && $noManualReadingsPeriod;
  1168. }
  1169. public function getPurposesForManualReadings()
  1170. {
  1171. $allowedPurposes = $this->getAllowedPurposesByMeasurementTarget();
  1172. return $this->purposes->filter(function (GaugePurpose $gp) use ($allowedPurposes) {
  1173. if (!empty($allowedPurposes) && !in_array($gp->getPurpose(), $allowedPurposes, true)) {
  1174. return false;
  1175. }
  1176. return $this->isPurposeManualEnabled($gp);
  1177. });
  1178. }
  1179. public function getPurposesForRemoteReadings()
  1180. {
  1181. $allowedPurposes = $this->getAllowedPurposesByMeasurementTarget();
  1182. return $this->purposes->filter(function (GaugePurpose $gp) use ($allowedPurposes) {
  1183. if (!empty($allowedPurposes) && !in_array($gp->getPurpose(), $allowedPurposes, true)) {
  1184. return false;
  1185. }
  1186. return $this->isPurposeRemoteEnabled($gp);
  1187. });
  1188. }
  1189. /**
  1190. * Returns purposes configured for remote readings only.
  1191. * Unlike getPurposesForRemoteReadings(), this is not filtered by measurement target.
  1192. */
  1193. public function getPurposesForConfiguredRemoteReadings()
  1194. {
  1195. return $this->purposes->filter(function (GaugePurpose $gp) {
  1196. return $this->isPurposeRemoteEnabled($gp);
  1197. });
  1198. }
  1199. private function getAllowedPurposesByMeasurementTarget(): array
  1200. {
  1201. $measurementTarget = $this->getMeasurementTarget();
  1202. $map = GaugeType::getPurposesByMeasurementTarget();
  1203. if ($measurementTarget === null) {
  1204. return [];
  1205. }
  1206. return $map[$measurementTarget] ?? [];
  1207. }
  1208. private function isPurposeManualEnabled(GaugePurpose $gp): bool
  1209. {
  1210. $readingsPeriod = $gp->getReadingsPeriod();
  1211. if ($readingsPeriod === null || $readingsPeriod === Period::NONE) {
  1212. return false;
  1213. }
  1214. return $this->getPrimaryMode() === GaugeType::MODE_MANUAL
  1215. || $this->getSecondaryMode() === GaugeType::MODE_MANUAL;
  1216. }
  1217. private function isPurposeRemoteEnabled(GaugePurpose $gp): bool
  1218. {
  1219. if (!$this->isRemote()) {
  1220. return false;
  1221. }
  1222. if ($this->getPrimaryMode() !== GaugeType::MODE_REMOTE && $this->getSecondaryMode() !== GaugeType::MODE_REMOTE) {
  1223. return false;
  1224. }
  1225. if ($gp->getRemoteReadingsMinuteFrequency() !== null || $gp->getRemoteReadingsPeriod() !== null) {
  1226. return true;
  1227. }
  1228. return $gp->getRemoteUnits()->count() > 0;
  1229. }
  1230. public function getContracts(): array|Collection|null
  1231. {
  1232. return $this->contracts;
  1233. }
  1234. /**
  1235. * @param Contract[]|Collection $contracts
  1236. * @return Gauge
  1237. */
  1238. public function setContracts($contracts): Gauge
  1239. {
  1240. $this->contracts = $contracts;
  1241. return $this;
  1242. }
  1243. public function addContract(Contract $contract): Gauge
  1244. {
  1245. if ($this->contracts === null) {
  1246. $this->contracts = new ArrayCollection();
  1247. }
  1248. if (!$this->contracts->contains($contract)) {
  1249. $this->contracts->add($contract);
  1250. }
  1251. return $this;
  1252. }
  1253. public function removeContract(Contract $contract): Gauge
  1254. {
  1255. if ($this->contracts === null) {
  1256. $this->contracts = new ArrayCollection();
  1257. }
  1258. if ($this->contracts->contains($contract)) {
  1259. $this->contracts->removeElement($contract);
  1260. $contract->removeGauge($this);
  1261. }
  1262. return $this;
  1263. }
  1264. }