src/Entity/Employee.php line 47

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Metadata\ApiResource;
  4. use ApiPlatform\Metadata\GetCollection;
  5. use ApiPlatform\Metadata\Get;
  6. use ApiPlatform\Metadata\Link;
  7. use App\Controller\Api\Employee\ApiEstablishmentEmployeeStatsController;
  8. use App\Controller\Api\Order\ApiEmployeeOrdersController;
  9. use App\Kimia;
  10. use App\Model\Wallet\WalletInterface;
  11. use App\Repository\EmployeeRepository;
  12. use App\Trait\ActivableTrait;
  13. use App\Trait\DateTrait;
  14. use App\Trait\DeletableTrait;
  15. use App\Trait\WalletTrait;
  16. use DateTimeImmutable;
  17. use Doctrine\Common\Collections\ArrayCollection;
  18. use Doctrine\Common\Collections\Collection;
  19. use Doctrine\DBAL\Types\Types;
  20. use Doctrine\ORM\Mapping as ORM;
  21. use Symfony\Component\Serializer\Annotation\Groups;
  22. #[ORM\Entity(repositoryClassEmployeeRepository::class)]
  23. #[ApiResource(
  24.     operations: [
  25.         new GetCollection(
  26.             normalizationContext: ['groups' => ['employee:list']]
  27.         ),
  28.         new GetCollection(
  29.             uriTemplate"/departments/{departmentId}/employees",
  30.             uriVariables: [
  31.                 'departmentId' => new Link(toProperty'department'fromClassDepartment::class),
  32.             ],
  33.             normalizationContext: ['groups' => ['employee:list']]
  34.         ),
  35.         new Get(),
  36.         new Get(
  37.             uriTemplate"/employees/{id}/stats",
  38.             controllerApiEstablishmentEmployeeStatsController::class,
  39.             normalizationContext: ['groups' => ['order:list']]
  40.         ),
  41.     ],
  42.     normalizationContext: ['groups' => ['employee:list''employee:view']]
  43. )]
  44. class Employee implements WalletInterface
  45. {
  46.     use DateTrait {
  47.         DateTrait::__construct as dateConstruct;
  48.     }
  49.     use ActivableTrait;
  50.     use DeletableTrait;
  51.     use WalletTrait;
  52.     #[ORM\Id]
  53.     #[ORM\GeneratedValue]
  54.     #[ORM\Column]
  55.     #[Groups(['employee:list''flow:list''attendance:list'])]
  56.     private ?int $id null;
  57.     #[ORM\Column(length255)]
  58.     #[Groups(['employee:list''flow:list''attendance:list'])]
  59.     private ?string $firstname null;
  60.     #[ORM\Column(length255)]
  61.     #[Groups(['employee:list''flow:list''attendance:list'])]
  62.     private ?string $lastname null;
  63.     #[ORM\Column(length180nullabletrue)]
  64.     #[Groups(['employee:list''flow:list''attendance:list'])]
  65.     private ?string $email null;
  66.     #[ORM\Column(length15)]
  67.     #[Groups(['employee:list''flow:list''attendance:list'])]
  68.     private ?string $phone null;
  69.     #[ORM\Column(typeTypes::JSON)]
  70.     #[Groups(['employee:view'])]
  71.     private array $permissions = [];
  72.     #[ORM\Column(length255)]
  73.     private ?string $status;
  74.     #[ORM\ManyToOne(cascade: ["persist"])]
  75.     #[ORM\JoinColumn(nullabletrue)]
  76.     private ?Invitation $invitation null;
  77.     #[ORM\ManyToOne]
  78.     private ?User $user null;
  79.     #[ORM\ManyToOne(inversedBy'employees')]
  80.     private ?Department $department null;
  81.     #[ORM\OneToMany(mappedBy'employee'targetEntityAttendance::class, orphanRemovaltrue)]
  82.     private Collection $attendances;
  83.     #[ORM\OneToMany(mappedBy'waiter'targetEntityOrder::class)]
  84.     private Collection $orders;
  85.     #[ORM\Column]
  86.     private ?int $quota;
  87.     #[ORM\ManyToOne(inversedBy'employees')]
  88.     private ?Establishment $establishment null;
  89.     #[ORM\OneToMany(mappedBy'user'targetEntityFlowUser::class)]
  90.     private Collection $flowUsers;
  91.     #[ORM\Column]
  92.     private ?bool $paymentQuota null;
  93.     #[ORM\Column(nullabletrue)]
  94.     private ?float $percentQuota null;
  95.     #[ORM\ManyToOne(inversedBy'employees')]
  96.     private ?Role $function null;
  97.     #[ORM\OneToMany(mappedBy'employee'targetEntityDocument::class)]
  98.     private Collection $documents;
  99.     #[ORM\OneToMany(mappedBy'employee'targetEntityEmployeeContract::class, orphanRemovaltrue)]
  100.     private Collection $contracts;
  101.     #[ORM\OneToMany(mappedBy'employee'targetEntityAddress::class)]
  102.     private Collection $addresses;
  103.     #[ORM\OneToMany(mappedBy'employee'targetEntityCard::class)]
  104.     private Collection $cards;
  105.     #[ORM\OneToMany(mappedBy'employee'targetEntityMutuality::class)]
  106.     private Collection $mutualities;
  107.     public function __construct()
  108.     {
  109.         $this->dateConstruct();
  110.         $this->quota 0;
  111.         $this->attendances = new ArrayCollection();
  112.         $this->orders = new ArrayCollection();
  113.         $this->flowUsers = new ArrayCollection();
  114.         $this->documents = new ArrayCollection();
  115.         $this->contracts = new ArrayCollection();
  116.         $this->addresses = new ArrayCollection();
  117.         $this->cards = new ArrayCollection();
  118.         $this->mutualities = new ArrayCollection();
  119.     }
  120.     public function getId(): ?int
  121.     {
  122.         return $this->id;
  123.     }
  124.     public function getFullname(): ?string
  125.     {
  126.         return "$this->firstname $this->lastname";
  127.     }
  128.     public function getName(): ?string
  129.     {
  130.         return "$this->firstname $this->lastname";
  131.     }
  132.     public function getFirstname(): ?string
  133.     {
  134.         return $this->firstname;
  135.     }
  136.     public function setFirstname(string $firstname): self
  137.     {
  138.         $this->firstname $firstname;
  139.         return $this;
  140.     }
  141.     public function getLastname(): ?string
  142.     {
  143.         return $this->lastname;
  144.     }
  145.     public function setLastname(string $lastname): self
  146.     {
  147.         $this->lastname $lastname;
  148.         return $this;
  149.     }
  150.     public function getEmail(): ?string
  151.     {
  152.         return $this->email;
  153.     }
  154.     public function setEmail(?string $email): self
  155.     {
  156.         $this->email $email;
  157.         return $this;
  158.     }
  159.     public function getPhone(): ?string
  160.     {
  161.         return $this->phone;
  162.     }
  163.     public function setPhone(string $phone): self
  164.     {
  165.         $this->phone preg_replace('/^(\+243|0|00243)([0-9]{9})$/''243$2'$phone);
  166.         return $this;
  167.     }
  168.     public function getPermissions(): array
  169.     {
  170.         return $this->permissions;
  171.     }
  172.     public static function getPermissionList(): array
  173.     {
  174.         return [
  175.             "Voir les départements" => Kimia::ROLE_VIEW_DEPARTMENT,
  176.             "Créer les départements" => Kimia::ROLE_CREATE_DEPARTMENT,
  177.             "Modifier les départements" => Kimia::ROLE_EDIT_DEPARTMENT,
  178.             "Supprimer les départements" => Kimia::ROLE_DELETE_DEPARTMENT,
  179.             "Activer les départements" => Kimia::ROLE_ACTIVATE_DEPARTMENT,
  180.             "Désactiver les départements" => Kimia::ROLE_DEACTIVATE_DEPARTMENT,
  181.             "Voir les articles" => Kimia::ROLE_VIEW_ARTICLE,
  182.             "Créer les articles" => Kimia::ROLE_CREATE_ARTICLE,
  183.             "Modifier les articles" => Kimia::ROLE_EDIT_ARTICLE,
  184.             "Supprimer les articles" => Kimia::ROLE_DELETE_ARTICLE,
  185.             "Activer les articles" => Kimia::ROLE_ACTIVATE_ARTICLE,
  186.             "Désactiver les articles" => Kimia::ROLE_DEACTIVATE_ARTICLE,
  187.             "Voir les collections" => Kimia::ROLE_VIEW_COLLECTION,
  188.             "Créer les collections" => Kimia::ROLE_CREATE_COLLECTION,
  189.             "Modifier les collections" => Kimia::ROLE_EDIT_COLLECTION,
  190.             "Supprimer les collections" => Kimia::ROLE_DELETE_COLLECTION,
  191.             "Activer les collections" => Kimia::ROLE_ACTIVATE_COLLECTION,
  192.             "Désactiver les collections" => Kimia::ROLE_DEACTIVATE_COLLECTION,
  193.             "Gérer les finances" => Kimia::ROLE_VIEW_FINANCE,
  194.             "Analyser les données" => Kimia::ROLE_VIEW_DATA_ANALYST,
  195.             "Voir le profile" => Kimia::ROLE_VIEW_PROFILE,
  196.             "Voir les réductions" => Kimia::ROLE_VIEW_REDUCTION,
  197.             "Créer les réductions" => Kimia::ROLE_CREATE_REDUCTION,
  198.             "Modifier les réductions" => Kimia::ROLE_EDIT_REDUCTION,
  199.             "Supprimer les réductions" => Kimia::ROLE_DELETE_REDUCTION,
  200.             "Activer les réductions" => Kimia::ROLE_ACTIVATE_REDUCTION,
  201.             "Desactiver les réductions" => Kimia::ROLE_DEACTIVATE_REDUCTION,
  202.             "Voir les stocks" => Kimia::ROLE_VIEW_STOCK,
  203.             "Créer les stocks" => Kimia::ROLE_CREATE_STOCK,
  204.             "Modifier les stocks" => Kimia::ROLE_EDIT_STOCK,
  205.             "Supprimer les stocks" => Kimia::ROLE_DELETE_STOCK,
  206.             "Approuver les stocks" => Kimia::ROLE_APPROVE_STOCK,
  207.             "Annuler les stocks" => Kimia::ROLE_CANCEL_STOCK,
  208.             "Voir les commandes" => Kimia::ROLE_VIEW_ORDER,
  209.             "Créer les commandes" => Kimia::ROLE_CREATE_ORDER,
  210.             "Modifier les commandes" => Kimia::ROLE_EDIT_ORDER,
  211.             "Supprimer les commandes" => Kimia::ROLE_DELETE_ORDER,
  212.             "Annuler les commandes" => Kimia::ROLE_CANCEL_ORDER,
  213.             "Voir les planning" => Kimia::ROLE_VIEW_PLANNING,
  214.             "Créer les planning" => Kimia::ROLE_CREATE_PLANNING,
  215.             "Modifier les planning" => Kimia::ROLE_EDIT_PLANNING,
  216.             "Supprimer les planning" => Kimia::ROLE_DELETE_PLANNING,
  217.             "Activer les planning" => Kimia::ROLE_ACTIVATE_PLANNING,
  218.             "Désactiver les planning" => Kimia::ROLE_DEACTIVATE_PLANNING,
  219.             "Voir les employés" => Kimia::ROLE_VIEW_EMPLOYEE,
  220.             "Ajouter des employés" => Kimia::ROLE_CREATE_EMPLOYEE,
  221.             "Modifier des employés" => Kimia::ROLE_EDIT_EMPLOYEE,
  222.             "Supprimer des employés" => Kimia::ROLE_DELETE_EMPLOYEE,
  223.             "Voir les clients" => Kimia::ROLE_VIEW_CLIENT,
  224.             "Créer les clients" => Kimia::ROLE_CREATE_CLIENT,
  225.             "Modifier les clients" => Kimia::ROLE_EDIT_CLIENT,
  226.             "Supprimer les clients" => Kimia::ROLE_DELETE_CLIENT,
  227.             "Voir les présences" => Kimia::ROLE_VIEW_PRESENCE,
  228.             "Créer les présences" => Kimia::ROLE_CREATE_PRESENCE,
  229.             "Modifier les présences" => Kimia::ROLE_EDIT_PRESENCE,
  230.             "Supprimer les présences" => Kimia::ROLE_DELETE_PRESENCE,
  231.             "Voir les modèles des factures" => Kimia::ROLE_VIEW_FACTURE,
  232.             "Créer les modèles des factures" => Kimia::ROLE_CREATE_FACTURE,
  233.             "Modifier les modèles des factures" => Kimia::ROLE_EDIT_FACTURE,
  234.             "Supprimer les modèles des factures" => Kimia::ROLE_DELETE_FACTURE,
  235.             "Serveur" => Kimia::ROLE_WAITER,
  236.         ];
  237.     }
  238.     public static function getGroupPermissionList(): array
  239.     {
  240.         return [
  241.             Kimia::GROUP_ROLE_DEPARTMENT => Kimia::GROUP_ROLE_DEPARTMENT_LIST,
  242.             Kimia::GROUP_ROLE_ARTICLE => Kimia::GROUP_ROLE_ARTICLE_LIST,
  243.             Kimia::GROUP_ROLE_COLLECTION => Kimia::GROUP_ROLE_COLLECTION_LIST,
  244.             Kimia::GROUP_ROLE_REDUCTION => Kimia::GROUP_ROLE_REDUCTION_LIST,
  245.             Kimia::GROUP_ROLE_STOCK => Kimia::GROUP_ROLE_STOCK_LIST,
  246.             Kimia::GROUP_ROLE_ORDER => Kimia::GROUP_ROLE_ORDER_LIST,
  247.             Kimia::GROUP_ROLE_PLANNING => Kimia::GROUP_ROLE_PLANNING_LIST,
  248.             Kimia::GROUP_ROLE_EMPLOYEE => Kimia::GROUP_ROLE_EMPLOYEE_LIST,
  249.             Kimia::GROUP_ROLE_CLIENT => Kimia::GROUP_ROLE_CLIENT_LIST,
  250.             Kimia::GROUP_ROLE_PRESENCE => Kimia::GROUP_ROLE_PRESENCE_LIST,
  251.             Kimia::GROUP_ROLE_FACTURE => Kimia::GROUP_ROLE_FACTURE_LIST
  252.         ];
  253.     }
  254.     public function setPermissions(array $permissions): self
  255.     {
  256.         $this->permissions $permissions;
  257.         return $this;
  258.     }
  259.     public function getIsGranted($role)
  260.     {
  261.         if (in_array($role$this->getPermissions())) return true;
  262.         return false;
  263.     }
  264.     public function getIsGrantedGroup($group)
  265.     {
  266.         foreach (self::getGroupPermissionList()[$group] as $r) {
  267.             if (in_array($r$this->getPermissions())) return true;
  268.         }
  269.         return false;
  270.     }
  271.     public function getStatus(): ?string
  272.     {
  273.         return $this->status;
  274.     }
  275.     public function setStatus(string $status): self
  276.     {
  277.         $this->status $status;
  278.         return $this;
  279.     }
  280.     public function getInvitation(): ?Invitation
  281.     {
  282.         return $this->invitation;
  283.     }
  284.     public function setInvitation(?Invitation $invitation): self
  285.     {
  286.         $this->invitation $invitation;
  287.         return $this;
  288.     }
  289.     public function getUser(): ?User
  290.     {
  291.         return $this->user;
  292.     }
  293.     public function setUser(?User $user): self
  294.     {
  295.         $this->user $user;
  296.         return $this;
  297.     }
  298.     public function getDepartment(): ?Department
  299.     {
  300.         return $this->department;
  301.     }
  302.     public function setDepartment(?Department $department): self
  303.     {
  304.         $this->department $department;
  305.         return $this;
  306.     }
  307.     /**
  308.      * @return Collection<int, Attendance>
  309.      */
  310.     public function getAttendances(): Collection
  311.     {
  312.         return $this->attendances;
  313.     }
  314.     public function addAttendance(Attendance $attendance): self
  315.     {
  316.         if (!$this->attendances->contains($attendance)) {
  317.             $this->attendances->add($attendance);
  318.             $attendance->setEmployee($this);
  319.         }
  320.         return $this;
  321.     }
  322.     public function removeAttendance(Attendance $attendance): self
  323.     {
  324.         if ($this->attendances->removeElement($attendance)) {
  325.             if ($attendance->getEmployee() === $this) {
  326.                 $attendance->setEmployee(null);
  327.             }
  328.         }
  329.         return $this;
  330.     }
  331.     /**
  332.      * @return Collection<int, Order>
  333.      */
  334.     public function getOrders(): Collection
  335.     {
  336.         return $this->orders;
  337.     }
  338.     public function addOrder(Order $order): self
  339.     {
  340.         if (!$this->orders->contains($order)) {
  341.             $this->orders->add($order);
  342.             $order->setWaiter($this);
  343.         }
  344.         return $this;
  345.     }
  346.     public function removeOrder(Order $order): self
  347.     {
  348.         if ($this->orders->removeElement($order)) {
  349.             // set the owning side to null (unless already changed)
  350.             if ($order->getWaiter() === $this) {
  351.                 $order->setWaiter(null);
  352.             }
  353.         }
  354.         return $this;
  355.     }
  356.     public function getQuota(): ?int
  357.     {
  358.         return $this->quota;
  359.     }
  360.     public function incrementQuota(int $quota 1): self
  361.     {
  362.         $this->quota += $quota;
  363.         return $this;
  364.     }
  365.     public function decrementQuota(int $quota 1): self
  366.     {
  367.         $this->quota -= $quota;
  368.         return $this;
  369.     }
  370.     public function getQuotaOfDay(): ?int
  371.     {
  372.         $quota 0;
  373.         $now = new DateTimeImmutable();
  374.         /** @var Order $order */
  375.         foreach ($this->orders as $order) {
  376.             if ($order->getCreatedAt()->format('d M Y') === $now->format('d M Y')) {
  377.                 if ($order->getStatus() === Kimia::STATUS_SUCCESS$quota++;
  378.             }
  379.         }
  380.         return $quota;
  381.     }
  382.     public function setQuota(int $quota): self
  383.     {
  384.         $this->quota $quota;
  385.         return $this;
  386.     }
  387.     public function getEstablishment(): ?Establishment
  388.     {
  389.         return $this->establishment;
  390.     }
  391.     public function setEstablishment(?Establishment $establishment): self
  392.     {
  393.         $this->establishment $establishment;
  394.         return $this;
  395.     }
  396.     /**
  397.      * @return Collection<int, FlowUser>
  398.      */
  399.     public function getFlowUsers(): Collection
  400.     {
  401.         return $this->flowUsers;
  402.     }
  403.     public function addFlowUser(FlowUser $flowUser): self
  404.     {
  405.         if (!$this->flowUsers->contains($flowUser)) {
  406.             $this->flowUsers->add($flowUser);
  407.             $flowUser->setUser($this);
  408.         }
  409.         return $this;
  410.     }
  411.     public function removeFlowUser(FlowUser $flowUser): self
  412.     {
  413.         if ($this->flowUsers->removeElement($flowUser)) {
  414.             // set the owning side to null (unless already changed)
  415.             if ($flowUser->getUser() === $this) {
  416.                 $flowUser->setUser(null);
  417.             }
  418.         }
  419.         return $this;
  420.     }
  421.     public function isPaymentQuota(): ?bool
  422.     {
  423.         return $this->paymentQuota;
  424.     }
  425.     public function setPaymentQuota(bool $paymentQuota): self
  426.     {
  427.         $this->paymentQuota $paymentQuota;
  428.         return $this;
  429.     }
  430.     public function getPercentQuota(): ?float
  431.     {
  432.         return $this->percentQuota;
  433.     }
  434.     public function setPercentQuota(?float $percentQuota): self
  435.     {
  436.         $this->percentQuota $percentQuota;
  437.         return $this;
  438.     }
  439.     public function __toString(): string
  440.     {
  441.         return $this->getFullname();
  442.     }
  443.     public function getFunction(): ?Role
  444.     {
  445.         return $this->function;
  446.     }
  447.     public function setFunction(?Role $function): static
  448.     {
  449.         $this->function $function;
  450.         return $this;
  451.     }
  452.     /**
  453.      * @return Collection<int, Document>
  454.      */
  455.     public function getDocuments(): Collection
  456.     {
  457.         return $this->documents;
  458.     }
  459.     public function addDocument(Document $document): static
  460.     {
  461.         if (!$this->documents->contains($document)) {
  462.             $this->documents->add($document);
  463.             $document->setEmployee($this);
  464.         }
  465.         return $this;
  466.     }
  467.     public function removeDocument(Document $document): static
  468.     {
  469.         if ($this->documents->removeElement($document)) {
  470.             // set the owning side to null (unless already changed)
  471.             if ($document->getEmployee() === $this) {
  472.                 $document->setEmployee(null);
  473.             }
  474.         }
  475.         return $this;
  476.     }
  477.     public function getContract(): ?Contract
  478.     {
  479.         return $this->contract;
  480.     }
  481.     public function setContract(?Contract $contract): static
  482.     {
  483.         $this->contract $contract;
  484.         return $this;
  485.     }
  486.     /**
  487.      * @return Collection<int, EmployeeContract>
  488.      */
  489.     public function getContracts(): Collection
  490.     {
  491.         return $this->contracts;
  492.     }
  493.     public function addContract(EmployeeContract $contract): static
  494.     {
  495.         if (!$this->contracts->contains($contract)) {
  496.             $this->contracts->add($contract);
  497.             $contract->setEmployee($this);
  498.         }
  499.         return $this;
  500.     }
  501.     public function removeContract(EmployeeContract $contract): static
  502.     {
  503.         if ($this->contracts->removeElement($contract)) {
  504.             // set the owning side to null (unless already changed)
  505.             if ($contract->getEmployee() === $this) {
  506.                 $contract->setEmployee(null);
  507.             }
  508.         }
  509.         return $this;
  510.     }
  511.     /**
  512.      * @return Collection<int, Address>
  513.      */
  514.     public function getAddresses(): Collection
  515.     {
  516.         return $this->addresses;
  517.     }
  518.     public function addAddress(Address $address): static
  519.     {
  520.         if (!$this->addresses->contains($address)) {
  521.             $this->addresses->add($address);
  522.             $address->setEmployee($this);
  523.         }
  524.         return $this;
  525.     }
  526.     public function removeAddress(Address $address): static
  527.     {
  528.         if ($this->addresses->removeElement($address)) {
  529.             // set the owning side to null (unless already changed)
  530.             if ($address->getEmployee() === $this) {
  531.                 $address->setEmployee(null);
  532.             }
  533.         }
  534.         return $this;
  535.     }
  536.     /**
  537.      * @return Collection<int, Card>
  538.      */
  539.     public function getCards(): Collection
  540.     {
  541.         return $this->cards;
  542.     }
  543.     public function addCard(Card $card): static
  544.     {
  545.         if (!$this->cards->contains($card)) {
  546.             $this->cards->add($card);
  547.             $card->setEmployee($this);
  548.         }
  549.         return $this;
  550.     }
  551.     public function removeCard(Card $card): static
  552.     {
  553.         if ($this->cards->removeElement($card)) {
  554.             // set the owning side to null (unless already changed)
  555.             if ($card->getEmployee() === $this) {
  556.                 $card->setEmployee(null);
  557.             }
  558.         }
  559.         return $this;
  560.     }
  561.     /**
  562.      * @return Collection<int, Mutuality>
  563.      */
  564.     public function getMutualities(): Collection
  565.     {
  566.         return $this->mutualities;
  567.     }
  568.     public function addMutuality(Mutuality $mutuality): static
  569.     {
  570.         if (!$this->mutualities->contains($mutuality)) {
  571.             $this->mutualities->add($mutuality);
  572.             $mutuality->setEmployee($this);
  573.         }
  574.         return $this;
  575.     }
  576.     public function removeMutuality(Mutuality $mutuality): static
  577.     {
  578.         if ($this->mutualities->removeElement($mutuality)) {
  579.             // set the owning side to null (unless already changed)
  580.             if ($mutuality->getEmployee() === $this) {
  581.                 $mutuality->setEmployee(null);
  582.             }
  583.         }
  584.         return $this;
  585.     }
  586. }