src/Domain/Whater/Model/WhaterPoint.php line 28

Open in your IDE?
  1. <?php
  2. namespace Whater\Domain\Whater\Model;
  3. use BornFree\TacticianDomainEvent\Recorder\ContainsRecordedEvents;
  4. use BornFree\TacticianDomainEvent\Recorder\EventRecorderCapabilities;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Ramsey\Uuid\Uuid;
  8. use Whater\Domain\Common\Exception\InvalidUUIDException;
  9. use Whater\Domain\User\Model\User;
  10. use Whater\Domain\Whater\Exception\InvalidWhaterLocationException;
  11. use Whater\Domain\Zones\Model\Ubication;
  12. use Whater\Domain\Zones\Model\Town;
  13. use Whater\Domain\Zones\Model\Establishment;
  14. use Whater\Domain\Whater\Model\WhaterDevice;
  15. use LongitudeOne\Spatial\PHP\Types\Geometry\Point;
  16. use Whater\Domain\Product\Model\Product;
  17. use Whater\Domain\Whater\Exception\InvalidWhaterStatusException;
  18. use Whater\Domain\Zones\Model\District;
  19. use Whater\Domain\Zones\Model\TownLocation;
  20. /**
  21. * Class WhaterPoint
  22. *
  23. * @package Whater\Domain\Whater\Model
  24. */
  25. class WhaterPoint implements ContainsRecordedEvents
  26. {
  27. use EventRecorderCapabilities;
  28. const WP_TYPE_FOUNTAIN = 'WP_TYPE_FOUNTAIN';
  29. const WP_TYPE_TAP = 'WP_TYPE_TAP';
  30. const WP_TYPE_DEPOSIT = 'WP_TYPE_DEPOSIT';
  31. const WP_TYPE_BOTTLED = 'WP_TYPE_BOTTLED';
  32. const WP_TYPE_TREATED = 'WP_TYPE_TREATED';
  33. const WP_TYPE_SPRING = 'WP_TYPE_SPRING';
  34. const WP_STATUS_SUITABLE = 'WP_STATUS_SUITABLE';
  35. const WP_STATUS_NOT_SUITABLE = 'WP_STATUS_NOT_SUITABLE';
  36. const WP_STATUS_WITH_RESTRICTIONS = 'WP_STATUS_WITH_RESTRICTIONS';
  37. const WP_STATUS_UNKNOWN = 'WP_STATUS_UNKNOWN';
  38. /**
  39. * @var string
  40. */
  41. private $uuid;
  42. /**
  43. * @var DistributionNetwork
  44. */
  45. private $distributionNetwork;
  46. /**
  47. * @var Town
  48. */
  49. private $town;
  50. /**
  51. * @var TownLocation
  52. */
  53. private $townLocation;
  54. /**
  55. * @var District
  56. */
  57. private $district;
  58. /**
  59. * @var Ubication
  60. */
  61. private $ubication;
  62. /**
  63. * @var Establishment
  64. */
  65. private $establishment;
  66. /**
  67. * @var string
  68. */
  69. private $name;
  70. /**
  71. * @var float
  72. */
  73. private $latitude;
  74. /**
  75. * @var float
  76. */
  77. private $longitude;
  78. /**
  79. * @var string
  80. */
  81. private $slug;
  82. /**
  83. * @var Point
  84. */
  85. private $geoPoint;
  86. /**
  87. * @var bool
  88. */
  89. private $confirmedGeoposition;
  90. /**
  91. * @var string
  92. */
  93. private $type;
  94. /**
  95. * @var WhaterOrganization
  96. */
  97. private $responsableOrganizationCorporative;
  98. /**
  99. * @var WhaterOrganization
  100. */
  101. private $responsableOrganizationUbication;
  102. /**
  103. * @var User
  104. */
  105. private $createdBy;
  106. /**
  107. * @var string
  108. */
  109. private $certification;
  110. /**
  111. * @var \DateTime
  112. */
  113. private $createdAt;
  114. /**
  115. * @var \DateTime
  116. */
  117. private $updatedAt;
  118. /**
  119. * @var Collection
  120. */
  121. private $analyticals;
  122. /**
  123. * @var Collection
  124. */
  125. private $valorations;
  126. /**
  127. * @var array
  128. */
  129. private $totalColor;
  130. /**
  131. * @var array
  132. */
  133. private $totalTaste;
  134. /**
  135. * @var array
  136. */
  137. private $totalSmell;
  138. /**
  139. * @var array
  140. */
  141. private $totalTurbidity;
  142. /**
  143. * @var array
  144. */
  145. private $totalGeneral;
  146. /**
  147. * @var string
  148. */
  149. private $whaterStatus;
  150. /**
  151. * @var \DateTime
  152. */
  153. private $whaterStatusAt;
  154. /**
  155. * @var \DateTime
  156. */
  157. private $lastProfessionalValorationAt;
  158. /**
  159. * @var int
  160. */
  161. private $countProfessionalGeneral;
  162. /**
  163. * @var float
  164. */
  165. private $averageProfessionalGeneral;
  166. /**
  167. * @var \DateTime
  168. */
  169. private $lastUserValorationAt;
  170. /**
  171. * @var int
  172. */
  173. private $countUserGeneral;
  174. /**
  175. * @var float
  176. */
  177. private $averageUserGeneral;
  178. /**
  179. * @var string
  180. */
  181. private $osmId;
  182. /**
  183. * @var string
  184. */
  185. private $sinacId;
  186. /**
  187. * @var string
  188. */
  189. private $sinacSlug;
  190. /**
  191. * @var array
  192. */
  193. private $osmData;
  194. /**
  195. * @var bool
  196. */
  197. private $dispersed;
  198. /**
  199. * @var Collection
  200. */
  201. private $images;
  202. /**
  203. * @var Collection
  204. */
  205. private $organizationLicenses;
  206. /**
  207. * @var WhaterDevice
  208. */
  209. private $whaterDevice;
  210. /**
  211. * @var Product
  212. */
  213. private $product;
  214. /**
  215. * @var array
  216. */
  217. private $importNotes;
  218. /**
  219. * @var bool
  220. */
  221. private $validatedByAdmin;
  222. /**
  223. * @var bool
  224. */
  225. private $notifyWhaterStatusChange;
  226. /**
  227. * @var Collection
  228. */
  229. private $articles;
  230. /**
  231. * @var Collection
  232. */
  233. private $whaterWidgetLogs;
  234. /**
  235. * @var Collection
  236. */
  237. private $whaterRefills;
  238. /**
  239. * @param string $whaterPointId
  240. * @param string $name
  241. * @param float $latitude
  242. * @param float $longitude
  243. * @param DistributionNetwork $distributionNetwork
  244. */
  245. public function __construct(
  246. ?string $whaterPointId = null,
  247. string $name,
  248. string $type,
  249. Town $town,
  250. float $latitude,
  251. float $longitude,
  252. bool $confirmedGeoposition = false,
  253. ?User $createdBy = null,
  254. ?DistributionNetwork $distributionNetwork = null,
  255. ?Ubication $ubication = null,
  256. ?Establishment $establishment = null,
  257. ?TownLocation $townLocation = null
  258. ) {
  259. try {
  260. $this->uuid = Uuid::fromString($whaterPointId ?: Uuid::uuid4())->toString();
  261. } catch (\InvalidArgumentException $e) {
  262. throw new InvalidUUIDException();
  263. }
  264. $this->name = $name;
  265. $this->type = $type;
  266. $this->latitude = $latitude;
  267. $this->longitude = $longitude;
  268. $this->slug = $name;
  269. $this->geoPoint = new Point($latitude, $longitude);
  270. $this->geoPoint->setLongitude($longitude);
  271. $this->geoPoint->setLatitude($latitude);
  272. $this->confirmedGeoposition = $confirmedGeoposition;
  273. $this->town = $town;
  274. $this->distributionNetwork = $distributionNetwork;
  275. $this->createdBy = $createdBy;
  276. $this->analyticals = new ArrayCollection();
  277. $this->valorations = new ArrayCollection();
  278. $this->whaterWidgetLogs = new ArrayCollection();
  279. $this->whaterStatus = WhaterPoint::WP_STATUS_UNKNOWN;
  280. $this->dispersed = false;
  281. $this->createdAt = new \DateTime();
  282. $this->updatedAt = new \DateTime();
  283. $this->images = new ArrayCollection();
  284. $this->whaterRefills = new ArrayCollection();
  285. $this->ubication = $ubication;
  286. $this->establishment = $establishment;
  287. $this->townLocation = $townLocation;
  288. $this->validatedByAdmin = false;
  289. $this->countUserGeneral = 0;
  290. $this->countProfessionalGeneral = 0;
  291. $this->averageProfessionalGeneral = null;
  292. $this->averageUserGeneral = null;
  293. $this->importNotes = [];
  294. $this->notifyWhaterStatusChange = false;
  295. $this->articles = new ArrayCollection();
  296. }
  297. public function updateWhaterpoint(
  298. string $name,
  299. string $slug,
  300. string $type,
  301. Town $town,
  302. float $latitude,
  303. float $longitude,
  304. bool $confirmedGeoposition,
  305. ?string $certification = null,
  306. ?User $createdBy = null,
  307. ?DistributionNetwork $distributionNetwork = null,
  308. ?Establishment $establishment = null,
  309. ?Ubication $ubication = null,
  310. ?WhaterDevice $whaterDevice = null,
  311. ?TownLocation $townLocation = null
  312. ) {
  313. $this->name = $name;
  314. $this->type = $type;
  315. $this->latitude = $latitude;
  316. $this->longitude = $longitude;
  317. $this->slug = $slug;
  318. if ($this->geoPoint() != null) {
  319. $this->geoPoint()->setLongitude($longitude);
  320. $this->geoPoint()->setLatitude($latitude);
  321. } else {
  322. $this->geoPoint = new Point($latitude, $longitude);
  323. }
  324. $this->confirmedGeoposition = $confirmedGeoposition;
  325. $this->town = $town;
  326. $this->distributionNetwork = $distributionNetwork;
  327. $this->whaterDevice = $whaterDevice;
  328. $this->establishment = $establishment;
  329. $this->ubication = $ubication;
  330. if ($this->whaterDevice != null) {
  331. $this->establishment = $this->whaterDevice->establishment();
  332. }
  333. if ($this->establishment != null) {
  334. $this->ubication = $this->establishment->ubication();
  335. }
  336. if ($this->ubication != null && !$this->ubication->town()->equals($this->town)) {
  337. throw new InvalidWhaterLocationException();
  338. }
  339. if ($createdBy != null) {
  340. $this->createdBy = $createdBy;
  341. }
  342. if ($townLocation != null) {
  343. $this->townLocation = $townLocation;
  344. }
  345. $this->certification = $certification;
  346. $this->updatedAt = new \DateTime();
  347. return $this;
  348. }
  349. public function setValidatedByAdmin(bool $validatedByAdmin)
  350. {
  351. $this->validatedByAdmin = $validatedByAdmin;
  352. }
  353. public function updateOsmData(
  354. string $osmId,
  355. array $osmData
  356. ) {
  357. $this->osmId = $osmId;
  358. $this->osmData = $osmData;
  359. $this->updatedAt = new \DateTime();
  360. return $this;
  361. }
  362. public function updateSinacData(
  363. string $sinacId,
  364. string $sinacSlug,
  365. bool $dispersed = false
  366. ) {
  367. $this->sinacId = $sinacId;
  368. $this->sinacSlug = $sinacSlug;
  369. $this->dispersed = $dispersed;
  370. $this->updatedAt = new \DateTime();
  371. return $this;
  372. }
  373. public function updateValorations()
  374. {
  375. $sumUserColor = array(
  376. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  377. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  378. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  379. );
  380. $sumUserTaste = array(
  381. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  382. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  383. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  384. );
  385. $sumUserSmell = array(
  386. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  387. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  388. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  389. );
  390. $sumUserTurbidity = array(
  391. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  392. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  393. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  394. );
  395. $sumUserGeneral = array(
  396. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  397. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  398. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  399. );
  400. $countUserColor = 0;
  401. $countUserTaste = 0;
  402. $countUserSmell = 0;
  403. $countUserTurbidity = 0;
  404. $countUserGeneral = 0;
  405. $sumProfessionalColor = array(
  406. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  407. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  408. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  409. );
  410. $sumProfessionalTaste = array(
  411. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  412. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  413. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  414. );
  415. $sumProfessionalSmell = array(
  416. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  417. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  418. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  419. );
  420. $sumProfessionalTurbidity = array(
  421. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  422. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  423. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  424. );
  425. $sumProfessionalGeneral = array(
  426. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  427. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  428. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  429. );
  430. $countProfessionalColor = 0;
  431. $countProfessionalTaste = 0;
  432. $countProfessionalSmell = 0;
  433. $countProfessionalTurbidity = 0;
  434. $countProfessionalGeneral = 0;
  435. foreach ($this->valorations() as $valoration) {
  436. if ($valoration->isProfessionalValoration()) {
  437. if (
  438. $this->lastProfessionalValorationAt == null ||
  439. $this->lastProfessionalValorationAt < $valoration->createdAt()
  440. ) {
  441. $this->lastProfessionalValorationAt = $valoration->createdAt();
  442. }
  443. if ($valoration->colorValoration() != null) {
  444. switch ($valoration->colorValoration()) {
  445. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  446. $sumProfessionalColor[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  447. break;
  448. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  449. $sumProfessionalColor[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  450. break;
  451. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  452. $sumProfessionalColor[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  453. break;
  454. default:
  455. break;
  456. }
  457. $countProfessionalColor++;
  458. }
  459. if ($valoration->tasteValoration() != null) {
  460. switch ($valoration->tasteValoration()) {
  461. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  462. $sumProfessionalTaste[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  463. break;
  464. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  465. $sumProfessionalTaste[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  466. break;
  467. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  468. $sumProfessionalTaste[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  469. break;
  470. default:
  471. break;
  472. }
  473. $countProfessionalTaste++;
  474. }
  475. if ($valoration->smellValoration() != null) {
  476. switch ($valoration->smellValoration()) {
  477. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  478. $sumProfessionalSmell[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  479. break;
  480. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  481. $sumProfessionalSmell[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  482. break;
  483. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  484. $sumProfessionalSmell[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  485. break;
  486. default:
  487. break;
  488. }
  489. $countProfessionalSmell++;
  490. }
  491. if ($valoration->turbidityValoration() != null) {
  492. switch ($valoration->turbidityValoration()) {
  493. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  494. $sumProfessionalTurbidity[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  495. break;
  496. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  497. $sumProfessionalTurbidity[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  498. break;
  499. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  500. $sumProfessionalTurbidity[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  501. break;
  502. default:
  503. break;
  504. }
  505. $countProfessionalTurbidity++;
  506. }
  507. if ($valoration->generalValoration() != null) {
  508. switch ($valoration->generalValoration()) {
  509. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  510. $sumProfessionalGeneral[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  511. break;
  512. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  513. $sumProfessionalGeneral[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  514. break;
  515. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  516. $sumProfessionalGeneral[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  517. break;
  518. default:
  519. break;
  520. }
  521. $countProfessionalGeneral++;
  522. }
  523. } else {
  524. if (
  525. $this->lastUserValorationAt == null ||
  526. $this->lastUserValorationAt < $valoration->createdAt()
  527. ) {
  528. $this->lastUserValorationAt = $valoration->createdAt();
  529. }
  530. if ($valoration->colorValoration() != null) {
  531. switch ($valoration->colorValoration()) {
  532. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  533. $sumUserColor[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  534. break;
  535. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  536. $sumUserColor[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  537. break;
  538. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  539. $sumUserColor[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  540. break;
  541. default:
  542. break;
  543. }
  544. $countUserColor++;
  545. }
  546. if ($valoration->tasteValoration() != null) {
  547. switch ($valoration->tasteValoration()) {
  548. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  549. $sumUserTaste[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  550. break;
  551. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  552. $sumUserTaste[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  553. break;
  554. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  555. $sumUserTaste[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  556. break;
  557. default:
  558. break;
  559. }
  560. $countUserTaste++;
  561. }
  562. if ($valoration->smellValoration() != null) {
  563. switch ($valoration->smellValoration()) {
  564. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  565. $sumUserSmell[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  566. break;
  567. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  568. $sumUserSmell[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  569. break;
  570. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  571. $sumUserSmell[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  572. break;
  573. default:
  574. break;
  575. }
  576. $countUserSmell++;
  577. }
  578. if ($valoration->turbidityValoration() != null) {
  579. switch ($valoration->turbidityValoration()) {
  580. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  581. $sumUserTurbidity[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  582. break;
  583. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  584. $sumUserTurbidity[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  585. break;
  586. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  587. $sumUserTurbidity[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  588. break;
  589. default:
  590. break;
  591. }
  592. $countUserTurbidity++;
  593. }
  594. if ($valoration->generalValoration() != null) {
  595. switch ($valoration->generalValoration()) {
  596. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  597. $sumUserGeneral[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  598. break;
  599. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  600. $sumUserGeneral[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  601. break;
  602. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  603. $sumUserGeneral[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  604. break;
  605. default:
  606. break;
  607. }
  608. $countUserGeneral++;
  609. }
  610. }
  611. }
  612. // count valoration
  613. $this->countProfessionalGeneral = $countProfessionalGeneral;
  614. $this->countUserGeneral = $countUserGeneral;
  615. // calculate average valoration for color, taste, smell and turbidity
  616. if ($countProfessionalColor + $countUserColor > 0) {
  617. foreach ($sumUserColor as $key => $value) {
  618. $this->totalColor[$key] = $sumUserColor[$key] + $sumProfessionalColor[$key];
  619. }
  620. } else {
  621. $this->totalColor = array(
  622. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  623. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  624. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  625. );
  626. }
  627. if ($countProfessionalTaste + $countUserTaste > 0) {
  628. foreach ($sumUserTaste as $key => $value) {
  629. $this->totalTaste[$key] = $sumUserTaste[$key] + $sumProfessionalTaste[$key];
  630. }
  631. } else {
  632. $this->totalTaste = array(
  633. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  634. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  635. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  636. );
  637. }
  638. if ($countProfessionalSmell + $countUserSmell > 0) {
  639. foreach ($sumUserSmell as $key => $value) {
  640. $this->totalSmell[$key] = $sumUserSmell[$key] + $sumProfessionalSmell[$key];
  641. }
  642. } else {
  643. $this->totalSmell = array(
  644. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  645. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  646. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  647. );
  648. }
  649. if ($countProfessionalTurbidity + $countUserTurbidity > 0) {
  650. foreach ($sumUserTurbidity as $key => $value) {
  651. $this->totalTurbidity[$key] = $sumUserTurbidity[$key] + $sumProfessionalTurbidity[$key];
  652. }
  653. } else {
  654. $this->totalTurbidity = array(
  655. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  656. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  657. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  658. );
  659. }
  660. if ($countProfessionalGeneral + $countUserGeneral > 0) {
  661. foreach ($sumUserGeneral as $key => $value) {
  662. $this->totalGeneral[$key] = $sumUserGeneral[$key] + $sumProfessionalGeneral[$key];
  663. }
  664. } else {
  665. $this->totalGeneral = array(
  666. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  667. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  668. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  669. );
  670. }
  671. // calculate average valoration for users and professionals
  672. if ($this->countProfessionalGeneral > 0) {
  673. $averageProfessionalGeneral =
  674. (($sumProfessionalGeneral[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION] * 10) +
  675. ($sumProfessionalGeneral[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION] * 5) +
  676. ($sumProfessionalGeneral[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION] * 0)) / $this->countProfessionalGeneral;
  677. $this->averageProfessionalGeneral = round($averageProfessionalGeneral, 2);
  678. } else {
  679. $this->averageProfessionalGeneral = null;
  680. }
  681. if ($this->countUserGeneral > 0) {
  682. $averageUserGeneral =
  683. (($sumUserGeneral[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION] * 10) +
  684. ($sumUserGeneral[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION] * 5) +
  685. ($sumUserGeneral[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION] * 0)) / $this->countUserGeneral;
  686. $this->averageUserGeneral = round($averageUserGeneral, 2);
  687. } else {
  688. $this->averageUserGeneral = null;
  689. }
  690. }
  691. public function updateResponsables(
  692. WhaterOrganization $responsableOrganizationCorporative = null,
  693. WhaterOrganization $responsableOrganizationUbication = null
  694. ) {
  695. $this->responsableOrganizationCorporative = $responsableOrganizationCorporative;
  696. $this->responsableOrganizationUbication = $responsableOrganizationUbication;
  697. }
  698. public function updateImportNotes(
  699. array $importNotes,
  700. ) {
  701. $this->importNotes = $importNotes;
  702. return $this;
  703. }
  704. /**
  705. * @return string
  706. */
  707. public function id(): string
  708. {
  709. return $this->uuid;
  710. }
  711. /**
  712. * @return string
  713. */
  714. public function name(): string
  715. {
  716. return $this->name;
  717. }
  718. /**
  719. * @return array
  720. */
  721. public function importNotes(): array
  722. {
  723. return $this->importNotes;
  724. }
  725. /**
  726. * @return string
  727. */
  728. public function type(): string
  729. {
  730. return $this->type;
  731. }
  732. /**
  733. * @return string
  734. */
  735. public function certification(): ?string
  736. {
  737. return $this->certification;
  738. }
  739. /**
  740. * @return string
  741. */
  742. public function osmId(): ?string
  743. {
  744. return $this->osmId;
  745. }
  746. /**
  747. * @return string
  748. */
  749. public function sinacId(): ?string
  750. {
  751. return $this->sinacId;
  752. }
  753. /**
  754. * @return string
  755. */
  756. public function sinacSlug(): ?string
  757. {
  758. return $this->sinacSlug;
  759. }
  760. /**
  761. * @return bool
  762. */
  763. public function dispersed(): bool
  764. {
  765. return $this->dispersed;
  766. }
  767. /**
  768. * @return array|null
  769. */
  770. public function osmData(): ?array
  771. {
  772. return $this->osmData;
  773. }
  774. /**
  775. * @return bool
  776. */
  777. public function validatedByAdmin(): bool
  778. {
  779. return $this->validatedByAdmin;
  780. }
  781. /**
  782. * @return float
  783. */
  784. public function latitude(): float
  785. {
  786. if ($this->latitude == null) {
  787. return 0;
  788. }
  789. return $this->latitude;
  790. }
  791. /**
  792. * @return float
  793. */
  794. public function longitude(): float
  795. {
  796. if ($this->longitude == null) {
  797. return 0;
  798. }
  799. return $this->longitude;
  800. }
  801. /**
  802. * @return string
  803. */
  804. public function slug(): string
  805. {
  806. return $this->slug;
  807. }
  808. /**
  809. * @return Point
  810. */
  811. public function geoPoint()
  812. {
  813. return $this->geoPoint;
  814. }
  815. /**
  816. * @return bool
  817. */
  818. public function confirmedGeoposition(): bool
  819. {
  820. return $this->confirmedGeoposition;
  821. }
  822. /**
  823. * @return array
  824. */
  825. public function totalColor(): ?array
  826. {
  827. return $this->totalColor;
  828. }
  829. /**
  830. * @return array
  831. */
  832. public function totalTaste(): ?array
  833. {
  834. return $this->totalTaste;
  835. }
  836. /**
  837. * @return array
  838. */
  839. public function totalSmell(): ?array
  840. {
  841. return $this->totalSmell;
  842. }
  843. /**
  844. * @return array
  845. */
  846. public function totalTurbidity(): ?array
  847. {
  848. return $this->totalTurbidity;
  849. }
  850. /**
  851. * @return array
  852. */
  853. public function totalGeneral(): ?array
  854. {
  855. return $this->totalGeneral;
  856. }
  857. /**
  858. * @return User|null
  859. */
  860. public function createdBy(): ?User
  861. {
  862. return $this->createdBy;
  863. }
  864. /**
  865. * @return DistributionNetwork
  866. */
  867. public function distributionNetwork(): ?DistributionNetwork
  868. {
  869. return $this->distributionNetwork;
  870. }
  871. /**
  872. * @return Town
  873. */
  874. public function town(): Town
  875. {
  876. return $this->town;
  877. }
  878. /**
  879. * @return TownLocation|null
  880. */
  881. public function townLocation(): ?TownLocation
  882. {
  883. return $this->townLocation;
  884. }
  885. /**
  886. * @return District
  887. */
  888. public function district(): ?District
  889. {
  890. return $this->district;
  891. }
  892. /**
  893. * @return null|Ubication
  894. */
  895. public function ubication(): ?Ubication
  896. {
  897. return $this->ubication;
  898. }
  899. /**
  900. * @return null|Establishment
  901. */
  902. public function establishment(): ?Establishment
  903. {
  904. return $this->establishment;
  905. }
  906. /**
  907. * @return Collection
  908. */
  909. public function analyticals(): Collection
  910. {
  911. return $this->analyticals;
  912. }
  913. /**
  914. * @return Collection
  915. */
  916. public function valorations(): Collection
  917. {
  918. return $this->valorations;
  919. }
  920. /**
  921. * @return \DateTime
  922. */
  923. public function createdAt(): \DateTime
  924. {
  925. return $this->createdAt;
  926. }
  927. /**
  928. * @return \DateTime
  929. */
  930. public function updatedAt(): \DateTime
  931. {
  932. return $this->updatedAt;
  933. }
  934. /**
  935. * @return string
  936. */
  937. public function whaterStatus(): string
  938. {
  939. return $this->whaterStatus;
  940. }
  941. /**
  942. * @return string
  943. */
  944. public function whaterStatusAt(): ?\DateTime
  945. {
  946. return $this->whaterStatusAt;
  947. }
  948. /**
  949. * @return \DateTime
  950. */
  951. public function lastProfessionalValorationAt(): ?\DateTime
  952. {
  953. return $this->lastProfessionalValorationAt;
  954. }
  955. /**
  956. * @return int
  957. */
  958. public function countProfessionalGeneral(): int
  959. {
  960. return $this->countProfessionalGeneral;
  961. }
  962. /**
  963. * @return float
  964. */
  965. public function averageProfessionalGeneral(): ?float
  966. {
  967. return $this->averageProfessionalGeneral;
  968. }
  969. /**
  970. * @return \DateTime
  971. */
  972. public function lastUserValorationAt(): ?\DateTime
  973. {
  974. return $this->lastUserValorationAt;
  975. }
  976. /**
  977. * @return int
  978. */
  979. public function countUserGeneral(): int
  980. {
  981. return $this->countUserGeneral;
  982. }
  983. /**
  984. * @return float
  985. */
  986. public function averageUserGeneral(): ?float
  987. {
  988. return $this->averageUserGeneral;
  989. }
  990. /**
  991. * @return bool
  992. */
  993. public function notifyWhaterStatusChange(): bool
  994. {
  995. return $this->notifyWhaterStatusChange;
  996. }
  997. public function updateWhaterStatus(?string $whaterStatus = null)
  998. {
  999. $oldWhaterStatus = $this->whaterStatus;
  1000. if ($whaterStatus == null) {
  1001. // last analytical status
  1002. $ws = WhaterPoint::WP_STATUS_UNKNOWN;
  1003. $arrAnalyticals = $this->analyticals()->toArray();
  1004. usort($arrAnalyticals, function ($a1, $a2) {
  1005. return $a1->analyzedAt() < $a2->analyzedAt();
  1006. });
  1007. if (count($arrAnalyticals) > 0) {
  1008. $lastAnalytical = $arrAnalyticals[0];
  1009. switch ($lastAnalytical->result()) {
  1010. case Analytical::ANALYTICAL_NOT_SUITABLE:
  1011. $ws = WhaterPoint::WP_STATUS_NOT_SUITABLE;
  1012. break;
  1013. case Analytical::ANALYTICAL_SUITABLE:
  1014. $ws = WhaterPoint::WP_STATUS_SUITABLE;
  1015. break;
  1016. case Analytical::ANALYTICAL_UNKNOWN:
  1017. break;
  1018. default:
  1019. break;
  1020. }
  1021. $this->whaterStatus = $ws;
  1022. if ($oldWhaterStatus != $ws) {
  1023. $this->notifyWhaterStatusChange = true;
  1024. } else {
  1025. $this->notifyWhaterStatusChange = false;
  1026. }
  1027. $this->whaterStatusAt = $lastAnalytical->analyzedAt();
  1028. }
  1029. // Check if the distribution network has a more recent status
  1030. if ($this->distributionNetwork() != null) {
  1031. $distributionNetwork = $this->distributionNetwork();
  1032. $lastDistributionNetworkStatusesAndDates = [
  1033. [
  1034. 'date' => $distributionNetwork->whaterStatusByGovernmentUpdatedAt(),
  1035. 'status' => $distributionNetwork->whaterStatusByGovernment(),
  1036. ],
  1037. [
  1038. 'date' => $distributionNetwork->whaterStatusByOperatorUpdatedAt(),
  1039. 'status' => $distributionNetwork->whaterStatusByOperator(),
  1040. ],
  1041. [
  1042. 'date' => $distributionNetwork->whaterStatusByUbicationUpdatedAt(),
  1043. 'status' => $distributionNetwork->whaterStatusByUbication(),
  1044. ],
  1045. ];
  1046. $lastDistributionNetworkStatus = null;
  1047. $lastDistributionNetworkDate = null;
  1048. foreach ($lastDistributionNetworkStatusesAndDates as $lastDistributionNetworkStatusAndDate) {
  1049. $lastStatus = $lastDistributionNetworkStatusAndDate['status'];
  1050. $lastDate = $lastDistributionNetworkStatusAndDate['date'];
  1051. if ($lastDate != null) {
  1052. if ($lastDistributionNetworkDate === null || $lastDate > $lastDistributionNetworkDate) {
  1053. $lastDistributionNetworkDate = $lastDate;
  1054. $lastDistributionNetworkStatus = $lastStatus;
  1055. }
  1056. }
  1057. }
  1058. if ($this->whaterStatusAt == null) {
  1059. $this->whaterStatusAt = $lastDistributionNetworkDate;
  1060. switch ($lastDistributionNetworkStatus) {
  1061. case DistributionNetwork::DN_STATUS_SUITABLE:
  1062. $this->whaterStatus = WhaterPoint::WP_STATUS_SUITABLE;
  1063. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_SUITABLE) {
  1064. $this->notifyWhaterStatusChange = true;
  1065. } else {
  1066. $this->notifyWhaterStatusChange = false;
  1067. }
  1068. break;
  1069. case DistributionNetwork::DN_STATUS_NOT_SUITABLE;
  1070. $this->whaterStatus = WhaterPoint::WP_STATUS_NOT_SUITABLE;
  1071. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_NOT_SUITABLE) {
  1072. $this->notifyWhaterStatusChange = true;
  1073. } else {
  1074. $this->notifyWhaterStatusChange = false;
  1075. }
  1076. break;
  1077. case DistributionNetwork::DN_STATUS_WITH_RESTRICTIONS:
  1078. $this->whaterStatus = WhaterPoint::WP_STATUS_WITH_RESTRICTIONS;
  1079. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_WITH_RESTRICTIONS) {
  1080. $this->notifyWhaterStatusChange = true;
  1081. } else {
  1082. $this->notifyWhaterStatusChange = false;
  1083. }
  1084. break;
  1085. case DistributionNetwork::DN_STATUS_UNKNOWN;
  1086. $this->whaterStatus = WhaterPoint::WP_STATUS_UNKNOWN;
  1087. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_UNKNOWN) {
  1088. $this->notifyWhaterStatusChange = true;
  1089. } else {
  1090. $this->notifyWhaterStatusChange = false;
  1091. }
  1092. break;
  1093. default:
  1094. $this->whaterStatus = WhaterPoint::WP_STATUS_UNKNOWN;
  1095. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_UNKNOWN) {
  1096. $this->notifyWhaterStatusChange = true;
  1097. } else {
  1098. $this->notifyWhaterStatusChange = false;
  1099. }
  1100. }
  1101. } else if ($lastDistributionNetworkDate != null && $lastDistributionNetworkDate > $this->whaterStatusAt) {
  1102. $this->whaterStatusAt = $lastDistributionNetworkDate;
  1103. switch ($lastDistributionNetworkStatus) {
  1104. case DistributionNetwork::DN_STATUS_SUITABLE:
  1105. $this->whaterStatus = WhaterPoint::WP_STATUS_SUITABLE;
  1106. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_SUITABLE) {
  1107. $this->notifyWhaterStatusChange = true;
  1108. } else {
  1109. $this->notifyWhaterStatusChange = false;
  1110. }
  1111. break;
  1112. case DistributionNetwork::DN_STATUS_NOT_SUITABLE;
  1113. $this->whaterStatus = WhaterPoint::WP_STATUS_NOT_SUITABLE;
  1114. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_NOT_SUITABLE) {
  1115. $this->notifyWhaterStatusChange = true;
  1116. } else {
  1117. $this->notifyWhaterStatusChange = false;
  1118. }
  1119. break;
  1120. case DistributionNetwork::DN_STATUS_WITH_RESTRICTIONS:
  1121. $this->whaterStatus = WhaterPoint::WP_STATUS_WITH_RESTRICTIONS;
  1122. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_WITH_RESTRICTIONS) {
  1123. $this->notifyWhaterStatusChange = true;
  1124. } else {
  1125. $this->notifyWhaterStatusChange = false;
  1126. }
  1127. break;
  1128. case DistributionNetwork::DN_STATUS_UNKNOWN;
  1129. $this->whaterStatus = WhaterPoint::WP_STATUS_UNKNOWN;
  1130. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_UNKNOWN) {
  1131. $this->notifyWhaterStatusChange = true;
  1132. } else {
  1133. $this->notifyWhaterStatusChange = false;
  1134. }
  1135. break;
  1136. default:
  1137. $this->whaterStatus = WhaterPoint::WP_STATUS_UNKNOWN;
  1138. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_UNKNOWN) {
  1139. $this->notifyWhaterStatusChange = true;
  1140. } else {
  1141. $this->notifyWhaterStatusChange = false;
  1142. }
  1143. }
  1144. }
  1145. }
  1146. } else {
  1147. switch ($whaterStatus) {
  1148. case WhaterPoint::WP_STATUS_NOT_SUITABLE:
  1149. case WhaterPoint::WP_STATUS_SUITABLE:
  1150. case WhaterPoint::WP_STATUS_UNKNOWN:
  1151. case WhaterPoint::WP_STATUS_WITH_RESTRICTIONS:
  1152. $this->whaterStatus = $whaterStatus;
  1153. if ($oldWhaterStatus != $whaterStatus) {
  1154. $this->notifyWhaterStatusChange = true;
  1155. } else {
  1156. $this->notifyWhaterStatusChange = false;
  1157. }
  1158. $this->whaterStatusAt = new \DateTime();
  1159. break;
  1160. default:
  1161. throw new InvalidWhaterStatusException();
  1162. }
  1163. }
  1164. }
  1165. public function editWhaterPoint(
  1166. string $name,
  1167. string $type,
  1168. Town $town,
  1169. ?DistributionNetwork $distributionNetwork = null,
  1170. ?Ubication $ubication = null,
  1171. ?Establishment $establishment = null
  1172. ) {
  1173. $this->name = $name;
  1174. $this->type = $type;
  1175. $this->town = $town;
  1176. $this->distributionNetwork = $distributionNetwork;
  1177. $this->ubication = $ubication;
  1178. $this->establishment = $establishment;
  1179. $this->updatedAt = new \DateTime();
  1180. return $this;
  1181. }
  1182. public function updateGeoposition(
  1183. float $latitude,
  1184. float $longitude
  1185. ) {
  1186. $this->latitude = $latitude;
  1187. $this->longitude = $longitude;
  1188. $this->geoPoint()->setLongitude($longitude);
  1189. $this->geoPoint()->setLatitude($latitude);
  1190. $this->updatedAt = new \DateTime();
  1191. return $this;
  1192. }
  1193. /**
  1194. * @return bool
  1195. */
  1196. public function equals(WhaterPoint $whaterPoint)
  1197. {
  1198. return $this->id() === $whaterPoint->id();
  1199. }
  1200. public function images(): Collection
  1201. {
  1202. return $this->images;
  1203. }
  1204. /**
  1205. * @return ?WhaterDevice
  1206. */
  1207. public function whaterDevice(): ?WhaterDevice
  1208. {
  1209. return $this->whaterDevice;
  1210. }
  1211. public function addDevice(WhaterDevice $whaterDevice)
  1212. {
  1213. $this->whaterDevice = $whaterDevice;
  1214. }
  1215. /**
  1216. * @return ?Product
  1217. */
  1218. public function product(): ?Product
  1219. {
  1220. return $this->product;
  1221. }
  1222. public function addProduct(Product $product)
  1223. {
  1224. $this->product = $product;
  1225. }
  1226. /**
  1227. * @return Collection
  1228. */
  1229. public function articles(): Collection
  1230. {
  1231. return $this->articles;
  1232. }
  1233. /**
  1234. * @return Collection
  1235. */
  1236. public function whaterWidgetLogs(): Collection
  1237. {
  1238. return $this->whaterWidgetLogs;
  1239. }
  1240. /**
  1241. * @return WhaterOrganization
  1242. */
  1243. public function responsableOrganizationCorporative(): ?WhaterOrganization
  1244. {
  1245. return $this->responsableOrganizationCorporative;
  1246. }
  1247. /**
  1248. * @return WhaterOrganization
  1249. */
  1250. public function responsableOrganizationUbication(): ?WhaterOrganization
  1251. {
  1252. return $this->responsableOrganizationUbication;
  1253. }
  1254. /**
  1255. * @return Collection
  1256. */
  1257. public function whaterRefills(): ?Collection
  1258. {
  1259. return $this->whaterRefills;
  1260. }
  1261. }