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
<?php
namespace App\Form\Admin ;
use App\Entity\Address ;
use App\Entity\BaseInvoice ;
use App\Entity\Carrier ;
use App\Entity\CarrierPriceRange ;
use App\Entity\DeliveryNote ;
use App\Entity\Invoice ;
use App\Entity\Shipment ;
use App\Service\GuardChargeService ;
use Doctrine\Common\Collections\ArrayCollection ;
use Symfony\Bridge\Doctrine\Form\Type\EntityType ;
use Symfony\Component\Form\AbstractType ;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType ;
use Symfony\Component\Form\Extension\Core\Type\HiddenType ;
use Symfony\Component\Form\Extension\Core\Type\NumberType ;
use Symfony\Component\Form\FormBuilderInterface ;
use Symfony\Component\Form\FormEvent ;
use Symfony\Component\Form\FormEvents ;
use Symfony\Component\Form\FormInterface ;
use Symfony\Component\OptionsResolver\OptionsResolver ;
use Symfony\Component\Validator\Constraints\NotBlank ;
class ShipmentFormType extends AbstractType
{
public function __construct (private GuardChargeService $guardChargeService )
{
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm ( FormBuilderInterface $builder , array $options )
{
/** @var Shipment $shipment */
$shipment = $options [ 'data' ];
$prices = $options [ 'prices' ];
$carriers = $prices -> map (fn( CarrierPriceRange $_ ) => $_ -> getCarrier ());
$userAddresses = array_filter ( $shipment -> getDeliveryNote ()-> getUser ()-> getAddresses ()-> toArray (), fn( Address $a ) => $a -> isComplete ());
$builder -> add ( 'deliveryNote' , EntityType ::class, array(
'label' => false ,
'class' => DeliveryNote ::class,
'required' => true ,
'choices' => [ $shipment -> getDeliveryNote ()],
'data' => $shipment -> getDeliveryNote (),
))->add ( 'carrier' , EntityType ::class, [
'class' => Carrier ::class,
'choices' => $carriers -> toArray (),
'choice_attr' => fn( Carrier $c ) => [
'data-price' => $prices -> filter (fn( CarrierPriceRange $r ) => $r -> getCarrier () === $c )-> first ()-> getPriceIncludedTaxes (),
'data-insurance' => $prices -> filter (fn( CarrierPriceRange $r ) => $r -> getCarrier () === $c )-> first ()-> getInsurance ()?-> getPriceIncludedTaxes () ?? 0 ,
'data-signature' => $prices -> filter (fn( CarrierPriceRange $r ) => $r -> getCarrier () === $c )-> first ()-> getDeliveryAgainstSignature ()?-> getPriceIncludedTaxes () ?? 0 ,
'data-isServicePoint' => (bool) $c -> isServicePoint ()
],
'attr' => [
'class' => 'carrier-select'
]
])->add ( 'deliveryAddress' , EntityType ::class, array(
'class' => Address ::class,
'label' => 'Adresse de livraison' ,
'required' => true ,
'mapped' => false ,
'choices' => $userAddresses ,
'attr' => [
'class' => count ( $userAddresses ) ? '' : 'd-none'
],
'error_bubbling' => false
))-> add ( 'billingAddress' , EntityType ::class, array(
'class' => Address ::class,
'label' => 'Adresse de facturation' ,
'required' => true ,
'mapped' => false ,
'choices' => $userAddresses ,
'attr' => [
'class' => count ( $userAddresses ) ? '' : 'd-none'
],
'constraints' => [
new NotBlank (),
],
))->add ( 'invoice' , EntityType ::class, [
'class' => Invoice ::class,
'mapped' => false ,
'label' => 'Facture' ,
'choices' => $shipment -> getDeliveryNote ()-> getInvoices ()-> filter (fn( Invoice $invoice ) => in_array ( $invoice -> getType (), [ BaseInvoice :: TYPE_SHIPPING , BaseInvoice :: TYPE_GUARD ]) && $invoice -> getPayments ()-> last () && $invoice -> getPayments ()-> last ()-> isSuccess ())
])->add ( 'amountGuardCharge' , NumberType ::class, [
'label' => 'Frais de gardiennage € (TTC)' ,
'scale' => 2 ,
'attr' => [
'placeholder' => 0
],
'required' => false
])-> add ( 'amountShipping' , NumberType ::class, [
'label' => 'Montant du transport € (TTC)' ,
'scale' => 2 ,
'attr' => [
'placeholder' => 0
],
'required' => false
])-> add ( 'insurance' , CheckboxType ::class, [
'label' => 'Assurance activée' ,
'required' => false
])-> add ( 'freeShipping' , CheckboxType ::class, [
'label' => 'Expédition gratuite' ,
'mapped' => false ,
'required' => false
]);;
$formModifierCarrier = function ( FormInterface $form , Carrier $carrier = null ): void {
if ($carrier && $carrier -> getIsServicePoint ()) {
$form -> add ( 'servicePoint' , HiddenType ::class, array(
'label' => false ,
'required' => false ,
'constraints' => array(
new NotBlank ( message : 'Le point relais n\'a pas été sélectionné.' )
),
'mapped' => false ,
'error_bubbling' => false ,
));
}
};
$builder -> get ( "carrier" )-> addEventListener (
FormEvents :: POST_SUBMIT ,
function (FormEvent $event ) use ( $formModifierCarrier ): void {
$carrier = $event -> getForm ()-> getData ();
$formModifierCarrier ( $event -> getForm ()-> getParent (), $carrier );
}
);
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions ( OptionsResolver $resolver )
{
$resolver -> setDefaults (array(
'data_class' => Shipment ::class,
'allow_extra_fields' => true ,
'attr' => [
'id' => 'shipment_form'
],
'prices' => new ArrayCollection (),
'validation_groups' => [ 'admin_create' , 'Default' ]
));
}
}