diff --git a/src/AppBundle/Controller/Admin/DashController.php b/src/AppBundle/Controller/Admin/DashController.php index bfbfd490..cf136e59 100644 --- a/src/AppBundle/Controller/Admin/DashController.php +++ b/src/AppBundle/Controller/Admin/DashController.php @@ -119,7 +119,6 @@ public function dashboardAction(Request $request) /** @var \AppBundle\Services\Membership\MembershipService $membershipService */ $membershipService = $this->get('service.membership'); - $membershipsAdded = $membershipService->membershipsAddedByMonth(); /** @var \AppBundle\Services\Item\ItemService $itemService */ $itemService = $this->get('service.item'); @@ -135,7 +134,6 @@ public function dashboardAction(Request $request) $membersAddedByMonth = []; $itemsAddedByMonth = []; - $membershipsAddedByMonth = []; $loansAddedByMonth = []; $labels = []; @@ -163,7 +161,6 @@ public function dashboardAction(Request $request) $totalMembers = $contactService->countAllContacts($chartStartDate); $totalItems = $itemService->countAllItems($chartStartDate); - $totalMemberships = $membershipService->countMemberships($chartStartDate); foreach ($keys AS $key) { // Contacts @@ -183,12 +180,8 @@ public function dashboardAction(Request $request) $itemsGrowth[] = $totalItems; // Memberships - if (!isset($membershipsAdded[$key])) { - $membershipsAdded[$key] = 0; - } - $membershipsAddedByMonth[] = $membershipsAdded[$key]; - $totalMemberships += $membershipsAdded[$key]; - $membershipsGrowth[] = $totalMemberships; + $activeMemberships = $membershipService->countActiveMemberships(\DateTime::createFromFormat('Y-m-d',$key.'-01')); + $membershipsGrowth[] = $activeMemberships; // Loans if (!isset($loansAdded[$key])) { @@ -235,7 +228,6 @@ public function dashboardAction(Request $request) 'loansAddedByMonth' => implode(',', $loansAddedByMonth), 'contactsAddedByMonth' => implode(',', $membersAddedByMonth), 'contactsGrowth' => implode(',', $memberGrowth), - 'membershipsAddedByMonth' => implode(',', $membershipsAddedByMonth), 'membershipsGrowth' => implode(',', $membershipsGrowth), 'membershipFeesByMonth' => implode(',', $membershipFeesByMonth), 'eventFeesByMonth' => implode(',', $eventFeesByMonth), diff --git a/src/AppBundle/Services/Membership/MembershipService.php b/src/AppBundle/Services/Membership/MembershipService.php index f44f55b8..120affaf 100644 --- a/src/AppBundle/Services/Membership/MembershipService.php +++ b/src/AppBundle/Services/Membership/MembershipService.php @@ -56,18 +56,22 @@ public function membershipsAddedByMonth() } /** - * @param \DateTime $dateTo - * @return int + * Count memberships that where ACTIVE on given date (might be EXPIRED now) + * Eliminate duplicates for contact as multiple expired memberships might span same period + * but only one was ACTIVE at any given time + * @return int count of active memberships at given date + * @throws DBALException */ - public function countMemberships(\DateTime $dateTo = null) + public function countActiveMemberships(\DateTime $date = null) { $repository = $this->em->getRepository('AppBundle:Membership'); $builder = $repository->createQueryBuilder('m'); - $builder->add('select', 'COUNT(m) AS qty'); - $builder->where("m.status = 'ACTIVE'"); - if ($dateTo) { - $builder->andWhere("m.createdAt < :dateTo"); - $builder->setParameter('dateTo', $dateTo->format("Y-m-01")); + $builder->add('select', 'COUNT( DISTINCT(m.contact)) AS qty'); + $builder->where("m.status IN ('ACTIVE', 'EXPIRED')"); + if ($date) { + $builder->andWhere("m.startsAt < :date"); + $builder->andWhere("m.expiresAt >= :date"); + $builder->setParameter('date', $date->format("Y-m-d")); } $query = $builder->getQuery(); if ( $results = $query->getResult() ) {