1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\MagentoBundle\Provider\Analytics; |
4
|
|
|
|
5
|
|
|
use OroCRM\Bundle\AnalyticsBundle\Entity\RFMMetricCategory; |
6
|
|
|
use OroCRM\Bundle\ChannelBundle\Entity\Channel; |
7
|
|
|
use OroCRM\Bundle\MagentoBundle\Entity\Order; |
8
|
|
|
|
9
|
|
View Code Duplication |
class CustomerMonetaryProvider extends AbstractCustomerRFMProvider |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* {@inheritdoc} |
13
|
|
|
*/ |
14
|
|
|
public function getType() |
15
|
|
|
{ |
16
|
|
|
return RFMMetricCategory::TYPE_MONETARY; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
protected function getScalarValues(Channel $channel, array $ids = []) |
23
|
|
|
{ |
24
|
|
|
$date = new \DateTime('now', new \DateTimeZone('UTC')); |
25
|
|
|
$qb = $this->doctrineHelper |
26
|
|
|
->getEntityRepository($this->className) |
27
|
|
|
->createQueryBuilder('c'); |
28
|
|
|
|
29
|
|
|
$qb |
30
|
|
|
->select('SUM( |
31
|
|
|
CASE WHEN o.subtotalAmount IS NOT NULL THEN o.subtotalAmount ELSE 0 END - |
32
|
|
|
CASE WHEN o.discountAmount IS NOT NULL THEN ABS(o.discountAmount) ELSE 0 END |
33
|
|
|
) as value', 'c.id') |
34
|
|
|
->join('c.orders', 'o') |
35
|
|
|
->where( |
36
|
|
|
$qb->expr()->andX( |
37
|
|
|
$qb->expr()->neq($qb->expr()->lower('o.status'), ':status'), |
38
|
|
|
$qb->expr()->gte('o.createdAt', ':date'), |
39
|
|
|
$qb->expr()->eq('c.dataChannel', ':channel') |
40
|
|
|
) |
41
|
|
|
) |
42
|
|
|
->groupBy('c.id') |
43
|
|
|
->orderBy($qb->expr()->asc('c.id')) |
44
|
|
|
->setParameter('status', Order::STATUS_CANCELED) |
45
|
|
|
->setParameter('channel', $channel) |
46
|
|
|
->setParameter('date', $date->sub(new \DateInterval('P365D'))); |
47
|
|
|
|
48
|
|
|
if (!empty($ids)) { |
49
|
|
|
$qb->andWhere($qb->expr()->in('c.id', ':ids')) |
50
|
|
|
->setParameter('ids', $ids); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $qb->getQuery()->getScalarResult(); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.