src/WellCommerce/Bundle/OrderBundle/Controller/Admin/SalesReportController.php line 48

Open in your IDE?
  1. <?php
  2. declare(strict_types=0);
  3. /*
  4.  * WellCommerce Foundation
  5.  *
  6.  * This file is part of the WellCommerce package.
  7.  *
  8.  * (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
  9.  *
  10.  * For the full copyright and license information,
  11.  * please view the LICENSE file that was distributed with this source code.
  12.  */
  13. namespace WellCommerce\Bundle\OrderBundle\Controller\Admin;
  14. use Doctrine\Common\Collections\Criteria;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  16. use Symfony\Component\Filesystem\Filesystem;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Component\Serializer\Encoder\CsvEncoder;
  20. use Symfony\Component\Serializer\Serializer;
  21. use WellCommerce\Bundle\AppBundle\Service\Shop\Storage\ShopStorage;
  22. use WellCommerce\Bundle\CoreBundle\Controller\Admin\AbstractAdminController;
  23. use WellCommerce\Bundle\CoreBundle\Helper\Mailer\MailerHelper;
  24. use WellCommerce\Bundle\OrderBundle\Entity\Order;
  25. use WellCommerce\Bundle\OrderBundle\Entity\SalesReport;
  26. use WellCommerce\Bundle\OrderBundle\Form\Admin\SalesReportFormBuilder;
  27. use WellCommerce\Bundle\OrderBundle\Manager\OrderManager;
  28. use WellCommerce\Bundle\OrderBundle\Service\Report\Serializer\SalesReportNormalizer;
  29. /**
  30.  * @Route("/report", name="admin.sales_report.", options={"expose"="true"})
  31.  * @IsGranted("ROLE_ADMIN")
  32.  */
  33. class SalesReportController extends AbstractAdminController
  34. {
  35.     public function __construct(OrderManager $manager)
  36.     {
  37.         parent::__construct($manager);
  38.     }
  39.     /**
  40.      * @Route("/index/{startDate},{endDate}", name="index", defaults={"startDate"=null, "endDate"=null})
  41.      */
  42.     public function indexAction(): Response
  43.     {
  44.         $this->denyAccessUnlessGranted($this->manager->getAlias() . '.index');
  45.         return $this->displayTemplate('index');
  46.     }
  47.     /**
  48.      * @Route("/export", name="export")
  49.      */
  50.     public function exportAction(SalesReportFormBuilder $formBuilderMailerHelper $mailerHelperShopStorage $shopStorage): Response
  51.     {
  52.         $this->denyAccessUnlessGranted($this->manager->getAlias() . '.index');
  53.         $resource = new SalesReport();
  54.         $form     $formBuilder->createForm($resource, [
  55.             'name'         => 'sales_report',
  56.             'ajax_enabled' => false,
  57.         ]);
  58.         if ($form->handleRequest()->isSubmitted()) {
  59.             if ($form->isValid()) {
  60.                 $this->prepareOrders($shopStorage$resource);
  61.                 $encoders    = [new CsvEncoder()];
  62.                 $normalizers = [new SalesReportNormalizer()];
  63.                 $serializer  = new Serializer($normalizers$encoders);
  64.                 $csvContent  $serializer->serialize($resource'csv');
  65.                 $csvFileName sprintf(
  66.                     'raport_sprzedazy_%s_%s.csv',
  67.                     $resource->getValidFrom()->format('YmdHis'),
  68.                     $resource->getValidTo()->format('YmdHis')
  69.                 );
  70.                 $directory  $this->getKernel()->getRootDir() . '/sales_report/';
  71.                 $filesystem = new Filesystem();
  72.                 if (false === $filesystem->exists($directory)) {
  73.                     $filesystem->mkdir($directory);
  74.                 }
  75.                 $csvPath $directory $csvFileName;
  76.                 $filesystem->dumpFile($csvPath$csvContent);
  77.                 $mailerHelper->sendEmail([
  78.                     'recipient'     => $resource->getNotify(),
  79.                     'subject'       => sprintf(
  80.                         'Raport sprzedaży za okres %s - %s',
  81.                         $resource->getValidFrom()->format('Y-m-d H:i:s'),
  82.                         $resource->getValidTo()->format('Y-m-d H:i:s')
  83.                     ),
  84.                     'template'      => 'WellCommerceOrderBundle:Admin/Email:sales_report.html.twig',
  85.                     'configuration' => $shopStorage->getCurrentShop()->getMailerConfiguration(),
  86.                     'parameters'    => [],
  87.                     'attachments'   => [
  88.                         $csvPath,
  89.                     ],
  90.                 ]);
  91.                 $this->getFlashHelper()->addSuccess('Raport zostanie wygenerowany i wysłany na Twój adres e-mail.');
  92.                 return $this->redirectToAction('index');
  93.             }
  94.         }
  95.         return $this->displayTemplate('export', [
  96.             'form' => $form,
  97.         ]);
  98.     }
  99.     protected function prepareOrders(ShopStorage $shopStorageSalesReport $report)
  100.     {
  101.         $criteria = new Criteria();
  102.         $criteria->where($criteria->expr()->gte('createdAt'$report->getValidFrom()));
  103.         $criteria->andWhere($criteria->expr()->lte('createdAt'$report->getValidTo()));
  104.         $criteria->andWhere($criteria->expr()->eq('confirmed'true));
  105.         $criteria->andWhere($criteria->expr()->eq('shop'$shopStorage->getCurrentShop()));
  106.         $collection $this->getEntityManager()->getRepository(Order::class)->getCollection($criteria);
  107.         $orders $collection->filter(function (Order $order) use ($report) {
  108.             return
  109.                 $report->getPaymentMethods()->contains($order->getPaymentMethod()) &&
  110.                 $report->getShippingMethods()->contains($order->getShippingMethod()) &&
  111.                 $report->getOrderStatuses()->contains($order->getCurrentStatus());
  112.         });
  113.         $report->setOrders($orders);
  114.     }
  115. }