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); } }