/** * Get the task that represents the leave for a given leave application * and add some time to it for the given application */ public function applyTimeForLeave(LeaveApplication $leaveApplication) { $project = $this->projectService->getProject(za()->getConfig('leave_project')); if (!$project) { throw new Exception("Leave project not set correctly in configuration"); } $monthYear = date('F Y', strtotime($leaveApplication->to)); $params = array('parentid=' => $project->id, 'title=' => $monthYear); // get the appropriate milestone $projs = $this->projectService->getProjects($params); $milestone = null; if (count($projs)) { $milestone = $projs[0]; } else { // create a new milestone // $milestone $date = date('Y-m-t', strtotime($leaveApplication->to)) . ' 23:59:59'; $milestone = $this->projectService->createMilestone($project, $monthYear, $date); } // now get the task for the given leave app $taskTitle = $leaveApplication->leavetype . ' Leave #' . $leaveApplication->id . ': ' . $leaveApplication->username . ' ' . date('Y-m-d', strtotime($leaveApplication->from)) . ' - ' . date('Y-m-d', strtotime($leaveApplication->to)); $params = array('projectid=' => $milestone->id, 'title=' => $taskTitle); $tasks = $this->projectService->getTasks($params); $user = $this->getUserByField('username', $leaveApplication->username); $task = null; if (count($tasks)) { $task = $tasks[0]; // delete all timesheet entries for this user on this task $records = $this->projectService->getDetailedTimesheet($user, $task->id); foreach ($records as $record) { $this->projectService->removeTimesheetRecord($record); } } else { // create the new task $task = new Task(); za()->inject($task); $task->title = $taskTitle; $task->projectid = $milestone->id; $task->category = 'Leave'; $task->due = $leaveApplication->to; $task->description = $leaveApplication->reason; $task->estimated = za()->getConfig('day_length') * $leaveApplication->days; $task->complete = 1; $task = $this->projectService->saveTask($task); } if ($task != null) { // now add all the timesheet entries for each given day $startTime = strtotime(date('Y-m-d', strtotime($leaveApplication->from)) . ' 09:00:00'); // now go through and add time for the given day for ($i = 0; $i < $leaveApplication->days; $i++) { // see if today's a weekend, if so we want to skip til the next monday $curDay = date('D', $startTime); if ($curDay == 'Sat') { $startTime += 2 * 86400; } else { if ($curDay == 'Sun') { $startTime += 86400; } } $endTime = $startTime + za()->getConfig('day_length') * 3600; $this->projectService->addTimesheetRecord($task, $user, $startTime, $endTime); $startTime += 86400; } } }
public function deleterecordAction() { $record = $this->byId(null, 'TimesheetRecord'); $this->projectService->removeTimesheetRecord($record); }