src/Flexy/FrontBundle/Themes/IlaveU/Controller/ShopAdmin/Order/OrderCrudController.php line 226
<?php
namespace App\Flexy\FrontBundle\Themes\IlaveU\Controller\ShopAdmin\Order;
use App\Entity\LdryOrder;
use App\Flexy\FrontBundle\Themes\IlaveU\Form\Order\MetaDataType;
use App\Flexy\ShopBundle\Entity\Customer\Customer;
use App\Flexy\ShopBundle\Entity\Order\Order;
use App\Flexy\ShopBundle\Entity\Resource\Agent;
use App\Flexy\ShopBundle\Entity\Vendor\Vendor;
use App\Flexy\ShopBundle\Form\Order\OrderItemType;
use App\Flexy\ShopBundle\Repository\Order\OrderRepository;
use App\Service\FlexySettingsProvider;
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\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\Field;
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
use EasyCorp\Bundle\EasyAdminBundle\Orm\EntityRepository;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
use EasyCorp\Bundle\EasyAdminBundle\Field\MoneyField;
use EasyCorp\Bundle\EasyAdminBundle\Field\NumberField;
use EasyCorp\Bundle\EasyAdminBundle\Filter\EntityFilter;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class OrderCrudController extends AbstractCrudController
{
public function __construct(private readonly ManagerRegistry $doctrine, private readonly EntityRepository $entityRepository)
{
}
public static function getEntityFqcn(): string
{
return Order::class;
}
public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
{
$em = $this->doctrine->getManager();
parent::createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters);
$response = $this->entityRepository->createQueryBuilder($searchDto, $entityDto, $fields, $filters);
$response->leftJoin("entity.customer","customer");
$response->andWhere("entity.status <> 'draft'");
$response->andWhere("customer IS NOT NULL AND customer.deletedAt IS NULL ");
if($this->isGranted("ROLE_VENDOR")){
$vendor = $em->getRepository(Vendor::class)->findOneBy(["user"=>$this->getUser()]);
$response->leftJoin("entity.orderItems ","orderItems");
$response->leftJoin("orderItems.product ","product");
$response->leftJoin("entity.vendor ","vendor");
$response->andWhere("vendor = :vendor");
$response->andWhere("entity.startDeliveryAt IS NOT NULL AND entity.deliveryType IS NOT NULL");
$response->setParameter("vendor",$vendor);
}
elseif($this->isGranted("ROLE_AGENT")){
$agent = $em->getRepository(Agent::class)->findOneBy(["user"=>$this->getUser()]);
$response->leftJoin("entity.agent ","agent");
$response->andWhere("agent = :agent");
$response->andWhere("entity.startDeliveryAt IS NOT NULL AND entity.deliveryType IS NOT NULL");
$response->setParameter("agent",$agent);
}
else{
$response->leftJoin("entity.orderItems ","orderItems");
$response->leftJoin("orderItems.product ","product");
$response->andWhere("product.vendor IS NULL");
$response->andWhere("entity.startProcessingAt IS NOT NULL");
}
return $response;
}
public function persistEntity(EntityManagerInterface $entityManager, $entityInstance): void
{
$customer = $entityInstance->getCustomer();
if(!$entityInstance->getCustomer()){
$customer = new Customer;
$customer->setFirstName($entityInstance->getFirstName());
$customer->setLastName($entityInstance->getLastName());
$customer->setPhone($entityInstance->getTel());
$customer->setEmail($entityInstance->getEmail());
$entityManager->persist($customer);
}
$orderItems = $entityInstance->getOrderItems();
$arrayCartLdryOrder = [];
foreach($orderItems as $singleOrderItem){
// Categories
$product = $singleOrderItem->getProduct();
$singleOrderItem->setPrice($product->getPrice());
$singleOrderItem->setParentOrder($entityInstance);
$entityManager->persist($singleOrderItem);
}
$entityInstance->setCountry("Maroc");
$entityInstance->setCustomer($customer);
$entityManager->persist($entityInstance);
$entityManager->flush();
}
public function updateEntity(EntityManagerInterface $entityManager, $entityInstance): void
{
$orderItems = $entityInstance->getOrderItems();
foreach($orderItems as $singleOrderItem){
if($singleOrderItem->getPrice() == null){
$product = $singleOrderItem->getProduct();
$singleOrderItem->setPrice($product->getPrice());
}
$singleOrderItem->setParentOrder($entityInstance);
$entityManager->persist($singleOrderItem);
}
$entityManager->persist($entityInstance);
$entityManager->flush();
}
public function configureFilters(Filters $filters): Filters
{
return $filters
->add("customer")
->add("createdAt","Date de creation")
->add("startProcessingAt")
->add("startDeliveryAt")
->add("source")
->add("shippingMethod")
->add("agent")
->add("status")
->add("statusShipping")
;
}
public function configureCrud(Crud $crud): Crud
{
return $crud
->setDefaultSort(['createdAt' => 'DESC'])
->setPaginatorPageSize(20)
->overrideTemplates([
"crud/detail"=>"@Flexy/FrontBundle/Themes/IlaveU/templates/admin/shopadmin/order/orderDetail.html.twig",
//"crud/index"=>"@Flexy/ShopBundle/templates/orders/index.html.twig",
])
->renderContentMaximized(true)
;
}
public function configureActions(Actions $actions): Actions
{
$editOrder = Action::new('editOrder', 'Modifier la commande', null)
->linkToRoute('admin_pos_ilaveu', fn(Order $order): array => [
'orderId' =>$order->getId(),
])->setIcon("fa fa-link");
$printOrder = Action::new('printOrder', 'Imprimer', null)
->linkToRoute('order_ticket', fn(Order $order): array => [
'id' =>$order->getId(),
])->setLabel("Imprimer ticket")->setIcon("fa fa-link");
$exportExcel = Action::new('exportExcel')
->linkToCrudAction('exportExcel')
->setCssClass('btn btn-primary action-foo')
->setIcon("fa-solid fa-file-excel")
->setLabel("Exporter en excel")
->createAsGlobalAction();
return $actions
->add(Crud::PAGE_INDEX, Action::DETAIL, fn(Action $action) => $action->setIcon("fa fa-link")->setLabel("Consulter"))
->add(Crud::PAGE_INDEX, $exportExcel)
->add(Crud::PAGE_INDEX, $editOrder)
->add(Crud::PAGE_INDEX,$printOrder)
//->remove(Crud::PAGE_INDEX, Action::EDIT)
->update(Crud::PAGE_INDEX, Action::EDIT, fn(Action $action) => $action->setIcon("fa fa-link")->setLabel("Affectation"))
->remove(Crud::PAGE_INDEX, Action::DELETE)
->remove(Crud::PAGE_INDEX, Action::NEW)
;
}
public function exportExcel(AdminContext $context)
{
$url = $this->container->get(AdminUrlGenerator::class)
->setController(ExportExcelOrderCrudController::class)
->setAction(Action::NEW)
->generateUrl();
return $this->redirect($url);
}
#[Route("/pos-ilaveu/{_locale}",name:"admin_pos_ilaveu")]
public function posSystem(FlexySettingsProvider $flexySettingsProvider,Request $request,OrderRepository $orderRepository)
{
$order = null;
if( array_key_exists("routeParams",$request->query->all())){
$orderId = $request->query->all()["routeParams"]["orderId"];
$order = $orderRepository->find((int)$orderId);
}
return $this->render("@Flexy/FrontBundle/Themes/IlaveU/templates/admin/shopadmin/order/possystem.html.twig",[
'order'=>$order
]);
}
#[Route("/orders/ticket/{id}",name:"order_ticket")]
public function printTicket(Order $order)
{
return $this->render("@Flexy/Apps/EcommerceBundle/templates/order/thermalTicket.html.twig",[
"order" => $order,
]);
}
public function configureFields(string $pageName): iterable
{
return [
FormField::addTab("Detail de la commande"),
TextField::new('idPrefixed',"Num CMD")->setDisabled(true)->setTemplatePath("@Flexy/FrontBundle/Themes/IlaveU/templates/admin/shopadmin/order/field/idPrefixedWithBadge.html.twig"),
//AssociationField::new('customer')->setTemplatePath("@Flexy/ShopBundle/templates/orders/field/customer.html.twig")->onlyOnIndex(),
AssociationField::new('customer',"Client")->setTemplatePath("@Flexy/FrontBundle/Themes/IlaveU/templates/admin/shopadmin/order/field/customer.html.twig")->onlyOnIndex(),
//MoneyField::new('walletPaymentAmount',"Crédit utilisé")->setCurrency("MAD")->onlyOnIndex(),
MoneyField::new("fullTotalAmount","Montant")->setCurrency("MAD")->setStoredAsCents(false)->onlyOnIndex()->setTemplatePath("@Flexy/FrontBundle/Themes/IlaveU/templates/admin/shopadmin/order/field/fullTotalAmount.html.twig"),
MoneyField::new("fullRestAmount","Reste à payer")->setCurrency("MAD")->setStoredAsCents(false)->onlyOnIndex()
->setTemplatePath("@Flexy/FrontBundle/Themes/IlaveU/templates/admin/shopadmin/order/field/fullRestAmount.html.twig"),
TextField::new('tel')->onlyOnIndex()->setTemplatePath("@Flexy/FrontBundle/Themes/IlaveU/templates/admin/shopadmin/order/field/tel.html.twig"),
DateTimeField::new('createdAt',"Date de création")->onlyOnIndex(),
DateTimeField::new('startProcessingAt',"Date Collecte")->setTemplatePath("@Flexy/ShopBundle/templates/orders/field/startCollectAt.html.twig")->onlyOnIndex(),
DateTimeField::new('startDeliveryAt',"Date Liv")->setTemplatePath("@Flexy/ShopBundle/templates/orders/field/startDeliveryAt.html.twig")->onlyOnIndex(),
ChoiceField::new('source')->setChoices([
"Old Mobile APP"=>"old-mobile-app",
"Old WebSite"=>"old-website",
"Old ADMIN"=>"old-admin",
"Old POS"=>"old-pos",
"ADMIN"=>"admin",
"WEBSITE"=>"website",
"App mobile"=>"mobile",
])->onlyOnIndex(),
AssociationField::new('shippingMethod',"Mode de livraison")->onlyOnIndex(),
ChoiceField::new('deliveryType',"Type de collecte")->setChoices([
"Client comptoire"=>"on-store",
"Collecte et livraison"=>"collecte-and-delivery",
"Collecte"=>"collecte",
"Livraison"=>"delivery",
])->setTemplatePath("@Flexy/ShopBundle/templates/orders/field/badgeText.html.twig")->onlyOnIndex(),
AssociationField::new('agent',"Livreur")->setQueryBuilder(function($queryBuilder){
$queryBuilder->leftJoin("entity.user","user");
$queryBuilder->andWhere('user.roles LIKE :role');
$queryBuilder->setParameter("role","%ROLE_SHIPPING_AGENT%");
}),
AssociationField::new('store',"Transferer au Store")->hideOnIndex(),
TextField::new('collectAddress',"Adr. Collecte"),
TextField::new('shippingAddress',"Adr. Livraison"),
MoneyField::new('shippingTips',"Pourboire")->setStoredAsCents(false)->onlyOnIndex()->setPermission("ROLE_AGENT")->setCurrency('EUR'),
BooleanField::new('isToDo',"Ajouter à mon programme")->onlyOnIndex()->setPermission("ROLE_AGENT"),
//FormField::addTab("Montant payé")->onlyOnIndex(),
// NumberField::new("payedAmount","Montant payé")->setTemplatePath("@Flexy/ShopBundle/templates/orders/field/payedAmount.html.twig")->onlyOnIndex(),
ChoiceField::new('status',"Status de réglement")->setChoices([
"Non complété"=>"draft",
"En attente"=>"waiting",
"En cours de prépratation"=>"processing",
"En cours de livraison"=>"shipping",
"Articles livrés"=>"shipped",
"Annulé"=>"cancelled",
])->setTemplatePath("@Flexy/FrontBundle/Themes/IlaveU/templates/admin/shopadmin/order/field/statusOrderPayment.html.twig")->onlyOnIndex(),
NumberField::new("nextStepShipment","Status de livraison")->setTemplatePath("@Flexy/FrontBundle/Themes/IlaveU/templates/admin/shopadmin/order/field/statusOrderShipping.html.twig")->onlyOnIndex()->onlyOnIndex(),
];
}
}