From 42399b551764af6ff75ad23a1b21e79a1146f377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sat, 31 Aug 2019 13:43:41 +0200 Subject: [PATCH] Apply the default currency value to pricedetails placeholder. --- src/Entity/PriceInformations/Orderdetail.php | 4 ++ src/Form/Part/OrderdetailType.php | 41 +++++++++++++++----- src/Form/Part/PartBaseType.php | 2 + 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/Entity/PriceInformations/Orderdetail.php b/src/Entity/PriceInformations/Orderdetail.php index 4045277d..f457a492 100644 --- a/src/Entity/PriceInformations/Orderdetail.php +++ b/src/Entity/PriceInformations/Orderdetail.php @@ -205,6 +205,10 @@ class Orderdetail extends DBElement return $this->supplier_product_url; } + if ($this->supplier === null) { + return ""; + } + return $this->getSupplier()->getAutoProductUrl($this->supplierpartnr); // maybe an automatic url is available... } diff --git a/src/Form/Part/OrderdetailType.php b/src/Form/Part/OrderdetailType.php index 8030667a..300ba0fe 100644 --- a/src/Form/Part/OrderdetailType.php +++ b/src/Form/Part/OrderdetailType.php @@ -34,6 +34,7 @@ namespace App\Form\Part; use App\Entity\Parts\MeasurementUnit; use App\Entity\Parts\Supplier; use App\Entity\PriceInformations\Orderdetail; +use App\Entity\PriceInformations\Pricedetail; use App\Form\Type\StructuralEntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; @@ -41,12 +42,18 @@ use Symfony\Component\Form\Extension\Core\Type\CollectionType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\UrlType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormEvents; use Symfony\Component\OptionsResolver\OptionsResolver; +use function foo\func; class OrderdetailType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { + /** @var Orderdetail $orderdetail */ + $orderdetail = $builder->getData(); + $builder->add('supplierpartnr', TextType::class, [ 'label' => 'orderdetails.edit.supplierpartnr', 'required' => false, @@ -70,16 +77,30 @@ class OrderdetailType extends AbstractType 'label' => 'orderdetails.edit.obsolete' ]); - //Attachment section - $builder->add('pricedetails', CollectionType::class, [ - 'entry_type' => PricedetailType::class, - 'allow_add' => true, 'allow_delete' => true, - 'label' => false, - 'by_reference' => false, - 'entry_options' => [ - 'measurement_unit' => $options['measurement_unit'] - ] - ]); + + //Add pricedetails after we know the data, so we can set the default currency + $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) { + /** @var Orderdetail $orderdetail */ + $orderdetail = $event->getData(); + + $dummy_pricedetail = new Pricedetail(); + if ($orderdetail->getSupplier() !== null) { + $dummy_pricedetail->setCurrency($orderdetail->getSupplier()->getDefaultCurrency()); + } + + //Attachment section + $event->getForm()->add('pricedetails', CollectionType::class, [ + 'entry_type' => PricedetailType::class, + 'allow_add' => true, 'allow_delete' => true, + 'label' => false, + 'prototype_data' => $dummy_pricedetail, + 'by_reference' => false, + 'entry_options' => [ + 'measurement_unit' => $options['measurement_unit'] + ] + ]); + + }); } public function configureOptions(OptionsResolver $resolver) diff --git a/src/Form/Part/PartBaseType.php b/src/Form/Part/PartBaseType.php index c2a6ba4f..dceb544e 100644 --- a/src/Form/Part/PartBaseType.php +++ b/src/Form/Part/PartBaseType.php @@ -38,6 +38,7 @@ use App\Entity\Parts\Manufacturer; use App\Entity\Parts\MeasurementUnit; use App\Entity\Parts\Part; use App\Entity\Parts\Storelocation; +use App\Entity\PriceInformations\Orderdetail; use App\Form\AttachmentFormType; use App\Form\AttachmentType; use App\Form\Type\SIUnitType; @@ -150,6 +151,7 @@ class PartBaseType extends AbstractType 'allow_add' => true, 'allow_delete' => true, 'label' => false, 'by_reference' => false, + 'prototype_data' => new Orderdetail(), 'entry_options' => [ 'measurement_unit' => $part->getPartUnit() ]