Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6fce97d

Browse files
committed
improve DQL in sales provider
1 parent f7d3b44 commit 6fce97d

4 files changed

Lines changed: 76 additions & 24 deletions

File tree

UPGRADE-1.7.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,18 @@ We've moved the following templates:
8282
8383
Until now shipping address used to be the default address of an Order. We have changed that, so now the billing address
8484
became the default address during checkout. It is an important change in our checkout process, please have that in mind.
85+
86+
## Postgres support
87+
88+
In case when you are using Postgres in your project, function `DATE_FORMAT` should be overridden.
89+
Adjust configuration in `config/packages/doctrine.yaml` to change `DATE_FORMAT` implementation:
90+
91+
```yaml
92+
doctrine:
93+
orm:
94+
entity_managers:
95+
default:
96+
dql:
97+
string_functions:
98+
DATE_FORMAT: App\Doctrine\DQL\DateFormat # OR any other path to your implementation
99+
```
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Paweł Jędrzejewski
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\CoreBundle\Doctrine\DQL;
15+
16+
use Doctrine\ORM\Query\AST\ArithmeticExpression;
17+
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
18+
use Doctrine\ORM\Query\Lexer;
19+
use Doctrine\ORM\Query\Parser;
20+
use Doctrine\ORM\Query\SqlWalker;
21+
22+
final class DateFormat extends FunctionNode
23+
{
24+
/** @var ArithmeticExpression|null */
25+
public $date = null;
26+
/** @var ArithmeticExpression|null */
27+
public $pattern = null;
28+
29+
public function parse(Parser $parser)
30+
{
31+
$parser->match(Lexer::T_IDENTIFIER);
32+
$parser->match(Lexer::T_OPEN_PARENTHESIS);
33+
$this->date = $parser->ArithmeticExpression();
34+
$parser->match(Lexer::T_COMMA);
35+
$this->pattern = $parser->StringPrimary();
36+
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
37+
}
38+
39+
public function getSql(SqlWalker $sqlWalker)
40+
{
41+
return sprintf('DATE_FORMAT(%s, %s)', $this->date->dispatch($sqlWalker), $this->pattern->dispatch($sqlWalker));
42+
}
43+
}

src/Sylius/Bundle/CoreBundle/Resources/config/app/config.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ doctrine:
3232
entity_managers:
3333
default:
3434
auto_mapping: true
35-
35+
dql:
36+
string_functions:
37+
DATE_FORMAT: Sylius\Bundle\CoreBundle\Doctrine\DQL\DateFormat
3638
knp_gaufrette:
3739
adapters:
3840
sylius_image:

src/Sylius/Component/Core/Dashboard/SalesDataProvider.php

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
namespace Sylius\Component\Core\Dashboard;
1515

1616
use Doctrine\ORM\EntityManagerInterface;
17+
use Doctrine\ORM\EntityRepository;
1718
use Sylius\Component\Core\Model\ChannelInterface;
19+
use Sylius\Component\Core\OrderPaymentStates;
1820
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
1921

2022
/**
@@ -25,7 +27,7 @@ final class SalesDataProvider implements SalesDataProviderInterface
2527
/** @var EntityManagerInterface */
2628
private $entityManager;
2729

28-
/** @var OrderRepositoryInterface */
30+
/** @var OrderRepositoryInterface|EntityRepository */
2931
private $orderRepository;
3032

3133
public function __construct(EntityManagerInterface $entityManager, OrderRepositoryInterface $orderRepository)
@@ -41,15 +43,21 @@ public function getLastYearSalesSummary(ChannelInterface $channel): SalesSummary
4143
$endDate = (new \DateTime('last day of this month'));
4244
$endDate->setTime(23, 59, 59);
4345

44-
$qb = $this->orderRepository->createQueryBuilder('so');
45-
$qb->select($this->getSelectStatement())
46-
->where($qb->expr()->eq('so.channel', ':channel'))
47-
->andWhere('so.checkoutCompletedAt BETWEEN :startDate AND :endDate')
46+
/** @psalm-suppress PossiblyUndefinedMethod */
47+
$queryBuilder = $this->orderRepository->createQueryBuilder('o')
48+
->select("DATE_FORMAT(o.checkoutCompletedAt, '%m.%y') AS date")
49+
->addSelect("SUM(o.total) as total")
50+
->where('o.channel = :channel')
51+
->andWhere('o.checkoutCompletedAt >= :startDate')
52+
->andWhere('o.checkoutCompletedAt <= :endDate')
53+
->andWhere('o.paymentState = :state')
4854
->groupBy('date')
4955
->setParameter('channel', $channel)
5056
->setParameter('startDate', $startDate)
51-
->setParameter('endDate', $endDate);
52-
$result = $qb->getQuery()->getScalarResult();
57+
->setParameter('endDate', $endDate)
58+
->setParameter('state', OrderPaymentStates::STATE_PAID)
59+
;
60+
$result = $queryBuilder->getQuery()->getScalarResult();
5361

5462
$data = [];
5563
foreach ($result as $item) {
@@ -62,20 +70,4 @@ public function getLastYearSalesSummary(ChannelInterface $channel): SalesSummary
6270
$data
6371
);
6472
}
65-
66-
/**
67-
* add DATE_FORMAT support for postgres by using
68-
* DoctrineExtensions\Query\Postgresql\DateFormat in doctrine configuration
69-
*
70-
* @return string
71-
*/
72-
private function getSelectStatement()
73-
{
74-
$dateFormat = '%m.%y';
75-
if ($this->entityManager->getConnection()->getDatabasePlatform()->getName() == 'postgresql') {
76-
$dateFormat = 'mm.yyyy';
77-
}
78-
79-
return "DATE_FORMAT(so.checkoutCompletedAt, '" . $dateFormat . "') AS date, SUM(so.total) as total";
80-
}
8173
}

0 commit comments

Comments
 (0)