src/Entity/PriceGroup.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Model\Currency;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use InvalidArgumentException;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. /**
  10.  * @ORM\Entity(
  11.  *     repositoryClass="App\Repository\PriceGroupRepository"
  12.  * )
  13.  * @ORM\Table(
  14.  *     name="price_group",
  15.  *     options={"collate"="utf8_swedish_ci"},
  16.  *     uniqueConstraints={
  17.  *         @ORM\UniqueConstraint(
  18.  *             name="price_group_label",
  19.  *             columns={"label"}
  20.  *         )
  21.  *     }
  22.  * )
  23.  * @ORM\HasLifecycleCallbacks
  24.  * @ORM\InheritanceType("SINGLE_TABLE")
  25.  * @ORM\DiscriminatorColumn(
  26.  *     name="entity",
  27.  *     type="string",
  28.  *     length=50
  29.  * )
  30.  * @ORM\DiscriminatorMap({
  31.  *     "purchase"="PurchasePriceGroup",
  32.  *     "sales"="SalesPriceGroup",
  33.  *     "company"="CompanySalesPriceGroup"
  34.  * })
  35.  */
  36. abstract class PriceGroup implements EntityInterface
  37. {
  38.     /**
  39.      * @ORM\Id
  40.      * @ORM\Column(
  41.      *     type="integer"
  42.      * )
  43.      * @ORM\GeneratedValue(
  44.      *     strategy="AUTO"
  45.      * )
  46.      *
  47.      * @var int|null
  48.      */
  49.     protected ?int $id null;
  50.     /**
  51.      * @ORM\Column(
  52.      *     type="string",
  53.      *     length=3
  54.      * )
  55.      * @Assert\NotNull
  56.      * @Assert\Choice(
  57.      *     callback={"\App\Model\Currency", "getCodeChoices"}
  58.      * )
  59.      *
  60.      * @var string
  61.      */
  62.     protected string $currency Currency::DEFAULT_CURRENCY;
  63.     /**
  64.      * @ORM\Column(
  65.      *     type="string",
  66.      *     length=50
  67.      * )
  68.      * @Assert\NotNull
  69.      *
  70.      * @var string
  71.      */
  72.     protected string $label;
  73.     /**
  74.      * @ORM\Column(
  75.      *     type="string",
  76.      *     length=200
  77.      * )
  78.      * @Assert\NotNull
  79.      *
  80.      * @var string
  81.      */
  82.     protected string $name;
  83.     /**
  84.      * @ORM\OneToMany(
  85.      *     targetEntity="PriceList",
  86.      *     mappedBy="priceGroup",
  87.      *     cascade={"persist","remove"}
  88.      * )
  89.      * @orm\OrderBy({"validFrom" = "DESC"})
  90.      *
  91.      * @var Collection<PriceList>
  92.      */
  93.     protected $priceLists;
  94.     /**
  95.      * @var ArrayCollection
  96.      */
  97.     protected ArrayCollection $replacingPriceLists;
  98.     /**
  99.      * @param string $name
  100.      * @param string $label
  101.      * @param string $currency
  102.      */
  103.     public function __construct(string $namestring $labelstring $currency)
  104.     {
  105.         $this->setName($name);
  106.         $this->setLabel($label);
  107.         $this->setCurrency($currency);
  108.         $this->priceLists = new ArrayCollection();
  109.         $this->replacingPriceLists = new ArrayCollection();
  110.     }
  111.     /**
  112.      * @return string
  113.      */
  114.     public function __toString(): string
  115.     {
  116.         return $this->name . ($this->currency != Currency::DEFAULT_CURRENCY ' (' $this->currency ')' '');
  117.     }
  118.     /**
  119.      * @param PriceList $priceList
  120.      *
  121.      * @throws InvalidArgumentException
  122.      */
  123.     public function addReplacingPriceList(PriceList $priceList)
  124.     {
  125.         if ($priceList->getPriceGroup() !== $this) {
  126.             throw new InvalidArgumentException('The price list must be a member of the same price group.');
  127.         }
  128.         $this->replacingPriceLists->add($priceList);
  129.     }
  130.     /**
  131.      * @return string
  132.      */
  133.     public function getCurrency(): string
  134.     {
  135.         return $this->currency;
  136.     }
  137.     /**
  138.      * @return PriceList|null
  139.      */
  140.     public function getCurrentPriceList(): ?PriceList
  141.     {
  142.         /* @var PriceList $currentPriceList */
  143.         $currentPriceList null;
  144.         foreach ($this->getPriceLists() as $priceList) {
  145.             if (($timestamp $priceList->getValidFrom()->getTimestamp()) <= time()) {
  146.                 if ($currentPriceList === null || $timestamp $currentPriceList->getValidFrom()->getTimestamp()) {
  147.                     $currentPriceList $priceList;
  148.                 }
  149.             }
  150.         }
  151.         return $currentPriceList;
  152.     }
  153.     /**
  154.      * @return int|null
  155.      */
  156.     public function getId(): ?int
  157.     {
  158.         return $this->id;
  159.     }
  160.     /**
  161.      * @return string
  162.      */
  163.     public function getLabel(): string
  164.     {
  165.         return $this->label;
  166.     }
  167.     /**
  168.      * @return string
  169.      */
  170.     public function getName(): string
  171.     {
  172.         return $this->name;
  173.     }
  174.     /**
  175.      * @return Collection<PriceList>
  176.      */
  177.     public function getPriceLists(): Collection
  178.     {
  179.         return $this->priceLists;
  180.     }
  181.     /**
  182.      * @return ArrayCollection<PriceList>
  183.      */
  184.     public function getReplacingPriceLists(): ArrayCollection
  185.     {
  186.         return $this->replacingPriceLists;
  187.     }
  188.     /**
  189.      * @ORM\PostLoad
  190.      */
  191.     public function postLoad(): void
  192.     {
  193.         $this->replacingPriceLists = new ArrayCollection();
  194.     }
  195.     /**
  196.      * @param string $currency
  197.      */
  198.     public function setCurrency(string $currency): void
  199.     {
  200.         $this->currency $currency;
  201.     }
  202.     /**
  203.      * @param string $label
  204.      */
  205.     public function setLabel(string $label): void
  206.     {
  207.         $this->label $label;
  208.     }
  209.     /**
  210.      * @param string $name
  211.      */
  212.     public function setName(string $name): void
  213.     {
  214.         $this->name $name;
  215.     }
  216.     /**
  217.      * @param ArrayCollection<PriceList>$priceLists
  218.      */
  219.     public function setPriceLists(ArrayCollection $priceLists): void
  220.     {
  221.         $this->priceLists $priceLists;
  222.     }
  223. }