<?php
namespace App\V4\Form\Type\QuoteLine;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use App\Form\Type\SpecificFieldType;
use App\Form\Type\SubresourceChoicesTrait;
use App\Normalizer\Form\TextAreaTypeNormalizer;
use App\Service\Cache\CacheManager;
use App\V4\Form\Type\QuoteLineInfo\QuoteLineInfoType;
use App\V4\Form\Type\VatFilterTrait;
use App\V4\Logger\SentryLogger;
use App\V4\Model\QuoteLine\QuoteLine;
use App\V4\Model\QuoteLineInfo\QuoteLineInfo;
use Psr\Cache\CacheException;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class QuoteLineType extends SpecificFieldType
{
use VatFilterTrait;
use SubresourceChoicesTrait {
getEntityChoices as private getEntityChoicesTrait;
}
/**
* @var CollectionDataProviderInterface
*/
private $collectionDataProvider;
/**
* @var CacheManager
*/
private $cacheManager;
/**
* @var SentryLogger
*/
private $sentryLogger;
/**
* @param CollectionDataProviderInterface $collectionDataProvider
* @param CacheManager $cacheManager
* @param SentryLogger $sentryLogger
*/
public function __construct(
CollectionDataProviderInterface $collectionDataProvider,
CacheManager $cacheManager,
SentryLogger $sentryLogger
) {
$this->collectionDataProvider = $collectionDataProvider;
$this->cacheManager = $cacheManager;
$this->sentryLogger = $sentryLogger;
}
/**
* {@inheritdoc}
*
* @throws ResourceClassNotSupportedException
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$quoteLine = $builder->getData();
$currentVatRate = QuoteLine::DEFAULT_VAT_RATE_VALUE;
if ($quoteLine instanceof QuoteLine) {
$currentVatRate = $quoteLine->getVatRate();
}
$builder
->add('isOptional', CheckboxType::class, [
'required' => false,
'false_values' => ['false', '0', '', null],
])
->add('name', TextType::class, [
'label' => 'quoteline_name',
'required' => true,
])
->add('unitPriceExclVat', NumberType::class, [
'label' => 'quoteline_unit_price_excl_vat',
'empty_data' => '0.00',
'scale' => 5,
'input' => 'string',
])
->add('quantity', NumberType::class, [
'label' => 'quoteline_quantity',
'required' => true,
])
->add('discountType', ChoiceType::class, [
'label' => 'quoteline_discount_type',
'choices' => [
'%' => QuoteLine::DISCOUNT_TYPE_PERCENTAGE,
'€' => QuoteLine::DISCOUNT_TYPE_AMOUNT,
],
])
->add('discountValue', NumberType::class, [
'label' => 'quoteline_discount_value',
'empty_data' => '0.00',
'input' => 'string',
'scale' => 5,
])
->add('sectionName', TextType::class)
->add('comment', TextareaType::class, [
'label' => 'quoteline_comment',
])
->add('description', TextareaType::class, [
'attr' => [
TextAreaTypeNormalizer::DATA_ATTR_WYSIWYG_MODE => TextAreaTypeNormalizer::WYSIWYG_MODE_RESTRICTED,
],
])
->add('quoteLineInfo', QuoteLineInfoType::class, [
'allow_extra_fields' => $options['allow_extra_fields'] ?? false,
'empty_data' => function () {
return new QuoteLineInfo();
},
])
->add('vatRate', ChoiceType::class, [
'choices' => $this->getVatChoices($this->collectionDataProvider, $currentVatRate),
'empty_data' => QuoteLine::DEFAULT_VAT_RATE_VALUE,
])
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => QuoteLine::class,
'required' => false,
'productId' => null,
]);
}
/**
* @param string $entityFQCN
* @param string $libelleProperty
*
* @return array<string, object>
*
* @throws CacheException
* @throws InvalidArgumentException
*/
private function getEntityChoices(string $entityFQCN, string $libelleProperty): array
{
return $this->getEntityChoicesTrait(
$this->collectionDataProvider,
$this->cacheManager,
$this->sentryLogger,
$entityFQCN
);
}
}