src/Entity/Gauge/ConsumptionLimit.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Gauge;
  3. use App\Entity\Client\Client;
  4. use App\Entity\Extension\BlameableTrait;
  5. use App\Entity\Extension\TimestampableTrait;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use App\Repository\Gauge\ConsumptionLimitRepository;
  10. /**
  11. * @ORM\Entity(repositoryClass=ConsumptionLimitRepository::class)
  12. * @ORM\Table(name="consumption_limit")
  13. */
  14. class ConsumptionLimit
  15. {
  16. use BlameableTrait;
  17. use TimestampableTrait;
  18. public const WEEKEND_KEY = 'weekend';
  19. public const WORKDAY_KEY = 'workday';
  20. /**
  21. * @var null|int
  22. * @ORM\Id()
  23. * @ORM\GeneratedValue()
  24. * @ORM\Column(type="integer")
  25. */
  26. private ?int $id = null;
  27. /**
  28. * @var Client
  29. * @ORM\ManyToOne(targetEntity="App\Entity\Client\Client")
  30. * @ORM\JoinColumn(name="client_id", referencedColumnName="id", nullable=false)
  31. */
  32. private Client $client;
  33. /**
  34. * @var string
  35. * @ORM\Column(type="string")
  36. *
  37. */
  38. private string $name;
  39. /**
  40. * @var int
  41. * @ORM\Column(type="integer")
  42. *
  43. */
  44. private int $gaugeType;
  45. /**
  46. * @var string
  47. * @ORM\Column(type="string")
  48. */
  49. protected string $systemCode;
  50. /**
  51. * @var int
  52. * @ORM\Column(type="integer")
  53. */
  54. private int $unit;
  55. /**
  56. * @var null|string
  57. *
  58. * @ORM\Column(type="text", nullable=true)
  59. */
  60. private ?string $note;
  61. /**
  62. * @var bool
  63. *
  64. * @ORM\Column(type="boolean")
  65. */
  66. private bool $enabled = true;
  67. /**
  68. * @var Collection|Gauge[]
  69. *
  70. * @ORM\ManyToMany(targetEntity="App\Entity\Gauge\Gauge", inversedBy="consumptionLimits", cascade={"persist"})
  71. */
  72. protected $gauges;
  73. /**
  74. * @var Collection|ConsumptionLimitItem[]
  75. * @ORM\OneToMany(targetEntity="App\Entity\Gauge\ConsumptionLimitItem", mappedBy="consumptionLimit", cascade={"all"}, orphanRemoval=true)
  76. */
  77. private $items;
  78. /**
  79. * @var null|int
  80. *
  81. * @ORM\Column(type="integer", nullable=true)
  82. */
  83. protected ?int $serialNumber = null;
  84. public function __construct()
  85. {
  86. $this->gauges = new ArrayCollection();
  87. $this->items = new ArrayCollection();
  88. }
  89. public function getId(): ?int
  90. {
  91. return $this->id;
  92. }
  93. public function setId(?int $id): ConsumptionLimit
  94. {
  95. $this->id = $id;
  96. return $this;
  97. }
  98. public function getClient(): Client
  99. {
  100. return $this->client;
  101. }
  102. public function setClient(Client $client): ConsumptionLimit
  103. {
  104. $this->client = $client;
  105. return $this;
  106. }
  107. public function getName(): string
  108. {
  109. return $this->name;
  110. }
  111. public function setName(string $name): ConsumptionLimit
  112. {
  113. $this->name = $name;
  114. return $this;
  115. }
  116. public function getGaugeType(): int
  117. {
  118. return $this->gaugeType;
  119. }
  120. public function setGaugeType(int $gaugeType): ConsumptionLimit
  121. {
  122. $this->gaugeType = $gaugeType;
  123. return $this;
  124. }
  125. public function getSystemCode(): string
  126. {
  127. return $this->systemCode;
  128. }
  129. public function setSystemCode(string $systemCode): ConsumptionLimit
  130. {
  131. $this->systemCode = $systemCode;
  132. return $this;
  133. }
  134. public function getUnit(): int
  135. {
  136. return $this->unit;
  137. }
  138. public function setUnit(int $unit): ConsumptionLimit
  139. {
  140. $this->unit = $unit;
  141. return $this;
  142. }
  143. public function getNote(): ?string
  144. {
  145. return $this->note;
  146. }
  147. public function setNote(?string $note): ConsumptionLimit
  148. {
  149. $this->note = $note;
  150. return $this;
  151. }
  152. public function isEnabled(): bool
  153. {
  154. return $this->enabled;
  155. }
  156. public function setEnabled(bool $enabled): ConsumptionLimit
  157. {
  158. $this->enabled = $enabled;
  159. return $this;
  160. }
  161. /**
  162. * @return Gauge[]|Collection
  163. */
  164. public function getGauges()
  165. {
  166. return $this->gauges;
  167. }
  168. /**
  169. * @param Gauge[]|Collection $gauges
  170. * @return ConsumptionLimit
  171. */
  172. public function setGauges($gauges)
  173. {
  174. $this->gauges = $gauges;
  175. return $this;
  176. }
  177. /**
  178. * @return ConsumptionLimitItem[]|ArrayCollection|Collection
  179. */
  180. public function getItems()
  181. {
  182. return $this->items;
  183. }
  184. /**
  185. * @param ConsumptionLimitItem[]|ArrayCollection|Collection $items
  186. * @return ConsumptionLimit
  187. */
  188. public function setItems($items)
  189. {
  190. $this->items = $items;
  191. return $this;
  192. }
  193. public function getItemsWithKeys(): array
  194. {
  195. $items = [];
  196. foreach ($this->items as $item) {
  197. $dayKey = $item->isWeekend() ? self::WEEKEND_KEY : self::WORKDAY_KEY;
  198. $items[$item->getHour()][$dayKey] = $item->getValue();
  199. }
  200. return $items;
  201. }
  202. public function addNewHourData(int $hour, string $dayKey, float $limitValue): self
  203. {
  204. $newItem = (new ConsumptionLimitItem())
  205. ->setHour($hour)
  206. ->setWeekend($dayKey === self::WEEKEND_KEY)
  207. ->setValue($limitValue)
  208. ->setConsumptionLimit($this)
  209. ;
  210. $this->items->add($newItem);
  211. return $this;
  212. }
  213. public function getSerialNumber(): ?int
  214. {
  215. return $this->serialNumber;
  216. }
  217. public function setSerialNumber(?int $serialNumber): ConsumptionLimit
  218. {
  219. $this->serialNumber = $serialNumber;
  220. return $this;
  221. }
  222. /**
  223. * üsed for log data
  224. * @return array
  225. */
  226. public function toArray(): array
  227. {
  228. return [
  229. 'id' => $this->getId(),
  230. 'systemCode' => $this->getSystemCode(),
  231. 'name' => $this->getName(),
  232. ];
  233. }
  234. }