diff --git a/src/Entity/Parts/Part.php b/src/Entity/Parts/Part.php index 741d073e..2363ff3f 100644 --- a/src/Entity/Parts/Part.php +++ b/src/Entity/Parts/Part.php @@ -712,7 +712,7 @@ class Part extends AttachmentContainingDBElement public function useFloatAmount(): bool { if ($this->partUnit instanceof MeasurementUnit) { - return $this->partUnit->isInteger(); + return !$this->partUnit->isInteger(); } //When no part unit is set, treat it as part count, and so use the integer value. diff --git a/src/Form/Part/PartBaseType.php b/src/Form/Part/PartBaseType.php index b1fb587c..2c25d68d 100644 --- a/src/Form/Part/PartBaseType.php +++ b/src/Form/Part/PartBaseType.php @@ -67,11 +67,7 @@ class PartBaseType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { /** @var Part $part */ - $part = $options['data']; - - $part_unit_name = $part->getPartUnit() !== null ? $part->getPartUnit()->getUnit() : ""; - $use_si_prefix = $part->getPartUnit() !== null ? $part->getPartUnit()->isUseSIPrefix() : false; - + $part = $builder->getData(); //Common section $builder @@ -84,7 +80,7 @@ class PartBaseType extends AbstractType 'disabled' => !$this->security->isGranted('description.edit', $part) ]) ->add('minAmount', SIUnitType::class, ['attr' => ['min' => 0, 'placeholder' => 'part.mininstock.placeholder'], 'label' => 'mininstock.label', - 'show_prefix' => $use_si_prefix, "unit" => $part_unit_name, "is_integer" => !$part->useFloatAmount(), + 'measurement_unit' => $part->getPartUnit(), 'disabled' => !$this->security->isGranted('mininstock.edit', $part), ]) ->add('category', StructuralEntityType::class, ['class' => Category::class, 'label' => 'category.label', 'disable_not_selectable' => true, @@ -128,6 +124,9 @@ class PartBaseType extends AbstractType 'entry_type' => PartLotType::class, 'allow_add' => true, 'allow_delete' => true, 'label' => false, + 'entry_options' => [ + 'measurement_unit' => $part->getPartUnit() + ], 'by_reference' => false ]); diff --git a/src/Form/Part/PartLotType.php b/src/Form/Part/PartLotType.php index 2f4b0940..063765ff 100644 --- a/src/Form/Part/PartLotType.php +++ b/src/Form/Part/PartLotType.php @@ -32,9 +32,11 @@ namespace App\Form\Part; +use App\Entity\Parts\MeasurementUnit; use App\Entity\Parts\Part; use App\Entity\Parts\PartLot; use App\Entity\Parts\Storelocation; +use App\Form\Type\SIUnitType; use App\Form\Type\StructuralEntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\DataMapperInterface; @@ -45,6 +47,8 @@ use Symfony\Component\Form\Extension\Core\Type\IntegerType; use Symfony\Component\Form\Extension\Core\Type\NumberType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use function GuzzleHttp\Promise\queue; @@ -62,15 +66,19 @@ class PartLotType extends AbstractType 'disable_not_selectable' => true, 'attr' => ['class' => 'selectpicker form-control-sm', 'data-live-search' => true]]); - $builder->add('amount',NumberType::class, [ 'html5' => true, + + $builder->add('amount', SIUnitType::class, [ + 'measurement_unit' => $options['measurement_unit'], 'label' => 'part_lot.edit.amount', - 'attr' => ['class' => 'form-control-sm', 'min' => 0, 'step' => 'any'] + 'attr' => ['class' => 'form-control-sm'] ]); + $builder->add('instock_unknown', CheckboxType::class, ['required' => false, 'label' => 'part_lot.edit.instock_unknown', 'attr' => ['class' => 'form-control-sm'], - 'label_attr'=> ['class' => 'checkbox-custom']]); - $builder->add('needs_refill', CheckboxType::class, ['label_attr'=> ['class' => 'checkbox-custom'], + 'label_attr' => ['class' => 'checkbox-custom']]); + + $builder->add('needs_refill', CheckboxType::class, ['label_attr' => ['class' => 'checkbox-custom'], 'label' => 'part_lot.edit.needs_refill', 'attr' => ['class' => 'form-control-sm'], 'required' => false]); @@ -80,7 +88,6 @@ class PartLotType extends AbstractType 'required' => false]); $builder->add('comment', TextType::class, ['label' => 'part_lot.edit.comment', - 'label' => 'part_lot.edit.comment', 'attr' => ['class' => 'form-control-sm'], 'required' => false, 'empty_data' => ""]); } @@ -91,5 +98,8 @@ class PartLotType extends AbstractType $resolver->setDefaults([ 'data_class' => PartLot::class, ]); + + $resolver->setRequired('measurement_unit'); + $resolver->setAllowedTypes('measurement_unit', [MeasurementUnit::class, 'null']); } } \ No newline at end of file diff --git a/src/Form/Type/SIUnitType.php b/src/Form/Type/SIUnitType.php index 608e8fa7..fd6092ef 100644 --- a/src/Form/Type/SIUnitType.php +++ b/src/Form/Type/SIUnitType.php @@ -32,6 +32,7 @@ namespace App\Form\Type; +use App\Entity\Parts\MeasurementUnit; use App\Services\SIFormatter; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\DataMapperInterface; @@ -41,6 +42,7 @@ use Symfony\Component\Form\Extension\Core\Type\NumberType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; +use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; class SIUnitType extends AbstractType implements DataMapperInterface @@ -55,18 +57,63 @@ class SIUnitType extends AbstractType implements DataMapperInterface public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ - 'show_prefix' => true, - 'is_integer' => false, + 'measurement_unit' => null, + 'show_prefix' => function (Options $options) { + if ($options['measurement_unit'] !== null) { + /** @var MeasurementUnit $unit */ + $unit = $options['measurement_unit']; + return $unit->isUseSIPrefix(); + } + return true; + }, + 'is_integer' => function (Options $options) { + if ($options['measurement_unit'] !== null) { + /** @var MeasurementUnit $unit */ + $unit = $options['measurement_unit']; + return $unit->isInteger(); + } + return false; + }, + 'unit' => function (Options $options) { + if ($options['measurement_unit'] !== null) { + /** @var MeasurementUnit $unit */ + $unit = $options['measurement_unit']; + return $unit->getUnit(); + } + return null; + }, 'error_mapping' => [ '.' => 'value'] ]); + $resolver->setAllowedTypes('measurement_unit', [MeasurementUnit::class, 'null']); $resolver->setRequired('unit'); + + //Options which allows us, to limit the input using HTML5 number input + $resolver->setDefaults([ + 'min' => 0, + 'max' => '', + 'step' => function (Options $options) { + if ($options['is_integer'] === true) { + return 1; + } + + return "any"; + }, + 'html5' => true + ]); } public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->add('value', NumberType::class); + ->add('value', NumberType::class, [ + 'html5' => $options['html5'], + 'attr' => [ + 'min' => (string) $options['min'], + 'max' => (string) $options['max'], + 'step' => (string) $options['step'] + ] + ]); if ($options['show_prefix']) { $builder->add('prefix', ChoiceType::class, [