Use SIUnitType for part lots amount.

This commit is contained in:
Jan Böhmer 2019-08-26 13:22:12 +02:00
parent 0cc95f58da
commit f5ebce2a77
4 changed files with 71 additions and 15 deletions

View file

@ -712,7 +712,7 @@ class Part extends AttachmentContainingDBElement
public function useFloatAmount(): bool public function useFloatAmount(): bool
{ {
if ($this->partUnit instanceof MeasurementUnit) { 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. //When no part unit is set, treat it as part count, and so use the integer value.

View file

@ -67,11 +67,7 @@ class PartBaseType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
/** @var Part $part */ /** @var Part $part */
$part = $options['data']; $part = $builder->getData();
$part_unit_name = $part->getPartUnit() !== null ? $part->getPartUnit()->getUnit() : "";
$use_si_prefix = $part->getPartUnit() !== null ? $part->getPartUnit()->isUseSIPrefix() : false;
//Common section //Common section
$builder $builder
@ -84,7 +80,7 @@ class PartBaseType extends AbstractType
'disabled' => !$this->security->isGranted('description.edit', $part) ]) 'disabled' => !$this->security->isGranted('description.edit', $part) ])
->add('minAmount', SIUnitType::class, ->add('minAmount', SIUnitType::class,
['attr' => ['min' => 0, 'placeholder' => 'part.mininstock.placeholder'], 'label' => 'mininstock.label', ['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), ]) 'disabled' => !$this->security->isGranted('mininstock.edit', $part), ])
->add('category', StructuralEntityType::class, ['class' => Category::class, ->add('category', StructuralEntityType::class, ['class' => Category::class,
'label' => 'category.label', 'disable_not_selectable' => true, 'label' => 'category.label', 'disable_not_selectable' => true,
@ -128,6 +124,9 @@ class PartBaseType extends AbstractType
'entry_type' => PartLotType::class, 'entry_type' => PartLotType::class,
'allow_add' => true, 'allow_delete' => true, 'allow_add' => true, 'allow_delete' => true,
'label' => false, 'label' => false,
'entry_options' => [
'measurement_unit' => $part->getPartUnit()
],
'by_reference' => false 'by_reference' => false
]); ]);

View file

@ -32,9 +32,11 @@
namespace App\Form\Part; namespace App\Form\Part;
use App\Entity\Parts\MeasurementUnit;
use App\Entity\Parts\Part; use App\Entity\Parts\Part;
use App\Entity\Parts\PartLot; use App\Entity\Parts\PartLot;
use App\Entity\Parts\Storelocation; use App\Entity\Parts\Storelocation;
use App\Form\Type\SIUnitType;
use App\Form\Type\StructuralEntityType; use App\Form\Type\StructuralEntityType;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataMapperInterface; 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\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
use function GuzzleHttp\Promise\queue; use function GuzzleHttp\Promise\queue;
@ -62,14 +66,18 @@ class PartLotType extends AbstractType
'disable_not_selectable' => true, 'disable_not_selectable' => true,
'attr' => ['class' => 'selectpicker form-control-sm', 'data-live-search' => 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', '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, $builder->add('instock_unknown', CheckboxType::class, ['required' => false,
'label' => 'part_lot.edit.instock_unknown', 'label' => 'part_lot.edit.instock_unknown',
'attr' => ['class' => 'form-control-sm'], 'attr' => ['class' => 'form-control-sm'],
'label_attr' => ['class' => 'checkbox-custom']]); 'label_attr' => ['class' => 'checkbox-custom']]);
$builder->add('needs_refill', CheckboxType::class, ['label_attr' => ['class' => 'checkbox-custom'], $builder->add('needs_refill', CheckboxType::class, ['label_attr' => ['class' => 'checkbox-custom'],
'label' => 'part_lot.edit.needs_refill', 'label' => 'part_lot.edit.needs_refill',
'attr' => ['class' => 'form-control-sm'], 'attr' => ['class' => 'form-control-sm'],
@ -80,7 +88,6 @@ class PartLotType extends AbstractType
'required' => false]); 'required' => false]);
$builder->add('comment', TextType::class, ['label' => 'part_lot.edit.comment', $builder->add('comment', TextType::class, ['label' => 'part_lot.edit.comment',
'label' => 'part_lot.edit.comment',
'attr' => ['class' => 'form-control-sm'], 'attr' => ['class' => 'form-control-sm'],
'required' => false, 'empty_data' => ""]); 'required' => false, 'empty_data' => ""]);
} }
@ -91,5 +98,8 @@ class PartLotType extends AbstractType
$resolver->setDefaults([ $resolver->setDefaults([
'data_class' => PartLot::class, 'data_class' => PartLot::class,
]); ]);
$resolver->setRequired('measurement_unit');
$resolver->setAllowedTypes('measurement_unit', [MeasurementUnit::class, 'null']);
} }
} }

View file

@ -32,6 +32,7 @@
namespace App\Form\Type; namespace App\Form\Type;
use App\Entity\Parts\MeasurementUnit;
use App\Services\SIFormatter; use App\Services\SIFormatter;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataMapperInterface; 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\FormBuilderInterface;
use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView; use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
class SIUnitType extends AbstractType implements DataMapperInterface class SIUnitType extends AbstractType implements DataMapperInterface
@ -55,18 +57,63 @@ class SIUnitType extends AbstractType implements DataMapperInterface
public function configureOptions(OptionsResolver $resolver) public function configureOptions(OptionsResolver $resolver)
{ {
$resolver->setDefaults([ $resolver->setDefaults([
'show_prefix' => true, 'measurement_unit' => null,
'is_integer' => false, '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'] 'error_mapping' => [ '.' => 'value']
]); ]);
$resolver->setAllowedTypes('measurement_unit', [MeasurementUnit::class, 'null']);
$resolver->setRequired('unit'); $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) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
$builder $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']) { if ($options['show_prefix']) {
$builder->add('prefix', ChoiceType::class, [ $builder->add('prefix', ChoiceType::class, [