<?php
namespace App\Entity;
use App\Repository\TimesheetStatusRepository;
use App\Repository\WorkerRepository;
use function Doctrine\Common\Cache\Psr6\get;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping as ORM;
use phpDocumentor\Reflection\Types\This;
use Monolog\DateTimeImmutable;
use PhpParser\ErrorHandler\Collecting;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
use Symfony\Component\Validator\Constraints\DateTime;
use Symfony\Component\Serializer\Annotation\MaxDepth;
use Symfony\Component\Serializer\Annotation\Groups;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\UniqueConstraint;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Doctrine\Common\Collections\Criteria;
/**
* ORM\Entity(repositoryClass=WorkerRepository::class)
*/
/**
* @ORM\HasLifecycleCallbacks
* @ORM\Entity(repositoryClass=WorkerRepository::class)
* @ORM\Table(
* name="worker",
* uniqueConstraints={
* @ORM\UniqueConstraint(columns={"name", "surname", "birthday_at"}),
* @ORM\UniqueConstraint(name="unique_phone_number", columns={"phone_number"}, options={"where": "(phone_number IS NOT NULL)"}),
* },
* )
* UniqueEntity(
* fields={"name", "surname", "birthday_at"},
* message="League for given country already exists in database."
* )
*/
class Worker
{
// Same Process processed via Entity Subscriber
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({
* "worker.manage.base",
* "customer.smart.active.employees",
* "timesheet.for.stakeholder",
* "worker.manage.base.timesheet",
* "customer.selected.project",
*
* "project.details.orders",
* "order.detail",
* "order.lead",
*
* "timesheet.collected.base",
*
* "worker.base",
* "worker.core", "worker@core",
*
* "worker.activity.substitution.worker.activity.worker.core"
* })
*/
private $id;
/**
* TODO replace base with core
* @ORM\Column(type="string", length=255)
* @Groups({
* "worker.manage.base",
* "customer.joined.projects.assigned.team.leaders",
* "customer.manage.base.team.lead",
* "customer.smart.active.employees",
* "timesheet.for.stakeholder",
* "timesheet.for.customer",
* "customer.selected.project",
*
* "project.details.orders",
* "order.detail",
* "order.lead",
*
* "timesheet.collected.base",
*
* "worker.base",
* "worker.core", "worker@core",
*
* "worker.substitution.core"
* })
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"worker.manage.base",
* "customer.joined.projects.assigned.team.leaders",
* "customer.manage.base.team.lead",
* "customer.smart.active.employees",
* "timesheet.for.stakeholder",
* "timesheet.for.customer",
* "customer.selected.project",
*
* "project.details.orders",
* "order.detail",
*
* "order.lead",
*
* "timesheet.collected.base",
*
* "worker.base",
* "worker.core", "worker@core",
*
* "worker.substitution.core"
* })
*/
private $surname;
/**
* @Groups({
* "customer.joined.projects.assigned.team.leaders",
* "customer.manage.base.team.lead",
* "customer.smart.active.employees",
* "worker.manage.base",
* "customer.selected.project",
*
* "project.details.orders",
* "order.detail",
* "order.lead",
* "worker.substitution.core",
*
*
* "worker.core", "worker@core"
*
* })
*/
private $full_name;
/**
* @deprecated use in WorkerActivities
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $entry_at;
/**
* @deprecated use in WorkerActivities
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $exit_at;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
* @Groups({
* "worker.manage.base",
* "worker.core", "worker@core"
* })
*/
private $birthday_at;
/**
* @ORM\Column(type="string", length=255)
* @Gedmo\Slug(fields={"name", "surname"})
* @Groups({
* "worker.manage.base",
* "customer.smart.active.employees",
* "worker.core", "worker@core"
* })
*/
private $slug;
/**
* @ORM\ManyToMany(targetEntity=Qualification::class, inversedBy="workers", fetch="EXTRA_LAZY")
* @Groups({"worker.manage.base"})
*/
private $qualifications;
/**
* @deprecated use in WorkerActivities
* @ORM\OneToMany(targetEntity=WorkerInProject::class, mappedBy="worker", orphanRemoval=true, fetch="EXTRA_LAZY")
* @ORM\OrderBy({"id":"DESC"})
* Groups({"worker.manage.base", "worker.manage.joined.projects"})
*/
private $worker_in_projects;
/**
* @ORM\OneToMany(targetEntity=WorkerActivities::class, mappedBy="worker", cascade={"persist", "remove"}, orphanRemoval=true, fetch="EXTRA_LAZY" )
* @ORM\OrderBy({"id":"DESC"})
* @Groups({
* "worker.manage.base",
* "worker.actually.activity",
* "worker.activities",
*
* "worker@activities"
*
* })
*
*/
private $worker_activities;
/**
* @deprecated use latest WorkerActivities
* Groups({"worker.actually.activity"})
*/
private $worker_actually_activity;
/**
* @ORM\OneToMany(targetEntity=WorkerCertificates::class, mappedBy="worker", cascade={"persist", "remove"}, orphanRemoval=true, fetch="EXTRA_LAZY" )
* @Groups({"worker.manage.base"})
*/
private $worker_certificates;
/**
* @deprecated use inside WorkerActivities
* @ORM\OneToMany(targetEntity=WorkerTimesheet::class, mappedBy="worker", cascade={"persist"}, cascade={"persist", "remove"}, orphanRemoval=true, fetch="EXTRA_LAZY")
* Groups({
* "worker.manage.base.timesheet",
* "timesheet.for.stakeholder",
* "timesheet.for.customer"
* })
*/
private $worker_timesheets;
/**
* @ORM\Column(type="decimal", precision=4, scale=2, nullable=true)
* @Groups({"worker.manage.base"})
*/
private $daily_work_time;
/**
* @ORM\Column(type="decimal", precision=7, scale=2, nullable=true)
* @Groups({"worker.manage.base", "worker.manage.selected.worker.in.project", "worker.manage.joined.projects"})
*/
private $cost_per_hour;
/**
* @ORM\Column(type="string", length=64, nullable=true, unique=false )
* @Groups({"worker.manage.base"})
*/
private $personal_id;
// Additional param
private array $total_worked_time;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $created_at;
/**
* @ORM\Column(type="string", length=24 , nullable=true )
* @Groups({"worker.manage.base"})
*/
private $phone_number;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"worker.manage.base"})
*/
private $uiid;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"worker.manage.base", "customer.smart.active.employees"})
*/
private $profile_image;
/**
* ____________________________________________________________________________________________________
* Event Listener Prop
* This prop Update during event.listener
* @Groups({"worker.manage.base", "customer.smart.active.employees"})
*/
private $profile_image_full_path;
/**
* TODO bu ozellik EventListernes yada Subscriber uzerinden yapilacak
* Event Listener Prop
* This prop Update during event.listener
* @param string $profile_image_full_path
* @return Worker|null
*/
public function setProfileImageFullPath( string $profile_image_full_path ): ?self {
$this->profile_image_full_path = $profile_image_full_path;// $this->getServiceArgumentFileDirs()['fetch'] . DIRECTORY_SEPARATOR . $this->getProfileImage();
return $this;
}
/**
* Event Listener Prop
*/
public function getProfileImageFullPath(): ?string { return $this->profile_image_full_path; /* $this->getServiceArgumentFileDirs()['fetch'] . DIRECTORY_SEPARATOR . $this->getProfileImage(); */ }
// ____________________________________________________________________________________________________
/**
* @ORM\OneToMany(targetEntity=UserAppSettings::class, mappedBy="worker")
*/
private $user_app_settings;
/**
* @ORM\ManyToMany(targetEntity=AccessRoles::class, inversedBy="workers")
*/
private $worker_accessed_roles;
/**
* TODO islemler bittikten sonra cikar
* TODO this should be in WorkerActivities
* @ORM\OneToMany(targetEntity=ProjectOrders::class, mappedBy="order_lead")
*/
private $projectOrders;
/**
* TODO thisi deprecated !!!!
* @deprecated use inside WorkerActivities
* @ORM\OneToOne(targetEntity=User::class, inversedBy="worker", cascade={"persist", "remove"})
* @Groups({
* "worker.user",
* "worker_activity@user"
* })
*/
private $user;
/**
* @Groups({
* "worker.latest.activity",
* "worker@latest_activity"
* })
}
*/
private $latest_activity;
/**
* @return mixed
*/
public function getLatestActivity()
{
$criteria = Criteria::create();
$criteria->orderBy(['created_at' => Criteria::DESC])->setMaxResults(1);
/**@var WorkerActivities $latestActivity*/
$latestActivity = $this->getWorkerActivities()->matching($criteria)->first();
$this->latest_activity = $latestActivity ?? null;
return $this->latest_activity;
}
/**
* @return mixed
*/
public function getTotalWorkedTime(): array
{
return $this->total_worked_time;
}
/**
* @param mixed $total_worked_time
*/
public function setTotalWorkedTime( \DateInterval $total_worked_time ): void
{
$this->total_worked_time = [
"days" => $total_worked_time->days,
"month" => $total_worked_time->m,
"year" => $total_worked_time->y,
];
}
/**
* @return mixed
*/
public function getFullName()
{
return $this->full_name ?? $this->name . " " . $this->surname;
}
/**
* @param mixed $full_name
*/
public function setFullName($full_name): void
{
$this->full_name = $full_name;
}
public function __construct()
{
$this->qualifications = new ArrayCollection();
$this->projects = new ArrayCollection();
$this->worker_in_projects = new ArrayCollection();
$this->worker_activities = new ArrayCollection();
$this->worker_certificates = new ArrayCollection();
$this->worker_timesheets = new ArrayCollection();
$this->user_app_settings = new ArrayCollection();
$this->role = new ArrayCollection();
$this->worker_accessed_roles = new ArrayCollection();
// TODO this should be in WorkerActivities
$this->projectOrders = new ArrayCollection();
// $this->ordersWithDelegatedAccess = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getSurname(): ?string
{
return $this->surname;
}
public function setSurname(string $surname): self
{
$this->surname = $surname;
return $this;
}
public function getEntryAt(): ?\DateTimeImmutable
{
return $this->entry_at;
}
public function setEntryAt(?\DateTimeImmutable $entry_at): self
{
$this->entry_at = $entry_at;
return $this;
}
public function getExitAt(): ?\DateTimeImmutable
{
return $this->exit_at;
}
public function setExitAt(?\DateTimeImmutable $exit_at): self
{
$this->exit_at = $exit_at;
return $this;
}
public function getBirthdayAt(): ?\DateTimeImmutable
{
return $this->birthday_at;
// return (new \DateTimeImmutable($this->birthday_at))->format('d.m.Y');
}
public function setBirthdayAt(?\DateTimeImmutable $birthday_at): self
{
$this->birthday_at = $birthday_at;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
/**
* @return Collection<int, Qualification>
*/
public function getQualifications(): ?Collection
{
return $this->qualifications;
}
public function addQualification(Qualification $qualification): self
{
if (!$this->qualifications->contains($qualification)) {
$this->qualifications[] = $qualification;
}
return $this;
}
public function removeQualification(Qualification $qualification): self
{
$this->qualifications->removeElement($qualification);
return $this;
}
/**
* @return Collection<int, WorkerInProject>
*/
public function getWorkerInProjects(): ?Collection
{
return $this->worker_in_projects;
}
public function addWorkerInProject(WorkerInProject $workerInProject): self
{
if (!$this->worker_in_projects->contains($workerInProject)) {
$this->worker_in_projects[] = $workerInProject;
$workerInProject->setWorker($this);
}
return $this;
}
public function removeWorkerInProject(WorkerInProject $workerInProject): self
{
if ($this->worker_in_projects->removeElement($workerInProject)) {
// set the owning side to null (unless already changed)
if ($workerInProject->getWorker() === $this) {
$workerInProject->setWorker(null);
}
}
return $this;
}
/**
* @return Collection<int, WorkerActivities>
*/
public function getWorkerActivities(): Collection
{
return $this->worker_activities;
}
/**
* @return Collection<int, WorkerActivities>
*/
public function getWorkerActuallyCustomer(): ?Customer
{
#dd($this->getWorkerActuallyActivity()['entity']->getCustomer());
return $this->getWorkerActuallyActivity() ? $this->getWorkerActuallyActivity()['entity']->getCustomer() : null;
}
public function setWorkerActuallyActivity( WorkerActivities $workerActivity ): void
{
$this->worker_actually_activity = $workerActivity;
}
public function getWorkerActuallyActivity()
{
// return $this->worker_actually_activity;
// $arr = $this->getWorkerActivities()->filter( function ( /**@var $workerActivity WorkerActivities*/ $workerActivity ){
// return is_null($workerActivity->getExitAt());
// })->toArray();
$simplified = $this->getWorkerActivities()->map( function ( /**@var $workerActivity WorkerActivities*/ $workerActivity ){
return [
"id" => $workerActivity->getId(),
"entry_at" => $workerActivity->getEntryAt(),
"exit_at" => $workerActivity->getExitAt(),
"created_at" => $workerActivity->getCreatedAt(),
"entity" => $workerActivity
];
})->toArray();
usort($simplified, fn($a, $b) => $b['created_at'] <=> $a['created_at']); // ID'ye göre azalan sıralama
return $simplified[0] ?? null; // ID'si en büyük olan
}
public function addWorkerActivity(WorkerActivities $workerActivity): self
{
if (!$this->worker_activities->contains($workerActivity)) {
$this->worker_activities[] = $workerActivity;
$workerActivity->setWorker($this);
}
return $this;
}
public function removeWorkerActivity(WorkerActivities $workerActivity): self
{
if ($this->worker_activities->removeElement($workerActivity)) {
// set the owning side to null (unless already changed)
if ($workerActivity->getWorker() === $this) {
$workerActivity->setWorker(null);
}
}
return $this;
}
/**
* @return Collection<int, WorkerCertificates>
*/
public function getWorkerCertificates(): Collection
{
return $this->worker_certificates;
}
public function addWorkerCertificate(WorkerCertificates $workerCertificate): self
{
if (!$this->worker_certificates->contains($workerCertificate)) {
$this->worker_certificates[] = $workerCertificate;
$workerCertificate->setWorker($this);
}
return $this;
}
public function removeWorkerCertificate(WorkerCertificates $workerCertificate): self
{
if ($this->worker_certificates->removeElement($workerCertificate)) {
// set the owning side to null (unless already changed)
if ($workerCertificate->getWorker() === $this) {
$workerCertificate->setWorker(null);
}
}
return $this;
}
/**
* @return Collection<int, WorkerTimesheet>
*/
public function getWorkerTimesheets(): Collection
{
return $this->worker_timesheets;
}
public function addWorkerTimesheet(WorkerTimesheet $workerTimesheet): self
{
if (!$this->worker_timesheets->contains($workerTimesheet)) {
$this->worker_timesheets[] = $workerTimesheet;
$workerTimesheet->setWorker($this);
}
return $this;
}
public function removeWorkerTimesheet(WorkerTimesheet $workerTimesheet): self
{
if ($this->worker_timesheets->removeElement($workerTimesheet)) {
// set the owning side to null (unless already changed)
if ($workerTimesheet->getWorker() === $this) {
$workerTimesheet->setWorker(null);
}
}
return $this;
}
public function getCostPerHour(): ?float
{
return $this->cost_per_hour;
}
public function setCostPerHour(?float $cost_per_hour): self
{
$this->cost_per_hour = $cost_per_hour;
return $this;
}
public function getDailyWorkTime(): ?float
{
return $this->daily_work_time;
}
public function setDailyWorkTime(?float $daily_work_time): self
{
$this->daily_work_time = $daily_work_time;
return $this;
}
public function getPersonalId(): ?string
{
return $this->personal_id;
}
public function setPersonalId(?string $personal_id): self
{
$this->personal_id = $personal_id;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->created_at;
}
public function setCreatedAt(\DateTimeImmutable $created_at): self
{
$this->created_at = $created_at;
return $this;
}
public function getPhoneNumber(): ?string
{
return $this->phone_number;
}
public function setPhoneNumber(?string $phone_number): self
{
$this->phone_number = $phone_number;
return $this;
}
public function getUiid(): ?string
{
return $this->uiid;
}
public function setUiid(?string $uiid): self
{
$this->uiid = $uiid;
return $this;
}
public function getProfileImage(): ?string
{
return $this->profile_image;
}
public function setProfileImage(?string $profile_image): self
{
$this->profile_image = $profile_image;
return $this;
}
/**
* @return Collection<int, UserAppSettings>
*/
public function getUserAppSettings(): Collection
{
return $this->user_app_settings;
}
public function addUserAppSetting(UserAppSettings $userAppSetting): self
{
if (!$this->user_app_settings->contains($userAppSetting)) {
$this->user_app_settings[] = $userAppSetting;
$userAppSetting->setWorker($this);
}
return $this;
}
public function removeUserAppSetting(UserAppSettings $userAppSetting): self
{
if ($this->user_app_settings->removeElement($userAppSetting)) {
// set the owning side to null (unless already changed)
if ($userAppSetting->getWorker() === $this) {
$userAppSetting->setWorker(null);
}
}
return $this;
}
/**
* @return Collection<int, AccessRoles>
*/
public function getWorkerAccessedRoles(): Collection
{
return $this->worker_accessed_roles;
}
public function addWorkerAccessedRole(AccessRoles $workerAccessedRole): self
{
if (!$this->worker_accessed_roles->contains($workerAccessedRole)) {
$this->worker_accessed_roles[] = $workerAccessedRole;
}
return $this;
}
public function removeWorkerAccessedRole(AccessRoles $workerAccessedRole): self
{
$this->worker_accessed_roles->removeElement($workerAccessedRole);
return $this;
}
/**
* // TODO this should be in WorkerActivities
* @return Collection<int, ProjectOrders>
*/
public function getProjectOrders(): Collection
{
return $this->projectOrders;
}
// TODO this should be in WorkerActivities
public function addProjectOrder(ProjectOrders $projectOrder): self
{
if (!$this->projectOrders->contains($projectOrder)) {
$this->projectOrders[] = $projectOrder;
$projectOrder->setOrderLead($this);
}
return $this;
}
// TODO this should be in WorkerActivities
public function removeProjectOrder(ProjectOrders $projectOrder): self
{
if ($this->projectOrders->removeElement($projectOrder)) {
// set the owning side to null (unless already changed)
if ($projectOrder->getOrderLead() === $this) {
$projectOrder->setOrderLead(null);
}
}
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
}