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.     public function __construct()
  159.     {
  160.         parent::__construct();
  161.         $this->isEmailNotification true;
  162.         $this->isSmsNotification true;
  163.         $this->hasBeenPremium false;
  164.         $this->orders = new ArrayCollection();
  165.         $this->freelancerAssistants = new ArrayCollection();
  166.         $this->comments = new ArrayCollection();
  167.         $this->setBusinessModel(BusinessModelEnum::NONE);
  168.     }
  169.     /**
  170.      * @return mixed
  171.      */
  172.     public function getUpdatedAt(): ?DateTimeInterface
  173.     {
  174.         return $this->updatedAt;
  175.     }
  176.     /**
  177.      * @param mixed $updatedAt
  178.      */
  179.     public function setUpdatedAt(?DateTimeInterface $updatedAt): self
  180.     {
  181.         $this->updatedAt $updatedAt;
  182.         return $this;
  183.     }
  184.     public function getPicturePath(): ?string
  185.     {
  186.         return $this->picturePath;
  187.     }
  188.     public function setPicturePath(?string $picturePath)
  189.     {
  190.         $this->picturePath $picturePath;
  191.         return $this;
  192.     }
  193.     public function hasPicture()
  194.     {
  195.         return !is_null($this->picturePath);
  196.     }
  197.     public function setPictureFile(File $picturePath null)
  198.     {
  199.         $this->pictureFile $picturePath;
  200.         if ($picturePath) {
  201.             $this->updatedAt = new DateTime('now');
  202.         }
  203.     }
  204.     public function getPictureFile()
  205.     {
  206.         return $this->pictureFile;
  207.     }
  208.     /**
  209.      * Représente l'entité par son nom et son type (admin ou freelance)
  210.      * @return string
  211.      */
  212.     public function getLabelWithType()
  213.     {
  214.         return $this->getName() . ' [Indépendant]';
  215.     }
  216.     public function getNameAndEmailUser()
  217.     {
  218.         if ($this->getUser() !== null) {
  219.             return strtoupper($this->getName()) . ' [' $this->getUser()->getEmail() . ']';
  220.         } else {
  221.             return $this->getName();
  222.         }
  223.     }
  224.     public function getId(): ?int
  225.     {
  226.         return $this->id;
  227.     }
  228.     public function getVatNumber(): ?string
  229.     {
  230.         return $this->vatNumber;
  231.     }
  232.     public function setVatNumber(string $vatNumber): self
  233.     {
  234.         $this->vatNumber $vatNumber;
  235.         return $this;
  236.     }
  237.     public function setNbCredit(int $nbCredit): self
  238.     {
  239.         $this->nbCredit $nbCredit;
  240.         $this->updateCustomerStatus();
  241.         return $this;
  242.     }
  243.     public function setNbPendingLead(int $nbPendingLead): self
  244.     {
  245.         $this->nbPendingLead $nbPendingLead;
  246.         $this->updateCustomerStatus();
  247.         return $this;
  248.     }
  249.     /**
  250.      * when nbCredit or nbPendingLead is updated, we check if this freelancer is still a customer
  251.      * - freelance must be enabled
  252.      * AND
  253.      *      - freelance must have enough credit to receive leads
  254.      *      OR
  255.      *      - freelance must have opted-in for "pay as you go" credit && have less than 2 pending credit costs
  256.      */
  257.     public function updateCustomerStatus()
  258.     {
  259.         if ($this->isEnabled() && (($this->nbCredit $this->nbPendingLead) > || ($this->isPayAsYouGo() && !$this->isPayAsYouGoThresholdReached()))) {
  260.             $this->setIsCustomer(true);
  261.         } else {
  262.             $this->setIsCustomer(false);
  263.         }
  264.     }
  265.     public function getNbPendingLead(): ?int
  266.     {
  267.         return $this->nbPendingLead;
  268.     }
  269.     public function getSiretNumber(): ?string
  270.     {
  271.         return $this->siretNumber;
  272.     }
  273.     public function setSiretNumber(string $siretNumber): self
  274.     {
  275.         $siretNumber str_replace(' '''$siretNumber);
  276.         $this->siretNumber $siretNumber;
  277.         return $this;
  278.     }
  279.     public function getPhoneNumberSmsNotification(): ?string
  280.     {
  281.         return $this->phoneNumberSmsNotification;
  282.     }
  283.     public function setPhoneNumberSmsNotification(?string $phoneNumberSmsNotification): self
  284.     {
  285.         $this->phoneNumberSmsNotification $phoneNumberSmsNotification;
  286.         return $this;
  287.     }
  288.     public function mustBeNotifiedBySms(): ?bool
  289.     {
  290.         return $this->getIsSmsNotification() && $this->getPhoneNumberSmsNotification();
  291.     }
  292.     /**
  293.      * @return string
  294.      * @deprecated use Formatter->phoneNumberInternationalFormat instead
  295.      */
  296.     public function getPhoneNumberSmsNotificationFormatted()
  297.     {
  298.         return wordwrap($this->getPhoneNumberSmsNotification(), 2' 'true);
  299.     }
  300.     public function getEmailNotification(): ?string
  301.     {
  302.         return $this->emailNotification;
  303.     }
  304.     public function setEmailNotification(?string $emailNotification): self
  305.     {
  306.         $this->emailNotification $emailNotification;
  307.         return $this;
  308.     }
  309.     public function mustBeNotifiedByEmail(): ?bool
  310.     {
  311.         return $this->getIsEmailNotification() && !empty($this->getEmailNotification());
  312.     }
  313.     // Returns an array with every mail addresses for a store
  314.     public function getEmailList(): array
  315.     {
  316.         // Optim: use constant for delimiter?
  317.         return explode(";"$this->getEmailNotification());
  318.     }
  319.     public function getIsSmsNotification(): ?bool
  320.     {
  321.         return $this->isSmsNotification;
  322.     }
  323.     public function setIsSmsNotification(bool $isSmsNotification): self
  324.     {
  325.         $this->isSmsNotification $isSmsNotification;
  326.         return $this;
  327.     }
  328.     public function getIsEmailNotification(): ?bool
  329.     {
  330.         return $this->isEmailNotification;
  331.     }
  332.     public function setIsEmailNotification(bool $isEmailNotification): self
  333.     {
  334.         $this->isEmailNotification $isEmailNotification;
  335.         return $this;
  336.     }
  337.     /**
  338.      * @return bool
  339.      */
  340.     public function isHasBeenPremium(): bool
  341.     {
  342.         return $this->hasBeenPremium;
  343.     }
  344.     /**
  345.      * @param bool $hasBeenPremium
  346.      */
  347.     public function setHasBeenPremium(bool $hasBeenPremium): void
  348.     {
  349.         $this->hasBeenPremium $hasBeenPremium;
  350.     }
  351.     /**
  352.      * @return Collection|Order[]
  353.      */
  354.     public function getOrders(): Collection
  355.     {
  356.         return $this->orders;
  357.     }
  358.     public function addOrder(Order $order): self
  359.     {
  360.         if (!$this->orders->contains($order)) {
  361.             $this->orders[] = $order;
  362.             $order->setFreelancer($this);
  363.         }
  364.         return $this;
  365.     }
  366.     public function removeOrder(Order $order): self
  367.     {
  368.         if ($this->orders->contains($order)) {
  369.             $this->orders->removeElement($order);
  370.             // set the owning side to null (unless already changed)
  371.             if ($order->getFreelancer() === $this) {
  372.                 $order->setFreelancer(null);
  373.             }
  374.         }
  375.         return $this;
  376.     }
  377.     /**
  378.      * @return bool
  379.      */
  380.     public function isEnabled(): bool
  381.     {
  382.         return $this->isEnabled;
  383.     }
  384.     /**
  385.      * @param bool $isEnabled
  386.      */
  387.     public function setIsEnabled(bool $isEnabled): void
  388.     {
  389.         $this->isEnabled $isEnabled;
  390.         $this->updateCustomerStatus();
  391.     }
  392.     public function getHasBeenPremium(): ?bool
  393.     {
  394.         return $this->hasBeenPremium;
  395.     }
  396.     public function getIsEnabled(): ?bool
  397.     {
  398.         return $this->isEnabled;
  399.     }
  400.     /**
  401.      * @return Collection|FreelancerAssistant[]
  402.      */
  403.     public function getFreelancerAssistants(): Collection
  404.     {
  405.         return $this->freelancerAssistants;
  406.     }
  407.     public function addFreelancerAssistant(FreelancerAssistant $freelancerAssistant): self
  408.     {
  409.         if (!$this->freelancerAssistants->contains($freelancerAssistant)) {
  410.             $this->freelancerAssistants[] = $freelancerAssistant;
  411.             $freelancerAssistant->setFreelancer($this);
  412.         }
  413.         return $this;
  414.     }
  415.     public function removeFreelancerAssistant(FreelancerAssistant $freelancerAssistant): self
  416.     {
  417.         if ($this->freelancerAssistants->contains($freelancerAssistant)) {
  418.             $this->freelancerAssistants->removeElement($freelancerAssistant);
  419.             // set the owning side to null (unless already changed)
  420.             if ($freelancerAssistant->getFreelancer() === $this) {
  421.                 $freelancerAssistant->setFreelancer(null);
  422.             }
  423.         }
  424.         return $this;
  425.     }
  426.     public function getSoldOutAt(): ?DateTimeInterface
  427.     {
  428.         return $this->soldOutAt;
  429.     }
  430.     public function setSoldOutAt(?DateTimeInterface $soldOutAt): self
  431.     {
  432.         $this->soldOutAt $soldOutAt;
  433.         return $this;
  434.     }
  435.     public function getNbCreditSms(): ?int
  436.     {
  437.         return $this->nbCreditSms;
  438.     }
  439.     public function setNbCreditSms(?int $nbCreditSms): self
  440.     {
  441.         $this->nbCreditSms $nbCreditSms;
  442.         return $this;
  443.     }
  444.     /**
  445.      * @return Collection|FreelancerComment[]
  446.      */
  447.     public function getComments(): Collection
  448.     {
  449.         return $this->comments;
  450.     }
  451.     public function addComment(FreelancerComment $comment): self
  452.     {
  453.         if (!$this->comments->contains($comment)) {
  454.             $this->comments[] = $comment;
  455.             $comment->setFreelancer($this);
  456.         }
  457.         return $this;
  458.     }
  459.     public function removeComment(FreelancerComment $comment): self
  460.     {
  461.         if ($this->comments->contains($comment)) {
  462.             $this->comments->removeElement($comment);
  463.             // set the owning side to null (unless already changed)
  464.             if ($comment->getFreelancer() === $this) {
  465.                 $comment->setFreelancer(null);
  466.             }
  467.         }
  468.         return $this;
  469.     }
  470.     public function getLastCommentExtract(): ?string
  471.     {
  472.         return !$this->comments->isEmpty() ? substr($this->comments->last()->getComment(), 030) : null;
  473.     }
  474.     public function getLogoPath(): ?string
  475.     {
  476.         return $this->logoPath;
  477.     }
  478.     public function setLogoPath(?string $logoPath): self
  479.     {
  480.         $this->logoPath $logoPath;
  481.         return $this;
  482.     }
  483.     public function getBusinessSoftware(): ?int
  484.     {
  485.         return $this->businessSoftware;
  486.     }
  487.     public function setBusinessSoftware(?int $businessSoftware): self
  488.     {
  489.         $this->businessSoftware $businessSoftware;
  490.         return $this;
  491.     }
  492.     public function getBusinessSoftwareName(): string
  493.     {
  494.         if (is_null($this->getBusinessSoftware())) {
  495.             return 'Aucune';
  496.         }
  497.         return FreelancerBusinessSoftwareEnum::getItemName($this->getBusinessSoftware());
  498.     }
  499.     public function setLogoFile(File $logoPath null)
  500.     {
  501.         $this->logoFile $logoPath;
  502.         if ($logoPath) {
  503.             $this->updatedAt = new DateTime('now');
  504.         }
  505.     }
  506.     public function getLogoFile()
  507.     {
  508.         return $this->logoFile;
  509.     }
  510.     public function setBusinessModel(int $businessModel): void
  511.     {
  512.         parent::setBusinessModel($businessModel);
  513.         $this->updateCustomerStatus();
  514.     }
  515.     public function hasAllStoresIndexableTemporary(): bool
  516.     {
  517.         //if a deadline is set, stores remain indexable
  518.         return !is_null($this->businessModelDeadline);// && $this->businessModelDeadline->format('Y-m-d H:i:s') == self::BUSINESS_MODEL_FAKE_DEADLINE;
  519.     }
  520.     public function markAsAllStoresIndexableTemporary(): void
  521.     {
  522.         $this->setBusinessModelDeadline(new DateTime(self::BUSINESS_MODEL_FAKE_DEADLINE));
  523.     }
  524.     public function getCreditCosts(): Collection
  525.     {
  526.         return $this->creditCosts;
  527.     }
  528.     public function addCreditCost(CreditCost $creditCost): self
  529.     {
  530.         if (!$this->creditCosts->contains($creditCost)) {
  531.             $this->creditCosts->add($creditCost);
  532.             $creditCost->setAdministrator($this);
  533.         }
  534.         return $this;
  535.     }
  536.     public function getPendingCreditCosts(): Collection
  537.     {
  538.         return $this->creditCosts->filter(function (CreditCost $creditCost) {
  539.             return $creditCost->isPending();
  540.         });
  541.     }
  542.     public function getNbPendingCreditCosts(): int
  543.     {
  544.         return $this->getPendingCreditCosts()->count();
  545.     }
  546.     public function isPayAsYouGoThresholdReached(): bool
  547.     {
  548.         if (is_null($this->getPayAsYouGoCapping())) {
  549.             return false;
  550.         }
  551.         return $this->getNbPendingCreditCosts() + $this->getNbPendingLead() >= $this->getPayAsYouGoCapping();
  552.     }
  553.     public function setCreditCosts(Collection $creditCosts): void
  554.     {
  555.         $this->creditCosts $creditCosts;
  556.     }
  557.     public function getPayAsYouGoCapping(): ?int
  558.     {
  559.         if (is_null($this->payAsYouGoCapping)) {
  560.             return self::PAYASYOUGO_CAPPING_DEFAULT;
  561.         }
  562.         return $this->payAsYouGoCapping;
  563.     }
  564.     public function setPayAsYouGoCapping(?int $payAsYouGoCapping): void
  565.     {
  566.         $this->payAsYouGoCapping $payAsYouGoCapping;
  567.         $this->updateCustomerStatus();
  568.     }
  569.     public function getOnboardingStatus(): string
  570.     {
  571.         return $this->onboardingStatus;
  572.     }
  573.     public function setOnboardingStatus(string $onboardingStatus): void
  574.     {
  575.         $this->onboardingStatus $onboardingStatus;
  576.     }
  577.     public function getNbCreditPurchased(): int
  578.     {
  579.         return $this->nbCreditPurchased;
  580.     }
  581.     public function setNbCreditPurchased(int $nbCreditPurchased): void
  582.     {
  583.         $this->nbCreditPurchased $nbCreditPurchased;
  584.     }
  585.     public function getNbLeadDelivered(): int
  586.     {
  587.         return $this->nbLeadDelivered;
  588.     }
  589.     public function setNbLeadDelivered(int $nbLeadDelivered): void
  590.     {
  591.         $this->nbLeadDelivered $nbLeadDelivered;
  592.     }
  593.     public function getNbSales(): int
  594.     {
  595.         return $this->nbSales;
  596.     }
  597.     public function setNbSales(int $nbSales): void
  598.     {
  599.         $this->nbSales $nbSales;
  600.     }
  601.     /**
  602.      */
  603.     public function hasAtLeastOneIndexableStore(): bool
  604.     {
  605.         return $this->getStores()->exists(function ($key$store) {
  606.             return $store->hasCurrentStoreIndexation();
  607.         });
  608.     }
  609.     public function getAgeCapping(): ?int
  610.     {
  611.         return 50;
  612.     }
  613. }