src/Application/Internit/LeadBundle/Entity/Product.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Application\Internit\LeadBundle\Entity;
  3. use App\Application\Internit\RealEstateBundle\Entity\RealEstate;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use App\Application\Internit\ContentBundle\Entity\Entity;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. /**
  8.  * Product
  9.  *
  10.  * @ORM\Table(name="internit_lead_product")
  11.  * @ORM\Entity(repositoryClass="App\Application\Internit\LeadBundle\Repository\ProductRepository")
  12.  */
  13. class Product extends Entity
  14. {
  15.   /**
  16.    * @var string
  17.    *
  18.    * @ORM\Column(name="name", type="string", length=255)
  19.   */
  20.   protected $name;
  21.   /**
  22.    * @var string
  23.    *
  24.    * @ORM\Column(name="description", type="string", length=255, nullable=true)
  25.   */
  26.   protected $description;
  27.   /**
  28.    * @var string
  29.    *
  30.    * @ORM\Column(name="token", type="string", length=32, nullable=true)
  31.   */
  32.   protected $token 0;
  33.   /**
  34.    *  @ORM\OneToMany(targetEntity="ProductUser", mappedBy="product", cascade={"persist","remove"}) 
  35.    *  @ORM\OrderBy({"position" = "ASC"})
  36.    */
  37.   protected $productUsers;
  38.   /** 
  39.    * @ORM\OneToMany(targetEntity="Lead", mappedBy="product") 
  40.    **/
  41.   protected $leads;
  42.   /**
  43.    * @var null|string
  44.    *
  45.    * @ORM\Column(name="viva_real", type="string", nullable=true)
  46.    */
  47.   protected $vivaReal;
  48.   /**
  49.    * @var null|string
  50.    *
  51.    * @ORM\Column(name="zap", type="string", nullable=true)
  52.    */
  53.   protected $zap;
  54.   /**
  55.    * @var null|string
  56.    *
  57.    * @ORM\Column(name="olx", type="string", nullable=true)
  58.    */
  59.   protected $olx;
  60.   /**
  61.    * @var string
  62.    *
  63.    * @ORM\Column(name="turn", type="integer")
  64.   */
  65.   protected $turn 0;
  66.   /**
  67.    *  @ORM\OneToMany(targetEntity="realStatePercent", mappedBy="product", cascade={"persist","remove"}) 
  68.    *  @ORM\OrderBy({"percent" = "DESC"})
  69.    * */
  70.   protected $realStatePercents;
  71.   /**
  72.    * E-mail que recebe todos os leads captados nesta campanha.
  73.    * 
  74.    * @ORM\Column(name="email", type="string", length=255, nullable=true)
  75.    */
  76.   protected $email;
  77.     /**
  78.    * Total de dias para a próxima rodada de corretores com base no lead existente.
  79.    * 
  80.    * @ORM\Column(name="fidelityDays", type="integer", nullable=true)
  81.    */
  82.   protected $fidelityDays null;
  83.   public function __construct() {
  84.       $this->productUsers = new ArrayCollection();
  85.       $this->realStatePercents = new ArrayCollection();
  86.       $this->leads = new ArrayCollection();
  87.   }
  88.   /**
  89.    * Get the value of name
  90.    *
  91.    * @return  string
  92.    */ 
  93.   public function getName()
  94.   {
  95.     return $this->name;
  96.   }
  97.   /**
  98.    * Set the value of name
  99.    *
  100.    * @param  string  $name
  101.    *
  102.    * @return  self
  103.    */ 
  104.   public function setName($name)
  105.   {
  106.     $this->name $name;
  107.     return $this;
  108.   }
  109.   /**
  110.    * Get the value of description
  111.    *
  112.    * @return  string
  113.    */ 
  114.   public function getDescription()
  115.   {
  116.     return $this->description;
  117.   }
  118.   /**
  119.    * Set the value of description
  120.    *
  121.    * @param  string  $description
  122.    *
  123.    * @return  self
  124.    */ 
  125.   public function setDescription($description)
  126.   {
  127.     $this->description $description;
  128.     return $this;
  129.   }
  130.   /**
  131.    * Get the value of token
  132.    *
  133.    * @return  string
  134.    */ 
  135.   public function getToken()
  136.   {
  137.     return $this->token;
  138.   }
  139.   /**
  140.    * Set the value of token
  141.    *
  142.    * @param  string  $token
  143.    *
  144.    * @return  self
  145.    */ 
  146.   public function setToken($token)
  147.   {
  148.     $this->token $token;
  149.     return $this;
  150.   }
  151.   /**
  152.    * Get the value of productUsers
  153.    */ 
  154.   public function getProductUsers()
  155.   {
  156.     return $this->productUsers;
  157.   }
  158.   /**
  159.    * Set the value of productUsers
  160.    *
  161.    * @return  self
  162.    */ 
  163.   public function setProductUsers($productUsers)
  164.   {
  165.     $this->productUsers $productUsers;
  166.     return $this;
  167.   }
  168.   public function addProductUsers($productUsers)
  169.   {      
  170.     if ($this->productUsers == null || $this->productUsers->contains($productUsers)) {
  171.       return;
  172.     }
  173.     $this->productUsers[] = $productUsers;
  174.     // $productUsers->setProduct($this);
  175.   }
  176.   public function removeProductUsers($productUsers)
  177.   {
  178.     $this->productUsers->removeElement($productUsers);
  179.     return $this;
  180.   }
  181.   /**
  182.    * Get the value of turn
  183.    *
  184.    * @return  string
  185.    */ 
  186.   public function getTurn()
  187.   {
  188.     return $this->turn;
  189.   }
  190.   /**
  191.    * Set the value of turn
  192.    *
  193.    * @param  string  $turn
  194.    *
  195.    * @return  self
  196.    */ 
  197.   public function setTurn(string $turn)
  198.   {
  199.     $this->turn $turn;
  200.     return $this;
  201.   }
  202.   /**
  203.    * Get the value of leads
  204.    */ 
  205.   public function getLeads()
  206.   {
  207.     return $this->leads;
  208.   }
  209.   /**
  210.    * Set the value of leads
  211.    *
  212.    * @return  self
  213.    */ 
  214.   public function setLeads($leads)
  215.   {
  216.     $this->leads $leads;
  217.     return $this;
  218.   }
  219.   /**
  220.    * Get the value of leads
  221.    */ 
  222.   public function getCorretores()
  223.   {
  224.     $corretores = [];
  225.     foreach ($this->productUsers as $corretor) {
  226.        // dump($corretor);
  227.       $corretores[] = $corretor->getUser()->getEmail();
  228.     }
  229.     return $corretores;
  230.   }
  231.   public function nextCorretor()
  232.   {
  233.       $email $this->getProductUsers()->slice($this->getTurn(), 1);
  234.       
  235.       if (count($email)) {
  236.           return current($email);
  237.       }
  238.       return false;
  239.   }
  240.   public function getCorretorPosition($position)
  241.     {
  242.         return $this->getProductUsers()->slice($position1)[0];
  243.     }
  244.   /**
  245.    * Get the value of realStatePercent
  246.    */ 
  247.   public function getRealStatePercents()
  248.   {
  249.     return $this->realStatePercents;
  250.   }
  251.   /**
  252.    * Set the value of realStatePercent
  253.    *
  254.    * @return  self
  255.    */ 
  256.   public function setRealStatePercents($realStatePercents)
  257.   {
  258.     $this->realStatePercents $realStatePercents;
  259.     return $this;
  260.   }
  261.   public function addRealStatePercents($realStatePercent)
  262.   {      
  263.     if ($this->realStatePercents == null || $this->realStatePercents->contains($realStatePercent)) {
  264.       return;
  265.     }
  266.     $this->realStatePercents[] = $realStatePercent;
  267.     // $productUsers->setProduct($this);
  268.   }
  269.   public function removeRealStatePercents($productUser)
  270.   {
  271.     $this->realStatePercents->removeElement($productUser);
  272.     return $this;
  273.   }
  274.   public function getTotalExtraLeads()
  275.   {
  276.       $total 0;
  277.       foreach ($this->realStatePercents as $realState) {
  278.           if($realState->getPercent() > 0){
  279.               $total += $realState->getExtraLeads();
  280.           }
  281.       }
  282.       return $total;
  283.   }
  284.   public function getCorretorLeadsByRealState(RealEstate $realState)
  285.   {
  286.       $corretorLeads = [];
  287.       /** @var $productUser ProductUser */
  288.       foreach ($this->getProductUsers() as $productUser) {
  289.         if($productUser->getRealEstate() == $realState) {
  290.             $corretorLeads[] = [
  291.                 'id' => $productUser->getUser()->getId(),
  292.                 'name' => $productUser->getUser()->getFirstName(),
  293.                 'email' => $productUser->getUser()->getEmail(),
  294.                 'qtdLeads' => $this->getQtdCorretorLeads($productUser->getUser()),
  295.                 'position' => $productUser->getPosition(),
  296.             ];
  297.         }
  298.       }
  299.       return $corretorLeads;
  300.   }
  301.   public function getLeadsGroupByRealState()
  302.   {
  303.       $result = [];
  304.       $corretorLeads = [];
  305.       /** @var Lead $lead */
  306.       foreach ($this->getLeads() as $lead) {
  307.           if(!isset($corretorLeads[$lead->getBrokerEmail()]))
  308.               $corretorLeads[$lead->getBrokerEmail()] = 1;
  309.           else{
  310.               $corretorLeads[$lead->getBrokerEmail()] += 1;
  311.           }
  312.       }
  313.       /** @var realStatePercent $realStatePercent */
  314.  /*     foreach ($this->realStatePercents as $realStatePercent) {
  315.           $realState = $realState->getRealState();
  316.       }*/
  317.       return $corretorLeads;
  318.   }
  319.   public function getQtdCorretorLeads($corretor)
  320.   {
  321.       $qtdLeads 0;
  322.       /** @var $lead Lead */
  323.       foreach ($this->getLeads() as $lead) {
  324.           if($lead->getBrokerEmail() == $corretor->getEmail()) { $qtdLeads++; }
  325.       }
  326.       return $qtdLeads;
  327.   }
  328.   public function getRealStateProductInfo()
  329.   {
  330.       $realStates = [];
  331.       foreach ($this->realStatePercents as $realState) {
  332.           $qtdLeads 0;
  333.           $firstLead $lastLead null;
  334.           foreach($this->getLeads() as $lead) {
  335.               if ($lead->getBrokerRealState() == $realState->getRealEstate()->getName()) {
  336.                   $qtdLeads++;
  337.                   if(!$firstLead || $lead->getCreatedAt() < $firstLead)
  338.                     $firstLead $lead->getCreatedAt();
  339.                   if(!$lastLead || $lead->getCreatedAt() > $lastLead)
  340.                       $lastLead $lead->getCreatedAt();
  341.               }
  342.           }
  343.           $realStates[] =[
  344.               'id' => $realState->getRealEstate()->getId(),
  345.               'name' => $realState->getRealEstate()->getName(),
  346.               'percent' =>  $realState->getPercent(),
  347.               'qtdLeads' => $qtdLeads,
  348.               'extraLeads' => $realState->getExtraLeads(),
  349.               'firstLead' => $firstLead,
  350.               'lastLead' => $lastLead,
  351.               'turn' => $realState->getTurn(),
  352.               'corretores' => $this->getCorretorLeadsByRealState($realState->getRealEstate()),
  353.           ];
  354.       }
  355.       //shuffle($realStates);
  356.       usort($realStates, function($a$b) { return $a['percent'] <= $b['percent']; });
  357.       return $realStates;
  358.   }
  359.   public function getRealStatePercentByName()
  360.   {
  361.     $realStates = [];
  362.     foreach ($this->realStatePercents as $realState) {
  363.       $realStates[$realState->getRealEstate()->getName()] = $realState->getPercent();
  364.     }
  365.     return $realStates;
  366.   }
  367.     public function getRealStatePercentByName2()
  368.     {
  369.         $realStates = [];
  370.         foreach ($this->realStatePercents as $realState) {
  371.             $realStates[str_replace(' '''$realState->getRealEstate()->getName())] = $realState->getPercent();
  372.         }
  373.         return $realStates;
  374.     }
  375.   /**
  376.    * 
  377.    */
  378.   public function countLeadsPerRealState()
  379.   {
  380.     $realStateLeadCounts $this->getRealStateNames();
  381.     foreach($this->getLeads() as $lead) {
  382.       if (isset($realStateLeadCounts[$lead->getBrokerRealState()])) {
  383.         $realStateLeadCounts[$lead->getBrokerRealState()]++;
  384.       }
  385.     }
  386.     return $realStateLeadCounts;
  387.   }
  388.     public function countLeadsPerRealState2()
  389.     {
  390.         $realStateLeadCounts $this->getRealStateNames();
  391.         foreach($this->getLeads() as $lead) {
  392.             if (isset($realStateLeadCounts[$lead->getBrokerRealState()])) {
  393.                 $realStateLeadCounts[$lead->getBrokerRealState()]++;
  394.             }
  395.         }
  396.         foreach ($this->getRealStateProductInfo() as $realStateInfo) {
  397.             if (isset($realStateLeadCounts[$realStateInfo['name']])) {
  398.                 $realStateLeadCounts[$realStateInfo['name']]+=$realStateInfo['extraLeads'];
  399.             }
  400.         }
  401.         return $realStateLeadCounts;
  402.     }
  403.   /**
  404.    * Obtém a lista de nomes das imobiliárias envolvidas no produto
  405.    */
  406.   private function getRealStateNames()
  407.   {
  408.     $realStateNames =  array_keys($this->getRealStatePercentByName());
  409.     $counts = array();
  410.     foreach($realStateNames as $name) {
  411.       $counts[$name] = 0;
  412.     }
  413.     return $counts;
  414.   }
  415.   /**
  416.    * Get e-mail que recebe todos os leads captados nesta campanha.
  417.    */ 
  418.   public function getEmail()
  419.   {
  420.     return $this->email;
  421.   }
  422.   /**
  423.    * Set e-mail que recebe todos os leads captados nesta campanha.
  424.    *
  425.    * @return  self
  426.    */ 
  427.   public function setEmail($email)
  428.   {
  429.     $this->email $email;
  430.     return $this;
  431.   }
  432.     /**
  433.    * Get dias que recebe a fidelidade de dias captados nesta campanha.
  434.    */ 
  435.   public function getFidelityDays(): ?int
  436.   {
  437.     return $this->fidelityDays;
  438.   }
  439.   /**
  440.    * Set dias para a rodada de corretores.
  441.    *
  442.    * @return  self
  443.    */ 
  444.   public function setFidelityDays($days)
  445.   {
  446.     $this->fidelityDays $days;
  447.     return $this;
  448.   }
  449.     /**
  450.      * @return string|null
  451.      */
  452.     public function getVivaReal(): ?string
  453.     {
  454.         return $this->vivaReal;
  455.     }
  456.     /**
  457.      * @param string|null $vivaReal
  458.      */
  459.     public function setVivaReal(?string $vivaReal): void
  460.     {
  461.         $this->vivaReal $vivaReal;
  462.     }
  463.     /**
  464.      * @return string|null
  465.      */
  466.     public function getZap(): ?string
  467.     {
  468.         return $this->zap;
  469.     }
  470.     /**
  471.      * @param string|null $zap
  472.      */
  473.     public function setZap(?string $zap): void
  474.     {
  475.         $this->zap $zap;
  476.     }
  477.     /**
  478.      * @return string|null
  479.      */
  480.     public function getOlx(): ?string
  481.     {
  482.         return $this->olx;
  483.     }
  484.     /**
  485.      * @param string|null $olx
  486.      */
  487.     public function setOlx(?string $olx): void
  488.     {
  489.         $this->olx $olx;
  490.     }
  491. }