<?php
namespace App\Entity\Slave;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
/**
* @ORM\Table(name="eposm_s_user")
* @ORM\Entity(repositoryClass="App\Repository\Slave\UserRepository")
* @ORM\HasLifecycleCallbacks()
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
public function __toString(){
return $this->profile->getSurname().' '.$this->profile->getName();
}
public function getNameShort(){
return $this->profile->getSurname().' '.substr($this->profile->getName(), 0, 1).'.';
}
public function displayNameAndCountTickets(){
return $this->profile->getSurname().' '.$this->profile->getName().' ('.$this->countTicketsUnderManagement('actual').' / '.$this->countTicketsUnderManagement('suspension').')';
}
public function getPermissionType($permissionSlug){
foreach($this->getAccountType()->getPermissions() as $jt){
if($jt->getPermission()->getSlug() == $permissionSlug)
return $jt->getRw();
}
return "";
}
public function getPermissionWarehouse($warehouseId){
foreach($this->getWarehouses() as $jt){
if($jt->getWarehouse()->getId() == $warehouseId)
return $jt->getPermission();
}
return "";
}
public function getPermissionTransfer($transferId){
foreach($this->getTransfersUserFrom() as $tf){
if($this->getPermissionWarehouse($tf->getWarehouseFrom()) == "RW" || $this->getPermissionWarehouse($tf->getWarehouseTo()) == "RW");
return true;
}
foreach($this->getTransfersUserTo() as $tt){
if($this->getPermissionWarehouse($tt->getWarehouseFrom()) == "RW" || $this->getPermissionWarehouse($tt->getWarehouseTo()) == "RW");
return true;
}
return false;
}
public function getPermissionTicket($ticketId){
if($this->getAccountTypology() != 'technician')
return true;
else{
foreach($this->getTickets() as $t){
if($t->getId() == $ticketId)
return true;
}
}
return false;
}
public function getAccountTypology(){
switch($this->getAccountType()->getCategory()->getSlug()){
case 'agency':
case 'administration':
case 'commercial':
case 'warehouse':
return $this->getAccountType()->getCategory()->getSlug();
break;
case 'tech-internal':
case 'tech-external':
return 'technician';
break;
default: break;
}
}
public function canMakeTicketActions($type, $settingBackOfficeValue){
switch($type){
case 'technician':
if($this->getAccountTypology() == 'technician' || (($this->getAccountTypology() == 'agency' || $this->getAccountTypology() == 'warehouse') && $settingBackOfficeValue))
return true;
break;
case 'only_backoffice':
if(($this->getAccountTypology() == 'agency' || $this->getAccountTypology() == 'warehouse') && $settingBackOfficeValue)
return true;
break;
case 'agency_warehouse':
if($this->getAccountTypology() == 'agency' || $this->getAccountTypology() == 'warehouse')
return true;
break;
default: break;
}
return false;
}
public function getCanDelete(){
if(sizeof($this->warehouses) > 0) return false;
if(sizeof($this->technicianAreas) > 0) return false;
if(sizeof($this->tickets) > 0) return false;
if(sizeof($this->interventions) > 0) return false;
return true;
}
public function isInHolidayAtDate($date){
$holidays = array();
foreach($this->getHolidays() as $h){
if($date->format('Ymd') >= $h->getDateFrom()->format('Ymd') && $date->format('Ymd') <= $h->getDateTo()->format('Ymd'))
return true;
}
return false;
}
public function getActualHoliday(){
$today = new \Datetime();
$holidays = array();
foreach($this->getHolidays() as $h){
if($today->format('Ymd') >= $h->getDateFrom()->format('Ymd') && $today->format('Ymd') <= $h->getDateTo()->format('Ymd'))
return $h;
}
return false;
}
public function canRead($area){
foreach($this->getAccountType()->getPermissions() as $jt){
if($area == $jt->getPermission()->getSlug()){
return true;
break;
}
}
}
public function canWrite($area){
foreach($this->getAccountType()->getPermissions() as $jt){
if($area == $jt->getPermission()->getSlug() && $jt->getRW() == 'RW'){
return true;
break;
}
}
}
public function canViewTab($tabSlug){
foreach($this->getAccountType()->getPermissions() as $jt){
if(str_contains($jt->getPermission()->getSlug(), $tabSlug)){
return true;
break;
}
}
return false;
}
public function countTicketsUnderManagement($type){
$count = 0;
foreach($this->getTickets() as $ticket){
switch($type){
case 'actual': if($ticket->getStatus()->getSlug() == 'assigned' || $ticket->getStatus()->getSlug() == 'taken_charge') $count++; break;
case 'suspension': if($ticket->getStatus()->getSlug() == 'suspension_request' || $ticket->getStatus()->getSlug() == 'suspended') $count++; break;
case 'actual_and_suspension': if($ticket->getStatus()->getSlug() == 'assigned' || $ticket->getStatus()->getSlug() == 'taken_charge' || $ticket->getStatus()->getSlug() == 'suspension_request' || $ticket->getStatus()->getSlug() == 'suspended') $count++; break;
case 'closed': if($ticket->getStatus()->getSlug() == 'closed' || $ticket->getStatus()->getSlug() == 'closed_portal') $count++; break;
default: break;
}
}
return $count;
}
public function mainWarehouse(){
if(sizeof($this->getWarehouses()) == 1)
return $this->getWarehouses()[0]->getWarehouse();
foreach($this->getWarehouses() as $jtuw){
if($jtuw->isMain())
return $jtuw->getWarehouse();
}
return null;
}
/**
* @ORM\Column(name="id", type="bigint")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(name="email", type="string", length=191, unique=true)
*/
protected $email;
/**
* @ORM\Column(name="password", type="string", length=191, nullable=true)
*/
protected $password;
/**
* @ORM\Column(name="role", type="string", length=191)
*/
protected $role = "ROLE_USER";
/**
* @ORM\Column(name="one_time_code", type="string", length=191, nullable=true)
*/
protected $oneTimeCode;
/**
* @ORM\Column(name="expiration_one_time_code", type="datetime", nullable=true)
*/
protected $expirationOneTimeCode;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
protected $active = false;
/**
* @ORM\Column(name="is_company_active", type="boolean")
*/
protected $companyActive = true;
/**
* @ORM\Column(name="is_admin_active", type="boolean")
*/
protected $adminActive = true;
/**
* @ORM\Column(name="exclude_auto_assign", type="boolean")
*/
protected $excludeAutoAssign = false;
/**
* @ORM\Column(name="is_economic_visible", type="boolean")
*/
protected $economicVisible = true;
// OneToOne
/**
* @ORM\OneToOne(targetEntity="App\Entity\Slave\UserProfile", mappedBy="user")
*/
private $profile;
//
// ManyToOne
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Slave\AccountType", inversedBy="users")
* @ORM\JoinColumn(name="account_type_id", referencedColumnName="id")
*/
private $accountType;
//
// OneToMany
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\UserDocument", mappedBy="user")
*/
private $documents;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\UserHoliday", mappedBy="user")
*/
private $holidays;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\UserHoliday", mappedBy="substitute")
*/
private $substitutions;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\JoinTableUserWarehouse", mappedBy="user")
*/
private $warehouses;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\TechnicianArea", mappedBy="user")
*/
private $technicianAreas;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\JoinTableUserSupplier", mappedBy="user")
*/
private $suppliers;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\ProductTransferLog", mappedBy="user")
*/
private $transferLogs;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\ProductTransfer", mappedBy="userFrom")
*/
private $transfersUserFrom;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\ProductTransfer", mappedBy="userTo")
*/
private $transfersUserTo;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\Ticket", mappedBy="technician")
*/
private $tickets;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\Intervention", mappedBy="technician")
*/
private $interventions;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\Intervention", mappedBy="operator")
*/
private $operatorInterventions;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\ProductRequest", mappedBy="technician")
*/
private $productRequests;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\TicketReminder", mappedBy="technician")
*/
private $reminders;
//
// ManyToMany
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Slave\Supplier", inversedBy="autoAssignUsers")
* @ORM\JoinTable(name="eposm_s_join_table_user_supplier_auto_assign")
*/
private $autoAssignSuppliers;
public function __construct()
{
$this->documents = new ArrayCollection();
$this->holidays = new ArrayCollection();
$this->substitutions = new ArrayCollection();
$this->warehouses = new ArrayCollection();
$this->technicianAreas = new ArrayCollection();
$this->suppliers = new ArrayCollection();
$this->transferLogs = new ArrayCollection();
$this->transfersUserFrom = new ArrayCollection();
$this->transfersUserTo = new ArrayCollection();
$this->tickets = new ArrayCollection();
$this->interventions = new ArrayCollection();
$this->productRequests = new ArrayCollection();
$this->operatorInterventions = new ArrayCollection();
$this->autoAssignSuppliers = new ArrayCollection();
$this->reminders = new ArrayCollection();
}
//
/**
* @inheritdoc
*/
public function getRoles(): array
{
switch($this->role){
case 'ROLE_USER': return array('ROLE_USER');
};
}
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
}
public function getUserIdentifier(): string
{
return (string) $this->email;
}
public function getUsername(): string
{
return (string) $this->email;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(?string $password): self
{
$this->password = $password;
return $this;
}
public function getId(): ?string
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getRole(): ?string
{
return $this->role;
}
public function setRole(string $role): self
{
$this->role = $role;
return $this;
}
public function getOneTimeCode(): ?string
{
return $this->oneTimeCode;
}
public function setOneTimeCode(?string $oneTimeCode): static
{
$this->oneTimeCode = $oneTimeCode;
return $this;
}
public function getExpirationOneTimeCode(): ?\DateTimeInterface
{
return $this->expirationOneTimeCode;
}
public function setExpirationOneTimeCode(?\DateTimeInterface $expirationOneTimeCode): static
{
$this->expirationOneTimeCode = $expirationOneTimeCode;
return $this;
}
public function isActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): static
{
$this->active = $active;
return $this;
}
public function isCompanyActive(): ?bool
{
return $this->companyActive;
}
public function setCompanyActive(bool $companyActive): static
{
$this->companyActive = $companyActive;
return $this;
}
public function isAdminActive(): ?bool
{
return $this->adminActive;
}
public function setAdminActive(bool $adminActive): static
{
$this->adminActive = $adminActive;
return $this;
}
public function isExcludeAutoAssign(): ?bool
{
return $this->excludeAutoAssign;
}
public function setExcludeAutoAssign(bool $excludeAutoAssign): static
{
$this->excludeAutoAssign = $excludeAutoAssign;
return $this;
}
public function isEconomicVisible(): ?bool
{
return $this->economicVisible;
}
public function setEconomicVisible(bool $economicVisible): static
{
$this->economicVisible = $economicVisible;
return $this;
}
public function getProfile(): ?UserProfile
{
return $this->profile;
}
public function setProfile(?UserProfile $profile): static
{
// unset the owning side of the relation if necessary
if ($profile === null && $this->profile !== null) {
$this->profile->setUser(null);
}
// set the owning side of the relation if necessary
if ($profile !== null && $profile->getUser() !== $this) {
$profile->setUser($this);
}
$this->profile = $profile;
return $this;
}
public function getAccountType(): ?AccountType
{
return $this->accountType;
}
public function setAccountType(?AccountType $accountType): static
{
$this->accountType = $accountType;
return $this;
}
/**
* @return Collection<int, UserDocument>
*/
public function getDocuments(): Collection
{
return $this->documents;
}
public function addDocument(UserDocument $document): static
{
if (!$this->documents->contains($document)) {
$this->documents->add($document);
$document->setUser($this);
}
return $this;
}
public function removeDocument(UserDocument $document): static
{
if ($this->documents->removeElement($document)) {
// set the owning side to null (unless already changed)
if ($document->getUser() === $this) {
$document->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, UserHoliday>
*/
public function getHolidays(): Collection
{
return $this->holidays;
}
public function addHoliday(UserHoliday $holiday): static
{
if (!$this->holidays->contains($holiday)) {
$this->holidays->add($holiday);
$holiday->setUser($this);
}
return $this;
}
public function removeHoliday(UserHoliday $holiday): static
{
if ($this->holidays->removeElement($holiday)) {
// set the owning side to null (unless already changed)
if ($holiday->getUser() === $this) {
$holiday->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, UserHoliday>
*/
public function getSubstitutions(): Collection
{
return $this->substitutions;
}
public function addSubstitution(UserHoliday $substitution): static
{
if (!$this->substitutions->contains($substitution)) {
$this->substitutions->add($substitution);
$substitution->setSubstitute($this);
}
return $this;
}
public function removeSubstitution(UserHoliday $substitution): static
{
if ($this->substitutions->removeElement($substitution)) {
// set the owning side to null (unless already changed)
if ($substitution->getSubstitute() === $this) {
$substitution->setSubstitute(null);
}
}
return $this;
}
/**
* @return Collection<int, JoinTableUserWarehouse>
*/
public function getWarehouses(): Collection
{
return $this->warehouses;
}
public function addWarehouse(JoinTableUserWarehouse $warehouse): static
{
if (!$this->warehouses->contains($warehouse)) {
$this->warehouses->add($warehouse);
$warehouse->setUser($this);
}
return $this;
}
public function removeWarehouse(JoinTableUserWarehouse $warehouse): static
{
if ($this->warehouses->removeElement($warehouse)) {
// set the owning side to null (unless already changed)
if ($warehouse->getUser() === $this) {
$warehouse->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, TechnicianArea>
*/
public function getTechnicianAreas(): Collection
{
return $this->technicianAreas;
}
public function addTechnicianArea(TechnicianArea $technicianArea): static
{
if (!$this->technicianAreas->contains($technicianArea)) {
$this->technicianAreas->add($technicianArea);
$technicianArea->setUser($this);
}
return $this;
}
public function removeTechnicianArea(TechnicianArea $technicianArea): static
{
if ($this->technicianAreas->removeElement($technicianArea)) {
// set the owning side to null (unless already changed)
if ($technicianArea->getUser() === $this) {
$technicianArea->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, JoinTableUserSupplier>
*/
public function getSuppliers(): Collection
{
return $this->suppliers;
}
public function addSupplier(JoinTableUserSupplier $supplier): static
{
if (!$this->suppliers->contains($supplier)) {
$this->suppliers->add($supplier);
$supplier->setUser($this);
}
return $this;
}
public function removeSupplier(JoinTableUserSupplier $supplier): static
{
if ($this->suppliers->removeElement($supplier)) {
// set the owning side to null (unless already changed)
if ($supplier->getUser() === $this) {
$supplier->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, ProductTransferLog>
*/
public function getTransferLogs(): Collection
{
return $this->transferLogs;
}
public function addTransferLog(ProductTransferLog $transferLog): static
{
if (!$this->transferLogs->contains($transferLog)) {
$this->transferLogs->add($transferLog);
$transferLog->setUser($this);
}
return $this;
}
public function removeTransferLog(ProductTransferLog $transferLog): static
{
if ($this->transferLogs->removeElement($transferLog)) {
// set the owning side to null (unless already changed)
if ($transferLog->getUser() === $this) {
$transferLog->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, ProductTransfer>
*/
public function getTransfersUserFrom(): Collection
{
return $this->transfersUserFrom;
}
public function addTransfersUserFrom(ProductTransfer $transfersUserFrom): static
{
if (!$this->transfersUserFrom->contains($transfersUserFrom)) {
$this->transfersUserFrom->add($transfersUserFrom);
$transfersUserFrom->setUserFrom($this);
}
return $this;
}
public function removeTransfersUserFrom(ProductTransfer $transfersUserFrom): static
{
if ($this->transfersUserFrom->removeElement($transfersUserFrom)) {
// set the owning side to null (unless already changed)
if ($transfersUserFrom->getUserFrom() === $this) {
$transfersUserFrom->setUserFrom(null);
}
}
return $this;
}
/**
* @return Collection<int, ProductTransfer>
*/
public function getTransfersUserTo(): Collection
{
return $this->transfersUserTo;
}
public function addTransfersUserTo(ProductTransfer $transfersUserTo): static
{
if (!$this->transfersUserTo->contains($transfersUserTo)) {
$this->transfersUserTo->add($transfersUserTo);
$transfersUserTo->setUserTo($this);
}
return $this;
}
public function removeTransfersUserTo(ProductTransfer $transfersUserTo): static
{
if ($this->transfersUserTo->removeElement($transfersUserTo)) {
// set the owning side to null (unless already changed)
if ($transfersUserTo->getUserTo() === $this) {
$transfersUserTo->setUserTo(null);
}
}
return $this;
}
/**
* @return Collection<int, Ticket>
*/
public function getTickets(): Collection
{
return $this->tickets;
}
public function addTicket(Ticket $ticket): static
{
if (!$this->tickets->contains($ticket)) {
$this->tickets->add($ticket);
$ticket->setTechnician($this);
}
return $this;
}
public function removeTicket(Ticket $ticket): static
{
if ($this->tickets->removeElement($ticket)) {
// set the owning side to null (unless already changed)
if ($ticket->getTechnician() === $this) {
$ticket->setTechnician(null);
}
}
return $this;
}
/**
* @return Collection<int, Intervention>
*/
public function getInterventions(): Collection
{
return $this->interventions;
}
public function addIntervention(Intervention $intervention): static
{
if (!$this->interventions->contains($intervention)) {
$this->interventions->add($intervention);
$intervention->setTechnician($this);
}
return $this;
}
public function removeIntervention(Intervention $intervention): static
{
if ($this->interventions->removeElement($intervention)) {
// set the owning side to null (unless already changed)
if ($intervention->getTechnician() === $this) {
$intervention->setTechnician(null);
}
}
return $this;
}
/**
* @return Collection<int, ProductRequest>
*/
public function getProductRequests(): Collection
{
return $this->productRequests;
}
public function addProductRequest(ProductRequest $productRequest): static
{
if (!$this->productRequests->contains($productRequest)) {
$this->productRequests->add($productRequest);
$productRequest->setTechnician($this);
}
return $this;
}
public function removeProductRequest(ProductRequest $productRequest): static
{
if ($this->productRequests->removeElement($productRequest)) {
// set the owning side to null (unless already changed)
if ($productRequest->getTechnician() === $this) {
$productRequest->setTechnician(null);
}
}
return $this;
}
/**
* @return Collection<int, Intervention>
*/
public function getOperatorInterventions(): Collection
{
return $this->operatorInterventions;
}
public function addOperatorIntervention(Intervention $operatorIntervention): static
{
if (!$this->operatorInterventions->contains($operatorIntervention)) {
$this->operatorInterventions->add($operatorIntervention);
$operatorIntervention->setOperator($this);
}
return $this;
}
public function removeOperatorIntervention(Intervention $operatorIntervention): static
{
if ($this->operatorInterventions->removeElement($operatorIntervention)) {
// set the owning side to null (unless already changed)
if ($operatorIntervention->getOperator() === $this) {
$operatorIntervention->setOperator(null);
}
}
return $this;
}
/**
* @return Collection<int, Supplier>
*/
public function getAutoAssignSuppliers(): Collection
{
return $this->autoAssignSuppliers;
}
public function addAutoAssignSupplier(Supplier $autoAssignSupplier): static
{
if (!$this->autoAssignSuppliers->contains($autoAssignSupplier)) {
$this->autoAssignSuppliers->add($autoAssignSupplier);
}
return $this;
}
public function removeAutoAssignSupplier(Supplier $autoAssignSupplier): static
{
$this->autoAssignSuppliers->removeElement($autoAssignSupplier);
return $this;
}
/**
* @return Collection<int, TicketReminder>
*/
public function getReminders(): Collection
{
return $this->reminders;
}
public function addReminder(TicketReminder $reminder): static
{
if (!$this->reminders->contains($reminder)) {
$this->reminders->add($reminder);
$reminder->setTechnician($this);
}
return $this;
}
public function removeReminder(TicketReminder $reminder): static
{
if ($this->reminders->removeElement($reminder)) {
// set the owning side to null (unless already changed)
if ($reminder->getTechnician() === $this) {
$reminder->setTechnician(null);
}
}
return $this;
}
}