<?php
namespace App\Entity;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(
* name="product_version",
* options={"collate"="utf8_swedish_ci"}
* )
* @ORM\HasLifecycleCallbacks
*/
class ProductVersion implements EntityInterface, PricedInterface
{
/**
* @ORM\Id
* @ORM\Column(
* type="integer"
* )
* @ORM\GeneratedValue(
* strategy="AUTO"
* )
*
* @var int|null
*/
protected ?int $id = null;
/**
* @ORM\Column(
* type="string",
* length=100
* )
* @Assert\NotNull
*
* @var string
*/
protected string $code;
/**
* @ORM\Column(
* name="implementation_date",
* type="date",
* nullable=true
* )
*
* @var DateTime|null
*/
protected ?DateTime $implementationDate = null;
/**
* @ORM\OneToMany(
* targetEntity="Price",
* mappedBy="productVersion",
* cascade={"persist","remove"}
* )
*
* @var Collection<Price>
*/
protected $prices;
/**
* @ORM\ManyToOne(
* targetEntity="Product",
* inversedBy="productVersions",
* cascade={"persist"}
* )
* @ORM\JoinColumn(
* name="product_id",
* referencedColumnName="id",
* onDelete="cascade"
* )
*
* @Assert\NotNull
*
* @var Product
*/
protected Product $product;
/**
* @ORM\Column(
* type="string",
* length=50,
* nullable=true
* )
*
* @var string|null
*/
protected ?string $state = null;
/**
* @param Product $product
* @param string $code
*/
public function __construct(Product $product, string $code)
{
$this->product = $product;
$this->code = $code;
$this->prices = new ArrayCollection();
}
/**
* @return string
*/
public function getCode(): string
{
return $this->code;
}
/**
* @return DateTime|null
*/
public function getImplementationDate(): ?DateTime
{
return $this->implementationDate;
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @return Price|null
*/
public function getPrice(): ?Price
{
/* @var Price $price */
$price = null;
foreach ($this->getPrices() as $priceTmp) {
$priceList = $priceTmp->getPriceList();
if ($priceList->getPriceGroup() instanceof PurchasePriceGroup) {
if (($validFrom = $priceList->getValidFrom()->getTimestamp()) < time()) {
if ($price === null || $validFrom > $price->getPriceList()->getValidFrom()->getTimestamp()) {
$price = $priceTmp;
}
}
}
}
return $price;
}
/**
* @param PriceList $priceList
*
* @return Price|null
*/
public function getPriceFromPriceList(PriceList $priceList): ?Price
{
foreach ($priceList->getPrices() as $price) {
if ($price->getProductVersion() === $this) {
return $price;
}
}
return null;
}
/**
* @return Collection<Price>
*/
public function getPrices(): Collection
{
return $this->prices;
}
/**
* @return Product
*/
public function getProduct(): Product
{
return $this->product;
}
/**
* @return string
*/
public function getSelectLabel(): string
{
return $this->code . ': ' . $this->getState();
}
/**
* @return null|string
*/
public function getState(): ?string
{
return $this->state;
}
/**
* @return bool
*/
public function isStateMaintain(): bool
{
return strtolower($this->state) == 'maintain';
}
/**
* @param string $code
*/
public function setCode(string $code): void
{
$this->code = $code;
}
/**
* @param DateTime|null $implementationDate
*/
public function setImplementationDate(?DateTime $implementationDate): void
{
$this->implementationDate = $implementationDate;
}
/**
* @param Product $product
*/
public function setProduct(Product $product): void
{
$this->product = $product;
}
/**
* @param string|null $state
*/
public function setState(?string $state): void
{
$this->state = $state;
}
}