<?php
namespace App\Entity;
use App\Model\Currency;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use InvalidArgumentException;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(
* repositoryClass="App\Repository\PriceGroupRepository"
* )
* @ORM\Table(
* name="price_group",
* options={"collate"="utf8_swedish_ci"},
* uniqueConstraints={
* @ORM\UniqueConstraint(
* name="price_group_label",
* columns={"label"}
* )
* }
* )
* @ORM\HasLifecycleCallbacks
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(
* name="entity",
* type="string",
* length=50
* )
* @ORM\DiscriminatorMap({
* "purchase"="PurchasePriceGroup",
* "sales"="SalesPriceGroup",
* "company"="CompanySalesPriceGroup"
* })
*/
abstract class PriceGroup implements EntityInterface
{
/**
* @ORM\Id
* @ORM\Column(
* type="integer"
* )
* @ORM\GeneratedValue(
* strategy="AUTO"
* )
*
* @var int|null
*/
protected ?int $id = null;
/**
* @ORM\Column(
* type="string",
* length=3
* )
* @Assert\NotNull
* @Assert\Choice(
* callback={"\App\Model\Currency", "getCodeChoices"}
* )
*
* @var string
*/
protected string $currency = Currency::DEFAULT_CURRENCY;
/**
* @ORM\Column(
* type="string",
* length=50
* )
* @Assert\NotNull
*
* @var string
*/
protected string $label;
/**
* @ORM\Column(
* type="string",
* length=200
* )
* @Assert\NotNull
*
* @var string
*/
protected string $name;
/**
* @ORM\OneToMany(
* targetEntity="PriceList",
* mappedBy="priceGroup",
* cascade={"persist","remove"}
* )
* @orm\OrderBy({"validFrom" = "DESC"})
*
* @var Collection<PriceList>
*/
protected $priceLists;
/**
* @var ArrayCollection
*/
protected ArrayCollection $replacingPriceLists;
/**
* @param string $name
* @param string $label
* @param string $currency
*/
public function __construct(string $name, string $label, string $currency)
{
$this->setName($name);
$this->setLabel($label);
$this->setCurrency($currency);
$this->priceLists = new ArrayCollection();
$this->replacingPriceLists = new ArrayCollection();
}
/**
* @return string
*/
public function __toString(): string
{
return $this->name . ($this->currency != Currency::DEFAULT_CURRENCY ? ' (' . $this->currency . ')' : '');
}
/**
* @param PriceList $priceList
*
* @throws InvalidArgumentException
*/
public function addReplacingPriceList(PriceList $priceList)
{
if ($priceList->getPriceGroup() !== $this) {
throw new InvalidArgumentException('The price list must be a member of the same price group.');
}
$this->replacingPriceLists->add($priceList);
}
/**
* @return string
*/
public function getCurrency(): string
{
return $this->currency;
}
/**
* @return PriceList|null
*/
public function getCurrentPriceList(): ?PriceList
{
/* @var PriceList $currentPriceList */
$currentPriceList = null;
foreach ($this->getPriceLists() as $priceList) {
if (($timestamp = $priceList->getValidFrom()->getTimestamp()) <= time()) {
if ($currentPriceList === null || $timestamp > $currentPriceList->getValidFrom()->getTimestamp()) {
$currentPriceList = $priceList;
}
}
}
return $currentPriceList;
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @return string
*/
public function getLabel(): string
{
return $this->label;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return Collection<PriceList>
*/
public function getPriceLists(): Collection
{
return $this->priceLists;
}
/**
* @return ArrayCollection<PriceList>
*/
public function getReplacingPriceLists(): ArrayCollection
{
return $this->replacingPriceLists;
}
/**
* @ORM\PostLoad
*/
public function postLoad(): void
{
$this->replacingPriceLists = new ArrayCollection();
}
/**
* @param string $currency
*/
public function setCurrency(string $currency): void
{
$this->currency = $currency;
}
/**
* @param string $label
*/
public function setLabel(string $label): void
{
$this->label = $label;
}
/**
* @param string $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
/**
* @param ArrayCollection<PriceList>$priceLists
*/
public function setPriceLists(ArrayCollection $priceLists): void
{
$this->priceLists = $priceLists;
}
}