Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

src/Form/Admin/ShipmentFormType.php line 112

Open in your IDE?
  1. <?php
  2. namespace App\Form\Admin;
  3. use App\Entity\Address;
  4. use App\Entity\BaseInvoice;
  5. use App\Entity\Carrier;
  6. use App\Entity\CarrierPriceRange;
  7. use App\Entity\DeliveryNote;
  8. use App\Entity\Invoice;
  9. use App\Entity\Shipment;
  10. use App\Service\GuardChargeService;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  13. use Symfony\Component\Form\AbstractType;
  14. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  15. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  16. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  17. use Symfony\Component\Form\FormBuilderInterface;
  18. use Symfony\Component\Form\FormEvent;
  19. use Symfony\Component\Form\FormEvents;
  20. use Symfony\Component\Form\FormInterface;
  21. use Symfony\Component\OptionsResolver\OptionsResolver;
  22. use Symfony\Component\Validator\Constraints\NotBlank;
  23. class ShipmentFormType extends AbstractType
  24. {
  25.     public function __construct(private GuardChargeService $guardChargeService)
  26.     {
  27.     }
  28.     /**
  29.      * @param FormBuilderInterface $builder
  30.      * @param array $options
  31.      */
  32.     public function buildForm(FormBuilderInterface $builder, array $options)
  33.     {
  34.         /** @var Shipment $shipment */
  35.         $shipment $options['data'];
  36.         $prices $options['prices'];
  37.         $carriers $prices->map(fn(CarrierPriceRange $_) => $_->getCarrier());
  38.         $userAddresses array_filter($shipment->getDeliveryNote()->getUser()->getAddresses()->toArray(), fn(Address $a) => $a->isComplete());
  39.         $builder->add('deliveryNote'EntityType::class, array(
  40.             'label' => false,
  41.             'class' => DeliveryNote::class,
  42.             'required' => true,
  43.             'choices' => [$shipment->getDeliveryNote()],
  44.             'data' => $shipment->getDeliveryNote(),
  45.         ))->add('carrier'EntityType::class, [
  46.             'class' => Carrier::class,
  47.             'choices' => $carriers->toArray(),
  48.             'choice_attr' => fn(Carrier $c) => [
  49.                 'data-price' => $prices->filter(fn(CarrierPriceRange $r) => $r->getCarrier() === $c)->first()->getPriceIncludedTaxes(),
  50.                 'data-insurance' => $prices->filter(fn(CarrierPriceRange $r) => $r->getCarrier() === $c)->first()->getInsurance()?->getPriceIncludedTaxes() ?? 0,
  51.                 'data-signature' => $prices->filter(fn(CarrierPriceRange $r) => $r->getCarrier() === $c)->first()->getDeliveryAgainstSignature()?->getPriceIncludedTaxes() ?? 0,
  52.                 'data-isServicePoint' => (bool)$c->isServicePoint()
  53.             ],
  54.             'attr' => [
  55.                 'class' => 'carrier-select'
  56.             ]
  57.         ])->add('deliveryAddress'EntityType::class, array(
  58.             'class' => Address::class,
  59.             'label' => 'Adresse de livraison',
  60.             'required' => true,
  61.             'mapped' => false,
  62.             'choices' => $userAddresses,
  63.             'attr' => [
  64.                 'class' => count($userAddresses) ? '' 'd-none'
  65.             ],
  66.             'error_bubbling' => false
  67.         ))->add('billingAddress'EntityType::class, array(
  68.             'class' => Address::class,
  69.             'label' => 'Adresse de facturation',
  70.             'required' => true,
  71.             'mapped' => false,
  72.             'choices' => $userAddresses,
  73.             'attr' => [
  74.                 'class' => count($userAddresses) ? '' 'd-none'
  75.             ],
  76.             'constraints' => [
  77.                 new NotBlank(),
  78.             ],
  79.         ))->add('invoice'EntityType::class, [
  80.             'class' => Invoice::class,
  81.             'mapped' => false,
  82.             'label' => 'Facture',
  83.             'choices' => $shipment->getDeliveryNote()->getInvoices()->filter(fn(Invoice $invoice) => in_array($invoice->getType(), [BaseInvoice::TYPE_SHIPPINGBaseInvoice::TYPE_GUARD]) && $invoice->getPayments()->last() && $invoice->getPayments()->last()->isSuccess())
  84.         ])->add('amountGuardCharge'NumberType::class, [
  85.             'label' => 'Frais de gardiennage € (TTC)',
  86.             'scale' => 2,
  87.             'attr' => [
  88.                 'placeholder' => 0
  89.             ],
  90.             'required' => false
  91.         ])->add('amountShipping'NumberType::class, [
  92.             'label' => 'Montant du transport € (TTC)',
  93.             'scale' => 2,
  94.             'attr' => [
  95.                 'placeholder' => 0
  96.             ],
  97.             'required' => false
  98.         ])->add('insurance'CheckboxType::class, [
  99.             'label' => 'Assurance activée',
  100.             'required' => false
  101.         ])->add('freeShipping'CheckboxType::class, [
  102.             'label' => 'Expédition gratuite',
  103.             'mapped' => false,
  104.             'required' => false
  105.         ]);;
  106.         $formModifierCarrier = function (FormInterface $formCarrier $carrier null): void {
  107.             if ($carrier && $carrier->getIsServicePoint()) {
  108.                 $form->add('servicePoint'HiddenType::class, array(
  109.                     'label' => false,
  110.                     'required' => false,
  111.                     'constraints' => array(
  112.                         new NotBlank(message'Le point relais n\'a pas été sélectionné.')
  113.                     ),
  114.                     'mapped' => false,
  115.                     'error_bubbling' => false,
  116.                 ));
  117.             }
  118.         };
  119.         $builder->get("carrier")->addEventListener(
  120.             FormEvents::POST_SUBMIT,
  121.             function (FormEvent $event) use ($formModifierCarrier): void {
  122.                 $carrier $event->getForm()->getData();
  123.                 $formModifierCarrier($event->getForm()->getParent(), $carrier);
  124.             }
  125.         );
  126.     }
  127.     /**
  128.      * @param OptionsResolver $resolver
  129.      */
  130.     public function configureOptions(OptionsResolver $resolver)
  131.     {
  132.         $resolver->setDefaults(array(
  133.             'data_class' => Shipment::class,
  134.             'allow_extra_fields' => true,
  135.             'attr' => [
  136.                 'id' => 'shipment_form'
  137.             ],
  138.             'prices' => new ArrayCollection(),
  139.             'validation_groups' => ['admin_create''Default']
  140.         ));
  141.     }
  142. }