|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\SalesBundle\Dashboard\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface; |
|
6
|
|
|
|
|
7
|
|
|
use Oro\Component\DoctrineUtils\ORM\QueryUtils; |
|
8
|
|
|
use Oro\Bundle\DashboardBundle\Model\WidgetOptionBag; |
|
9
|
|
|
use Oro\Bundle\DashboardBundle\Filter\DateFilterProcessor; |
|
10
|
|
|
use Oro\Bundle\UserBundle\Dashboard\OwnerHelper; |
|
11
|
|
|
use Oro\Bundle\SecurityBundle\ORM\Walker\AclHelper; |
|
12
|
|
|
|
|
13
|
|
|
use OroCRM\Bundle\SalesBundle\Entity\Repository\OpportunityRepository; |
|
14
|
|
|
|
|
15
|
|
|
class OpportunityByStatusProvider |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var RegistryInterface */ |
|
18
|
|
|
protected $registry; |
|
19
|
|
|
|
|
20
|
|
|
/** @var AclHelper */ |
|
21
|
|
|
protected $aclHelper; |
|
22
|
|
|
|
|
23
|
|
|
/** @var DateFilterProcessor */ |
|
24
|
|
|
protected $dateFilterProcessor; |
|
25
|
|
|
|
|
26
|
|
|
/** @var OwnerHelper */ |
|
27
|
|
|
protected $ownerHelper; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param RegistryInterface $doctrine |
|
31
|
|
|
* @param AclHelper $aclHelper |
|
32
|
|
|
* @param DateFilterProcessor $processor |
|
33
|
|
|
* @param OwnerHelper $ownerHelper |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct( |
|
36
|
|
|
RegistryInterface $doctrine, |
|
37
|
|
|
AclHelper $aclHelper, |
|
38
|
|
|
DateFilterProcessor $processor, |
|
39
|
|
|
OwnerHelper $ownerHelper |
|
40
|
|
|
) { |
|
41
|
|
|
$this->registry = $doctrine; |
|
42
|
|
|
$this->aclHelper = $aclHelper; |
|
43
|
|
|
$this->dateFilterProcessor = $processor; |
|
44
|
|
|
$this->ownerHelper = $ownerHelper; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param WidgetOptionBag $widgetOptions |
|
49
|
|
|
* |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getOpportunitiesGroupedByStatus(WidgetOptionBag $widgetOptions) |
|
53
|
|
|
{ |
|
54
|
|
|
$dateRange = $widgetOptions->get('dateRange'); |
|
55
|
|
|
$owners = $this->ownerHelper->getOwnerIds($widgetOptions); |
|
56
|
|
|
$excludedStatuses = $widgetOptions->get('excluded_statuses', []); |
|
57
|
|
|
$orderBy = $widgetOptions->get('useQuantityAsData') ? 'quantity' : 'budget'; |
|
58
|
|
|
$qb = $this->getOpportunityRepository() |
|
59
|
|
|
->getGroupedOpportunitiesByStatusQB('o', $orderBy); |
|
60
|
|
|
$this->dateFilterProcessor->process($qb, $dateRange, 'o.createdAt'); |
|
61
|
|
|
|
|
62
|
|
|
// Ignore filters by opportunities, if filters by date exists. |
|
63
|
|
|
$where = $qb->getDQLPart('where'); |
|
64
|
|
|
if ($where) { |
|
65
|
|
|
$qb->where( |
|
66
|
|
|
$qb->expr()->orX( |
|
67
|
|
|
$where, |
|
68
|
|
|
$qb->expr()->isNull('o.id') |
|
69
|
|
|
) |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if ($excludedStatuses) { |
|
74
|
|
|
$qb->andWhere( |
|
75
|
|
|
$qb->expr()->notIn('s.id', $excludedStatuses) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if ($owners) { |
|
80
|
|
|
QueryUtils::applyOptimizedIn($qb, 'o.owner', $owners); |
|
81
|
|
|
} |
|
82
|
|
|
return $this->aclHelper->apply($qb)->getArrayResult(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return OpportunityRepository |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function getOpportunityRepository() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->registry->getRepository('OroCRMSalesBundle:Opportunity'); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|