src/Admin/SaidaAlmoxarifadoAdmin.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Admin;
  3. use App\Entity\SaidaAlmoxarifado;
  4. use App\Services\ToolService;
  5. use Sonata\AdminBundle\Admin\AbstractAdmin;
  6. use Sonata\AdminBundle\Form\FormMapper;
  7. use Sonata\AdminBundle\Datagrid\ListMapper;
  8. use Sonata\AdminBundle\Show\ShowMapper;
  9. use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
  10. use Sonata\Form\Type\CollectionType;
  11. use Symfony\Component\Form\Extension\Core\Type\TextType;
  12. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  13. use Sonata\AdminBundle\Form\Type\ModelType;
  14. final class SaidaAlmoxarifadoAdmin extends AbstractAdmin
  15. {
  16.     private $movService;
  17.     public function __construct($code$class$baseControllerNameToolService $movService)
  18.     {
  19.         parent::__construct($code$class$baseControllerName);
  20.         $this->movService $movService;
  21.     }
  22.     protected function configureFormFields(FormMapper $formMapper)
  23.     {
  24.         /** @var SaidaAlmoxarifado $subject */
  25.         $subject $this->getSubject();
  26.         $isManutencao $subject && $subject->getMotivo() === 'MANUTENCAO';
  27.         
  28.         $formMapper
  29.             ->with('Dados')
  30.                 ->add('motivo'ChoiceType::class, [
  31.                     'choices' => [
  32.                         'Ordem de Serviço' => 'OS',
  33.                         'Manutenção' => 'MANUTENCAO'
  34.                     ],
  35.                     'placeholder' => '-- Selecione o Motivo --',
  36.                     'label' => 'Motivo da Saída',
  37.                     'required' => true,
  38.                 ])
  39.                 ->add('os'ModelType::class, [
  40.                     'placeholder' => '-- Selecione --',
  41.                     'label' => 'Ordem de serviço',
  42.                     'required' => !$isManutencao,
  43.                     'expanded' => false,
  44.                     'multiple' => false,
  45.                     'btn_add'=>false,
  46.                     'help' => $isManutencao 'OS não é obrigatória para saídas de manutenção' ''
  47.                 ])
  48.                 ->add('liberadoPor'TextType::class, [
  49.                     'label' => 'Liberado por',
  50.                     'required' => false,
  51.                     'attr' => ['placeholder' => 'Nome de quem liberou']
  52.                 ])
  53.                 ->add('retiradoPor'TextType::class, [
  54.                     'label' => 'Retirado por',
  55.                     'required' => false,
  56.                     'attr' => ['placeholder' => 'Nome de quem retirou']
  57.                 ])
  58.                 ->add('status'ChoiceType::class, [
  59.                     'choices' => ['OPEN'=>'OPEN''PARCIAL'=>'PARCIAL''FECHADA'=>'FECHADA'],
  60.                     'disabled' => true,
  61.                 ])
  62.             ->end()
  63.             ->with('Itens da Saída')
  64.                 ->add('itens'CollectionType::class, [
  65.                     'label' => 'Itens da Saída',
  66.                     'by_reference' => false,
  67.                 ], [
  68.                     'edit' => 'inline',
  69.                     'inline' => 'table'
  70.                 ])
  71.             ->end();
  72.     }
  73.     protected function configureListFields(ListMapper $listMapper)
  74.     {
  75.         $listMapper
  76.             ->addIdentifier('id')
  77.             ->add('motivo')
  78.             ->add('os')
  79.             ->add('retiradoPor')
  80.             ->add('liberadoPor')
  81.             ->add('status')
  82.             ->add('createdAt')
  83.             ->add('_action'null, [
  84.                 'actions' => [
  85.                     'show' => [],
  86.                     'edit' => [],
  87.                     'delete' => [],
  88.                 ]
  89.             ])
  90.         ;
  91.     }
  92.     protected function configureShowFields(ShowMapper $showMapper)
  93.     {
  94.         $showMapper
  95.             ->with('Informações da Saída', ['class' => 'col-md-6'])
  96.                 ->add('id'null, ['label' => 'ID'])
  97.                 ->add('motivo'null, ['label' => 'Motivo da Saída'])
  98.                 ->add('os'null, ['label' => 'Ordem de Serviço'])
  99.                 ->add('liberadoPor'null, ['label' => 'Liberado por'])
  100.                 ->add('retiradoPor'null, ['label' => 'Retirado por'])
  101.                 ->add('status'null, ['label' => 'Status'])
  102.                 ->add('createdAt'null, ['label' => 'Data de Criação''format' => 'd/m/Y H:i'])
  103.             ->end()
  104.             ->with('Itens da Saída', ['class' => 'col-md-12'])
  105.                 ->add('itens'null, [
  106.                     'label' => 'Itens',
  107.                     'template' => 'SaidaAlmoxarifado/show_itens.html.twig'
  108.                 ])
  109.             ->end()
  110.         ;
  111.     }
  112.     public function prePersist($saida)
  113.     {
  114.         foreach ($saida->getItens() as $item) {
  115.             $item->setSaida($saida);
  116.         }
  117.         try {
  118.             $this->movService->registrarSaida($saida);
  119.         } catch (\DomainException $e) {
  120.             $this->renderErroQuantidade($e->getMessage());
  121.         }
  122.     }
  123.     public function preUpdate($saida)
  124.     {
  125.         // Você pode bloquear edição se fechada
  126.         if ($saida->getStatus() === 'FECHADA') {
  127.             throw new \RuntimeException('Saída fechada não pode ser editada.');
  128.         }
  129.         foreach ($saida->getItens() as $item) {
  130.             $item->setSaida($saida);
  131.         }
  132.         try {
  133.             $this->movService->registrarSaida($saida);
  134.         } catch (\DomainException $e) {
  135.             $this->renderErroQuantidade($e->getMessage());
  136.         }
  137.     }
  138.     private function renderErroQuantidade($mensagem)
  139.     {
  140.         // Renderiza template de erro e interrompe execução
  141.         echo \Symfony\Component\HttpFoundation\Response::create(
  142.             $this->getConfigurationPool()->getContainer()->get('twig')->render(
  143.                 'SaidaAlmoxarifado/erro_quantidade.html.twig',
  144.                 ['mensagem' => nl2br($mensagem)]
  145.             )
  146.         )->send();
  147.         exit;
  148.     }
  149. }