public function addFor($id)
 {
     if ($this->shouldLockIfNoPermission('data.edit')) {
         return;
     }
     $modelPart = new Part($this->db);
     if ($this->request->is('post')) {
         $user = $this->session->get('user');
         if ($modelPart->addToDevice($_POST['description'], $id, $user['username'])) {
             $this->flash->success('Added part successfully!');
             return $this->redirect('/parts/view/' . $modelPart->lastInsertId());
         } else {
             $this->flash->addMany($modelPart->getAllMessages(), 'warning');
         }
         return $this->redirect('/parts/view/' . $id);
     }
     $modelDevice = new Device($this->db);
     $device = $modelDevice->findById($id);
     if (null === $device) {
         $this->flash->error('Unable to get device information!');
         return $this->redirect('/parts/view/' . $id);
     }
     $this->set('device', $device);
 }
 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 generateInvoice($id)
 {
     $modelDevice = new Device($this->db);
     $device = $modelDevice->findById($id);
     if ($device === null) {
         $this->flash->error('Unable to generate invoice!');
         return $this->redirect('/devices/view/' . $id);
     }
     $modelCustomer = new Customer($this->db);
     $customer = $modelCustomer->findById($device['customer_id']);
     if ($customer === null) {
         $this->flash->error('Unable to generate invoice!');
         return $this->redirect('/devices/view/' . $id);
     }
     $modelPart = new Part($this->db);
     $parts = $modelPart->findAllByDeviceId($device['id']);
     if ($parts === null) {
         $this->flash->error('Unable to generate invoice!');
         return $this->redirect('/devices/view/' . $id);
     }
     $infoShop = [$customer['shop'], $customer['shop_periods']];
     $infoAddress = [\ICanBoogie\titleize($customer['address']), strtoupper($customer['state']) . ' ' . \ICanBoogie\titleize($customer['zip'])];
     $infoStaff = [];
     if (strlen(trim($customer['room_number'])) > 0) {
         $infoStaff[] = 'Room #' . $customer['room_number'];
     }
     if (strlen(trim($customer['phone_ext'])) > 0) {
         $infoStaff[] = 'Ext.' . $customer['phone_ext'];
     }
     $this->set('now', date('n/j/Y', time()));
     $this->set('parts', $parts);
     $this->set('device', $device);
     $this->set('customer', $customer);
     $this->set('infoShop', implode(', ', array_filter(array_map('trim', $infoShop), 'strlen')));
     $this->set('infoStaff', implode(', ', array_filter(array_map('trim', $infoStaff), 'strlen')));
     $this->set('infoAddress', implode(', ', array_filter(array_map('trim', $infoAddress), 'strlen')));
 }
 public function delete($id)
 {
     if ($this->shouldLockIfNoPermission('data.edit')) {
         return;
     }
     $customerService = new Customer($this->db);
     if ($this->request->is('post')) {
         $deviceService = new Device($this->db);
         $devices = $deviceService->findAllByCustomerId($id);
         if (null === $devices) {
             $this->flash->error('Unable to delete customer!');
             return $this->redirect('/customers');
         }
         $customerService->deleteById($id);
         $partService = new Part($this->db);
         foreach ($devices as $device) {
             $partService->deleteAllByDeviceId($device['id']);
         }
         $deviceService->deleteAllByCustomerId($id);
         $this->flash->success('Deleted customer successfully!');
         return $this->redirect('/customers');
     }
     $item = $customerService->findById($id);
     $this->set('item', $item);
     $this->set('id', $id);
 }