Skip to content

Commit

Permalink
CRM-6669: Widget “Campaigns By Close Revenue ”: Total for campaign ar…
Browse files Browse the repository at this point in the history
…… (#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
  • Loading branch information
pusachev authored and Aleksey Solonenko committed Feb 3, 2017
1 parent ccdedc5 commit 46f7ec1
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public function getCampaignsByCloseRevenueQB($opportunitiesAlias)
->join('OroCRMSalesBundle:Lead', 'lead', 'WITH', 'lead.campaign = campaign')
->join('lead.opportunities', $opportunitiesAlias)
->where(sprintf('%s.status=\'won\'', $opportunitiesAlias))
->andWhere(sprintf('%s.closeRevenue>0', $opportunitiesAlias))
->orderBy('closeRevenue', 'DESC')
->groupBy('campaign.name');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function testDateRangeAllTypeFilter($requestData)
$this->assertNotEmpty($crawler->html());

$chartData = $this->getChartData($crawler);

//If we have data for chart we need only first campaign
if ($chartData) {
$chartData = reset($chartData);
Expand All @@ -61,6 +62,53 @@ public function testDateRangeAllTypeFilter($requestData)
);
}

/**
* @dataProvider widgetConfigureProvider
* @array $requestData
*/
public function testFilterCampaignByNullCloseRevenue(array $requestData)
{
$this->configureWidget($this->widget, $requestData['widgetConfig']);

$crawler = $this->client->request(
'GET',
$this->getUrl(
'orocrm_campaign_dashboard_campaigns_by_close_revenue_chart',
[
'widget' => 'campaigns_by_close_revenue',
'_widgetId' => $this->widget->getId()
]
)
);
$response = $this->client->getResponse();
$this->assertEquals($response->getStatusCode(), 200, "Failed in getting widget view !");
$this->assertNotEmpty($crawler->html());

$chartData = $this->getChartData($crawler);

$this->assertCount(
$requestData['expectedCampaignCount'],
$chartData,
"Opportunity with null or 0 close revenue is presented"
);
}

public function widgetConfigureProvider()
{
return [
'Closed lost opportunities' => [
[
'widgetConfig' => [
'campaigns_by_close_revenue[dateRange][part]' => 'value',
'campaigns_by_close_revenue[dateRange][type]' => AbstractDateFilterType::TYPE_ALL_TIME,
],
'expectedResult' => 200, // 2 opportunities * $100
'expectedCampaignCount' => 1 // Opportunity with test campaign have null close revenue
],
],
];
}

/**
* @return array
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,56 +26,99 @@ class LoadCampaignByCloseRevenueWidgetFixture extends AbstractFixture

private $opportunityCount = 0;

protected function createLead()
{
/**
* @param string $name
* @param Campaign $campaign
* @param string $referenceName
*
* @return Lead
*/
protected function createLead(
$name,
Campaign $campaign,
$referenceName = null
) {
$lead = new Lead();
$lead->setName('Lead name');
$lead->setName($name);
$lead->setOrganization($this->organization);
$lead->setCampaign($this->getReference('default_campaign'));
$lead->setCampaign($campaign);
$this->em->persist($lead);
$this->em->flush();
$this->setReference('default_lead', $lead);

($referenceName === null ) ?: $this->setReference($referenceName, $lead);

return $lead;
}

protected function createOpportunity($createdAt, $status)
{
/**
* @param \DateTime $createdAt
* @param string $status
* @param Lead $lead
* @param int $closeRevenue
*/
protected function createOpportunity(
$createdAt,
$status,
Lead $lead,
$closeRevenue = null
) {
$className = ExtendHelper::buildEnumValueClassName(Opportunity::INTERNAL_STATUS_CODE);
$opportunityStatus = $this->em->getRepository($className)->find(ExtendHelper::buildEnumValueId($status));

$opportunity = new Opportunity();
$opportunity->setName(sprintf('Test Opportunity #%d', ++$this->opportunityCount));
$opportunity->setStatus($opportunityStatus);
$opportunity->setLead($this->getReference('default_lead'));
$opportunity->setCloseRevenue(100);
$opportunity->setLead($lead);
($closeRevenue === null ) ?: $opportunity->setCloseRevenue($closeRevenue);
$opportunity->setOrganization($this->organization);
$this->em->persist($opportunity);

$opportunity->setCreatedAt($createdAt);
$this->em->flush();
}

protected function createCampaign()
/**
* @param string $name
* @param string $code
* @param string $reference
*
* @return Campaign
*/
protected function createCampaign($name, $code, $reference = null)
{
$campaign = new Campaign();
$campaign->setName('Campaign');
$campaign->setCode('cmp');
$campaign->setName($name);
$campaign->setCode($code);
$campaign->setOrganization($this->organization);
$campaign->setReportPeriod(Campaign::PERIOD_MONTHLY);
$this->em->persist($campaign);
$this->em->flush();
$this->setReference('default_campaign', $campaign);

($reference === null) ?: $this->setReference($reference, $campaign);

return $campaign;
}

protected function createOpportunities()
{
$createdAt = new \DateTime('2016-12-28 12:03:10', new \DateTimeZone('UTC'));

$defaultCampaign = $this->createCampaign('Default campaing', 'cmt');
$anotherCampaign = $this->createCampaign('Another campaing', 'test');

$defaultLead = $this->createLead('Default Lead', $defaultCampaign);
$anotherLead = $this->createLead('Another Lead', $anotherCampaign);

// Every opportunity has value of $100
$this->createOpportunity($createdAt, 'won');
$this->createOpportunity($createdAt, 'in_progress');
$this->createOpportunity($createdAt, 'lost');
$this->createOpportunity($createdAt, 'won', $defaultLead, 100);
$this->createOpportunity($createdAt, 'in_progress', $defaultLead, 100);
$this->createOpportunity($createdAt, 'lost', $defaultLead, 100);

//This opportunity without close revenue
$this->createOpportunity($createdAt, 'won', $anotherLead);

$createdAt->add(new \DateInterval('P1D'));
$this->createOpportunity($createdAt, 'won');
$this->createOpportunity($createdAt, 'won', $defaultLead, 100);
}

/**
Expand All @@ -85,8 +128,6 @@ public function load(ObjectManager $manager)
{
$this->organization = $manager->getRepository('OroOrganizationBundle:Organization')->getFirst();
$this->em = $manager;
$this->createCampaign();
$this->createLead();
$this->createOpportunities();
$dashboard = new Dashboard();
$dashboard->setName('dashboard');
Expand Down

0 comments on commit 46f7ec1

Please sign in to comment.