public function validate($year, $month) { $timesheet = new timesheetModel($year, $month); //Get Current Batch ID $auth = Staple_Auth::get(); $user = new userModel($auth->getAuthId()); $batchId = $user->getBatchId(); //Check for unvalidated entries within the current pay period. $i = 0; foreach ($timesheet->getEntries() as $entry) { if ($entry->inTimeRaw >= $timesheet->getStartDateTimeString() && $entry->inTimeRaw <= $timesheet->getEndDateTimeString()) { if ($entry->batchId == $timesheet->getBatch()) { $i++; } } } if ($i > 0) { $this->view->timesheet = $timesheet; $form = new validateTimeSheetForm(); $form->setAction($this->_link(array('timesheet', 'validate', $timesheet->getCurrentYear(), $timesheet->getCurrentMonth()))); if ($form->wasSubmitted()) { if ($entry->inTimeRaw >= $timesheet->getStartDateTimeString() && $entry->inTimeRaw <= $timesheet->getEndDateTimeString()) { $timesheet->validate($batchId); header("location:" . $this->_link(array('timesheet')) . ""); } } else { $this->view->form = $form; $this->view->needsValidation = false; } } else { $this->view->needsValidation = false; $this->view->timesheet = array(); } }
function save() { if (isset($this->date) && !isset($this->id)) { $user = new userModel(); if ($this->getUserId() != $user->getId()) { //Check if date is in the currect pay period. $timesheet = new timesheetModel(date('Y'), date('m')); if ($this->date < $timesheet->getStartDateTimeString()) { //Check for existing date $sql = "SELECT id FROM overrideDates WHERE date = '" . $this->db->real_escape_string($this->date) . "' AND userId = '" . $this->db->real_escape_string($this->userId) . "'"; if ($this->db->query($sql)->num_rows == 0) { //Check for already existing time entry $sql = "SELECT FROM_UNIXTIME(inTime,'%Y-%m-%d') AS date FROM timeEntries WHERE userId = '" . $this->db->real_escape_string($this->userId) . "'"; $query = $this->db->query($sql); $matchDates = 0; while ($result = $query->fetch_assoc()) { $date = new DateTime(); $date->setTimestamp($this->date); $submitDate = $date->format('Y-m-d'); if ($result['date'] == $submitDate) { $matchDates++; } } if ($matchDates == 0) { $sql = "\n INSERT INTO overrideDates (date, userId) VALUES ('" . $this->db->real_escape_string($this->date) . "','" . $this->db->real_escape_string($this->userId) . "')\n "; if ($this->db->query($sql)) { $audit = new auditModel(); $audit->setUserId($this->userId); $audit->setAction('Date unlock'); $audit->setItem($this->username . " unlocked date " . $this->getDate()); $audit->save(); return True; } } else { $this->errors[] = 'Time entry already exists for this date.'; } } else { $this->errors[] = 'Unlock already submitted for this date.'; } } else { $this->errors[] = "Date cannot be part of the current pay period."; } } else { $this->errors[] = "Cannot unlock time entires for your own timesheet."; } } }