diff --git a/src/Entity/Parts/PartLot.php b/src/Entity/Parts/PartLot.php index 3d42968f..884bdbcf 100644 --- a/src/Entity/Parts/PartLot.php +++ b/src/Entity/Parts/PartLot.php @@ -35,6 +35,7 @@ namespace App\Entity\Parts; use App\Entity\Base\DBElement; use App\Entity\Base\TimestampTrait; use App\Validator\Constraints\Selectable; +use App\Validator\Constraints\ValidPartLot; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; @@ -45,6 +46,7 @@ use Symfony\Component\Validator\Constraints as Assert; * @ORM\Entity() * @ORM\Table(name="part_lots") * @ORM\HasLifecycleCallbacks() + * @ValidPartLot() */ class PartLot extends DBElement { diff --git a/src/Validator/Constraints/ValidPartLot.php b/src/Validator/Constraints/ValidPartLot.php new file mode 100644 index 00000000..964a118c --- /dev/null +++ b/src/Validator/Constraints/ValidPartLot.php @@ -0,0 +1,49 @@ +em = $em; + } + + /** + * Checks if the passed value is valid. + * + * @param mixed $value The value that should be validated + * @param Constraint $constraint The constraint for the validation + */ + public function validate($value, Constraint $constraint) + { + if (!$constraint instanceof ValidPartLot) { + throw new UnexpectedTypeException($constraint, ValidPartLot::class); + } + + if (!$value instanceof PartLot) { + throw new UnexpectedTypeException($value, PartLot::class); + } + + //We can only validate the values if we know the storelocation + if ($value->getStorageLocation()) { + $parts = $value->getStorageLocation()->getParts(); + + //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']) { + $this->context->buildViolation('validator.part_lot.location_full.no_increasment') + ->setParameter('{{ old_amount }}', $db_lot['amount']) + ->atPath('amount')->addViolation(); + } + + if (!$parts->contains($value->getPart())) { + $this->context->buildViolation('validator.part_lot.location_full') + ->atPath('storage_location')->addViolation(); + } + } + + + //Check for onlyExisting + if ($value->getStorageLocation()->isLimitToExistingParts()) { + if (!$parts->contains($value->getPart())) { + $this->context->buildViolation('validator.part_lot.only_existing') + ->atPath('storage_location')->addViolation(); + } + } + + //Check for only single part + if ($value->getStorageLocation()->isLimitToExistingParts()) { + if (($parts->count() > 0) && !$parts->contains($value->getPart())) { + $this->context->buildViolation('validator.part_lot.single_part') + ->atPath('storage_location')->addViolation(); + } + } + } + } +} \ No newline at end of file