src/Admin/OrdemDeServicoAdmin.php line 23

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Admin;
  4. use App\Entity\InterrupcaoOs;
  5. use App\Entity\OrdemDeServico;
  6. use App\Entity\PropostaComercial;
  7. use App\Enums\ClassificacaoDespesaEnum;
  8. use App\Enums\StatusOrdemServicoEnum;
  9. use Sonata\AdminBundle\Admin\AbstractAdmin;
  10. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  11. use Sonata\AdminBundle\Datagrid\ListMapper;
  12. use Sonata\AdminBundle\Form\FormMapper;
  13. use Sonata\AdminBundle\Form\Type\ModelType;
  14. use Sonata\AdminBundle\Route\RouteCollection;
  15. use Sonata\AdminBundle\Show\ShowMapper;
  16. use Sonata\Form\Type\DatePickerType;
  17. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  18. use Sonata\Form\Type\DateRangePickerType;
  19. final class OrdemDeServicoAdmin extends BaseAdmin
  20. {
  21.     protected $datagridValues = [
  22.         '_page'         => 1,
  23.         '_sort_order'   => 'DESC',
  24.         '_sort_by'      => 'createdAt',
  25.         '_per_page'     => 1000
  26.     ];
  27.     
  28.     protected function configureRoutes(RouteCollection $collection)
  29.     {
  30.         $collection->add('osPdf');
  31.         $collection->add('relatorioDiarioObraPdf');
  32.         $collection->add('addBoletim');
  33.         $collection->add('centroCusto');
  34.         $collection->add('cronograma');
  35.         $collection->add('salvaPercentualServico');
  36.         $collection->add('relatorioCp');
  37.         $collection->add('relatorioCpv2');
  38.         $collection->add('relatoriosCps');
  39.         $collection->add('acompanhamentoMoldagem');
  40.         $collection->add('getamostra');
  41.         $collection->add('putamostra');
  42.         $collection->add('salvaordem');
  43.         $collection->add('salvaordemitem');
  44.         $collection->add('alterastatus');
  45.         $collection->add('salvarpesquisa');
  46.         $collection->add('addConcretagem');
  47.         $collection->add('equipamentosForaAlmoxarifado');
  48.     }
  49.     protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
  50.     {
  51.         $datagridMapper
  52.             ->add('cliente','doctrine_orm_callback',[
  53.                 'label'=>'Cliente Razao Social',
  54.                 'callback'   => array($this'filterCliente'),
  55.                 //'field_type' => 'text',
  56.             ])
  57.             ->add('proposta.numero',null, ['label'=>'Proposta Número'])
  58.             ->add('proposta.anoProposta',null, ['label'=>'Proposta Ano'])
  59.             //->add('createdAt', null, ['label'=>'Criado em'])
  60.             ->add('createdAt''doctrine_orm_datetime_range', ['label'=>'Data de criação''field_type'=> DateRangePickerType::class])
  61.             ->add('status')
  62.         ;
  63.     }
  64.     // Filtro de condomínio por logradouro
  65.     public function filterCliente($queryBuilder$alias$field$value)
  66.     {
  67.         if (!$value['value']) {
  68.             return;
  69.         }
  70.         $value mb_strtolower($value['value']);
  71.         $queryBuilder
  72.             ->join("$alias.proposta","prop")
  73.             ->join("prop.cliente","cli")
  74.             ->andWhere("LOWER(cli.razaoSocial) LIKE :nome" )
  75.             ->setParameter('nome'"%$value%");
  76.         return true;
  77.     }
  78.     protected function configureListFields(ListMapper $listMapper): void
  79.     {
  80.         $listMapper
  81.             ->add('id'null, ['label'=>'ID'])
  82.             ->add('proposta.cliente.razaoSocial'null, ['label'=>'Cliente'])
  83.             ->add('proposta.titulo'null, ['label'=>'Proposta''template'=>'Os/titulo_proposta.html.twig'])
  84.             ->add('proposta.numero'null, ['label'=>'Nº Proposta','template'=>'Os/numero_proposta.html.twig'])
  85.             ->add('createdAt'null, ['label'=>'Criado em'])
  86.             ->add('status'null, ['label'=>'Status''template'=>'Os/change_status_os_list.html.twig'])
  87.             ->add('_action'null, [
  88.                 'actions' => [
  89.                     //'show' => [],
  90.                     'edit' => ['template' => 'Os/menu_select.html.twig'],
  91.                    'delete' => [],
  92.                     'osPdf' => [
  93.                         'template' => 'Os/list__action_pdfOs.html.twig',
  94.                     ],
  95. //                    'relatorioDiarioObraPdf' => [
  96. //                        'template' => 'Os/list__action_pdfRelatorioDiarioObra.html.twig',
  97. //                    ],
  98. //                    'centroCusto' => [
  99. //                        'template' => 'Os/list__action_centroCusto.html.twig',
  100. //                    ],
  101. //                    'cronograma' => [
  102. //                        'template' => 'Os/list__action_cronograma.html.twig',
  103. //                    ],
  104.                 ],
  105.             ]);
  106.     }
  107.     protected function configureFormFields(FormMapper $formMapper): void
  108.     {
  109.         $formMapper
  110.             ->add('proposta'ModelType::class, [
  111.                 'placeholder' => '-- Selecione --',
  112.                 'label' => 'Proposta',
  113.                 'required' => true,
  114.                 'expanded' => false,
  115.                 'multiple' => false,
  116.                 //'query' => $this->getPropostaQuery(),
  117.             ], ['attr' => ['disabled' => true]])
  118.             ->add('status'ChoiceType::class, [
  119.                 'choices' => StatusOrdemServicoEnum::getAssociatedValues(),
  120.             ])
  121.             ->add('cno'null, ['label' => 'CNO''required' => false])
  122.             ->add('previsaoEntrega'DatePickerType::class, ['label' => 'Data prevista de entrega''required' => false])
  123.             ->add('finalizadaEm'DatePickerType::class, ['label' => 'Data de conclusão''required' => false])
  124.             ->add('observacoes'null, ['label' => "Observações"'required' => false'attr' => ['class' => 'editorHtml']]);
  125.     }
  126.     
  127.     private function getPropostaQuery(): \Doctrine\ORM\QueryBuilder
  128.     {
  129.         
  130.         $container $this->getConfigurationPool()->getContainer();
  131.         $em $container->get('doctrine.orm.entity_manager');
  132.         
  133.         // Obtenha o repositório da entidade associada
  134.         $repository $em->getRepository(PropostaComercial::class);
  135.     
  136.         // Crie o QueryBuilder para retornar apenas propostas com haveNewVersion = false
  137.         $qb $repository->createQueryBuilder('p');
  138.         $qb->where(
  139.             $qb->expr()->orX(
  140.                 $qb->expr()->eq('p.haveNewVersion'':haveNewVersion'),
  141.                 $qb->expr()->isNull('p.haveNewVersion')
  142.             )
  143.         )
  144.         ->setParameter('haveNewVersion'false);
  145.     
  146.         return $qb;
  147.     }
  148.     protected function configureShowFields(ShowMapper $showMapper): void
  149.     {
  150.         $showMapper
  151.             ->add('createdAt')
  152.             ->add('status')
  153.             ->add('observacoes')
  154.             ->add('updatedAt')
  155.             ;
  156.     }
  157.     public function preUpdate($object)
  158.     {
  159.         /** @var OrdemDeServico $object */
  160.         parent::preUpdate($object); // TODO: Change the autogenerated stub
  161.         $container $this->getConfigurationPool()->getContainer();
  162.         $em $container->get('doctrine.orm.entity_manager');
  163.         // Cria um registro de interrupção
  164.         // Se uma OS for mudada para interrompida
  165.         if($object->getStatus() == StatusOrdemServicoEnum::INTERROMPIDA){
  166.             $interrupcao = (new InterrupcaoOs())
  167.                 ->setCreatedAt(new \DateTime('now'))
  168.                 ->setStatus(StatusOrdemServicoEnum::INTERROMPIDA)
  169.                 ->setOs($object)
  170.                 ->setIsLida(false)
  171.                 ;
  172.             $em->persist($interrupcao);
  173.             $em->flush();
  174.         } else { // Qualquer outro status
  175.             // Pega a última interrupção não lida
  176.             $interrupcaoEntity $em->getRepository(InterrupcaoOs::class)->findOneBy(['os'=>$object,'isLida'=>false]);
  177.             if($interrupcaoEntity instanceof InterrupcaoOs){
  178.                 // Calcula o intervalo de interrução
  179.                 $dataUltimaInterrupcao $interrupcaoEntity->getCreatedAt();
  180.                 $today = new \DateTime('now');
  181.                 $dateInterval $dataUltimaInterrupcao->diff($today);
  182.                 $diasNaOs = (int)$object->getDiasInterrupcoes();
  183.                 $diasInterrompida = (int)$dateInterval->days;
  184.                 $totalInterrupcao $diasNaOs+$diasInterrompida;
  185.                 $object->setDiasInterrupcoes($totalInterrupcao);
  186.                 $interrupcaoEntity->setIsLida(true);
  187.                 $interrupcaoEntity->setLidaAt(new \DateTime('now'));
  188.                 $em->flush();
  189.             }
  190.         }
  191.         // Atualiza dias de interrupção da os
  192.         if(!$object->getFinalizadaEm() instanceof \DateTime && $object->getStatus() == StatusOrdemServicoEnum::CONCLUIDA){
  193.             if(!$object->getFinalizadaEm() instanceof \DateTime){
  194.                 $object->setFinalizadaEm(new \DateTime('now'));
  195.             }
  196.         }
  197.         return $object;
  198.     }
  199.     public function prePersist($object)
  200.     {
  201.         $object->setCadastradoPor($this->getUserLogado());
  202.         parent::prePersist($object); // TODO: Change the autogenerated stub
  203.     }
  204. }