src/Entity/ProductVersion.php line 19

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.  * @ORM\Table(
  11.  *     name="product_version",
  12.  *     options={"collate"="utf8_swedish_ci"}
  13.  * )
  14.  * @ORM\HasLifecycleCallbacks
  15.  */
  16. class ProductVersion implements EntityInterfacePricedInterface
  17. {
  18.     /**
  19.      * @ORM\Id
  20.      * @ORM\Column(
  21.      *     type="integer"
  22.      * )
  23.      * @ORM\GeneratedValue(
  24.      *     strategy="AUTO"
  25.      * )
  26.      *
  27.      * @var int|null
  28.      */
  29.     protected ?int $id null;
  30.     /**
  31.      * @ORM\Column(
  32.      *     type="string",
  33.      *     length=100
  34.      * )
  35.      * @Assert\NotNull
  36.      *
  37.      * @var string
  38.      */
  39.     protected string $code;
  40.     /**
  41.      * @ORM\Column(
  42.      *     name="implementation_date",
  43.      *     type="date",
  44.      *     nullable=true
  45.      * )
  46.      *
  47.      * @var DateTime|null
  48.      */
  49.     protected ?DateTime $implementationDate null;
  50.     /**
  51.      * @ORM\OneToMany(
  52.      *     targetEntity="Price",
  53.      *     mappedBy="productVersion",
  54.      *     cascade={"persist","remove"}
  55.      * )
  56.      *
  57.      * @var Collection<Price>
  58.      */
  59.     protected $prices;
  60.     /**
  61.      * @ORM\ManyToOne(
  62.      *     targetEntity="Product",
  63.      *     inversedBy="productVersions",
  64.      *     cascade={"persist"}
  65.      * )
  66.      * @ORM\JoinColumn(
  67.      *     name="product_id",
  68.      *     referencedColumnName="id",
  69.      *     onDelete="cascade"
  70.      * )
  71.      *
  72.      * @Assert\NotNull
  73.      *
  74.      * @var Product
  75.      */
  76.     protected Product $product;
  77.     /**
  78.      * @ORM\Column(
  79.      *     type="string",
  80.      *     length=50,
  81.      *     nullable=true
  82.      * )
  83.      *
  84.      * @var string|null
  85.      */
  86.     protected ?string $state null;
  87.     /**
  88.      * @param Product $product
  89.      * @param string  $code
  90.      */
  91.     public function __construct(Product $productstring $code)
  92.     {
  93.         $this->product $product;
  94.         $this->code $code;
  95.         $this->prices = new ArrayCollection();
  96.     }
  97.     /**
  98.      * @return string
  99.      */
  100.     public function getCode(): string
  101.     {
  102.         return $this->code;
  103.     }
  104.     /**
  105.      * @return DateTime|null
  106.      */
  107.     public function getImplementationDate(): ?DateTime
  108.     {
  109.         return $this->implementationDate;
  110.     }
  111.     /**
  112.      * @return int|null
  113.      */
  114.     public function getId(): ?int
  115.     {
  116.         return $this->id;
  117.     }
  118.     /**
  119.      * @return Price|null
  120.      */
  121.     public function getPrice(): ?Price
  122.     {
  123.         /* @var Price $price */
  124.         $price null;
  125.         foreach ($this->getPrices() as $priceTmp) {
  126.             $priceList $priceTmp->getPriceList();
  127.             if ($priceList->getPriceGroup() instanceof PurchasePriceGroup) {
  128.                 if (($validFrom $priceList->getValidFrom()->getTimestamp()) < time()) {
  129.                     if ($price === null || $validFrom $price->getPriceList()->getValidFrom()->getTimestamp()) {
  130.                         $price $priceTmp;
  131.                     }
  132.                 }
  133.             }
  134.         }
  135.         return $price;
  136.     }
  137.     /**
  138.      * @param PriceList $priceList
  139.      *
  140.      * @return Price|null
  141.      */
  142.     public function getPriceFromPriceList(PriceList $priceList): ?Price
  143.     {
  144.         foreach ($priceList->getPrices() as $price) {
  145.             if ($price->getProductVersion() === $this) {
  146.                 return $price;
  147.             }
  148.         }
  149.         return null;
  150.     }
  151.     /**
  152.      * @return Collection<Price>
  153.      */
  154.     public function getPrices(): Collection
  155.     {
  156.         return $this->prices;
  157.     }
  158.     /**
  159.      * @return Product
  160.      */
  161.     public function getProduct(): Product
  162.     {
  163.         return $this->product;
  164.     }
  165.     /**
  166.      * @return string
  167.      */
  168.     public function getSelectLabel(): string
  169.     {
  170.         return $this->code ': ' $this->getState();
  171.     }
  172.     /**
  173.      * @return null|string
  174.      */
  175.     public function getState(): ?string
  176.     {
  177.         return $this->state;
  178.     }
  179.     /**
  180.      * @return bool
  181.      */
  182.     public function isStateMaintain(): bool
  183.     {
  184.         return strtolower($this->state) == 'maintain';
  185.     }
  186.     /**
  187.      * @param string $code
  188.      */
  189.     public function setCode(string $code): void
  190.     {
  191.         $this->code $code;
  192.     }
  193.     /**
  194.      * @param DateTime|null $implementationDate
  195.      */
  196.     public function setImplementationDate(?DateTime $implementationDate): void
  197.     {
  198.         $this->implementationDate $implementationDate;
  199.     }
  200.     /**
  201.      * @param Product $product
  202.      */
  203.     public function setProduct(Product $product): void
  204.     {
  205.         $this->product $product;
  206.     }
  207.     /**
  208.      * @param string|null $state
  209.      */
  210.     public function setState(?string $state): void
  211.     {
  212.         $this->state $state;
  213.     }
  214. }