Skip to content

Commit 46f7ec1

Browse files
pusachevAleksey Solonenko
authored and
Aleksey Solonenko
committed
CRM-6669: Widget “Campaigns By Close Revenue ”: Total for campaign ar… (#7009)
* CRM-6669: Widget “Campaigns By Close Revenue ”: Total for campaign are shown incorrectly for “today” date range. - fixed sql query for filter campaigns with close revenue more than 0 - added functional test for null opportunity close revenue
1 parent ccdedc5 commit 46f7ec1

File tree

3 files changed

+109
-19
lines changed

3 files changed

+109
-19
lines changed

Diff for: src/OroCRM/Bundle/CampaignBundle/Entity/Repository/CampaignRepository.php

+1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public function getCampaignsByCloseRevenueQB($opportunitiesAlias)
153153
->join('OroCRMSalesBundle:Lead', 'lead', 'WITH', 'lead.campaign = campaign')
154154
->join('lead.opportunities', $opportunitiesAlias)
155155
->where(sprintf('%s.status=\'won\'', $opportunitiesAlias))
156+
->andWhere(sprintf('%s.closeRevenue>0', $opportunitiesAlias))
156157
->orderBy('closeRevenue', 'DESC')
157158
->groupBy('campaign.name');
158159

Diff for: src/OroCRM/Bundle/SalesBundle/Tests/Functional/Dashboard/CampaignByCloseRevenueTest.php

+48
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function testDateRangeAllTypeFilter($requestData)
4949
$this->assertNotEmpty($crawler->html());
5050

5151
$chartData = $this->getChartData($crawler);
52+
5253
//If we have data for chart we need only first campaign
5354
if ($chartData) {
5455
$chartData = reset($chartData);
@@ -61,6 +62,53 @@ public function testDateRangeAllTypeFilter($requestData)
6162
);
6263
}
6364

65+
/**
66+
* @dataProvider widgetConfigureProvider
67+
* @array $requestData
68+
*/
69+
public function testFilterCampaignByNullCloseRevenue(array $requestData)
70+
{
71+
$this->configureWidget($this->widget, $requestData['widgetConfig']);
72+
73+
$crawler = $this->client->request(
74+
'GET',
75+
$this->getUrl(
76+
'orocrm_campaign_dashboard_campaigns_by_close_revenue_chart',
77+
[
78+
'widget' => 'campaigns_by_close_revenue',
79+
'_widgetId' => $this->widget->getId()
80+
]
81+
)
82+
);
83+
$response = $this->client->getResponse();
84+
$this->assertEquals($response->getStatusCode(), 200, "Failed in getting widget view !");
85+
$this->assertNotEmpty($crawler->html());
86+
87+
$chartData = $this->getChartData($crawler);
88+
89+
$this->assertCount(
90+
$requestData['expectedCampaignCount'],
91+
$chartData,
92+
"Opportunity with null or 0 close revenue is presented"
93+
);
94+
}
95+
96+
public function widgetConfigureProvider()
97+
{
98+
return [
99+
'Closed lost opportunities' => [
100+
[
101+
'widgetConfig' => [
102+
'campaigns_by_close_revenue[dateRange][part]' => 'value',
103+
'campaigns_by_close_revenue[dateRange][type]' => AbstractDateFilterType::TYPE_ALL_TIME,
104+
],
105+
'expectedResult' => 200, // 2 opportunities * $100
106+
'expectedCampaignCount' => 1 // Opportunity with test campaign have null close revenue
107+
],
108+
],
109+
];
110+
}
111+
64112
/**
65113
* @return array
66114
*/

Diff for: src/OroCRM/Bundle/SalesBundle/Tests/Functional/Fixture/LoadCampaignByCloseRevenueWidgetFixture.php

+60-19
Original file line numberDiff line numberDiff line change
@@ -26,56 +26,99 @@ class LoadCampaignByCloseRevenueWidgetFixture extends AbstractFixture
2626

2727
private $opportunityCount = 0;
2828

