<?php
namespace App\Controller\Admin;
use App\Entity\Cabecera;
use App\Entity\Sucursal;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
use EasyCorp\Bundle\EasyAdminBundle\Field\NumberField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Filter\BooleanFilter;
use EasyCorp\Bundle\EasyAdminBundle\Filter\ChoiceFilter;
use EasyCorp\Bundle\EasyAdminBundle\Filter\DateTimeFilter;
use EasyCorp\Bundle\EasyAdminBundle\Filter\EntityFilter;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
// use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
use App\Controller\Admin\StatusTrait;
class CabeceraCrudController extends AbstractCrudController
{
use StatusTrait;
private $entityManager;
public static function getEntityFqcn(): string
{
return Cabecera::class;
}
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function configureCrud(Crud $crud): Crud
{
return $crud
->setDefaultSort(['createdAt' => 'DESC'])
->showEntityActionsInlined()
->setPageTitle('index', '%entity_label_plural%')
->setPageTitle('detail', '%entity_label_singular% #%entity_id%')
->setEntityLabelInSingular('Pedido')
->setEntityLabelInPlural('Pedidos')
->setSearchFields(['nombrecliente', 'telefonocliente', 'user.username'])
->setDefaultSort(['createdAt' => 'DESC'])
->setDateTimeFormat('dd/MM/yy H:mm')
->setPaginatorPageSize(30)
->setHelp(Crud::PAGE_INDEX, 'Todos lo pedidos de la plataforma');
}
public function configureFields(string $pageName): iterable
{
return [
FormField::addPanel('Datos De Facturación')->setIcon('fa fa-address-card'),
TextField::new('nombrecliente', 'Nombre'),
TextField::new('telefonocliente', 'Telefono'),
TextField::new('direccioncliente', 'Dirección'),
TextField::new('emailcliente', 'Email')->onlyOnDetail(),
FormField::addPanel('Datos De Envío')->setIcon('fa fa-truck '),
TextField::new('nombre_receptor', 'Nombre')->onlyOnDetail(),
TextField::new('telefono_receptor', 'Teléfono')->onlyOnDetail(),
TextField::new('observaciones_receptor', 'Observaciones')->onlyOnDetail(),
FormField::addPanel('Datos Del Pedido')->setIcon('fa fa-bars'),
TextField::new('sucursal', 'Sucursal'),
DateTimeField::new('fechareserva', 'Programado'),
TextField::new('metododepago', 'Metodo De Pago')->onlyOnDetail(),
TextField::new('email_linkdepago', 'Email Link')->onlyOnDetail(),
TextField::new('linkdepago', 'Link de pago')->onlyOnDetail(),
TextField::new('ultimolinkdepago', 'Estado de la transacción')
->formatValue(function ($value, $entity) {
$ultimoLinkdepago = $value;
if ($ultimoLinkdepago === null) {
return sprintf('<span class="badge badge-%s">%s</span>', 'neutro', htmlspecialchars('N/A', ENT_QUOTES, 'UTF-8'));
}
$statusName = $this->mapStatusNumberToName($ultimoLinkdepago); // Esta función convierte el número en un nombre de estado.
$badgeColor = $this->getBadgeColorForStatus($ultimoLinkdepago);
return sprintf('<span class="badge badge-%s">%s</span>', $badgeColor, htmlspecialchars($statusName, ENT_QUOTES, 'UTF-8'));
})->onlyOnDetail(),
TextField::new('filename', 'Cod Unico')->onlyOnDetail(),
BooleanField::new('isEnviado', 'XML recibido')->renderAsSwitch(false)->hideOnForm(),
ChoiceField::new('Tipodeservicio', 'Tipo')
->setChoices([
'DOMICILIO' => 4,
'RECOGER' => 16,
'PROGRAMADO' => 100,
])
->hideOnDetail(),
IntegerField::new('user', 'Vendedor'),
TextField::new('estado', 'Estado')->hideOnForm(),
DateTImeField::new('createdAt', 'Creado'),
FormField::addPanel('Log Link de pago')->setIcon('fa fa-list'),
CollectionField::new('CabeceraLinkdepago', 'Link de pago')->setTemplatePath('admin/field/linkdepago.html.twig')->onlyOnDetail(),
FormField::addPanel('Pedido')->setIcon('fa fa-tag'),
CollectionField::new('lineas', 'Productos')->setTemplatePath('admin/field/lineas.html.twig')->onlyOnDetail(),
NumberField::new('propinatotal', 'Propina')->onlyOnDetail(),
NumberField::new('Total', 'Total')->onlyOnDetail(),
FormField::addPanel('Log')->setIcon('fa fa-list'),
CollectionField::new('cabecera_status', 'Estado')->setTemplatePath('admin/field/status.html.twig')->onlyOnDetail(),
];
}
public function configureActions(Actions $actions): Actions
{
$editar = Action::new('editar', 'Editar')
->displayIf(static function ($entity) {
$estados = array('PROGRAMADO', 'EDICION', "INICIADO");
// return $entity->getEstado() == 'PROGRAMADO';
return in_array($entity->getEstado(), $estados);
})
->addCssClass('btn text-success')
->linkToRoute('cc_favoritos', function (Cabecera $cabecera): array {
return [
'id' => $cabecera->getId(),
'action' => 'finalizar'
];
});
$anular = Action::new('anular', 'Anular')
->displayIf(static function ($entity) {
$estados = array('PROCESANDO');
// if(($entity->getEstado() != 'PROGRAMADO') and ($entity->getIsEnviado() === false)){
if (in_array($entity->getEstado(), $estados) and ($entity->getIsEnviado() === false)) {
$r = true;
} else {
$r = false;
}
return $r;
})
->addCssClass('btn text-danger')
->linkToRoute('cambiar_estado', function (Cabecera $cabecera): array {
return [
'id' => $cabecera->getId(),
'action' => 'anular'
];
});
$cancelar = Action::new('cancelar', 'Cancelar')
->displayIf(static function ($entity) {
$estados = array('INICIADO', 'PROGRAMADO');
// return $entity->getEstado() == 'PROGRAMADO';
if (in_array($entity->getEstado(), $estados) and ($entity->getIsEnviado() === false)) {
$r = true;
} else {
$r = false;
}
return $r;
})
->addCssClass('btn text-warning')
->linkToRoute('cambiar_estado', function (Cabecera $cabecera): array {
return [
'id' => $cabecera->getId(),
'action' => 'cancelar'
];
});
return $actions
->add(Crud::PAGE_INDEX, Action::DETAIL)
->add(Crud::PAGE_DETAIL, $anular)
->add(Crud::PAGE_DETAIL, $cancelar)
->add(Crud::PAGE_DETAIL, $editar)
->remove(Crud::PAGE_INDEX, Action::DELETE)
->remove(Crud::PAGE_INDEX, Action::EDIT)
->remove(Crud::PAGE_INDEX, Action::NEW)
->remove(Crud::PAGE_DETAIL, Action::EDIT)
->remove(Crud::PAGE_DETAIL, Action::DELETE)
->setPermission($anular, 'ROLE_ADMIN');
}
public function configureFilters(Filters $filters): Filters
{
$sucursales = $this->entityManager->getRepository(Sucursal::class)->findAll();
$choices = [];
foreach ($sucursales as $i => $sucursal) {
$choices[$sucursal->getNombre()] = $sucursal->getNombre();
}
// return $choices;
return $filters
->add(BooleanFilter::new('isFinalizada', 'Finalizada'))
->add(DateTimeFilter::new('createdAt', 'Fecha de Creación'))
->add(ChoiceFilter::new('sucursal', 'Sucursal')
->setChoices($choices));
}
}