<?php
namespace Whater\Domain\Whater\Model;
use BornFree\TacticianDomainEvent\Recorder\ContainsRecordedEvents;
use BornFree\TacticianDomainEvent\Recorder\EventRecorderCapabilities;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Ramsey\Uuid\Uuid;
use Whater\Domain\Common\Exception\InvalidUUIDException;
use Whater\Domain\User\Model\User;
use Whater\Domain\Whater\Exception\InvalidWhaterLocationException;
use Whater\Domain\Zones\Model\Ubication;
use Whater\Domain\Zones\Model\Town;
use Whater\Domain\Zones\Model\Establishment;
use Whater\Domain\Whater\Model\WhaterDevice;
use LongitudeOne\Spatial\PHP\Types\Geometry\Point;
use Whater\Domain\Product\Model\Product;
use Whater\Domain\Whater\Exception\InvalidWhaterStatusException;
use Whater\Domain\Zones\Model\District;
use Whater\Domain\Zones\Model\TownLocation;
/**
* Class WhaterPoint
*
* @package Whater\Domain\Whater\Model
*/
class WhaterPoint implements ContainsRecordedEvents
{
use EventRecorderCapabilities;
const WP_TYPE_FOUNTAIN = 'WP_TYPE_FOUNTAIN';
const WP_TYPE_TAP = 'WP_TYPE_TAP';
const WP_TYPE_DEPOSIT = 'WP_TYPE_DEPOSIT';
const WP_TYPE_BOTTLED = 'WP_TYPE_BOTTLED';
const WP_TYPE_TREATED = 'WP_TYPE_TREATED';
const WP_TYPE_SPRING = 'WP_TYPE_SPRING';
const WP_STATUS_SUITABLE = 'WP_STATUS_SUITABLE';
const WP_STATUS_NOT_SUITABLE = 'WP_STATUS_NOT_SUITABLE';
const WP_STATUS_WITH_RESTRICTIONS = 'WP_STATUS_WITH_RESTRICTIONS';
const WP_STATUS_UNKNOWN = 'WP_STATUS_UNKNOWN';
/**
* @var string
*/
private $uuid;
/**
* @var DistributionNetwork
*/
private $distributionNetwork;
/**
* @var Town
*/
private $town;
/**
* @var TownLocation
*/
private $townLocation;
/**
* @var District
*/
private $district;
/**
* @var Ubication
*/
private $ubication;
/**
* @var Establishment
*/
private $establishment;
/**
* @var string
*/
private $name;
/**
* @var float
*/
private $latitude;
/**
* @var float
*/
private $longitude;
/**
* @var string
*/
private $slug;
/**
* @var Point
*/
private $geoPoint;
/**
* @var bool
*/
private $confirmedGeoposition;
/**
* @var string
*/
private $type;
/**
* @var WhaterOrganization
*/
private $responsableOrganizationCorporative;
/**
* @var WhaterOrganization
*/
private $responsableOrganizationUbication;
/**
* @var User
*/
private $createdBy;
/**
* @var string
*/
private $certification;
/**
* @var \DateTime
*/
private $createdAt;
/**
* @var \DateTime
*/
private $updatedAt;
/**
* @var Collection
*/
private $analyticals;
/**
* @var Collection
*/
private $valorations;
/**
* @var array
*/
private $totalColor;
/**
* @var array
*/
private $totalTaste;
/**
* @var array
*/
private $totalSmell;
/**
* @var array
*/
private $totalTurbidity;
/**
* @var array
*/
private $totalGeneral;
/**
* @var string
*/
private $whaterStatus;
/**
* @var \DateTime
*/
private $whaterStatusAt;
/**
* @var \DateTime
*/
private $lastProfessionalValorationAt;
/**
* @var int
*/
private $countProfessionalGeneral;
/**
* @var float
*/
private $averageProfessionalGeneral;
/**
* @var \DateTime
*/
private $lastUserValorationAt;
/**
* @var int
*/
private $countUserGeneral;
/**
* @var float
*/
private $averageUserGeneral;
/**
* @var string
*/
private $osmId;
/**
* @var string
*/
private $sinacId;
/**
* @var string
*/
private $sinacSlug;
/**
* @var array
*/
private $osmData;
/**
* @var bool
*/
private $dispersed;
/**
* @var Collection
*/
private $images;
/**
* @var Collection
*/
private $organizationLicenses;
/**
* @var WhaterDevice
*/
private $whaterDevice;
/**
* @var Product
*/
private $product;
/**
* @var array
*/
private $importNotes;
/**
* @var bool
*/
private $validatedByAdmin;
/**
* @var bool
*/
private $notifyWhaterStatusChange;
/**
* @var Collection
*/
private $articles;
/**
* @param string $whaterPointId
* @param string $name
* @param float $latitude
* @param float $longitude
* @param DistributionNetwork $distributionNetwork
*/
public function __construct(
string $whaterPointId = null,
string $name,
string $type,
Town $town,
float $latitude,
float $longitude,
bool $confirmedGeoposition = false,
User $createdBy = null,
DistributionNetwork $distributionNetwork = null,
Ubication $ubication = null,
Establishment $establishment = null,
TownLocation $townLocation = null
) {
try {
$this->uuid = Uuid::fromString($whaterPointId ?: Uuid::uuid4())->toString();
} catch (\InvalidArgumentException $e) {
throw new InvalidUUIDException();
}
$this->name = $name;
$this->type = $type;
$this->latitude = $latitude;
$this->longitude = $longitude;
$this->slug = $name;
$this->geoPoint = new Point($latitude, $longitude);
$this->geoPoint->setLongitude($longitude);
$this->geoPoint->setLatitude($latitude);
$this->confirmedGeoposition = $confirmedGeoposition;
$this->town = $town;
$this->distributionNetwork = $distributionNetwork;
$this->createdBy = $createdBy;
$this->analyticals = new ArrayCollection();
$this->valorations = new ArrayCollection();
$this->whaterStatus = WhaterPoint::WP_STATUS_UNKNOWN;
$this->dispersed = false;
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
$this->images = new ArrayCollection();
$this->ubication = $ubication;
$this->establishment = $establishment;
$this->townLocation = $townLocation;
$this->validatedByAdmin = false;
$this->countUserGeneral = 0;
$this->countProfessionalGeneral = 0;
$this->averageProfessionalGeneral = null;
$this->averageUserGeneral = null;
$this->importNotes = [];
$this->notifyWhaterStatusChange = false;
$this->articles = new ArrayCollection();
}
public function updateWhaterpoint(
string $name,
string $slug,
string $type,
Town $town,
float $latitude,
float $longitude,
bool $confirmedGeoposition,
string $certification = null,
User $createdBy = null,
DistributionNetwork $distributionNetwork = null,
Establishment $establishment = null,
Ubication $ubication = null,
WhaterDevice $whaterDevice = null,
TownLocation $townLocation = null
) {
$this->name = $name;
$this->type = $type;
$this->latitude = $latitude;
$this->longitude = $longitude;
$this->slug = $slug;
if ($this->geoPoint() != null) {
$this->geoPoint()->setLongitude($longitude);
$this->geoPoint()->setLatitude($latitude);
} else {
$this->geoPoint = new Point($latitude, $longitude);
}
$this->confirmedGeoposition = $confirmedGeoposition;
$this->town = $town;
$this->distributionNetwork = $distributionNetwork;
$this->whaterDevice = $whaterDevice;
$this->establishment = $establishment;
$this->ubication = $ubication;
if ($this->whaterDevice != null) {
$this->establishment = $this->whaterDevice->establishment();
}
if ($this->establishment != null) {
$this->ubication = $this->establishment->ubication();
}
if ($this->ubication != null && !$this->ubication->town()->equals($this->town)) {
throw new InvalidWhaterLocationException();
}
if ($createdBy != null) {
$this->createdBy = $createdBy;
}
if ($townLocation != null) {
$this->townLocation = $townLocation;
}
$this->certification = $certification;
$this->updatedAt = new \DateTime();
return $this;
}
public function setValidatedByAdmin(bool $validatedByAdmin)
{
$this->validatedByAdmin = $validatedByAdmin;
}
public function updateOsmData(
string $osmId,
array $osmData
) {
$this->osmId = $osmId;
$this->osmData = $osmData;
$this->updatedAt = new \DateTime();
return $this;
}
public function updateSinacData(
string $sinacId,
string $sinacSlug,
bool $dispersed = false
) {
$this->sinacId = $sinacId;
$this->sinacSlug = $sinacSlug;
$this->dispersed = $dispersed;
$this->updatedAt = new \DateTime();
return $this;
}
public function updateValorations()
{
$sumUserColor = array(
WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
$sumUserTaste = array(
WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
$sumUserSmell = array(
WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
$sumUserTurbidity = array(
WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
$sumUserGeneral = array(
WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
$countUserColor = 0;
$countUserTaste = 0;
$countUserSmell = 0;
$countUserTurbidity = 0;
$countUserGeneral = 0;
$sumProfessionalColor = array(
WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
$sumProfessionalTaste = array(
WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
$sumProfessionalSmell = array(
WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
$sumProfessionalTurbidity = array(
WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
$sumProfessionalGeneral = array(
WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
$countProfessionalColor = 0;
$countProfessionalTaste = 0;
$countProfessionalSmell = 0;
$countProfessionalTurbidity = 0;
$countProfessionalGeneral = 0;
foreach ($this->valorations() as $valoration) {
if ($valoration->isProfessionalValoration()) {
if (
$this->lastProfessionalValorationAt == null ||
$this->lastProfessionalValorationAt < $valoration->createdAt()
) {
$this->lastProfessionalValorationAt = $valoration->createdAt();
}
if ($valoration->colorValoration() != null) {
switch ($valoration->colorValoration()) {
case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
$sumProfessionalColor[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumProfessionalColor[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumProfessionalColor[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
break;
default:
break;
}
$countProfessionalColor++;
}
if ($valoration->tasteValoration() != null) {
switch ($valoration->tasteValoration()) {
case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
$sumProfessionalTaste[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumProfessionalTaste[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumProfessionalTaste[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
break;
default:
break;
}
$countProfessionalTaste++;
}
if ($valoration->smellValoration() != null) {
switch ($valoration->smellValoration()) {
case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
$sumProfessionalSmell[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumProfessionalSmell[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumProfessionalSmell[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
break;
default:
break;
}
$countProfessionalSmell++;
}
if ($valoration->turbidityValoration() != null) {
switch ($valoration->turbidityValoration()) {
case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
$sumProfessionalTurbidity[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumProfessionalTurbidity[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumProfessionalTurbidity[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
break;
default:
break;
}
$countProfessionalTurbidity++;
}
if ($valoration->generalValoration() != null) {
switch ($valoration->generalValoration()) {
case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
$sumProfessionalGeneral[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumProfessionalGeneral[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumProfessionalGeneral[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
break;
default:
break;
}
$countProfessionalGeneral++;
}
} else {
if (
$this->lastUserValorationAt == null ||
$this->lastUserValorationAt < $valoration->createdAt()
) {
$this->lastUserValorationAt = $valoration->createdAt();
}
if ($valoration->colorValoration() != null) {
switch ($valoration->colorValoration()) {
case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
$sumUserColor[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumUserColor[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumUserColor[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
break;
default:
break;
}
$countUserColor++;
}
if ($valoration->tasteValoration() != null) {
switch ($valoration->tasteValoration()) {
case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
$sumUserTaste[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumUserTaste[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumUserTaste[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
break;
default:
break;
}
$countUserTaste++;
}
if ($valoration->smellValoration() != null) {
switch ($valoration->smellValoration()) {
case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
$sumUserSmell[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumUserSmell[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumUserSmell[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
break;
default:
break;
}
$countUserSmell++;
}
if ($valoration->turbidityValoration() != null) {
switch ($valoration->turbidityValoration()) {
case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
$sumUserTurbidity[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumUserTurbidity[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumUserTurbidity[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
break;
default:
break;
}
$countUserTurbidity++;
}
if ($valoration->generalValoration() != null) {
switch ($valoration->generalValoration()) {
case WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION:
$sumUserGeneral[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumUserGeneral[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumUserGeneral[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
break;
default:
break;
}
$countUserGeneral++;
}
}
}
// count valoration
$this->countProfessionalGeneral = $countProfessionalGeneral;
$this->countUserGeneral = $countUserGeneral;
// calculate average valoration for color, taste, smell and turbidity
if ($countProfessionalColor + $countUserColor > 0) {
foreach ($sumUserColor as $key => $value) {
$this->totalColor[$key] = $sumUserColor[$key] + $sumProfessionalColor[$key];
}
} else {
$this->totalColor = array(
WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
}
if ($countProfessionalTaste + $countUserTaste > 0) {
foreach ($sumUserTaste as $key => $value) {
$this->totalTaste[$key] = $sumUserTaste[$key] + $sumProfessionalTaste[$key];
}
} else {
$this->totalTaste = array(
WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
}
if ($countProfessionalSmell + $countUserSmell > 0) {
foreach ($sumUserSmell as $key => $value) {
$this->totalSmell[$key] = $sumUserSmell[$key] + $sumProfessionalSmell[$key];
}
} else {
$this->totalSmell = array(
WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
}
if ($countProfessionalTurbidity + $countUserTurbidity > 0) {
foreach ($sumUserTurbidity as $key => $value) {
$this->totalTurbidity[$key] = $sumUserTurbidity[$key] + $sumProfessionalTurbidity[$key];
}
} else {
$this->totalTurbidity = array(
WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
}
if ($countProfessionalGeneral + $countUserGeneral > 0) {
foreach ($sumUserGeneral as $key => $value) {
$this->totalGeneral[$key] = $sumUserGeneral[$key] + $sumProfessionalGeneral[$key];
}
} else {
$this->totalGeneral = array(
WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION => 0,
WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
}
// calculate average valoration for users and professionals
if ($this->countProfessionalGeneral > 0) {
$averageProfessionalGeneral =
(($sumProfessionalGeneral[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION] * 10) +
($sumProfessionalGeneral[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION] * 5) +
($sumProfessionalGeneral[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION] * 0)) / $this->countProfessionalGeneral;
$this->averageProfessionalGeneral = round($averageProfessionalGeneral, 2);
} else {
$this->averageProfessionalGeneral = null;
}
if ($this->countUserGeneral > 0) {
$averageUserGeneral =
(($sumUserGeneral[WhaterValoration::WP_QUALITY_ACCEPTABLE_VALORATION] * 10) +
($sumUserGeneral[WhaterValoration::WP_QUALITY_NEEDS_IMPROVEMENT_VALORATION] * 5) +
($sumUserGeneral[WhaterValoration::WP_QUALITY_NOT_ACCEPTABLE_VALORATION] * 0)) / $this->countUserGeneral;
$this->averageUserGeneral = round($averageUserGeneral, 2);
} else {
$this->averageUserGeneral = null;
}
}
public function updateResponsables(
WhaterOrganization $responsableOrganizationCorporative = null,
WhaterOrganization $responsableOrganizationUbication = null
) {
$this->responsableOrganizationCorporative = $responsableOrganizationCorporative;
$this->responsableOrganizationUbication = $responsableOrganizationUbication;
}
public function updateImportNotes(
array $importNotes,
) {
$this->importNotes = $importNotes;
return $this;
}
/**
* @return string
*/
public function id(): string
{
return $this->uuid;
}
/**
* @return string
*/
public function name(): string
{
return $this->name;
}
/**
* @return array
*/
public function importNotes(): array
{
return $this->importNotes;
}
/**
* @return string
*/
public function type(): string
{
return $this->type;
}
/**
* @return string
*/
public function certification(): ?string
{
return $this->certification;
}
/**
* @return string
*/
public function osmId(): ?string
{
return $this->osmId;
}
/**
* @return string
*/
public function sinacId(): ?string
{
return $this->sinacId;
}
/**
* @return string
*/
public function sinacSlug(): ?string
{
return $this->sinacSlug;
}
/**
* @return bool
*/
public function dispersed(): bool
{
return $this->dispersed;
}
/**
* @return array|null
*/
public function osmData(): ?array
{
return $this->osmData;
}
/**
* @return bool
*/
public function validatedByAdmin(): bool
{
return $this->validatedByAdmin;
}
/**
* @return float
*/
public function latitude(): float
{
if ($this->latitude == null) {
return 0;
}
return $this->latitude;
}
/**
* @return float
*/
public function longitude(): float
{
if ($this->longitude == null) {
return 0;
}
return $this->longitude;
}
/**
* @return string
*/
public function slug(): string
{
return $this->slug;
}
/**
* @return Point
*/
public function geoPoint()
{
return $this->geoPoint;
}
/**
* @return bool
*/
public function confirmedGeoposition(): bool
{
return $this->confirmedGeoposition;
}
/**
* @return array
*/
public function totalColor(): ?array
{
return $this->totalColor;
}
/**
* @return array
*/
public function totalTaste(): ?array
{
return $this->totalTaste;
}
/**
* @return array
*/
public function totalSmell(): ?array
{
return $this->totalSmell;
}
/**
* @return array
*/
public function totalTurbidity(): ?array
{
return $this->totalTurbidity;
}
/**
* @return array
*/
public function totalGeneral(): ?array
{
return $this->totalGeneral;
}
/**
* @return User|null
*/
public function createdBy(): ?User
{
return $this->createdBy;
}
/**
* @return DistributionNetwork
*/
public function distributionNetwork(): ?DistributionNetwork
{
return $this->distributionNetwork;
}
/**
* @return Town
*/
public function town(): Town
{
return $this->town;
}
/**
* @return TownLocation|null
*/
public function townLocation(): ?TownLocation
{
return $this->townLocation;
}
/**
* @return District
*/
public function district(): ?District
{
return $this->district;
}
/**
* @return null|Ubication
*/
public function ubication(): ?Ubication
{
return $this->ubication;
}
/**
* @return null|Establishment
*/
public function establishment(): ?Establishment
{
return $this->establishment;
}
/**
* @return Collection
*/
public function analyticals(): Collection
{
return $this->analyticals;
}
/**
* @return Collection
*/
public function valorations(): Collection
{
return $this->valorations;
}
/**
* @return \DateTime
*/
public function createdAt(): \DateTime
{
return $this->createdAt;
}
/**
* @return \DateTime
*/
public function updatedAt(): \DateTime
{
return $this->updatedAt;
}
/**
* @return string
*/
public function whaterStatus(): string
{
return $this->whaterStatus;
}
/**
* @return string
*/
public function whaterStatusAt(): ?\DateTime
{
return $this->whaterStatusAt;
}
/**
* @return \DateTime
*/
public function lastProfessionalValorationAt(): ?\DateTime
{
return $this->lastProfessionalValorationAt;
}
/**
* @return int
*/
public function countProfessionalGeneral(): int
{
return $this->countProfessionalGeneral;
}
/**
* @return float
*/
public function averageProfessionalGeneral(): ?float
{
return $this->averageProfessionalGeneral;
}
/**
* @return \DateTime
*/
public function lastUserValorationAt(): ?\DateTime
{
return $this->lastUserValorationAt;
}
/**
* @return int
*/
public function countUserGeneral(): int
{
return $this->countUserGeneral;
}
/**
* @return float
*/
public function averageUserGeneral(): ?float
{
return $this->averageUserGeneral;
}
/**
* @return bool
*/
public function notifyWhaterStatusChange(): bool
{
return $this->notifyWhaterStatusChange;
}
public function updateWhaterStatus(string $whaterStatus = null)
{
$oldWhaterStatus = $this->whaterStatus;
if ($whaterStatus == null) {
// last analytical status
$ws = WhaterPoint::WP_STATUS_UNKNOWN;
$arrAnalyticals = $this->analyticals()->toArray();
usort($arrAnalyticals, function ($a1, $a2) {
return $a1->analyzedAt() < $a2->analyzedAt();
});
if (count($arrAnalyticals) > 0) {
$lastAnalytical = $arrAnalyticals[0];
switch ($lastAnalytical->result()) {
case Analytical::ANALYTICAL_NOT_SUITABLE:
$ws = WhaterPoint::WP_STATUS_NOT_SUITABLE;
break;
case Analytical::ANALYTICAL_SUITABLE:
$ws = WhaterPoint::WP_STATUS_SUITABLE;
break;
case Analytical::ANALYTICAL_UNKNOWN:
break;
default:
break;
}
$this->whaterStatus = $ws;
if ($oldWhaterStatus != $ws) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
$this->whaterStatusAt = $lastAnalytical->analyzedAt();
}
// Check if the distribution network has a more recent status
if ($this->distributionNetwork() != null) {
$distributionNetwork = $this->distributionNetwork();
$lastDistributionNetworkStatusesAndDates = [
[
'date' => $distributionNetwork->whaterStatusByGovernmentUpdatedAt(),
'status' => $distributionNetwork->whaterStatusByGovernment(),
],
[
'date' => $distributionNetwork->whaterStatusByOperatorUpdatedAt(),
'status' => $distributionNetwork->whaterStatusByOperator(),
],
[
'date' => $distributionNetwork->whaterStatusByUbicationUpdatedAt(),
'status' => $distributionNetwork->whaterStatusByUbication(),
],
];
$lastDistributionNetworkStatus = null;
$lastDistributionNetworkDate = null;
foreach ($lastDistributionNetworkStatusesAndDates as $lastDistributionNetworkStatusAndDate) {
$lastStatus = $lastDistributionNetworkStatusAndDate['status'];
$lastDate = $lastDistributionNetworkStatusAndDate['date'];
if ($lastDate != null) {
if ($lastDistributionNetworkDate === null || $lastDate > $lastDistributionNetworkDate) {
$lastDistributionNetworkDate = $lastDate;
$lastDistributionNetworkStatus = $lastStatus;
}
}
}
if ($this->whaterStatusAt == null) {
$this->whaterStatusAt = $lastDistributionNetworkDate;
switch ($lastDistributionNetworkStatus) {
case DistributionNetwork::DN_STATUS_SUITABLE:
$this->whaterStatus = WhaterPoint::WP_STATUS_SUITABLE;
if ($oldWhaterStatus != WhaterPoint::WP_STATUS_SUITABLE) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
break;
case DistributionNetwork::DN_STATUS_NOT_SUITABLE;
$this->whaterStatus = WhaterPoint::WP_STATUS_NOT_SUITABLE;
if ($oldWhaterStatus != WhaterPoint::WP_STATUS_NOT_SUITABLE) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
break;
case DistributionNetwork::DN_STATUS_WITH_RESTRICTIONS:
$this->whaterStatus = WhaterPoint::WP_STATUS_WITH_RESTRICTIONS;
if ($oldWhaterStatus != WhaterPoint::WP_STATUS_WITH_RESTRICTIONS) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
break;
case DistributionNetwork::DN_STATUS_UNKNOWN;
$this->whaterStatus = WhaterPoint::WP_STATUS_UNKNOWN;
if ($oldWhaterStatus != WhaterPoint::WP_STATUS_UNKNOWN) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
break;
default:
$this->whaterStatus = WhaterPoint::WP_STATUS_UNKNOWN;
if ($oldWhaterStatus != WhaterPoint::WP_STATUS_UNKNOWN) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
}
} else if ($lastDistributionNetworkDate != null && $lastDistributionNetworkDate > $this->whaterStatusAt) {
$this->whaterStatusAt = $lastDistributionNetworkDate;
switch ($lastDistributionNetworkStatus) {
case DistributionNetwork::DN_STATUS_SUITABLE:
$this->whaterStatus = WhaterPoint::WP_STATUS_SUITABLE;
if ($oldWhaterStatus != WhaterPoint::WP_STATUS_SUITABLE) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
break;
case DistributionNetwork::DN_STATUS_NOT_SUITABLE;
$this->whaterStatus = WhaterPoint::WP_STATUS_NOT_SUITABLE;
if ($oldWhaterStatus != WhaterPoint::WP_STATUS_NOT_SUITABLE) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
break;
case DistributionNetwork::DN_STATUS_WITH_RESTRICTIONS:
$this->whaterStatus = WhaterPoint::WP_STATUS_WITH_RESTRICTIONS;
if ($oldWhaterStatus != WhaterPoint::WP_STATUS_WITH_RESTRICTIONS) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
break;
case DistributionNetwork::DN_STATUS_UNKNOWN;
$this->whaterStatus = WhaterPoint::WP_STATUS_UNKNOWN;
if ($oldWhaterStatus != WhaterPoint::WP_STATUS_UNKNOWN) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
break;
default:
$this->whaterStatus = WhaterPoint::WP_STATUS_UNKNOWN;
if ($oldWhaterStatus != WhaterPoint::WP_STATUS_UNKNOWN) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
}
}
}
} else {
switch ($whaterStatus) {
case WhaterPoint::WP_STATUS_NOT_SUITABLE:
case WhaterPoint::WP_STATUS_SUITABLE:
case WhaterPoint::WP_STATUS_UNKNOWN:
case WhaterPoint::WP_STATUS_WITH_RESTRICTIONS:
$this->whaterStatus = $whaterStatus;
if ($oldWhaterStatus != $whaterStatus) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
$this->whaterStatusAt = new \DateTime();
break;
default:
throw new InvalidWhaterStatusException();
}
}
}
public function editWhaterPoint(
string $name,
string $type,
Town $town,
DistributionNetwork $distributionNetwork = null,
Ubication $ubication = null,
Establishment $establishment = null
) {
$this->name = $name;
$this->type = $type;
$this->town = $town;
$this->distributionNetwork = $distributionNetwork;
$this->ubication = $ubication;
$this->establishment = $establishment;
$this->updatedAt = new \DateTime();
return $this;
}
public function updateGeoposition(
float $latitude,
float $longitude
) {
$this->latitude = $latitude;
$this->longitude = $longitude;
$this->geoPoint()->setLongitude($longitude);
$this->geoPoint()->setLatitude($latitude);
$this->updatedAt = new \DateTime();
return $this;
}
/**
* @return bool
*/
public function equals(WhaterPoint $whaterPoint)
{
return $this->id() === $whaterPoint->id();
}
public function images(): Collection
{
return $this->images;
}
/**
* @return ?WhaterDevice
*/
public function whaterDevice(): ?WhaterDevice
{
return $this->whaterDevice;
}
public function addDevice(WhaterDevice $whaterDevice)
{
$this->whaterDevice = $whaterDevice;
}
/**
* @return ?Product
*/
public function product(): ?Product
{
return $this->product;
}
public function addProduct(Product $product)
{
$this->product = $product;
}
/**
* @return Collection
*/
public function articles(): Collection
{
return $this->articles;
}
/**
* @return WhaterOrganization
*/
public function responsableOrganizationCorporative(): ?WhaterOrganization
{
return $this->responsableOrganizationCorporative;
}
/**
* @return WhaterOrganization
*/
public function responsableOrganizationUbication(): ?WhaterOrganization
{
return $this->responsableOrganizationUbication;
}
}