<?php
namespace App\Entity;
use App\Constant\OwnerType;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="cal_accommodation")
*/
class Accommodation
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @Assert\NotBlank()
* @Assert\Length(max=100)
* @ORM\Column(type="string", length=100)
*/
private $name = "";
/**
* @ORM\OneToOne(targetEntity="App\Entity\Address", cascade={"persist"}, orphanRemoval=true)
*/
private $address;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Translation", cascade={"persist"}, orphanRemoval=true)
*/
private $description;
/**
* @ORM\OneToOne(targetEntity="App\Entity\AccommodationImage", cascade={"persist"}, orphanRemoval=true)
*/
private $cover;
/**
* @ORM\OneToMany(targetEntity="App\Entity\AccommodationImage", mappedBy="accommodation", cascade={"persist"}, orphanRemoval=true)
* @ORM\OrderBy({"position" = "ASC"})
*/
private $images;
/**
* @ORM\OneToOne(targetEntity="App\Entity\AccommodationImage", cascade={"persist"}, orphanRemoval=true)
*/
private $plan;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $virtualTour = "";
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $petFriendly;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $girlsOnly;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $surface;
/**
* @ORM\Column(type="string", length=24, nullable=true)
*/
private $type = "";
/**
* @ORM\Column(type="string", length=500, nullable=true)
*/
private $constructionYear = "";
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $totalBedrooms;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $totalBathrooms;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $totalKitchens;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $totalToilets;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $garden;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Translation", cascade={"persist"}, orphanRemoval=true)
*/
private $access;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\PropertyListItem")
*/
private $district;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\PropertyListItem")
* @ORM\JoinTable(name="cal_accommodations_universities")
*/
private $universities;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\PropertyListItem")
* @ORM\JoinTable(name="cal_accommodations_transports")
*/
private $transports;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\PropertyListItem")
* @ORM\JoinTable(name="cal_accommodations_equipments")
*/
private $equipments;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\PropertyListItem")
* @ORM\JoinTable(name="cal_accommodations_security")
*/
private $security;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\PropertyListItem")
* @ORM\JoinTable(name="cal_accommodations_services")
*/
private $services;
/**
* @var \DateTime $createdAt
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @var \DateTime $updated
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
*/
private $updatedAt;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $disabled = false;
/**
* @ORM\OneToMany(targetEntity="App\Entity\AccommodationRoom", mappedBy="accommodation", orphanRemoval=false)
*/
private $rooms = [];
/**
* @ORM\OneToMany(targetEntity="App\Entity\AccommodationReport", mappedBy="accommodation", orphanRemoval=false)
*/
private $reports = [];
/**
* @ORM\OneToMany(targetEntity="App\Entity\Booking", mappedBy="accommodation")
*/
private $bookings;
/**
* @ORM\OneToMany(targetEntity="App\Entity\AccommodationZone", mappedBy="accommodation", cascade={"persist"}, orphanRemoval=true)
* @ORM\OrderBy({"priority" = "DESC"})
*/
private $zones;
/**
* @ORM\Column(type="string", length=50, nullable=true)
*/
private $ownerType = OwnerType::COLOCALYON_TRANSACTION;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="ownedAccommodations")
*/
private $owner;
/**
* @ORM\OneToOne(targetEntity="App\Entity\AccommodationOwnerInfo", cascade={"persist"}, orphanRemoval=true)
*/
private $ownerInfo;
/**
* @ORM\OneToOne(targetEntity="App\Entity\AccommodationInfo", cascade={"persist"}, orphanRemoval=true)
*/
private $info;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Maintenance", mappedBy="accommodation")
*/
private $maintenances;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Survey", mappedBy="accommodation")
*/
private $surveys;
/**
* @ORM\OneToOne(targetEntity="App\Entity\CleaningRecurrence", cascade={"persist"}, orphanRemoval=true)
*/
private $cleaningRecurrence;
/**
* @ORM\OneToMany(targetEntity="App\Entity\CleaningDate", mappedBy="accommodation")
* @ORM\OrderBy({"date" = "ASC"})
*/
private $cleaningDates;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $squareMeterPrice;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $majoredSquareMeterPrice;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Insurance", mappedBy="accommodation", cascade={"persist"})
*/
private $insurances;
/**
* @ORM\OneToOne (targetEntity="App\Entity\WorkflowProject", mappedBy="accommodation")
*/
private $project;
//--------------------------------------------------------------
// Utilities
//--------------------------------------------------------------
public function __toString() {
return $this->getName();
}
public function __construct()
{
$this->zones = new ArrayCollection();
$this->images = new ArrayCollection();
$this->rooms = new ArrayCollection();
$this->reports = new ArrayCollection();
$this->maintenances = new ArrayCollection();
$this->surveys = new ArrayCollection();
$this->cleaningDates = new ArrayCollection();
$this->insurances = new ArrayCollection();
}
public function getLowestPrice() {
foreach ($this->getActiveRooms() as $room) {
if(empty($lowest) || $lowest > $room->getRent()){
$lowest = $room->getRent();
}
}
return !empty($lowest) ? $lowest : 0;
}
public function getTotalAvailableRooms() {
$total = 0;
foreach ($this->getActiveRooms() as $room) {
if($room->isAvailable()){
$total++;
}
}
return $total;
}
public function getFirstRoomAvailability() {
$dates = array_filter($this->getRooms()->map(function(AccommodationRoom $room){
return $room->getAvailablilityDate();
})->toArray());
return !empty($dates) ? min($dates) : null;
}
public function getActiveRooms() {
return $this->getRooms()->filter(function(AccommodationRoom $room){
return !$room->getDisabled();
});
}
public function getRoommates() {
$users = [];
foreach ($this->getActiveRooms() as $room) {
if(!$room->isAvailable()){
$occupancy = $room->getOccupancy();
if ($occupancy instanceof AccommodationOccupancy) {
$users[] = $occupancy->getUser();
}
}
}
return $users;
}
public function getCleaningDuration() {
return !empty($this->getCleaningRecurrence()) ? $this->getCleaningRecurrence()->getDuration() : 60;
}
public function getNextsCleaningDates() {
return $this->getCleaningDates()->slice(0, 3);
}
public function getRoomsWithCleaning() {
return $this->getRooms()->filter(function ($room) {
$occupancy = $room->getOccupancy();
if ($occupancy instanceof AccommodationOccupancy) {
return $occupancy->isActive() && $occupancy->getActiveCleaning();
}
return false;
});
}
public function hasEmptyZone() {
if ($this->getZones()->count() === 0) return true;
$hasEmptyZone = false;
foreach ($this->getZones() as $zone) {
if ($zone->isEmpty()) $hasEmptyZone = true;
}
foreach ($this->getRooms() as $room) {
if ($room->hasEmptyZone()) $hasEmptyZone = true;
}
return $hasEmptyZone;
}
public function export() {
$datas = [
"id" => $this->getId(),
"name" => $this->getName(),
"address" => $this->getAddress()->getFullAddress(),
"surface" => $this->getSurface(),
"total_bedrooms" => $this->getTotalBedrooms(),
"total_bathrooms" => $this->getTotalBathrooms(),
"total_kitchens" => $this->getTotalKitchens(),
"total_toilets" => $this->getTotalToilets(),
"total_roommates" => count($this->getRoommates()),
];
if ($this->getOwner() instanceof User) {
$datas = array_merge($datas, [
"owner" => $this->getOwner()->getFullName(),
]);
}
return $datas;
}
//--------------------------------------------------------------
// Collections
//--------------------------------------------------------------
public function addImage(AccommodationImage $image)
{
$image->setAccommodation($this);
$this->images[] = $image;
return $this;
}
public function removeImage(AccommodationImage $image)
{
$this->images->removeElement($image);
}
//--------------------------------------------------------------
// Getters and setters
//--------------------------------------------------------------
public function getImages()
{
return $this->images;
}
public function setImages($images)
{
$this->images = $images;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getAddress()
{
return $this->address;
}
/**
* @param mixed $address
*/
public function setAddress($address)
{
$this->address = $address;
}
/**
* @return mixed
*/
public function getDescription()
{
return $this->description;
}
/**
* @param mixed $description
*/
public function setDescription($description)
{
$this->description = $description;
}
/**
* @return mixed
*/
public function getCover()
{
return $this->cover;
}
/**
* @param mixed $cover
*/
public function setCover($cover)
{
$this->cover = $cover;
}
/**
* @return mixed
*/
public function getVirtualTour()
{
return $this->virtualTour;
}
/**
* @param mixed $virtualTour
*/
public function setVirtualTour($virtualTour)
{
$this->virtualTour = $virtualTour;
}
/**
* @return \DateTime
*/
public function getCreatedAt(): \DateTime
{
return $this->createdAt;
}
/**
* @param \DateTime $createdAt
*/
public function setCreatedAt(\DateTime $createdAt)
{
$this->createdAt = $createdAt;
}
/**
* @return \DateTime
*/
public function getUpdatedAt(): \DateTime
{
return $this->updatedAt;
}
/**
* @param \DateTime $updatedAt
*/
public function setUpdatedAt(\DateTime $updatedAt)
{
$this->updatedAt = $updatedAt;
}
/**
* @return mixed
*/
public function getPetFriendly()
{
return $this->petFriendly;
}
/**
* @param mixed $petFriendly
*/
public function setPetFriendly($petFriendly)
{
$this->petFriendly = $petFriendly;
}
/**
* @return mixed
*/
public function getGirlsOnly()
{
return $this->girlsOnly;
}
/**
* @param mixed $girlsOnly
*/
public function setGirlsOnly($girlsOnly)
{
$this->girlsOnly = $girlsOnly;
}
/**
* @return mixed
*/
public function getSurface()
{
return $this->surface;
}
/**
* @param mixed $surface
*/
public function setSurface($surface)
{
$this->surface = $surface;
}
/**
* @return mixed
*/
public function getType()
{
return $this->type;
}
/**
* @param mixed $type
*/
public function setType($type)
{
$this->type = $type;
}
/**
* @return mixed
*/
public function getTotalBedrooms()
{
return $this->totalBedrooms;
}
/**
* @param mixed $totalBedrooms
*/
public function setTotalBedrooms($totalBedrooms)
{
$this->totalBedrooms = $totalBedrooms;
}
/**
* @return mixed
*/
public function getTotalBathrooms()
{
return $this->totalBathrooms;
}
/**
* @param mixed $totalBathrooms
*/
public function setTotalBathrooms($totalBathrooms)
{
$this->totalBathrooms = $totalBathrooms;
}
/**
* @return mixed
*/
public function getTotalKitchens()
{
return $this->totalKitchens;
}
/**
* @param mixed $totalKitchens
*/
public function setTotalKitchens($totalKitchens)
{
$this->totalKitchens = $totalKitchens;
}
/**
* @return mixed
*/
public function getTotalToilets()
{
return $this->totalToilets;
}
/**
* @param mixed $totalToilets
*/
public function setTotalToilets($totalToilets)
{
$this->totalToilets = $totalToilets;
}
/**
* @return mixed
*/
public function getGarden()
{
return $this->garden;
}
/**
* @param mixed $garden
*/
public function setGarden($garden)
{
$this->garden = $garden;
}
/**
* @return mixed
*/
public function getAccess()
{
return $this->access;
}
/**
* @param mixed $access
*/
public function setAccess($access)
{
$this->access = $access;
}
/**
* @return mixed
*/
public function getDistrict()
{
return $this->district;
}
/**
* @param mixed $district
*/
public function setDistrict($district)
{
$this->district = $district;
}
/**
* @return mixed
*/
public function getUniversities()
{
return $this->universities;
}
/**
* @param mixed $universities
*/
public function setUniversities($universities)
{
$this->universities = $universities;
}
/**
* @return mixed
*/
public function getTransports()
{
return $this->transports;
}
/**
* @param mixed $transports
*/
public function setTransports($transports)
{
$this->transports = $transports;
}
/**
* @return mixed
*/
public function getEquipments()
{
return $this->equipments;
}
/**
* @param mixed $equipments
*/
public function setEquipments($equipments)
{
$this->equipments = $equipments;
}
/**
* @return mixed
*/
public function getSecurity()
{
return $this->security;
}
/**
* @param mixed $security
*/
public function setSecurity($security)
{
$this->security = $security;
}
/**
* @return mixed
*/
public function getServices()
{
return $this->services;
}
/**
* @param mixed $services
*/
public function setServices($services)
{
$this->services = $services;
}
/**
* @return mixed
*/
public function getDisabled()
{
return $this->disabled;
}
/**
* @param mixed $disabled
*/
public function setDisabled($disabled)
{
$this->disabled = $disabled;
}
/**
* @return mixed
*/
public function getRooms()
{
return $this->rooms;
}
/**
* @param mixed $rooms
*/
public function setRooms($rooms)
{
$this->rooms = $rooms;
}
/**
* @return mixed
*/
public function getBookings()
{
return $this->bookings;
}
/**
* @param mixed $bookings
*/
public function setBookings($bookings)
{
$this->bookings = $bookings;
}
/**
* @return mixed
*/
public function getZones()
{
return $this->zones;
}
/**
* @param mixed $zones
*/
public function setZones($zones): void
{
$this->zones = $zones;
}
/**
* @return mixed
*/
public function getOwner()
{
return $this->owner;
}
/**
* @param mixed $owner
*/
public function setOwner($owner): void
{
$this->owner = $owner;
}
/**
* @return string
*/
public function getOwnerType(): string
{
return $this->ownerType;
}
/**
* @param string $ownerType
*/
public function setOwnerType(string $ownerType): void
{
$this->ownerType = $ownerType;
}
/**
* @return mixed
*/
public function getOwnerInfo()
{
return $this->ownerInfo;
}
/**
* @param mixed $ownerInfo
*/
public function setOwnerInfo($ownerInfo): void
{
$this->ownerInfo = $ownerInfo;
}
/**
* @return mixed
*/
public function getInfo()
{
return $this->info;
}
/**
* @param mixed $info
*/
public function setInfo($info): void
{
$this->info = $info;
}
/**
* @return mixed
*/
public function getSurveys()
{
return $this->surveys;
}
/**
* @param mixed $surveys
*/
public function setSurveys($surveys): void
{
$this->surveys = $surveys;
}
/**
* @return ArrayCollection
*/
public function getCleaningDates()
{
return $this->cleaningDates;
}
/**
* @param ArrayCollection $cleaningDates
*/
public function setCleaningDates($cleaningDates): void
{
$this->cleaningDates = $cleaningDates;
}
/**
* @return mixed
*/
public function getCleaningRecurrence()
{
return $this->cleaningRecurrence;
}
/**
* @param mixed $cleaningRecurrence
*/
public function setCleaningRecurrence($cleaningRecurrence): void
{
$this->cleaningRecurrence = $cleaningRecurrence;
}
/**
* @return string
*/
public function getConstructionYear()
{
return $this->constructionYear;
}
/**
* @param string $constructionYear
*/
public function setConstructionYear($constructionYear): void
{
$this->constructionYear = $constructionYear;
}
/**
* @return mixed
*/
public function getSquareMeterPrice()
{
return $this->squareMeterPrice / 100;
}
/**
* @param mixed $squareMeterPrice
*/
public function setSquareMeterPrice($squareMeterPrice): void
{
$this->squareMeterPrice = $squareMeterPrice * 100;
}
/**
* @return mixed
*/
public function getMajoredSquareMeterPrice()
{
return $this->majoredSquareMeterPrice / 100;
}
/**
* @param mixed $majoredSquareMeterPrice
*/
public function setMajoredSquareMeterPrice($majoredSquareMeterPrice): void
{
$this->majoredSquareMeterPrice = $majoredSquareMeterPrice * 100;
}
/**
* @return mixed
*/
public function getPlan()
{
return $this->plan;
}
/**
* @param mixed $plan
*/
public function setPlan($plan): void
{
$this->plan = $plan;
}
/**
* @return Collection
*/
public function getMaintenances(): Collection
{
return $this->maintenances;
}
/**
* @param Collection $maintenances
*/
public function setMaintenances(Collection $maintenances): void
{
$this->maintenances = $maintenances;
}
/**
* @return Collection
*/
public function getInsurances(): Collection
{
return $this->insurances;
}
/**
* @param Collection $insurances
*/
public function setInsurances(Collection $insurances): void
{
$this->insurances = $insurances;
}
/**
* @return mixed
*/
public function getProject()
{
return $this->project;
}
/**
* @param mixed $project
*/
public function setProject($project): void
{
$this->project = $project;
}
}