From 46f7ec19837ce786625e5ec85d26288be204045d Mon Sep 17 00:00:00 2001 From: Pavel Usachev Date: Fri, 3 Feb 2017 12:53:19 +0200 Subject: [PATCH] =?UTF-8?q?CRM-6669:=20Widget=20=E2=80=9CCampaigns=20By=20?= =?UTF-8?q?Close=20Revenue=20=E2=80=9D:=20Total=20for=20campaign=20ar?= =?UTF-8?q?=E2=80=A6=20(#7009)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- .../Entity/Repository/CampaignRepository.php | 1 + .../Dashboard/CampaignByCloseRevenueTest.php | 48 +++++++++++ ...oadCampaignByCloseRevenueWidgetFixture.php | 79 ++++++++++++++----- 3 files changed, 109 insertions(+), 19 deletions(-) diff --git a/src/OroCRM/Bundle/CampaignBundle/Entity/Repository/CampaignRepository.php b/src/OroCRM/Bundle/CampaignBundle/Entity/Repository/CampaignRepository.php index 00ca8e2ab80..17d2f04c330 100644 --- a/src/OroCRM/Bundle/CampaignBundle/Entity/Repository/CampaignRepository.php +++ b/src/OroCRM/Bundle/CampaignBundle/Entity/Repository/CampaignRepository.php @@ -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'); diff --git a/src/OroCRM/Bundle/SalesBundle/Tests/Functional/Dashboard/CampaignByCloseRevenueTest.php b/src/OroCRM/Bundle/SalesBundle/Tests/Functional/Dashboard/CampaignByCloseRevenueTest.php index d278cd9ce1b..6155574149e 100644 --- a/src/OroCRM/Bundle/SalesBundle/Tests/Functional/Dashboard/CampaignByCloseRevenueTest.php +++ b/src/OroCRM/Bundle/SalesBundle/Tests/Functional/Dashboard/CampaignByCloseRevenueTest.php @@ -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); @@ -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 */ diff --git a/src/OroCRM/Bundle/SalesBundle/Tests/Functional/Fixture/LoadCampaignByCloseRevenueWidgetFixture.php b/src/OroCRM/Bundle/SalesBundle/Tests/Functional/Fixture/LoadCampaignByCloseRevenueWidgetFixture.php index 1d749d004c6..45b8e63eab4 100644 --- a/src/OroCRM/Bundle/SalesBundle/Tests/Functional/Fixture/LoadCampaignByCloseRevenueWidgetFixture.php +++ b/src/OroCRM/Bundle/SalesBundle/Tests/Functional/Fixture/LoadCampaignByCloseRevenueWidgetFixture.php @@ -26,27 +26,50 @@ 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); @@ -54,28 +77,48 @@ protected function createOpportunity($createdAt, $status) $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); } /** @@ -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');