src/Entity/Tutoring/Month.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Tutoring;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Repository\Tutoring\MonthRepository;
  5. use App\Traits\Actions;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Doctrine\ORM\Mapping\OrderBy;
  10. /**
  11.  * Month
  12.  */
  13. #[ORM\Entity(repositoryClassMonthRepository::class)]
  14. #[ORM\Table(name'hs_tc_tutoring_month')]
  15. #[ApiResource]
  16. class Month implements \Stringable
  17. {
  18.     use Actions;
  19.     /**
  20.      * @var string
  21.      */
  22.     #[ORM\Column(name'name'type'string'length255)]
  23.     private $name;
  24.     #[ORM\ManyToOne(targetEntityCourse::class, cascade: ['persist'], inversedBy'months')]
  25.     private ?Course $course null;
  26.     /**
  27.      * @var integer
  28.      */
  29.     #[ORM\Column(name'position'type'integer')]
  30.     private $position;
  31.     /**
  32.      * @var float
  33.      */
  34.     #[ORM\Column(name'price'type'float'nullabletrue)]
  35.     private $price;
  36.     #[ORM\OneToMany(mappedBy'month'targetEntityMeeting::class, cascade: ['persist'], orphanRemovaltrue)]
  37.     #[ORM\OrderBy(["createAt" => "ASC"])]
  38.     private ?Collection $meetings null;
  39.     #[ORM\OneToMany(mappedBy'month'targetEntityOwe::class, cascade: ['persist'], orphanRemovaltrue)]
  40.     private ?Collection $owes null;
  41.     public function __construct()
  42.     {
  43.         
  44.         $this->createAt=new \DateTime('now');
  45.         $this->published=true;
  46.     }
  47.     /**
  48.      * Get id
  49.      *
  50.      * @return integer 
  51.      */
  52.     public function getId()
  53.     {
  54.         return $this->id;
  55.     }
  56.     /**
  57.      * Set nom
  58.      *
  59.      * @param string $name
  60.      * @return Month
  61.      */
  62.     public function setName($name)
  63.     {
  64.         $this->name $name;
  65.         return $this;
  66.     }
  67.     /**
  68.      * Get name
  69.      *
  70.      * @return string 
  71.      */
  72.     public function getName()
  73.     {
  74.         return $this->name;
  75.     }
  76.     function __toString(): string
  77.     {
  78.         return $this->name;
  79.     }
  80.     /**
  81.      * Set course
  82.      *
  83.      *
  84.      * @return Month
  85.      */
  86.     public function setCourse(Course $course null)
  87.     {
  88.         $this->course $course;
  89.         return $this;
  90.     }
  91.     /**
  92.      * Get course
  93.      *
  94.      * @return \App\Entity\Tutoring\Course
  95.      */
  96.     public function getCourse()
  97.     {
  98.         return $this->course;
  99.     }
  100.     /**
  101.      * @return int
  102.      */
  103.     public function getPosition()
  104.     {
  105.         return $this->position;
  106.     }
  107.     /**
  108.      * @param int $position
  109.      * @return Month
  110.      */
  111.     public function setPosition($position)
  112.     {
  113.         $this->position $position;
  114.         return $this;
  115.     }
  116.     /**
  117.      * @return float
  118.      */
  119.     public function getPrice()
  120.     {
  121.         return $this->price;
  122.     }
  123.     /**
  124.      * @param float $price
  125.      * @return Month
  126.      */
  127.     public function setPrice($price)
  128.     {
  129.         $this->price $price;
  130.         return $this;
  131.     }
  132.     /**
  133.      * Add meeting
  134.      *
  135.      *
  136.      * @return Month
  137.      */
  138.     public function addMeeting(Meeting $meeting)
  139.     {
  140.         $meeting->setMonth($this);
  141.         $this->meetings[] = $meeting;
  142.         return $this;
  143.     }
  144.     /**
  145.      * Remove meeting
  146.      */
  147.     public function removeMeeting(Meeting $meeting)
  148.     {
  149.         $this->meetings->removeElement($meeting);
  150.     }
  151.     /**
  152.      * Get meetings
  153.      *
  154.      * @return \Doctrine\Common\Collections\Collection
  155.      */
  156.     public function getMeetings()
  157.     {
  158.         return $this->meetings;
  159.     }
  160.     public function getMeetingsByStatut($statut){
  161.         $items=new ArrayCollection();
  162.         switch ($statut){
  163.             case 'all':
  164.                 $items=$this->meetings;
  165.                 break;
  166.             case 'checked':
  167.                 /** @var Meeting $meeting */
  168.                 foreach ($this->getMeetings() as $meeting){
  169.                     if($meeting->isPublished()){
  170.                         $items->add($meeting);
  171.                     }
  172.                 }
  173.                 break;
  174.             case 'unchecked':
  175.                 /** @var Meeting $meeting */
  176.                 foreach ($this->getMeetings() as $meeting){
  177.                     if(!$meeting->isPublished()){
  178.                         $items->add($meeting);
  179.                     }
  180.                 }
  181.                 break;
  182.         }
  183.         return $items;
  184.     }
  185.     public function getMeetingsPassed()
  186.     {
  187.         $items=new ArrayCollection();
  188.         $today=new \DateTime('now');
  189.         /** @var Meeting $meeting */
  190.         foreach ($this->getMeetings() as $meeting){
  191.             if($meeting->getCreateAt()< $today){
  192.                 $items->add($meeting);
  193.             }
  194.         }
  195.         return $items;
  196.     }
  197.     /**
  198.      * Add owe
  199.      *
  200.      *
  201.      * @return Month
  202.      */
  203.     public function addOwe(Owe $owe)
  204.     {
  205.         $owe->setMonth($this);
  206.         $this->owes[] = $owe;
  207.         return $this;
  208.     }
  209.     /**
  210.      * Remove owe
  211.      */
  212.     public function removeOwe(Owe $owe)
  213.     {
  214.         $this->owes->removeElement($owe);
  215.     }
  216.     /**
  217.      * Get owes
  218.      *
  219.      * @return \Doctrine\Common\Collections\Collection
  220.      */
  221.     public function getOwes()
  222.     {
  223.         return $this->owes;
  224.     }
  225.     public function getStartAt(){
  226.         //
  227.         //$now=new \DateTime('now');
  228.         $format 'd-m-Y H:i:s';
  229.         if($this->name !=="FI"){
  230.             return \DateTime::createFromFormat($format'1-'.$this->getName().' 00:01:00');
  231.         }
  232.         return $this->course->getStartAt();
  233.     }
  234.     public function getHalf(){
  235.         
  236.         $format 'd-m-Y H:i:s';
  237.         return \DateTime::createFromFormat($format'10-'.$this->getName().' 00:00:00');
  238.     }
  239.     public function getEndAt(){
  240.         
  241.         $format 'd-m-Y H:i:s';
  242.         return \DateTime::createFromFormat($format$this->getStartAt()->format('t-m-Y').' 23:59:59');
  243.     }
  244.     public function isPassed(){
  245.         
  246.         $now=new \DateTime('now');
  247.         return $now>$this->getStartAt();
  248.     }
  249.     public function isOutDate(){
  250.         
  251.         $now=new \DateTime('now');
  252.         return $now>$this->getHalf();
  253.     }
  254.     public function dateInMonth(\DateTime $date){
  255.         return $date>$this->getStartAt() && $date<$this->getEndAt();
  256.     }
  257.     public function getTotalOwe(){
  258.         $total=0;
  259.         /** @var Owe $owe */
  260.         foreach ($this->owes as $owe){
  261.             $total+=$owe->getPrice();
  262.         }
  263.         return $total;
  264.     }
  265.     public function getTotalPaies(){
  266.         $total=0;
  267.         /** @var Owe $owe */
  268.         foreach ($this->owes as $owe){
  269.             $total+=$owe->getRowscourse()->getPaieMonth($owe)['amount'];
  270.         }
  271.         return $total;
  272.     }
  273.     function compareByCreatedAt($a$b) {
  274.         // Récupérer les valeurs de createdAt pour chaque objet
  275.         $createdAtA $a->getCreatedAt();
  276.         $createdAtB $b->getCreatedAt();
  277.         // Comparer les valeurs de createdAt
  278.         if ($createdAtA == $createdAtB) {
  279.             return 0;
  280.         }
  281.         return ($createdAtA $createdAtB) ? -1;
  282.     }
  283.     public function getMeetingsByDate()
  284.     {
  285.         $items=$this->meetings->toArray();
  286.         usort($items, fn(Meeting $aMeeting $b) => $a->getCreateAt() <=> $b->getCreateAt());
  287.         return $items;
  288.     }
  289. }