src/Form/SupplierInvoicesType.php line 19

  1. <?php
  2. namespace App\Form;
  3. use App\Entity\SupplierInvoices;
  4. use Symfony\Component\Form\AbstractType;
  5. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7. use Symfony\Component\Form\FormEvent;
  8. use Symfony\Component\Form\FormEvents;
  9. use Symfony\Component\OptionsResolver\OptionsResolver;
  10. use Symfony\Component\Form\Extension\Core\Type\FileType;
  11. use Symfony\Component\Form\Extension\Core\Type\TextType;
  12. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  13. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  14. use Symfony\Component\Form\Extension\Core\Type\DateType;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. class SupplierInvoicesType extends AbstractType
  17. {
  18.     /**
  19.      * @var Translator
  20.      */
  21.     protected $translator;
  22.     /**
  23.      * VehicleBeneficiaryDetailsType constructor.
  24.      * @param Translator|null $translator
  25.      */
  26.     public function __construct(TranslatorInterface $translatornull)
  27.     {
  28.         $this->translator $translator;
  29.     }
  30.     /**
  31.      * {@inheritdoc}
  32.      */
  33.     public function buildForm(FormBuilderInterface $builder, array $options)
  34.     {
  35.         $trans  $this->translator;
  36.         $numberValidation $trans->trans('you should enter numbers', [], 'validators');
  37.         $alphanumericValidation $trans->trans('you should enter alpha-numeric', [], 'validators');
  38.         $builder->add('total'null, array(
  39.                     'label'    => 'total',
  40.                     'required' => false,
  41.                     'attr'  => ['validate-msg' => $numberValidation]
  42.                 ))
  43.             ->add('invoiceNumber'null, array(
  44.                     'label'    => 'Claim Invoice Number',
  45.                     'required' => false,
  46.                     'attr'  => ['validate-msg' => $alphanumericValidation]
  47.                 ))
  48.             ->add('isPayed'CheckboxType::class, array(
  49.                     'label'    => 'Payment Status',
  50.                     'required' => false
  51.                 ))
  52.             ->add('paymentType'ChoiceType::class, array(
  53.                     'label'       => 'Payment Method',
  54.                     'choices'     => SupplierInvoices::PAYMENT_TYPES,
  55.                     'required'    => false,
  56.                     'placeholder' => 'Please Select',
  57.                 ))
  58.             ->add('paymentDate'DateType::class, array(
  59.                     'label'    => 'Invoice Date',
  60.                     'widget'   => 'single_text',
  61.                     'required' => false,
  62.                     'html5'    => false
  63.                 ))
  64.             ->add('imagePath'FileType::class, array(
  65.                     'label'    => 'Attach an Image',
  66.                     'required' => false,
  67.                     'attr'     => ['class' => 'docImg']
  68.                 ))
  69.             ->add('clearImage'HiddenType::class, array(
  70.                     'label'    => false,
  71.                     'required' => false,
  72.                     'mapped'   => false
  73.                 ));
  74.         //show the checkbox of including/ignoring VAT in invoice value only if there is an effective VAT rate
  75.         //this is the way to show the form field if the form page was reloaded due to failed validation
  76.         $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options ) {
  77.             $invoice $event->getData();
  78.             $form  $event->getForm();
  79.             $shallShowVatCheckboxfalse;
  80.             if( !$invoice && $options['theEffectiveVat']) {
  81.                 //if no invoice yet and there is an Effective VAT rate then show the option of adding/ignoring VAT
  82.                 $shallShowVatCheckbox true;
  83.                 $vatText $options['theEffectiveVat']->getVatText();
  84.             }elseif ($invoice && $invoice->getVatRate()){
  85.                 //else if there is an invoice and it has a VAT rate then show the option of adding/ignoring VAT
  86.                 $shallShowVatCheckbox true;
  87.                 $vatText $invoice->getVatRate()->getVatText();
  88.             }
  89.             if($shallShowVatCheckbox) {
  90.                 $form->add('check_vat_amount'CheckboxType::class, array(
  91.                     'label' => 'value is vat inclusive',
  92.                     'label_translation_parameters' => [
  93.                         '%vat_rate%' => $vatText],
  94.                     'required' => false,
  95.                     'attr' => array('class' => ''),
  96.                     'mapped' => false
  97.                 ));
  98.             }
  99.         });
  100.     }
  101.     
  102.     /**
  103.      * {@inheritdoc}
  104.      */
  105.     public function configureOptions(OptionsResolver $resolver)
  106.     {
  107.         $resolver->setDefaults(array(
  108.             'data_class'         => 'App\Entity\SupplierInvoices',
  109.             'translation_domain' => 'vehicles',
  110.             'locale'             => 'en',
  111.             'allow_extra_fields' => true,
  112.             'theEffectiveVat' => false
  113.         ));
  114.     }
  115.     /**
  116.      * {@inheritdoc}
  117.      */
  118.     public function getBlockPrefix()
  119.     {
  120.         return 'appbundle_supplierinvoices';
  121.     }
  122. }