/**
  * Handles the command
  *
  * @param UpdateDepartmentCommand $command
  * @return Department
  */
 public function handle($command)
 {
     $department = $this->departmentRepository->getDepartmentByID($command->id);
     $updateDepartment = "";
     //Check if model exists
     if ($department) {
         //Update department_id and department_name
         $department->department_id = $command->department_id;
         $department->department_name = $command->department_name;
         $updateDepartment = $this->departmentRepository->save($department);
         //Check if saving of department is successful and if department_head as input exists
         if ($updateDepartment && $command->department_head) {
             $previousHead = $department->employees()->where('position', '=', '1')->first();
             /**Check if there is already an employee who is heading the department, 
              *if yes change his position to member employee
              */
             if ($previousHead) {
                 $previousHead->position = 0;
                 $demotePreviousHead = $this->employeeRepository->save($previousHead);
             }
             //Chenge the chosen employee position to head employee and save
             $nextHead = $department->employees()->where('username', $command->department_head)->firstOrFail();
             $nextHead->position = 1;
             $promoteMember = $this->employeeRepository->save($nextHead);
         }
     }
     return $department;
 }
 /**
  * Handles the command.
  *
  * @param RemoveDepartmentCommand $command
  * @return Department
  */
 public function handle($command)
 {
     $departmentRemove = $this->departmentRepository->remove($command->department_id);
     if ($departmentRemove) {
         $employees = $this->employeeRepository->getEmployeesByDepartment($command->department_id);
         if ($employees) {
             foreach ($employees as $employee) {
                 $employee->position = 0;
                 //assign as member employee
                 $employee->department_id = 'D-BCD';
                 //assign to default department
                 $employee->save();
             }
         }
     }
     return $departmentRemove;
 }
 /**
  * Handles the command.
  *
  * @param RestoreDepartmentCommand $command
  * @return Department
  */
 public function handle($command)
 {
     $departmentRestore = $this->departmentRepository->restore($command->department_id);
     return $departmentRestore;
 }
 /**
  * Show the form for editing employee information.
  *
  * @param  String $username
  * @return View
  */
 public function edit($username)
 {
     $employee = $this->employees->find($username);
     $departments = $this->departments->orderByName();
     return View::make('admin.edit.employee', ['pageTitle' => 'Edit Employee Profile'], compact('employee', 'departments'));
 }
 /**
  * Export list of departments to Excel
  *
  * @return Excel
  */
 public function export()
 {
     $departments = $this->departments->getCSVReport();
     $excel = new ExportToExcel($departments, 'List of Departments');
     return $excel->export();
 }
 /**
  * Handle the command
  *
  * @param AddDepartmentCommand $command
  * @return Department
  */
 public function handle($command)
 {
     $department = Department::register($command->department_id, $command->department_name);
     $this->departmentRepository->save($department);
     return $department;
 }