<?php
namespace App\Entity;
use Symfony\Component\Serializer\Annotation\Groups;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: 'App\Repository\DepartmentRepository')]
class Department
{
#[Groups(['LogService'])]
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[Groups(['LogService', 'APIJob'])]
#[ORM\Column(type: 'string', length: 32)]
private $name;
#[ORM\ManyToOne(targetEntity: 'App\Entity\Company', inversedBy: 'departments')]
#[ORM\JoinColumn(nullable: false)]
private $company;
#[Groups(['LogService'])]
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $description;
#[ORM\OneToMany(targetEntity: 'App\Entity\Organization', mappedBy: 'department')]
private $organizations;
#[ORM\ManyToMany(targetEntity: Training::class, mappedBy: 'departments')]
private $trainings;
#[ORM\ManyToMany(targetEntity: Announcement::class, mappedBy: 'departments')]
private $announcements;
#[ORM\OneToMany(targetEntity: Job::class, mappedBy: 'department')]
private $jobs;
#[ORM\OneToMany(targetEntity: AppraisalForm::class, mappedBy: 'department')]
private $appraisalForms;
#[ORM\OneToMany(targetEntity: User::class, mappedBy: 'department')]
private $users;
#[ORM\OneToMany(targetEntity: TaskType::class, mappedBy: 'department')]
private $taskTypes;
#[ORM\OneToMany(targetEntity: UserRole::class, mappedBy: 'department')]
private $userRoles;
#[ORM\ManyToMany(targetEntity: Course::class, mappedBy: 'departments')]
private $courses;
#[ORM\ManyToMany(targetEntity: CourseAutoEnrollment::class, mappedBy: 'departments')]
private $courseAutoEnrollments;
#[ORM\OneToMany(targetEntity: XeroLineItem::class, mappedBy: 'department')]
#[ORM\JoinColumn(name: 'invoice_line_item_id', referencedColumnName: 'id', nullable: false)]
private $xeroLineItems;
#[ORM\OneToMany(mappedBy: 'department', targetEntity: Checklist::class)]
private Collection $checklists;
#[ORM\OneToMany(mappedBy: 'Department', targetEntity: ChatbotSectionDepartment::class, orphanRemoval: true)]
private Collection $chatbotSectionDepartments;
#[ORM\OneToMany(mappedBy: 'department', targetEntity: ChatbotDepartmentPrompt::class, orphanRemoval: true)]
private Collection $chatbotDepartmentPrompts;
#[ORM\OneToMany(mappedBy: 'department', targetEntity: DepartmentOverview::class, orphanRemoval: true)]
private Collection $departmentOverviews;
#[ORM\ManyToMany(targetEntity: Vendor::class, mappedBy: 'departmentInCharge')]
private Collection $vendors;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true)]
private ?User $leader = null;
public function __construct()
{
// $this->user = new ArrayCollection();
$this->organizations = new ArrayCollection();
$this->trainings = new ArrayCollection();
$this->announcements = new ArrayCollection();
$this->jobs = new ArrayCollection();
$this->appraisalForms = new ArrayCollection();
$this->users = new ArrayCollection();
$this->taskTypes = new ArrayCollection();
$this->userRoles = new ArrayCollection();
$this->courses = new ArrayCollection();
$this->courseAutoEnrollments = new ArrayCollection();
$this->xeroLineItems = new ArrayCollection();
$this->checklists = new ArrayCollection();
$this->chatbotSectionDepartments = new ArrayCollection();
$this->chatbotDepartmentPrompts = new ArrayCollection();
$this->departmentOverviews = new ArrayCollection();
$this->vendors = 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 getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
/**
* @return Collection|Organization[]
*/
public function getOrganizations(): Collection
{
return $this->organizations;
}
public function addOrganization(Organization $organization): self
{
if (!$this->organizations->contains($organization)) {
$this->organizations[] = $organization;
$organization->setDepartment($this);
}
return $this;
}
public function removeOrganization(Organization $organization): self
{
if ($this->organizations->contains($organization)) {
$this->organizations->removeElement($organization);
// set the owning side to null (unless already changed)
if ($organization->getDepartment() === $this) {
$organization->setDepartment(null);
}
}
return $this;
}
public function getOrganizationMembers()
{
$users = [];
foreach ($this->getOrganizations() as $user) {
if ($user->getDepartment() == $this) array_push($users, $user);
};
return $users;
}
public function getOrganizationUsers($isActive = true)
{
$users = [];
$registered_users = [];
foreach ($this->getOrganizations() as $user) {
$uid = $user->getUser() ? $user->getUser()->getId() : false;
if ($uid != null && in_array($uid, $registered_users) == false) {
if ($user->getUser()->getIsActive() == $isActive) {
array_push($users, $user);
array_push($registered_users, $uid);
};
}
};
return $users;
}
public function getTotalActiveUser()
{
$users = $this->getUsers();
$total = 0;
foreach ($users as $user) {
if ($user->getIsActive()) {
$total++;
}
}
return $total;
}
/**
* @return Collection|Training[]
*/
public function getTrainings(): Collection
{
return $this->trainings;
}
public function addTraining(Training $training): self
{
if (!$this->trainings->contains($training)) {
$this->trainings[] = $training;
$training->addDepartments($this);
}
return $this;
}
public function removeTraining(Training $training): self
{
if ($this->trainings->contains($training)) {
$this->trainings->removeElement($training);
$training->removeDepartments($this);
}
return $this;
}
/**
* @return Collection|Announcement[]
*/
public function getAnnouncements(): Collection
{
return $this->announcements;
}
public function addAnnouncement(Announcement $announcement): self
{
if (!$this->announcements->contains($announcement)) {
$this->announcements[] = $announcement;
$announcement->addDepartment($this);
}
return $this;
}
public function removeAnnouncement(Announcement $announcement): self
{
if ($this->announcements->contains($announcement)) {
$this->announcements->removeElement($announcement);
$announcement->removeDepartment($this);
}
return $this;
}
/**
* @return Collection|Job[]
*/
public function getJobs(): Collection
{
return $this->jobs;
}
public function addJob(Job $job): self
{
if (!$this->jobs->contains($job)) {
$this->jobs[] = $job;
$job->setDepartment($this);
}
return $this;
}
public function removeJob(Job $job): self
{
if ($this->jobs->removeElement($job)) {
// set the owning side to null (unless already changed)
if ($job->getDepartment() === $this) {
$job->setDepartment(null);
}
}
return $this;
}
/**
* @return Collection|AppraisalForm[]
*/
public function getAppraisalForms(): Collection
{
return $this->appraisalForms;
}
public function addAppraisalForm(AppraisalForm $appraisalForm): self
{
if (!$this->appraisalForms->contains($appraisalForm)) {
$this->appraisalForms[] = $appraisalForm;
$appraisalForm->setDepartment($this);
}
return $this;
}
public function removeAppraisalForm(AppraisalForm $appraisalForm): self
{
if ($this->appraisalForms->removeElement($appraisalForm)) {
// set the owning side to null (unless already changed)
if ($appraisalForm->getDepartment() === $this) {
$appraisalForm->setDepartment(null);
}
}
return $this;
}
/**
* @return Collection<int, User>
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->setDepartment($this);
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->removeElement($user)) {
// set the owning side to null (unless already changed)
if ($user->getDepartment() === $this) {
$user->setDepartment(null);
}
}
return $this;
}
/**
* @return Collection<int, TaskType>
*/
public function getTaskTypes(): Collection
{
return $this->taskTypes;
}
public function addTaskType(TaskType $taskType): self
{
if (!$this->taskTypes->contains($taskType)) {
$this->taskTypes[] = $taskType;
$taskType->setDepartment($this);
}
return $this;
}
public function removeTaskType(TaskType $taskType): self
{
if ($this->taskTypes->removeElement($taskType)) {
// set the owning side to null (unless already changed)
if ($taskType->getDepartment() === $this) {
$taskType->setDepartment(null);
}
}
return $this;
}
/**
* @return Collection|UserRole[]
*/
public function getUserRoles(): Collection
{
return $this->userRoles;
}
public function addUserRole(UserRole $userRole): self
{
if (!$this->userRoles->contains($userRole)) {
$this->userRoles[] = $userRole;
$userRole->setDepartment($this);
}
return $this;
}
public function removeUserRole(UserRole $userRole): self
{
if ($this->userRoles->removeElement($userRole)) {
// set the owning side to null (unless already changed)
if ($userRole->getDepartment() === $this) {
$userRole->setDepartment(null);
}
}
return $this;
}
/**
* @return Collection|Course[]
*/
public function getCourses(): Collection
{
return $this->courses;
}
public function addCourse(Course $course): self
{
if (!$this->courses->contains($course)) {
$this->courses[] = $course;
$course->addDepartment($this);
}
return $this;
}
public function removeCourse(Course $course): self
{
if ($this->courses->removeElement($course)) {
$course->removeDepartment($this);
}
return $this;
}
/**
* @return Collection|CourseAutoEnrollment[]
*/
public function getCourseAutoEnrollments(): Collection
{
return $this->courseAutoEnrollments;
}
public function addCourseAutoEnrollment(CourseAutoEnrollment $CourseAutoEnrollment): self
{
if (!$this->courseAutoEnrollments->contains($CourseAutoEnrollment)) {
$this->courseAutoEnrollments[] = $CourseAutoEnrollment;
$CourseAutoEnrollment->addDepartment($this);
}
return $this;
}
public function removeCourseAutoEnrollment(CourseAutoEnrollment $CourseAutoEnrollment): self
{
if ($this->courseAutoEnrollments->removeElement($CourseAutoEnrollment)) {
$CourseAutoEnrollment->removeDepartment($this);
}
return $this;
}
/**
* @return Collection|XeroLineItem[]
*/
public function getXeroLineItems(): Collection
{
return $this->xeroLineItems;
}
public function addXeroLineItem(XeroLineItem $xeroLineItem): self
{
if (!$this->xeroLineItems->contains($xeroLineItem)) {
$this->xeroLineItems[] = $xeroLineItem;
$xeroLineItem->setDepartment($this);
}
return $this;
}
public function removeXeroLineItem(XeroLineItem $xeroLineItem): self
{
if ($this->xeroLineItems->removeElement($xeroLineItem)) {
// set the owning side to null (unless already changed)
if ($xeroLineItem->getDepartment() === $this) {
$xeroLineItem->setDepartment(null);
}
}
return $this;
}
public function getAllManager($sort = true, $includeSub = false)
{
$managers = [];
foreach ($this->getUsers() as $user) {
if ($user->getIsActive() == true && $user->getIsManager($includeSub)) array_push($managers, $user);
};
if ($sort) {
usort($managers, function ($a, $b) {
return strcmp($a->getPersonalInfo()->getFirstName(), $b->getPersonalInfo()->getFirstName());
});
};
return $managers;
}
public function getAllSubManager($sort = true)
{
$managers = [];
foreach ($this->getUsers() as $user) {
if ($user->getIsActive() && $subManagers = $user->getAllManager($user->getManager())) {
$managers = array_merge($managers, $subManagers);
}
}
if ($sort) {
usort($managers, function ($a, $b) {
return strcmp($a->getPersonalInfo()->getFirstName(), $b->getPersonalInfo()->getFirstName());
});
}
return $managers;
}
/**
* @return Collection<int, Checklist>
*/
public function getChecklists(): Collection
{
return $this->checklists;
}
public function addChecklist(Checklist $checklist): self
{
if (!$this->checklists->contains($checklist)) {
$this->checklists->add($checklist);
$checklist->setDepartment($this);
}
return $this;
}
public function removeChecklist(Checklist $checklist): self
{
if ($this->checklists->removeElement($checklist)) {
// set the owning side to null (unless already changed)
if ($checklist->getDepartment() === $this) {
$checklist->setDepartment(null);
}
}
return $this;
}
/**
* @return Collection<int, ChatbotSectionDepartment>
*/
public function getChatbotSectionDepartments(): Collection
{
return $this->chatbotSectionDepartments;
}
public function addChatbotSectionDepartment(ChatbotSectionDepartment $chatbotSectionDepartment): self
{
if (!$this->chatbotSectionDepartments->contains($chatbotSectionDepartment)) {
$this->chatbotSectionDepartments->add($chatbotSectionDepartment);
$chatbotSectionDepartment->setDepartment($this);
}
return $this;
}
public function removeChatbotSectionDepartment(ChatbotSectionDepartment $chatbotSectionDepartment): self
{
if ($this->chatbotSectionDepartments->removeElement($chatbotSectionDepartment)) {
// set the owning side to null (unless already changed)
if ($chatbotSectionDepartment->getDepartment() === $this) {
$chatbotSectionDepartment->setDepartment(null);
}
}
return $this;
}
/**
* @return Collection<int, ChatbotDepartmentPrompt>
*/
public function getChatbotDepartmentPrompts(): Collection
{
return $this->chatbotDepartmentPrompts;
}
public function addChatbotDepartmentPrompt(ChatbotDepartmentPrompt $chatbotDepartmentPrompt): self
{
if (!$this->chatbotDepartmentPrompts->contains($chatbotDepartmentPrompt)) {
$this->chatbotDepartmentPrompts->add($chatbotDepartmentPrompt);
$chatbotDepartmentPrompt->setDepartment($this);
}
return $this;
}
public function removeChatbotDepartmentPrompt(ChatbotDepartmentPrompt $chatbotDepartmentPrompt): self
{
if ($this->chatbotDepartmentPrompts->removeElement($chatbotDepartmentPrompt)) {
// set the owning side to null (unless already changed)
if ($chatbotDepartmentPrompt->getDepartment() === $this) {
$chatbotDepartmentPrompt->setDepartment(null);
}
}
return $this;
}
/**
* @return Collection<int, DepartmentOverview>
*/
public function getDepartmentOverviews(): Collection
{
return $this->departmentOverviews;
}
public function addDepartmentOverview(DepartmentOverview $departmentOverview): self
{
if (!$this->departmentOverviews->contains($departmentOverview)) {
$this->departmentOverviews->add($departmentOverview);
$departmentOverview->setDepartment($this);
}
return $this;
}
public function removeDepartmentOverview(DepartmentOverview $departmentOverview): self
{
if ($this->departmentOverviews->removeElement($departmentOverview)) {
// set the owning side to null (unless already changed)
if ($departmentOverview->getDepartment() === $this) {
$departmentOverview->setDepartment(null);
}
}
return $this;
}
/**
* @return Collection<int, Vendor>
*/
public function getVendors(): Collection
{
return $this->vendors;
}
public function addVendor(Vendor $vendor): self
{
if (!$this->vendors->contains($vendor)) {
$this->vendors->add($vendor);
$vendor->addDepartmentInCharge($this);
}
return $this;
}
public function removeVendor(Vendor $vendor): self
{
if ($this->vendors->removeElement($vendor)) {
$vendor->removeDepartmentInCharge($this);
}
return $this;
}
public function getLeader(): ?User
{
return $this->leader;
}
public function setLeader(?User $leader): self
{
$this->leader = $leader;
return $this;
}
public function getPseudoLeader()
{
foreach ($this->users as $user) {
if ($user->getIsActive() == true && (in_array('ROLE_TEAM_LEADER', $user->getRoles()) || $user->getIsManager()) && (in_array('ROLE_OWNER', $user->getRoles()) || ($user->getManager() && in_array('ROLE_MANAGEMENT', $user->getManager()->getRoles())))) {
return $user;
}
};
if ($this->getTotalActiveUser() == 1) {
foreach ($this->users as $user) {
if ($user->getIsActive() == true) {
return $user;
}
};
}
}
}