src/Entity/Tutoring/Rowscourse.php line 27

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: dada
  5.  * Date: 12/07/19
  6.  * Time: 20:34
  7.  */
  8. namespace App\Entity\Tutoring;
  9. use ApiPlatform\Core\Annotation\ApiResource;
  10. use App\Repository\Tutoring\RowscourseRepository;
  11. use App\Traits\Actions;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Doctrine\ORM\Mapping as ORM;
  15. use Doctrine\ORM\Mapping\OrderBy;
  16. /**
  17.  * Rowscourse
  18.  *
  19.  * @ORM\HasLifecycleCallbacks()
  20.  */
  21. #[ORM\Entity(repositoryClassRowscourseRepository::class)]
  22. #[ORM\Table(name'hs_tc_tutoring_rowscourse')]
  23. #[ApiResource]
  24. class Rowscourse implements \Stringable
  25. {
  26.     use Actions;
  27.     #[ORM\ManyToOne(targetEntityCourse::class, inversedBy'rowscourses')]
  28.     #[ORM\JoinColumn(nullabletrue)]
  29.     private $course;
  30.     #[ORM\ManyToOne(targetEntityStudent::class, cascade: ['persist'], inversedBy'rowscourses')]
  31.     #[ORM\JoinColumn(nullablefalse)]
  32.     private $student;
  33.     /**
  34.      * @var float
  35.      */
  36.     #[ORM\Column(name'price'type'float'nullabletrue)]
  37.     private $price;
  38.     #[ORM\Column(name'fees_registration'type'float'nullabletrue)]
  39.     private ?float $feesRegistration 0;
  40.     /**
  41.      * @var \DateTime
  42.      */
  43.     #[ORM\Column(name'start_at'type'datetime')]
  44.     private $startAt;
  45.     /**
  46.      * @var \DateTime
  47.      */
  48.     #[ORM\Column(name'end_at'type'datetime')]
  49.     private $endAt;
  50.     /**
  51.      * @OrderBy({"position" = "ASC"})
  52.      */
  53.     #[ORM\OneToMany(mappedBy'rowscourse'targetEntityOwe::class, cascade: ['persist'], orphanRemovaltrue)]
  54.     private ?Collection $owes null;
  55.     /**
  56.      * @OrderBy({"meeting" = "ASC"})
  57.      */
  58.     #[ORM\OneToMany(mappedBy'rowscourse'targetEntityPresence::class, cascade: ['persist'], orphanRemovaltrue)]
  59.     private ?Collection $presences null;
  60.     /**
  61.      * @OrderBy({"createAt" = "ASC"})
  62.      */
  63.     #[ORM\OneToMany(mappedBy'rowscourse'targetEntitySpayment::class, cascade: ['persist'], orphanRemovaltrue)]
  64.     private ?Collection $payments null;
  65.     public function __construct()
  66.     {
  67.         
  68.         $this->owes=new ArrayCollection();
  69.         $this->presences=new ArrayCollection();
  70.         $this->payments=new ArrayCollection();
  71.         $this->createAt=new \DateTime('now');
  72.         $this->published=true;
  73.     }
  74.     /**
  75.      * @return Course
  76.      */
  77.     public function getCourse()
  78.     {
  79.         return $this->course;
  80.     }
  81.     /**
  82.      * @return Rowscourse
  83.      */
  84.     public function setCourse(mixed $course)
  85.     {
  86.         $this->course $course;
  87.         return $this;
  88.     }
  89.     /**
  90.      * @return float
  91.      */
  92.     public function getPrice()
  93.     {
  94.         return $this->price;
  95.     }
  96.     /**
  97.      * @param float $price
  98.      * @return Rowscourse
  99.      */
  100.     public function setPrice($price)
  101.     {
  102.         $this->price $price;
  103.         return $this;
  104.     }
  105.     public function getFeesRegistration(): ?float
  106.     {
  107.         return $this->feesRegistration;
  108.     }
  109.     public function setFeesRegistration(?float $feesRegistration): Rowscourse
  110.     {
  111.         $this->feesRegistration $feesRegistration;
  112.         return $this;
  113.     }
  114.     /**
  115.      * @return Student
  116.      */
  117.     public function getStudent()
  118.     {
  119.         return $this->student;
  120.     }
  121.     /**
  122.      * @return Rowscourse
  123.      */
  124.     public function setStudent(mixed $student)
  125.     {
  126.         $this->student $student;
  127.         return $this;
  128.     }
  129.     /**
  130.      * @return \DateTime
  131.      */
  132.     public function getStartAt()
  133.     {
  134.         return $this->startAt;
  135.     }
  136.     /**
  137.      * @param \DateTime $startAt
  138.      * @return Rowscourse
  139.      */
  140.     public function setStartAt($startAt)
  141.     {
  142.         $this->startAt $startAt;
  143.         return $this;
  144.     }
  145.     /**
  146.      * @return \DateTime
  147.      */
  148.     public function getEndAt()
  149.     {
  150.         return $this->endAt;
  151.     }
  152.     /**
  153.      * @param \DateTime $endAt
  154.      * @return Rowscourse
  155.      */
  156.     public function setEndAt($endAt)
  157.     {
  158.         $this->endAt $endAt;
  159.         return $this;
  160.     }
  161.     /**
  162.      * Add payment
  163.      *
  164.      *
  165.      * @return Rowscourse
  166.      */
  167.     public function addPayment(Spayment $payment)
  168.     {
  169.         $payment->setRowscourse($this);
  170.         $this->payments[] = $payment;
  171.         return $this;
  172.     }
  173.     /**
  174.      * Remove payment
  175.      */
  176.     public function removePayment(Spayment $payment)
  177.     {
  178.         $this->payments->removeElement($payment);
  179.     }
  180.     /**
  181.      * Get payments
  182.      *
  183.      * @return \Doctrine\Common\Collections\Collection
  184.      */
  185.     public function getPayments()
  186.     {
  187.         return $this->payments;
  188.     }
  189.     /**
  190.      * Add owe
  191.      *
  192.      *
  193.      * @return Rowscourse
  194.      */
  195.     public function addOwe(Owe $owe)
  196.     {
  197.         $owe->setRowscourse($this);
  198.         $this->owes[] = $owe;
  199.         return $this;
  200.     }
  201.     /**
  202.      * Remove owe
  203.      */
  204.     public function removeOwe(Owe $owe)
  205.     {
  206.         $this->owes->removeElement($owe);
  207.     }
  208.     /**
  209.      * Get owes
  210.      *
  211.      * @return \Doctrine\Common\Collections\Collection
  212.      */
  213.     public function getOwes()
  214.     {
  215.         return $this->owes;
  216.     }
  217.     /**
  218.      * Add presence
  219.      *
  220.      *
  221.      * @return Rowscourse
  222.      */
  223.     public function addPresence(Presence $presence)
  224.     {
  225.         $presence->setRowscourse($this);
  226.         $this->presences[] = $presence;
  227.         return $this;
  228.     }
  229.     /**
  230.      * Remove presence
  231.      */
  232.     public function removePresence(Presence $presence)
  233.     {
  234.         $this->presences->removeElement($presence);
  235.     }
  236.     /**
  237.      * Get presences
  238.      *
  239.      * @return \Doctrine\Common\Collections\Collection
  240.      */
  241.     public function getPresences()
  242.     {
  243.         return $this->presences;
  244.     }
  245.     /*public function getMonths(){
  246.         
  247.         $items=array();
  248.         $date= clone $this->startAt;
  249.         while ($date<$this->endAt){
  250.             $month=$date->format('m-Y');
  251.             $items[]=$month;
  252.             $date->modify('+1 month');
  253.         }
  254.         return $items;
  255.     }*/
  256. /*    public function getIndexMonthCurrent(){
  257.         
  258.         $new=new \DateTime('now');
  259.         $month=$new->format('m-Y');
  260.         if (array_search($month, $this->getMonths()))
  261.             return array_search($month, $this->getMonths());
  262.         else
  263.             if ($new<$this->startAt) return -1;
  264.             else return -2;
  265.     }*/
  266.     public function isStillInMonth(Month $month){
  267.         if ($month->getEndAt()<$this->startAt || $month->getStartAt()>$this->endAt) return false;
  268.         else return true;
  269.     }
  270.     public function isStillInMonthText(string $monthText): bool
  271.     {
  272.         // Normaliser le séparateur
  273.         $monthText str_replace('/''-'$monthText);
  274.         // Créer la date de début du mois
  275.         $startMonth \DateTime::createFromFormat('m-Y'$monthText);
  276.         if (!$startMonth) return false;
  277.         // Cloner et aller au dernier jour du mois
  278.         $endMonth = clone $startMonth;
  279.         $endMonth->modify('last day of this month')->setTime(235959);
  280.         // Vérifier le chevauchement
  281.         if ($endMonth $this->startAt || $startMonth $this->endAt) return false;
  282.         return true;
  283.     }
  284.     public function getSumToPaid(){
  285.         if ($this->getCourse() === null) return 0;
  286.         $t=0;
  287.         /** @var Owe $m */
  288.         foreach ($this->getOwes() as $m){
  289.             if($m->getMonth()->getName()!='FI'){
  290.                 if($m->getMonth()->isPassed() && $this->getCourse()->isStillInMonth($m->getMonth()) && $this->isStillInMonth($m->getMonth()))
  291.                     $t+=$m->getPrice();
  292.             }
  293.             else{
  294.                 $t+=$m->getPrice();
  295.             }
  296.         }
  297.         return $t;
  298.     }
  299.     public function getSumToPayWithoutFI()
  300.     {
  301.         if ($this->getCourse() === null) return 0;
  302.         $t=0;
  303.         /** @var Owe $m */
  304.         foreach ($this->getOwes() as $m){
  305.             if($m->getMonth()->getName()!='FI'){
  306.                 if($m->getMonth()->isPassed() && $this->getCourse()->isStillInMonth($m->getMonth()) && $this->isStillInMonth($m->getMonth()))
  307.                     $t+=$m->getPrice();
  308.             }
  309.         }
  310.         return $t;
  311.     }
  312.     public function getOutDate(){
  313.         if ($this->getCourse() === null) return 0;
  314.         $t=0;
  315. //        /** @var Owe $m */
  316. //        foreach ($this->getOwes() as $m){
  317. //            if($m->getMonth()->isOutDate())
  318. //            $t+=$m->getPrice();
  319. //        }
  320.         foreach ($this->getOwes() as $m){
  321.             if($m->getMonth()->getName()!='FI'){
  322.                 if($m->getMonth()->isOutDate() && $this->getCourse()->isStillInMonth($m->getMonth()) && $this->isStillInMonth($m->getMonth()))
  323.                     $t+=$m->getPrice();
  324.             }
  325.             else{
  326.                 $t+=$m->getPrice();
  327.             }
  328.         }
  329.         return $t;
  330.     }
  331.     public function getSumPaid(){
  332.         $s=0;
  333.         /** @var Spayment $payment */
  334.         foreach ($this->getPayments() as $payment){
  335.                 $s+=$payment->getAmount();
  336.         }
  337.         return $s;
  338.     }
  339.     public function getSumPaidForTutor(){
  340.         $fi=0;
  341.         foreach ($this->getOwes() as $m){
  342.             if($m->getMonth()->getName()=='FI'$fi=$m->getPrice();
  343.         }
  344. //        $s=0;
  345. //        foreach ($this->getPayments() as $payment){
  346. //            if($payment->isPublished()) $s+=$payment->getAmount();
  347. //        }
  348.         return max(min($this->getSumToPayWithoutFI(), $this->getSumPaid())-$fi,0);
  349.     }
  350.     public function getRest(){
  351.         return ($this->getSumToPaid()-$this->getSumPaid())>0?($this->getSumToPaid()-$this->getSumPaid()):0;
  352.     }
  353.     public function getAdvance(){
  354.         return ($this->getSumToPaid()-$this->getSumPaid())<0?-($this->getSumToPaid()-$this->getSumPaid()):0;
  355.     }
  356.     public function getLatePaid(){
  357.         return ($this->getOutDate()-$this->getSumPaid())>0?($this->getOutDate()-$this->getSumPaid()):0;
  358.     }
  359.     public function getMonthsPaid(){
  360.         $months=[];
  361.         /** @var Owe $owe */
  362.         foreach ($this->owes as $owe){
  363.             if($owe->isPaied()){ 
  364.                 $months[]=$owe;
  365.             }
  366.         }
  367.         return $months;
  368.     }
  369.     public function getPaieMonth(Owe $owe){
  370.         return ['month'=>$owe->getMonth()->getName(), 'amount'=>$owe->amountPaied(), 'rest'=>0];
  371.     }
  372.     public function getNbrMonthPaid(){
  373.         return is_countable($this->getMonthsPaid()) ? count($this->getMonthsPaid()) : 0;
  374.     }
  375.     public function getListMonthPaid(){
  376.         $list='';
  377.         foreach ($this->getMonthsPaid() as $line){
  378.             $list.="<span class='label label-success' style='margin-left: 5px;'>"$line['month'] ."</span>";
  379.         }
  380.         return $list;
  381.     }
  382.     public function getPresence(Meeting $meeting){
  383.         /** @var Presence $presence */
  384.         foreach ($this->getPresences() as $presence){
  385.             if($presence->getMeeting()===$meeting) return $presence;
  386.         }
  387.         return null;
  388.     }
  389.     public function getAbsences()
  390.     {
  391.         $items=new ArrayCollection();
  392.         /** @var Presence $presence */
  393.         foreach ($this->getPresences() as $presence){
  394.             if(!$presence->isPublished() && $presence->getMeeting()->isPublished()){
  395.                 $items->add($presence);
  396.             }
  397.         }
  398.         return $items;
  399.     }
  400.     public function getNbrAbsences()
  401.     {
  402.         return $this->getAbsences()->count();
  403.     }
  404.     public function getMonthPresence(Month $month$statut){
  405.         $items=new ArrayCollection();
  406.         switch ($statut){
  407.             case 'all':
  408.                 /** @var Presence $presence */
  409.                 foreach ($this->getPresences() as $presence){
  410.                     if($presence->getMeeting()->getMonth()===$month && $presence->getMeeting()->isPublished()){
  411.                         $items->add($presence);
  412.                     }
  413.                 }
  414.                 break;
  415.             case 'checked':
  416.                 /** @var Presence $presence */
  417.                 foreach ($this->getPresences() as $presence){
  418.                     if($presence->getMeeting()->getMonth()===$month && $presence->isPublished() && $presence->getMeeting()->isPublished()){
  419.                         $items->add($presence);
  420.                     }
  421.                 }
  422.                 break;
  423.             case 'unchecked':
  424.                 /** @var Presence $presence */
  425.                 foreach ($this->getPresences() as $presence){
  426.                     if($presence->getMeeting()->getMonth()===$month && !$presence->isPublished() && $presence->getMeeting()->isPublished()){
  427.                         $items->add($presence);
  428.                     }
  429.                 }
  430.                 break;
  431.         }
  432.         return $items;
  433.     }
  434.     public function isAbandoned(Month $month){
  435.         if($this->getEndAt()<$month->getStartAt()) return true;
  436.         return false;
  437.     }
  438.     public function isAbandonedByDate($date){
  439.         $date=\DateTime::createFromFormat('d/m/Y'$date);
  440.         if($this->getEndAt()<$date) return true;
  441.         return false;
  442.     }
  443.     public function getOweByMonth($month){
  444.         /** @var Owe $owe */
  445.         foreach ($this->owes as $owe){
  446.             if($owe->getMonth()->getName()===$month) return $owe;
  447.         }
  448.         return null;
  449.     }
  450.     public function getSubject(){
  451.         if($this->course==null) return "Frais d'inscription";
  452.         return $this->getCourse()->getSubject();
  453.     }
  454.     public function __toString(): string
  455.     {
  456.         return $this->getCourse().'-->'.$this->getSubject();
  457.     }
  458.     public function getPcPay(){
  459.         if($this->getSumToPaid()!=0)
  460.             return round($this->getSumPaid()/$this->getSumToPaid()*100,0);
  461.         else
  462.             return 0;
  463.     }
  464.     public function getMonthByName($name)
  465.     {
  466.         foreach ($this->owes as $owe){
  467.             if ($owe->getMonth()->getName()==$name){
  468.                 return $owe->getMonth();
  469.             }
  470.         }
  471.         return null;
  472.     }
  473.     public function getOweByNameMonth($name)
  474.     {
  475.         foreach ($this->owes as $owe){
  476.             if ($owe->getMonth()->getName()==$name){
  477.                 return $owe;
  478.             }
  479.         }
  480.         return null;
  481.     }
  482.     public function getAmountPaidByMonth($month)
  483.     {
  484.         return $this->getOweByNameMonth($month)!==null?$this->getOweByNameMonth($month)->amountPaied():0;
  485.     }
  486.     public function getSimilarCourses(bool $tutor=false)
  487.     {
  488.         $courses = [];
  489.         foreach ($this->getCourse()->getSubject()->getCourses() as $course) {
  490.             if ($course->getId() != $this->getCourse()->getId()) {
  491.                 if ($course->getSchoolyear()->getId() == $this->getCourse()->getSchoolyear()->getId() && $course->getLevels()->contains($this->getStudent()->getLevel())) {
  492.                     if ($tutor) {
  493.                         if ($course->getTutor()->getId() == $this->getCourse()->getTutor()->getId()) {
  494.                             $courses[] = $course;
  495.                         }
  496.                     } else {
  497.                         $courses[] = $course;
  498.                     }
  499.                 }
  500.             }
  501.         }
  502.         return $courses;
  503.     }
  504. }