123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- class unlockModel extends Staple_Model
- {
- private $db;
- private $username;
- private $errors;
- private $id;
- private $date;
- private $userId;
- /**
- * @return mixed
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * @param mixed $id
- */
- public function setId($id)
- {
- $this->id = $id;
- }
- /**
- * @return mixed
- */
- public function getDate()
- {
- $d = new DateTime();
- $d->setTimestamp($this->date);
- return $d->format('Y-m-d');
- }
- /**
- * @param mixed $date
- */
- public function setDate($date)
- {
- $date = strtotime($date);
- $d = new DateTime();
- $d->setTimestamp($date);
- $this->date = $d->format('U');
- }
- /**
- * @return mixed
- */
- public function getUserId()
- {
- return $this->userId;
- }
- /**
- * @param mixed $userId
- */
- public function setUserId($userId)
- {
- $this->userId = $userId;
- }
- /**
- * @return mixed
- */
- public function getErrors()
- {
- return $this->errors;
- }
- /**
- * @param mixed $errors
- */
- public function setErrors($errors)
- {
- $this->errors = $errors;
- }
- function __construct()
- {
- $this->db = Staple_DB::get();
- $auth = Staple_Auth::get();
- $this->username = $auth->getAuthId();
- }
- function load($uid)
- {
- $sql = "SELECT * FROM overrideDates WHERE userId = '".$this->db->real_escape_string($uid)."' ORDER BY date ASC";
- if($this->db->query($sql)->fetch_row() > 0)
- {
- $query = $this->db->query($sql);
- while($result = $query->fetch_assoc())
- {
- $data[] = $result;
- }
- return $data;
- }
- }
- 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 = "
- INSERT INTO overrideDates (date, userId) VALUES ('".$this->db->real_escape_string($this->date)."','".$this->db->real_escape_string($this->userId)."')
- ";
- 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.";
- }
- }
- }
- function unlock($id)
- {
- $sql = "
- SELECT userId FROM timeEntries WHERE id = '".$this->db->real_escape_string($id)."';
- ";
- if($this->db->query($sql)->num_rows > 0)
- {
- $query = $this->db->query($sql);
- $result = $query->fetch_assoc();
- $userId = $result['userId'];
- $user = new userModel();
- $user = $user->userInfo($userId);
- $userId = $user['id'];
- $batchId = $user['batchId'];
- //Check if it's for the same user.
- $currentUser = new userModel();
- if($currentUser->getId() != $userId)
- {
- $sql = "
- UPDATE timeEntries SET batchId = '".$this->db->real_escape_string($batchId)."' WHERE id = '".$this->db->real_escape_string($id)."'
- ";
- if($this->db->query($sql))
- {
- $audit = new auditModel();
- $audit->setUserId($userId);
- $audit->setAction('Single unlock');
- $audit->setItem($this->username." unlocked time entry ". $id);
- $audit->save();
- return true;
- }
- }
- }
- }
- }
- ?>
|