<?php
namespace App\Entity;
use App\Repository\UserRepository;
use App\ValueObjects\BankAccounting;
use App\ValueObjects\WorkingTime;
use DateTime;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ORM\Table(name="users", indexes={
* @ORM\Index(name="idx_is_deleted", columns={"is_deleted"})
* })
*/
class User implements UserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer", options={"unsigned": true})
*/
private ?int $id = null;
/**
* @ORM\Column(type="string", length=255, unique=true)
*/
private string $username = '';
/**
* @ORM\Column(type="json")
*/
private array $roles = [];
/**
* @ORM\Column(type="string", length=255)
*/
private string $password;
/**
* @ORM\Column(type="string", length=255)
*/
private string $salt = "";
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $title = "";
/**
* @ORM\Column(type="boolean", options={"default": false})
*/
private ?bool $is_blocked = false;
/**
* @ORM\Column(type="boolean", options={"default": false})
*/
private ?bool $is_deleted = false;
/**
* @ORM\Column(type="text", nullable=true)
*/
private ?string $description = "";
/**
* @ORM\ManyToMany(targetEntity=Club::class, inversedBy="users", fetch="EXTRA_LAZY")
* @ORM\JoinTable(name="users_clubs")
*/
private Collection $clubs;
/**
* @ORM\Column(type="integer", options={"unsigned": true})
*/
private int $type_id = 0;
/**
* @ORM\Column(type="bigint", nullable=true, options={"default": 0})
*/
private ?int $balance = 0;
/**
* @ORM\OneToMany(targetEntity=Payment::class, mappedBy="operator")
*/
private Collection $payments;
private array $allowedClubsId = [];
private array $allowedUsersId = [];
private array $allowedCashdesksId = [];
private array $allowedCashdesksIdDeleted = [];
private array $permissionsAllowedForChange = [];
/**
* @ORM\OneToMany(targetEntity=Payment::class, mappedBy="user")
*/
private Collection $payments_user;
/**
* @ORM\ManyToOne(targetEntity=Club::class)
*/
private ?Club $current_club;
/**
* @ORM\OneToOne(targetEntity=Cashdesk::class, inversedBy="user", cascade={"persist", "remove"})
*/
private ?Cashdesk $current_cashdesk;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $remote_client_token;
/**
* @ORM\ManyToMany(targetEntity=Cashdesk::class, inversedBy="users")
* @ORM\JoinTable(name="users_cashdesks")
*/
private Collection $cashdesks;
/**
* @ORM\ManyToOne(targetEntity=Locale::class)
*/
private ?Locale $locale = null;
/**
* @ORM\ManyToMany(targetEntity=User::class)
* @ORM\JoinTable(name="users_users")
*/
private Collection $users;
/**
* @ORM\Column(type="datetime", nullable=true, columnDefinition="TIMESTAMP DEFAULT NULL")
*/
private ?DateTimeInterface $last_action_time = null;
/**
* @ORM\Column(type="datetime", nullable=true, columnDefinition="TIMESTAMP DEFAULT NULL")
*/
private ?DateTimeInterface $created_date = null;
/**
* @ORM\OneToMany(targetEntity=Timetable::class, mappedBy="user", orphanRemoval=true)
*/
private Collection $timetables;
/**
* @ORM\Column(type="boolean", nullable=true, options={"unsigned": true, "default": 1})
*/
private ?bool $timetable_enabled = true;
/**
* @ORM\Column(type="boolean", nullable=true, options={"unsigned": true, "default": 1})
*/
private ?bool $salary_enabled = true;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private ?int $hour_cost_0 = null;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private ?int $hour_cost_1 = null;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private ?int $hour_cost_2 = null;
/**
* @ORM\Column(type="integer", nullable=true, options={"unsigned": true})
*/
private ?int $shift_duration = null;
/**
* @ORM\Column(type="integer", nullable=true, options={"unsigned": true})
*/
private ?int $shift_cost = null;
/**
* @ORM\Column(type="string", options={"default": ""})
*/
private string $start_working_time = '';
/**
* @ORM\Column(type="string", options={"default": ""})
*/
private string $finish_working_time = '';
/**
* @ORM\Column(type="text", nullable=true)
*/
private ?string $bank_title = null;
/**
* @ORM\Column(type="string", length=255, options={"default": ""})
*/
private string $ban = '';
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private ?DateTimeInterface $last_login_time = null;
public function __construct()
{
$this->clubs = new ArrayCollection();
$this->payments = new ArrayCollection();
$this->payments_user = new ArrayCollection();
$this->cashdesks = new ArrayCollection();
$this->users = new ArrayCollection();
$this->timetables = new ArrayCollection();
}
public function isAllowedPaymentJackpot(): bool
{
return $this->getCurrentCashdesk()
&& $this->getCurrentCashdesk()->isGranted(Roles::ROLE_CASHDESK_ALLOW_PAYING_JACKPOTS);
}
public function hasRole(string $role): bool
{
return in_array($role, $this->getRoles());
}
public function setWorkingTime(WorkingTime $workingTime)
{
$this->setStartWorkingTime($workingTime->getStart());
$this->setFinishWorkingTime($workingTime->getFinish());
}
public function setBankAccounting(BankAccounting $bankAccounting)
{
$this->bank_title = $bankAccounting->getBankTitle();
$this->ban = $bankAccounting->getBan();
}
/**
* Allow user work in working time (if defined) only
* @return bool
*/
public function isWorkingTimeNow(): bool
{
if (!$this->isTimetableEnabled()) {
return true;
}
if (empty($this->getStartWorkingTime()) || empty($this->getFinishWorkingTime())) {
return true;
}
$currentTime = (new DateTime())->format("H:i");
if ($this->getStartWorkingTime() <= $currentTime && $currentTime <= $this->getFinishWorkingTime()) {
return true;
}
return false;
}
public function releaseCurrentCashdesk(): void
{
if (!empty($this->current_cashdesk)) {
$this->current_cashdesk = null;
}
}
public function isHideForAdministrator(string $permission): bool
{
if (is_null($this->getRoles())) {
return false;
}
return in_array($permission, $this->getRoles());
}
/**
* Get current club id via cashdesk
* @return int|null
*/
public function getCurrentClubId(): ?int
{
if ($this->getCurrentCashdesk() && $this->getCurrentCashdesk()->getClubId()) {
return $this->getCurrentCashdesk()->getClubId();
}
return null;
}
/**
* Get current affiliate id via cashdesk
* @return int
*/
public function getCurrentAffiliateId(): int
{
if ($this->getCurrentCashdesk() && $this->getCurrentCashdesk()->getClubId()) {
return $this->getCurrentCashdesk()->getClub()->getAffiliateId();
}
return 0;
}
/**
* @return array<int>
*/
public function getAllowedClubsId(): array
{
return $this->allowedClubsId;
}
/**
* @param array<int> $allowedClubsId
*/
public function setAllowedClubsId(array $allowedClubsId): void
{
$this->allowedClubsId = $allowedClubsId;
}
/**
* @return array<int>
*/
public function getAllowedUsersId(): array
{
return $this->allowedUsersId;
}
/**
* @param array<int> $allowedUsersId
*/
public function setAllowedUsersId(array $allowedUsersId): void
{
$this->allowedUsersId = $allowedUsersId;
}
public function getId(): ?int
{
return $this->id;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return $this->password;
// not needed for apps that do not check user passwords
}
/**
* @see UserInterface
*/
public function getSalt(): string
{
return $this->salt;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function getIsBlocked(): ?bool
{
return $this->is_blocked;
}
public function setIsBlocked(?bool $is_blocked): self
{
$this->is_blocked = $is_blocked;
return $this;
}
public function getIsDeleted(): ?bool
{
return $this->is_deleted;
}
public function setIsDeleted(?bool $is_deleted): self
{
$this->is_deleted = $is_deleted;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
/**
* @return Collection
*/
public function getClubs(): Collection
{
return $this->clubs;
}
public function addClub(Club $club): self
{
if (!$this->clubs->contains($club)) {
$this->clubs[] = $club;
}
return $this;
}
public function removeClub(Club $club): self
{
if ($this->clubs->contains($club)) {
$this->clubs->removeElement($club);
}
return $this;
}
public function getTypeId(): ?int
{
return $this->type_id;
}
public function setTypeId(int $type_id): self
{
$this->type_id = $type_id;
return $this;
}
public function getBalance(): ?int
{
return $this->balance;
}
public function setBalance(?int $balance): self
{
$this->balance = $balance;
return $this;
}
/**
* @return Collection
*/
public function getPayments(): Collection
{
return $this->payments;
}
public function addPayment(Payment $payment): self
{
if (!$this->payments->contains($payment)) {
$this->payments[] = $payment;
$payment->setOperator($this);
}
return $this;
}
public function removePayment(Payment $payment): self
{
if ($this->payments->contains($payment)) {
$this->payments->removeElement($payment);
// set the owning side to null (unless already changed)
if ($payment->getOperator() === $this) {
$payment->setOperator(null);
}
}
return $this;
}
/**
* @return array<int>
*/
public function getAllowedCashdesksId(): array
{
return $this->allowedCashdesksId;
}
/**
* @param array<int> $allowedCashdesksId
*/
public function setAllowedCashdesksId(array $allowedCashdesksId): void
{
$this->allowedCashdesksId = $allowedCashdesksId;
}
/**
* @return array<int>
*/
public function getAllowedCashdesksIdDeleted(): array
{
return $this->allowedCashdesksIdDeleted;
}
/**
* @param array<int> $allowedCashdesksIdDeleted
*/
public function setAllowedCashdesksIdDeleted(array $allowedCashdesksIdDeleted): void
{
$this->allowedCashdesksIdDeleted = $allowedCashdesksIdDeleted;
}
/**
* @return Collection
*/
public function getPaymentsUser(): Collection
{
return $this->payments_user;
}
public function addPaymentsUser(Payment $paymentsUser): self
{
if (!$this->payments_user->contains($paymentsUser)) {
$this->payments_user[] = $paymentsUser;
$paymentsUser->setUser($this);
}
return $this;
}
public function removePaymentsUser(Payment $paymentsUser): self
{
if ($this->payments_user->contains($paymentsUser)) {
$this->payments_user->removeElement($paymentsUser);
// set the owning side to null (unless already changed)
if ($paymentsUser->getUser() === $this) {
$paymentsUser->setUser(null);
}
}
return $this;
}
public function getCurrentClub(): ?Club
{
return $this->current_club;
}
public function setCurrentClub(?Club $current_club): self
{
$this->current_club = $current_club;
return $this;
}
public function getCurrentCashdesk(): ?Cashdesk
{
return $this->current_cashdesk;
}
public function setCurrentCashdesk(?Cashdesk $current_cashdesk): self
{
$this->current_cashdesk = $current_cashdesk;
return $this;
}
public function getRemoteClientToken(): ?string
{
return $this->remote_client_token;
}
public function setRemoteClientToken(?string $remote_client_token): self
{
$this->remote_client_token = $remote_client_token;
return $this;
}
/**
* @return Collection
*/
public function getCashdesks(): Collection
{
return $this->cashdesks;
}
public function addCashdesk(Cashdesk $cashdesk): self
{
if (!$this->cashdesks->contains($cashdesk)) {
$this->cashdesks[] = $cashdesk;
}
return $this;
}
public function removeCashdesk(Cashdesk $cashdesk): self
{
if ($this->cashdesks->contains($cashdesk)) {
$this->cashdesks->removeElement($cashdesk);
}
return $this;
}
public function getLocale(): ?Locale
{
return $this->locale;
}
public function setLocale(?Locale $locale): self
{
$this->locale = $locale;
return $this;
}
/**
* @return Collection
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(self $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
}
return $this;
}
public function removeUser(self $user): self
{
if ($this->users->contains($user)) {
$this->users->removeElement($user);
}
return $this;
}
public function getLastActionTime(): ?DateTimeInterface
{
return $this->last_action_time;
}
public function setLastActionTime(?DateTimeInterface $last_action_time): self
{
$this->last_action_time = $last_action_time;
return $this;
}
public function getCreatedDate(): ?DateTimeInterface
{
return $this->created_date;
}
public function setCreatedDate(?DateTimeInterface $created_date): self
{
$this->created_date = $created_date;
return $this;
}
/**
* @return Collection
*/
public function getTimetables(): Collection
{
return $this->timetables;
}
public function addTimetable(Timetable $timetable): self
{
if (!$this->timetables->contains($timetable)) {
$this->timetables[] = $timetable;
$timetable->setUser($this);
}
return $this;
}
public function isTimetableEnabled(): ?bool
{
return $this->timetable_enabled;
}
public function setTimetableEnabled(?bool $timetable_enabled): self
{
$this->timetable_enabled = $timetable_enabled;
return $this;
}
public function isSalaryEnabled(): ?bool
{
return $this->salary_enabled;
}
public function setSalaryEnabled(?bool $salary_enabled): self
{
$this->salary_enabled = $salary_enabled;
return $this;
}
public function getShiftDuration(): ?int
{
return $this->shift_duration;
}
public function setShiftDuration(?int $shift_duration): self
{
$this->shift_duration = $shift_duration;
return $this;
}
public function getShiftCost(): ?int
{
return $this->shift_cost;
}
public function setShiftCost(?int $shift_cost): self
{
$this->shift_cost = $shift_cost;
return $this;
}
/**
* @return array
*/
public function getPermissionsAllowedForChange(): array
{
return $this->permissionsAllowedForChange;
}
/**
* @param array $permissionsAllowedForChange
* @return User
*/
public function setPermissionsAllowedForChange(array $permissionsAllowedForChange): User
{
$this->permissionsAllowedForChange = $permissionsAllowedForChange;
return $this;
}
public function getStartWorkingTime(): string
{
return $this->start_working_time;
}
public function setStartWorkingTime(string $start_working_time): self
{
$this->start_working_time = $start_working_time;
return $this;
}
public function getFinishWorkingTime(): string
{
return $this->finish_working_time;
}
public function setFinishWorkingTime(string $finish_working_time): self
{
$this->finish_working_time = $finish_working_time;
return $this;
}
public function getHourCost_0(): ?int
{
return $this->hour_cost_0;
}
public function setHourCost_0(?int $hour_cost_0): self
{
$this->hour_cost_0 = $hour_cost_0;
return $this;
}
public function getHourCost_1(): ?int
{
return $this->hour_cost_1;
}
public function setHourCost_1(?int $hour_cost_1): self
{
$this->hour_cost_1 = $hour_cost_1;
return $this;
}
public function getHourCost_2(): ?int
{
return $this->hour_cost_2;
}
public function setHourCost_2(?int $hour_cost_2): self
{
$this->hour_cost_2 = $hour_cost_2;
return $this;
}
/**
* @return string|null
*/
public function getBankTitle(): ?string
{
return $this->bank_title;
}
/**
* @return string
*/
public function getBan(): string
{
return $this->ban;
}
public function getLastLoginTime(): ?\DateTimeInterface
{
return $this->last_login_time;
}
public function setLastLoginTime(?\DateTimeInterface $last_login_time): self
{
$this->last_login_time = $last_login_time;
return $this;
}
public function getUserIdentifier(): string
{
return $this->username;
}
}