public function execute($request)
 {
     $userObj = sfContext::getInstance()->getUser()->getAttribute('user');
     $this->editPunchIn = array();
     $this->editPunchOut = array();
     $this->employeeId = $request->getParameter('employeeId');
     $this->messageData = array($request->getParameter('message[0]'), $request->getParameter('message[1]'));
     $this->_checkAuthentication($this->employeeId, $userObj);
     $this->date = $request->getParameter('date');
     $this->actionRecorder = $request->getParameter('actionRecorder');
     $this->errorRows = $request->getParameter('errorRows');
     $userObj = sfContext::getInstance()->getUser()->getAttribute('user');
     $userId = $userObj->getUserId();
     $userEmployeeNumber = $userObj->getEmployeeNumber();
     $this->records = $this->getAttendanceService()->getAttendanceRecord($this->employeeId, $this->date);
     $totalRows = sizeOf($this->records);
     $values = array('employeeId' => $this->employeeId, 'date' => $this->date);
     $this->editAttendanceForm = new EditAttendanceRecordForm(array(), $values);
     $formSubmitAction = $request->getParameter('formSubmitAction');
     if ($this->actionRecorder == "viewEmployee") {
         $userRoleFactory = new UserRoleFactory();
         $decoratedUser = $userRoleFactory->decorateUserRole($userId, $this->employeeId, $userEmployeeNumber);
     }
     if ($this->actionRecorder == "viewMy") {
         $user = new User();
         $decoratedUser = new EssUserRoleDecorator($user);
     }
     $i = 1;
     foreach ($this->records as $record) {
         $allowedActionsForCurrentRecord = $decoratedUser->getAllowedActions(WorkflowStateMachine::FLOW_ATTENDANCE, $record->getState());
         if (in_array(WorkflowStateMachine::ATTENDANCE_ACTION_EDIT_PUNCH_IN_TIME, $allowedActionsForCurrentRecord)) {
             $this->editPunchIn[$i] = true;
         } else {
             $this->editPunchIn[$i] = false;
         }
         if (in_array(WorkflowStateMachine::ATTENDANCE_ACTION_EDIT_PUNCH_OUT_TIME, $allowedActionsForCurrentRecord)) {
             $this->editPunchOut[$i] = true;
         } else {
             $this->editPunchOut[$i] = false;
         }
         $i++;
     }
     if ($formSubmitAction) {
         if ($request->isMethod('post')) {
             $this->editAttendanceForm->bind($request->getParameter('attendance'));
             if ($this->editAttendanceForm->isValid()) {
                 $errorArray = $this->editAttendanceForm->save($totalRows, $this->editAttendanceForm);
                 if (!empty($errorArray)) {
                     $errorStr = json_encode($errorArray);
                     $this->redirect('attendance/editAttendanceRecord?employeeId=' . $this->employeeId . '&date=' . $this->date . '&actionRecorder=' . $this->actionRecorder . '&errorRows=' . $errorStr);
                 } else {
                     $messageData = array('SUCCESS', __(TopLevelMessages::SAVE_SUCCESS));
                     if ($this->actionRecorder == "viewMy") {
                         $this->redirect('attendance/viewMyAttendanceRecord' . '?' . http_build_query(array('message' => $messageData, 'actionRecorder' => $this->actionRecorder, 'employeeId' => $this->employeeId, 'date' => $this->date, 'trigger' => true)));
                     }
                     if ($this->actionRecorder == "viewEmployee") {
                         $this->redirect('attendance/viewAttendanceRecord' . '?' . http_build_query(array('message' => $messageData, 'actionRecorder' => $this->actionRecorder, 'employeeId' => $this->employeeId, 'date' => $this->date, 'trigger' => true)));
                     }
                 }
             }
         }
     }
 }
