<?php
namespace App\Entity;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use App\Validation as ExcAssert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Demande
*
* @ORM\Table(name="Demande")
* @ORM\Entity(repositoryClass="App\Entity\DemandeRepository")
* @ORM\HasLifecycleCallbacks
*/
class Demande
{
/**
* Newly created demande, waiting for validation
*/
public const STATUS_NEW = 0;
/**
* Valid demande, visible on site, available to purchase
*/
public const STATUS_AVAILABLE = 1;
/**
* Valid demande, visible on site, has been purchased 3 times already
*/
public const STATUS_BOUGHT = 2;
/**
* Manually disabled or expired. Not visible on site anymore
*/
public const STATUS_DISABLED = 4;
/**
* Deleted through admin interface
*/
public const STATUS_DELETED = 5;
/**
* En attente d'envoi de notification aux prestataires
*/
public const STATUS_NOTIFICATION_PRESTATAIRES = 6;
public static $statusLabels = array(
self::STATUS_NEW => 'Nouvelle',
self::STATUS_AVAILABLE => "Visible / Disponible à l'achat",
self::STATUS_BOUGHT => "Visible / Non disponible à l'achat",
self::STATUS_DISABLED => 'Désactivée',
self::STATUS_DELETED => 'Supprimée',
self::STATUS_NOTIFICATION_PRESTATAIRES => 'Notification aux prestataires',
);
public static $clientStatuts = array(
0 => 'Propriétaire',
1 => 'Acquéreur',
2 => 'Mandataire',
3 => 'Autre'
);
public static $bienTypes = array(
1 => 'Maison',
2 => 'Appartement',
3 => 'Immeuble',
4 => 'Partie commune',
5 => 'Dépendance',
6 => 'Garage',
7 => 'Local professionnel',
8 => 'Local industriel',
9 => 'Local agricole',
10 => 'Bureau',
11 => 'Terrain',
12 => 'Autre',
);
public static $nombrePieces = array(
1 => 1,
2 => 2,
3 => 3,
4 => 4,
5 => 5,
6 => 6,
7 => '7 et +',
0 => 'Sans objet'
);
public static $coproprieteAnswers = array(
1 => 'Oui',
0 => 'Non',
3 => 'Sans objet'
);
public static $prestationAnswers = array(
1 => 'Fourniture et pose',
2 => 'Fourniture',
3 => 'Pose',
4 => 'Dépannage / Maintenance',
0 => 'Sans objet'
);
public static $answers = array(
1 => 'Oui',
0 => 'Non',
2 => 'Je ne sais pas',
3 => 'Sans objet'
);
public static $constructYears = array(
1 => 'Avant 1949',
2 => 'Entre 1949 et 1997',
3 => 'Après 1997',
0 => 'Sans objet'
);
public static function createWithId($id)
{
$instance = new self();
$instance->id = $id;
return $instance;
}
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="prix", type="integer")
*/
private $prix;
/**
* @var integer
*
* @ORM\Column(name="credits", type="integer")
*/
private $credits;
/**
* @var int
*
* @ORM\Column(name="status", type="integer")
*/
private $status;
/**
* @var DateTime
*
* @ORM\Column(name="creation_date", type="datetime")
*/
private $creationDate;
/**
* @var DateTime
*
* @ORM\Column(name="modified_date", type="datetime")
*/
private $modifiedDate;
/**
* @var int
*
* @ORM\Column(name="client_statut", type="integer")
* @Assert\NotBlank(message="Veuillez renseigner ce champ.")
*/
private $clientStatut;
/**
* @var string
*
* @ORM\Column(name="client_nom", type="string", length=255)
* @Assert\NotBlank(message="Veuillez renseigner ce champ.")
*/
private $clientNom;
/**
* @var string
*
* @ORM\Column(name="client_email", type="string", length=255)
* @Assert\NotBlank(message="Veuillez renseigner ce champ.")
* @Assert\Email(message="Veuillez entrer une adresse email valide.")
*/
private $clientEmail;
/**
* @var string
*
* @ORM\Column(name="client_telephone", type="string", length=255)
* @Assert\NotBlank(message="Veuillez renseigner ce champ.")
* @ExcAssert\Phone
*/
private $clientTelephone;
/**
* @var int
*
* @ORM\Column(name="bien_type", type="integer")
* @Assert\NotBlank(message="Veuillez renseigner ce champ.")
*/
private $bienType;
/**
* @var string
*
* @ORM\Column(name="bien_localisation", type="string", length=255, nullable=true)
*/
private $bienLocalisation;
/**
* @var string
*
* @ORM\Column(name="bien_code_postal", type="string", length=20)
* @Assert\NotBlank(message="Veuillez renseigner ce champ.")
*/
private $bienCodePostal;
/**
* @var string
*
* @ORM\Column(name="bien_ville", type="string", length=255)
* @Assert\NotBlank(message="Veuillez renseigner ce champ.")
*/
private $bienVille;
/**
* @var string
*
* @ORM\Column(name="bien_nombre_pieces", type="string", length=255)
* @Assert\NotBlank(message="Veuillez renseigner ce champ.")
*/
private $bienNombrePieces;
/**
* @var string
*
* @ORM\Column(name="bien_surface", type="string", length=255)
* @Assert\NotBlank(message="Veuillez renseigner ce champ.")
*/
private $bienSurface;
/**
* @var string
*
* @ORM\Column(name="bien_annee_construction", type="string", length=60)
* @Assert\NotBlank(message="Veuillez renseigner ce champ.")
*/
private $bienAnneeConstruction;
/**
* @var integer
*
* @ORM\Column(name="bien_copropriete", type="integer", nullable=true)
* @Assert\NotBlank(message="Veuillez renseigner ce champ.")
*/
private $bienCopropriete;
/**
* @var integer
*
* @ORM\Column(name="bien_gaz", type="integer", nullable=true)
* @Assert\NotBlank(groups={"diagnostics"}, message="Veuillez renseigner ce champ.")
*/
private $bienGaz;
/**
* @var integer
*
* @ORM\Column(name="bien_electricite", type="integer", nullable=true)
* @Assert\NotBlank(groups={"diagnostics"}, message="Veuillez renseigner ce champ.")
*/
private $bienElectricite;
/**
* @var string
*
* @ORM\Column(name="bien_photo", type="string", length=255, nullable=true)
*/
private $bienPhotoPath;
/**
* @Assert\Image(
* maxSize="10M",
* maxSizeMessage="Le poids maximum possible est de 10 Mo."
* )
*
*/
private $bienPhoto;
/**
* @var string
*
* @ORM\Column(name="description_file", type="string", length=255, nullable=true)
*/
private $descriptionFilePath;
/**
* @Assert\File(
* maxSize="10000000",
* maxSizeMessage="Le poids maximum possible est de 10 Mo."
* )
*/
private $descriptionFile;
/**
* @var DateTime
*
* @ORM\Column(name="expiration", type="datetime")
*/
private $expiration;
/**
* @var string
*
* @ORM\Column(name="comment", type="text", nullable=true)
*/
private $comment;
/**
* @var int
*
* @Assert\NotBlank(
* message="Veuillez renseigner ce champ.",
* groups={"solutions", "energies"}
* )
* @ORM\Column(name="prestation", type="integer", nullable=true)
*/
private $prestation;
/**
* @var string
*
* @ORM\Column(name="provenance", type="string", nullable=true)
*/
private $provenance;
/**
* @var array
* @ORM\Column(name="similar", type="simple_array", nullable=true)
*/
private $similar;
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Transaction", mappedBy="demandes")
*/
private $transactions;
/**
* @var Categorie
*
* @ORM\ManyToOne(targetEntity="Categorie")
* @ORM\JoinColumn(name="categorie_id", referencedColumnName="id")
* @Assert\NotBlank(message="Veuillez renseigner ce champ.")
*/
private $categorie;
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="RefundRequest", mappedBy="demande", cascade={"remove"})
*/
private $refundRequests;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $estimation;
public function __construct()
{
$this->creationDate = new DateTime();
$this->modifiedDate = new DateTime();
$this->transactions = new ArrayCollection();
$this->refundRequests = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set price
*
* @param integer $prix
*
* @return Demande
*/
public function setPrix($prix)
{
$this->prix = $prix;
return $this;
}
/**
* Get price
*
* @return integer
*/
public function getPrix()
{
return $this->prix;
}
/**
* Set credits
*
* @param integer $credits
*
* @return Demande
*/
public function setCredits($credits)
{
$this->credits = $credits;
return $this;
}
/**
* Get credits
*
* @return integer
*/
public function getCredits()
{
if ($this->credits):
return $this->credits;
else:
if ($credits = $this->getCategorie()->getCredits()):
$this->setCredits($credits);
elseif ($credits = $this->getCategorie()->getParent()->getCredits()):
$this->setCredits($credits);
endif;
endif;
return $this->credits;
}
/**
* Set validated
*
* @param boolean $status
*
* @return Demande
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get validated
*
* @return boolean
*/
public function getStatus()
{
return $this->status;
}
public function getStatusLabel()
{
return self::$statusLabels[$this->status];
}
/**
* Set creationDate
*
* @param DateTime $creationDate
*
* @return Demande
*/
public function setCreationDate($creationDate)
{
$this->creationDate = $creationDate;
return $this;
}
/**
* Get creationDate
*
* @return DateTime
*/
public function getCreationDate()
{
return $this->creationDate;
}
/**
* Set clientStatus
*
* @param int $clientStatu
*
* @return Demande
*/
public function setClientStatut($clientStatu)
{
$this->clientStatut = $clientStatu;
return $this;
}
/**
* Get clientStatus
*
* @return int
*/
public function getClientStatut()
{
return $this->clientStatut;
}
public function getClientStatusLabel()
{
return self::$clientStatuts[$this->clientStatut];
}
/**
* Set clientName
*
* @param string $clientNom
*
* @return Demande
*/
public function setClientNom($clientNom)
{
$this->clientNom = $clientNom;
return $this;
}
/**
* Get clientName
*
* @return string
*/
public function getClientNom()
{
return $this->clientNom;
}
/**
* Set clientEmail
*
* @param string $clientEmail
*
* @return Demande
*/
public function setClientEmail($clientEmail)
{
$this->clientEmail = $clientEmail;
return $this;
}
/**
* Get clientEmail
*
* @return string
*/
public function getClientEmail()
{
return $this->clientEmail;
}
/**
* Set clientPhone
*
* @param string $clientTelephone
*
* @return Demande
*/
public function setClientTelephone($clientTelephone)
{
$this->clientTelephone = $clientTelephone;
return $this;
}
/**
* Get clientPhone
*
* @return string
*/
public function getClientTelephone()
{
return $this->clientTelephone;
}
/**
* @param int $bienType
*
* @return Demande
*/
public function setBienType($bienType)
{
$this->bienType = $bienType;
return $this;
}
/**
* @return int
*/
public function getBienType()
{
return $this->bienType;
}
public function getBienTypeLabel()
{
return self::$bienTypes[$this->bienType];
}
/**
* @param string $bienLocalisation
*
* @return Demande
*/
public function setBienLocalisation($bienLocalisation)
{
$this->bienLocalisation = $bienLocalisation;
return $this;
}
/**
* @return string
*/
public function getBienLocalisation()
{
return $this->bienLocalisation;
}
/**
* @param string $bienCodePostal
*
* @return Demande
*/
public function setBienCodePostal($bienCodePostal)
{
$this->bienCodePostal = $bienCodePostal;
return $this;
}
/**
* @return string
*/
public function getBienCodePostal()
{
return $this->bienCodePostal;
}
public function getBienDepartement()
{
if (strlen($this->bienCodePostal) === 5) {
return substr($this->bienCodePostal, 0, 2);
}
return $this->bienCodePostal;
}
/**
* @param string $bienVille
*
* @return Demande
*/
public function setBienVille($bienVille)
{
$this->bienVille = $bienVille;
return $this;
}
/**
* @return string
*/
public function getBienVille()
{
return $this->bienVille;
}
/**
* @param string $bienNombrePieces
*
* @return Demande
*/
public function setBienNombrePieces($bienNombrePieces)
{
$this->bienNombrePieces = $bienNombrePieces;
return $this;
}
/**
* Get goodRoomCount
*
* @return string
*/
public function getBienNombrePieces()
{
return $this->bienNombrePieces;
}
public function getBienNombrePiecesLabel()
{
return self::$nombrePieces[$this->bienNombrePieces];
}
/**
* @param string $bienSurface
*
* @return Demande
*/
public function setBienSurface($bienSurface)
{
$this->bienSurface = $bienSurface;
return $this;
}
/**
* @return string
*/
public function getBienSurface()
{
return $this->bienSurface;
}
/**
* @param string $bienAnneeConstruction
*
* @return Demande
*/
public function setBienAnneeConstruction($bienAnneeConstruction)
{
$this->bienAnneeConstruction = $bienAnneeConstruction;
return $this;
}
/**
* @return string
*/
public function getBienAnneeConstruction()
{
return $this->bienAnneeConstruction;
}
/**
* @param integer $bienGaz
*
* @return Demande
*/
public function setBienGaz($bienGaz)
{
$this->bienGaz = $bienGaz;
return $this;
}
/**
* @return integer
*/
public function getBienGaz()
{
return $this->bienGaz;
}
public function getBienGazLabel()
{
return self::$answers[$this->bienGaz];
}
public function getBienElectriciteLabel()
{
return self::$answers[$this->bienElectricite];
}
/**
* @param integer $bienElectricite
*
* @return Demande
*/
public function setBienElectricite($bienElectricite)
{
$this->bienElectricite = $bienElectricite;
return $this;
}
/**
* @return integer
*/
public function getBienElectricite()
{
return $this->bienElectricite;
}
/**
* Set goodPhoto
*
* @param string $bienPhotoPath
*
* @return Demande
*/
public function setBienPhotoPath($bienPhotoPath)
{
$this->bienPhotoPath = $bienPhotoPath;
return $this;
}
/**
* Get goodPhoto
*
* @return string
*/
public function getBienPhotoPath()
{
return $this->bienPhotoPath;
}
/**
*/
public function getBienPhoto()
{
return $this->bienPhoto;
}
/**
*/
public function setBienPhoto($bienPhoto)
{
$this->bienPhoto = $bienPhoto;
}
/**
* Set descriptionFile
*
* @param string $descriptionFilePath
*
* @return Demande
*/
public function setDescriptionFilePath($descriptionFilePath)
{
$this->descriptionFilePath = $descriptionFilePath;
return $this;
}
/**
* Get descriptionFile
*
* @return string
*/
public function getDescriptionFilePath()
{
return $this->descriptionFilePath;
}
/**
*/
public function getDescriptionFile()
{
return $this->descriptionFile;
}
/**
*/
public function setDescriptionFile($descriptionFile)
{
$this->descriptionFile = $descriptionFile;
}
/**
* Set expiration
*
* @param DateTime $expiration
*
* @return Demande
*/
public function setExpiration($expiration)
{
if ($expiration) {
$expiration->setTime(23, 59, 00);
}
$this->expiration = $expiration;
return $this;
}
/**
* Get expiration
*
* @return DateTime
*/
public function getExpiration()
{
return $this->expiration;
}
/**
* Set comment
*
* @param string $comment
*
* @return Demande
*/
public function setComment($comment)
{
$this->comment = $comment;
return $this;
}
/**
* Get comment
*
* @return string
*/
public function getComment()
{
return $this->comment;
}
/**
* Add transactions
*
* @param Transaction $transactions
*
* @return Demande
*/
public function addTransaction(Transaction $transactions)
{
$this->transactions[] = $transactions;
return $this;
}
/**
* Remove transactions
*
* @param Transaction $transactions
*/
public function removeTransaction(Transaction $transactions)
{
$this->transactions->removeElement($transactions);
}
/**
* Get transactions
*
* @return Transaction[]
*/
public function getTransactions()
{
return $this->transactions;
}
/**
* Set categorie
*
* @param Categorie $categorie
*
* @return Demande
*/
public function setCategorie(Categorie $categorie)
{
$this->categorie = $categorie;
return $this;
}
/**
* Get categorie
*
* @return Categorie
*/
public function getCategorie()
{
return $this->categorie;
}
/**
* Set bienCopropriete
*
* @param boolean $bienCopropriete
*
* @return Demande
*/
public function setBienCopropriete($bienCopropriete)
{
$this->bienCopropriete = $bienCopropriete;
return $this;
}
/**
* Get bienCopropriete
*
* @return boolean
*/
public function getBienCopropriete()
{
return $this->bienCopropriete;
}
public function getBienCoproprieteLabel()
{
return self::$coproprieteAnswers[$this->bienCopropriete];
}
/**
* Set prestation
*
* @param integer $prestation
*
* @return Demande
*/
public function setPrestation($prestation)
{
$this->prestation = $prestation;
return $this;
}
/**
* Get prestation
*
* @return integer
*/
public function getPrestation()
{
return $this->prestation;
}
public function getPrestationLabel()
{
return self::$prestationAnswers[$this->prestation];
}
/**
* @param string $provenance
*
* @return Demande
*/
public function setProvenance($provenance)
{
$this->provenance = preg_replace('/(https?:\/\/)?(www.)?([^\/]+)(.*)?/i', '\3', $provenance);
// $this->provenance = $provenance;
return $this;
}
/**
* @return string
*/
public function getProvenance()
{
return $this->provenance;
}
/**
* @param DateTime $modifiedDate
* @return Demande
*/
public function setModifiedDate($modifiedDate)
{
$this->modifiedDate = $modifiedDate;
return $this;
}
/**
* @return DateTime
*/
public function getModifiedDate()
{
return $this->modifiedDate;
}
public function isDisabled()
{
return $this->status === self::STATUS_NEW ||
$this->status === self::STATUS_DISABLED ||
$this->expiration < new DateTime();
}
public function isDeleted()
{
return $this->status === self::STATUS_DELETED;
}
public function isExpired()
{
return $this->expiration < new DateTime();
}
public function isEmpty()
{
$props = array(
'clientStatut',
'clientNom',
'clientEmail',
'clientTelephone',
'bienType',
'bienCodePostal',
'bienVille',
'bienNombrePieces',
'bienSurface',
'bienAnneeConstruction',
'bienCopropriete',
'bienGaz',
'bienElectricite',
'comment'
);
foreach ($props as $prop) {
if ($this->{$prop} !== null) {
return false;
}
}
return true;
}
public function isNew()
{
return $this->status === self::STATUS_NEW;
}
public function isBought()
{
return $this->status === self::STATUS_BOUGHT;
}
public function __toString()
{
return 'Demande n°'. $this->getId();
}
/**
* @return mixed
*/
public function getRefundRequests()
{
return $this->refundRequests;
}
public function getRefundRequest($prestataireId)
{
if ($this->refundRequests) {
/** @var RefundRequest $request */
foreach ($this->refundRequests as $request) {
if ($request->getPrestataire()->getId() == $prestataireId) {
return $request;
}
}
}
return null;
}
/**
* @param mixed $refundRequests
*/
public function setRefundRequests($refundRequests)
{
$this->refundRequests = $refundRequests;
}
public function getNbAchats()
{
$count = 0;
/** @var Transaction $transaction */
foreach ($this->transactions as $transaction) {
if ($transaction->isConfirmed()) {
++$count;
}
}
return $count;
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preSave()
{
$this->clientNom = mb_convert_case($this->clientNom, MB_CASE_TITLE);
}
/**
* @return array
*/
public function getSimilar()
{
return $this->similar;
}
/**
* @param array $similar
*/
public function setSimilar($similar)
{
$this->similar = $similar;
}
public function getEstimation(): ?bool
{
return $this->estimation;
}
public function setEstimation(?bool $estimation): self
{
$this->estimation = $estimation;
return $this;
}
}