29-
protected function createLead()
30-
{
29+
/**
30+
* @param string $name
31+
* @param Campaign $campaign
32+
* @param string $referenceName
33+
*
34+
* @return Lead
35+
*/
36+
protected function createLead(
37+
$name,
38+
Campaign $campaign,
39+
$referenceName = null
40+
) {
3141
$lead = new Lead();
32-
$lead->setName('Lead name');
42+
$lead->setName($name);
3343
$lead->setOrganization($this->organization);
34-
$lead->setCampaign($this->getReference('default_campaign'));
44+
$lead->setCampaign($campaign);
3545
$this->em->persist($lead);
3646
$this->em->flush();
37-
$this->setReference('default_lead', $lead);
47+
48+
($referenceName === null ) ?: $this->setReference($referenceName, $lead);
49+
50+
return $lead;
3851
}
3952

40-
protected function createOpportunity($createdAt, $status)
41-
{
53+
/**
54+
* @param \DateTime $createdAt
55+
* @param string $status
56+
* @param Lead $lead
57+
* @param int $closeRevenue
58+
*/
59+
protected function createOpportunity(
60+
$createdAt,
61+
$status,
62+
Lead $lead,
63+
$closeRevenue = null
64+
) {
4265
$className = ExtendHelper::buildEnumValueClassName(Opportunity::INTERNAL_STATUS_CODE);
4366
$opportunityStatus = $this->em->getRepository($className)->find(ExtendHelper::buildEnumValueId($status));
4467

4568
$opportunity = new Opportunity();
4669
$opportunity->setName(sprintf('Test Opportunity #%d', ++$this->opportunityCount));
4770
$opportunity->setStatus($opportunityStatus);
48-
$opportunity->setLead($this->getReference('default_lead'));
49-
$opportunity->setCloseRevenue(100);
71+
$opportunity->setLead($lead);
72+
($closeRevenue === null ) ?: $opportunity->setCloseRevenue($closeRevenue);
5073
$opportunity->setOrganization($this->organization);
5174
$this->em->persist($opportunity);
5275

5376
$opportunity->setCreatedAt($createdAt);
5477
$this->em->flush();
5578
}
5679

57-
protected function createCampaign()
80+
/**
81+
* @param string $name
82+
* @param string $code
83+
* @param string $reference
84+
*
85+
* @return Campaign
86+
*/
87+
protected function createCampaign($name, $code, $reference = null)
5888
{
5989
$campaign = new Campaign();
60-
$campaign->setName('Campaign');
61-
$campaign->setCode('cmp');
90+
$campaign->setName($name);
91+
$campaign->setCode($code);
6292
$campaign->setOrganization($this->organization);
6393
$campaign->setReportPeriod(Campaign::PERIOD_MONTHLY);
6494
$this->em->persist($campaign);
6595
$this->em->flush();
66-
$this->setReference('default_campaign', $campaign);
96+
97+
($reference === null) ?: $this->setReference($reference, $campaign);
98+
99+
return $campaign;
67100
}
68101

69102
protected function createOpportunities()
70103
{
71104
$createdAt = new \DateTime('2016-12-28 12:03:10', new \DateTimeZone('UTC'));
105+
106+
$defaultCampaign = $this->createCampaign('Default campaing', 'cmt');
107+
$anotherCampaign = $this->createCampaign('Another campaing', 'test');
108+
109+
$defaultLead = $this->createLead('Default Lead', $defaultCampaign);
110+
$anotherLead = $this->createLead('Another Lead', $anotherCampaign);
111+
72112
// Every opportunity has value of $100
73-
$this->createOpportunity($createdAt, 'won');
74-
$this->createOpportunity($createdAt, 'in_progress');
75-
$this->createOpportunity($createdAt, 'lost');
113+
$this->createOpportunity($createdAt, 'won', $defaultLead, 100);
114+
$this->createOpportunity($createdAt, 'in_progress', $defaultLead, 100);
115+
$this->createOpportunity($createdAt, 'lost', $defaultLead, 100);
116+
117+
//This opportunity without close revenue
118+
$this->createOpportunity($createdAt, 'won', $anotherLead);
76119

77120
$createdAt->add(new \DateInterval('P1D'));
78-
$this->createOpportunity($createdAt, 'won');
121+
$this->createOpportunity($createdAt, 'won', $defaultLead, 100);
79122
}
80123

81124
/**
@@ -85,8 +128,6 @@ public function load(ObjectManager $manager)
85128
{
86129
$this->organization = $manager->getRepository('OroOrganizationBundle:Organization')->getFirst();
87130
$this->em = $manager;
88-
$this->createCampaign();
89-
$this->createLead();
90131
$this->createOpportunities();
91132
$dashboard = new Dashboard();
92133
$dashboard->setName('dashboard');

0 commit comments

Comments
 (0)