Code Duplication    Length = 44-47 lines in 2 locations

src/OroCRM/Bundle/MagentoBundle/Provider/Analytics/CustomerFrequencyProvider.php 1 location

@@ 9-52 (lines=44) @@
6
use OroCRM\Bundle\ChannelBundle\Entity\Channel;
7
use OroCRM\Bundle\MagentoBundle\Entity\Order;
8
9
class CustomerFrequencyProvider extends AbstractCustomerRFMProvider
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    public function getType()
15
    {
16
        return RFMMetricCategory::TYPE_FREQUENCY;
17
    }
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected function getScalarValues(Channel $channel, array $ids = [])
23
    {
24
        $qb = $this->doctrineHelper
25
            ->getEntityRepository($this->className)
26
            ->createQueryBuilder('c');
27
28
        $date = new \DateTime('now', new \DateTimeZone('UTC'));
29
        $qb
30
            ->select('COUNT(o) as value', 'c.id')
31
            ->join('c.orders', 'o')
32
            ->where(
33
                $qb->expr()->andX(
34
                    $qb->expr()->neq($qb->expr()->lower('o.status'), ':status'),
35
                    $qb->expr()->gte('o.createdAt', ':date'),
36
                    $qb->expr()->eq('c.dataChannel', ':channel')
37
                )
38
            )
39
            ->groupBy('c.id')
40
            ->orderBy($qb->expr()->asc('c.id'))
41
            ->setParameter('status', Order::STATUS_CANCELED)
42
            ->setParameter('channel', $channel)
43
            ->setParameter('date', $date->sub(new \DateInterval('P365D')));
44
45
        if (!empty($ids)) {
46
            $qb->andWhere($qb->expr()->in('c.id', ':ids'))
47
                ->setParameter('ids', $ids);
48
        }
49
50
        return $qb->getQuery()->getScalarResult();
51
    }
52
}
53

src/OroCRM/Bundle/MagentoBundle/Provider/Analytics/CustomerMonetaryProvider.php 1 location

@@ 9-55 (lines=47) @@
6
use OroCRM\Bundle\ChannelBundle\Entity\Channel;
7
use OroCRM\Bundle\MagentoBundle\Entity\Order;
8
9
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