src/Entity/Formation/Apayment.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Formation;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Repository\Formation\ApaymentRepository;
  5. use App\Traits\Actions;
  6. use App\Entity\Formation\Apayline;
  7. use App\Entity\Formation\Participant;
  8. use App\Entity\User;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. #[ORM\Entity(repositoryClassApaymentRepository::class)]
  13. #[ORM\Table(name'hs_tc_formation_apayment')]
  14. #[ApiResource]
  15. class Apayment implements \Stringable
  16. {
  17.     use Actions;
  18.     #[ORM\ManyToOne(targetEntity"Participant"inversedBy"payments")]
  19.     #[ORM\JoinColumn(nullablefalse)]
  20.     private Participant $participant;
  21.     #[ORM\ManyToOne(targetEntityUser::class, inversedBy"payments")]
  22.     #[ORM\JoinColumn(nullabletrue)]
  23.     private User $initie;
  24.     #[ORM\Column(name"costs"type"string"length255)]
  25.     private string $costs;
  26.     #[ORM\Column(name"way"type"string"length255nullabletrue)]
  27.     private ?string $way null;
  28.     #[ORM\Column(name"amount"type"float")]
  29.     private float $amount;
  30.     #[ORM\Column(name"doc_number"type"string"length255)]
  31.     private string $docNumber '----';
  32. //    #[ORM\Column(name: "cached", type: "boolean")]
  33. //    private bool $cached;
  34.     #[ORM\Column(type'string'length255)]
  35.     private string $moyen;
  36.     #[ORM\OneToMany(mappedBy"payment"targetEntityApayline::class, cascade: ["all"], orphanRemovaltrue)]
  37.     private Collection $items;
  38.     public function __construct()
  39.     {
  40.         // date_default_timezone_set('Africa/Casablanca');
  41.         $this->createAt=new \DateTime('now');
  42.         $this->published=false;
  43.         $this->items=new ArrayCollection();
  44.     }
  45.     /**
  46.      * @return string
  47.      */
  48.     public function getCosts(): ?string
  49.     {
  50.         return $this->costs;
  51.     }
  52.     /**
  53.      * @param string $costs
  54.      */
  55.     public function setCosts(?string $costs): Apayment
  56.     {
  57.         $this->costs $costs;
  58.         return $this;
  59.     }
  60.     /**
  61.      * Set way
  62.      *
  63.      * @param string $way
  64.      *
  65.      * @return Apayment
  66.      */
  67.     public function setWay($way)
  68.     {
  69.         $this->way $way;
  70.         return $this;
  71.     }
  72.     /**
  73.      * Get way
  74.      *
  75.      * @return string
  76.      */
  77.     public function getWay()
  78.     {
  79.         return $this->way;
  80.     }
  81.     /**
  82.      * Set amount
  83.      *
  84.      * @param float $amount
  85.      *
  86.      * @return Apayment
  87.      */
  88.     public function setAmount($amount)
  89.     {
  90.         $this->amount $amount;
  91.         return $this;
  92.     }
  93.     /**
  94.      * Get amount
  95.      *
  96.      * @return float
  97.      */
  98.     public function getAmount()
  99.     {
  100.         return $this->amount;
  101.     }
  102.     /**
  103.      * Set docNumber
  104.      *
  105.      * @param string $docNumber
  106.      *
  107.      * @return Apayment
  108.      */
  109.     public function setDocNumber($docNumber)
  110.     {
  111.         $this->docNumber $docNumber;
  112.         return $this;
  113.     }
  114.     /**
  115.      * Get docNumber
  116.      *
  117.      * @return string
  118.      */
  119.     public function getDocNumber()
  120.     {
  121.         return $this->docNumber;
  122.     }
  123.     public function getMoyen(): string
  124.     {
  125.         return $this->moyen;
  126.     }
  127.     public function setMoyen(string $moyen): Apayment
  128.     {
  129.         $this->moyen $moyen;
  130.         return $this;
  131.     }
  132.     /**
  133.      * Set cached
  134.      *
  135.      * @param boolean $cached
  136.      *
  137.      * @return Apayment
  138.      */
  139.     /**
  140.      * Set participant
  141.      *
  142.      *
  143.      * @return Apayment
  144.      */
  145.     public function setParticipant(Participant $participant)
  146.     {
  147.         $this->participant $participant;
  148.         return $this;
  149.     }
  150.     /**
  151.      * Get participant
  152.      *
  153.      * @return Participant
  154.      */
  155.     public function getParticipant()
  156.     {
  157.         return $this->participant;
  158.     }
  159.     /**
  160.      * Set initie
  161.      *
  162.      *
  163.      * @return Apayment
  164.      */
  165.     public function setInitie(User $initie)
  166.     {
  167.         $this->initie $initie;
  168.         return $this;
  169.     }
  170.     /**
  171.      * Get initie
  172.      *
  173.      * @return User
  174.      */
  175.     public function getInitie()
  176.     {
  177.         return $this->initie;
  178.     }
  179.     /**
  180.      * Add item
  181.      *
  182.      *
  183.      * @return Apayment
  184.      */
  185.     public function addItem(Apayline $item)
  186.     {
  187.         $item->setPayment($this);
  188.         $this->items->add($item);
  189.         return $this;
  190.     }
  191.     /**
  192.      * Remove item
  193.      */
  194.     public function removeItem(Apayline $item)
  195.     {
  196.         $this->items->removeElement($item);
  197.     }
  198.     /**
  199.      * Get items
  200.      *
  201.      * @return Collection
  202.      */
  203.     public function getItems()
  204.     {
  205.         return $this->items;
  206.     }
  207.     public function getPrevious(){
  208.         $items=[];
  209.         /** @var Apayment $payment */
  210.         foreach ($this->getParticipant()->getPayments() as $payment){
  211.             if($payment->getCreateAt()<=$this->getCreateAt() && $payment->getId()<$this->getId()){
  212.                 $items[]=$payment;
  213.             }
  214.         }
  215.         return $items;
  216.     }
  217.     public function getSumPrevious(){
  218.         $s=0;
  219.         /** @var Apayment $pay */
  220.         foreach ($this->getPrevious() as $pay){
  221.             if($pay->getCosts()==$this->getCosts()) $s+=$pay->getAmount();
  222.         }
  223.         return $s;
  224.     }
  225.     public function getTotalAmountCost(){
  226.         $m=0;
  227.         /** @var Apayline $item */
  228.         foreach ($this->items as $item){
  229.             $m+=$item->getCost()->getMontant();
  230.         }
  231.         return $m;
  232.     }
  233.     public function getPrinterAmount($amount){
  234.         if($this->costs=='Frais de formation' || $this->costs =='monthlyPayment') {
  235.             if ($this->participant->isDiscount()) {
  236.                 return $amount;
  237.             } else {
  238.                 return ceil($amount $this->items->count() * $this->participant->getClasse()->getMonthlyPayment() / $this->getTotalAmountCost());
  239.             }
  240.         }
  241.         else return $amount;
  242.     }
  243.     public function getMonthsPaid($format='long'){
  244.         $months=[];
  245.         $amount=$this->amount;
  246.         if($this->participant->getCostsActivity()!=0) {
  247.             //if($this->participant->isDiscount() || $this->participant->getMonthlyPayment()==$this->participant->getClasse()->getActivity()->getMonthlyPayment()){
  248.             $index intdiv($this->getSumPrevious(), $this->participant->getCostsActivity());
  249.             if ($this->getSumPrevious() % $this->participant->getCostsActivity() != 0) {
  250.                 $months[] = ['month' => $this->participant->getClasse()->getMount($index$format), 'amount' => $this->getPrinterAmount($this->participant->getCostsActivity() - ($this->getSumPrevious() % $this->participant->getCostsActivity()))];
  251.                 $index++;
  252.                 $amount -= $this->participant->getCostsActivity() - ($this->getSumPrevious() % $this->participant->getCostsActivity());
  253.             }
  254.             while ($amount 0) {
  255.                 if ($amount >= $this->participant->getCostsActivity()) {
  256.                     $months[] = ['month' => $this->participant->getClasse()->getMount($index$format), 'amount' => $this->getPrinterAmount($this->participant->getCostsActivity())];
  257.                 } else {
  258.                     $months[] = ['month' => $this->participant->getClasse()->getMount($index$format), 'amount' => $this->getPrinterAmount($amount)];
  259.                 }
  260.                 $amount -= $this->participant->getCostsActivity();
  261.                 $index++;
  262.             }
  263. //            }
  264. //            else{
  265. //                $index = intdiv($this->getSumPrevious(), $this->participant->getCostsActivity());
  266. //                if ($this->getSumPrevious() % $this->participant->getCostsActivity() != 0) {
  267. //                    $months[] = ['month' => $this->participant->getClasse()->getMount($index, $format), 'amount' => $this->participant->getCostsActivity() - ($this->getSumPrevious() % $this->participant->getCostsActivity())];
  268. //                    $index++;
  269. //                    $amount -= $this->participant->getCostsActivity() - ($this->getSumPrevious() % $this->participant->getCostsActivity());
  270. //                }
  271. //                while ($amount >= 0) {
  272. //                    if ($amount >= $this->participant->getCostsActivity()) {
  273. //                        $months[] = ['month' => $this->participant->getClasse()->getMount($index, $format), 'amount' => $this->participant->getClasse()->getActivity()->getMonthlyPayment()];
  274. //                    } else {
  275. //                        $months[] = ['month' => $this->participant->getClasse()->getMount($index, $format), 'amount' => $amount];
  276. //                    }
  277. //                    $amount -= $this->participant->getCostsActivity();
  278. //                    $index++;
  279. //                }
  280. //            }
  281.         }
  282.         return $months;
  283.     }
  284.     public function getMonthPaid(){
  285.         $nbr =$this->participant->getCostsActivity()!=0?intdiv($this->getSumPrevious()+$this->getAmount(), $this->participant->getCostsActivity()):0;
  286.         return $nbr-1;
  287.     }
  288.     public function getMonths(){
  289.         return $this->way;
  290.     }
  291.     public function __toString(): string
  292.     {
  293.         return
  294.             'Id:'.$this->id.'<br>'.
  295.             'Date:'.$this->getCreateAt()->format('d-m-Y').'<br>'.
  296.             'Stagiaire: '.$this->participant->getCompletName().'<br>'.
  297.             'Raison:'.'<br>'.
  298.             'Montant: '.$this->getAmount().' DH<br>'
  299.             ;
  300.     }
  301.     public function getWayPerso(){
  302.         $way='';
  303.         /** @var Apayline $item */
  304.         foreach ($this->getItems() as $key=>$item){
  305.             if($key==0$way.=$item->getCost()->getDisplayType(). ': '; else $way.=' - ';
  306.             $way.=$item->getCost()->getName().' ('.number_format($item->getAmount(), 2).' DH)';
  307.         }
  308.         return $way;
  309.     }
  310.     public function getWayPrinter(){
  311.         $way='';
  312.         /** @var Apayline $item */
  313.         foreach ($this->getItems() as $key=>$item){
  314.             if($key==0$way.=$item->getCost()->getDisplayType(). ': '; else $way.=' - ';
  315.             $way.=$item->getCost()->getName().' ('.number_format($item->getPrinterAmount(), 2).' DH)';
  316.         }
  317.         return $way;
  318.     }
  319.     public function setCreateAt2(\DateTime $dateTime): Apayment
  320.     {
  321.         $this->createAt=$dateTime;
  322.         return $this;
  323.     }
  324. }