src/Entity/PriceList.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use DateTime;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. /**
  9.  * @ORM\Entity(
  10.  *     repositoryClass="App\Repository\PriceListRepository"
  11.  * )
  12.  * @ORM\Table(
  13.  *     name="price_list",
  14.  *     options={"collate"="utf8_swedish_ci"}
  15.  * )
  16.  * @ORM\HasLifecycleCallbacks
  17.  */
  18. class PriceList implements EntityInterface
  19. {
  20.     /**
  21.      * @ORM\Id
  22.      * @ORM\Column(
  23.      *     type="integer"
  24.      * )
  25.      * @ORM\GeneratedValue(
  26.      *     strategy="AUTO"
  27.      * )
  28.      *
  29.      * @var int|null
  30.      */
  31.     protected ?int $id null;
  32.     /**
  33.      * @ORM\ManyToOne(
  34.      *     targetEntity="PriceGroup",
  35.      *     inversedBy="priceLists"
  36.      * )
  37.      * @ORM\JoinColumn(
  38.      *     name="price_group_id",
  39.      *     referencedColumnName="id",
  40.      *     onDelete="cascade"
  41.      * )
  42.      * @Assert\NotNull
  43.      *
  44.      * @var PriceGroup
  45.      */
  46.     protected PriceGroup $priceGroup;
  47.     /**
  48.      * @ORM\OneToMany(
  49.      *     targetEntity="Price",
  50.      *     mappedBy="priceList",
  51.      *     cascade={"persist","remove"}
  52.      * )
  53.      *
  54.      * @var Collection<Price>
  55.      */
  56.     protected $prices;
  57.     /**
  58.      * @ORM\Column(
  59.      *     name="valid_from",
  60.      *     type="datetime"
  61.      * )
  62.      * @Assert\NotNull
  63.      *
  64.      * @var DateTime
  65.      */
  66.     protected DateTime $validFrom;
  67.     /**
  68.      * @param PriceGroup $priceGroup
  69.      * @param DateTime   $validFrom
  70.      */
  71.     public function __construct(PriceGroup $priceGroupDateTime $validFrom)
  72.     {
  73.         $this->priceGroup $priceGroup;
  74.         $this->validFrom $validFrom;
  75.         $this->prices = new ArrayCollection();
  76.     }
  77.     /**
  78.      * @param Price $price
  79.      */
  80.     public function addPrice(Price $price): void
  81.     {
  82.         if ($this->prices === null) {
  83.             $this->prices = new ArrayCollection();
  84.         }
  85.         $price->setPriceList($this);
  86.         $this->prices->add($price);
  87.     }
  88.     /**
  89.      * @return int|null
  90.      */
  91.     public function getId(): ?int
  92.     {
  93.         return $this->id;
  94.     }
  95.     /**
  96.      * @param PricedInterface $product
  97.      *
  98.      * @return Price|null
  99.      */
  100.     public function getPrice(PricedInterface $product): ?Price
  101.     {
  102.         foreach ($this->getPrices() as $price) {
  103.             if ($product instanceof Product && ($priceProduct $price->getProduct()) !== null) {
  104.                 if ($priceProduct->getCode() == $product->getCode()) {
  105.                     return $price;
  106.                 }
  107.             } elseif ($product instanceof ProductVersion && ($priceProduct $price->getProductVersion()) !== null) {
  108.                 if ($priceProduct->getCode() == $product->getCode()) {
  109.                     return $price;
  110.                 }
  111.             }
  112.         }
  113.         return null;
  114.     }
  115.     /**
  116.      * @return PriceGroup
  117.      */
  118.     public function getPriceGroup(): PriceGroup
  119.     {
  120.         return $this->priceGroup;
  121.     }
  122.     /**
  123.      * @return Price[]
  124.      */
  125.     public function getPrices()
  126.     {
  127.         return $this->prices;
  128.     }
  129.     /**
  130.      * @return DateTime
  131.      */
  132.     public function getValidFrom(): DateTime
  133.     {
  134.         return $this->validFrom;
  135.     }
  136. }