<?php
namespace App\Entity;
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(
* repositoryClass="App\Repository\ProductRepository"
* )
* @ORM\Table(
* name="product",
* options={"collate"="utf8_swedish_ci"},
* uniqueConstraints={
* @ORM\UniqueConstraint(
* name="product_code",
* columns={"code"}
* )
* }
* )
* @ORM\HasLifecycleCallbacks
*/
class Product implements EntityInterface, PricedInterface
{
use ProductTrait;
const STATE_FIELD_MAINTENANCE = 'Field mainten.';
/**
* @ORM\Id
* @ORM\Column(
* type="integer"
* )
* @ORM\GeneratedValue(
* strategy="AUTO"
* )
*
* @var int|null
*/
protected ?int $id = null;
/**
* @ORM\OneToMany(
* targetEntity="Price",
* mappedBy="product",
* cascade={"persist","remove"}
* )
*
* @var Collection<Price>
*/
protected Collection $prices;
/**
* @ORM\OneToMany(
* targetEntity="ProductVersion",
* mappedBy="product",
* cascade={"persist","remove"}
* )
*
* @var Collection<ProductVersion>
*/
protected Collection $productVersions;
/**
* @ORM\Column(
* type="string",
* length=50,
* nullable=true
* )
*
* @var string|null
*/
protected ?string $state = null;
/**
* @ORM\ManyToOne(
* targetEntity="Supplier",
* inversedBy="products"
* )
* @ORM\JoinColumn(
* name="supplier_id",
* referencedColumnName="id",
* onDelete="SET NULL"
* )
*
* @var Supplier|null
*/
protected ?Supplier $supplier = null;
public function __construct()
{
$this->prices = new ArrayCollection();
$this->productVersions = new ArrayCollection();
}
/**
* @param ProductVersion $productVersion
*/
public function addProductVersion(ProductVersion $productVersion): void
{
$productVersion->setProduct($this);
$this->productVersions->add($productVersion);
}
/**
* @return string[]
*/
public static function getActiveStates(): array
{
return ['maintain', 'ramp-down'];
}
/**
* @return array<ProductVersion>
*/
public function getActiveProductVersions(): array
{
$activeStates = self::getActiveStates();
$versions = [];
foreach ($this->getProductVersions() as $version) {
$state = strtolower($version->getState());
if (in_array($state, $activeStates)) {
$versions[$state] = $version;
}
}
return $versions;
}
/**
* @return ProductVersion|null
*/
public function getCurrentProductVersion(): ?ProductVersion
{
$activeStates = self::getActiveStates();
$activeVersions = $this->getActiveProductVersions();
foreach ($activeStates as $activeState) {
if (array_key_exists($activeState, $activeVersions)) {
return $activeVersions[$activeState];
}
}
return null;
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @param Company|null $company
*
* @return Price|null
*/
public function getPrice(Company $company = null): ?Price
{
/* @var Price $price */
$price = null;
foreach ($this->getPrices() as $priceTmp) {
$priceList = $priceTmp->getPriceList();
if ($company !== null && ($companyPriceGroup = $company->getPriceGroup()) !== null) {
if ($priceList->getPriceGroup() === $companyPriceGroup) {
if (($validFrom = $priceList->getValidFrom()->getTimestamp()) < time()) {
if ($price === null || $validFrom > $price->getPriceList()->getValidFrom()->getTimestamp()) {
$price = $priceTmp;
}
}
}
} else {
if (($priceGroup = $priceList->getPriceGroup()) instanceof SalesPriceGroup) {
// The price group must have the same currency as the company
if ($company !== null && $company->getCurrency() != $priceGroup->getCurrency()) {
continue;
}
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->getProduct() === $this) {
return $price;
}
}
return null;
}
/**
* @return Collection<Price>
*/
public function getPrices(): Collection
{
return $this->prices;
}
/**
* @return Collection<ProductVersion>
*/
public function getProductVersions(): Collection
{
return $this->productVersions;
}
/**
* @return null|string
*/
public function getState(): ?string
{
return $this->state;
}
/**
* @return Supplier|null
*/
public function getSupplier(): ?Supplier
{
return $this->supplier;
}
/**
* @return bool
*/
public function isStateFieldMaintenance(): bool
{
return $this->state == self::STATE_FIELD_MAINTENANCE;
}
/**
* @param string|null $state
*/
public function setState(?string $state): void
{
$this->state = $state;
}
/**
* @param Supplier|null $supplier
*/
public function setSupplier(Supplier $supplier = null): void
{
$this->supplier = $supplier;
}
}