src/Entity/PurchaseItem.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\PurchaseItemRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Doctrine\DBAL\Types\Types;
  8. use App\Validator\Constraints as CustomAssert;
  9. #[ORM\Entity(repositoryClass: PurchaseItemRepository::class)]
  10. class PurchaseItem
  11. {
  12.    
  13.     #[ORM\Id]
  14.     #[ORM\GeneratedValue]
  15.     #[ORM\Column]
  16.     private ?int $id = null;
  17.     
  18.     #[ORM\Column(length: 255)]
  19.     private ?string $activityName = null;
  20.     #[ORM\Column]
  21.     private ?int $activityPrice = null;
  22.     
  23.     #[ORM\Column]
  24.     private ?string $activityCategoryTitle = null;
  25.     #[ORM\Column(type: Types::DATETIME_MUTABLE)]
  26.     private ?\DateTimeInterface $activityStart = null;
  27.     #[ORM\Column(type: Types::DATETIME_MUTABLE)]
  28.     private ?\DateTimeInterface $activityEnd = null;
  29.     #[ORM\Column]
  30.     private ?int $quantity = null;
  31.     #[ORM\Column]
  32.     private ?int $total = null;
  33.     #[ORM\ManyToOne(inversedBy: 'purchaseItems')]
  34.     private ?Activity $activity = null;
  35.     #[ORM\ManyToOne(inversedBy: 'purchaseItems')]
  36.     private ?Calendar $calendar = null;
  37.     #[ORM\ManyToOne(inversedBy: 'purchaseItems')]
  38.     #[ORM\JoinColumn(nullable: false)]
  39.     private ?Purchase $purchase = null;
  40.     #[ORM\ManyToMany(targetEntity: Payment::class, mappedBy: 'PurchaseItems')]
  41.     private Collection $payments;
  42.     public function __construct()
  43.     {
  44.         $this->payments = new ArrayCollection();
  45.     }
  46.  
  47.    // #[ORM\Column]
  48.    // private ?int $activitieStock = null;
  49.     
  50.    public function __toString()
  51.    {
  52.        return (string) $this->id; // or any other property you want to represent the entity with
  53.    }
  54.    
  55.     public function getId(): ?int
  56.     {
  57.         return $this->id;
  58.     }
  59.     public function getActivity(): ?Activity
  60.     {
  61.         return $this->activity;
  62.     }
  63.     public function setActivity(?Activity $activity): self
  64.     {
  65.         $this->activity = $activity;
  66.         return $this;
  67.     }
  68.     public function getActivityCategoryTitle(): ?string
  69.     {
  70.         return $this->activityCategoryTitle;
  71.     }
  72.     public function setActivityCategoryTitle(string $activityCategoryTitle): self
  73.     {
  74.         $this->activityCategoryTitle = $activityCategoryTitle;
  75.         return $this;
  76.     }
  77.     public function getPurchase(): ?Purchase
  78.     {
  79.         return $this->purchase;
  80.     }
  81.     public function setPurchase(?Purchase $purchase): self
  82.     {
  83.         $this->purchase = $purchase;
  84.         if(!$purchase->getPurchaseItems()->contains($this)) {
  85.             $purchase->addPurchaseItem($this);
  86.          }
  87.          
  88.         return $this;
  89.     }
  90.     public function getActivityName(): ?string
  91.     {
  92.         return $this->activityName;
  93.     }
  94.     public function setActivityName(string $activityName): self
  95.     {
  96.         $this->activityName = $activityName;
  97.         return $this;
  98.     }
  99.     public function getActivityStart(): ?\DateTimeInterface
  100.     {
  101.         return $this->activityStart;
  102.     }
  103.     public function setActivityStart(?\DateTimeInterface $activityStart): void
  104.     {
  105.         $this->activityStart = $activityStart;
  106.     }
  107.     public function getActivityEnd(): ?\DateTimeInterface
  108.     {
  109.         return $this->activityEnd;
  110.     }
  111.     public function setActivityEnd(?\DateTimeInterface $activityEnd): void
  112.     {
  113.         $this->activityEnd = $activityEnd;
  114.     }
  115.     public function getActivityPrice(): ?int
  116.     {
  117.         return $this->activityPrice;
  118.     }
  119.  
  120.     public function setActivityPrice(?int $activityPrice): self
  121.     {
  122.        if ($activityPrice === null) {
  123.            // Si le prix de l'activité est null, récupérez le prix par défaut de CategorySetting
  124.            $categorySetting = $this->getActivity()->getCategory()->getCategorySetting();
  125.            $activityPrice = $categorySetting ? $categorySetting->getPrice() : 0;
  126.        }
  127.        $this->activityPrice = $activityPrice;
  128.        return $this;
  129.      }
  130.     public function getCalendar(): ?Calendar
  131.     {
  132.         return $this->calendar;
  133.     }
  134.     public function setCalendar(?Calendar $calendar): self
  135.     {
  136.         $this->calendar = $calendar;
  137.         return $this;
  138.     }
  139.   
  140.     public function getQuantity(): ?int
  141.     {
  142.         return $this->quantity;
  143.     }
  144.     public function setQuantity(int $quantity): self
  145.     {
  146.         $this->quantity = $quantity;
  147.         return $this;
  148.     }
  149.     public function getNewQuantity(): ?int
  150.     {
  151.         return $this->quantity;
  152.     }
  153.     public function getTotal(): ?int
  154.     {
  155.         // Vérifiez d'abord si les deux valeurs sont définies
  156.         if ($this->activityPrice !== null && $this->quantity !== null) {
  157.             // Calculez le total en multipliant le prix de l'activité par la quantité
  158.             return $this->activityPrice * $this->quantity;
  159.         } else {
  160.             // Si l'une des valeurs est null, retournez null
  161.             return null;
  162.         }
  163.     }
  164.     public function setTotal(int $total): self
  165.     {
  166.         $this->total = $total;
  167.         return $this;
  168.     }
  169.     /**
  170.      * @return Collection<int, Payment>
  171.      */
  172.     public function getPayments(): Collection
  173.     {
  174.         if ($this->payments->isEmpty()) {
  175.             $defaultPayment = new Payment();
  176.             $defaultPayment->setValue(0.00);
  177.             return new ArrayCollection([$defaultPayment]);
  178.         }
  179.         return $this->payments;
  180.     }
  181.     public function addPayment(Payment $payment): static
  182.     {
  183.         if (!$this->payments->contains($payment)) {
  184.             $this->payments->add($payment);
  185.             $payment->addPurchaseItem($this);
  186.         }
  187.         return $this;
  188.     }
  189.     public function removePayment(Payment $payment): static
  190.     {
  191.         if ($this->payments->removeElement($payment)) {
  192.             $payment->removePurchaseItem($this);
  193.         }
  194.         return $this;
  195.     }
  196.   
  197.   
  198. }