<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
/**
* Consultant
*/
#[ORM\Table(name: 'consultant')]
#[ORM\Entity(repositoryClass: 'App\Repository\ConsultantRepository')]
class Consultant implements PasswordAuthenticatedUserInterface
{
#[ORM\OneToMany(targetEntity: 'App\Entity\CompanyConsultantLink', mappedBy: 'consultant', cascade: ['remove'])]
private $companies;
#[ORM\JoinTable(name: 'consultant_agent_link')]
#[ORM\ManyToMany(targetEntity: 'Agent', inversedBy: 'consultants')]
private $agents;
/**
* @var int
*/
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\OneToMany(targetEntity: 'App\Entity\CompanyConsultantLink', mappedBy: 'consultants')]
private $id;
#[ORM\OneToMany(targetEntity: 'Contact', mappedBy: 'consultantObject')]
public $contacts;
/**
* @var string
*/
#[ORM\Column(name: 'username', type: 'string', length: 255)]
private $username;
/**
* @var string
*/
#[ORM\Column(name: 'email', type: 'string', length: 255, nullable: true)]
public $email;
/**
* @var string
*/
#[ORM\Column(name: 'password', type: 'string', length: 255)]
private $password;
/**
* @var boolean
*/
#[ORM\Column(name: 'isAdmin', type: 'boolean')]
private $isAdmin;
/**
* @var boolean
*/
#[ORM\Column(name: 'is_acceptant', type: 'boolean')]
private $isAcceptant;
/**
* @var string
*/
#[ORM\Column(name: 'producent_id', type: 'string', length: 10, nullable: true)]
private $producentId;
/**
* @var array
*/
#[ORM\Column(name: 'rights', type: 'array', nullable: true)]
protected $rights;
/**
* @var array
*/
#[ORM\Column(name: 'agents_ff', type: 'array', nullable: true)]
protected $agentsFF;
/**
* @var \DateTime
*/
#[ORM\Column(name: 'changelog_seen', type: 'datetime', nullable: true)]
public $changelog_seen;
function __construct()
{
$this->agents = new ArrayCollection();
$this->companies = new ArrayCollection();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
*
* @return Consultant
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set password
*
* @param string $password
*
* @return Consultant
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword() : string|null
{
return $this->password;
}
/**
* @return array
*/
public function getAgentsFF()
{
return $this->agentsFF;
}
/**
* @param array $agentsFF
*/
public function setAgentsFF(array $agentsFF): void
{
$this->agentsFF = $agentsFF;
}
private function getSalt() {
return $this->username;
}
/**
* @return Agent[]
*/
public function getAgents()
{
return $this->agents;
}
/**
* @param mixed $agents
*/
public function setAgents(ArrayCollection $agents)
{
$this->agents = $agents;
}
public function isAdmin() {
return $this->isAdmin;
}
public function setIsAdmin($bool) {
$this->isAdmin = $bool;
}
/**
* @return array
*/
public function getRights() {
return $this->rights;
}
/**
* @param array $rights
*/
public function setRights(array $rights) {
$this->rights = $rights;
}
/**
* @param $right
* @return bool
*/
public function can($right) {
if (!empty($this->getRights())) {
return in_array($right, $this->getRights());
} else {
return $this->isAdmin();
}
}
/**
* @return CompanyConsultantLink[]
*/
public function getCompanies()
{
return $this->companies;
}
/**
* @param mixed $companies
*/
public function setCompanies($companies, $isAdmin): void
{
foreach ($companies as $company) {
$link = new CompanyConsultantLink($company, $this, $isAdmin);
}
$this->companies = $companies;
}
/**
* Returns the companies where this consultant is admin
*/
public function getCompaniesAdmin() {
$result = [];
$companies = $this->getCompanies();
foreach ($companies as $company) {
if ($company->getIsAdmin()) {
$result[] = $company->getCompany();
}
}
return $result;
}
/**
* Checks whether the consultant has any rights
* @return bool
*/
public function isCompanyAdmin() {
return $this->isAdmin() || !empty($this->getCompaniesAdmin());
}
/**
* @param Company $company Company of which you want the link of
* @return CompanyConsultantLink|null Link, null if is no member
*/
public function getCompanyLink(Company $company) {
foreach ($this->getCompanies() as $companyConsultantLink) {
if ($companyConsultantLink->getCompany()->getId() === $company->getId()) {
return $companyConsultantLink;
}
}
return null;
}
/**
* @param Company $company
* @return bool True iff consultant is admin of supplied company
*/
public function isAdminOfCompany(Company $company) {
$companyConsultantLink = $this->getCompanyLink($company);
if ($companyConsultantLink === null) {
return false;
} else {
return $companyConsultantLink->getIsAdmin();
}
}
/**
* @param Company $company
* @return bool True iff consultant is member of supplied company
*/
public function isMemberOfCompany(Company $company) {
return $this->getCompanyLink($company) !== null;
}
/**
* Checks whether the consultant is an Anva consultant
* @return bool
*/
public function inAnva() {
if ($this->isAdmin) {
return true;
}
foreach ($this->getCompanies() as $companyLink) {
if ($companyLink->getCompany()->isInAnva()) {
return true;
}
}
return false;
}
public function canAbonnement($agentId) {
if ($agentId == 'Amstelgeld & Assurantiƫn' || $agentId == 'Internetverzekeren.nl') {
return true;
}
if ($this->isAdmin) {
return true;
}
foreach ($this->getCompanies() as $company) {
if ($company->getCompany()->getName() === 'Amstelgeld') {
return true;
}
}
return false;
}
/**
* @return string|null
*/
public function getProducentId()
{
return $this->producentId ?? 22222;
}
/**
* @param string $producentId
*/
public function setProducentId(string $producentId): void
{
$this->producentId = $producentId;
}
/**
* @return bool
*/
public function isAcceptant(): bool
{
return $this->isAcceptant || $this->isAdmin;
}
/**
* @param bool $isAcceptant
*/
public function setIsAcceptant(bool $isAcceptant): void
{
$this->isAcceptant = $isAcceptant;
}
public function inAssu() {
if ($this->isAdmin) {
return true;
}
foreach ($this->getCompanies() as $companyLink) {
if ($companyLink->getCompany()->getName() == 'Amstelgeld') {
return true;
}
}
return false;
}
public function hasEmail() {
return isset($this->email);
}
public function inFF() {
if ($this->isAdmin) {
return true;
}
foreach ($this->getCompanies() as $companyLink) {
if ($companyLink->getCompany()->getName() == 'Optima') {
return true;
}
}
return false;
}
public function hasAccessFF($kantoorId) {
if ($this->isAdmin()) {
return true;
}
return in_array($kantoorId, $this->getAgentsFF());
}
}