<?php
namespace App\Application\Internit\LeadBundle\Entity;
use App\Application\Internit\RealEstateBundle\Entity\RealEstate;
use Doctrine\ORM\Mapping as ORM;
use App\Application\Internit\ContentBundle\Entity\Entity;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Product
*
* @ORM\Table(name="internit_lead_product")
* @ORM\Entity(repositoryClass="App\Application\Internit\LeadBundle\Repository\ProductRepository")
*/
class Product extends Entity
{
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
protected $name;
/**
* @var string
*
* @ORM\Column(name="description", type="string", length=255, nullable=true)
*/
protected $description;
/**
* @var string
*
* @ORM\Column(name="token", type="string", length=32, nullable=true)
*/
protected $token = 0;
/**
* @ORM\OneToMany(targetEntity="ProductUser", mappedBy="product", cascade={"persist","remove"})
* @ORM\OrderBy({"position" = "ASC"})
*/
protected $productUsers;
/**
* @ORM\OneToMany(targetEntity="Lead", mappedBy="product")
**/
protected $leads;
/**
* @var null|string
*
* @ORM\Column(name="viva_real", type="string", nullable=true)
*/
protected $vivaReal;
/**
* @var null|string
*
* @ORM\Column(name="zap", type="string", nullable=true)
*/
protected $zap;
/**
* @var null|string
*
* @ORM\Column(name="olx", type="string", nullable=true)
*/
protected $olx;
/**
* @var string
*
* @ORM\Column(name="turn", type="integer")
*/
protected $turn = 0;
/**
* @ORM\OneToMany(targetEntity="realStatePercent", mappedBy="product", cascade={"persist","remove"})
* @ORM\OrderBy({"percent" = "DESC"})
* */
protected $realStatePercents;
/**
* E-mail que recebe todos os leads captados nesta campanha.
*
* @ORM\Column(name="email", type="string", length=255, nullable=true)
*/
protected $email;
public function __construct() {
$this->productUsers = new ArrayCollection();
$this->realStatePercents = new ArrayCollection();
$this->leads = new ArrayCollection();
}
/**
* Get the value of name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set the value of name
*
* @param string $name
*
* @return self
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get the value of description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set the value of description
*
* @param string $description
*
* @return self
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get the value of token
*
* @return string
*/
public function getToken()
{
return $this->token;
}
/**
* Set the value of token
*
* @param string $token
*
* @return self
*/
public function setToken($token)
{
$this->token = $token;
return $this;
}
/**
* Get the value of productUsers
*/
public function getProductUsers()
{
return $this->productUsers;
}
/**
* Set the value of productUsers
*
* @return self
*/
public function setProductUsers($productUsers)
{
$this->productUsers = $productUsers;
return $this;
}
public function addProductUsers($productUsers)
{
if ($this->productUsers == null || $this->productUsers->contains($productUsers)) {
return;
}
$this->productUsers[] = $productUsers;
// $productUsers->setProduct($this);
}
public function removeProductUsers($productUsers)
{
$this->productUsers->removeElement($productUsers);
return $this;
}
/**
* Get the value of turn
*
* @return string
*/
public function getTurn()
{
return $this->turn;
}
/**
* Set the value of turn
*
* @param string $turn
*
* @return self
*/
public function setTurn(string $turn)
{
$this->turn = $turn;
return $this;
}
/**
* Get the value of leads
*/
public function getLeads()
{
return $this->leads;
}
/**
* Set the value of leads
*
* @return self
*/
public function setLeads($leads)
{
$this->leads = $leads;
return $this;
}
/**
* Get the value of leads
*/
public function getCorretores()
{
$corretores = [];
foreach ($this->productUsers as $corretor) {
// dump($corretor);
$corretores[] = $corretor->getUser()->getEmail();
}
return $corretores;
}
public function nextCorretor()
{
$email = $this->getProductUsers()->slice($this->getTurn(), 1);
if (count($email)) {
return current($email);
}
return false;
}
public function getCorretorPosition($position)
{
return $this->getProductUsers()->slice($position, 1)[0];
}
/**
* Get the value of realStatePercent
*/
public function getRealStatePercents()
{
return $this->realStatePercents;
}
/**
* Set the value of realStatePercent
*
* @return self
*/
public function setRealStatePercents($realStatePercents)
{
$this->realStatePercents = $realStatePercents;
return $this;
}
public function addRealStatePercents($realStatePercent)
{
if ($this->realStatePercents == null || $this->realStatePercents->contains($realStatePercent)) {
return;
}
$this->realStatePercents[] = $realStatePercent;
// $productUsers->setProduct($this);
}
public function removeRealStatePercents($productUser)
{
$this->realStatePercents->removeElement($productUser);
return $this;
}
public function getTotalExtraLeads()
{
$total = 0;
foreach ($this->realStatePercents as $realState) {
$total += $realState->getExtraLeads();
}
return $total;
}
public function getCorretorLeadsByRealState(RealEstate $realState)
{
$corretorLeads = [];
/** @var $productUser ProductUser */
foreach ($this->getProductUsers() as $productUser) {
if($productUser->getRealEstate() == $realState) {
$corretorLeads[] = [
'id' => $productUser->getUser()->getId(),
'name' => $productUser->getUser()->getFirstName(),
'email' => $productUser->getUser()->getEmail(),
'qtdLeads' => $this->getQtdCorretorLeads($productUser->getUser()),
'position' => $productUser->getPosition(),
];
}
}
return $corretorLeads;
}
public function getLeadsGroupByRealState()
{
$result = [];
$corretorLeads = [];
/** @var Lead $lead */
foreach ($this->getLeads() as $lead) {
if(!isset($corretorLeads[$lead->getBrokerEmail()]))
$corretorLeads[$lead->getBrokerEmail()] = 1;
else{
$corretorLeads[$lead->getBrokerEmail()] += 1;
}
}
/** @var realStatePercent $realStatePercent */
/* foreach ($this->realStatePercents as $realStatePercent) {
$realState = $realState->getRealState();
}*/
return $corretorLeads;
}
public function getQtdCorretorLeads($corretor)
{
$qtdLeads = 0;
/** @var $lead Lead */
foreach ($this->getLeads() as $lead) {
if($lead->getBrokerEmail() == $corretor->getEmail()) { $qtdLeads++; }
}
return $qtdLeads;
}
public function getRealStateProductInfo()
{
$realStates = [];
foreach ($this->realStatePercents as $realState) {
$qtdLeads = 0;
$firstLead = $lastLead = null;
foreach($this->getLeads() as $lead) {
if ($lead->getBrokerRealState() == $realState->getRealEstate()->getName()) {
$qtdLeads++;
if(!$firstLead || $lead->getCreatedAt() < $firstLead)
$firstLead = $lead->getCreatedAt();
if(!$lastLead || $lead->getCreatedAt() > $lastLead)
$lastLead = $lead->getCreatedAt();
}
}
$realStates[] =[
'id' => $realState->getRealEstate()->getId(),
'name' => $realState->getRealEstate()->getName(),
'percent' => $realState->getPercent(),
'qtdLeads' => $qtdLeads,
'extraLeads' => $realState->getExtraLeads(),
'firstLead' => $firstLead,
'lastLead' => $lastLead,
'turn' => $realState->getTurn(),
'corretores' => $this->getCorretorLeadsByRealState($realState->getRealEstate()),
];
}
//shuffle($realStates);
usort($realStates, function($a, $b) { return $a['percent'] <= $b['percent']; });
return $realStates;
}
public function getRealStatePercentByName()
{
$realStates = [];
foreach ($this->realStatePercents as $realState) {
$realStates[$realState->getRealEstate()->getName()] = $realState->getPercent();
}
return $realStates;
}
public function getRealStatePercentByName2()
{
$realStates = [];
foreach ($this->realStatePercents as $realState) {
$realStates[str_replace(' ', '', $realState->getRealEstate()->getName())] = $realState->getPercent();
}
return $realStates;
}
/**
*
*/
public function countLeadsPerRealState()
{
$realStateLeadCounts = $this->getRealStateNames();
foreach($this->getLeads() as $lead) {
if (isset($realStateLeadCounts[$lead->getBrokerRealState()])) {
$realStateLeadCounts[$lead->getBrokerRealState()]++;
}
}
return $realStateLeadCounts;
}
public function countLeadsPerRealState2()
{
$realStateLeadCounts = $this->getRealStateNames();
foreach($this->getLeads() as $lead) {
if (isset($realStateLeadCounts[$lead->getBrokerRealState()])) {
$realStateLeadCounts[$lead->getBrokerRealState()]++;
}
}
foreach ($this->getRealStateProductInfo() as $realStateInfo) {
if (isset($realStateLeadCounts[$realStateInfo['name']])) {
$realStateLeadCounts[$realStateInfo['name']]+=$realStateInfo['extraLeads'];
}
}
return $realStateLeadCounts;
}
/**
* Obtém a lista de nomes das imobiliárias envolvidas no produto
*/
private function getRealStateNames()
{
$realStateNames = array_keys($this->getRealStatePercentByName());
$counts = array();
foreach($realStateNames as $name) {
$counts[$name] = 0;
}
return $counts;
}
/**
* Get e-mail que recebe todos os leads captados nesta campanha.
*/
public function getEmail()
{
return $this->email;
}
/**
* Set e-mail que recebe todos os leads captados nesta campanha.
*
* @return self
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* @return string|null
*/
public function getVivaReal(): ?string
{
return $this->vivaReal;
}
/**
* @param string|null $vivaReal
*/
public function setVivaReal(?string $vivaReal): void
{
$this->vivaReal = $vivaReal;
}
/**
* @return string|null
*/
public function getZap(): ?string
{
return $this->zap;
}
/**
* @param string|null $zap
*/
public function setZap(?string $zap): void
{
$this->zap = $zap;
}
/**
* @return string|null
*/
public function getOlx(): ?string
{
return $this->olx;
}
/**
* @param string|null $olx
*/
public function setOlx(?string $olx): void
{
$this->olx = $olx;
}
}