function __construct($id = null) { $this->db = Staple_DB::get(); if ($id !== null) { $sql = "SELECT * FROM timeEntries WHERE id = '" . $this->db->real_escape_string($id) . "'"; if ($this->db->query($sql)->fetch_row() > 0) { $query = $this->db->query($sql); $result = $query->fetch_assoc(); //Set ID and Date $this->setId($result['id']); $this->setBatchId($result['batchId']); $this->setDate(date("m/d/Y", $result['inTime'])); $this->setFullDate(date("l, F jS Y", $result['inTime'])); //Set inTime $inTime = new DateTime(); $inTime->setTimestamp($result['inTime']); $this->setInTime($inTime->format('g:i A')); $this->setInTimeRaw($result['inTime']); $this->setRoundedInTime($this->nearestQuarterHour($result['inTime'])); $this->setInTimeDate(date("Y-m-d", $result['inTime'])); //Out Time $outTime = new DateTime(); $outTime->setTimestamp($result['outTime']); $this->setOutTime($outTime->format('g:i A')); $this->setOutTimeRaw($result['outTime']); $this->setRoundedOutTime($this->nearestQuarterHour($result['outTime'])); $this->setOutTimeDate(date("Y-m-d", $result['outTime'])); $this->setLessTime($result['lessTime']); //Calculate Time Worked switch ($result['lessTime']) { case 60: $lessTime = 1; break; case 30: $lessTime = 0.5; break; case 15: $lessTime = 0.25; break; default: $lessTime = 0; } //Total Worked Time $dateTime1 = new DateTime($this->roundedInTime); $dateTime1->setDate(date('Y', strtotime($this->inTimeDate)), date('m', strtotime($this->inTimeDate)), date('d', strtotime($this->inTimeDate))); $dateTime2 = new DateTime($this->roundedOutTime); $dateTime2->setDate(date('Y', strtotime($this->outTimeDate)), date('m', strtotime($this->outTimeDate)), date('d', strtotime($this->outTimeDate))); $interval = $dateTime1->diff($dateTime2); $timeWorked = $this->timeToDecimal($interval->h . ":" . $interval->i) - $lessTime; if ($timeWorked !== 0) { $this->setTimeWorked($timeWorked); } else { $this->setTimeWorked(0); } //Get Code Information $code = new codeModel(); $this->setCodeId($result['codeId']); $code->load($result['codeId']); $this->setCodeName($code->getName()); $this->setUserId($result['userId']); $this->setTimestamp($result['timestamp']); $this->setNote($result['note']); return true; } } }
function calculateEntry($id) { $sql = "\n SELECT * FROM timeEntries WHERE id = '" . $this->db->real_escape_string($id) . "';\n "; $query = $this->db->query($sql); $result = $query->fetch_assoc(); //Set inTime $inTime = new DateTime(); $inTime->setTimestamp($result['inTime']); $roundedInTime = $this->nearestQuarterHour($result['inTime']); $inTimeRaw = $result['inTime']; $inTimeDate = date("Y-m-d", $result['inTime']); //Out Time $outTime = new DateTime(); $outTime->setTimestamp($result['outTime']); $roundedOutTime = $this->nearestQuarterHour($result['outTime']); $outTimeRaw = $result['outTime']; $roundedOutTime = $this->nearestQuarterHour($result['outTime']); $outTimeDate = date("Y-m-d", $result['outTime']); $lessTime = $result['lessTime']; $timestamp = $result['timestamp']; $note = $result['note']; //Calculate Time Worked switch ($result['lessTime']) { case 60: $lessTime = 1; break; case 30: $lessTime = 0.5; break; case 15: $lessTime = 0.25; break; default: $lessTime = 0; } //Total Worked Time $dateTime1 = new DateTime($roundedInTime); $dateTime1->setDate(date('Y', strtotime($inTimeDate)), date('m', strtotime($inTimeDate)), date('d', strtotime($inTimeDate))); $dateTime2 = new DateTime($roundedOutTime); $dateTime2->setDate(date('Y', strtotime($outTimeDate)), date('m', strtotime($outTimeDate)), date('d', strtotime($outTimeDate))); $interval = $dateTime1->diff($dateTime2); $timeWorked = $this->timeToDecimal($interval->h . ":" . $interval->i) - $lessTime; if ($timeWorked !== 0) { $timeWorked = $timeWorked; } else { $timeWorked = 0; } //Get Code Information $code = new codeModel(); $codeId = $result['codeId']; $code->load($result['codeId']); $codeName = $code->getName(); $data['date'] = date('Y-m-d', $inTimeRaw); $data['inTime'] = $inTimeRaw; $data['outTime'] = $outTimeRaw; $data['lessTime'] = $lessTime; $data['timeWorked'] = $timeWorked; $data['code'] = $codeName; $data['timestamp'] = $timestamp; $data['note'] = $note; //Get the user of the entry. $entry = new timeEntryModel($id); if ($entry->validated($id, $result['userId'])) { $data['validated'] = 0; } else { $data['validated'] = 1; } return $data; }