Esempio n. 2
0
 public function executeGetRelatedAttendanceRecords($request)
 {
     $this->allowedToDelete = array();
     $this->allowedActions = array();
     $this->allowedActions['Delete'] = false;
     $this->allowedActions['Edit'] = false;
     $this->allowedActions['PunchIn'] = false;
     $this->allowedActions['PunchOut'] = false;
     $this->userObj = $this->getContext()->getUser()->getAttribute('user');
     $userId = $this->userObj->getUserId();
     $userEmployeeNumber = $this->userObj->getEmployeeNumber();
     $this->employeeId = $request->getParameter('employeeId');
     $this->date = $request->getParameter('date');
     $this->actionRecorder = $request->getParameter('actionRecorder');
     if ($this->actionRecorder == "viewEmployee") {
         $userRoleFactory = new UserRoleFactory();
         $decoratedUser = $userRoleFactory->decorateUserRole($userId, $this->employeeId, $userEmployeeNumber);
     }
     if ($this->actionRecorder == "viewMy") {
         $user = new User();
         $decoratedUser = new EssUserRoleDecorator($user);
     }
     $this->records = $this->getAttendanceService()->getAttendanceRecord($this->employeeId, $this->date);
     $actions = array(PluginWorkflowStateMachine::ATTENDANCE_ACTION_EDIT_PUNCH_OUT_TIME, PluginWorkflowStateMachine::ATTENDANCE_ACTION_EDIT_PUNCH_IN_TIME);
     $actionableStates = $decoratedUser->getActionableAttendanceStates($actions);
     $recArray = array();
     if ($this->records != null) {
         if ($actionableStates != null) {
             foreach ($actionableStates as $state) {
                 foreach ($this->records as $record) {
                     if ($state == $record->getState()) {
                         $this->allowedActions['Edit'] = true;
                         break;
                     }
                 }
             }
         }
         $actions = array(PluginWorkflowStateMachine::ATTENDANCE_ACTION_DELETE);
         $actionableStates = $decoratedUser->getActionableAttendanceStates($actions);
         if ($actionableStates != null) {
             foreach ($actionableStates as $state) {
                 foreach ($this->records as $record) {
                     if ($state == $record->getState()) {
                         $this->allowedActions['Delete'] = true;
                         break;
                     }
                 }
             }
         }
         foreach ($this->records as $record) {
             $this->allowedToDelete[] = $this->allowedToPerformAction(WorkflowStateMachine::FLOW_ATTENDANCE, PluginWorkflowStateMachine::ATTENDANCE_ACTION_DELETE, $record->getState(), $decoratedUser);
             $recArray[] = $record;
         }
     } else {
         $attendanceRecord = null;
     }
     $actions = array(PluginWorkflowStateMachine::ATTENDANCE_ACTION_PROXY_PUNCH_IN, PluginWorkflowStateMachine::ATTENDANCE_ACTION_PROXY_PUNCH_OUT);
     $allowedActionsList = array();
     $actionableStates = $decoratedUser->getActionableAttendanceStates($actions);
     if ($actionableStates != null) {
         if (!empty($recArray)) {
             $lastRecordPunchOutTime = $recArray[count($this->records) - 1]->getPunchOutUserTime();
             if (empty($lastRecordPunchOutTime)) {
                 $attendanceRecord = "";
             } else {
                 $attendanceRecord = null;
             }
         }
         foreach ($actionableStates as $actionableState) {
             $allowedActionsArray = $decoratedUser->getAllowedActions(PluginWorkflowStateMachine::FLOW_ATTENDANCE, $actionableState);
             if (!is_null($allowedActionsArray)) {
                 $allowedActionsList = array_unique(array_merge($allowedActionsArray, $allowedActionsList));
             }
         }
         if (is_null($attendanceRecord) && in_array(WorkflowStateMachine::ATTENDANCE_ACTION_PROXY_PUNCH_IN, $allowedActionsList)) {
             $this->allowedActions['PunchIn'] = true;
         }
         if (!is_null($attendanceRecord) && in_array(WorkflowStateMachine::ATTENDANCE_ACTION_PROXY_PUNCH_OUT, $allowedActionsList)) {
             $this->allowedActions['PunchOut'] = true;
         }
     }
 }
 public function execute($request)
 {
     //timesheetStartDateFromDropDown is from the drop down
     //timesheetStartDate is from the edit timesheet
     //$startDateSelectedFromDropDown set when the user is accessing the view from the search drop down
     //$startDateOfTheTimesheetForUpdates is set when the user performs an action on the timesheet,and it is used to update the timesheet
     $this->createTimesheetForm = new CreateTimesheetForm();
     $this->currentDate = date('Y-m-d');
     $this->headingText = $this->getTimesheetPeriodService()->getTimesheetHeading();
     $this->successMessage = array($request->getParameter('message[0]'), $request->getParameter('message[1]'));
     $startDateSelectedFromDropDown = $request->getParameter('timesheetStartDateFromDropDown');
     $this->userObj = $this->getContext()->getUser()->getAttribute('user');
     $userId = $this->userObj->getUserId();
     $this->format = $this->getTimesheetService()->getTimesheetTimeFormat();
     $this->timeService = $this->getTimesheetService();
     $clientTimeZoneOffset = $this->userObj->getUserTimeZoneOffset();
     $serverTimezoneOffset = (int) date('Z');
     $timeStampDiff = $clientTimeZoneOffset * 3600 - $serverTimezoneOffset;
     if ($request->isMethod('post')) {
         if ($request->getParameter('updateActionLog')) {
             $timesheet = $this->setTimesheetState($request);
             $comment = $request->getParameter('Comment');
             if ($request->getParameter('resetAction')) {
                 $this->setTimesheetActionLog(Timesheet::RESET_ACTION, $comment, $timesheet->getTimesheetId(), $userId);
             } else {
                 $this->setTimesheetActionLog($timesheet->getState(), $comment, $timesheet->getTimesheetId(), $userId);
             }
         }
     }
     /* Decorated user object in the user session, which can be used only to get user's employee number, user id, employee list and accessible Time menus */
     $employeeId = $this->userObj->getEmployeeNumber();
     $this->currentDate = date('Y-m-d');
     $this->actionName = $this->getActionName();
     $submitted = $request->getParameter('submitted');
     if (isset($submitted)) {
         $this->successMessage = array('SUCCESS', __("Timesheet Submitted"));
     }
     $startDateOfTheTimesheetForUpdates = $request->getParameter('timesheetStartDate');
     $this->dateForm = new startDaysListForm(array(), array('employeeId' => $employeeId));
     $dateOptions = $this->dateForm->getDateOptions();
     if (isset($startDateSelectedFromDropDown)) {
         // timesheet is access via the search drop down
         $this->toggleDate = $startDateSelectedFromDropDown;
         $timesheetStartingDate = $startDateSelectedFromDropDown;
     } elseif (isset($startDateOfTheTimesheetForUpdates)) {
         // if the the user is redirecting in the same timesheet(edit,submit)
         $startDatesOfTimeSheetsAccessible = $this->getAcessibleTimesheetStartDates($dateOptions);
         if ($startDatesOfTimeSheetsAccessible == null) {
             $this->messageData = array('NOTICE', __("No Accessible Timesheets"));
             $this->redirect('time/viewMyTimesheet');
         } elseif (in_array($startDateOfTheTimesheetForUpdates, $startDatesOfTimeSheetsAccessible)) {
             $this->toggleDate = $startDateOfTheTimesheetForUpdates;
             $timesheetStartingDate = $startDateOfTheTimesheetForUpdates;
         } else {
             $timesheetStartingDate = $startDatesOfTimeSheetsAccessible[0];
         }
     } else {
         // if the timesheet is access from the menu "My Timesheets"
         if ($dateOptions == null) {
             $statusArray = $this->getTimesheetService()->createTimesheet($employeeId, $this->currentDate);
             switch ($statusArray['state']) {
                 case $statusArray['state'] == 1:
                     $this->redirect('time/overLappingTimesheetError');
                     break;
                 case $statusArray['state'] == 2:
                     $timesheetStartingDate = $statusArray['message'];
                     break;
                 case $statusArray['state'] == 3:
                     $timesheetStartingDate = $statusArray['message'];
                     break;
                 case $statusArray['state'] == 4:
                     $timesheetStartingDate = $statusArray['message'];
                     $this->messageData = array('NOTICE', __("No Accessible Timesheets"));
                     break;
             }
         } else {
             $statusArray = $this->getTimesheetService()->createTimesheet($employeeId, $this->currentDate);
             switch ($statusArray['state']) {
                 case $statusArray['state'] == 1:
                     $this->redirect('time/overLappingTimesheetError');
                     break;
                 case $statusArray['state'] == 2:
                     $timesheetStartingDate = $statusArray['message'];
                     break;
                 case $statusArray['state'] == 3:
                     $timesheetStartingDate = $statusArray['message'];
                     $this->getTimesheetService()->createPreviousTimesheets($timesheetStartingDate, $employeeId);
                     //this creates the timesheets automatically for the past weeks, if the user have not created them
                     break;
                 case $statusArray['state'] == 4:
                     $latestDate = $this->getlatestStartDate($dateOptions);
                     $timesheetStartingDate = $latestDate;
                     break;
             }
         }
     }
     $this->timesheet = $this->getTimesheetService()->getTimesheetByStartDateAndEmployeeId($timesheetStartingDate, $employeeId);
     $this->currentState = $this->timesheet->getState();
     $this->dateForm = new startDaysListForm(array(), array('employeeId' => $employeeId));
     $dateOptions = $this->dateForm->getDateOptions();
     if ($request->getParameter('selectedIndex') != null) {
         $selectedIndex = $request->getParameter('selectedIndex');
     } else {
         $selectedIndex = $this->dateForm->returnSelectedIndex($timesheetStartingDate, $employeeId);
     }
     if (isset($selectedIndex)) {
         $this->dateForm->setDefault('startDates', $selectedIndex);
     }
     $noOfDays = $this->timesheetService->dateDiff($this->timesheet->getStartDate(), $this->timesheet->getEndDate());
     $values = array('date' => $timesheetStartingDate, 'employeeId' => $employeeId, 'timesheetId' => $this->timesheet->getTimesheetId(), 'noOfDays' => $noOfDays);
     $form = new TimesheetForm(array(), $values);
     $this->timesheetRows = $form->getTimesheet($timesheetStartingDate, $employeeId, $this->timesheet->getTimesheetId());
     $this->currentState = $this->timesheet->getState();
     $user = new User();
     $decoratedUser = new EssUserRoleDecorator($user);
     $this->allowedActions = $decoratedUser->getAllowedActions(WorkflowStateMachine::FLOW_TIME_TIMESHEET, $this->currentState);
     $this->allowedToCreateTimesheets = $decoratedUser->getAllowedActions(PluginWorkflowStateMachine::FLOW_TIME_TIMESHEET, PluginTimesheet::STATE_INITIAL);
     $this->submitNextState = $decoratedUser->getNextState(WorkflowStateMachine::FLOW_TIME_TIMESHEET, $this->currentState, WorkflowStateMachine::TIMESHEET_ACTION_SUBMIT);
     $this->approveNextState = $decoratedUser->getNextState(WorkflowStateMachine::FLOW_TIME_TIMESHEET, $this->currentState, WorkflowStateMachine::TIMESHEET_ACTION_APPROVE);
     $this->rejectNextState = $decoratedUser->getNextState(WorkflowStateMachine::FLOW_TIME_TIMESHEET, $this->currentState, WorkflowStateMachine::TIMESHEET_ACTION_REJECT);
     $this->resetNextState = $decoratedUser->getNextState(WorkflowStateMachine::FLOW_TIME_TIMESHEET, $this->currentState, WorkflowStateMachine::TIMESHEET_ACTION_RESET);
     $this->rowDates = $form->getDatesOfTheTimesheetPeriod($this->timesheet->getStartDate(), $this->timesheet->getEndDate());
     $this->actionLogRecords = $this->getTimesheetService()->getTimesheetActionLogByTimesheetId($this->timesheet->getTimesheetId());
     $this->setTemplate("viewTimesheet");
 }