public function generatestats() { if ($this->shouldLockIfNoPermission('data.edit')) { return; } if ($this->request->is('post')) { $reportName = time(); $modelCustomer = new Customer($this->db); $modelDevice = new Device($this->db); $modelPart = new Part($this->db); $user = $this->session->get('user'); $output = 'Created By: ' . $user['username'] . "\n"; $customerData = $modelCustomer->findAllCustomers(); if (null === $customerData) { $this->flash->error('Unable to get customers!'); return $this->redirect('/reports'); } $output .= "\nCUSTOMERS\n------------\n"; $count = count($customerData); $output .= ($count === 1 ? 'There is ' : 'There are ') . $count . ($count === 1 ? ' customer' : ' customers') . " total.\n"; $ids = []; foreach ($customerData as $row) { $ids[] = $row['id']; } $deviceData = $modelDevice->findAllLinkedToCustomerIds($ids); if (null === $deviceData) { $this->flash->error('Unable to get devices!'); return $this->redirect('/reports'); } $totalDeviceData = ['Tower' => ['data' => [], 'singular_description' => 'tower', 'plural_description' => 'towers'], 'Laptop' => ['data' => [], 'singular_description' => 'laptop', 'plural_description' => 'laptops'], 'Phone' => ['data' => [], 'singular_description' => 'phone', 'plural_description' => 'phones'], 'Printer' => ['data' => [], 'singular_description' => 'printer', 'plural_description' => 'printers'], 'Tablet' => ['data' => [], 'singular_description' => 'tablet', 'plural_description' => 'tablets'], 'Other' => ['data' => [], 'singular_description' => 'other device', 'plural_description' => 'other devices']]; $keys = array_keys($totalDeviceData); foreach ($deviceData as $row) { if (in_array($row['type'], $keys)) { $totalDeviceData[$row['type']]['data'][] = $row; } else { $totalDeviceData['Other']['data'][] = $row; } } $output .= "\nDEVICES\n------------\n"; foreach ($totalDeviceData as $row) { $total = $count === 1 ? $row['singular_description'] : $row['plural_description']; $count = count($row['data']); $output .= ($count === 1 ? 'There is ' : 'There are ') . $count . ' ' . $total . " total.\n"; } foreach ($deviceData as $row) { $ids[] = $row['id']; } $data = $modelPart->findAllLinkedToDeviceIds($ids); if (null === $data) { $this->flash->error('Unable to get parts!'); return $this->redirect('/reports'); } $output .= "\nPARTS\n------------\n"; $total = $count === 1 ? ' part' : ' parts'; $count = count($data); $output .= ($count === 1 ? 'There is ' : 'There are ') . $count . $total . " total.\n"; $output .= "\nNEW DEVICES\n------------\n"; foreach ($deviceData as $row) { $found = false; if ($row['status'] === 'New') { foreach ($customerData as $customer) { if ($customer['id'] === $row['customer_id'] || strlen(trim($row['status'])) === 0) { $found = true; $output .= $customer['first_name'] . ' ' . $customer['last_name']; $output .= ' --> ' . $row['type'] . "\n"; } } if (!$found) { $output .= 'Error: There is a device with a missing customer!'; } } } $output .= "\nPENDING DEVICES\n------------\n"; foreach ($deviceData as $row) { $found = false; if ($row['status'] === 'Pending') { foreach ($customerData as $customer) { if ($customer['id'] === $row['customer_id']) { $found = true; $output .= $customer['first_name'] . ' ' . $customer['last_name']; $output .= ' --> ' . $row['type'] . "\n"; } } if (!$found) { $output .= 'Error: There is a device with a missing customer!'; } } } if (!is_dir($this->reportsFolder)) { mkdir($this->reportsFolder, self::REPORT_FOLDER_PERMISSIONS, true); } $reportName .= '-stats'; $filename = $this->reportsFolder . DS . 'generated-' . $reportName . '.txt'; file_put_contents($filename, $output); return $this->redirect('/reports/view/' . $reportName); } }
public function index() { if ($this->shouldLockIfNoPermission('data.view')) { return; } $now = time(); $filterStartDate = 1; $filterEndDate = 1; if (isset($_GET['start_year']) && isset($_GET['start_month'])) { $filterStartDate = strtotime($_GET['start_year'] . '-' . $_GET['start_month'] . '-1'); } if (isset($_GET['end_year']) && isset($_GET['end_month'])) { $filterEndDate = strtotime($_GET['end_year'] . '-' . ($_GET['end_month'] + 1) . '-1') - 1; } if ($filterEndDate <= $filterStartDate) { $filterEndDate = $now - $now % self::SECONDS_IN_DAY + self::SECONDS_IN_DAY; } $searchQuery = ''; if (isset($_GET['search_query'])) { $searchQuery = $_GET['search_query']; } $customerService = new Customer($this->db); $allItems = $customerService->findAllCustomers(); if ($allItems === false) { $this->flash->error('Unable to get customers information!'); return $this->redirect('/'); } $firstDateOfCustomerCreated = $now; $lastDateOfCustomerCreated = 1; foreach ($allItems as $key => $item) { if ($firstDateOfCustomerCreated > $item['created']) { $firstDateOfCustomerCreated = $item['created']; } if ($lastDateOfCustomerCreated < $item['created']) { $lastDateOfCustomerCreated = $item['created']; } } $items = $customerService->searchBetweenDates($searchQuery, $filterStartDate, $filterEndDate); if ($items === false) { $this->flash->error('Unable to get customers information!'); return $this->redirect('/'); } $earliestPossibleDate = $now; $latestPossibleDate = 1; foreach ($items as $key => $item) { if ($earliestPossibleDate > $item['created']) { $earliestPossibleDate = $item['created']; } if ($latestPossibleDate < $item['created']) { $latestPossibleDate = $item['created']; } } if ($filterStartDate < $firstDateOfCustomerCreated) { $filterStartDate = $firstDateOfCustomerCreated; } if ($filterEndDate > $lastDateOfCustomerCreated) { $filterEndDate = $lastDateOfCustomerCreated; } $sortKey = isset($_GET['sortkey']) ? $_GET['sortkey'] : 'first_name'; $sortUsingDescending = isset($_GET['sortorder']) && $_GET['sortorder'] === 'desc' || !isset($_GET['sortorder']); $alpha = new KeySorter($sortKey); if ($sortUsingDescending) { $items = $alpha->sortDescending($items); } else { $items = array_reverse($alpha->sortDescending($items)); } foreach ($items as &$row) { $row['created'] = $this->formatNullableDate($row['created']); } /* Build ranges */ $ranges = []; $startYear = idate('Y', $firstDateOfCustomerCreated); $endYear = idate('Y', $lastDateOfCustomerCreated); for ($i = $startYear; $i <= $endYear; $i++) { $ranges[] = $i; } $this->set('ranges', $ranges); $sortOrder = $sortUsingDescending ? 'desc' : 'asc'; $nextSortOrder = $sortUsingDescending ? 'asc' : 'desc'; $this->set('searchQuery', $searchQuery); $this->set('startDate', $filterStartDate); $this->set('endDate', $filterEndDate); $this->set('startMonth', idate('n', $firstDateOfCustomerCreated)); $this->set('selectedStartYear', idate('Y', $filterStartDate)); $this->set('endMonth', idate('n', $lastDateOfCustomerCreated)); $this->set('selectedEndYear', idate('Y', $filterEndDate)); $this->set('sortOrder', $sortOrder); $this->set('nextSortOrder', $nextSortOrder); $this->set('sortKey', $sortKey); $this->set('items', $items); $this->set('months', self::$months); }