src/Flexy/FrontBundle/Themes/IlaveU/Controller/ShopAdmin/Order/OrderCrudController.php line 226

  1. <?php
  2. namespace App\Flexy\FrontBundle\Themes\IlaveU\Controller\ShopAdmin\Order;
  3. use App\Entity\LdryOrder;
  4. use App\Flexy\FrontBundle\Themes\IlaveU\Form\Order\MetaDataType;
  5. use App\Flexy\ShopBundle\Entity\Customer\Customer;
  6. use App\Flexy\ShopBundle\Entity\Order\Order;
  7. use App\Flexy\ShopBundle\Entity\Resource\Agent;
  8. use App\Flexy\ShopBundle\Entity\Vendor\Vendor;
  9. use App\Flexy\ShopBundle\Form\Order\OrderItemType;
  10. use App\Flexy\ShopBundle\Repository\Order\OrderRepository;
  11. use App\Service\FlexySettingsProvider;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  14. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  15. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  16. use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
  17. use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
  18. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  19. use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
  20. use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
  21. use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
  22. use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
  23. use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
  24. use EasyCorp\Bundle\EasyAdminBundle\Field\Field;
  25. use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
  26. use Doctrine\ORM\QueryBuilder;
  27. use Doctrine\Persistence\ManagerRegistry;
  28. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  29. use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
  30. use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
  31. use EasyCorp\Bundle\EasyAdminBundle\Orm\EntityRepository;
  32. use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
  33. use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
  34. use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
  35. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  36. use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
  37. use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
  38. use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
  39. use EasyCorp\Bundle\EasyAdminBundle\Field\MoneyField;
  40. use EasyCorp\Bundle\EasyAdminBundle\Field\NumberField;
  41. use EasyCorp\Bundle\EasyAdminBundle\Filter\EntityFilter;
  42. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  43. use Symfony\Component\HttpFoundation\Request;
  44. use Symfony\Component\Routing\Annotation\Route;
  45. class OrderCrudController extends AbstractCrudController
  46. {
  47.     public function __construct(private readonly ManagerRegistry $doctrine, private readonly EntityRepository $entityRepository)
  48.     {
  49.     }
  50.     public static function getEntityFqcn(): string
  51.     {
  52.         return Order::class;
  53.     }
  54.     
  55.     public function createIndexQueryBuilder(SearchDto $searchDtoEntityDto $entityDtoFieldCollection $fieldsFilterCollection $filters): QueryBuilder
  56.     {
  57.         $em $this->doctrine->getManager();
  58.         
  59.         
  60.         parent::createIndexQueryBuilder($searchDto$entityDto$fields$filters);
  61.         
  62.         
  63.         $response $this->entityRepository->createQueryBuilder($searchDto$entityDto$fields$filters);
  64.         $response->leftJoin("entity.customer","customer");
  65.         $response->andWhere("entity.status <> 'draft'");
  66.         $response->andWhere("customer IS NOT NULL AND customer.deletedAt IS NULL ");
  67.         if($this->isGranted("ROLE_VENDOR")){
  68.             
  69.             $vendor $em->getRepository(Vendor::class)->findOneBy(["user"=>$this->getUser()]);
  70.             $response->leftJoin("entity.orderItems ","orderItems");
  71.             $response->leftJoin("orderItems.product ","product");
  72.             $response->leftJoin("entity.vendor ","vendor");
  73.             $response->andWhere("vendor = :vendor");
  74.             $response->andWhere("entity.startDeliveryAt IS NOT NULL AND entity.deliveryType IS NOT NULL");
  75.             $response->setParameter("vendor",$vendor);
  76.         }
  77.         elseif($this->isGranted("ROLE_AGENT")){
  78.             
  79.             $agent $em->getRepository(Agent::class)->findOneBy(["user"=>$this->getUser()]);
  80.             $response->leftJoin("entity.agent ","agent");
  81.             $response->andWhere("agent = :agent");
  82.             $response->andWhere("entity.startDeliveryAt IS NOT NULL AND entity.deliveryType IS NOT NULL");
  83.             $response->setParameter("agent",$agent);
  84.         }
  85.         else{
  86.             $response->leftJoin("entity.orderItems ","orderItems");
  87.             $response->leftJoin("orderItems.product ","product");
  88.             $response->andWhere("product.vendor IS NULL");
  89.             $response->andWhere("entity.startProcessingAt IS NOT NULL");
  90.         }
  91.         
  92.         return $response;
  93.     }
  94.     public function persistEntity(EntityManagerInterface $entityManager$entityInstance): void
  95.     {
  96.         $customer $entityInstance->getCustomer();
  97.         if(!$entityInstance->getCustomer()){
  98.             $customer = new Customer;
  99.             $customer->setFirstName($entityInstance->getFirstName());
  100.             $customer->setLastName($entityInstance->getLastName());
  101.             $customer->setPhone($entityInstance->getTel());
  102.             $customer->setEmail($entityInstance->getEmail());
  103.             $entityManager->persist($customer);
  104.         }
  105.         
  106.         $orderItems $entityInstance->getOrderItems();
  107.         $arrayCartLdryOrder = [];
  108.         foreach($orderItems as $singleOrderItem){
  109.             // Categories 
  110.             $product $singleOrderItem->getProduct();
  111.             
  112.             $singleOrderItem->setPrice($product->getPrice());
  113.             $singleOrderItem->setParentOrder($entityInstance);
  114.             $entityManager->persist($singleOrderItem);
  115.             
  116.         }
  117.         
  118.         
  119.         $entityInstance->setCountry("Maroc");
  120.         $entityInstance->setCustomer($customer);
  121.         $entityManager->persist($entityInstance);
  122.         $entityManager->flush();
  123.     }
  124.     
  125.     public function updateEntity(EntityManagerInterface $entityManager$entityInstance): void
  126.     {
  127.         $orderItems $entityInstance->getOrderItems();
  128.         foreach($orderItems as $singleOrderItem){
  129.             if($singleOrderItem->getPrice() == null){
  130.                 $product $singleOrderItem->getProduct();
  131.                 $singleOrderItem->setPrice($product->getPrice());
  132.             }
  133.             $singleOrderItem->setParentOrder($entityInstance);
  134.             
  135.             
  136.             $entityManager->persist($singleOrderItem);
  137.             
  138.         }
  139.         $entityManager->persist($entityInstance);
  140.         $entityManager->flush();
  141.     }
  142.     public function configureFilters(Filters $filters): Filters
  143.     {
  144.         return $filters
  145.                 ->add("customer")
  146.                 ->add("createdAt","Date de creation")
  147.                 ->add("startProcessingAt")
  148.                 ->add("startDeliveryAt")
  149.                 ->add("source")
  150.                 ->add("shippingMethod")
  151.                 ->add("agent")
  152.                 ->add("status")
  153.                 ->add("statusShipping")
  154.                 ;
  155.     }
  156.     
  157.     public function configureCrud(Crud $crud): Crud
  158.     {
  159.         return $crud
  160.         ->setDefaultSort(['createdAt' => 'DESC'])
  161.         ->setPaginatorPageSize(20)
  162.         ->overrideTemplates([
  163.             "crud/detail"=>"@Flexy/FrontBundle/Themes/IlaveU/templates/admin/shopadmin/order/orderDetail.html.twig",
  164.             //"crud/index"=>"@Flexy/ShopBundle/templates/orders/index.html.twig",
  165.         ])
  166.         ->renderContentMaximized(true)
  167.         ;
  168.     }
  169.     public function configureActions(Actions $actions): Actions
  170.     {
  171.         $editOrder Action::new('editOrder''Modifier la commande'null)
  172.             
  173.             ->linkToRoute('admin_pos_ilaveu', fn(Order $order): array => [
  174.                 'orderId' =>$order->getId(),
  175.             ])->setIcon("fa fa-link");
  176.         $printOrder Action::new('printOrder''Imprimer'null)
  177.             
  178.             ->linkToRoute('order_ticket', fn(Order $order): array => [
  179.                 'id' =>$order->getId(),
  180.             ])->setLabel("Imprimer ticket")->setIcon("fa fa-link");
  181.         $exportExcel Action::new('exportExcel')
  182.             ->linkToCrudAction('exportExcel')
  183.             ->setCssClass('btn btn-primary action-foo')
  184.             ->setIcon("fa-solid fa-file-excel")
  185.             ->setLabel("Exporter en excel")
  186.             ->createAsGlobalAction();
  187.             
  188.         return $actions
  189.             ->add(Crud::PAGE_INDEXAction::DETAIL, fn(Action $action) => $action->setIcon("fa fa-link")->setLabel("Consulter"))
  190.             ->add(Crud::PAGE_INDEX$exportExcel)
  191.             ->add(Crud::PAGE_INDEX$editOrder)
  192.             ->add(Crud::PAGE_INDEX,$printOrder)
  193.             //->remove(Crud::PAGE_INDEX, Action::EDIT)
  194.             ->update(Crud::PAGE_INDEXAction::EDIT, fn(Action $action) => $action->setIcon("fa fa-link")->setLabel("Affectation"))
  195.             ->remove(Crud::PAGE_INDEXAction::DELETE)
  196.             ->remove(Crud::PAGE_INDEXAction::NEW)
  197.         ;
  198.     }
  199.     public function exportExcel(AdminContext $context)
  200.     {
  201.         
  202.         $url $this->container->get(AdminUrlGenerator::class)
  203.             ->setController(ExportExcelOrderCrudController::class)
  204.             ->setAction(Action::NEW)
  205.             ->generateUrl();
  206.             return $this->redirect($url);
  207.     }
  208.     #[Route("/pos-ilaveu/{_locale}",name:"admin_pos_ilaveu")]
  209.     public function posSystem(FlexySettingsProvider $flexySettingsProvider,Request $request,OrderRepository $orderRepository)
  210.     {
  211.         $order null;
  212.         if( array_key_exists("routeParams",$request->query->all())){
  213.             $orderId $request->query->all()["routeParams"]["orderId"];
  214.             $order $orderRepository->find((int)$orderId);
  215.         }
  216.         return $this->render("@Flexy/FrontBundle/Themes/IlaveU/templates/admin/shopadmin/order/possystem.html.twig",[
  217.             'order'=>$order
  218.         ]);
  219.     }
  220.     #[Route("/orders/ticket/{id}",name:"order_ticket")]
  221.     public function printTicket(Order $order)
  222.     {
  223.             return $this->render("@Flexy/Apps/EcommerceBundle/templates/order/thermalTicket.html.twig",[
  224.                 "order" => $order,
  225.             ]);
  226.     }
  227.     public function configureFields(string $pageName): iterable
  228.     {
  229.         return [
  230.             FormField::addTab("Detail de la commande"),
  231.             
  232.             TextField::new('idPrefixed',"Num CMD")->setDisabled(true)->setTemplatePath("@Flexy/FrontBundle/Themes/IlaveU/templates/admin/shopadmin/order/field/idPrefixedWithBadge.html.twig"),
  233.             //AssociationField::new('customer')->setTemplatePath("@Flexy/ShopBundle/templates/orders/field/customer.html.twig")->onlyOnIndex(),
  234.             AssociationField::new('customer',"Client")->setTemplatePath("@Flexy/FrontBundle/Themes/IlaveU/templates/admin/shopadmin/order/field/customer.html.twig")->onlyOnIndex(),
  235.             //MoneyField::new('walletPaymentAmount',"Crédit utilisé")->setCurrency("MAD")->onlyOnIndex(),
  236.             
  237.             MoneyField::new("fullTotalAmount","Montant")->setCurrency("MAD")->setStoredAsCents(false)->onlyOnIndex()->setTemplatePath("@Flexy/FrontBundle/Themes/IlaveU/templates/admin/shopadmin/order/field/fullTotalAmount.html.twig"),
  238.             MoneyField::new("fullRestAmount","Reste à payer")->setCurrency("MAD")->setStoredAsCents(false)->onlyOnIndex()
  239.             ->setTemplatePath("@Flexy/FrontBundle/Themes/IlaveU/templates/admin/shopadmin/order/field/fullRestAmount.html.twig"),
  240.             
  241.             TextField::new('tel')->onlyOnIndex()->setTemplatePath("@Flexy/FrontBundle/Themes/IlaveU/templates/admin/shopadmin/order/field/tel.html.twig"),
  242.             DateTimeField::new('createdAt',"Date de création")->onlyOnIndex(),
  243.             DateTimeField::new('startProcessingAt',"Date Collecte")->setTemplatePath("@Flexy/ShopBundle/templates/orders/field/startCollectAt.html.twig")->onlyOnIndex(),
  244.             DateTimeField::new('startDeliveryAt',"Date Liv")->setTemplatePath("@Flexy/ShopBundle/templates/orders/field/startDeliveryAt.html.twig")->onlyOnIndex(),
  245.             ChoiceField::new('source')->setChoices([
  246.                 "Old Mobile APP"=>"old-mobile-app",
  247.                 "Old WebSite"=>"old-website",
  248.                 "Old ADMIN"=>"old-admin",
  249.                 "Old POS"=>"old-pos",
  250.                 "ADMIN"=>"admin",
  251.                 "WEBSITE"=>"website",
  252.                 "App mobile"=>"mobile",
  253.             ])->onlyOnIndex(),
  254.             AssociationField::new('shippingMethod',"Mode de livraison")->onlyOnIndex(),
  255.             
  256.             ChoiceField::new('deliveryType',"Type de collecte")->setChoices([
  257.                 "Client comptoire"=>"on-store",
  258.                 "Collecte et livraison"=>"collecte-and-delivery",
  259.                 "Collecte"=>"collecte",
  260.                 "Livraison"=>"delivery",
  261.             ])->setTemplatePath("@Flexy/ShopBundle/templates/orders/field/badgeText.html.twig")->onlyOnIndex(),
  262.             
  263.              AssociationField::new('agent',"Livreur")->setQueryBuilder(function($queryBuilder){
  264.                 $queryBuilder->leftJoin("entity.user","user");
  265.                 $queryBuilder->andWhere('user.roles LIKE :role');
  266.                 $queryBuilder->setParameter("role","%ROLE_SHIPPING_AGENT%");
  267.             }),
  268.             
  269.             AssociationField::new('store',"Transferer au Store")->hideOnIndex(),
  270.             TextField::new('collectAddress',"Adr. Collecte"),
  271.             TextField::new('shippingAddress',"Adr. Livraison"),
  272.             MoneyField::new('shippingTips',"Pourboire")->setStoredAsCents(false)->onlyOnIndex()->setPermission("ROLE_AGENT")->setCurrency('EUR'),
  273.             BooleanField::new('isToDo',"Ajouter à mon programme")->onlyOnIndex()->setPermission("ROLE_AGENT"),
  274.             //FormField::addTab("Montant payé")->onlyOnIndex(),
  275.             // NumberField::new("payedAmount","Montant payé")->setTemplatePath("@Flexy/ShopBundle/templates/orders/field/payedAmount.html.twig")->onlyOnIndex(),
  276.             ChoiceField::new('status',"Status de réglement")->setChoices([
  277.                 "Non complété"=>"draft",                
  278.                 "En attente"=>"waiting",
  279.                 "En cours de prépratation"=>"processing",
  280.                 "En cours de livraison"=>"shipping",
  281.                 "Articles livrés"=>"shipped",
  282.                 "Annulé"=>"cancelled",
  283.             ])->setTemplatePath("@Flexy/FrontBundle/Themes/IlaveU/templates/admin/shopadmin/order/field/statusOrderPayment.html.twig")->onlyOnIndex(),
  284.             NumberField::new("nextStepShipment","Status de livraison")->setTemplatePath("@Flexy/FrontBundle/Themes/IlaveU/templates/admin/shopadmin/order/field/statusOrderShipping.html.twig")->onlyOnIndex()->onlyOnIndex(),
  285.         ];
  286.     }
  287.     
  288. }