2019-09-16 21:40:47 +02:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-02-22 18:14:36 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-09-16 21:40:47 +02:00
|
|
|
namespace App\Entity\Parts\PartTraits;
|
|
|
|
|
|
|
|
use App\Entity\Parts\MeasurementUnit;
|
|
|
|
use App\Entity\Parts\PartLot;
|
|
|
|
use Doctrine\Common\Collections\Collection;
|
2022-08-14 19:32:53 +02:00
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
2023-03-12 01:12:35 +01:00
|
|
|
use Symfony\Component\Serializer\Annotation\Groups;
|
2022-08-14 19:32:53 +02:00
|
|
|
use Symfony\Component\Validator\Constraints as Assert;
|
2019-09-16 21:40:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This trait collects all aspects of a part related to instock, part lots.
|
|
|
|
*/
|
|
|
|
trait InstockTrait
|
|
|
|
{
|
|
|
|
/**
|
2019-11-09 00:31:42 +01:00
|
|
|
* @var Collection|PartLot[] A list of part lots where this part is stored
|
2019-09-16 21:40:47 +02:00
|
|
|
*/
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Assert\Valid]
|
|
|
|
#[Groups(['extended', 'full', 'import'])]
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\OneToMany(targetEntity: 'PartLot', mappedBy: 'part', cascade: ['persist', 'remove'], orphanRemoval: true)]
|
|
|
|
#[ORM\OrderBy(['amount' => 'DESC'])]
|
|
|
|
protected \Doctrine\Common\Collections\Collection $partLots;
|
2019-09-16 21:40:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var float The minimum amount of the part that has to be instock, otherwise more is ordered.
|
2019-11-09 00:47:20 +01:00
|
|
|
* Given in the partUnit.
|
2019-09-16 21:40:47 +02:00
|
|
|
*/
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Assert\PositiveOrZero]
|
|
|
|
#[Groups(['extended', 'full', 'import'])]
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::FLOAT)]
|
2022-09-18 22:59:31 +02:00
|
|
|
protected float $minamount = 0;
|
2019-09-16 21:40:47 +02:00
|
|
|
|
|
|
|
/**
|
2020-01-04 20:24:09 +01:00
|
|
|
* @var ?MeasurementUnit the unit in which the part's amount is measured
|
2019-09-16 21:40:47 +02:00
|
|
|
*/
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Groups(['extended', 'full', 'import'])]
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\ManyToOne(targetEntity: 'MeasurementUnit')]
|
|
|
|
#[ORM\JoinColumn(name: 'id_part_unit')]
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?MeasurementUnit $partUnit = null;
|
2019-09-16 21:40:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all part lots where this part is stored.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-09-16 21:40:47 +02:00
|
|
|
* @return PartLot[]|Collection
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function getPartLots(): Collection
|
2019-09-16 21:40:47 +02:00
|
|
|
{
|
|
|
|
return $this->partLots;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the given part lot, to the list of part lots.
|
|
|
|
* The part lot is assigned to this part.
|
2020-03-15 13:56:31 +01:00
|
|
|
*
|
2020-02-02 14:05:36 +01:00
|
|
|
* @return $this
|
2019-09-16 21:40:47 +02:00
|
|
|
*/
|
|
|
|
public function addPartLot(PartLot $lot): self
|
|
|
|
{
|
|
|
|
$lot->setPart($this);
|
|
|
|
$this->partLots->add($lot);
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-09-16 21:40:47 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the given part lot from the list of part lots.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-01-04 20:24:09 +01:00
|
|
|
* @param PartLot $lot the part lot that should be deleted
|
2020-03-15 13:56:31 +01:00
|
|
|
*
|
2020-02-02 14:05:36 +01:00
|
|
|
* @return $this
|
2019-09-16 21:40:47 +02:00
|
|
|
*/
|
|
|
|
public function removePartLot(PartLot $lot): self
|
|
|
|
{
|
|
|
|
$this->partLots->removeElement($lot);
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-09-16 21:40:47 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the measurement unit in which the part's amount should be measured.
|
|
|
|
* Returns null if no specific unit was that. That means the parts are measured simply in quantity numbers.
|
|
|
|
*/
|
|
|
|
public function getPartUnit(): ?MeasurementUnit
|
|
|
|
{
|
|
|
|
return $this->partUnit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the measurement unit in which the part's amount should be measured.
|
|
|
|
* Set to null, if the part should be measured in quantities.
|
2020-03-15 13:56:31 +01:00
|
|
|
*
|
2020-02-02 14:05:36 +01:00
|
|
|
* @return $this
|
2019-09-16 21:40:47 +02:00
|
|
|
*/
|
|
|
|
public function setPartUnit(?MeasurementUnit $partUnit): self
|
|
|
|
{
|
|
|
|
$this->partUnit = $partUnit;
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-09-16 21:40:47 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the count of parts which must be in stock at least.
|
2023-04-15 23:14:53 +02:00
|
|
|
* If an integer-based part unit is selected, the value will be rounded to integers.
|
2019-09-16 21:40:47 +02:00
|
|
|
*
|
|
|
|
* @return float count of parts which must be in stock at least
|
|
|
|
*/
|
|
|
|
public function getMinAmount(): float
|
|
|
|
{
|
|
|
|
if ($this->useFloatAmount()) {
|
|
|
|
return $this->minamount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return round($this->minamount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if this part uses the float amount .
|
|
|
|
* This setting is based on the part unit (see MeasurementUnit->isInteger()).
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-09-16 21:40:47 +02:00
|
|
|
* @return bool True if the float amount field should be used. False if the integer instock field should be used.
|
|
|
|
*/
|
|
|
|
public function useFloatAmount(): bool
|
|
|
|
{
|
|
|
|
if ($this->partUnit instanceof MeasurementUnit) {
|
2020-08-21 21:36:22 +02:00
|
|
|
return !$this->partUnit->isInteger();
|
2019-09-16 21:40:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//When no part unit is set, treat it as part count, and so use the integer value.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-03 23:15:29 +02:00
|
|
|
/**
|
|
|
|
* Returns true, if the total instock amount of this part is less than the minimum amount.
|
|
|
|
*/
|
|
|
|
public function isNotEnoughInstock(): bool
|
|
|
|
{
|
|
|
|
return $this->getAmountSum() < $this->getMinAmount();
|
|
|
|
}
|
|
|
|
|
2023-04-29 22:33:46 +02:00
|
|
|
/**
|
|
|
|
* Returns true, if at least one of the part lots has an unknown amount.
|
|
|
|
* It is possible that other part lots have a known amount, then getAmountSum() will return sum of all known amounts.
|
|
|
|
* @return bool True if at least one part lot has an unknown amount.
|
|
|
|
*/
|
|
|
|
public function isAmountUnknown(): bool
|
|
|
|
{
|
|
|
|
foreach ($this->getPartLots() as $lot) {
|
|
|
|
if ($lot->isInstockUnknown()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-16 21:40:47 +02:00
|
|
|
/**
|
|
|
|
* Returns the summed amount of this part (over all part lots)
|
2023-04-29 22:33:46 +02:00
|
|
|
* Part Lots that have unknown value or are expired, are not used for this value (counted as 0).
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-09-16 21:40:47 +02:00
|
|
|
* @return float The amount of parts given in partUnit
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function getAmountSum(): float
|
2019-09-16 21:40:47 +02:00
|
|
|
{
|
|
|
|
//TODO: Find a method to do this natively in SQL, the current method could be a bit slow
|
|
|
|
$sum = 0;
|
|
|
|
foreach ($this->getPartLots() as $lot) {
|
2023-04-15 23:14:53 +02:00
|
|
|
//Don't use the in stock value, if it is unknown
|
2019-11-09 00:31:42 +01:00
|
|
|
if ($lot->isInstockUnknown() || $lot->isExpired() ?? false) {
|
2019-09-16 21:40:47 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sum += $lot->getAmount();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->useFloatAmount()) {
|
|
|
|
return $sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
return round($sum);
|
|
|
|
}
|
|
|
|
|
2022-11-28 23:59:01 +01:00
|
|
|
/**
|
|
|
|
* Returns the summed amount of all part lots that are expired. If no part lots are expired 0 is returned.
|
|
|
|
*/
|
|
|
|
public function getExpiredAmountSum(): float
|
|
|
|
{
|
|
|
|
$sum = 0.0;
|
|
|
|
foreach ($this->getPartLots() as $lot) {
|
|
|
|
if ($lot->isExpired() && !$lot->isInstockUnknown()) {
|
|
|
|
$sum += $lot->getAmount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->useFloatAmount()) {
|
|
|
|
return $sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
return round($sum);
|
|
|
|
}
|
|
|
|
|
2019-09-16 21:40:47 +02:00
|
|
|
/**
|
|
|
|
* Set the minimum amount of parts that have to be instock.
|
|
|
|
* See getPartUnit() for the associated unit.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-09-16 22:04:59 +02:00
|
|
|
* @param float $new_minamount the new count of parts which should be in stock at least
|
2020-03-15 13:56:31 +01:00
|
|
|
*
|
2020-02-02 14:05:36 +01:00
|
|
|
* @return $this
|
2019-09-16 21:40:47 +02:00
|
|
|
*/
|
|
|
|
public function setMinAmount(float $new_minamount): self
|
|
|
|
{
|
|
|
|
$this->minamount = $new_minamount;
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-09-16 21:40:47 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|