src/Form/GestionComerciale/AvoirType.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Form\GestionComerciale;
  3. use App\DataTransformer\Clients\IdToClientTransformer;
  4. use App\Entity\Adresses\Adresse;
  5. use App\Entity\Clients\Client;
  6. use App\Entity\Clients\ClientCategorie;
  7. use App\Entity\GestionComerciale\Commande;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  10. use Symfony\Component\Form\AbstractType;
  11. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  12. use Symfony\Component\Form\FormBuilderInterface;
  13. use Symfony\Component\Form\FormEvent;
  14. use Symfony\Component\Form\FormEvents;
  15. use Symfony\Component\Form\FormInterface;
  16. use Symfony\Component\OptionsResolver\OptionsResolver;
  17. class AvoirType extends AbstractType
  18. {
  19.     private $entityManager;
  20.     public function __construct(EntityManagerInterface $entityManager)
  21.     {
  22.         $this->entityManager $entityManager;
  23.     }
  24.     /**
  25.      * @param FormBuilderInterface $builder
  26.      * @param array $options
  27.      */
  28.     public function buildForm(FormBuilderInterface $builder, array $options)
  29.     {
  30.         $builder
  31.             ->add('totalPoids'HiddenType::class)
  32.             ->add('commentaire')
  33.             ->add('client'EntityType::class, [
  34.                 'multiple' => false,
  35.                 'attr'  => [
  36.                     'class' => 'select2-hidden-accessible',
  37.                 ],
  38.                 'class' => Client::class
  39.             ]);
  40.         //$builder->add('referenceClient');
  41. //        $builder->get('client')->addModelTransformer(new IdToClientTransformer($this->entityManager));
  42.         $formModifier = function (FormInterface $formClient $client null) {
  43.             $adresses null === $client ? [] : $client->getAdresses();
  44.             $pro "0";
  45.             if (is_object($client)) {
  46.                 $repo_client_categorie $this->entityManager->getRepository(ClientCategorie::class);
  47.                 $premiere_categorie    $repo_client_categorie->getCategorieRacinePourClient($client);
  48.                 if ( ! is_object($premiere_categorie)) {
  49.                     $pro "0";
  50.                 } elseif ( ! is_object($premiere_categorie->getCategorie())) {
  51.                     $pro "0";
  52.                 } elseif ( ! $premiere_categorie->getCategorie()->getProfessionnel()) {
  53.                     $pro "0";
  54.                 } else {
  55.                     $pro "1";
  56.                 }
  57.             }
  58.             $dataTva       1;
  59.             $dataAtributes = [];
  60.             foreach ($adresses as $a) {
  61.                 $paysUe "0";
  62.                 if (is_object($a->getPays())) {
  63.                     if ($a->getPays()->getId() != "370095" && $a->getPays()->getEurope()) {
  64.                         $paysUe "1";
  65.                     }
  66.                     if ($pro == && $a->getPays()->getId() != "370095") {
  67.                         $dataTva 0;
  68.                     }
  69.                     if ($pro == && $a->getPays()->getEurope() == false) {
  70.                         $dataTva 0;
  71.                     }
  72.                 }
  73.                 $dataTva 1;
  74.                 if (is_object($client->getCompta()) && $client->getCompta()->getTva() == 0) {
  75.                     //    echo "eeee";
  76.                     $dataTva 0;
  77.                 }
  78.                 $adrTab          = ['data-pro' => $pro'data-ue' => $paysUe'data-tva' => $dataTva];
  79.                 $dataAtributes[] = $adrTab;
  80.             }
  81.             $form->add('adresseLivraison'EntityType::class, [
  82.                 'class'       => Adresse::class,
  83.                 'placeholder' => '',
  84.                 'choices'     => $adresses,
  85.                 'choice_attr' => $dataAtributes,
  86.             ]);
  87.             $form->add('adresseFacturation'EntityType::class, [
  88.                 'class'       => Adresse::class,
  89.                 'placeholder' => '',
  90.                 'choices'     => $adresses,
  91.                 'choice_attr' => $dataAtributes,
  92.             ]);
  93.         };
  94.         $builder->addEventListener(
  95.             FormEvents::PRE_SET_DATA,
  96.             function (FormEvent $event) use ($formModifier) {
  97.                 // this would be your entity, i.e. SportMeetup
  98.                 $data $event->getData();
  99.                 $formModifier($event->getForm(), $data->getClient());
  100.             }
  101.         );
  102.         $builder->get('client')->addEventListener(
  103.             FormEvents::POST_SUBMIT,
  104.             function (FormEvent $event) use ($formModifier) {
  105.                 // It's important here to fetch $event->getForm()->getData(), as
  106.                 // $event->getData() will get you the client data (that is, the ID)
  107.                 $client $event->getForm()->getData();
  108.                 // since we've added the listener to the child, we'll have to pass on
  109.                 // the parent to the callback functions!
  110.                 $formModifier($event->getForm()->getParent(), $client);
  111.             }
  112.         );
  113.     }
  114.     public function configureOptions(OptionsResolver $resolver)
  115.     {
  116.         $resolver->setDefaults([
  117.                                    'data_class' => Commande::class,
  118.                                ]);
  119.     }
  120.     public function getName()
  121.     {
  122.         return 'dtc_gestioncomercialebundle_commande';
  123.         //return 'dtc_gestioncomercialebundle_ordre_reparation';
  124.     }
  125.     public function getBlockPrefix():string
  126.     {
  127.         return 'dtc_gestioncomercialebundle_commande';
  128.     }
  129. }