/** * 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; }
/** * * Handle the command * * @param RegisterEmployeeCommand $command * @return mixed */ public function handle($command) { $account = Account::addAccount($command->username, $command->password); $employee = Employee::register($command->username, $command->first_name, $command->middle_name, $command->last_name, $command->birthdate, $command->address, $command->email, $command->mobile, $command->department_id); $this->accountRepository->save($account); $this->employeeRepository->save($employee); return $employee; }
/** * 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; }
/** * Command handler * * @param RemoveEmployeeCommand * @return Employee */ public function handle($command) { $accountDelete = $this->accountRepository->remove($command->username); $employeeDelete = $this->employeeRepository->remove($command->username); return $employeeDelete; }
/** * Export list of employees to Excel * * @return Excel */ public function export() { $employees = $this->employees->getCSVReport(); $excel = new ExportToExcel($employees, 'List of Employees'); return $excel->export(); }
/** * Handle the command * * @param UpdateEmployeeCommand $command * @return mixed */ public function handle($command) { $update = $this->employeeRepository->update($command->username, $command->first_name, $command->middle_name, $command->last_name, $command->birthdate, $command->address, $command->email, $command->mobile, $command->department_id); return $update; }