<?php
declare(strict_types=0);
/*
* WellCommerce Foundation
*
* This file is part of the WellCommerce package.
*
* (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/
namespace WellCommerce\Bundle\OrderBundle\Controller\Admin;
use Doctrine\Common\Collections\Criteria;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Encoder\CsvEncoder;
use Symfony\Component\Serializer\Serializer;
use WellCommerce\Bundle\AppBundle\Service\Shop\Storage\ShopStorage;
use WellCommerce\Bundle\CoreBundle\Controller\Admin\AbstractAdminController;
use WellCommerce\Bundle\CoreBundle\Helper\Mailer\MailerHelper;
use WellCommerce\Bundle\OrderBundle\Entity\Order;
use WellCommerce\Bundle\OrderBundle\Entity\SalesReport;
use WellCommerce\Bundle\OrderBundle\Form\Admin\SalesReportFormBuilder;
use WellCommerce\Bundle\OrderBundle\Manager\OrderManager;
use WellCommerce\Bundle\OrderBundle\Service\Report\Serializer\SalesReportNormalizer;
/**
* @Route("/report", name="admin.sales_report.", options={"expose"="true"})
* @IsGranted("ROLE_ADMIN")
*/
class SalesReportController extends AbstractAdminController
{
public function __construct(OrderManager $manager)
{
parent::__construct($manager);
}
/**
* @Route("/index/{startDate},{endDate}", name="index", defaults={"startDate"=null, "endDate"=null})
*/
public function indexAction(): Response
{
$this->denyAccessUnlessGranted($this->manager->getAlias() . '.index');
return $this->displayTemplate('index');
}
/**
* @Route("/export", name="export")
*/
public function exportAction(SalesReportFormBuilder $formBuilder, MailerHelper $mailerHelper, ShopStorage $shopStorage): Response
{
$this->denyAccessUnlessGranted($this->manager->getAlias() . '.index');
$resource = new SalesReport();
$form = $formBuilder->createForm($resource, [
'name' => 'sales_report',
'ajax_enabled' => false,
]);
if ($form->handleRequest()->isSubmitted()) {
if ($form->isValid()) {
$this->prepareOrders($shopStorage, $resource);
$encoders = [new CsvEncoder()];
$normalizers = [new SalesReportNormalizer()];
$serializer = new Serializer($normalizers, $encoders);
$csvContent = $serializer->serialize($resource, 'csv');
$csvFileName = sprintf(
'raport_sprzedazy_%s_%s.csv',
$resource->getValidFrom()->format('YmdHis'),
$resource->getValidTo()->format('YmdHis')
);
$directory = $this->getKernel()->getRootDir() . '/sales_report/';
$filesystem = new Filesystem();
if (false === $filesystem->exists($directory)) {
$filesystem->mkdir($directory);
}
$csvPath = $directory . $csvFileName;
$filesystem->dumpFile($csvPath, $csvContent);
$mailerHelper->sendEmail([
'recipient' => $resource->getNotify(),
'subject' => sprintf(
'Raport sprzedaży za okres %s - %s',
$resource->getValidFrom()->format('Y-m-d H:i:s'),
$resource->getValidTo()->format('Y-m-d H:i:s')
),
'template' => 'WellCommerceOrderBundle:Admin/Email:sales_report.html.twig',
'configuration' => $shopStorage->getCurrentShop()->getMailerConfiguration(),
'parameters' => [],
'attachments' => [
$csvPath,
],
]);
$this->getFlashHelper()->addSuccess('Raport zostanie wygenerowany i wysłany na Twój adres e-mail.');
return $this->redirectToAction('index');
}
}
return $this->displayTemplate('export', [
'form' => $form,
]);
}
protected function prepareOrders(ShopStorage $shopStorage, SalesReport $report)
{
$criteria = new Criteria();
$criteria->where($criteria->expr()->gte('createdAt', $report->getValidFrom()));
$criteria->andWhere($criteria->expr()->lte('createdAt', $report->getValidTo()));
$criteria->andWhere($criteria->expr()->eq('confirmed', true));
$criteria->andWhere($criteria->expr()->eq('shop', $shopStorage->getCurrentShop()));
$collection = $this->getEntityManager()->getRepository(Order::class)->getCollection($criteria);
$orders = $collection->filter(function (Order $order) use ($report) {
return
$report->getPaymentMethods()->contains($order->getPaymentMethod()) &&
$report->getShippingMethods()->contains($order->getShippingMethod()) &&
$report->getOrderStatuses()->contains($order->getCurrentStatus());
});
$report->setOrders($orders);
}
}