Files
myep-team/src/Entity/Assignment.php
T

535 lines
14 KiB
PHP

<?php
namespace App\Entity;
use App\Entity\Traits\BlameableEntity;
use App\Entity\Traits\SoftDeletableEntity;
use App\Entity\Traits\TimestampableEntity;
use App\Repository\AssignmentRepository;
use Carbon\CarbonPeriod;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: AssignmentRepository::class)]
class Assignment implements BlameableEntityInterface, TimestampableEntityInterface, SoftDeletableEntityInterface
{
use BlameableEntity;
use TimestampableEntity;
use SoftDeletableEntity;
public const STATUS_DRAFT = 'draft';
public const STATUS_PUBLISHED = 'published';
public const STATUS_STAFFED = 'staffed';
public const STATUS_PARTLY_STAFFED = 'partly_staffed';
public const STATUS_STAFFING = 'staffing';
public const STATUS_UNSTAFFED = 'unstaffed';
public const STATUS_DELETED = 'deleted';
public const STATUS_CALLED_OFF = 'called_off';
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(type: 'string', length: 36, unique: true)]
private string $uuid;
#[ORM\ManyToOne]
#[Assert\NotNull(message: 'Bitte gib die Destination an')]
private ?Destination $destination = null;
#[ORM\ManyToOne]
#[Assert\NotNull(message: 'Bitte gib das Jobprofil an')]
private ?JobProfile $jobProfile = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $jobProfileInfo = null;
#[ORM\Column(nullable: true)]
#[Assert\Range(min: 1, minMessage: 'Die Mindestanzahl ist 1')]
private ?int $availableDispositions = 1;
#[ORM\Column]
private ?bool $showAvailableDispositions = false;
#[ORM\Column(type: Types::DATE_IMMUTABLE, nullable: true)]
private ?\DateTimeImmutable $dateFrom = null;
#[ORM\Column(type: Types::DATE_IMMUTABLE, nullable: true)]
#[Assert\GreaterThan(propertyPath: 'dateFrom', message: 'Das Enddatum muss nach dem Beginndatum liegen')]
private ?\DateTimeImmutable $dateTo = null;
#[ORM\Column(type: Types::DATE_IMMUTABLE, nullable: true)]
private ?\DateTimeImmutable $pickupDate = null;
#[ORM\Column(nullable: true)]
private ?int $pickup = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $benefits;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $remarks = null;
#[ORM\ManyToMany(targetEntity: Fee::class)]
private Collection $fees;
#[ORM\OneToMany(mappedBy: 'assignment', targetEntity: Application::class)]
private Collection $applications;
#[ORM\OneToMany(mappedBy: 'assignment', targetEntity: Disposition::class)]
private Collection $dispositions;
#[ORM\ManyToOne]
private ?User $contact = null;
#[ORM\ManyToMany(targetEntity: Teamer::class, mappedBy: 'bookmarks')]
private Collection $teamers;
#[ORM\ManyToOne]
private ?User $owner = null;
#[ORM\Column(length: 64)]
private ?string $status;
#[ORM\Column(length: 64)]
private ?string $staffingStatus;
public function __construct()
{
$this->uuid = Uuid::v4();
$this->status = static::STATUS_DRAFT;
$this->staffingStatus = static::STATUS_UNSTAFFED;
$this->fees = new ArrayCollection();
$this->applications = new ArrayCollection();
$this->dispositions = new ArrayCollection();
$this->teamers = new ArrayCollection();
$this->benefits = "Anreise im E&P Reisebus\nUnterkunft\nVerpflegung\nSkipass\n";
}
public static function duplicate(Assignment $assignment): static
{
$instance = new static();
$instance
->setDestination($assignment->getDestination())
->setJobProfile($assignment->getJobProfile())
->setAvailableDispositions($assignment->getAvailableDispositions())
->setShowAvailableDispositions($assignment->isShowAvailableDispositions())
->setDateFrom($assignment->getDateFrom())
->setDateTo($assignment->getDateTo())
->setPickupDate($assignment->getPickupDate())
->setPickup($assignment->getPickup())
->setBenefits($assignment->getBenefits())
->setContact($assignment->getContact());
$assignment->getFees()->map(function (Fee $fee) use ($instance) {
$instance->addFee($fee);
});
return $instance;
}
public function updateStaffingStatus(): void
{
$confirmedDispositions = $this->getValidDispositions();
$validApplications = $this->getValidApplications();
if ($this->availableDispositions === $confirmedDispositions->count()) {
$this->setStaffingStatus(Assignment::STATUS_STAFFED);
} elseif (
$this->availableDispositions > $confirmedDispositions->count()
&& 0 < $confirmedDispositions->count()
) {
$this->setStaffingStatus(Assignment::STATUS_PARTLY_STAFFED);
} elseif (
$this->availableDispositions > $confirmedDispositions->count()
&& 0 < $validApplications->count()
) {
$this->setStaffingStatus(Assignment::STATUS_STAFFING);
} else {
$this->setStaffingStatus(Assignment::STATUS_UNSTAFFED);
}
}
public function getId(): ?int
{
return $this->id;
}
public function getUuid(): string
{
return $this->uuid;
}
public function getDestination(): ?Destination
{
return $this->destination;
}
public function setDestination(?Destination $destination): static
{
$this->destination = $destination;
return $this;
}
public function getJobProfile(): ?JobProfile
{
return $this->jobProfile;
}
public function setJobProfile(?JobProfile $jobProfile): static
{
$this->jobProfile = $jobProfile;
return $this;
}
public function getJobProfileInfo(): ?string
{
return $this->jobProfileInfo;
}
public function setJobProfileInfo(?string $jobProfileInfo): static
{
$this->jobProfileInfo = $jobProfileInfo;
return $this;
}
public function getAvailableDispositions(): ?int
{
return $this->availableDispositions;
}
public function setAvailableDispositions(?int $availableDispositions): static
{
$this->availableDispositions = $availableDispositions;
return $this;
}
public function isShowAvailableDispositions(): ?bool
{
return $this->showAvailableDispositions;
}
public function setShowAvailableDispositions(bool $showAvailableDispositions): static
{
$this->showAvailableDispositions = $showAvailableDispositions;
return $this;
}
public function getDateFrom(): ?\DateTimeImmutable
{
return $this->dateFrom;
}
public function setDateFrom(?\DateTimeImmutable $dateFrom): static
{
$this->dateFrom = $dateFrom;
return $this;
}
public function getDateTo(): ?\DateTimeImmutable
{
return $this->dateTo;
}
public function setDateTo(?\DateTimeImmutable $dateTo): static
{
$this->dateTo = $dateTo;
return $this;
}
public function getEffectivePeriod(): ?CarbonPeriod
{
if (null !== $destination = $this->getDestination()) {
$dateFrom = $this->getDateFrom() ?? $destination->getDateFrom();
$dateTo = $this->getDateTo() ?? $destination->getDateTo();
return new CarbonPeriod($dateFrom, $dateTo);
}
return null;
}
public function getEffectiveDateFrom(): ?\DateTimeImmutable
{
if (null !== $effectivePeriod = $this->getEffectivePeriod()) {
return $effectivePeriod->start->toDateTimeImmutable();
}
return null;
}
public function getEffectiveDateTo(): ?\DateTimeImmutable
{
if (null !== $effectivePeriod = $this->getEffectivePeriod()) {
return $effectivePeriod->end->toDateTimeImmutable();
}
return null;
}
public function getEffectivePickupDate(): ?\DateTimeImmutable
{
if (null !== $this->pickupDate) {
return $this->pickupDate;
}
if (0 !== (int) $this->pickup && null !== $destination = $this->getDestination()) {
return $destination
->getDateFrom()
->modify('-1 day')
;
}
return null;
}
public function getPickupDate(): ?\DateTimeImmutable
{
return $this->pickupDate;
}
public function setPickupDate(?\DateTimeImmutable $pickupDate): static
{
$this->pickupDate = $pickupDate;
return $this;
}
public function getPickup(): ?int
{
return $this->pickup;
}
public function setPickup(null|int|string $pickup): static
{
$this->pickup = (int) $pickup;
return $this;
}
public function getBenefits(): ?string
{
return $this->benefits;
}
public function setBenefits(?string $benefits): static
{
$this->benefits = $benefits;
return $this;
}
public function getRemarks(): ?string
{
return $this->remarks;
}
public function setRemarks(?string $remarks): static
{
$this->remarks = $remarks;
return $this;
}
/**
* @return Collection<int, Fee>
*/
public function getFees(): Collection
{
return $this->fees;
}
public function addFee(Fee $fee): static
{
if (!$this->fees->contains($fee)) {
$this->fees->add($fee);
}
return $this;
}
public function removeFee(Fee $fee): static
{
$this->fees->removeElement($fee);
return $this;
}
/**
* @return Collection<int, Application>
*/
public function getApplications(): Collection
{
return $this->applications;
}
public function getApplicationByTeamer(Teamer $teamer): ?Application
{
$applications = $this->applications->filter(function (Application $application) use ($teamer) {
return $application->getTeamer() === $teamer;
});
if (0 === count($applications)) {
return null;
}
return $applications->first();
}
public function getValidApplications(): Collection
{
return $this->applications->filter(function (Application $application) {
return Application::STATUS_REJECTED !== $application->getStatus();
});
}
public function addApplication(Application $application): static
{
if (!$this->applications->contains($application)) {
$this->applications->add($application);
$application->setAssignment($this);
}
return $this;
}
public function removeApplication(Application $application): static
{
if ($this->applications->removeElement($application)) {
// set the owning side to null (unless already changed)
if ($application->getAssignment() === $this) {
$application->setAssignment(null);
}
}
return $this;
}
/**
* @return Collection<int, Disposition>
*/
public function getDispositions(): Collection
{
return $this->dispositions;
}
public function addDisposition(Disposition $disposition): static
{
if (!$this->dispositions->contains($disposition)) {
$this->dispositions->add($disposition);
$disposition->setAssignment($this);
}
return $this;
}
public function removeDisposition(Disposition $disposition): static
{
if ($this->dispositions->removeElement($disposition)) {
// set the owning side to null (unless already changed)
if ($disposition->getAssignment() === $this) {
$disposition->setAssignment(null);
}
}
return $this;
}
public function getValidDispositions(): Collection
{
return $this->dispositions->filter(function (Disposition $disposition) {
return true === in_array($disposition->getStatus(), [
Disposition::STATUS_NEW,
Disposition::STATUS_CHECKING_CONTRACT,
Disposition::STATUS_CONFIRMED,
]);
});
}
public function getContact(): ?User
{
return $this->contact;
}
public function setContact(?User $contact): static
{
$this->contact = $contact;
return $this;
}
/**
* @return Collection<int, Teamer>
*/
public function getTeamers(): Collection
{
return $this->teamers;
}
public function addTeamer(Teamer $teamer): static
{
if (!$this->teamers->contains($teamer)) {
$this->teamers->add($teamer);
}
return $this;
}
public function removeTeamer(Teamer $teamer): static
{
$this->teamers->removeElement($teamer);
return $this;
}
public function getOwner(): ?User
{
return $this->owner;
}
public function setOwner(?User $owner): static
{
$this->owner = $owner;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(string $status): static
{
$this->status = $status;
return $this;
}
public function getStaffingStatus(): ?string
{
return $this->staffingStatus;
}
public function setStaffingStatus(?string $staffingStatus): static
{
$this->staffingStatus = $staffingStatus;
return $this;
}
}