<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\UserRepository;
use App\State\UserPasswordHasher;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use App\Controller\UserController;
use App\Filter\CustomOrFilter; // Змініть імпорт на правильний
// use ApiPlatform\Core\Metadata\Reswource\DeprecationMetadata;
#[ORM\Table(name: 'users')]
#[ApiResource(
operations: [
new GetCollection(),
new Post(processor: UserPasswordHasher::class, validationContext: ['groups' => ['Default', 'user:create']]),
new Get(),
new Put(processor: UserPasswordHasher::class),
new Patch(processor: UserPasswordHasher::class),
new Post(
name: 'me',
uriTemplate: '/users/me',
controller: UserController::class
)
],
normalizationContext: ['groups' => ['user:read']],
denormalizationContext: ['groups' => ['user:create', 'user:update']],
)]
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(fields: ['username'], message: 'There is already an account with this username')]
#[ApiFilter(CustomOrFilter::class,properties: [
'username' => 'ipartial',
'firstName' => 'ipartial',
'lastName' => 'ipartial',
'codeUser' => 'exact',
'codeManager' => 'exact',
])]
#[ApiFilter(SearchFilter::class, properties: [
'accounts.manager.id' => 'exact',
])]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['user:read', 'read', 'account:read', 'order:read', 'order_product:read', 'pre_order_product:read', 'pre_order:read', 'load_invoice:read', 'load_invocie:read', 'product_storage_balance:read', 'product:read'])]
private ?int $id = null;
#[ORM\Column(length: 180, unique: true)]
#[Groups(['user:read', 'user:create', 'user:update', 'read', 'account:read'])]
private ?string $username = null;
#[Groups(['user:read', 'user:update'])]
#[ORM\Column]
private array $roles = [];
/**
* @var string The hashed password
*/
#[ORM\Column]
private ?string $password = null;
#[Assert\NotBlank(groups: ['user:create'])]
#[Groups(['user:create', 'user:update'])]
private ?string $plainPassword = null;
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'firstName')]
private ?self $manager = null;
#[Groups(['user:read', 'user:create', 'user:update', 'read', 'account:read', 'account:write', 'order:read', 'order_product:read', 'pre_order_product:read', 'pre_order:read', 'load_invoice:read', 'load_invocie:read', 'product_storage_balance:read', 'product:read'])]
#[ORM\Column(length: 255, nullable: true)]
private ?string $firstName = null;
#[Groups(['user:read', 'user:create', 'user:update', 'read', 'account:read', 'account:write', 'order:read', 'order_product:read', 'pre_order_product:read', 'pre_order:read', 'load_invoice:read', 'load_invocie:read', 'product_storage_balance:read', 'product:read'])]
#[ORM\Column(length: 255, nullable: true)]
private ?string $lastName = null;
#[Groups(['user:read', 'user:create', 'user:update', 'account:read'])]
#[ORM\Column(length: 100, nullable: true)]
private ?string $phone = null;
#[Groups(['user:read', 'user:create', 'user:update'])]
#[ORM\Column(length: 255, nullable: true)]
private ?string $address = null;
#[Groups(['user:read', 'user:create', 'user:update'])]
#[ORM\Column(length: 50, nullable: true)]
private ?string $status = null;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: UserLikeList::class)]
private Collection $userLikeLists;
#[ORM\OneToMany(mappedBy: 'modified_user', targetEntity: Products::class)]
private Collection $products;
#[ORM\OneToMany(mappedBy: 'created_by', targetEntity: Products::class)]
private Collection $create_products;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Orders::class)]
private Collection $orders;
#[ORM\OneToMany(mappedBy: 'manager', targetEntity: Orders::class)]
private Collection $managerOrders;
#[Groups(['user:read', 'user:create', 'user:update', 'read', 'account:read'])]
#[ORM\Column(length: 255, nullable: true)]
private ?string $email = null;
#[Groups(['user:read', 'user:create', 'user:update', 'read'])]
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Accounts::class)]
private Collection $accounts;
#[ORM\OneToMany(mappedBy: 'client', targetEntity: LoadInvoice::class)]
private Collection $loadInvoices;
#[ORM\OneToMany(mappedBy: 'manager', targetEntity: Accepted::class)]
private Collection $accepteds;
#[ORM\OneToMany(mappedBy: 'modified_user', targetEntity: AcceptedProduct::class)]
private Collection $n;
#[ORM\Column(length: 20, nullable: true)]
#[Groups(['user:read', 'user:create', 'user:update', 'order:read', 'order_product:read'])]
private ?string $codeUser = null;
#[ORM\Column(length: 20, nullable: true)]
#[Groups(['user:read', 'user:create', 'user:update', 'order:read', 'order_product:read'])]
private ?string $codeManager = null;
#[ORM\OneToMany(mappedBy: 'manager', targetEntity: Accounts::class)]
private Collection $managerAccounts;
#[ORM\OneToMany(mappedBy: 'client', targetEntity: PreOrder::class)]
private Collection $preOrders;
#[ORM\OneToMany(mappedBy: 'manager', targetEntity: PreOrder::class)]
private Collection $ManagerPreOrders;
#[Groups(['user:read', 'user:create', 'user:update', 'read', 'account:read'])]
#[ORM\Column(length: 255, nullable: true)]
private ?string $workSchedule = null;
#[Groups(['user:read', 'user:create', 'user:update', 'read', 'account:read'])]
#[ORM\Column(length: 255, nullable: true)]
private ?string $telegram = null;
#[Groups(['user:read', 'user:create', 'user:update', 'read', 'account:read'])]
#[ORM\Column(length: 255, nullable: true)]
private ?string $viber = null;
#[Groups(['user:read', 'user:create', 'user:update', 'read', 'account:read'])]
#[ORM\OneToMany(mappedBy: 'users', targetEntity: MediaObject::class)]
private Collection $mediaObjects;
#[ORM\ManyToMany(targetEntity: Coupons::class, mappedBy: 'users')]
private Collection $coupons;
#[ORM\OneToMany(mappedBy: 'manager', targetEntity: ProductBalanceInStorage::class)]
private Collection $productBalanceInStorages;
#[Groups(['user:read', 'user:create', 'user:update'])]
#[ORM\ManyToOne(inversedBy: 'users')]
private ?Location $location = null;
public function __construct()
{
$this->userLikeLists = new ArrayCollection();
$this->products = new ArrayCollection();
$this->create_products = new ArrayCollection();
$this->orders = new ArrayCollection();
$this->managerOrders = new ArrayCollection();
$this->accounts = new ArrayCollection();
$this->loadInvoices = new ArrayCollection();
$this->accepteds = new ArrayCollection();
$this->n = new ArrayCollection();
$this->managerAccounts = new ArrayCollection();
$this->preOrders = new ArrayCollection();
$this->ManagerPreOrders = new ArrayCollection();
$this->mediaObjects = new ArrayCollection();
$this->coupons = new ArrayCollection();
$this->productBalanceInStorages = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->username;
}
/**
* @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 PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getPlainPassword(): ?string
{
return $this->plainPassword;
}
public function setPlainPassword(?string $plainPassword): self
{
$this->plainPassword = $plainPassword;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
$this->plainPassword = null;
}
public function getManagerId(): ?self
{
return $this->manager;
}
public function setManagerId(?self $manager): self
{
$this->manager = $manager;
return $this;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(?string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getAddress(): ?string
{
return $this->address;
}
public function setAddress(?string $address): self
{
$this->address = $address;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(?string $status): self
{
$this->status = $status;
return $this;
}
/**
* @return Collection<int, UserLikeList>
*/
public function getUserLikeLists(): Collection
{
return $this->userLikeLists;
}
public function addUserLikeList(UserLikeList $userLikeList): self
{
if (!$this->userLikeLists->contains($userLikeList)) {
$this->userLikeLists->add($userLikeList);
$userLikeList->setUserId($this);
}
return $this;
}
public function removeUserLikeList(UserLikeList $userLikeList): self
{
if ($this->userLikeLists->removeElement($userLikeList)) {
// set the owning side to null (unless already changed)
if ($userLikeList->getUserId() === $this) {
$userLikeList->setUserId(null);
}
}
return $this;
}
/**
* @return Collection<int, Products>
*/
public function getProducts(): Collection
{
return $this->products;
}
public function addProduct(Products $product): self
{
if (!$this->products->contains($product)) {
$this->products->add($product);
$product->setModifiedUserId($this);
}
return $this;
}
// public function removeProduct(Products $product): self
// {
// if ($this->products->removeElement($product)) {
// // set the owning side to null (unless already changed)
// if ($product->getModifiedUserId() === $this) {
// $product->setModifiedUserId(null);
// }
// }
// return $this;
// }
/**
* @return Collection<int, Products>
*/
public function getCreateProducts(): Collection
{
return $this->create_products;
}
public function addCreateProduct(Products $createProduct): self
{
if (!$this->create_products->contains($createProduct)) {
$this->create_products->add($createProduct);
$createProduct->setCreatedBy($this);
}
return $this;
}
// public function removeCreateProduct(Products $createProduct): self
// {
// if ($this->create_products->removeElement($createProduct)) {
// // set the owning side to null (unless already changed)
// if ($createProduct->getCreatedBy() === $this) {
// $createProduct->setCreatedBy(null);
// }
// }
// return $this;
// }
/**
* @return Collection<int, Orders>
*/
public function getOrders(): Collection
{
return $this->orders;
}
public function addOrder(Orders $order): self
{
if (!$this->orders->contains($order)) {
$this->orders->add($order);
$order->setUserId($this);
}
return $this;
}
// public function removeOrder(Orders $order): self
// {
// if ($this->orders->removeElement($order)) {
// // set the owning side to null (unless already changed)
// if ($order->getUserId() === $this) {
// $order->setUserId(null);
// }
// }
// return $this;
// }
/**
* @return Collection<int, Orders>
*/
public function getManagerOrders(): Collection
{
return $this->managerOrders;
}
public function addManagerOrder(Orders $managerOrder): self
{
if (!$this->managerOrders->contains($managerOrder)) {
$this->managerOrders->add($managerOrder);
$managerOrder->setManagerId($this);
}
return $this;
}
// public function removeManagerOrder(Orders $managerOrder): self
// {
// if ($this->managerOrders->removeElement($managerOrder)) {
// // set the owning side to null (unless already changed)
// if ($managerOrder->getManagerId() === $this) {
// $managerOrder->setManagerId(null);
// }
// }
// return $this;
// }
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
/**
* @return Collection<int, Accounts>
*/
public function getAccounts(): Collection
{
return $this->accounts;
}
public function addAccount(Accounts $account): self
{
if (!$this->accounts->contains($account)) {
$this->accounts->add($account);
$account->setUserId($this);
}
return $this;
}
public function removeAccount(Accounts $account): self
{
if ($this->accounts->removeElement($account)) {
// set the owning side to null (unless already changed)
if ($account->getUserId() === $this) {
$account->setUserId(null);
}
}
return $this;
}
/**
* @return Collection<int, LoadInvoice>
*/
public function getLoadInvoices(): Collection
{
return $this->loadInvoices;
}
public function addLoadInvoice(LoadInvoice $loadInvoice): self
{
if (!$this->loadInvoices->contains($loadInvoice)) {
$this->loadInvoices->add($loadInvoice);
$loadInvoice->setClient($this);
}
return $this;
}
public function removeLoadInvoice(LoadInvoice $loadInvoice): self
{
if ($this->loadInvoices->removeElement($loadInvoice)) {
// set the owning side to null (unless already changed)
if ($loadInvoice->getClient() === $this) {
$loadInvoice->setClient(null);
}
}
return $this;
}
/**
* @return Collection<int, Accepted>
*/
public function getAccepteds(): Collection
{
return $this->accepteds;
}
public function addAccepted(Accepted $accepted): self
{
if (!$this->accepteds->contains($accepted)) {
$this->accepteds->add($accepted);
$accepted->setManager($this);
}
return $this;
}
public function removeAccepted(Accepted $accepted): self
{
if ($this->accepteds->removeElement($accepted)) {
// set the owning side to null (unless already changed)
if ($accepted->getManager() === $this) {
$accepted->setManager(null);
}
}
return $this;
}
/**
* @return Collection<int, AcceptedProduct>
*/
public function getN(): Collection
{
return $this->n;
}
public function addN(AcceptedProduct $n): self
{
if (!$this->n->contains($n)) {
$this->n->add($n);
$n->setModifiedUser($this);
}
return $this;
}
public function removeN(AcceptedProduct $n): self
{
if ($this->n->removeElement($n)) {
// set the owning side to null (unless already changed)
if ($n->getModifiedUser() === $this) {
$n->setModifiedUser(null);
}
}
return $this;
}
public function getCodeUser(): ?string
{
return $this->codeUser;
}
public function setCodeUser(string $codeUser): self
{
$this->codeUser = $codeUser;
return $this;
}
public function getCodeManager(): ?string
{
return $this->codeManager;
}
public function setCodeManager(string $codeManager): self
{
$this->codeManager = $codeManager;
return $this;
}
/**
* @return Collection<int, Accounts>
*/
public function getManagerAccounts(): Collection
{
return $this->managerAccounts;
}
public function addManagerAccount(Accounts $managerAccount): self
{
if (!$this->managerAccounts->contains($managerAccount)) {
$this->managerAccounts->add($managerAccount);
$managerAccount->setManager($this);
}
return $this;
}
public function removeManagerAccount(Accounts $managerAccount): self
{
if ($this->managerAccounts->removeElement($managerAccount)) {
// set the owning side to null (unless already changed)
if ($managerAccount->getManager() === $this) {
$managerAccount->setManager(null);
}
}
return $this;
}
/**
* @return Collection<int, PreOrder>
*/
public function getPreOrders(): Collection
{
return $this->preOrders;
}
public function addPreOrder(PreOrder $preOrder): self
{
if (!$this->preOrders->contains($preOrder)) {
$this->preOrders->add($preOrder);
$preOrder->setClient($this);
}
return $this;
}
public function removePreOrder(PreOrder $preOrder): self
{
if ($this->preOrders->removeElement($preOrder)) {
// set the owning side to null (unless already changed)
if ($preOrder->getClient() === $this) {
$preOrder->setClient(null);
}
}
return $this;
}
/**
* @return Collection<int, PreOrder>
*/
public function getManagerPreOrders(): Collection
{
return $this->ManagerPreOrders;
}
public function addManagerPreOrder(PreOrder $managerPreOrder): self
{
if (!$this->ManagerPreOrders->contains($managerPreOrder)) {
$this->ManagerPreOrders->add($managerPreOrder);
$managerPreOrder->setManager($this);
}
return $this;
}
public function removeManagerPreOrder(PreOrder $managerPreOrder): self
{
if ($this->ManagerPreOrders->removeElement($managerPreOrder)) {
// set the owning side to null (unless already changed)
if ($managerPreOrder->getManager() === $this) {
$managerPreOrder->setManager(null);
}
}
return $this;
}
public function getWorkSchedule(): ?string
{
return $this->workSchedule;
}
public function setWorkSchedule(?string $workSchedule): self
{
$this->workSchedule = $workSchedule;
return $this;
}
public function getTelegram(): ?string
{
return $this->telegram;
}
public function setTelegram(?string $telegram): self
{
$this->telegram = $telegram;
return $this;
}
public function getViber(): ?string
{
return $this->viber;
}
public function setViber(?string $viber): self
{
$this->viber = $viber;
return $this;
}
/**
* @return Collection<int, MediaObject>
*/
public function getMediaObjects(): Collection
{
return $this->mediaObjects;
}
public function addMediaObject(MediaObject $mediaObject): self
{
if (!$this->mediaObjects->contains($mediaObject)) {
$this->mediaObjects->add($mediaObject);
$mediaObject->setUsers($this);
}
return $this;
}
public function removeMediaObject(MediaObject $mediaObject): self
{
if ($this->mediaObjects->removeElement($mediaObject)) {
// set the owning side to null (unless already changed)
if ($mediaObject->getUsers() === $this) {
$mediaObject->setUsers(null);
}
}
return $this;
}
/**
* @return Collection<int, Coupons>
*/
public function getCoupons(): Collection
{
return $this->coupons;
}
public function addCoupon(Coupons $coupon): self
{
if (!$this->coupons->contains($coupon)) {
$this->coupons->add($coupon);
$coupon->addUser($this);
}
return $this;
}
public function removeCoupon(Coupons $coupon): self
{
if ($this->coupons->removeElement($coupon)) {
$coupon->removeUser($this);
}
return $this;
}
/**
* @return Collection<int, ProductBalanceInStorage>
*/
public function getProductBalanceInStorages(): Collection
{
return $this->productBalanceInStorages;
}
public function addProductBalanceInStorage(ProductBalanceInStorage $productBalanceInStorage): self
{
if (!$this->productBalanceInStorages->contains($productBalanceInStorage)) {
$this->productBalanceInStorages->add($productBalanceInStorage);
$productBalanceInStorage->setManager($this);
}
return $this;
}
public function removeProductBalanceInStorage(ProductBalanceInStorage $productBalanceInStorage): self
{
if ($this->productBalanceInStorages->removeElement($productBalanceInStorage)) {
// set the owning side to null (unless already changed)
if ($productBalanceInStorage->getManager() === $this) {
$productBalanceInStorage->setManager(null);
}
}
return $this;
}
public function getLocation(): ?Location
{
return $this->location;
}
public function setLocation(?Location $location): self
{
$this->location = $location;
return $this;
}
}