src/Entity/Product.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. /**
  8.  * @ORM\Entity(
  9.  *     repositoryClass="App\Repository\ProductRepository"
  10.  * )
  11.  * @ORM\Table(
  12.  *     name="product",
  13.  *     options={"collate"="utf8_swedish_ci"},
  14.  *     uniqueConstraints={
  15.  *         @ORM\UniqueConstraint(
  16.  *             name="product_code",
  17.  *             columns={"code"}
  18.  *         )
  19.  *     }
  20.  * )
  21.  * @ORM\HasLifecycleCallbacks
  22.  */
  23. class Product implements EntityInterfacePricedInterface
  24. {
  25.     use ProductTrait;
  26.     const STATE_FIELD_MAINTENANCE 'Field mainten.';
  27.     /**
  28.      * @ORM\Id
  29.      * @ORM\Column(
  30.      *     type="integer"
  31.      * )
  32.      * @ORM\GeneratedValue(
  33.      *     strategy="AUTO"
  34.      * )
  35.      *
  36.      * @var int|null
  37.      */
  38.     protected ?int $id null;
  39.     /**
  40.      * @ORM\OneToMany(
  41.      *     targetEntity="Price",
  42.      *     mappedBy="product",
  43.      *     cascade={"persist","remove"}
  44.      * )
  45.      *
  46.      * @var Collection<Price>
  47.      */
  48.     protected Collection $prices;
  49.     /**
  50.      * @ORM\OneToMany(
  51.      *     targetEntity="ProductVersion",
  52.      *     mappedBy="product",
  53.      *     cascade={"persist","remove"}
  54.      * )
  55.      *
  56.      * @var Collection<ProductVersion>
  57.      */
  58.     protected Collection $productVersions;
  59.     /**
  60.      * @ORM\Column(
  61.      *     type="string",
  62.      *     length=50,
  63.      *     nullable=true
  64.      * )
  65.      *
  66.      * @var string|null
  67.      */
  68.     protected ?string $state null;
  69.     /**
  70.      * @ORM\ManyToOne(
  71.      *     targetEntity="Supplier",
  72.      *     inversedBy="products"
  73.      * )
  74.      * @ORM\JoinColumn(
  75.      *     name="supplier_id",
  76.      *     referencedColumnName="id",
  77.      *     onDelete="SET NULL"
  78.      * )
  79.      *
  80.      * @var Supplier|null
  81.      */
  82.     protected ?Supplier $supplier null;
  83.     public function __construct()
  84.     {
  85.         $this->prices = new ArrayCollection();
  86.         $this->productVersions = new ArrayCollection();
  87.     }
  88.     /**
  89.      * @param ProductVersion $productVersion
  90.      */
  91.     public function addProductVersion(ProductVersion $productVersion): void
  92.     {
  93.         $productVersion->setProduct($this);
  94.         $this->productVersions->add($productVersion);
  95.     }
  96.     /**
  97.      * @return string[]
  98.      */
  99.     public static function getActiveStates(): array
  100.     {
  101.         return ['maintain''ramp-down'];
  102.     }
  103.     /**
  104.      * @return array<ProductVersion>
  105.      */
  106.     public function getActiveProductVersions(): array
  107.     {
  108.         $activeStates self::getActiveStates();
  109.         $versions = [];
  110.         foreach ($this->getProductVersions() as $version) {
  111.             $state strtolower($version->getState());
  112.             if (in_array($state$activeStates)) {
  113.                 $versions[$state] = $version;
  114.             }
  115.         }
  116.         return $versions;
  117.     }
  118.     /**
  119.      * @return ProductVersion|null
  120.      */
  121.     public function getCurrentProductVersion(): ?ProductVersion
  122.     {
  123.         $activeStates self::getActiveStates();
  124.         $activeVersions $this->getActiveProductVersions();
  125.         foreach ($activeStates as $activeState) {
  126.             if (array_key_exists($activeState$activeVersions)) {
  127.                 return $activeVersions[$activeState];
  128.             }
  129.         }
  130.         return null;
  131.     }
  132.     /**
  133.      * @return int|null
  134.      */
  135.     public function getId(): ?int
  136.     {
  137.         return $this->id;
  138.     }
  139.     /**
  140.      * @param Company|null $company
  141.      *
  142.      * @return Price|null
  143.      */
  144.     public function getPrice(Company $company null): ?Price
  145.     {
  146.         /* @var Price $price */
  147.         $price null;
  148.         foreach ($this->getPrices() as $priceTmp) {
  149.             $priceList $priceTmp->getPriceList();
  150.             if ($company !== null && ($companyPriceGroup $company->getPriceGroup()) !== null) {
  151.                 if ($priceList->getPriceGroup() === $companyPriceGroup) {
  152.                     if (($validFrom $priceList->getValidFrom()->getTimestamp()) < time()) {
  153.                         if ($price === null || $validFrom $price->getPriceList()->getValidFrom()->getTimestamp()) {
  154.                             $price $priceTmp;
  155.                         }
  156.                     }
  157.                 }
  158.             } else {
  159.                 if (($priceGroup $priceList->getPriceGroup()) instanceof SalesPriceGroup) {
  160.                     // The price group must have the same currency as the company
  161.                     if ($company !== null && $company->getCurrency() != $priceGroup->getCurrency()) {
  162.                         continue;
  163.                     }
  164.                     if (($validFrom $priceList->getValidFrom()->getTimestamp()) < time()) {
  165.                         if ($price === null || $validFrom $price->getPriceList()->getValidFrom()->getTimestamp()) {
  166.                             $price $priceTmp;
  167.                         }
  168.                     }
  169.                 }
  170.             }
  171.         }
  172.         return $price;
  173.     }
  174.     /**
  175.      * @param PriceList $priceList
  176.      *
  177.      * @return Price|null
  178.      */
  179.     public function getPriceFromPriceList(PriceList $priceList): ?Price
  180.     {
  181.         foreach ($priceList->getPrices() as $price) {
  182.             if ($price->getProduct() === $this) {
  183.                 return $price;
  184.             }
  185.         }
  186.         return null;
  187.     }
  188.     /**
  189.      * @return Collection<Price>
  190.      */
  191.     public function getPrices(): Collection
  192.     {
  193.         return $this->prices;
  194.     }
  195.     /**
  196.      * @return Collection<ProductVersion>
  197.      */
  198.     public function getProductVersions(): Collection
  199.     {
  200.         return $this->productVersions;
  201.     }
  202.     /**
  203.      * @return null|string
  204.      */
  205.     public function getState(): ?string
  206.     {
  207.         return $this->state;
  208.     }
  209.     /**
  210.      * @return Supplier|null
  211.      */
  212.     public function getSupplier(): ?Supplier
  213.     {
  214.         return $this->supplier;
  215.     }
  216.     /**
  217.      * @return bool
  218.      */
  219.     public function isStateFieldMaintenance(): bool
  220.     {
  221.         return $this->state == self::STATE_FIELD_MAINTENANCE;
  222.     }
  223.     /**
  224.      * @param string|null $state
  225.      */
  226.     public function setState(?string $state): void
  227.     {
  228.         $this->state $state;
  229.     }
  230.     /**
  231.      * @param Supplier|null $supplier
  232.      */
  233.     public function setSupplier(Supplier $supplier null): void
  234.     {
  235.         $this->supplier $supplier;
  236.     }
  237. }