<?php
namespace App\Entity\Tutoring;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Schoolyear;
use App\Entity\User;
use App\Repository\Tutoring\TutorRepository;
use App\Traits\Actions;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Doctrine\ORM\Event\LifecycleEventArgs;
/**
* Tutor
*
* @ORM\HasLifecycleCallbacks()
*/
#[ORM\Entity(repositoryClass: TutorRepository::class)]
#[ORM\Table(name: 'hs_tc_tutoring_tutor')]
#[ApiResource]
class Tutor implements \Stringable
{
use Actions;
#[ORM\Column(name: 'first_name', type: 'string', length: 255)]
private ?string $firstName = null;
#[ORM\Column(name: 'last_name', type: 'string', length: 255)]
private ?string $lastName = null;
/**
* @var string
*/
#[ORM\Column(name: 'email', type: 'string', length: 255, nullable: true)]
private $email;
/**
* @var string
*/
#[ORM\Column(name: 'phone', type: 'string', length: 255, nullable: true)]
private $phone;
#[ORM\Column(name: 'wp', type: 'string', length: 255, nullable: true)]
private ?string $wp = null;
#[ORM\Column(name: 'type_pay', type: 'string', length: 255, nullable: true)]
private ?string $typePay = null;
#[ORM\Column(name: 'pc_pay', type: 'float', nullable: true)]
private ?float $pcPay = null;
#[ORM\Column(name: 'min_pay', type: 'float', nullable: true)]
private ?float $minPay = null;
/**
* @var string
*/
#[ORM\Column(name: 'specialty', type: 'string', length: 255, nullable: true)]
private $specialty;
#[ORM\OneToOne(inversedBy: "tutor", targetEntity: User::class, cascade: ["persist", "remove"])]
#[ORM\JoinColumn(nullable: true)]
private $user;
#[ORM\OneToMany(mappedBy: 'tutor', targetEntity: Course::class)]
private ?Collection $courses = null;
/**
* Tutor constructor.
*/
public function __construct()
{
$this->createAt=new \DateTime('now');
$this->published=false;
$this->wp="212";
}
/**
* @return string
*/
public function getFirstName(): ?string
{
return $this->firstName;
}
/**
* @param string $firstName
*/
public function setFirstName(?string $firstName): Tutor
{
$this->firstName = $firstName;
return $this;
}
/**
* @return string
*/
public function getLastName(): ?string
{
return $this->lastName;
}
/**
* @param string $lastName
*/
public function setLastName(?string $lastName): Tutor
{
$this->lastName = $lastName;
return $this;
}
/**
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* @param string $email
* @return Tutor
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* @return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* @param string $phone
* @return Tutor
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
public function getWp(): ?string
{
return $this->wp;
}
public function setWp(?string $wp): void
{
$this->wp = $wp;
}
/**
* @return string
*/
public function getSpecialty()
{
return $this->specialty;
}
/**
* @param string $specialty
* @return Tutor
*/
public function setSpecialty($specialty)
{
$this->specialty = $specialty;
return $this;
}
/**
* @return string
*/
public function getTypePay(): ?string
{
return $this->typePay;
}
/**
* @param string $typePay
*/
public function setTypePay(?string $typePay): Tutor
{
$this->typePay = $typePay;
return $this;
}
/**
* @return float
*/
public function getPcPay(): ?float
{
return $this->pcPay;
}
/**
* @param float $pcPay
*/
public function setPcPay(?float $pcPay): Tutor
{
$this->pcPay = $pcPay;
return $this;
}
/**
* @return float
*/
public function getMinPay(): ?float
{
return $this->minPay;
}
/**
* @param float $minPay
*/
public function setMinPay(?float $minPay): Tutor
{
$this->minPay = $minPay;
return $this;
}
public function __toString(): string
{
return $this->lastName.' '.$this->firstName;
}
/**
* @return mixed
*/
public function getUser()
{
return $this->user;
}
/**
* @return Tutor
*/
public function setUser(mixed $user)
{
$this->user = $user;
return $this;
}
/**
* Add course
*
*
* @return Tutor
*/
public function addCourse(Course $course)
{
$course->setTutor($this);
$this->courses[] = $course;
return $this;
}
/**
* Remove course
*/
public function removeCourse(Course $course)
{
$this->courses->removeElement($course);
}
/**
* Get courses
*
* @return ArrayCollection
*/
public function getCourses()
{
return $this->courses;
}
public function getCoursesByYear(Schoolyear $schoolyear)
{
$items=[];
foreach ($this->getCourses() as $course) {
if ($course->getSchoolyear() == $schoolyear) {
$items[] = $course;
}
}
return $items;
}
public function getRevenue(Schoolyear $schoolyear){
$r=0;
/** @var Course $cours */
foreach ($this->getCoursesByYear($schoolyear) as $cours){
$r+=$cours->getSumToPaid();
}
return $r;
}
public function getTotalFees(Schoolyear $schoolyear){
$f=0;
/** @var Course $cours */
foreach ($this->getCoursesByYear($schoolyear) as $cours){
$f+=$cours->getSumPaidForTutor();
}
return round($f*$this->pcPay/100,2);
}
public function getFeesByMonth($month){
$f=0;
/** @var Course $cours */
foreach ($this->getCourses() as $cours){
if('FI'==$month) continue;
$f+=$cours->getFeesByMonths($month);
}
return $f;
}
public function getRegulationByMonth($month){
$sum=0;
/** @var Course $cours */
foreach ($this->getCourses() as $cours){
$sum+=$cours->getRegulationByMonth($month);
}
return $sum;
}
public function getRestByMonth($month)
{
$sum=0;
foreach ($this->getCourses() as $cours){
$sum+=$cours->getRestByMonth($month);
}
return $sum;
}
public function getTotalRegulations(Schoolyear $schoolyear){
$t=0;
/** @var Course $cours */
foreach ($this->getCoursesByYear($schoolyear) as $cours){
$t+=$cours->getTotalRegulations();
}
return $t;
}
public function getRestRegulation(Schoolyear $schoolyear){
return max($this->getTotalFees($schoolyear)-$this->getTotalRegulations($schoolyear),0);
}
public function getAvanceBySY(Schoolyear $schoolyear){
return min($this->getTotalFees($schoolyear)-$this->getTotalRegulations($schoolyear),0);
}
public function getSituationBeforeYear(Schoolyear $schoolyear){
if($schoolyear->getPrevious()!=null){
return $this->getSituationBeforeYear($schoolyear->getPrevious())+ $this->getRestRegulation($schoolyear->getPrevious())-$this->getAvanceBySY($schoolyear->getPrevious());
}
else
return 0;
}
public function getSituationAfterYear(Schoolyear $schoolyear){
return $this->getSituationBeforeYear($schoolyear)+$this->getRevenue($schoolyear);
}
}