<?php
namespace App\Entity;
use App\Entity\CourseOccurrence;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use Doctrine\Common\Collections\Collection;
use Menke\UserBundle\Entity\Client;
use Menke\CommonsBundle\Entity\GenericEntity;
use Doctrine\Common\Collections\ArrayCollection;
use EXSyst\Component\Swagger\Response;
/**
* @ORM\Entity(repositoryClass="App\Repository\CourseRepository")
* @JMS\ExclusionPolicy("all")
*/
class Course extends GenericEntity
{
/**
* @ORM\ManyToOne(targetEntity="\Menke\UserBundle\Entity\Client")
* @ORM\JoinColumn(name="clientId", referencedColumnName="id")
*/
protected $client;
/**
* @ORM\Column(type="string", length=255)
* @JMS\Expose
* @JMS\Groups({"public"})
*/
protected $title;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @JMS\Expose
* @JMS\Groups({"public"})
*/
protected $subtitle;
/**
* @ORM\Column(type="string", length=255, options={"default" : "Course"})
* @JMS\Expose
* @JMS\Groups({"public", "personal"})
*/
protected $courseNature = 'Course';
/**
* @ORM\ManyToOne(targetEntity="App\Entity\CourseSubscription")
* @JMS\Expose
* @JMS\Groups({"public"})
*/
protected $subscription;
/**
* @ORM\Column(type="text", nullable=true)
* @JMS\Expose
* @JMS\Groups({"public"})
*/
protected $description;
/**
* @ORM\Column(type="float")
* @JMS\Expose
* @JMS\Groups({"public"})
*/
protected $price;
/**
* @ORM\Column(type="float", nullable=true)
*/
protected $taxRate = 0;
/**
* @ORM\OneToMany(targetEntity="CourseOccurrence", mappedBy="course", cascade={"all"})
* @JMS\Expose
* @JMS\Groups({"public"})
*/
protected $occurrences;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Category")
* @JMS\Expose
* @JMS\Groups({"public"})
*/
protected $category;
/**
* @ORM\OneToMany(targetEntity="App\Entity\CourseImage", mappedBy="course", orphanRemoval=true, cascade={"all"})
* @ORM\OrderBy({"orderId" = "ASC"})
* @JMS\Expose
* @JMS\Groups({"public"})
*/
protected $images;
/**
* @ORM\Column(type="string", length=20, nullable=true)
* @JMS\Expose
* @JMS\Groups({"public"})
*/
protected $number;
/**
* @ORM\Column(type="float", nullable=true)
* @JMS\Expose
* @JMS\Groups({"public"})
*/
protected $materialCost = 0.0;
/**
* @ORM\Column(type="integer", nullable=true)
* @JMS\Expose
* @JMS\Groups({"public"})
*/
protected $targetAgeMin;
/**
* @ORM\Column(type="integer", nullable=true)
* @JMS\Expose
* @JMS\Groups({"public"})
*/
protected $targetAgeMax;
/**
* @ORM\OneToMany(targetEntity="App\Entity\CourseText", mappedBy="course", orphanRemoval=true, cascade={"all"})
* @ORM\OrderBy({"orderId" = "ASC"})
* @JMS\Expose
* @JMS\Groups({"detail"})
*/
protected $texts;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\CourseSeries", inversedBy="courses")
* @JMS\Expose
* @JMS\Groups({"public"})
*/
protected $series;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\CourseType")
* @JMS\Expose
* @JMS\Groups({"public", "personal"})
*/
protected $type;
/**
* @ORM\Column(type="text", nullable=true)
*/
protected $invoiceUpperComment;
/**
* @ORM\Column(type="text", nullable=true)
*/
protected $invoiceLowerComment;
/**
* @ORM\Column(type="text", nullable=true)
*/
protected $invoiceLowerCommentDebit;
/**
* @JMS\Expose
* @JMS\Groups({"public"})
*/
protected $fields;
public function __construct()
{
$this->occurrences = new ArrayCollection();
$this->images = new ArrayCollection();
$this->texts = new ArrayCollection();
}
/**
* @return Client
*/
public function getClient(): ?Client
{
return $this->client;
}
/**
* @param Client $client
* @return Course
*/
public function setClient($client): self
{
$this->client = $client;
return $this;
}
/**
* @return null|string
*/
public function getTitle(): ?string
{
// $title = str_replace('­', '', $this->title);
// return $title;
return $this->title;
}
/**
* @param string $title
* @return Course
*/
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
/**
* @return null|string
*/
public function getSubtitle(): ?string
{
return $this->subtitle;
}
/**
* @param null|string $subtitle
* @return Course
*/
public function setSubtitle(?string $subtitle): self
{
$this->subtitle = $subtitle;
return $this;
}
/**
* @return null|string
*/
public function getDescription(): ?string
{
return $this->description;
}
/**
* @param null|string $description
* @return Course
*/
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
/**
* @return float|null
*/
public function getPrice(): ?float
{
return $this->price;
}
/**
* @param float $price
* @return Course
*/
public function setPrice(float $price): self
{
$this->price = $price;
return $this;
}
/**
* @return float
*/
public function getTaxRate(): ?float
{
return $this->taxRate;
}
/**
* @param float $taxRate
* @return Course
*/
public function setTaxRate(?float $taxRate)
{
$this->taxRate = $taxRate;
return $this;
}
/**
* @JMS\Expose
* @JMS\Groups({"detail"})
* @JMS\VirtualProperty()
* @JMS\Type("array<App\Entity\CourseOccurrence>")
* @param ArrayCollection<CourseOccurrence> $occurrences
* @return array<App\Entity\CourseOccurrence>
*/
public function getOccurrences(bool $onlyPublished = false, bool $isBookable = false): ArrayCollection
{
$occurrences = new ArrayCollection;
foreach ($this->occurrences as $occurrence) {
if ($isBookable) {
if ($occurrence->getEnd() > new \DateTime() && $occurrence->isBookable() && (!$onlyPublished || $occurrence->getPublished())) {
$occurrences->add($occurrence);
}
}else{
$occurrences->add($occurrence);
}
}
return $occurrences;
}
public function getAllOccurrences(bool $onlyPublished = false, bool $isBookable = false): ArrayCollection
{
$occurrences = new ArrayCollection();
foreach ($this->occurrences as $occurrence) {
if ($onlyPublished && !$occurrence->getPublished()) {
continue;
}
if ($isBookable && !$occurrence->isBookable()) {
continue;
}
// Beispiel für zusätzliche Berechnungen
// $freeSlots = $occurrence->getSlots() - $occurrence->getBookedSlots();
//$occurrence->setFreeSlots($freeSlots); // Beispielmethodenaufruf
$occurrences->add($occurrence);
}
return $occurrences;
}
/**
* @param ArrayCollection<CourseOccurrence> $occurrences
* @return Course
*/
public function setOccurrences($occurrences): self
{
$this->occurrences = $occurrences;
return $this;
}
/**
* @param CourseOccurrence $occurrence
* @return Course
*/
public function addOccurrence(CourseOccurrence $occurrence): self
{
$occurrence->setCourse($this);
$this->occurrences->add($occurrence);
return $this;
}
/**
* @param CourseOccurrence $occurrence
* @return Course
*/
public function removeOccurrence(CourseOccurrence $occurrence)
{
if ($this->occurrences->contains($occurrence)) {
$this->occurrences->removeElement($occurrence);
if ($occurrence->getCourse() === $this) {
$occurrence->setCourse(null);
}
}
return $this;
}
/**
* @return Category|null
*/
public function getCategory(): ?Category
{
return $this->category;
}
/**
* @param Category|null $category
* @return Course
*/
public function setCategory(?Category $category): self
{
$this->category = $category;
return $this;
}
/**
* @return string
*/
public function getCourseNature(): string
{
return $this->courseNature;
}
/**
*
*/
public function setCourseNature($courseNature)
{
$this->courseNature = $courseNature;
}
/**
* @return Collection|CourseImage[]
*/
public function getImages(): Collection
{
return $this->images;
}
/**
* @param CourseImage $image
* @return Course
*/
public function addImage(CourseImage $image): self
{
if (!$this->images->contains($image)) {
$this->images[] = $image;
$image->setCourse($this);
}
return $this;
}
/**
* @param CourseImage $image
* @return Course
*/
public function removeImage(CourseImage $image): self
{
if ($this->images->contains($image)) {
$this->images->removeElement($image);
// set the owning side to null (unless already changed)
if ($image->getCourse() === $this) {
$image->setCourse(null);
}
}
return $this;
}
/**
* @return null|string
*/
public function getNumber(): ?string
{
return $this->number;
}
/**
* @param null|string $number
* @return Course
*/
public function setNumber(?string $number): self
{
$this->number = $number;
return $this;
}
public function getMaterialCost(): ?float
{
return $this->materialCost;
}
public function setMaterialCost(?float $materialCost): self
{
$this->materialCost = $materialCost;
return $this;
}
public function getTargetAgeMin(): ?int
{
return $this->targetAgeMin;
}
public function setTargetAgeMin(?int $targetAgeMin): self
{
$this->targetAgeMin = $targetAgeMin;
return $this;
}
public function getTargetAgeMax(): ?int
{
return $this->targetAgeMax;
}
public function setTargetAgeMax(?int $targetAgeMax): self
{
$this->targetAgeMax = $targetAgeMax;
return $this;
}
/**
* @return Collection|CourseText[]
*/
public function getTexts(): Collection
{
return $this->texts;
}
public function addText(CourseText $text): self
{
if (!$this->texts->contains($text)) {
$this->texts[] = $text;
$text->setCourse($this);
}
return $this;
}
public function removeText(CourseText $text): self
{
if ($this->texts->contains($text)) {
$this->texts->removeElement($text);
// set the owning side to null (unless already changed)
if ($text->getCourse() === $this) {
$text->setCourse(null);
}
}
return $this;
}
public function getSeries(): ?CourseSeries
{
return $this->series;
}
public function setSeries(?CourseSeries $series): self
{
$this->series = $series;
return $this;
}
public function getSubscription(): ?CourseSubscription
{
return $this->subscription;
}
public function setSubscription(?CourseSubscription $subscription): self
{
$this->subscription = $subscription;
return $this;
}
/**
* @JMS\Expose
* @JMS\Groups({"public", "personal"})
* @JMS\VirtualProperty()
* @JMS\Type("int")
* @return int
*/
public function getId(): ?int
{
return parent::getId();
}
/**
* @return CourseType|null
*/
public function getType(): ?CourseType
{
return $this->type;
}
/**
* @param Type|null $CourseType
* @return CourseType
*/
public function setType(?CourseType $type): self
{
$this->type = $type;
return $this;
}
/**
* @return string|null
*/
public function getInvoiceUpperComment(): ?string
{
return $this->invoiceUpperComment;
}
/**
* @param string|null $invoiceUpperComment
* @return self
*/
public function setInvoiceUpperComment(?string $invoiceUpperComment): self
{
$this->invoiceUpperComment = $invoiceUpperComment;
return $this;
}
/**
* @return string|null
*/
public function getInvoiceLowerComment(): ?string
{
return $this->invoiceLowerComment;
}
/**
* @param string|null $invoiceLowerComment
* @return self
*/
public function setInvoiceLowerComment(?string $invoiceLowerComment): self
{
$this->invoiceLowerComment = $invoiceLowerComment;
return $this;
}
/**
* @return string|null
*/
public function getInvoiceLowerCommentDebit(): ?string
{
return $this->invoiceLowerCommentDebit;
}
/**
* @param string|null $invoiceLowerCommentDebit
* @return self
*/
public function setInvoiceLowerCommentDebit(?string $invoiceLowerCommentDebit): self
{
$this->invoiceLowerCommentDebit = $invoiceLowerCommentDebit;
return $this;
}
/**
* @JMS\Expose
* @JMS\Groups({"public"})
* @JMS\VirtualProperty()
* @JMS\SerializedName("type")
* @JMS\Type("string")
* @return int
*/
public function getTypeSlug()
{
return $this->getType() ? $this->getType()->getSlug() : null;
}
/**
* @return bool|null
*/
public function isArchive(): ?bool
{
$onlyPublished = true;
$isBookable = false;
if (count($this->getAllOccurrences($onlyPublished, $isBookable)) < 1) {
return true;
}
$now = new \DateTime();
foreach ($this->getAllOccurrences($onlyPublished, $isBookable) as $occurrence) {
$diff = $now->diff($occurrence->getEnd());
if ($diff->invert == 0 || $diff->d > $_ENV['DAYS_TO_ARCHIVE']) {
return false;
}
}
return true;
}
/**
* @return array|null
*/
public function getFields(): ?array
{
return $this->fields;
}
/**
* @param array $fields
*/
public function setFields(array $fields): void
{
$this->fields = $fields;
}
}