Completed
Push — master ( 0b205b...bde6a0 )
by
unknown
10:16
created

getForecastOfOpportunitiesValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 11
rs 9.4285
c 1
b 0
f 1
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
3
namespace OroCRM\Bundle\SalesBundle\Provider\Opportunity;
4
5
use Symfony\Bridge\Doctrine\RegistryInterface;
6
7
use Oro\Bundle\DashboardBundle\Model\WidgetOptionBag;
8
use Oro\Bundle\LocaleBundle\Formatter\NumberFormatter;
9
use Oro\Bundle\QueryDesignerBundle\QueryDesigner\FilterProcessor;
10
use Oro\Bundle\SecurityBundle\ORM\Walker\AclHelper;
11
use Oro\Bundle\UserBundle\Dashboard\OwnerHelper;
12
use Oro\Component\DoctrineUtils\ORM\QueryUtils;
13
14
use OroCRM\Bundle\SalesBundle\Entity\Repository\OpportunityRepository;
15
16
class IndeterminateForecastProvider
17
{
18
    /** @var RegistryInterface */
19
    protected $doctrine;
20
21
    /** @var AclHelper */
22
    protected $aclHelper;
23
24
    /** @var OwnerHelper */
25
    protected $ownerHelper;
26
27
    /** @var FilterProcessor */
28
    protected $filterProcessor;
29
30
    /** @var NumberFormatter */
31
    protected $numberFormatter;
32
33
    /** @var array */
34
    protected $data = [];
35
36
    /**
37
     * @param RegistryInterface $doctrine
38
     * @param AclHelper $aclHelper
39
     * @param OwnerHelper $ownerHelper
40
     * @param FilterProcessor $filterProcessor
41
     * @param NumberFormatter $numberFormatter
42
     */
43
    public function __construct(
44
        RegistryInterface $doctrine,
45
        AclHelper $aclHelper,
46
        OwnerHelper $ownerHelper,
47
        FilterProcessor $filterProcessor,
48
        NumberFormatter $numberFormatter
49
    ) {
50
        $this->doctrine = $doctrine;
51
        $this->aclHelper = $aclHelper;
52
        $this->ownerHelper = $ownerHelper;
53
        $this->filterProcessor = $filterProcessor;
54
        $this->numberFormatter = $numberFormatter;
55
    }
56
57
    /**
58
     * @param WidgetOptionBag $widgetOptions
59
     * @param string          $dataKey
60
     *
61
     * @return string
62
     */
63
    public function getForecastOfOpportunitiesValues(WidgetOptionBag $widgetOptions, $dataKey)
64
    {
65
        $data = $this->getIndeterminateData(
66
            $this->ownerHelper->getOwnerIds($widgetOptions),
67
            $widgetOptions->get('queryFilter', [])
68
        );
69
70
        return [
71
            'value' => $this->numberFormatter->formatCurrency($data[$dataKey]),
72
        ];
73
    }
74
75
    /**
76
     * @param array
77
     * @param array
78
     *
79
     * @return ['totalIndeterminate' => <double>, 'weightedIndeterminate' => <double>]
0 ignored issues
show
Documentation introduced by
The doc-type 'totalIndeterminate'">['totalIndeterminate' could not be parsed: Unknown type name "[" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
80
     */
81
    public function getIndeterminateData(array $ownerIds, array $queryFilter = null)
82
    {
83
        $cacheKey = md5(serialize(func_get_args()));
84
        if (!isset($this->data[$cacheKey])) {
85
            $filters = isset($queryFilter['definition']['filters'])
86
                ? $queryFilter['definition']['filters']
87
                : [];
88
89
            $alias = 'o';
90
            $qb = $this->filterProcessor
91
                ->process(
92
                    $this->getOpportunityRepository()
93
                        ->getForecastQB($alias)
94
                        ->andWhere(sprintf('%s.closeDate IS NULL', $alias)),
95
                    'OroCRM\Bundle\SalesBundle\Entity\Opportunity',
96
                    $filters,
97
                    $alias
98
                );
99
            $qb
100
                ->andWhere(
101
                    $qb->expr()->orX(
102
                        $qb->expr()->andX(
103
                            sprintf('%s.probability <> 0', $alias),
104
                            sprintf('%s.probability <> 1', $alias)
105
                        ),
106
                        sprintf('%s.probability is NULL', $alias)
107
                    )
108
                );
109
110
            if (!empty($ownerIds)) {
111
                $qb->join('o.owner', 'owner');
112
                QueryUtils::applyOptimizedIn($qb, 'owner.id', $ownerIds);
113
            }
114
115
            $result = $this->aclHelper->apply($qb)->getOneOrNullResult()
116
                ?: ['budgetAmount' => 0, 'weightedForecast' => 0];
117
118
            $this->data[$cacheKey] = [
119
                'totalIndeterminate'    => $result['budgetAmount'],
120
                'weightedIndeterminate' => $result['weightedForecast'],
121
            ];
122
        }
123
124
        return $this->data[$cacheKey];
125
    }
126
127
    /**
128
     * @return OpportunityRepository
129
     */
130
    protected function getOpportunityRepository()
131
    {
132
        return $this->doctrine->getRepository('OroCRMSalesBundle:Opportunity');
133
    }
134
}
135