<?php
namespace App\Entity\Tutoring;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\Tutoring\MonthRepository;
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;
/**
* Month
*/
#[ORM\Entity(repositoryClass: MonthRepository::class)]
#[ORM\Table(name: 'hs_tc_tutoring_month')]
#[ApiResource]
class Month implements \Stringable
{
use Actions;
/**
* @var string
*/
#[ORM\Column(name: 'name', type: 'string', length: 255)]
private $name;
#[ORM\ManyToOne(targetEntity: Course::class, cascade: ['persist'], inversedBy: 'months')]
private ?Course $course = null;
/**
* @var integer
*/
#[ORM\Column(name: 'position', type: 'integer')]
private $position;
/**
* @var float
*/
#[ORM\Column(name: 'price', type: 'float', nullable: true)]
private $price;
#[ORM\OneToMany(mappedBy: 'month', targetEntity: Meeting::class, cascade: ['persist'], orphanRemoval: true)]
#[ORM\OrderBy(["createAt" => "ASC"])]
private ?Collection $meetings = null;
#[ORM\OneToMany(mappedBy: 'month', targetEntity: Owe::class, cascade: ['persist'], orphanRemoval: true)]
private ?Collection $owes = null;
public function __construct()
{
$this->createAt=new \DateTime('now');
$this->published=true;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nom
*
* @param string $name
* @return Month
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
function __toString(): string
{
return $this->name;
}
/**
* Set course
*
*
* @return Month
*/
public function setCourse(Course $course = null)
{
$this->course = $course;
return $this;
}
/**
* Get course
*
* @return \App\Entity\Tutoring\Course
*/
public function getCourse()
{
return $this->course;
}
/**
* @return int
*/
public function getPosition()
{
return $this->position;
}
/**
* @param int $position
* @return Month
*/
public function setPosition($position)
{
$this->position = $position;
return $this;
}
/**
* @return float
*/
public function getPrice()
{
return $this->price;
}
/**
* @param float $price
* @return Month
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Add meeting
*
*
* @return Month
*/
public function addMeeting(Meeting $meeting)
{
$meeting->setMonth($this);
$this->meetings[] = $meeting;
return $this;
}
/**
* Remove meeting
*/
public function removeMeeting(Meeting $meeting)
{
$this->meetings->removeElement($meeting);
}
/**
* Get meetings
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getMeetings()
{
return $this->meetings;
}
public function getMeetingsByStatut($statut){
$items=new ArrayCollection();
switch ($statut){
case 'all':
$items=$this->meetings;
break;
case 'checked':
/** @var Meeting $meeting */
foreach ($this->getMeetings() as $meeting){
if($meeting->isPublished()){
$items->add($meeting);
}
}
break;
case 'unchecked':
/** @var Meeting $meeting */
foreach ($this->getMeetings() as $meeting){
if(!$meeting->isPublished()){
$items->add($meeting);
}
}
break;
}
return $items;
}
public function getMeetingsPassed()
{
$items=new ArrayCollection();
$today=new \DateTime('now');
/** @var Meeting $meeting */
foreach ($this->getMeetings() as $meeting){
if($meeting->getCreateAt()< $today){
$items->add($meeting);
}
}
return $items;
}
/**
* Add owe
*
*
* @return Month
*/
public function addOwe(Owe $owe)
{
$owe->setMonth($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;
}
public function getStartAt(){
//
//$now=new \DateTime('now');
$format = 'd-m-Y H:i:s';
if($this->name !=="FI"){
return \DateTime::createFromFormat($format, '1-'.$this->getName().' 00:01:00');
}
return $this->course->getStartAt();
}
public function getHalf(){
$format = 'd-m-Y H:i:s';
return \DateTime::createFromFormat($format, '10-'.$this->getName().' 00:00:00');
}
public function getEndAt(){
$format = 'd-m-Y H:i:s';
return \DateTime::createFromFormat($format, $this->getStartAt()->format('t-m-Y').' 23:59:59');
}
public function isPassed(){
$now=new \DateTime('now');
return $now>$this->getStartAt();
}
public function isOutDate(){
$now=new \DateTime('now');
return $now>$this->getHalf();
}
public function dateInMonth(\DateTime $date){
return $date>$this->getStartAt() && $date<$this->getEndAt();
}
public function getTotalOwe(){
$total=0;
/** @var Owe $owe */
foreach ($this->owes as $owe){
$total+=$owe->getPrice();
}
return $total;
}
public function getTotalPaies(){
$total=0;
/** @var Owe $owe */
foreach ($this->owes as $owe){
$total+=$owe->getRowscourse()->getPaieMonth($owe)['amount'];
}
return $total;
}
function compareByCreatedAt($a, $b) {
// Récupérer les valeurs de createdAt pour chaque objet
$createdAtA = $a->getCreatedAt();
$createdAtB = $b->getCreatedAt();
// Comparer les valeurs de createdAt
if ($createdAtA == $createdAtB) {
return 0;
}
return ($createdAtA < $createdAtB) ? -1 : 1;
}
public function getMeetingsByDate()
{
$items=$this->meetings->toArray();
usort($items, fn(Meeting $a, Meeting $b) => $a->getCreateAt() <=> $b->getCreateAt());
return $items;
}
}