Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions src/AppBundle/Controller/Admin/DashController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -135,7 +134,6 @@ public function dashboardAction(Request $request)

$membersAddedByMonth = [];
$itemsAddedByMonth = [];
$membershipsAddedByMonth = [];
$loansAddedByMonth = [];
$labels = [];

Expand Down Expand Up @@ -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
Expand All @@ -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])) {
Expand Down Expand Up @@ -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),
Expand Down
20 changes: 12 additions & 8 deletions src/AppBundle/Services/Membership/MembershipService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) {
Expand Down