<?php
/**
* Created by PhpStorm.
* User: dada
* Date: 12/07/19
* Time: 20:34
*/
namespace App\Entity\Tutoring;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\Tutoring\RowscourseRepository;
use App\Traits\Actions;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\OrderBy;
/**
* Rowscourse
*
* @ORM\HasLifecycleCallbacks()
*/
#[ORM\Entity(repositoryClass: RowscourseRepository::class)]
#[ORM\Table(name: 'hs_tc_tutoring_rowscourse')]
#[ApiResource]
class Rowscourse implements \Stringable
{
use Actions;
#[ORM\ManyToOne(targetEntity: Course::class, inversedBy: 'rowscourses')]
#[ORM\JoinColumn(nullable: true)]
private $course;
#[ORM\ManyToOne(targetEntity: Student::class, cascade: ['persist'], inversedBy: 'rowscourses')]
#[ORM\JoinColumn(nullable: false)]
private $student;
/**
* @var float
*/
#[ORM\Column(name: 'price', type: 'float', nullable: true)]
private $price;
#[ORM\Column(name: 'fees_registration', type: 'float', nullable: true)]
private ?float $feesRegistration = 0;
/**
* @var \DateTime
*/
#[ORM\Column(name: 'start_at', type: 'datetime')]
private $startAt;
/**
* @var \DateTime
*/
#[ORM\Column(name: 'end_at', type: 'datetime')]
private $endAt;
/**
* @OrderBy({"position" = "ASC"})
*/
#[ORM\OneToMany(mappedBy: 'rowscourse', targetEntity: Owe::class, cascade: ['persist'], orphanRemoval: true)]
private ?Collection $owes = null;
/**
* @OrderBy({"meeting" = "ASC"})
*/
#[ORM\OneToMany(mappedBy: 'rowscourse', targetEntity: Presence::class, cascade: ['persist'], orphanRemoval: true)]
private ?Collection $presences = null;
/**
* @OrderBy({"createAt" = "ASC"})
*/
#[ORM\OneToMany(mappedBy: 'rowscourse', targetEntity: Spayment::class, cascade: ['persist'], orphanRemoval: true)]
private ?Collection $payments = null;
public function __construct()
{
$this->owes=new ArrayCollection();
$this->presences=new ArrayCollection();
$this->payments=new ArrayCollection();
$this->createAt=new \DateTime('now');
$this->published=true;
}
/**
* @return Course
*/
public function getCourse()
{
return $this->course;
}
/**
* @return Rowscourse
*/
public function setCourse(mixed $course)
{
$this->course = $course;
return $this;
}
/**
* @return float
*/
public function getPrice()
{
return $this->price;
}
/**
* @param float $price
* @return Rowscourse
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
public function getFeesRegistration(): ?float
{
return $this->feesRegistration;
}
public function setFeesRegistration(?float $feesRegistration): Rowscourse
{
$this->feesRegistration = $feesRegistration;
return $this;
}
/**
* @return Student
*/
public function getStudent()
{
return $this->student;
}
/**
* @return Rowscourse
*/
public function setStudent(mixed $student)
{
$this->student = $student;
return $this;
}
/**
* @return \DateTime
*/
public function getStartAt()
{
return $this->startAt;
}
/**
* @param \DateTime $startAt
* @return Rowscourse
*/
public function setStartAt($startAt)
{
$this->startAt = $startAt;
return $this;
}
/**
* @return \DateTime
*/
public function getEndAt()
{
return $this->endAt;
}
/**
* @param \DateTime $endAt
* @return Rowscourse
*/
public function setEndAt($endAt)
{
$this->endAt = $endAt;
return $this;
}
/**
* Add payment
*
*
* @return Rowscourse
*/
public function addPayment(Spayment $payment)
{
$payment->setRowscourse($this);
$this->payments[] = $payment;
return $this;
}
/**
* Remove payment
*/
public function removePayment(Spayment $payment)
{
$this->payments->removeElement($payment);
}
/**
* Get payments
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPayments()
{
return $this->payments;
}
/**
* Add owe
*
*
* @return Rowscourse
*/
public function addOwe(Owe $owe)
{
$owe->setRowscourse($this);
$this->owes[] = $owe;
return $this;
}
/**
* Remove owe
*/
public function removeOwe(Owe $owe)
{
$this->owes->removeElement($owe);
}
/**
* Get owes
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getOwes()
{
return $this->owes;
}
/**
* Add presence
*
*
* @return Rowscourse
*/
public function addPresence(Presence $presence)
{
$presence->setRowscourse($this);
$this->presences[] = $presence;
return $this;
}
/**
* Remove presence
*/
public function removePresence(Presence $presence)
{
$this->presences->removeElement($presence);
}
/**
* Get presences
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPresences()
{
return $this->presences;
}
/*public function getMonths(){
$items=array();
$date= clone $this->startAt;
while ($date<$this->endAt){
$month=$date->format('m-Y');
$items[]=$month;
$date->modify('+1 month');
}
return $items;
}*/
/* public function getIndexMonthCurrent(){
$new=new \DateTime('now');
$month=$new->format('m-Y');
if (array_search($month, $this->getMonths()))
return array_search($month, $this->getMonths());
else
if ($new<$this->startAt) return -1;
else return -2;
}*/
public function isStillInMonth(Month $month){
if ($month->getEndAt()<$this->startAt || $month->getStartAt()>$this->endAt) return false;
else return true;
}
public function isStillInMonthText(string $monthText): bool
{
// Normaliser le séparateur
$monthText = str_replace('/', '-', $monthText);
// Créer la date de début du mois
$startMonth = \DateTime::createFromFormat('m-Y', $monthText);
if (!$startMonth) return false;
// Cloner et aller au dernier jour du mois
$endMonth = clone $startMonth;
$endMonth->modify('last day of this month')->setTime(23, 59, 59);
// Vérifier le chevauchement
if ($endMonth < $this->startAt || $startMonth > $this->endAt) return false;
return true;
}
public function getSumToPaid(){
if ($this->getCourse() === null) return 0;
$t=0;
/** @var Owe $m */
foreach ($this->getOwes() as $m){
if($m->getMonth()->getName()!='FI'){
if($m->getMonth()->isPassed() && $this->getCourse()->isStillInMonth($m->getMonth()) && $this->isStillInMonth($m->getMonth()))
$t+=$m->getPrice();
}
else{
$t+=$m->getPrice();
}
}
return $t;
}
public function getSumToPayWithoutFI()
{
if ($this->getCourse() === null) return 0;
$t=0;
/** @var Owe $m */
foreach ($this->getOwes() as $m){
if($m->getMonth()->getName()!='FI'){
if($m->getMonth()->isPassed() && $this->getCourse()->isStillInMonth($m->getMonth()) && $this->isStillInMonth($m->getMonth()))
$t+=$m->getPrice();
}
}
return $t;
}
public function getOutDate(){
if ($this->getCourse() === null) return 0;
$t=0;
// /** @var Owe $m */
// foreach ($this->getOwes() as $m){
// if($m->getMonth()->isOutDate())
// $t+=$m->getPrice();
// }
foreach ($this->getOwes() as $m){
if($m->getMonth()->getName()!='FI'){
if($m->getMonth()->isOutDate() && $this->getCourse()->isStillInMonth($m->getMonth()) && $this->isStillInMonth($m->getMonth()))
$t+=$m->getPrice();
}
else{
$t+=$m->getPrice();
}
}
return $t;
}
public function getSumPaid(){
$s=0;
/** @var Spayment $payment */
foreach ($this->getPayments() as $payment){
$s+=$payment->getAmount();
}
return $s;
}
public function getSumPaidForTutor(){
$fi=0;
foreach ($this->getOwes() as $m){
if($m->getMonth()->getName()=='FI') $fi=$m->getPrice();
}
// $s=0;
// foreach ($this->getPayments() as $payment){
// if($payment->isPublished()) $s+=$payment->getAmount();
// }
return max(min($this->getSumToPayWithoutFI(), $this->getSumPaid())-$fi,0);
}
public function getRest(){
return ($this->getSumToPaid()-$this->getSumPaid())>0?($this->getSumToPaid()-$this->getSumPaid()):0;
}
public function getAdvance(){
return ($this->getSumToPaid()-$this->getSumPaid())<0?-($this->getSumToPaid()-$this->getSumPaid()):0;
}
public function getLatePaid(){
return ($this->getOutDate()-$this->getSumPaid())>0?($this->getOutDate()-$this->getSumPaid()):0;
}
public function getMonthsPaid(){
$months=[];
/** @var Owe $owe */
foreach ($this->owes as $owe){
if($owe->isPaied()){
$months[]=$owe;
}
}
return $months;
}
public function getPaieMonth(Owe $owe){
return ['month'=>$owe->getMonth()->getName(), 'amount'=>$owe->amountPaied(), 'rest'=>0];
}
public function getNbrMonthPaid(){
return is_countable($this->getMonthsPaid()) ? count($this->getMonthsPaid()) : 0;
}
public function getListMonthPaid(){
$list='';
foreach ($this->getMonthsPaid() as $line){
$list.="<span class='label label-success' style='margin-left: 5px;'>". $line['month'] ."</span>";
}
return $list;
}
public function getPresence(Meeting $meeting){
/** @var Presence $presence */
foreach ($this->getPresences() as $presence){
if($presence->getMeeting()===$meeting) return $presence;
}
return null;
}
public function getAbsences()
{
$items=new ArrayCollection();
/** @var Presence $presence */
foreach ($this->getPresences() as $presence){
if(!$presence->isPublished() && $presence->getMeeting()->isPublished()){
$items->add($presence);
}
}
return $items;
}
public function getNbrAbsences()
{
return $this->getAbsences()->count();
}
public function getMonthPresence(Month $month, $statut){
$items=new ArrayCollection();
switch ($statut){
case 'all':
/** @var Presence $presence */
foreach ($this->getPresences() as $presence){
if($presence->getMeeting()->getMonth()===$month && $presence->getMeeting()->isPublished()){
$items->add($presence);
}
}
break;
case 'checked':
/** @var Presence $presence */
foreach ($this->getPresences() as $presence){
if($presence->getMeeting()->getMonth()===$month && $presence->isPublished() && $presence->getMeeting()->isPublished()){
$items->add($presence);
}
}
break;
case 'unchecked':
/** @var Presence $presence */
foreach ($this->getPresences() as $presence){
if($presence->getMeeting()->getMonth()===$month && !$presence->isPublished() && $presence->getMeeting()->isPublished()){
$items->add($presence);
}
}
break;
}
return $items;
}
public function isAbandoned(Month $month){
if($this->getEndAt()<$month->getStartAt()) return true;
return false;
}
public function isAbandonedByDate($date){
$date=\DateTime::createFromFormat('d/m/Y', $date);
if($this->getEndAt()<$date) return true;
return false;
}
public function getOweByMonth($month){
/** @var Owe $owe */
foreach ($this->owes as $owe){
if($owe->getMonth()->getName()===$month) return $owe;
}
return null;
}
public function getSubject(){
if($this->course==null) return "Frais d'inscription";
return $this->getCourse()->getSubject();
}
public function __toString(): string
{
return $this->getCourse().'-->'.$this->getSubject();
}
public function getPcPay(){
if($this->getSumToPaid()!=0)
return round($this->getSumPaid()/$this->getSumToPaid()*100,0);
else
return 0;
}
public function getMonthByName($name)
{
foreach ($this->owes as $owe){
if ($owe->getMonth()->getName()==$name){
return $owe->getMonth();
}
}
return null;
}
public function getOweByNameMonth($name)
{
foreach ($this->owes as $owe){
if ($owe->getMonth()->getName()==$name){
return $owe;
}
}
return null;
}
public function getAmountPaidByMonth($month)
{
return $this->getOweByNameMonth($month)!==null?$this->getOweByNameMonth($month)->amountPaied():0;
}
public function getSimilarCourses(bool $tutor=false)
{
$courses = [];
foreach ($this->getCourse()->getSubject()->getCourses() as $course) {
if ($course->getId() != $this->getCourse()->getId()) {
if ($course->getSchoolyear()->getId() == $this->getCourse()->getSchoolyear()->getId() && $course->getLevels()->contains($this->getStudent()->getLevel())) {
if ($tutor) {
if ($course->getTutor()->getId() == $this->getCourse()->getTutor()->getId()) {
$courses[] = $course;
}
} else {
$courses[] = $course;
}
}
}
}
return $courses;
}
}