src/Entity/Freelancer.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Enum\BusinessModelEnum;
  4. use App\Entity\Enum\FreelancerBusinessSoftwareEnum;
  5. use App\Entity\Enum\OnboardingStatusEnum;
  6. use App\Entity\Traits\CreditOwnerInterface;
  7. use App\Entity\Traits\CreditOwnerTrait;
  8. use DateTime;
  9. use DateTimeInterface;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Symfony\Component\HttpFoundation\File\File;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  16. use Gedmo\Mapping\Annotation as Gedmo;
  17. /**
  18.  * @ORM\Entity(repositoryClass="App\Repository\FreelancerRepository")
  19.  * @ORM\EntityListeners({"App\Listener\FreelancerListener"})
  20.  * @Gedmo\Loggable(logEntryClass="App\Entity\LogEntry")
  21.  * @Vich\Uploadable
  22.  */
  23. class Freelancer extends Administrator implements CreditOwnerInterface
  24. {
  25.     use CreditOwnerTrait;
  26.     public const CREDIT_ALMOST_DOWN 2;
  27.     private const PAYASYOUGO_CAPPING_DEFAULT 5;
  28.     /**
  29.      * Fake deadline used to identify freelance who have their store indexable till they reach 0 credit
  30.      */
  31.     public const BUSINESS_MODEL_FAKE_DEADLINE '2030-01-01 00:00:00';
  32.     /**
  33.      * Deadline relacing the 2 months previously used before passing businessModel from 'credit' to 'none'
  34.      */
  35.     public const BUSINESS_FORCED_DEADLINE '2025-05-20 00:00:00';
  36.     /**
  37.      * @ORM\Column(type="string", length=25, nullable=true)
  38.      */
  39.     private $vatNumber;
  40.     /**
  41.      * @Assert\NotBlank()
  42.      * @Assert\Regex(pattern="/^\d{14}$/", message="Le numéro de SIRET n'est pas valide.")
  43.      * @ORM\Column(type="string", length=25)
  44.      */
  45.     private $siretNumber;
  46.     /**
  47.      * @Assert\Regex(pattern="/^(?:\+33|0)\s*[1-9](?:[\s.-]*\d{2}){4}$/", message="Le numéro de téléphone n'est pas valide.")
  48.      * @ORM\Column(type="string", length=255, nullable=true)
  49.      */
  50.     private $phoneNumberSmsNotification;
  51.     /**
  52.      * @ORM\Column(type="string", length=255, nullable=true)
  53.      */
  54.     private $emailNotification;
  55.     /**
  56.      * @var boolean
  57.      * @ORM\Column(type="boolean")
  58.      */
  59.     private $isSmsNotification;
  60.     /**
  61.      * @var boolean
  62.      * @ORM\Column(type="boolean")
  63.      */
  64.     private $isEmailNotification;
  65.     /**
  66.      * specify if this freelancer has already been an premium member (even if nbCredit is null)
  67.      * @var boolean
  68.      * @ORM\Column(type="boolean", options={"default" : false})
  69.      */
  70.     private $hasBeenPremium;
  71.     /**
  72.      * @ORM\Column(type="datetime", nullable=true)
  73.      */
  74.     private $soldOutAt;
  75.     /**
  76.      * specify if this freelancer is active and can receive prospect
  77.      * @var boolean
  78.      * @ORM\Column(type="boolean", options={"default" : true})
  79.      */
  80.     private $isEnabled 1;
  81.     /**
  82.      * @ORM\OneToMany(targetEntity="App\Entity\Order", mappedBy="freelancer", cascade={"remove"})
  83.      */
  84.     private $orders;
  85.     /**
  86.      * @ORM\OneToMany(targetEntity=FreelancerAssistant::class, mappedBy="freelancer")
  87.      */
  88.     private $freelancerAssistants;
  89.     /**
  90.      * @ORM\Column(type="datetime", nullable=true)
  91.      */
  92.     private $updatedAt;
  93.     /**
  94.      * @ORM\Column(type="string", length=255, nullable=true)
  95.      */
  96.     private $picturePath;
  97.     /**
  98.      * @assert\File( mimeTypes = {"image/jpeg", "image/png", "image/gif", "image/jpg","image/webp"})
  99.      * @Vich\UploadableField(mapping="freelancer_images", fileNameProperty="picturePath")
  100.      * @var File
  101.      */
  102.     private $pictureFile;
  103.     /**
  104.      * @ORM\Column(type="string", length=255, nullable=true)
  105.      */
  106.     private $logoPath;
  107.     /**
  108.      * @assert\File( mimeTypes = {"image/jpeg", "image/png", "image/gif", "image/jpg","image/webp"})
  109.      * @Vich\UploadableField(mapping="freelancer_logo", fileNameProperty="logoPath")
  110.      * @var File
  111.      */
  112.     private $logoFile;
  113.     /**
  114.      * @ORM\Column(type="integer")
  115.      */
  116.     private $nbPendingLead;
  117.     /**
  118.      * @ORM\Column(type="integer", nullable=true)
  119.      */
  120.     private $nbCreditSms;
  121.     /**
  122.      * @ORM\OneToMany(targetEntity=FreelancerComment::class, mappedBy="freelancer", orphanRemoval=true)
  123.      */
  124.     private $comments;
  125.     /**
  126.      * @ORM\Column(type="integer", nullable=true)
  127.      */
  128.     private $businessSoftware;
  129.     /**
  130.      * @ORM\Column(type="integer", nullable=true)
  131.      * @Assert\Range(
  132.      *      min = 1,
  133.      *      max = 99,
  134.      *      notInRangeMessage = "Le nombre de contact doit être entre {{ min }} et {{ max }}",
  135.      * )
  136.      */
  137.     private ?int $payAsYouGoCapping self::PAYASYOUGO_CAPPING_DEFAULT;
  138.     /**
  139.      * @ORM\OneToMany(targetEntity=CreditCost::class, mappedBy="administrator")
  140.      */
  141.     private Collection $creditCosts;
  142.     /**
  143.      * @ORM\Column(type="string", length=255, nullable=true)
  144.      */
  145.     private string $onboardingStatus OnboardingStatusEnum::IN_PROGRESS;
  146.     /**
  147.      * @ORM\Column(type="integer", name="stat_nb_credit_purchased")
  148.      */
  149.     private int $nbCreditPurchased 0;
  150.     /**
  151.      * @ORM\Column(type="integer", name="stat_nb_lead_delivered")
  152.      */
  153.     private int $nbLeadDelivered 0;
  154.     /**
  155.      * @ORM\Column(type="integer", name="stat_nb_sales")
  156.      */
  157.     private int $nbSales 0;
  158.     /**
  159.      * @ORM\Column(type="boolean", options={"default" : false})
  160.      */
  161.     private bool $schedulingAppointment false;
  162.     public function __construct()
  163.     {
  164.         parent::__construct();
  165.         $this->isEmailNotification true;
  166.         $this->isSmsNotification true;
  167.         $this->hasBeenPremium false;
  168.         $this->orders = new ArrayCollection();
  169.         $this->freelancerAssistants = new ArrayCollection();
  170.         $this->comments = new ArrayCollection();
  171.         $this->setBusinessModel(BusinessModelEnum::NONE);
  172.     }
  173.     public function setBusinessModel(int $businessModel): void
  174.     {
  175.         parent::setBusinessModel($businessModel);
  176.         $this->updateCustomerStatus();
  177.     }
  178.     /**
  179.      * when nbCredit or nbPendingLead is updated, we check if this freelancer is still a customer
  180.      * - freelance must be enabled
  181.      * AND
  182.      *      - freelance must have enough credit to receive leads
  183.      *      OR
  184.      *      - freelance must have opted-in for "pay as you go" credit && have less than 2 pending credit costs
  185.      */
  186.     public function updateCustomerStatus()
  187.     {
  188.         if ($this->isEnabled() && (($this->nbCredit $this->nbPendingLead) > || ($this->isPayAsYouGo() && !$this->isPayAsYouGoThresholdReached()))) {
  189.             $this->setIsCustomer(true);
  190.         } else {
  191.             $this->setIsCustomer(false);
  192.         }
  193.     }
  194.     /**
  195.      * @return bool
  196.      */
  197.     public function isEnabled(): bool
  198.     {
  199.         return $this->isEnabled;
  200.     }
  201.     public function isPayAsYouGoThresholdReached(): bool
  202.     {
  203.         if (is_null($this->getPayAsYouGoCapping())) {
  204.             return false;
  205.         }
  206.         return $this->getNbPendingCreditCosts() + $this->getNbPendingLead() >= $this->getPayAsYouGoCapping();
  207.     }
  208.     public function getPayAsYouGoCapping(): ?int
  209.     {
  210.         if (is_null($this->payAsYouGoCapping)) {
  211.             return self::PAYASYOUGO_CAPPING_DEFAULT;
  212.         }
  213.         return $this->payAsYouGoCapping;
  214.     }
  215.     public function setPayAsYouGoCapping(?int $payAsYouGoCapping): void
  216.     {
  217.         $this->payAsYouGoCapping $payAsYouGoCapping;
  218.         $this->updateCustomerStatus();
  219.     }
  220.     public function getNbPendingCreditCosts(): int
  221.     {
  222.         return $this->getPendingCreditCosts()->count();
  223.     }
  224.     public function getPendingCreditCosts(): Collection
  225.     {
  226.         return $this->creditCosts->filter(function (CreditCost $creditCost) {
  227.             return $creditCost->isPending();
  228.         });
  229.     }
  230.     public function getNbPendingLead(): ?int
  231.     {
  232.         return $this->nbPendingLead;
  233.     }
  234.     public function setNbPendingLead(int $nbPendingLead): self
  235.     {
  236.         $this->nbPendingLead $nbPendingLead;
  237.         $this->updateCustomerStatus();
  238.         return $this;
  239.     }
  240.     /**
  241.      * @return mixed
  242.      */
  243.     public function getUpdatedAt(): ?DateTimeInterface
  244.     {
  245.         return $this->updatedAt;
  246.     }
  247.     /**
  248.      * @param mixed $updatedAt
  249.      */
  250.     public function setUpdatedAt(?DateTimeInterface $updatedAt): self
  251.     {
  252.         $this->updatedAt $updatedAt;
  253.         return $this;
  254.     }
  255.     public function getPicturePath(): ?string
  256.     {
  257.         return $this->picturePath;
  258.     }
  259.     public function setPicturePath(?string $picturePath)
  260.     {
  261.         $this->picturePath $picturePath;
  262.         return $this;
  263.     }
  264.     public function hasPicture()
  265.     {
  266.         return !is_null($this->picturePath);
  267.     }
  268.     public function getPictureFile()
  269.     {
  270.         return $this->pictureFile;
  271.     }
  272.     public function setPictureFile(File $picturePath null)
  273.     {
  274.         $this->pictureFile $picturePath;
  275.         if ($picturePath) {
  276.             $this->updatedAt = new DateTime('now');
  277.         }
  278.     }
  279.     /**
  280.      * Représente l'entité par son nom et son type (admin ou freelance)
  281.      * @return string
  282.      */
  283.     public function getLabelWithType()
  284.     {
  285.         return $this->getName() . ' [Indépendant]';
  286.     }
  287.     public function getNameAndEmailUser()
  288.     {
  289.         if ($this->getUser() !== null) {
  290.             return strtoupper($this->getName()) . ' [' $this->getUser()->getEmail() . ']';
  291.         } else {
  292.             return $this->getName();
  293.         }
  294.     }
  295.     public function getId(): ?int
  296.     {
  297.         return $this->id;
  298.     }
  299.     public function getVatNumber(): ?string
  300.     {
  301.         return $this->vatNumber;
  302.     }
  303.     public function setVatNumber(string $vatNumber): self
  304.     {
  305.         $this->vatNumber $vatNumber;
  306.         return $this;
  307.     }
  308.     public function setNbCredit(int $nbCredit): self
  309.     {
  310.         $this->nbCredit $nbCredit;
  311.         $this->updateCustomerStatus();
  312.         return $this;
  313.     }
  314.     public function getSiretNumber(): ?string
  315.     {
  316.         return $this->siretNumber;
  317.     }
  318.     public function setSiretNumber(string $siretNumber): self
  319.     {
  320.         $siretNumber str_replace(' '''$siretNumber);
  321.         $this->siretNumber $siretNumber;
  322.         return $this;
  323.     }
  324.     // Returns an array with every mail addresses for a store
  325.     public function mustBeNotifiedBySms(): ?bool
  326.     {
  327.         return $this->getIsSmsNotification() && $this->getPhoneNumberSmsNotification();
  328.     }
  329.     public function getIsSmsNotification(): ?bool
  330.     {
  331.         return $this->isSmsNotification;
  332.     }
  333.     public function setIsSmsNotification(bool $isSmsNotification): self
  334.     {
  335.         $this->isSmsNotification $isSmsNotification;
  336.         return $this;
  337.     }
  338.     public function getPhoneNumberSmsNotification(): ?string
  339.     {
  340.         return $this->phoneNumberSmsNotification;
  341.     }
  342.     public function setPhoneNumberSmsNotification(?string $phoneNumberSmsNotification): self
  343.     {
  344.         $this->phoneNumberSmsNotification $phoneNumberSmsNotification;
  345.         return $this;
  346.     }
  347.     /**
  348.      * @return string
  349.      * @deprecated use Formatter->phoneNumberInternationalFormat instead
  350.      */
  351.     public function getPhoneNumberSmsNotificationFormatted()
  352.     {
  353.         return wordwrap($this->getPhoneNumberSmsNotification(), 2' 'true);
  354.     }
  355.     public function mustBeNotifiedByEmail(): ?bool
  356.     {
  357.         return $this->getIsEmailNotification() && !empty($this->getEmailNotification());
  358.     }
  359.     public function getIsEmailNotification(): ?bool
  360.     {
  361.         return $this->isEmailNotification;
  362.     }
  363.     public function setIsEmailNotification(bool $isEmailNotification): self
  364.     {
  365.         $this->isEmailNotification $isEmailNotification;
  366.         return $this;
  367.     }
  368.     public function getEmailNotification(): ?string
  369.     {
  370.         return $this->emailNotification;
  371.     }
  372.     public function setEmailNotification(?string $emailNotification): self
  373.     {
  374.         $this->emailNotification $emailNotification;
  375.         return $this;
  376.     }
  377.     public function getEmailList(): array
  378.     {
  379.         // Optim: use constant for delimiter?
  380.         return explode(";"$this->getEmailNotification());
  381.     }
  382.     /**
  383.      * @return bool
  384.      */
  385.     public function isHasBeenPremium(): bool
  386.     {
  387.         return $this->hasBeenPremium;
  388.     }
  389.     public function getHasBeenPremium(): ?bool
  390.     {
  391.         return $this->hasBeenPremium;
  392.     }
  393.     /**
  394.      * @param bool $hasBeenPremium
  395.      */
  396.     public function setHasBeenPremium(bool $hasBeenPremium): void
  397.     {
  398.         $this->hasBeenPremium $hasBeenPremium;
  399.     }
  400.     /**
  401.      * @return Collection|Order[]
  402.      */
  403.     public function getOrders(): Collection
  404.     {
  405.         return $this->orders;
  406.     }
  407.     public function addOrder(Order $order): self
  408.     {
  409.         if (!$this->orders->contains($order)) {
  410.             $this->orders[] = $order;
  411.             $order->setFreelancer($this);
  412.         }
  413.         return $this;
  414.     }
  415.     public function removeOrder(Order $order): self
  416.     {
  417.         if ($this->orders->contains($order)) {
  418.             $this->orders->removeElement($order);
  419.             // set the owning side to null (unless already changed)
  420.             if ($order->getFreelancer() === $this) {
  421.                 $order->setFreelancer(null);
  422.             }
  423.         }
  424.         return $this;
  425.     }
  426.     public function getIsEnabled(): ?bool
  427.     {
  428.         return $this->isEnabled;
  429.     }
  430.     /**
  431.      * @param bool $isEnabled
  432.      */
  433.     public function setIsEnabled(bool $isEnabled): void
  434.     {
  435.         $this->isEnabled $isEnabled;
  436.         $this->updateCustomerStatus();
  437.     }
  438.     /**
  439.      * @return Collection|FreelancerAssistant[]
  440.      */
  441.     public function getFreelancerAssistants(): Collection
  442.     {
  443.         return $this->freelancerAssistants;
  444.     }
  445.     public function addFreelancerAssistant(FreelancerAssistant $freelancerAssistant): self
  446.     {
  447.         if (!$this->freelancerAssistants->contains($freelancerAssistant)) {
  448.             $this->freelancerAssistants[] = $freelancerAssistant;
  449.             $freelancerAssistant->setFreelancer($this);
  450.         }
  451.         return $this;
  452.     }
  453.     public function removeFreelancerAssistant(FreelancerAssistant $freelancerAssistant): self
  454.     {
  455.         if ($this->freelancerAssistants->contains($freelancerAssistant)) {
  456.             $this->freelancerAssistants->removeElement($freelancerAssistant);
  457.             // set the owning side to null (unless already changed)
  458.             if ($freelancerAssistant->getFreelancer() === $this) {
  459.                 $freelancerAssistant->setFreelancer(null);
  460.             }
  461.         }
  462.         return $this;
  463.     }
  464.     public function getSoldOutAt(): ?DateTimeInterface
  465.     {
  466.         return $this->soldOutAt;
  467.     }
  468.     public function setSoldOutAt(?DateTimeInterface $soldOutAt): self
  469.     {
  470.         $this->soldOutAt $soldOutAt;
  471.         return $this;
  472.     }
  473.     public function getNbCreditSms(): ?int
  474.     {
  475.         return $this->nbCreditSms;
  476.     }
  477.     public function setNbCreditSms(?int $nbCreditSms): self
  478.     {
  479.         $this->nbCreditSms $nbCreditSms;
  480.         return $this;
  481.     }
  482.     /**
  483.      * @return Collection|FreelancerComment[]
  484.      */
  485.     public function getComments(): Collection
  486.     {
  487.         return $this->comments;
  488.     }
  489.     public function addComment(FreelancerComment $comment): self
  490.     {
  491.         if (!$this->comments->contains($comment)) {
  492.             $this->comments[] = $comment;
  493.             $comment->setFreelancer($this);
  494.         }
  495.         return $this;
  496.     }
  497.     public function removeComment(FreelancerComment $comment): self
  498.     {
  499.         if ($this->comments->contains($comment)) {
  500.             $this->comments->removeElement($comment);
  501.             // set the owning side to null (unless already changed)
  502.             if ($comment->getFreelancer() === $this) {
  503.                 $comment->setFreelancer(null);
  504.             }
  505.         }
  506.         return $this;
  507.     }
  508.     public function getLastCommentExtract(): ?string
  509.     {
  510.         return !$this->comments->isEmpty() ? substr($this->comments->last()->getComment(), 030) : null;
  511.     }
  512.     public function getLogoPath(): ?string
  513.     {
  514.         return $this->logoPath;
  515.     }
  516.     public function setLogoPath(?string $logoPath): self
  517.     {
  518.         $this->logoPath $logoPath;
  519.         return $this;
  520.     }
  521.     public function getBusinessSoftwareName(): string
  522.     {
  523.         if (is_null($this->getBusinessSoftware())) {
  524.             return 'Aucune';
  525.         }
  526.         return FreelancerBusinessSoftwareEnum::getItemName($this->getBusinessSoftware());
  527.     }
  528.     public function getBusinessSoftware(): ?int
  529.     {
  530.         return $this->businessSoftware;
  531.     }
  532.     public function setBusinessSoftware(?int $businessSoftware): self
  533.     {
  534.         $this->businessSoftware $businessSoftware;
  535.         return $this;
  536.     }
  537.     public function getLogoFile()
  538.     {
  539.         return $this->logoFile;
  540.     }
  541.     public function setLogoFile(File $logoPath null)
  542.     {
  543.         $this->logoFile $logoPath;
  544.         if ($logoPath) {
  545.             $this->updatedAt = new DateTime('now');
  546.         }
  547.     }
  548.     public function hasAllStoresIndexableTemporary(): bool
  549.     {
  550.         //if a deadline is set, stores remain indexable
  551.         return !is_null($this->businessModelDeadline);// && $this->businessModelDeadline->format('Y-m-d H:i:s') == self::BUSINESS_MODEL_FAKE_DEADLINE;
  552.     }
  553.     public function markAsAllStoresIndexableTemporary(): void
  554.     {
  555.         $this->setBusinessModelDeadline(new DateTime(self::BUSINESS_MODEL_FAKE_DEADLINE));
  556.     }
  557.     public function getCreditCosts(): Collection
  558.     {
  559.         return $this->creditCosts;
  560.     }
  561.     public function setCreditCosts(Collection $creditCosts): void
  562.     {
  563.         $this->creditCosts $creditCosts;
  564.     }
  565.     public function addCreditCost(CreditCost $creditCost): self
  566.     {
  567.         if (!$this->creditCosts->contains($creditCost)) {
  568.             $this->creditCosts->add($creditCost);
  569.             $creditCost->setAdministrator($this);
  570.         }
  571.         return $this;
  572.     }
  573.     public function getOnboardingStatus(): string
  574.     {
  575.         return $this->onboardingStatus;
  576.     }
  577.     public function setOnboardingStatus(string $onboardingStatus): void
  578.     {
  579.         $this->onboardingStatus $onboardingStatus;
  580.     }
  581.     public function getNbCreditPurchased(): int
  582.     {
  583.         return $this->nbCreditPurchased;
  584.     }
  585.     public function setNbCreditPurchased(int $nbCreditPurchased): void
  586.     {
  587.         $this->nbCreditPurchased $nbCreditPurchased;
  588.     }
  589.     public function getNbLeadDelivered(): int
  590.     {
  591.         return $this->nbLeadDelivered;
  592.     }
  593.     public function setNbLeadDelivered(int $nbLeadDelivered): void
  594.     {
  595.         $this->nbLeadDelivered $nbLeadDelivered;
  596.     }
  597.     public function getNbSales(): int
  598.     {
  599.         return $this->nbSales;
  600.     }
  601.     public function setNbSales(int $nbSales): void
  602.     {
  603.         $this->nbSales $nbSales;
  604.     }
  605.     /**
  606.      */
  607.     public function hasAtLeastOneIndexableStore(): bool
  608.     {
  609.         return $this->getStores()->exists(function ($key$store) {
  610.             return $store->hasCurrentStoreIndexation();
  611.         });
  612.     }
  613.     public function getAgeCapping(): ?int
  614.     {
  615.         return 50;
  616.     }
  617.     public function isSchedulingAppointment(): bool
  618.     {
  619.         return $this->schedulingAppointment;
  620.     }
  621.     public function setSchedulingAppointment(bool $schedulingAppointment): void
  622.     {
  623.         $this->schedulingAppointment $schedulingAppointment;
  624.     }
  625. }