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. * @param string $whaterPointId
  236. * @param string $name
  237. * @param float $latitude
  238. * @param float $longitude
  239. * @param DistributionNetwork $distributionNetwork
  240. */
  241. public function __construct(
  242. string $whaterPointId = null,
  243. string $name,
  244. string $type,
  245. Town $town,
  246. float $latitude,
  247. float $longitude,
  248. bool $confirmedGeoposition = false,
  249. User $createdBy = null,
  250. DistributionNetwork $distributionNetwork = null,
  251. Ubication $ubication = null,
  252. Establishment $establishment = null,
  253. TownLocation $townLocation = null
  254. ) {
  255. try {
  256. $this->uuid = Uuid::fromString($whaterPointId ?: Uuid::uuid4())->toString();
  257. } catch (\InvalidArgumentException $e) {
  258. throw new InvalidUUIDException();
  259. }
  260. $this->name = $name;
  261. $this->type = $type;
  262. $this->latitude = $latitude;
  263. $this->longitude = $longitude;
  264. $this->slug = $name;
  265. $this->geoPoint = new Point($latitude, $longitude);
  266. $this->geoPoint->setLongitude($longitude);
  267. $this->geoPoint->setLatitude($latitude);
  268. $this->confirmedGeoposition = $confirmedGeoposition;
  269. $this->town = $town;
  270. $this->distributionNetwork = $distributionNetwork;
  271. $this->createdBy = $createdBy;
  272. $this->analyticals = new ArrayCollection();
  273. $this->valorations = new ArrayCollection();
  274. $this->whaterWidgetLogs = new ArrayCollection();
  275. $this->whaterStatus = WhaterPoint::WP_STATUS_UNKNOWN;
  276. $this->dispersed = false;
  277. $this->createdAt = new \DateTime();
  278. $this->updatedAt = new \DateTime();
  279. $this->images = new ArrayCollection();
  280. $this->ubication = $ubication;
  281. $this->establishment = $establishment;
  282. $this->townLocation = $townLocation;
  283. $this->validatedByAdmin = false;
  284. $this->countUserGeneral = 0;
  285. $this->countProfessionalGeneral = 0;
  286. $this->averageProfessionalGeneral = null;
  287. $this->averageUserGeneral = null;
  288. $this->importNotes = [];
  289. $this->notifyWhaterStatusChange = false;
  290. $this->articles = new ArrayCollection();
  291. }
  292. public function updateWhaterpoint(
  293. string $name,
  294. string $slug,
  295. string $type,
  296. Town $town,
  297. float $latitude,
  298. float $longitude,
  299. bool $confirmedGeoposition,
  300. string $certification = null,
  301. User $createdBy = null,
  302. DistributionNetwork $distributionNetwork = null,
  303. Establishment $establishment = null,
  304. Ubication $ubication = null,
  305. WhaterDevice $whaterDevice = null,
  306. TownLocation $townLocation = null
  307. ) {
  308. $this->name = $name;
  309. $this->type = $type;
  310. $this->latitude = $latitude;
  311. $this->longitude = $longitude;
  312. $this->slug = $slug;
  313. if ($this->geoPoint() != null) {
  314. $this->geoPoint()->setLongitude($longitude);
  315. $this->geoPoint()->setLatitude($latitude);
  316. } else {
  317. $this->geoPoint = new Point($latitude, $longitude);
  318. }
  319. $this->confirmedGeoposition = $confirmedGeoposition;
  320. $this->town = $town;
  321. $this->distributionNetwork = $distributionNetwork;
  322. $this->whaterDevice = $whaterDevice;
  323. $this->establishment = $establishment;
  324. $this->ubication = $ubication;
  325. if ($this->whaterDevice != null) {
  326. $this->establishment = $this->whaterDevice->establishment();
  327. }
  328. if ($this->establishment != null) {
  329. $this->ubication = $this->establishment->ubication();
  330. }
  331. if ($this->ubication != null && !$this->ubication->town()->equals($this->town)) {
  332. throw new InvalidWhaterLocationException();
  333. }
  334. if ($createdBy != null) {
  335. $this->createdBy = $createdBy;
  336. }
  337. if ($townLocation != null) {
  338. $this->townLocation = $townLocation;
  339. }
  340. $this->certification = $certification;
  341. $this->updatedAt = new \DateTime();
  342. return $this;
  343. }
  344. public function setValidatedByAdmin(bool $validatedByAdmin)
  345. {
  346. $this->validatedByAdmin = $validatedByAdmin;
  347. }
  348. public function updateOsmData(
  349. string $osmId,
  350. array $osmData
  351. ) {
  352. $this->osmId = $osmId;
  353. $this->osmData = $osmData;
  354. $this->updatedAt = new \DateTime();
  355. return $this;
  356. }
  357. public function updateSinacData(
  358. string $sinacId,
  359. string $sinacSlug,
  360. bool $dispersed = false
  361. ) {
  362. $this->sinacId = $sinacId;
  363. $this->sinacSlug = $sinacSlug;
  364. $this->dispersed = $dispersed;
  365. $this->updatedAt = new \DateTime();
  366. return $this;
  367. }
  368. public function updateValorations()
  369. {
  370. $sumUserColor = array(
  371. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  372. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  373. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  374. );
  375. $sumUserTaste = 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. $sumUserSmell = 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. $sumUserTurbidity = 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. $sumUserGeneral = 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. $countUserColor = 0;
  396. $countUserTaste = 0;
  397. $countUserSmell = 0;
  398. $countUserTurbidity = 0;
  399. $countUserGeneral = 0;
  400. $sumProfessionalColor = array(
  401. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  402. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  403. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  404. );
  405. $sumProfessionalTaste = 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. $sumProfessionalSmell = 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. $sumProfessionalTurbidity = 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. $sumProfessionalGeneral = 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. $countProfessionalColor = 0;
  426. $countProfessionalTaste = 0;
  427. $countProfessionalSmell = 0;
  428. $countProfessionalTurbidity = 0;
  429. $countProfessionalGeneral = 0;
  430. foreach ($this->valorations() as $valoration) {
  431. if ($valoration->isProfessionalValoration()) {
  432. if (
  433. $this->lastProfessionalValorationAt == null ||
  434. $this->lastProfessionalValorationAt < $valoration->createdAt()
  435. ) {
  436. $this->lastProfessionalValorationAt = $valoration->createdAt();
  437. }
  438. if ($valoration->colorValoration() != null) {
  439. switch ($valoration->colorValoration()) {
  440. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  441. $sumProfessionalColor[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  442. break;
  443. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  444. $sumProfessionalColor[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  445. break;
  446. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  447. $sumProfessionalColor[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  448. break;
  449. default:
  450. break;
  451. }
  452. $countProfessionalColor++;
  453. }
  454. if ($valoration->tasteValoration() != null) {
  455. switch ($valoration->tasteValoration()) {
  456. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  457. $sumProfessionalTaste[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  458. break;
  459. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  460. $sumProfessionalTaste[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  461. break;
  462. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  463. $sumProfessionalTaste[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  464. break;
  465. default:
  466. break;
  467. }
  468. $countProfessionalTaste++;
  469. }
  470. if ($valoration->smellValoration() != null) {
  471. switch ($valoration->smellValoration()) {
  472. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  473. $sumProfessionalSmell[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  474. break;
  475. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  476. $sumProfessionalSmell[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  477. break;
  478. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  479. $sumProfessionalSmell[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  480. break;
  481. default:
  482. break;
  483. }
  484. $countProfessionalSmell++;
  485. }
  486. if ($valoration->turbidityValoration() != null) {
  487. switch ($valoration->turbidityValoration()) {
  488. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  489. $sumProfessionalTurbidity[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  490. break;
  491. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  492. $sumProfessionalTurbidity[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  493. break;
  494. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  495. $sumProfessionalTurbidity[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  496. break;
  497. default:
  498. break;
  499. }
  500. $countProfessionalTurbidity++;
  501. }
  502. if ($valoration->generalValoration() != null) {
  503. switch ($valoration->generalValoration()) {
  504. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  505. $sumProfessionalGeneral[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  506. break;
  507. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  508. $sumProfessionalGeneral[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  509. break;
  510. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  511. $sumProfessionalGeneral[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  512. break;
  513. default:
  514. break;
  515. }
  516. $countProfessionalGeneral++;
  517. }
  518. } else {
  519. if (
  520. $this->lastUserValorationAt == null ||
  521. $this->lastUserValorationAt < $valoration->createdAt()
  522. ) {
  523. $this->lastUserValorationAt = $valoration->createdAt();
  524. }
  525. if ($valoration->colorValoration() != null) {
  526. switch ($valoration->colorValoration()) {
  527. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  528. $sumUserColor[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  529. break;
  530. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  531. $sumUserColor[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  532. break;
  533. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  534. $sumUserColor[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  535. break;
  536. default:
  537. break;
  538. }
  539. $countUserColor++;
  540. }
  541. if ($valoration->tasteValoration() != null) {
  542. switch ($valoration->tasteValoration()) {
  543. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  544. $sumUserTaste[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  545. break;
  546. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  547. $sumUserTaste[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  548. break;
  549. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  550. $sumUserTaste[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  551. break;
  552. default:
  553. break;
  554. }
  555. $countUserTaste++;
  556. }
  557. if ($valoration->smellValoration() != null) {
  558. switch ($valoration->smellValoration()) {
  559. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  560. $sumUserSmell[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  561. break;
  562. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  563. $sumUserSmell[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  564. break;
  565. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  566. $sumUserSmell[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  567. break;
  568. default:
  569. break;
  570. }
  571. $countUserSmell++;
  572. }
  573. if ($valoration->turbidityValoration() != null) {
  574. switch ($valoration->turbidityValoration()) {
  575. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  576. $sumUserTurbidity[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  577. break;
  578. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  579. $sumUserTurbidity[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  580. break;
  581. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  582. $sumUserTurbidity[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  583. break;
  584. default:
  585. break;
  586. }
  587. $countUserTurbidity++;
  588. }
  589. if ($valoration->generalValoration() != null) {
  590. switch ($valoration->generalValoration()) {
  591. case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
  592. $sumUserGeneral[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
  593. break;
  594. case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
  595. $sumUserGeneral[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
  596. break;
  597. case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
  598. $sumUserGeneral[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
  599. break;
  600. default:
  601. break;
  602. }
  603. $countUserGeneral++;
  604. }
  605. }
  606. }
  607. // count valoration
  608. $this->countProfessionalGeneral = $countProfessionalGeneral;
  609. $this->countUserGeneral = $countUserGeneral;
  610. // calculate average valoration for color, taste, smell and turbidity
  611. if ($countProfessionalColor + $countUserColor > 0) {
  612. foreach ($sumUserColor as $key => $value) {
  613. $this->totalColor[$key] = $sumUserColor[$key] + $sumProfessionalColor[$key];
  614. }
  615. } else {
  616. $this->totalColor = array(
  617. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  618. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  619. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  620. );
  621. }
  622. if ($countProfessionalTaste + $countUserTaste > 0) {
  623. foreach ($sumUserTaste as $key => $value) {
  624. $this->totalTaste[$key] = $sumUserTaste[$key] + $sumProfessionalTaste[$key];
  625. }
  626. } else {
  627. $this->totalTaste = array(
  628. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  629. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  630. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  631. );
  632. }
  633. if ($countProfessionalSmell + $countUserSmell > 0) {
  634. foreach ($sumUserSmell as $key => $value) {
  635. $this->totalSmell[$key] = $sumUserSmell[$key] + $sumProfessionalSmell[$key];
  636. }
  637. } else {
  638. $this->totalSmell = array(
  639. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  640. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  641. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  642. );
  643. }
  644. if ($countProfessionalTurbidity + $countUserTurbidity > 0) {
  645. foreach ($sumUserTurbidity as $key => $value) {
  646. $this->totalTurbidity[$key] = $sumUserTurbidity[$key] + $sumProfessionalTurbidity[$key];
  647. }
  648. } else {
  649. $this->totalTurbidity = array(
  650. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  651. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  652. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  653. );
  654. }
  655. if ($countProfessionalGeneral + $countUserGeneral > 0) {
  656. foreach ($sumUserGeneral as $key => $value) {
  657. $this->totalGeneral[$key] = $sumUserGeneral[$key] + $sumProfessionalGeneral[$key];
  658. }
  659. } else {
  660. $this->totalGeneral = array(
  661. WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
  662. WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
  663. WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
  664. );
  665. }
  666. // calculate average valoration for users and professionals
  667. if ($this->countProfessionalGeneral > 0) {
  668. $averageProfessionalGeneral =
  669. (($sumProfessionalGeneral[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION] * 10) +
  670. ($sumProfessionalGeneral[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION] * 5) +
  671. ($sumProfessionalGeneral[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION] * 0)) / $this->countProfessionalGeneral;
  672. $this->averageProfessionalGeneral = round($averageProfessionalGeneral, 2);
  673. } else {
  674. $this->averageProfessionalGeneral = null;
  675. }
  676. if ($this->countUserGeneral > 0) {
  677. $averageUserGeneral =
  678. (($sumUserGeneral[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION] * 10) +
  679. ($sumUserGeneral[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION] * 5) +
  680. ($sumUserGeneral[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION] * 0)) / $this->countUserGeneral;
  681. $this->averageUserGeneral = round($averageUserGeneral, 2);
  682. } else {
  683. $this->averageUserGeneral = null;
  684. }
  685. }
  686. public function updateResponsables(
  687. WhaterOrganization $responsableOrganizationCorporative = null,
  688. WhaterOrganization $responsableOrganizationUbication = null
  689. ) {
  690. $this->responsableOrganizationCorporative = $responsableOrganizationCorporative;
  691. $this->responsableOrganizationUbication = $responsableOrganizationUbication;
  692. }
  693. public function updateImportNotes(
  694. array $importNotes,
  695. ) {
  696. $this->importNotes = $importNotes;
  697. return $this;
  698. }
  699. /**
  700. * @return string
  701. */
  702. public function id(): string
  703. {
  704. return $this->uuid;
  705. }
  706. /**
  707. * @return string
  708. */
  709. public function name(): string
  710. {
  711. return $this->name;
  712. }
  713. /**
  714. * @return array
  715. */
  716. public function importNotes(): array
  717. {
  718. return $this->importNotes;
  719. }
  720. /**
  721. * @return string
  722. */
  723. public function type(): string
  724. {
  725. return $this->type;
  726. }
  727. /**
  728. * @return string
  729. */
  730. public function certification(): ?string
  731. {
  732. return $this->certification;
  733. }
  734. /**
  735. * @return string
  736. */
  737. public function osmId(): ?string
  738. {
  739. return $this->osmId;
  740. }
  741. /**
  742. * @return string
  743. */
  744. public function sinacId(): ?string
  745. {
  746. return $this->sinacId;
  747. }
  748. /**
  749. * @return string
  750. */
  751. public function sinacSlug(): ?string
  752. {
  753. return $this->sinacSlug;
  754. }
  755. /**
  756. * @return bool
  757. */
  758. public function dispersed(): bool
  759. {
  760. return $this->dispersed;
  761. }
  762. /**
  763. * @return array|null
  764. */
  765. public function osmData(): ?array
  766. {
  767. return $this->osmData;
  768. }
  769. /**
  770. * @return bool
  771. */
  772. public function validatedByAdmin(): bool
  773. {
  774. return $this->validatedByAdmin;
  775. }
  776. /**
  777. * @return float
  778. */
  779. public function latitude(): float
  780. {
  781. if ($this->latitude == null) {
  782. return 0;
  783. }
  784. return $this->latitude;
  785. }
  786. /**
  787. * @return float
  788. */
  789. public function longitude(): float
  790. {
  791. if ($this->longitude == null) {
  792. return 0;
  793. }
  794. return $this->longitude;
  795. }
  796. /**
  797. * @return string
  798. */
  799. public function slug(): string
  800. {
  801. return $this->slug;
  802. }
  803. /**
  804. * @return Point
  805. */
  806. public function geoPoint()
  807. {
  808. return $this->geoPoint;
  809. }
  810. /**
  811. * @return bool
  812. */
  813. public function confirmedGeoposition(): bool
  814. {
  815. return $this->confirmedGeoposition;
  816. }
  817. /**
  818. * @return array
  819. */
  820. public function totalColor(): ?array
  821. {
  822. return $this->totalColor;
  823. }
  824. /**
  825. * @return array
  826. */
  827. public function totalTaste(): ?array
  828. {
  829. return $this->totalTaste;
  830. }
  831. /**
  832. * @return array
  833. */
  834. public function totalSmell(): ?array
  835. {
  836. return $this->totalSmell;
  837. }
  838. /**
  839. * @return array
  840. */
  841. public function totalTurbidity(): ?array
  842. {
  843. return $this->totalTurbidity;
  844. }
  845. /**
  846. * @return array
  847. */
  848. public function totalGeneral(): ?array
  849. {
  850. return $this->totalGeneral;
  851. }
  852. /**
  853. * @return User|null
  854. */
  855. public function createdBy(): ?User
  856. {
  857. return $this->createdBy;
  858. }
  859. /**
  860. * @return DistributionNetwork
  861. */
  862. public function distributionNetwork(): ?DistributionNetwork
  863. {
  864. return $this->distributionNetwork;
  865. }
  866. /**
  867. * @return Town
  868. */
  869. public function town(): Town
  870. {
  871. return $this->town;
  872. }
  873. /**
  874. * @return TownLocation|null
  875. */
  876. public function townLocation(): ?TownLocation
  877. {
  878. return $this->townLocation;
  879. }
  880. /**
  881. * @return District
  882. */
  883. public function district(): ?District
  884. {
  885. return $this->district;
  886. }
  887. /**
  888. * @return null|Ubication
  889. */
  890. public function ubication(): ?Ubication
  891. {
  892. return $this->ubication;
  893. }
  894. /**
  895. * @return null|Establishment
  896. */
  897. public function establishment(): ?Establishment
  898. {
  899. return $this->establishment;
  900. }
  901. /**
  902. * @return Collection
  903. */
  904. public function analyticals(): Collection
  905. {
  906. return $this->analyticals;
  907. }
  908. /**
  909. * @return Collection
  910. */
  911. public function valorations(): Collection
  912. {
  913. return $this->valorations;
  914. }
  915. /**
  916. * @return \DateTime
  917. */
  918. public function createdAt(): \DateTime
  919. {
  920. return $this->createdAt;
  921. }
  922. /**
  923. * @return \DateTime
  924. */
  925. public function updatedAt(): \DateTime
  926. {
  927. return $this->updatedAt;
  928. }
  929. /**
  930. * @return string
  931. */
  932. public function whaterStatus(): string
  933. {
  934. return $this->whaterStatus;
  935. }
  936. /**
  937. * @return string
  938. */
  939. public function whaterStatusAt(): ?\DateTime
  940. {
  941. return $this->whaterStatusAt;
  942. }
  943. /**
  944. * @return \DateTime
  945. */
  946. public function lastProfessionalValorationAt(): ?\DateTime
  947. {
  948. return $this->lastProfessionalValorationAt;
  949. }
  950. /**
  951. * @return int
  952. */
  953. public function countProfessionalGeneral(): int
  954. {
  955. return $this->countProfessionalGeneral;
  956. }
  957. /**
  958. * @return float
  959. */
  960. public function averageProfessionalGeneral(): ?float
  961. {
  962. return $this->averageProfessionalGeneral;
  963. }
  964. /**
  965. * @return \DateTime
  966. */
  967. public function lastUserValorationAt(): ?\DateTime
  968. {
  969. return $this->lastUserValorationAt;
  970. }
  971. /**
  972. * @return int
  973. */
  974. public function countUserGeneral(): int
  975. {
  976. return $this->countUserGeneral;
  977. }
  978. /**
  979. * @return float
  980. */
  981. public function averageUserGeneral(): ?float
  982. {
  983. return $this->averageUserGeneral;
  984. }
  985. /**
  986. * @return bool
  987. */
  988. public function notifyWhaterStatusChange(): bool
  989. {
  990. return $this->notifyWhaterStatusChange;
  991. }
  992. public function updateWhaterStatus(string $whaterStatus = null)
  993. {
  994. $oldWhaterStatus = $this->whaterStatus;
  995. if ($whaterStatus == null) {
  996. // last analytical status
  997. $ws = WhaterPoint::WP_STATUS_UNKNOWN;
  998. $arrAnalyticals = $this->analyticals()->toArray();
  999. usort($arrAnalyticals, function ($a1, $a2) {
  1000. return $a1->analyzedAt() < $a2->analyzedAt();
  1001. });
  1002. if (count($arrAnalyticals) > 0) {
  1003. $lastAnalytical = $arrAnalyticals[0];
  1004. switch ($lastAnalytical->result()) {
  1005. case Analytical::ANALYTICAL_NOT_SUITABLE:
  1006. $ws = WhaterPoint::WP_STATUS_NOT_SUITABLE;
  1007. break;
  1008. case Analytical::ANALYTICAL_SUITABLE:
  1009. $ws = WhaterPoint::WP_STATUS_SUITABLE;
  1010. break;
  1011. case Analytical::ANALYTICAL_UNKNOWN:
  1012. break;
  1013. default:
  1014. break;
  1015. }
  1016. $this->whaterStatus = $ws;
  1017. if ($oldWhaterStatus != $ws) {
  1018. $this->notifyWhaterStatusChange = true;
  1019. } else {
  1020. $this->notifyWhaterStatusChange = false;
  1021. }
  1022. $this->whaterStatusAt = $lastAnalytical->analyzedAt();
  1023. }
  1024. // Check if the distribution network has a more recent status
  1025. if ($this->distributionNetwork() != null) {
  1026. $distributionNetwork = $this->distributionNetwork();
  1027. $lastDistributionNetworkStatusesAndDates = [
  1028. [
  1029. 'date' => $distributionNetwork->whaterStatusByGovernmentUpdatedAt(),
  1030. 'status' => $distributionNetwork->whaterStatusByGovernment(),
  1031. ],
  1032. [
  1033. 'date' => $distributionNetwork->whaterStatusByOperatorUpdatedAt(),
  1034. 'status' => $distributionNetwork->whaterStatusByOperator(),
  1035. ],
  1036. [
  1037. 'date' => $distributionNetwork->whaterStatusByUbicationUpdatedAt(),
  1038. 'status' => $distributionNetwork->whaterStatusByUbication(),
  1039. ],
  1040. ];
  1041. $lastDistributionNetworkStatus = null;
  1042. $lastDistributionNetworkDate = null;
  1043. foreach ($lastDistributionNetworkStatusesAndDates as $lastDistributionNetworkStatusAndDate) {
  1044. $lastStatus = $lastDistributionNetworkStatusAndDate['status'];
  1045. $lastDate = $lastDistributionNetworkStatusAndDate['date'];
  1046. if ($lastDate != null) {
  1047. if ($lastDistributionNetworkDate === null || $lastDate > $lastDistributionNetworkDate) {
  1048. $lastDistributionNetworkDate = $lastDate;
  1049. $lastDistributionNetworkStatus = $lastStatus;
  1050. }
  1051. }
  1052. }
  1053. if ($this->whaterStatusAt == null) {
  1054. $this->whaterStatusAt = $lastDistributionNetworkDate;
  1055. switch ($lastDistributionNetworkStatus) {
  1056. case DistributionNetwork::DN_STATUS_SUITABLE:
  1057. $this->whaterStatus = WhaterPoint::WP_STATUS_SUITABLE;
  1058. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_SUITABLE) {
  1059. $this->notifyWhaterStatusChange = true;
  1060. } else {
  1061. $this->notifyWhaterStatusChange = false;
  1062. }
  1063. break;
  1064. case DistributionNetwork::DN_STATUS_NOT_SUITABLE;
  1065. $this->whaterStatus = WhaterPoint::WP_STATUS_NOT_SUITABLE;
  1066. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_NOT_SUITABLE) {
  1067. $this->notifyWhaterStatusChange = true;
  1068. } else {
  1069. $this->notifyWhaterStatusChange = false;
  1070. }
  1071. break;
  1072. case DistributionNetwork::DN_STATUS_WITH_RESTRICTIONS:
  1073. $this->whaterStatus = WhaterPoint::WP_STATUS_WITH_RESTRICTIONS;
  1074. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_WITH_RESTRICTIONS) {
  1075. $this->notifyWhaterStatusChange = true;
  1076. } else {
  1077. $this->notifyWhaterStatusChange = false;
  1078. }
  1079. break;
  1080. case DistributionNetwork::DN_STATUS_UNKNOWN;
  1081. $this->whaterStatus = WhaterPoint::WP_STATUS_UNKNOWN;
  1082. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_UNKNOWN) {
  1083. $this->notifyWhaterStatusChange = true;
  1084. } else {
  1085. $this->notifyWhaterStatusChange = false;
  1086. }
  1087. break;
  1088. default:
  1089. $this->whaterStatus = WhaterPoint::WP_STATUS_UNKNOWN;
  1090. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_UNKNOWN) {
  1091. $this->notifyWhaterStatusChange = true;
  1092. } else {
  1093. $this->notifyWhaterStatusChange = false;
  1094. }
  1095. }
  1096. } else if ($lastDistributionNetworkDate != null && $lastDistributionNetworkDate > $this->whaterStatusAt) {
  1097. $this->whaterStatusAt = $lastDistributionNetworkDate;
  1098. switch ($lastDistributionNetworkStatus) {
  1099. case DistributionNetwork::DN_STATUS_SUITABLE:
  1100. $this->whaterStatus = WhaterPoint::WP_STATUS_SUITABLE;
  1101. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_SUITABLE) {
  1102. $this->notifyWhaterStatusChange = true;
  1103. } else {
  1104. $this->notifyWhaterStatusChange = false;
  1105. }
  1106. break;
  1107. case DistributionNetwork::DN_STATUS_NOT_SUITABLE;
  1108. $this->whaterStatus = WhaterPoint::WP_STATUS_NOT_SUITABLE;
  1109. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_NOT_SUITABLE) {
  1110. $this->notifyWhaterStatusChange = true;
  1111. } else {
  1112. $this->notifyWhaterStatusChange = false;
  1113. }
  1114. break;
  1115. case DistributionNetwork::DN_STATUS_WITH_RESTRICTIONS:
  1116. $this->whaterStatus = WhaterPoint::WP_STATUS_WITH_RESTRICTIONS;
  1117. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_WITH_RESTRICTIONS) {
  1118. $this->notifyWhaterStatusChange = true;
  1119. } else {
  1120. $this->notifyWhaterStatusChange = false;
  1121. }
  1122. break;
  1123. case DistributionNetwork::DN_STATUS_UNKNOWN;
  1124. $this->whaterStatus = WhaterPoint::WP_STATUS_UNKNOWN;
  1125. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_UNKNOWN) {
  1126. $this->notifyWhaterStatusChange = true;
  1127. } else {
  1128. $this->notifyWhaterStatusChange = false;
  1129. }
  1130. break;
  1131. default:
  1132. $this->whaterStatus = WhaterPoint::WP_STATUS_UNKNOWN;
  1133. if ($oldWhaterStatus != WhaterPoint::WP_STATUS_UNKNOWN) {
  1134. $this->notifyWhaterStatusChange = true;
  1135. } else {
  1136. $this->notifyWhaterStatusChange = false;
  1137. }
  1138. }
  1139. }
  1140. }
  1141. } else {
  1142. switch ($whaterStatus) {
  1143. case WhaterPoint::WP_STATUS_NOT_SUITABLE:
  1144. case WhaterPoint::WP_STATUS_SUITABLE:
  1145. case WhaterPoint::WP_STATUS_UNKNOWN:
  1146. case WhaterPoint::WP_STATUS_WITH_RESTRICTIONS:
  1147. $this->whaterStatus = $whaterStatus;
  1148. if ($oldWhaterStatus != $whaterStatus) {
  1149. $this->notifyWhaterStatusChange = true;
  1150. } else {
  1151. $this->notifyWhaterStatusChange = false;
  1152. }
  1153. $this->whaterStatusAt = new \DateTime();
  1154. break;
  1155. default:
  1156. throw new InvalidWhaterStatusException();
  1157. }
  1158. }
  1159. }
  1160. public function editWhaterPoint(
  1161. string $name,
  1162. string $type,
  1163. Town $town,
  1164. DistributionNetwork $distributionNetwork = null,
  1165. Ubication $ubication = null,
  1166. Establishment $establishment = null
  1167. ) {
  1168. $this->name = $name;
  1169. $this->type = $type;
  1170. $this->town = $town;
  1171. $this->distributionNetwork = $distributionNetwork;
  1172. $this->ubication = $ubication;
  1173. $this->establishment = $establishment;
  1174. $this->updatedAt = new \DateTime();
  1175. return $this;
  1176. }
  1177. public function updateGeoposition(
  1178. float $latitude,
  1179. float $longitude
  1180. ) {
  1181. $this->latitude = $latitude;
  1182. $this->longitude = $longitude;
  1183. $this->geoPoint()->setLongitude($longitude);
  1184. $this->geoPoint()->setLatitude($latitude);
  1185. $this->updatedAt = new \DateTime();
  1186. return $this;
  1187. }
  1188. /**
  1189. * @return bool
  1190. */
  1191. public function equals(WhaterPoint $whaterPoint)
  1192. {
  1193. return $this->id() === $whaterPoint->id();
  1194. }
  1195. public function images(): Collection
  1196. {
  1197. return $this->images;
  1198. }
  1199. /**
  1200. * @return ?WhaterDevice
  1201. */
  1202. public function whaterDevice(): ?WhaterDevice
  1203. {
  1204. return $this->whaterDevice;
  1205. }
  1206. public function addDevice(WhaterDevice $whaterDevice)
  1207. {
  1208. $this->whaterDevice = $whaterDevice;
  1209. }
  1210. /**
  1211. * @return ?Product
  1212. */
  1213. public function product(): ?Product
  1214. {
  1215. return $this->product;
  1216. }
  1217. public function addProduct(Product $product)
  1218. {
  1219. $this->product = $product;
  1220. }
  1221. /**
  1222. * @return Collection
  1223. */
  1224. public function articles(): Collection
  1225. {
  1226. return $this->articles;
  1227. }
  1228. /**
  1229. * @return Collection
  1230. */
  1231. public function whaterWidgetLogs(): Collection
  1232. {
  1233. return $this->whaterWidgetLogs;
  1234. }
  1235. /**
  1236. * @return WhaterOrganization
  1237. */
  1238. public function responsableOrganizationCorporative(): ?WhaterOrganization
  1239. {
  1240. return $this->responsableOrganizationCorporative;
  1241. }
  1242. /**
  1243. * @return WhaterOrganization
  1244. */
  1245. public function responsableOrganizationUbication(): ?WhaterOrganization
  1246. {
  1247. return $this->responsableOrganizationUbication;
  1248. }
  1249. }