src/Entity/Campaign.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Service\MissionService;
  4. use App\Enum\Role;
  5. use App\Enum\ProductType;
  6. use App\Repository\CampaignRepository;
  7. use App\Service\CampaignService;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\DBAL\Types\Types;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Symfony\Component\Serializer\Annotation\Groups;
  13. use Symfony\Component\Uid\Uuid;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. #[ORM\Entity(repositoryClassCampaignRepository::class)]
  16. #[ORM\Table(name'campaigns')]
  17. class Campaign
  18. {
  19.     #[ORM\Id]
  20.     #[ORM\Column(type'string'length22uniquetrue)]
  21.     #[Groups(['campaign''mission_list''mission_read','message_read'])]
  22.     
  23.     private string $id;
  24.     #[ORM\Column(type'string')]
  25.     #[Assert\NotBlank(message'Merci de remplir ce champs')]
  26.     #[Groups(['campaign''mission_list''mission_read'])]
  27.     private ?string $name '';
  28.     #[ORM\Column(type'string')]
  29.     #[Groups(['campaign','mission_list','mission_read'])]
  30.     private string $state 'provisional';
  31.     #[ORM\Column(type'datetime'nullabletrue)]
  32.     private ?\DateTimeInterface $dateClosed null;
  33.     
  34.     #[ORM\Column(type'boolean'nullabletrue)]
  35.     private $isNew;
  36.     #[ORM\Column(type'boolean'nullabletrue)]
  37.     private $disabled;
  38.       #[ORM\Column(type'boolean'nullabletrue)]
  39.     private $confidentiality;
  40.     #[ORM\Column(type'datetime_immutable')]
  41.     #[Groups(['campaign'])]
  42.     private \DateTimeImmutable $createdAt;
  43.     #[ORM\ManyToOne(targetEntityCompany::class, inversedBy'campaigns',  cascade: ["persist"])]
  44.     #[Groups(['campaign''mission_read','mission_list'])]
  45.     private ?Company $company null;
  46.     #[ORM\OneToMany(mappedBy'campaign'targetEntityMission::class, cascade: ['persist'], orphanRemovaltrue)]
  47.     #[Groups(['campaign'])]
  48.     private Collection $missions;
  49.     #[ORM\ManyToOne(targetEntityUser::class, inversedBy'campaigns')]
  50.     private ?User $orderedBy null;
  51.     #[ORM\Column(type'boolean'nullabletrue)]
  52.     private ?bool $invoiced false;
  53.      #[ORM\Column(type'boolean'nullabletrue)]
  54.     private ?bool $activeFrequency false;
  55.     #[ORM\OneToMany(mappedBy'campaign'targetEntityInvoice::class, cascade: ['persist'])]
  56.     private ?Collection $invoices;
  57.     #[ORM\OneToOne(targetEntityRecurringService::class,cascade: ['persist'])]
  58.     private $frequency;
  59.     #[ORM\OneToMany(mappedBy'campaign'targetEntityMessage::class)]
  60.     #[Groups(['mission_read'])]
  61.     private ?Collection $messages;
  62.     #[ORM\Column(type'string'length255nullabletrue)]
  63.     private ?string $totalCostCampaign null;
  64.     #[ORM\Column(type'text'nullabletrue)]
  65.     private ?string $cancelReason null;
  66.     #[ORM\Column(type'text'nullabletrue)]
  67.     private ?string $brief null;
  68.     #[ORM\Column(type'boolean'nullabletrue)]
  69.     private $isPaidOnCredit;
  70.     #[ORM\Column(type'string'length255nullabletrue)]
  71.     private $paymentMethod;
  72.     #[ORM\Column(type'datetime_immutable'nullabletrue)]
  73.     private $activatedAt;
  74.     #[ORM\Column(type'string'length255nullabletrue)]
  75.     private $clientStatus;
  76.     #[ORM\Column(type'boolean'nullabletrue)]
  77.     private $isToBeinvoiced;
  78.     #[ORM\Column(type'float'nullabletrue)]
  79.     private $discountForCompany;
  80.     #[ORM\Column(type'boolean'nullabletrue)]
  81.     private $reSendToClient;
  82.      #[ORM\OneToMany(mappedBy'campaign'targetEntityFileMission::class, cascade: ["persist""remove"], orphanRemovaltrue)]
  83.     private ?Collection $fileMissions;
  84.      #[ORM\OneToMany(mappedBy'campaign'targetEntitynoteCampaing::class, cascade: ["persist""remove"], orphanRemovaltrue)]
  85.     private ?Collection $noteCampaing;
  86.     /**
  87.      * This property is not persited because there are a problème in the commande
  88.      * 
  89.      * @var string
  90.      */
  91.     private ?string $errorCommand null;
  92.     #[ORM\Column(type'string'length255nullabletrue)]
  93.     private $numberInitialOfMissions;
  94.     #[ORM\Column(type'string'length255nullabletrue)]
  95.     private $lastState;
  96.     #[ORM\ManyToOne(targetEntityCreditHistory::class, inversedBy'campaigns')]
  97.     private $creditHistory;
  98.     #[ORM\Column(type'text'nullabletrue)]
  99.     private $deleteReason;
  100.     #[ORM\Column(type'boolean')]
  101.     private $isOrdoredByAdmin;
  102.     #[ORM\Column(type'array'nullabletrue)]
  103.     private $defautObserver = [];
  104.     #[ORM\Column(type'array'nullabletrue)]
  105.     private $defaultValidator = [];
  106.     #[ORM\Column(type'array'nullabletrue)]
  107.     private $defaultValidatorExternal = [];
  108.     #[ORM\Column(type'array'nullabletrue)]
  109.     private $defautObserverExternal = [];
  110.     #[ORM\Column(nullabletrue)]
  111.     private ?\DateTimeImmutable $statusModifyAt null;
  112.     #[ORM\Column(nullabletrue)]
  113.     private ?bool $isRecurrent null;
  114.     #[ORM\Column(length255,nullabletrue)]
  115.     private ?string $recurringState null;
  116.     #[ORM\OneToMany(mappedBy'campaign'targetEntityCampaignPendingToSend::class)]
  117.     private $campaignPendingToSends;
  118.     #[ORM\Column(type'integer'nullabletrue)]
  119.     private $lastStepWp;
  120.     #[ORM\Column(type'integer'nullabletrue)]
  121.     private $frontWpId;
  122.     #[ORM\Column(type'string'length255nullabletrue)]
  123.     private $wpSlug;
  124.     #[ORM\Column(type'array'nullabletrue)]
  125.     #[Groups(['campaign''mission_list''mission_read'])]
  126.     private $stateHistory = [];
  127.     #[ORM\Column(type'integer'nullabletrue)]
  128.     private $typeInitiation;
  129.     #[ORM\Column(type'boolean'nullabletrue)]
  130.     private $emailSentInsuffBudget;
  131.     #[ORM\Column(type'array'nullabletrue)]
  132.     private $user_writing = [];
  133.     #[ORM\Column(type'string'length255nullabletrue)]
  134.     private $cloningSourceId;
  135.     #[ORM\Column(nullabletrue)]
  136.     private ?int $isActivated 0;
  137.     #[Groups(['mission_read'])]
  138.     private $isAlreadyStart=false;
  139.     #[ORM\Column(nullabletrue)]
  140.     private ?bool $newForLivrable null;
  141.     public function __construct()
  142.     {
  143.         $this->id Uuid::v4()->toBase58();
  144.         $this->createdAt = new \DateTimeImmutable();
  145.         $this->activatedAt = new \DateTimeImmutable();
  146.         $this->statusModifyAt = new \DateTimeImmutable();
  147.         $this->missions = new ArrayCollection();
  148.         $this->invoices = new ArrayCollection();
  149.         $this->messages = new ArrayCollection();
  150.         $this->isToBeinvoiced false;
  151.         $this->reSendToClientfalse;
  152.         $this->clientStatus="";
  153.         $this->isOrdoredByAdmin false;
  154.         $this->activeFrequency false;
  155.         $this->recurringState null;
  156.         $this->fileMissions = new ArrayCollection();
  157.         $this->confidentiality false;
  158.         $this->campaignPendingToSends = new ArrayCollection();
  159.         $this->stateHistory = [];
  160.         $this->emailSentInsuffBudget false;
  161.         $this->wpSlug "";
  162.         $this->newForLivrable false;
  163.         $this->noteCampaing = new ArrayCollection(); 
  164.     }
  165.     public function __clone()
  166.     {
  167.         $this->cloningSourceId =  $this->getId(); 
  168.         $this->id Uuid::v4()->toBase58();
  169.         $this->missions = new ArrayCollection();
  170.         $this->messages = new ArrayCollection();
  171.         $this->createdAt = new \DateTimeImmutable();
  172.         $this->activatedAt = new \DateTimeImmutable();;
  173.         $this->invoices = clone $this->invoices;
  174.         $this->frequency null;
  175.         
  176.        
  177.     
  178.     }
  179.     public function getId(): ?string
  180.     {
  181.         return $this->id;
  182.     }
  183.     public function generateNewId():self{
  184.         $this->id Uuid::v4()->toBase58() ;
  185.         return $this;
  186.     }
  187.     /**
  188.      * @return string
  189.      */
  190.     public function getName(): string
  191.     {
  192.         return $this->name;
  193.     }
  194.     /**
  195.      * @param string $name
  196.      * @return Campaign
  197.      */
  198.     public function setName(?string $name): Campaign
  199.     {
  200.         $this->name $name;
  201.         return $this;
  202.     }
  203.     /**
  204.      * @return \DateTimeImmutable|null
  205.      */
  206.     public function getCreatedAt(): ?\DateTimeInterface
  207.     {
  208.         return $this->createdAt;
  209.     }
  210.     /**
  211.      * @param \DateTimeImmutable|null $createdAt
  212.      * @return Campaign
  213.      */
  214.     public function setCreatedAt(?\DateTimeImmutable $createdAt): Campaign
  215.     {
  216.         $this->createdAt $createdAt;
  217.         return $this;
  218.     }
  219.     public function getCompany(): ?Company
  220.     {
  221.         return $this->company;
  222.     }
  223.     public function setCompany(?Company $company): self
  224.     {
  225.         $this->company $company;
  226.         return $this;
  227.     }
  228.     /**
  229.      * @return Collection|Mission[]
  230.      */
  231.     public function getMissions(): Collection
  232.     {
  233.         return $this->missions;
  234.     }
  235.     public function addMission(Mission $mission): self
  236.     {
  237.         if (!$this->missions->contains($mission)) {
  238.             $this->missions[] = $mission;
  239.             $mission->setCampaign($this);
  240.         }
  241.         return $this;
  242.     }
  243.     public function removeMission(Mission $mission): self
  244.     {
  245.         if ($this->missions->removeElement($mission)) {
  246.             // set the owning side to null (unless already changed)
  247.             if ($mission->getCampaign() === $this) {
  248.                 $mission->setCampaign(null);
  249.             }
  250.         }
  251.         return $this;
  252.     }
  253.     /**
  254.      * @return string
  255.      */
  256.     public function getState(): string
  257.     {
  258.         return $this->state;
  259.     }
  260.     /**
  261.      * @param string $state
  262.      * @return Campaign
  263.      */
  264.     public function setState(string $state): Campaign
  265.     {   
  266.         $this->statusModifyAt = new \DateTimeImmutable();
  267.         $notActivated $this->getCreatedAt() ==  $this->getActivatedAt() ? true false;
  268.         $isCampaignNotActivated is_null($this->getIsActivated()) || $this->getIsActivated() == true false
  269.         if ($this->getState() !== "in_progress" &&  ($state == "in_progress" || $state == "archived") && $notActivated && $isCampaignNotActivated){
  270.             $this->setActivatedAt(new \DateTimeImmutable());
  271.             $this->setIsActivated(1);
  272.         }
  273.         $this->setLastState($this->state);
  274.         $stateHistory $this->getStateHistory()!=null ?  $this->getStateHistory() : [];
  275.         $this->setStateHistory([...$stateHistory, [
  276.             [
  277.                 'date'=> date('d/M/Y h:i:s'), 
  278.                 'state'=>$this->state
  279.             ]
  280.         ]]);
  281.         $this->state $state;
  282.         //modify date closed
  283.         if ($state == "closed") {
  284.             $this->dateClosed = new \DateTimeImmutable();
  285.             $this->newForLivrable true;
  286.         }
  287.         //end modification
  288.         return $this;
  289.     }
  290.     
  291.     public function getOrderedBy(): ?User
  292.     {
  293.         return $this->orderedBy;
  294.     }
  295.     public function setOrderedBy(?User $orderedBy): self
  296.     {
  297.         $this->orderedBy $orderedBy;
  298.         return $this;
  299.     }
  300.     public function getProgress(): int
  301.     {
  302.         $progress 0;
  303.         foreach ($this->getMissionsWithoutEspaceDeDiscussion() as $mission) {
  304.             if (!empty($mission->getWorkflow())) {
  305.                 if ($mission->getState() == "finalized") {
  306.                     $progress +=100;
  307.                 }else{
  308.                     $progress += $mission->getWorkflow()->getProgress();
  309.                 }
  310.             }
  311.         }
  312.         $count 0;
  313.         foreach ($this->getMissionsWithoutEspaceDeDiscussion() as $mission) {
  314.             if ($mission->getProduct()->getName() != "Espace de discussion") {
  315.                 $count++;
  316.             }
  317.         }
  318.         if ($progress 0) {
  319.             return $progress $count;
  320.         }
  321.         // $progress  = $progress > 100 ? 100 :  $progress; 
  322.         return round($progress);
  323.     }
  324.     public function getTotalCost(): float
  325.     {
  326.         $cost 0;
  327.      
  328.         foreach ($this->getMissions() as $mission) {
  329.             if($mission->getState() != "cancelled"){
  330.                 $cost += $mission->getPrice() * $mission->getQuantity();
  331.                 
  332.             }
  333.         }
  334.         return $cost;
  335.     }
  336.     public function getInvoiced(): ?bool
  337.     {
  338.         return $this->invoiced;
  339.     }
  340.     public function setInvoiced(?bool $invoiced): self
  341.     {
  342.         $this->invoiced $invoiced;
  343.         return $this;
  344.     }
  345.     /**
  346.      * @return Collection|Invoice[]
  347.      */
  348.     public function getInvoices(): Collection
  349.     {
  350.         return $this->invoices;
  351.     }
  352.     public function addInvoice(Invoice $invoice): self
  353.     {
  354.         if (!$this->invoices->contains($invoice)) {
  355.             $this->invoices[] = $invoice;
  356.             $invoice->setCampaign($this);
  357.         }
  358.         return $this;
  359.     }
  360.     public function removeInvoice(Invoice $invoice): self
  361.     {
  362.         if ($this->invoices->removeElement($invoice)) {
  363.             // set the owning side to null (unless already changed)
  364.             if ($invoice->getCampaign() === $this) {
  365.                 $invoice->setCampaign(null);
  366.             }
  367.         }
  368.         return $this;
  369.     }
  370.     /**
  371.      * @return Collection|Message[]
  372.      */
  373.     public function getMessages(): Collection
  374.     {
  375.         return $this->messages;
  376.     }
  377.     public function addMessage(Message $message): self
  378.     {
  379.         if (!$this->messages->contains($message)) {
  380.             $this->messages[] = $message;
  381.             $message->setCampaign($this);
  382.         }
  383.         return $this;
  384.     }
  385.     public function subcontractorActivatedForfait($idUser){
  386.         foreach ($this->getMissions() as $mission) {
  387.             foreach ($mission->getParticipants() as $key => $participant) {
  388.                 if ($idUser == $participant->getUser()->getId() and $participant->isActivated()) {
  389.                     return true;
  390.                 }
  391.             }
  392.         }
  393.         return false;
  394.     }
  395.     public function allProductIsForfait(){
  396.      
  397.         foreach ($this->getMissions() as $mission) {
  398.             if ($mission->getProduct()->getType() != ProductType::AU_FORFAIT) {
  399.                 return false;
  400.             }
  401.         }
  402.         return true;
  403.     }
  404.     public function removeMessage(Message $message): self
  405.     {
  406.         if ($this->messages->removeElement($message)) {
  407.             // set the owning side to null (unless already changed)
  408.             if ($message->getCampaign() === $this) {
  409.                 $message->setCampaign(null);
  410.             }
  411.         }
  412.         return $this;
  413.     }
  414.     public function getTotalCostCampaign(): ?string
  415.     {
  416.         return $this->totalCostCampaign;
  417.     }
  418.     public function setTotalCostCampaign(?string $totalCostCampaign): self
  419.     {
  420.         $this->totalCostCampaign $totalCostCampaign;
  421.         return $this;
  422.     }
  423.     /**
  424.      * @return string|null
  425.      */
  426.     public function getCancelReason(): ?string
  427.     {
  428.         return $this->cancelReason;
  429.     }
  430.     /**
  431.      * @param string|null $cancelReason
  432.      */
  433.     public function setCancelReason(?string $cancelReason): void
  434.     {
  435.         $this->cancelReason $cancelReason;
  436.     }
  437.     /**
  438.      * @return string|null
  439.      */
  440.     public function getBrief(): ?string
  441.     {
  442.         return $this->addTargetBlankToBriefLink($this->brief);
  443.     }
  444.     /**
  445.      * @param string|null $brief
  446.      */
  447.     public function setBrief(?string $brief):void
  448.     {
  449.         $this->brief $this->addTargetBlankToBriefLink($brief);
  450.     }
  451.     /**
  452.      * @param string|null $brief
  453.      * @return string|null $brief
  454.      */
  455.     private function addTargetBlankToBriefLink(?string $brief): ?string {
  456.         $regex '/<a\s+(?:[^>]*?\s+)?href="([^"]*)"(?:[^>]*?\s+)?(target="_blank")?>([^<]*)<\/a>/i';
  457.         if(!is_null($brief)){
  458.             preg_match_all($regex,$brief,$matches,PREG_SET_ORDER);
  459.             foreach ($matches as $match){
  460.                 // $match[1] est l'URL du lien
  461.                 // $match[2] est l'attribut target si prĂ©sent
  462.                 // $match[3] est le contenu du lien
  463.                 if (!empty($match[1]) && empty($match[2])) {
  464.                     // Ajoutez l'attribut target="_blank" si l'URL n'est pas vide et que l'attribut target n'est pas prĂ©sent
  465.                     $brief str_replace($match[0], '<a href="'.$match[1].'" target="_blank">'.$match[3].'</a>'$brief);
  466.                 }
  467.             }
  468.         }
  469.        
  470.         return $brief;
  471.     }
  472.     public function canActivate(bool $isAdmin falsebool $isSubContractor false): bool
  473.     {
  474.         if ($this->getMissions()->count() === 0) {
  475.             return false;
  476.         }
  477.         if (!$isSubContractor && !$isAdmin) {
  478.             return false;
  479.         }
  480.         $subcontractors = [];
  481.         foreach ($this->getMissions() as $mission) {
  482.             if (!$mission->canActivate()) {
  483.                 return false;
  484.             }
  485.             foreach ($mission->getParticipants() as $participant) {
  486.                 if ($participant->getRole() === Role::ROLE_SUBCONTRACTOR && !in_array($participant->getUser()->getId(), $subcontractors)) {
  487.                     $subcontractors[] = $participant->getUser()->getId();
  488.                 }
  489.             }
  490.         }
  491.         if (!$isAdmin && count($subcontractors) > 1) {
  492.             return false;
  493.         }
  494.         return true;
  495.     }
  496.     public function canShowPriceInClient(){
  497.         $show true;
  498.         foreach ($this->getMissions() as $mission) {
  499.             //product forfaitaire pas pris en compte
  500.             if ($mission->getProduct()->getType()->value == 1) {
  501.                 continue;
  502.             }
  503.             //end
  504.             //first test is determine all  mission already have a complete subcontrator
  505.             $jobNotExists array_diff($this->getJobInMission($mission), $this->getJobSubcontractorInMission($mission));
  506.              if (!empty($jobNotExists)) {
  507.                  return false;
  508.              }
  509.              if($mission->getProduct()->getType()->value  == ){
  510.                 continue;
  511.              }
  512.              //second test is determine all  subcontrator is evaluated
  513.              foreach ($mission->getParticipants() as $participant) {
  514.                 if($mission->getPriceSale()!=null){
  515.                     continue;
  516.                 }
  517.                 if ($participant->getRole()->value =="ROLE_SUBCONTRACTOR" ) {
  518.                     if (is_null($participant->getEstimatedIncome()))
  519.                          {
  520.                         return false;
  521.                     }
  522.                 }
  523.             }
  524.         }
  525.        
  526.         return $show;
  527.     }
  528.     /**
  529.      * get all job in mission
  530.      * @param  Mission $mission 
  531.      * @return Array
  532.      */
  533.     public function getJobInMission(Mission $mission)
  534.     {
  535.         $listJobInMission = [];
  536.         foreach ($mission->getProduct()->getJobs() as $job) {
  537.             $listJobInMission[] = $job->getName();
  538.         }
  539.         return $listJobInMission;
  540.     }
  541.      /**
  542.      * get all job participant in mission
  543.      * @param  Mission $mission 
  544.      * @return Array
  545.      */
  546.     public function getJobSubcontractorInMission(Mission $mission)
  547.     {
  548.         $listAllJobSub = [];
  549.         foreach ($mission->getParticipants() as $participant) {
  550.             if (!is_null($participant) && !is_null($participant->getJob()))
  551.                 $listAllJobSub[] = $participant->getJob()->getName();
  552.         }
  553.         return $listAllJobSub;
  554.     }
  555.     /**
  556.      * Get content of campaign and returned this content
  557.      * 
  558.      * 1 : if content of campaign is a product forfaitaire only
  559.      * 
  560.      * 2 : if content of campaign is a product to evaluate only
  561.      * 
  562.      * 3 : if content of campaign is a product mixed only
  563.      * 
  564.      * @return int 
  565.      */
  566.     public function getTypeOfProduct():int
  567.     {
  568.         $nombreProductForfaitaire ;
  569.         $nombreProductAEvaluer ;
  570.         $nombreOfMissions sizeof($this->getMissions());
  571.         foreach ($this->getMissions() as $key => $mission) {
  572.                 if($mission->getProduct()->getType()->value == || $mission->getProduct()->getType()->value == ){
  573.                     $nombreProductForfaitaire++;
  574.                 }
  575.                 else{
  576.                     $nombreProductAEvaluer ++;
  577.                 }
  578.         }
  579.         if($nombreProductForfaitaire==$nombreOfMissions){
  580.             return 1;
  581.         }
  582.         elseif($nombreProductAEvaluer==$nombreOfMissions){
  583.             return 2;
  584.         }
  585.         else {
  586.             return 3;
  587.         }
  588.     }
  589.    
  590.     /**
  591.      * This method show if one subcontrator is in an all mission
  592.      * 
  593.      * @return bool true if one subcontractor for all mission in campaign, and false for contrary
  594.      */
  595.     public function isOneSubcontractorForAllMission():bool
  596.     {
  597.         $userIdnull;
  598.         foreach ($this->getMissions() as $mission) {
  599.             foreach ($mission->getParticipants() as $participant) {
  600.                 if($participant->getRole()->value =="ROLE_SUBCONTRACTOR" && !in_array('ROLE_BOT',$participant->getUser()->getRoles())){
  601.                     if($userId == null){
  602.                         $userId $participant->getUser()->getId();
  603.                     }
  604.                     else{
  605.                         if($userId !==  $participant->getUser()->getId()){
  606.                             return false;
  607.                         }
  608.                     }
  609.                 }
  610.             }
  611.         }
  612.         return true;
  613.     }
  614.     public function haveBot(){
  615.         foreach ($this->getMissions() as $mission) {
  616.             foreach ($mission->getParticipants() as $participant) {
  617.                 if (in_array('ROLE_BOT',$participant->getUser()->getRoles())) {
  618.                     return true;
  619.                 }
  620.             }
  621.         }
  622.         return false;
  623.     }
  624.     /**
  625.      * Get all  mission no subcontractor
  626.      * 
  627.      * @return array
  628.      */
  629.     public function getMissionNoSubContractor(): array
  630.     {   
  631.         $missionNoSubContractor = [];
  632.         foreach ($this->getMissions() as $mission) {
  633.             if(!$this->missionHaveSubcontractor($mission)){
  634.                 $missionNoSubContractor=[...$missionNoSubContractor$mission];
  635.             }
  636.         }
  637.         return $missionNoSubContractor;
  638.     }
  639.     public function canResendMail()
  640.     {
  641.         foreach ($this->getMissions() as $mission) {
  642.             if ($mission->getWorkflow() !== null)
  643.                 foreach ($mission->getWorkflow()->getSteps() as  $step) {
  644.                     if ($step->isActive()) {
  645.                         return true;
  646.                     }
  647.                 }
  648.         }
  649.         return false;
  650.     }
  651.     /**
  652.      * For know if a campain is payed by credit
  653.      * 
  654.      * @return bool|null
  655.      */
  656.     public function getIsPaidOnCredit(): ?bool
  657.     {
  658.         return $this->isPaidOnCredit;
  659.     }
  660.     /**
  661.      * @param bool|null $isPaidOnCredit
  662.      * 
  663.      * @return self
  664.      */
  665.     public function setIsPaidOnCredit(?bool $isPaidOnCredit): self
  666.     {
  667.         $this->isPaidOnCredit $isPaidOnCredit;
  668.         return $this;
  669.     }
  670.     public function getPaymentMethod(): ?string
  671.     {
  672.         return $this->paymentMethod;
  673.     }
  674.     public function setPaymentMethod(?string $paymentMethod): self
  675.     {
  676.         $this->paymentMethod $paymentMethod;
  677.         return $this;
  678.     }
  679.     public function canActivateEveryMission()
  680.     {
  681.         $verifisAllMissionisDefined true;
  682.         foreach ($this->getMissions() as $mission) {
  683.             if ( $mission->getStateProvider() != "cancelled" ) {
  684.                 if ($mission->canActivate() === false) {
  685.                     return false;
  686.                 }
  687.                 if (!$this->missionHaveSubcontractor($mission)) {
  688.                     return false;
  689.                 }
  690.                 if (is_null($mission->getWorkflow())) {
  691.                     return false;
  692.                 }
  693.             }
  694.         }
  695.         return $verifisAllMissionisDefined;
  696.     }
  697.     public function missionHaveSubcontractor($mission)
  698.     {
  699.         //1 = temps passĂ© izy de tsy ilaina.
  700.         $haveSubcontractor false;
  701.         foreach ($mission->getParticipants() as $participant) {
  702.             if (($participant->getRole() === Role::ROLE_SUBCONTRACTOR and $participant->getUser()->getBillingMethod() == and !in_array('ROLE_BOT'$participant->getUser()->getRoles()))
  703.                 or
  704.                 ($participant->getRole() === Role::ROLE_SUBCONTRACTOR and $participant->getUser()->getBillingMethod() == and !in_array('ROLE_BOT'$participant->getUser()->getRoles()))
  705.             ) {
  706.                 $haveSubcontractor true;
  707.             }
  708.         }
  709.         return $haveSubcontractor;
  710.     }
  711.     public function getActivatedAt(): ?\DateTimeImmutable
  712.     {
  713.         return $this->activatedAt;
  714.     }
  715.     public function setActivatedAt(?\DateTimeImmutable $activatedAt): self
  716.     {
  717.         $this->activatedAt $activatedAt;
  718.         return $this;
  719.     }
  720.     public function getClientStatus(): ?string
  721.     {
  722.         return $this->clientStatus;
  723.     }
  724.     public function setClientStatus(?string $clientStatus): self
  725.     {
  726.         $this->clientStatus $clientStatus;
  727.         return $this;
  728.     }
  729.     public function isToBeinvoiced(): ?bool
  730.     {
  731.         return $this->isToBeinvoiced;
  732.     }
  733.     public function setIsToBeinvoiced(?bool $isToBeinvoiced): self
  734.     {
  735.         $this->isToBeinvoiced $isToBeinvoiced;
  736.         return $this;
  737.     }
  738.     public function getDiscountForCompany(): ?float
  739.     {
  740.         return $this->discountForCompany;
  741.     }
  742.     public function setDiscountForCompany(?float $discountForCompany): self
  743.     {
  744.         $this->discountForCompany $discountForCompany;
  745.         return $this;
  746.     }
  747.     public function getReSendToClient(): ?bool
  748.     {
  749.         return $this->reSendToClient;
  750.     }
  751.     public function setReSendToClient(?bool $reSendToClient): self
  752.     {
  753.         $this->reSendToClient $reSendToClient;
  754.         return $this;
  755.     }
  756.     /**
  757.      * To get a error if a commande have an error 
  758.      * 
  759.      * @return string
  760.      */
  761.     public function getErrorCommand():?string{
  762.         return $this->errorCommand;
  763.     }
  764.    
  765.     /**
  766.      * set a error command manually because this is not persisted
  767.      * 
  768.      * @param string $errorCommand
  769.      * 
  770.      * @return void
  771.      */
  772.     public function setErrorCommand(?string $errorCommand):void 
  773.     {
  774.         $this->errorCommand$errorCommand;
  775.     }
  776.     public function getNumberInitialOfMissions(): ?string
  777.     {
  778.         return $this->numberInitialOfMissions;
  779.     }
  780.     public function setNumberInitialOfMissions(?string $numberInitialOfMissions): self
  781.     {
  782.         $this->numberInitialOfMissions $numberInitialOfMissions;
  783.         return $this;
  784.     }
  785.     public function getLastState(): ?string
  786.     {
  787.         return $this->lastState;
  788.     }
  789.     public function setLastState(?string $lastState): self
  790.     {
  791.         $this->lastState $lastState;
  792.         return $this;
  793.     }
  794.     public function getDiscountValueForCredit(): float
  795.     {
  796.         return 220*(1-$this->getDiscountForCompany()/100);
  797.     }
  798.     public function getCreditHistory(): ?CreditHistory
  799.     {
  800.         return $this->creditHistory;
  801.     }
  802.     public function setCreditHistory(?CreditHistory $creditHistory): self
  803.     {
  804.         $this->creditHistory $creditHistory;
  805.         return $this;
  806.     }
  807.     public function canClosedCampaing(){
  808.         $missions $this->missions;
  809.         //connaitre les delais des participant s'il ont deja ajoutĂ© leur delais
  810.         foreach ($missions as $mission) {
  811.             //si tout les participants ont remplies leurs delais
  812.             foreach ($mission->getParticipants() as $participant) {
  813.                 if ($participant->getRole() === Role::ROLE_SUBCONTRACTOR){
  814.                     if ( 
  815.                         (is_null($participant->getInitialTime()) || 
  816.                         is_null($participant->getEstimatedIncome())
  817.                         )
  818.                         ||
  819.                         ($participant->getInitialTime() == || $participant->getEstimatedIncome() == )
  820.                     ) {
  821.                         return false;
  822.                     }
  823.                 }
  824.             }
  825.             // $jobNotExists = array_diff($this->missionService->getJobInMission($mission), $this->missionService->getJobSubcontractorInMission($mission));
  826.         }
  827.         return true;
  828.     }
  829.     public function getDeleteReason(): ?string
  830.     {
  831.         return $this->deleteReason;
  832.     }
  833.     public function setDeleteReason(?string $deleteReason): self
  834.     {
  835.         $this->deleteReason $deleteReason;
  836.         
  837.         return $this;
  838.     }
  839.     public function isOrdoredByAdmin(): ?bool
  840.     {
  841.         return $this->isOrdoredByAdmin;
  842.     }
  843.     public function setIsOrdoredByAdmin(bool $isOrdoredByAdmin): self
  844.     {
  845.         $this->isOrdoredByAdmin $isOrdoredByAdmin;
  846.         return $this;
  847.     }
  848.     public function getDefautObserver(): ?array
  849.     {
  850.         return $this->defautObserver;
  851.     }
  852.     public function setDefautObserver(array $defautObserver): self
  853.     {
  854.         $this->defautObserver $defautObserver;
  855.         return $this;
  856.     }
  857.     public function getDefaultValidator(): ?array
  858.     {
  859.         return $this->defaultValidator;
  860.     }
  861.     public function setDefaultValidator(?array $defaultValidator): self
  862.     {
  863.         $this->defaultValidator $defaultValidator;
  864.         return $this;
  865.     }
  866.     public function getStatusModifyAt(): ?\DateTimeImmutable
  867.     {
  868.         return $this->statusModifyAt;
  869.     }
  870.     public function setStatusModifyAt(?\DateTimeImmutable $statusModifyAt): static
  871.     {
  872.         $this->statusModifyAt $statusModifyAt;
  873.         return $this;
  874.     }
  875.     public function getIsToBeinvoiced(): ?bool
  876.     {
  877.         return $this->isToBeinvoiced;
  878.     }
  879.     public function getIsOrdoredByAdmin(): ?bool
  880.     {
  881.         return $this->isOrdoredByAdmin;
  882.     }
  883.     public function getFrequency(): ?RecurringService
  884.     {
  885.         return $this->frequency;
  886.     }
  887.     public function setFrequency(?RecurringService $frequency): self
  888.     {
  889.         $this->frequency $frequency;
  890.         return $this;
  891.     }
  892.     public function getActiveFrequency(): ?bool
  893.     {
  894.         return $this->activeFrequency;
  895.     }
  896.     public function setActiveFrequency(?bool $activeFrequency): self
  897.     {
  898.         $this->activeFrequency $activeFrequency;
  899.         return $this;
  900.     }
  901.     public function isRecurrent(): ?bool
  902.     {
  903.         return $this->isRecurrent;
  904.     }
  905.     public function setIsRecurrent(?bool $isRecurrent): static
  906.     {
  907.         $this->isRecurrent $isRecurrent;
  908.         return $this;
  909.     }
  910.     public function  __toString(){
  911.         return $this->getName();
  912.     }
  913.     public function getRecurringState(): ?string
  914.     {
  915.         return $this->recurringState;
  916.     }
  917.     public function setRecurringState(?string $recurringState): static
  918.     {
  919.         $this->recurringState $recurringState;
  920.         return $this;
  921.     }
  922.     public function getIsRecurrent(): ?bool
  923.     {
  924.         return $this->isRecurrent;
  925.     }
  926.     /**
  927.      * @return Collection|FileMission[]
  928.      */
  929.     public function getFileMissions(): Collection
  930.     {
  931.         return $this->fileMissions;
  932.     }
  933.     public function getFirstMission(){
  934.         
  935.             $missions $this->getMissions();
  936.             if ($missions->isEmpty()) {
  937.                 return null;
  938.             }
  939.             if ($missions->count() === 1) {
  940.                 return $missions->first();
  941.             }
  942.             $filtered $this->getMissionsWithoutEspaceDeDiscussion();
  943.             return $filtered->isEmpty() ? null $filtered->first();
  944.     }
  945.     public function addFileMission(FileMission $fileMission): self
  946.     {
  947.         if (!$this->fileMissions->contains($fileMission)) {
  948.             $this->fileMissions[] = $fileMission;
  949.             $fileMission->setCampaign($this);
  950.         }
  951.         return $this;
  952.     }
  953.     public function removeFileMission(FileMission $fileMission): self
  954.     {
  955.         if ($this->fileMissions->removeElement($fileMission)) {
  956.             // set the owning side to null (unless already changed)
  957.             if ($fileMission->getCampaign() === $this) {
  958.                 $fileMission->setCampaign(null);
  959.             }
  960.         }
  961.         return $this;
  962.     }
  963.     public function getIsNew(): ?bool
  964.     {
  965.         return $this->isNew;
  966.     }
  967.     public function setIsNew(?bool $isNew): self
  968.     {
  969.         $this->isNew $isNew;
  970.         return $this;
  971.     }
  972.     public function getConfidentiality(): ?bool
  973.     {
  974.         return $this->confidentiality;
  975.     }
  976.     public function setConfidentiality(?bool $confidentiality): self
  977.     {
  978.         $this->confidentiality $confidentiality;
  979.         return $this;
  980.     }
  981.     public function getDisabled(): ?bool
  982.     {
  983.         return $this->disabled;
  984.     }
  985.     public function setDisabled(?bool $disabled): self
  986.     {
  987.         $this->disabled $disabled;
  988.         return $this;
  989.     }
  990.     /**
  991.      * @return Collection|CampaignPendingToSend[]
  992.      */
  993.     public function getCampaignPendingToSends(): Collection
  994.     {
  995.         return $this->campaignPendingToSends;
  996.     }
  997.     public function addCampaignPendingToSend(CampaignPendingToSend $campaignPendingToSend): self
  998.     {
  999.         if (!$this->campaignPendingToSends->contains($campaignPendingToSend)) {
  1000.             $this->campaignPendingToSends[] = $campaignPendingToSend;
  1001.             $campaignPendingToSend->setCampaign($this);
  1002.         }
  1003.         return $this;
  1004.     }
  1005.     public function removeCampaignPendingToSend(CampaignPendingToSend $campaignPendingToSend): self
  1006.     {
  1007.         if ($this->campaignPendingToSends->removeElement($campaignPendingToSend)) {
  1008.             // set the owning side to null (unless already changed)
  1009.             if ($campaignPendingToSend->getCampaign() === $this) {
  1010.                 $campaignPendingToSend->setCampaign(null);
  1011.             }
  1012.         }
  1013.         return $this;
  1014.     }
  1015.     public function getDefaultValidatorExternal(): ?array
  1016.     {
  1017.         return $this->defaultValidatorExternal;
  1018.     }
  1019.     public function setDefaultValidatorExternal(?array $defaultValidatorExternal): self
  1020.     {
  1021.         $this->defaultValidatorExternal $defaultValidatorExternal;
  1022.         return $this;
  1023.     }
  1024.     public function getDefautObserverExternal(): ?array
  1025.     {
  1026.         return $this->defautObserverExternal;
  1027.     }
  1028.     public function setDefautObserverExternal(?array $defautObserverExternal): self
  1029.     {
  1030.         $this->defautObserverExternal $defautObserverExternal;
  1031.         return $this;
  1032.     }
  1033.     public function getLastStepWp(): ?int
  1034.     {
  1035.         return $this->lastStepWp;
  1036.     }
  1037.     public function setLastStepWp(?int $lastStepWp): self
  1038.     {
  1039.         $this->lastStepWp $lastStepWp;
  1040.         return $this;
  1041.     }
  1042.     public function getFrontWpId(): ?int
  1043.     {
  1044.         return $this->frontWpId;
  1045.     }
  1046.     public function setFrontWpId(?int $frontWpId): self
  1047.     {
  1048.         $this->frontWpId $frontWpId;
  1049.         return $this;
  1050.     }
  1051.     public function getWpSlug(): ?string
  1052.     {
  1053.         return $this->wpSlug;
  1054.     }
  1055.     public function setWpSlug(?string $wpSlug): self
  1056.     {
  1057.         $this->wpSlug $wpSlug;
  1058.         return $this;
  1059.     }
  1060.     public function getStateHistory(): ?array
  1061.     {
  1062.         return $this->stateHistory;
  1063.     }
  1064.     public function setStateHistory(?array $stateHistory): self
  1065.     {
  1066.         $this->stateHistory $stateHistory;
  1067.         return $this;
  1068.     }
  1069.     public function getTypeInitiation(): ?int
  1070.     {
  1071.         return $this->typeInitiation;
  1072.     }
  1073.     public function setTypeInitiation(?int $typeInitiation): self
  1074.     {
  1075.         $this->typeInitiation $typeInitiation;
  1076.         return $this;
  1077.     }
  1078.     public function getEmailSentInsuffBudget(): ?bool
  1079.     {
  1080.         return $this->emailSentInsuffBudget;
  1081.     }
  1082.     public function setEmailSentInsuffBudget(?bool $emailSentInsuffBudget): self
  1083.     {
  1084.         $this->emailSentInsuffBudget $emailSentInsuffBudget;
  1085.         return $this;
  1086.     }
  1087.     public function getAllUserExternal(){
  1088.         $allUserExternal = [];
  1089.         foreach ($this->getMissions() as $mission) {
  1090.             foreach ($mission->getParticipants() as $participant) {
  1091.                 if (in_array($participant->getRole()->value, ['ROLE_OBSERVER_EXTERNAL','ROLE_VALIDATOR_EXTERNAL']) and $participant->getUser()->getDeleted() == 0) {
  1092.                        $allUserExternal = [...$allUserExternal,$participant->getUser()];
  1093.                     }
  1094.                
  1095.             }
  1096.         }
  1097.         return $allUserExternal;
  1098.     }
  1099.     public function getAllUserInCampaing(){
  1100.         $allUser = [];
  1101.         foreach ($this->getMissions() as $mission) {
  1102.             foreach ($mission->getParticipants() as $participant) {
  1103.                 $allUser = [...$allUser,$participant->getUser()->getId()];
  1104.             }
  1105.         }
  1106.         return $allUser;
  1107.     }
  1108.      public function getAllUserInCampaingWithoutValidatorAndSubcontractor(){
  1109.         $allUser = [];
  1110.         $noRepeatIds = [];
  1111.         foreach ($this->getMissions() as $mission) {
  1112.             foreach ($mission->getParticipants() as $participant) {
  1113.                 if (!in_array($participant->getUser()->getId(), $noRepeatIds) and $participant->getRole() !== Role::ROLE_VALIDATOR and $participant->getRole() !== Role::ROLE_OBSERVER) {
  1114.                     $allUser = [...$allUser,$participant];
  1115.                     $noRepeatIds[] = $participant->getUser()->getId();
  1116.                 }
  1117.             }
  1118.         }
  1119.         return $allUser;
  1120.     }
  1121.     public function getUserWriting(): ?array
  1122.     {
  1123.         return $this->user_writing;
  1124.     }
  1125.     public function setUserWriting(?array $user_writing): self
  1126.     {
  1127.         $this->user_writing $user_writing;
  1128.         return $this;
  1129.     }
  1130.     public function getMissionsWithoutEspaceDeDiscussion(){
  1131.         $missions $this->getMissions(); 
  1132.         $tempMissions = [];
  1133.         foreach ($missions as $mission) {
  1134.             if($mission->getProduct()->getName() != "Espace de discussion"){
  1135.                 $tempMissions = [...$tempMissions$mission];
  1136.             }
  1137.         }
  1138.         return  new ArrayCollection($tempMissions); 
  1139.         
  1140.     }
  1141.     public function getMissionsWithoutTempPasse(){
  1142.         $missions $this->getMissions(); 
  1143.         $missonWithoutTempPasse = [];
  1144.         foreach ($missions as $mission) {
  1145.             if ($mission->getProduct()->getType() != ProductType::AU_TEMPS_PASSE) {
  1146.                 $tempMissions = [...$missonWithoutTempPasse$mission];
  1147.             }
  1148.         }
  1149.         return  new ArrayCollection($missonWithoutTempPasse); 
  1150.         
  1151.     }
  1152.     public function isAlreadyStart(){
  1153.         if($this->state == 'in_progress'){
  1154.             return true;
  1155.         }
  1156.         elseif($this->getStateHistory() != null ){
  1157.             foreach ($this->getStateHistory() as  $history) {
  1158.                   if($history[0] == 'in_progress'){
  1159.                     return true
  1160.                   }
  1161.             }
  1162.         }
  1163.         return false
  1164.     }
  1165.     // For api only, not persisted
  1166.     public function getIsAlreadyStart(){
  1167.         return $this->isAlreadyStart();
  1168.     }
  1169.     public function setIsAlreadyStart($isAlreadyStart){
  1170.         $this->isAlreadyStart $isAlreadyStart;
  1171.         return $this;
  1172.     }
  1173.     //
  1174.     public function getCloningSourceId(): ?string
  1175.     {
  1176.         return $this->cloningSourceId;
  1177.     }
  1178.     public function setCloningSourceId(?string $cloningSourceId): self
  1179.     {
  1180.         $this->cloningSourceId $cloningSourceId;
  1181.     
  1182.     }
  1183.     public function getIsActivated(): ?int
  1184.     {
  1185.         return $this->isActivated;
  1186.     }
  1187.     public function setIsActivated(?int $isActivated): static
  1188.     {
  1189.         $this->isActivated $isActivated;
  1190.         return $this;
  1191.     }
  1192.     public function getContactProject(){
  1193.         $contactProject = [];
  1194.         foreach ($this->getMissions() as $key => $mission) {
  1195.             foreach ($mission->getParticipants() as $participant) {
  1196.                 if ($participant->getRole() === Role::PROJECT_CONTACT) {
  1197.                     $contactProject[] = $participant->getUser();
  1198.                 }
  1199.             }
  1200.             if ($key 1) {
  1201.                 break;
  1202.             }
  1203.         }
  1204.         return $contactProject;
  1205.     }
  1206.     public function getDateClosed(): ?\DateTimeInterface
  1207.     {
  1208.         return $this->dateClosed;
  1209.     }
  1210.     public function setDateClosed(?\DateTimeInterface $dateClosed): static
  1211.     {
  1212.         $this->dateClosed $dateClosed;
  1213.         return $this;
  1214.     }
  1215.     public function isIsNew(): ?bool
  1216.     {
  1217.         return $this->isNew;
  1218.     }
  1219.     public function isDisabled(): ?bool
  1220.     {
  1221.         return $this->disabled;
  1222.     }
  1223.     public function isConfidentiality(): ?bool
  1224.     {
  1225.         return $this->confidentiality;
  1226.     }
  1227.     public function isInvoiced(): ?bool
  1228.     {
  1229.         return $this->invoiced;
  1230.     }
  1231.     public function isActiveFrequency(): ?bool
  1232.     {
  1233.         return $this->activeFrequency;
  1234.     }
  1235.     public function isIsPaidOnCredit(): ?bool
  1236.     {
  1237.         return $this->isPaidOnCredit;
  1238.     }
  1239.     public function isIsToBeinvoiced(): ?bool
  1240.     {
  1241.         return $this->isToBeinvoiced;
  1242.     }
  1243.     public function isReSendToClient(): ?bool
  1244.     {
  1245.         return $this->reSendToClient;
  1246.     }
  1247.     public function isIsOrdoredByAdmin(): ?bool
  1248.     {
  1249.         return $this->isOrdoredByAdmin;
  1250.     }
  1251.     public function isIsRecurrent(): ?bool
  1252.     {
  1253.         return $this->isRecurrent;
  1254.     }
  1255.     public function isEmailSentInsuffBudget(): ?bool
  1256.     {
  1257.         return $this->emailSentInsuffBudget;
  1258.     }
  1259.     public function isNewForLivrable(): ?bool
  1260.     {
  1261.         return $this->newForLivrable;
  1262.     }
  1263.     public function setNewForLivrable(?bool $newForLivrable): static
  1264.     {
  1265.         $this->newForLivrable $newForLivrable;
  1266.         return $this;
  1267.     }
  1268.     /**
  1269.      * @return Collection<int, noteCampaing>
  1270.      */
  1271.     public function getNoteCampaing(): Collection
  1272.     {
  1273.         return $this->noteCampaing;
  1274.     }
  1275.     public function addNoteCampaing(noteCampaing $noteCampaing): static
  1276.     {
  1277.         if (!$this->noteCampaing->contains($noteCampaing)) {
  1278.             $this->noteCampaing->add($noteCampaing);
  1279.             $noteCampaing->setCampaign($this);
  1280.         }
  1281.         return $this;
  1282.     }
  1283.     public function removeNoteCampaing(noteCampaing $noteCampaing): static
  1284.     {
  1285.         if ($this->noteCampaing->removeElement($noteCampaing)) {
  1286.             // set the owning side to null (unless already changed)
  1287.             if ($noteCampaing->getCampaign() === $this) {
  1288.                 $noteCampaing->setCampaign(null);
  1289.             }
  1290.         }
  1291.         return $this;
  1292.     }
  1293. }