2019-09-04 23:20:10 +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-04 23:20:10 +02:00
|
|
|
namespace App\Validator\Constraints;
|
|
|
|
|
|
|
|
use App\Entity\Parts\PartLot;
|
2020-05-16 20:53:35 +02:00
|
|
|
use App\Entity\Parts\Storelocation;
|
2020-05-17 20:58:58 +02:00
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
2019-09-04 23:20:10 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
|
|
|
use Symfony\Component\Validator\Constraint;
|
|
|
|
use Symfony\Component\Validator\ConstraintValidator;
|
|
|
|
|
|
|
|
class ValidPartLotValidator extends ConstraintValidator
|
|
|
|
{
|
2022-09-18 22:59:31 +02:00
|
|
|
protected EntityManagerInterface $em;
|
2019-09-04 23:20:10 +02:00
|
|
|
|
|
|
|
public function __construct(EntityManagerInterface $em)
|
|
|
|
{
|
|
|
|
$this->em = $em;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the passed value is valid.
|
|
|
|
*
|
2019-11-09 00:47:20 +01:00
|
|
|
* @param mixed $value The value that should be validated
|
2019-09-04 23:20:10 +02:00
|
|
|
* @param Constraint $constraint The constraint for the validation
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
public function validate($value, Constraint $constraint): void
|
2019-09-04 23:20:10 +02:00
|
|
|
{
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!$constraint instanceof ValidPartLot) {
|
2019-09-04 23:20:10 +02:00
|
|
|
throw new UnexpectedTypeException($constraint, ValidPartLot::class);
|
|
|
|
}
|
|
|
|
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!$value instanceof PartLot) {
|
2019-09-04 23:20:10 +02:00
|
|
|
throw new UnexpectedTypeException($value, PartLot::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
//We can only validate the values if we know the storelocation
|
|
|
|
if ($value->getStorageLocation()) {
|
2020-05-16 20:53:35 +02:00
|
|
|
$repo = $this->em->getRepository(Storelocation::class);
|
2020-05-17 20:58:58 +02:00
|
|
|
//We can only determine associated parts, if the part have an ID
|
2023-01-29 20:42:18 +01:00
|
|
|
//When the storage location is new (no ID), we can just assume there are no other parts
|
|
|
|
if (null !== $value->getID() && $value->getStorageLocation()->getID()) {
|
2020-06-13 22:19:03 +02:00
|
|
|
$parts = new ArrayCollection($repo->getParts($value->getStorageLocation()));
|
2020-05-17 20:58:58 +02:00
|
|
|
} else {
|
|
|
|
$parts = new ArrayCollection([]);
|
|
|
|
}
|
2019-09-04 23:20:10 +02:00
|
|
|
|
|
|
|
//Check for isFull() attribute
|
|
|
|
if ($value->getStorageLocation()->isFull()) {
|
|
|
|
//Compare with saved amount value
|
|
|
|
$db_lot = $this->em->getUnitOfWork()->getOriginalEntityData($value);
|
|
|
|
|
|
|
|
//Amount increasment is not allowed
|
|
|
|
if ($db_lot && $value->getAmount() > $db_lot['amount']) {
|
2020-06-13 22:29:32 +02:00
|
|
|
$this->context->buildViolation('validator.part_lot.location_full.no_increase')
|
|
|
|
->setParameter('{{ old_amount }}', (string) $db_lot['amount'])
|
2019-09-04 23:20:10 +02:00
|
|
|
->atPath('amount')->addViolation();
|
|
|
|
}
|
|
|
|
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!$parts->contains($value->getPart())) {
|
2019-09-04 23:20:10 +02:00
|
|
|
$this->context->buildViolation('validator.part_lot.location_full')
|
|
|
|
->atPath('storage_location')->addViolation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check for onlyExisting
|
2020-08-21 22:43:37 +02:00
|
|
|
if ($value->getStorageLocation()->isLimitToExistingParts() && !$parts->contains($value->getPart())) {
|
|
|
|
$this->context->buildViolation('validator.part_lot.only_existing')
|
|
|
|
->atPath('storage_location')->addViolation();
|
2019-09-04 23:20:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Check for only single part
|
2020-08-21 22:43:37 +02:00
|
|
|
if ($value->getStorageLocation()->isOnlySinglePart() && ($parts->count() > 0) && !$parts->contains(
|
|
|
|
$value->getPart()
|
|
|
|
)) {
|
2020-08-21 22:44:38 +02:00
|
|
|
$this->context->buildViolation('validator.part_lot.single_part')
|
2019-09-04 23:20:10 +02:00
|
|
|
->atPath('storage_location')->addViolation();
|
2020-08-21 22:44:38 +02:00
|
|
|
}
|
2019-09-04 23:20:10 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|