<?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 Location\Coordinate;
use Location\Ellipsoid;
use Location\Polygon as LocationPolygon;
use LongitudeOne\Spatial\PHP\Types\Geometry\Polygon;
use Ramsey\Uuid\Uuid;
use Whater\Domain\Common\Exception\InvalidUUIDException;
use Whater\Domain\Whater\Event\DistributionNetworkPolygonWasUpdate;
use Whater\Domain\Whater\Exception\InvalidDistributionNetworkParentException;
use Whater\Domain\Zones\Model\Country;
/**
* Class DistributionNetwork
*
* @package Whater\Domain\Whater\Model
*/
class DistributionNetwork implements ContainsRecordedEvents
{
use EventRecorderCapabilities;
const DN_STATUS_SUITABLE = 'DN_STATUS_SUITABLE';
const DN_STATUS_NOT_SUITABLE = 'DN_STATUS_NOT_SUITABLE';
const DN_STATUS_WITH_RESTRICTIONS = 'DN_STATUS_WITH_RESTRICTIONS';
const DN_STATUS_UNKNOWN = 'DN_STATUS_UNKNOWN';
const DN_TYPE_EXTERNAL = 'DN_TYPE_EXTERNAL';
const DN_TYPE_INTERNAL = 'DN_TYPE_INTERNAL';
const DN_TYPE_INTERMEDIATE = 'DN_TYPE_INTERMEDIATE';
/**
* @var string
*/
private $uuid;
/**
* @var string
*/
private $slug;
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $details;
/**
* @var string
*/
private $whaterStatus;
/**
* @var string
*/
private $whaterStatusByGovernment;
/**
* @var \DateTime
*/
private $whaterStatusByGovernmentUpdatedAt;
/**
* @var string
*/
private $whaterStatusByOperator;
/**
* @var \DateTime
*/
private $whaterStatusByOperatorUpdatedAt;
/**
* @var string
*/
private $whaterStatusByUbication;
/**
* @var \DateTime
*/
private $whaterStatusByUbicationUpdatedAt;
/**
* @var \DateTime
*/
private $createdAt;
/**
* @var \DateTime
*/
private $updatedAt;
/**
* @var \DateTime
*/
private $lastAnalitycsUpdateAt;
/**
* @var \DateTime
*/
private $lastSinacConnectionAt;
/**
* @var Collection
*/
private $towns;
/**
* @var Country
*/
private $country;
/**
* @var Collection
*/
private $whaterPoints;
/**
* @var WhaterOrganization
*/
private $responsableOrganizationGovernment;
/**
* @var WhaterOrganization
*/
private $responsableOrganizationOperator;
/**
* @var WhaterOrganization
*/
private $responsableOrganizationCorporative;
/**
* @var WhaterOrganization
*/
private $responsableOrganizationUbication;
/**
* @var float
*/
private $centerLatitude;
/**
* @var float
*/
private $centerLongitude;
/**
* @var string
*/
private $type;
/**
* @var Collection
*/
private $analyticals;
/**
* @var Collection
*/
private $ubications;
/**
* @var Collection
*/
private $townLocations;
/**
* @var Collection
*/
private $establishments;
/**
* @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 DistributionNetwork
*/
private $parent;
/**
* @var Collection
*/
private $children;
/**
* @var int
*/
private $sinacConnectionErrors;
/**
* @var string
*/
private $sinacId;
/**
* @var Polygon
*/
private $geometryPolygon;
/**
* @var float
*/
private $geometryAreaInHa;
/**
* @var bool
*/
private $notifyWhaterStatusChange;
/**
* @var Collection
*/
private $ownershipRequests;
/**
* @var Collection
*/
private $articles;
/**
* @var \DateTime
*/
private $lastProfessionalValorationAt;
/**
* @var int
*/
private $countProfessionalGeneral;
/**
* @var float
*/
private $averageProfessionalGeneral;
/**
* @var \DateTime
*/
private $lastUserValorationAt;
/**
* @var int
*/
private $countUserGeneral;
/**
* @var float
*/
private $averageUserGeneral;
/**
* @param string $distributionNetworkId
* @param string $isoCode
* @param string $name
*/
public function __construct(
Country $country,
string $distributionNetworkId = null,
string $name,
string $type = DistributionNetwork::DN_TYPE_EXTERNAL,
string $details = null,
string $slug = null,
string $sinacId = null,
DistributionNetwork $parent = null
) {
try {
$this->uuid = Uuid::fromString($distributionNetworkId ?: Uuid::uuid4())->toString();
} catch (\InvalidArgumentException $e) {
throw new InvalidUUIDException();
}
$this->country = $country;
$this->name = $name;
$this->type = $type;
$this->details = $details;
$this->slug = $slug;
$this->whaterPoints = new ArrayCollection();
$this->towns = new ArrayCollection();
$this->sinacId = $sinacId;
$this->whaterStatus = DistributionNetwork::DN_STATUS_UNKNOWN;
$this->centerLongitude = round($country->longitude(), 8);
$this->centerLatitude = round($country->latitude(), 8);
$this->geometryPolygon = new Polygon([[
[$this->centerLongitude + 0.005, $this->centerLatitude + 0.005],
[$this->centerLongitude + 0.005, $this->centerLatitude - 0.005],
[$this->centerLongitude - 0.005, $this->centerLatitude - 0.005],
[$this->centerLongitude - 0.005, $this->centerLatitude + 0.005],
[$this->centerLongitude + 0.005, $this->centerLatitude + 0.005]
]]);
$this->countUserGeneral = 0;
$this->countProfessionalGeneral = 0;
$this->averageProfessionalGeneral = null;
$this->averageUserGeneral = null;
$this->geometryAreaInHa = 100;
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
$this->analyticals = new ArrayCollection();
$this->children = new ArrayCollection();
$this->ubications = new ArrayCollection();
$this->townLocations = new ArrayCollection();
$this->establishments = new ArrayCollection();
$this->valorations = new ArrayCollection();
$this->articles = new ArrayCollection();
$this->parent = $parent;
$this->sinacConnectionErrors = 0;
$this->notifyWhaterStatusChange = false;
$this->ownershipRequests = new ArrayCollection();
if ($type == DistributionNetwork::DN_TYPE_INTERMEDIATE) {
if ($this->parent == null) {
throw new InvalidDistributionNetworkParentException();
} else if ($this->parent()->type != DistributionNetwork::DN_TYPE_EXTERNAL) {
throw new InvalidDistributionNetworkParentException();
}
} else if ($type == DistributionNetwork::DN_TYPE_INTERNAL) {
if ($this->parent == null) {
throw new InvalidDistributionNetworkParentException();
} else if ($this->parent()->type != DistributionNetwork::DN_TYPE_INTERMEDIATE) {
throw new InvalidDistributionNetworkParentException();
}
}
}
public function editDistributionNetwork(
Country $country,
string $name,
string $slug,
string $details = null,
string $sinacId = null
) {
$this->country = $country;
$this->name = $name;
$this->slug = $slug;
$this->details = $details;
$this->sinacId = $sinacId;
$this->updatedAt = new \DateTime();
return $this;
}
public function editPolygon(
Polygon $geometryPolygon
) {
$this->geometryPolygon = $geometryPolygon;
$this->record(new DistributionNetworkPolygonWasUpdate($this));
}
public function editCenterAndArea(
float $centerLatitude = null,
float $centerLongitude = null,
float $geometryAreaInHa = null
) {
if ($centerLatitude != null) {
$this->centerLatitude = $centerLatitude;
}
if ($centerLongitude != null) {
$this->centerLongitude = $centerLongitude;
}
if ($geometryAreaInHa != null) {
$this->geometryAreaInHa = $geometryAreaInHa;
}
}
public function updateAnalitycsUpdateTime(\DateTime $lastAnalitycsUpdateAt = null)
{
if ($lastAnalitycsUpdateAt != null) {
$this->lastAnalitycsUpdateAt = $lastAnalitycsUpdateAt;
} else if (count($this->analyticals()) == 0) {
$this->lastAnalitycsUpdateAt = new \DateTime();
} else {
$arrAnalyticals = $this->analyticals()->toArray();
usort($arrAnalyticals, function ($a1, $a2) {
return $a1->analyzedAt() < $a2->analyzedAt();
});
$this->lastAnalitycsUpdateAt = $arrAnalyticals[0]->analyzedAt();
}
}
public function updateWhaterStatus(string $whaterStatus = null)
{
$oldWhaterStatus = $this->whaterStatus;
if ($whaterStatus != null) {
switch ($whaterStatus) {
case DistributionNetwork::DN_STATUS_SUITABLE:
case Analytical::ANALYTICAL_SUITABLE:
$this->whaterStatus = DistributionNetwork::DN_STATUS_SUITABLE;
if ($oldWhaterStatus != DistributionNetwork::DN_STATUS_SUITABLE) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
break;
case DistributionNetwork::DN_STATUS_NOT_SUITABLE;
case Analytical::ANALYTICAL_NOT_SUITABLE:
$this->whaterStatus = DistributionNetwork::DN_STATUS_NOT_SUITABLE;
if ($oldWhaterStatus != DistributionNetwork::DN_STATUS_NOT_SUITABLE) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
break;
case DistributionNetwork::DN_STATUS_WITH_RESTRICTIONS:
$this->whaterStatus = DistributionNetwork::DN_STATUS_WITH_RESTRICTIONS;
if ($oldWhaterStatus != DistributionNetwork::DN_STATUS_WITH_RESTRICTIONS) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
break;
case DistributionNetwork::DN_STATUS_UNKNOWN;
case Analytical::ANALYTICAL_UNKNOWN:
$this->whaterStatus = DistributionNetwork::DN_STATUS_UNKNOWN;
if ($oldWhaterStatus != DistributionNetwork::DN_STATUS_UNKNOWN) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
break;
default:
$this->whaterStatus = DistributionNetwork::DN_STATUS_UNKNOWN;
if ($oldWhaterStatus != DistributionNetwork::DN_STATUS_UNKNOWN) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
}
} else {
$arrAnalyticals = $this->analyticals()->toArray();
usort($arrAnalyticals, function ($a1, $a2) {
return $a1->analyzedAt() < $a2->analyzedAt();
});
if (count($arrAnalyticals) > 0) {
$lastAnaytical = $arrAnalyticals[0];
switch ($lastAnaytical->result()) {
case Analytical::ANALYTICAL_NOT_SUITABLE:
$this->whaterStatus = DistributionNetwork::DN_STATUS_NOT_SUITABLE;
if ($oldWhaterStatus != DistributionNetwork::DN_STATUS_NOT_SUITABLE) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
if ($this->responsableOrganizationGovernment != null && $lastAnaytical->responsable() != null && $lastAnaytical->responsable()->equals($this->responsableOrganizationGovernment)) {
$this->whaterStatusByGovernment = DistributionNetwork::DN_STATUS_NOT_SUITABLE;
$this->whaterStatusByGovernmentUpdatedAt = $lastAnaytical->analyzedAt();
}
if ($this->responsableOrganizationOperator != null && $lastAnaytical->responsable() != null && $lastAnaytical->responsable()->equals($this->responsableOrganizationOperator)) {
$this->whaterStatusByOperator = DistributionNetwork::DN_STATUS_NOT_SUITABLE;
$this->whaterStatusByOperatorUpdatedAt = $lastAnaytical->analyzedAt();
}
if ($this->whaterStatusByUbication != null && $lastAnaytical->responsable() != null && $lastAnaytical->responsable()->equals($this->whaterStatusByUbication)) {
$this->whaterStatusByUbication = DistributionNetwork::DN_STATUS_NOT_SUITABLE;
$this->whaterStatusByUbicationUpdatedAt = $lastAnaytical->analyzedAt();
}
break;
case Analytical::ANALYTICAL_SUITABLE:
$this->whaterStatus = DistributionNetwork::DN_STATUS_SUITABLE;
if ($oldWhaterStatus != DistributionNetwork::DN_STATUS_SUITABLE) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
if ($this->responsableOrganizationGovernment != null && $lastAnaytical->responsable() != null && $lastAnaytical->responsable()->equals($this->responsableOrganizationGovernment)) {
$this->whaterStatusByGovernment = DistributionNetwork::DN_STATUS_SUITABLE;
$this->whaterStatusByGovernmentUpdatedAt = $lastAnaytical->analyzedAt();
}
if ($this->responsableOrganizationOperator != null && $lastAnaytical->responsable() != null && $lastAnaytical->responsable()->equals($this->responsableOrganizationOperator)) {
$this->whaterStatusByOperator = DistributionNetwork::DN_STATUS_SUITABLE;
$this->whaterStatusByOperatorUpdatedAt = $lastAnaytical->analyzedAt();
}
if ($this->responsableOrganizationUbication != null && $lastAnaytical->responsable() != null && $lastAnaytical->responsable()->equals($this->responsableOrganizationUbication)) {
$this->whaterStatusByUbication = DistributionNetwork::DN_STATUS_SUITABLE;
$this->whaterStatusByUbicationUpdatedAt = $lastAnaytical->analyzedAt();
}
break;
case Analytical::ANALYTICAL_UNKNOWN:
$this->whaterStatus = DistributionNetwork::DN_STATUS_UNKNOWN;
if ($oldWhaterStatus != DistributionNetwork::DN_STATUS_UNKNOWN) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
if ($this->responsableOrganizationGovernment != null && $lastAnaytical->responsable() != null && $lastAnaytical->responsable()->equals($this->responsableOrganizationGovernment)) {
$this->whaterStatusByGovernment = DistributionNetwork::DN_STATUS_UNKNOWN;
$this->whaterStatusByGovernmentUpdatedAt = $lastAnaytical->analyzedAt();
}
if ($this->responsableOrganizationOperator != null && $lastAnaytical->responsable() != null && $lastAnaytical->responsable()->equals($this->responsableOrganizationOperator)) {
$this->whaterStatusByOperator = DistributionNetwork::DN_STATUS_UNKNOWN;
$this->whaterStatusByOperatorUpdatedAt = $lastAnaytical->analyzedAt();
}
if ($this->responsableOrganizationUbication != null && $lastAnaytical->responsable() != null && $lastAnaytical->responsable()->equals($this->responsableOrganizationUbication)) {
$this->whaterStatusByUbication = DistributionNetwork::DN_STATUS_UNKNOWN;
$this->whaterStatusByUbicationUpdatedAt = $lastAnaytical->analyzedAt();
}
break;
default:
break;
}
if ($lastAnaytical->analyzedAt() > $this->lastAnalitycsUpdateAt) {
$this->lastAnalitycsUpdateAt = $lastAnaytical->analyzedAt();
}
} else {
$this->whaterStatus = DistributionNetwork::DN_STATUS_UNKNOWN;
if ($oldWhaterStatus != DistributionNetwork::DN_STATUS_UNKNOWN) {
$this->notifyWhaterStatusChange = true;
} else {
$this->notifyWhaterStatusChange = false;
}
}
}
// update all whaterpoint status
$this->updateWhaterPointsStatus();
}
public function updateWhaterPointsStatus()
{
foreach ($this->whaterPoints() as $whaterPoint) {
$whaterPoint->updateWhaterStatus();
}
}
public function updateSinacConnectionErrors(
int $sinacConnectionErrors,
\DateTime $lastSinacConnectionAt
) {
$this->sinacConnectionErrors = $sinacConnectionErrors;
$this->lastSinacConnectionAt = $lastSinacConnectionAt;
}
/**
* @return string
*/
public function id(): string
{
return $this->uuid;
}
/**
* @return string
*/
public function name(): string
{
return $this->name;
}
/**
* @return string
*/
public function details(): ?string
{
return $this->details;
}
/**
* @return string
*/
public function slug(): string
{
return $this->slug;
}
/**
* @return string
*/
public function whaterStatus(): string
{
return $this->whaterStatus;
}
/**
* @return string
*/
public function whaterStatusByGovernment(): ?string
{
return $this->whaterStatusByGovernment;
}
/**
* @return \DateTime
*/
public function whaterStatusByGovernmentUpdatedAt(): ?\DateTime
{
return $this->whaterStatusByGovernmentUpdatedAt;
}
/**
* @return string
*/
public function whaterStatusByOperator(): ?string
{
return $this->whaterStatusByOperator;
}
/**
* @return \DateTime
*/
public function whaterStatusByOperatorUpdatedAt(): ?\DateTime
{
return $this->whaterStatusByOperatorUpdatedAt;
}
/**
* @return string
*/
public function whaterStatusByUbication(): ?string
{
return $this->whaterStatusByUbication;
}
/**
* @return \DateTime
*/
public function whaterStatusByUbicationUpdatedAt(): ?\DateTime
{
return $this->whaterStatusByUbicationUpdatedAt;
}
/**
* @return Collection
*/
public function whaterPoints(): Collection
{
return $this->whaterPoints;
}
/**
* @return Collection
*/
public function towns(): Collection
{
return $this->towns;
}
/**
* @return Country
*/
public function country(): Country
{
return $this->country;
}
/**
* @return Collection
*/
public function ownershipRequests(): Collection
{
return $this->ownershipRequests;
}
public function lastAnalytical(): ?Analytical
{
$lastAnalytical = null;
$arrAnalyticals = $this->analyticals()->toArray();
usort($arrAnalyticals, function ($a1, $a2) {
return $a1->analyzedAt() < $a2->analyzedAt();
});
if (count($arrAnalyticals) > 0) {
$lastAnalytical = $arrAnalyticals[0];
}
return $lastAnalytical;
}
public function updateResponsables(
WhaterOrganization $responsableOrganizationGovernment = null,
WhaterOrganization $responsableOrganizationOperator = null,
WhaterOrganization $responsableOrganizationCorporative = null,
WhaterOrganization $responsableOrganizationUbication = null
) {
$this->responsableOrganizationGovernment = $responsableOrganizationGovernment;
$this->responsableOrganizationOperator = $responsableOrganizationOperator;
$this->responsableOrganizationCorporative = $responsableOrganizationCorporative;
$this->responsableOrganizationUbication = $responsableOrganizationUbication;
$this->updatedAt = new \DateTime();
}
public function updateResponsableOrganizationOperator(
WhaterOrganization $responsableOrganizationOperator = null,
) {
$this->responsableOrganizationOperator = $responsableOrganizationOperator;
$this->updatedAt = new \DateTime();
}
public function updateValorations()
{
$sumUserColor = array(
DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
$sumUserTaste = array(
DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
$sumUserSmell = array(
DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
$sumUserTurbidity = array(
DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
$sumUserGeneral = array(
DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
$countUserColor = 0;
$countUserTaste = 0;
$countUserSmell = 0;
$countUserTurbidity = 0;
$countUserGeneral = 0;
$sumProfessionalColor = array(
DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
$sumProfessionalTaste = array(
DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
$sumProfessionalSmell = array(
DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
$sumProfessionalTurbidity = array(
DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
$sumProfessionalGeneral = array(
DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
DistributionNetworkValoration::DN_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 DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION:
$sumProfessionalColor[DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumProfessionalColor[DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumProfessionalColor[DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
break;
default:
break;
}
$countProfessionalColor++;
}
if ($valoration->tasteValoration() != null) {
switch ($valoration->tasteValoration()) {
case DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION:
$sumProfessionalTaste[DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumProfessionalTaste[DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumProfessionalTaste[DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
break;
default:
break;
}
$countProfessionalTaste++;
}
if ($valoration->smellValoration() != null) {
switch ($valoration->smellValoration()) {
case DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION:
$sumProfessionalSmell[DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumProfessionalSmell[DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumProfessionalSmell[DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
break;
default:
break;
}
$countProfessionalSmell++;
}
if ($valoration->turbidityValoration() != null) {
switch ($valoration->turbidityValoration()) {
case DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION:
$sumProfessionalTurbidity[DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumProfessionalTurbidity[DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumProfessionalTurbidity[DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
break;
default:
break;
}
$countProfessionalTurbidity++;
}
if ($valoration->generalValoration() != null) {
switch ($valoration->generalValoration()) {
case DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION:
$sumProfessionalGeneral[DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumProfessionalGeneral[DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumProfessionalGeneral[DistributionNetworkValoration::DN_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 DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION:
$sumUserColor[DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumUserColor[DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumUserColor[DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
break;
default:
break;
}
$countUserColor++;
}
if ($valoration->tasteValoration() != null) {
switch ($valoration->tasteValoration()) {
case DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION:
$sumUserTaste[DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumUserTaste[DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumUserTaste[DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
break;
default:
break;
}
$countUserTaste++;
}
if ($valoration->smellValoration() != null) {
switch ($valoration->smellValoration()) {
case DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION:
$sumUserSmell[DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumUserSmell[DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumUserSmell[DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
break;
default:
break;
}
$countUserSmell++;
}
if ($valoration->turbidityValoration() != null) {
switch ($valoration->turbidityValoration()) {
case DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION:
$sumUserTurbidity[DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumUserTurbidity[DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumUserTurbidity[DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION]++;
break;
default:
break;
}
$countUserTurbidity++;
}
if ($valoration->generalValoration() != null) {
switch ($valoration->generalValoration()) {
case DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION:
$sumUserGeneral[DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION:
$sumUserGeneral[DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION]++;
break;
case DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION:
$sumUserGeneral[DistributionNetworkValoration::DN_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(
DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
DistributionNetworkValoration::DN_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(
DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
DistributionNetworkValoration::DN_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(
DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
DistributionNetworkValoration::DN_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(
DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
DistributionNetworkValoration::DN_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(
DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION => 0,
DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION => 0
);
}
// calculate average valoration for users and professionals
if ($this->countProfessionalGeneral > 0) {
$averageProfessionalGeneral =
(($sumProfessionalGeneral[DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION] * 10) +
($sumProfessionalGeneral[DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION] * 5) +
($sumProfessionalGeneral[DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION] * 0)) / $this->countProfessionalGeneral;
$this->averageProfessionalGeneral = round($averageProfessionalGeneral, 2);
} else {
$this->averageProfessionalGeneral = null;
}
if ($this->countUserGeneral > 0) {
$averageUserGeneral =
(($sumUserGeneral[DistributionNetworkValoration::DN_QUALITY_ACCEPTABLE_VALORATION] * 10) +
($sumUserGeneral[DistributionNetworkValoration::DN_QUALITY_NEEDS_IMPROVEMENT_VALORATION] * 5) +
($sumUserGeneral[DistributionNetworkValoration::DN_QUALITY_NOT_ACCEPTABLE_VALORATION] * 0)) / $this->countUserGeneral;
$this->averageUserGeneral = round($averageUserGeneral, 2);
} else {
$this->averageUserGeneral = null;
}
}
/**
* @return string
*/
public function sinacId(): ?string
{
return $this->sinacId;
}
/**
* @return \DateTime
*/
public function createdAt(): \DateTime
{
return $this->createdAt;
}
/**
* @return \DateTime
*/
public function updatedAt(): \DateTime
{
return $this->updatedAt;
}
/**
* @return \DateTime
*/
public function lastAnalitycsUpdateAt(): ?\DateTime
{
return $this->lastAnalitycsUpdateAt;
}
/**
* @return float
*/
public function centerLatitude(): float
{
if ($this->centerLatitude == null) {
return 0;
}
return $this->centerLatitude;
}
/**
* @return float
*/
public function centerLongitude(): float
{
if ($this->centerLongitude == null) {
return 0;
}
return $this->centerLongitude;
}
/**
* @return Polygon
*/
public function geometryPolygon(): ?Polygon
{
return $this->geometryPolygon;
}
/**
* @return LocationPolygon
*/
public function locationPolygon(): ?LocationPolygon
{
if ($this->geometryPolygon() != null) {
$pol = new LocationPolygon();
$points = $this->geometryPolygon()->toArray();
foreach ($points[0] as $point) {
$lat = $point[1];
$long = $point[0];
$pol->addPoint(new Coordinate($lat, $long, Ellipsoid::createDefault()));
}
return $pol;
}
return null;
}
public function getGeoJsonPolygon(): ?string
{
if ($this->geometryPolygon() != null) {
$geoJsonCoordinates = [];
// Recorre los anillos (exterior + huecos si existen)
foreach ($this->geometryPolygon()->getRings() as $ring) {
$ringCoordinates = [];
// Recorre los puntos del anillo
foreach ($ring->getPoints() as $point) {
$ringCoordinates[] = [$point->getLongitude(), $point->getLatitude()];
}
// Cierra el anillo si no está cerrado
if ($ringCoordinates[0] !== $ringCoordinates[count($ringCoordinates) - 1]) {
$ringCoordinates[] = $ringCoordinates[0];
}
// Añade el anillo a las coordenadas del GeoJSON
$geoJsonCoordinates[] = $ringCoordinates;
}
// Construye el GeoJSON
$geoJson = [
"type" => "Polygon",
"coordinates" => $geoJsonCoordinates
];
return json_encode($geoJson, JSON_PRETTY_PRINT);
}
return null;
}
/**
* @return float
*/
public function geometryAreaInHa(): ?float
{
return $this->geometryAreaInHa;
}
/**
* @return Collection
*/
public function analyticals(): Collection
{
return $this->analyticals;
}
/**
* @return string
*/
public function type(): string
{
return $this->type;
}
/**
* @return Collection
*/
public function ubications(): Collection
{
return $this->ubications;
}
/**
* @return Collection
*/
public function townLocations(): Collection
{
return $this->townLocations;
}
/**
* @return bool
*/
public function equals(DistributionNetwork $distributionNetwork)
{
return $this->id() === $distributionNetwork->id();
}
/**
* @return Collection
*/
public function establishments(): Collection
{
return $this->establishments;
}
/**
* @return Collection
*/
public function valorations(): Collection
{
return $this->valorations;
}
/**
* @return Collection
*/
public function children(): Collection
{
return $this->children;
}
/**
* @return null|DistributionNetwork
*/
public function parent(): ?DistributionNetwork
{
return $this->parent;
}
/**
* @return int
*/
public function sinacConnectionErrors(): int
{
return $this->sinacConnectionErrors;
}
/**
* @return \DateTime
*/
public function lastSinacConnectionAt(): ?\DateTime
{
return $this->lastSinacConnectionAt;
}
/**
* @return WhaterOrganization
*/
public function responsableOrganizationGovernment(): ?WhaterOrganization
{
return $this->responsableOrganizationGovernment;
}
/**
* @return WhaterOrganization
*/
public function responsableOrganizationOperator(): ?WhaterOrganization
{
return $this->responsableOrganizationOperator;
}
/**
* @return WhaterOrganization
*/
public function responsableOrganizationCorporative(): ?WhaterOrganization
{
return $this->responsableOrganizationCorporative;
}
/**
* @return WhaterOrganization
*/
public function responsableOrganizationUbication(): ?WhaterOrganization
{
return $this->responsableOrganizationUbication;
}
/**
* @return bool
*/
public function notifyWhaterStatusChange(): bool
{
return $this->notifyWhaterStatusChange;
}
/**
* @return Collection
*/
public function articles(): Collection
{
return $this->articles;
}
/**
* @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 \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;
}
}