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