src/Entity/ComponentInventoryItem.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ComponentInventoryItemRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. #[ORM\Entity(
  9.     repositoryClassComponentInventoryItemRepository::class
  10. )]
  11. #[ORM\Table(
  12.     name'component_inventory_item',
  13.     options: ['collate' => 'utf8_swedish_ci']
  14. )]
  15. #[ORM\UniqueConstraint(
  16.     name'product_code',
  17.     columns: ['code']
  18. )]
  19. class ComponentInventoryItem implements EntityInterface
  20. {
  21.     const TYPE_COMPONENT 'component';
  22.     const TYPE_SALES_ITEM 'sales_item';
  23.     const DESCRIPTION_MAX_LENGTH 80;
  24.     #[ORM\Id]
  25.     #[ORM\Column(
  26.         type'integer'
  27.     )]
  28.     #[ORM\GeneratedValue(
  29.         strategy'AUTO'
  30.     )]
  31.     protected ?int $id null;
  32.     #[ORM\Column(
  33.         type'string',
  34.         length100
  35.     )]
  36.     #[Assert\NotBlank]
  37.     protected string $code;
  38.     #[ORM\Column(
  39.         type'string',
  40.         length80,
  41.         nullabletrue
  42.     )]
  43.     protected ?string $description null;
  44.     /**
  45.      * @var Collection<ComponentInventoryLogEntry>|null
  46.      */
  47.     #[ORM\OneToMany(
  48.         mappedBy'item',
  49.         targetEntityComponentInventoryLogEntry::class,
  50.         cascade: ['persist''remove']
  51.     )]
  52.     #[ORM\OrderBy(['created' => 'DESC'])]
  53.     #[Assert\Valid]
  54.     protected ?Collection $logEntries null;
  55.     #[ORM\Column(
  56.         type'integer'
  57.     )]
  58.     #[Assert\NotNull]
  59.     #[Assert\PositiveOrZero]
  60.     protected int $quantity;
  61.     #[ORM\Column(
  62.         type'string',
  63.         length20,
  64.         nullabletrue
  65.     )]
  66.     protected string $type;
  67.     /**
  68.      * Unit price in euro cents with three decimals.
  69.      */
  70.     #[ORM\Column(
  71.         type'decimal',
  72.         precision13,
  73.         scale3,
  74.         nullabletrue
  75.     )]
  76.     #[Assert\NotNull]
  77.     #[Assert\PositiveOrZero]
  78.     protected string $unitPrice;
  79.     /**
  80.      * @param string $code
  81.      * @param int $quantity
  82.      * @param float $unitPrice
  83.      * @param string|null $description
  84.      * @param string $type
  85.      */
  86.     public function __construct(
  87.         string $code,
  88.         int $quantity,
  89.         float $unitPrice,
  90.         ?string $description,
  91.         string $type self::TYPE_COMPONENT
  92.     ) {
  93.         $this->code $code;
  94.         $this->description $description;
  95.         $this->quantity $quantity;
  96.         $this->setUnitPrice($unitPrice);
  97.         $this->type $type;
  98.     }
  99.     /**
  100.      * @param ComponentInventoryLogEntry $logEntry
  101.      */
  102.     public function addLogEntry(ComponentInventoryLogEntry $logEntry): void
  103.     {
  104.         if (null === $this->logEntries) {
  105.             $this->logEntries = new ArrayCollection();
  106.         }
  107.         $this->logEntries->add($logEntry);
  108.     }
  109.     /**
  110.      * @param int $currentQuantity
  111.      * @param int $increaseWithQuantity
  112.      * @param float $currentUnitPrice
  113.      * @param float $updatedUnitPrice
  114.      * @return float
  115.      */
  116.     public static function calculateUnitPrice(
  117.         int $currentQuantity,
  118.         int $increaseWithQuantity,
  119.         float $currentUnitPrice,
  120.         float $updatedUnitPrice
  121.     ): float {
  122.         $updatedQuantity $currentQuantity $increaseWithQuantity;
  123.         return ($currentQuantity $currentUnitPrice $increaseWithQuantity $updatedUnitPrice) / $updatedQuantity;
  124.     }
  125.     /**
  126.      * @return string
  127.      */
  128.     public function getCode(): string
  129.     {
  130.         return $this->code;
  131.     }
  132.     /**
  133.      * @return string|null
  134.      */
  135.     public function getDescription(): ?string
  136.     {
  137.         return $this->description;
  138.     }
  139.     /**
  140.      * @return int|null
  141.      */
  142.     public function getId(): ?int
  143.     {
  144.         return $this->id;
  145.     }
  146.     /**
  147.      * @return Collection<ComponentInventoryLogEntry>
  148.      */
  149.     public function getLogEntries(): Collection
  150.     {
  151.         return $this->logEntries;
  152.     }
  153.     /**
  154.      * @return int
  155.      */
  156.     public function getQuantity(): int
  157.     {
  158.         return $this->quantity;
  159.     }
  160.     /**
  161.      * @return float
  162.      */
  163.     public function getTotalValue(): float
  164.     {
  165.         return $this->quantity $this->getUnitPrice();
  166.     }
  167.     /**
  168.      * @return string
  169.      */
  170.     public function getType(): string
  171.     {
  172.         return $this->type;
  173.     }
  174.     /**
  175.      * @return string[]
  176.      */
  177.     public static function getTypeChoices(): array
  178.     {
  179.         return [
  180.             self::TYPE_COMPONENT,
  181.             self::TYPE_SALES_ITEM,
  182.         ];
  183.     }
  184.     /**
  185.      * @return float
  186.      */
  187.     public function getUnitPrice(): float
  188.     {
  189.         return (float) $this->unitPrice;
  190.     }
  191.     /**
  192.      * @param string $code
  193.      */
  194.     public function setCode(string $code): void
  195.     {
  196.         $this->code $code;
  197.     }
  198.     /**
  199.      * @param string|null $description
  200.      */
  201.     public function setDescription(?string $description): void
  202.     {
  203.         $this->description $description;
  204.     }
  205.     /**
  206.      * @param int $quantity
  207.      */
  208.     public function setQuantity(int $quantity): void
  209.     {
  210.         $this->quantity $quantity;
  211.     }
  212.     /**
  213.      * @param string $type
  214.      */
  215.     public function setType(string $type): void
  216.     {
  217.         $this->type $type;
  218.     }
  219.     /**
  220.      * @param float $unitPrice
  221.      */
  222.     public function setUnitPrice(float $unitPrice): void
  223.     {
  224.         $this->unitPrice number_format($unitPrice3'.''');
  225.     }
  226. }