src/Form/PieceLineType.php line 152

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Lot;
  4. use App\Entity\Product;
  5. use App\Repository\ProductRepository;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Symfony\Component\Form\AbstractType;
  8. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  9. use Symfony\Component\Form\FormBuilderInterface;
  10. use Symfony\Component\Form\FormEvent;
  11. use Symfony\Component\Form\FormEvents;
  12. use Symfony\Component\Form\FormInterface;
  13. use Symfony\Component\OptionsResolver\OptionsResolver;
  14. use Symfony\Component\Form\Extension\Core\Type\TextType;
  15. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  16. use Doctrine\ORM\EntityRepository;
  17. use App\Entity\PieceLine;
  18. use Symfony\Component\Validator\Constraints\NotBlank;
  19. class PieceLineType extends AbstractType
  20. {
  21.     private $productRepo;
  22.     public function __construct(ProductRepository $productRepo)
  23.     {
  24.         $this->productRepo $productRepo;
  25.     }
  26.     public function buildForm(FormBuilderInterface $builder, array $options)
  27.     {
  28.         $products $this->productRepo->findBy([],['name'=>'ASC']);
  29.         $tab $options['products'];
  30.         foreach($products as $product){
  31.             if($product->hasStock() and $tab->contains($product) === false){
  32.                 $tab->add($product);
  33.             }
  34.         }
  35.         $builder
  36.             ->add('qty'TextType::class, [
  37.                 "label" => false,
  38.                 "required" => false,
  39.                 'attr' => [
  40.                     'autocomplete' => 'off',
  41.                     'placeholder' => 'Quantité',
  42.                     'class' => 'qte_product'
  43.                 ],
  44.                 'constraints'=>[
  45.                     new NotBlank([
  46.                         'message' => 'Saisissez une qté'
  47.                     ])
  48.                 ]
  49.             ])
  50.             ->add('priceBuyHT'TextType::class, [
  51.                 "label" => false,
  52.                 "required" => false,
  53.                 'attr' => [
  54.                     "class" => "text-right",
  55.                     "readonly" => "readonly",
  56.                     'autocomplete' => 'off',
  57.                     'placeholder' => 'Prix Achat HT']
  58.             ])
  59.             ->add('marge'TextType::class, [
  60.                 "label" => false,
  61.                 "required" => false,
  62.                 'attr' => [
  63.                     "class" => "text-right",
  64.                     "readonly" => "readonly",
  65.                     'autocomplete' => 'off',
  66.                     'placeholder' => 'Marge']
  67.             ])
  68.             ->add('priceHT'TextType::class, [
  69.                 "label" => false,
  70.                 "required" => false,
  71.                 'attr' => [
  72.                     "class" => "text-right",
  73.                     "readonly" => "readonly",
  74.                     'autocomplete' => 'off',
  75.                     'placeholder' => 'Prix HT']
  76.             ])
  77.             ->add('totalHT'TextType::class, [
  78.                 "label" => false,
  79.                 "required" => false,
  80.                 'attr' => [
  81.                     "class" => "text-right",
  82.                     "readonly" => "readonly",
  83.                     'autocomplete' => 'off',
  84.                     'placeholder' => 'Total HT']
  85.             ])
  86.             ->add('rabais'TextType::class, [
  87.                 "label" => false,
  88.                 "required" => false,
  89.                 'attr' => [
  90.                     'class' => 'rabais_product',
  91.                     'autocomplete' => 'off',
  92.                     'placeholder' => 'Remise']
  93.             ])
  94.             ->add('amountRabais'TextType::class, [
  95.                 "label" => false,
  96.                 "required" => false,
  97.                 'attr' => [
  98.                     "class" => "text-right",
  99.                     "readonly" => "readonly",
  100.                     'autocomplete' => 'off',
  101.                     'placeholder' => 'montant rabais']
  102.             ])
  103.             ->add('tva'ChoiceType::class,
  104.                 [
  105.                     'choices' => $options['config'],
  106.                     'multiple' => false,
  107.                     'expanded' => false,
  108.                     "label" => false,
  109.                     "required" => false,
  110.                     'placeholder' => 'Tva',
  111.                     'attr' => [
  112.                         'class' => 'tva_product']
  113.                 ]
  114.             )
  115.             ->add('product'EntityType::class, [
  116.                 'class' => Product::class,
  117.                 'choices' => $tab,
  118.                 'choice_label' => 'codeName',
  119.                 'placeholder' => 'Produit',
  120.                 'label' => false,
  121.                 'required' => false,
  122.                 'attr' => [
  123.                     'class' => 'select_product'
  124.                 ],
  125.                 'constraints'=>[
  126.                     new NotBlank([
  127.                         'message' => 'Choisissez un produit'
  128.                     ])
  129.                 ]
  130.             ]);
  131.         $addFacilityForm = function (FormInterface $form$product_id) {
  132.             $form->add('lot'EntityType::class, [
  133.                 'class' => Lot::class,
  134.                 "required" => false,
  135.                 'choice_label' => 'infoLot',
  136.                 'label' => "Lot",
  137.                 'placeholder' => "Lot",
  138.                 'query_builder' => function (EntityRepository $er) use ($product_id) {
  139.                     // this does the trick to get the right options
  140.                     return $er->createQueryBuilder('l')
  141.                         ->where('l.product = :product')
  142.                         ->setParameter('product',$product_id);
  143.                 }
  144.             ]);
  145.         };
  146.         $builder->addEventListener(
  147.             FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($addFacilityForm) {
  148.             $product $event->getData() ? $event->getData()->getProduct() : null;
  149.             $product_id $product $product->getId() : null;
  150.             $addFacilityForm($event->getForm(), $product_id);
  151.         }
  152.         );
  153.         $builder->addEventListener(
  154.             FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($addFacilityForm) {
  155.             $data $event->getData();
  156.             $product_id array_key_exists('product'$data) ? $data['product'] : null;
  157.             $addFacilityForm($event->getForm(), $product_id);
  158.         }
  159.         );
  160.     }
  161.     public function configureOptions(OptionsResolver $resolver)
  162.     {
  163.         $resolver->setDefaults([
  164.             'data_class' => PieceLine::class,
  165.             'products' => null
  166.         ]);
  167.         $resolver->setRequired('config');
  168.     }
  169. }