unlockModel.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. class unlockModel extends Staple_Model
  3. {
  4. private $db;
  5. private $username;
  6. private $errors;
  7. private $id;
  8. private $date;
  9. private $userId;
  10. /**
  11. * @return mixed
  12. */
  13. public function getId()
  14. {
  15. return $this->id;
  16. }
  17. /**
  18. * @param mixed $id
  19. */
  20. public function setId($id)
  21. {
  22. $this->id = $id;
  23. }
  24. /**
  25. * @return mixed
  26. */
  27. public function getDate()
  28. {
  29. $d = new DateTime();
  30. $d->setTimestamp($this->date);
  31. return $d->format('Y-m-d');
  32. }
  33. /**
  34. * @param mixed $date
  35. */
  36. public function setDate($date)
  37. {
  38. $date = strtotime($date);
  39. $d = new DateTime();
  40. $d->setTimestamp($date);
  41. $this->date = $d->format('U');
  42. }
  43. /**
  44. * @return mixed
  45. */
  46. public function getUserId()
  47. {
  48. return $this->userId;
  49. }
  50. /**
  51. * @param mixed $userId
  52. */
  53. public function setUserId($userId)
  54. {
  55. $this->userId = $userId;
  56. }
  57. /**
  58. * @return mixed
  59. */
  60. public function getErrors()
  61. {
  62. return $this->errors;
  63. }
  64. /**
  65. * @param mixed $errors
  66. */
  67. public function setErrors($errors)
  68. {
  69. $this->errors = $errors;
  70. }
  71. function __construct()
  72. {
  73. $this->db = Staple_DB::get();
  74. $auth = Staple_Auth::get();
  75. $this->username = $auth->getAuthId();
  76. }
  77. function load($uid)
  78. {
  79. $sql = "SELECT * FROM overrideDates WHERE userId = '".$this->db->real_escape_string($uid)."' ORDER BY date ASC";
  80. if($this->db->query($sql)->fetch_row() > 0)
  81. {
  82. $query = $this->db->query($sql);
  83. while($result = $query->fetch_assoc())
  84. {
  85. $data[] = $result;
  86. }
  87. return $data;
  88. }
  89. }
  90. function save()
  91. {
  92. if(isset($this->date) && !isset($this->id))
  93. {
  94. $user = new userModel();
  95. if($this->getUserId() != $user->getId())
  96. {
  97. //Check if date is in the currect pay period.
  98. $timesheet = new timesheetModel(date('Y'),date('m'));
  99. if($this->date < $timesheet->getStartDateTimeString())
  100. {
  101. //Check for existing date
  102. $sql = "SELECT id FROM overrideDates WHERE date = '".$this->db->real_escape_string($this->date)."' AND userId = '".$this->db->real_escape_string($this->userId)."'";
  103. if($this->db->query($sql)->num_rows == 0)
  104. {
  105. //Check for already existing time entry
  106. $sql = "SELECT FROM_UNIXTIME(inTime,'%Y-%m-%d') AS date FROM timeEntries WHERE userId = '".$this->db->real_escape_string($this->userId)."'";
  107. $query = $this->db->query($sql);
  108. $matchDates = 0;
  109. while($result = $query->fetch_assoc())
  110. {
  111. $date = new DateTime();
  112. $date->setTimestamp($this->date);
  113. $submitDate = $date->format('Y-m-d');
  114. if($result['date'] == $submitDate)
  115. {
  116. $matchDates++;
  117. }
  118. }
  119. if($matchDates == 0)
  120. {
  121. $sql = "
  122. INSERT INTO overrideDates (date, userId) VALUES ('".$this->db->real_escape_string($this->date)."','".$this->db->real_escape_string($this->userId)."')
  123. ";
  124. if($this->db->query($sql))
  125. {
  126. $audit = new auditModel();
  127. $audit->setUserId($this->userId);
  128. $audit->setAction('Date unlock');
  129. $audit->setItem($this->username." unlocked date ".$this->getDate());
  130. $audit->save();
  131. return True;
  132. }
  133. }
  134. else
  135. {
  136. $this->errors[] = 'Time entry already exists for this date.';
  137. }
  138. }
  139. else
  140. {
  141. $this->errors[] = 'Unlock already submitted for this date.';
  142. }
  143. }
  144. else
  145. {
  146. $this->errors[] = "Date cannot be part of the current pay period.";
  147. }
  148. }
  149. else
  150. {
  151. $this->errors[] = "Cannot unlock time entires for your own timesheet.";
  152. }
  153. }
  154. }
  155. function unlock($id)
  156. {
  157. $sql = "
  158. SELECT userId FROM timeEntries WHERE id = '".$this->db->real_escape_string($id)."';
  159. ";
  160. if($this->db->query($sql)->num_rows > 0)
  161. {
  162. $query = $this->db->query($sql);
  163. $result = $query->fetch_assoc();
  164. $userId = $result['userId'];
  165. $user = new userModel();
  166. $user = $user->userInfo($userId);
  167. $userId = $user['id'];
  168. $batchId = $user['batchId'];
  169. //Check if it's for the same user.
  170. $currentUser = new userModel();
  171. if($currentUser->getId() != $userId)
  172. {
  173. $sql = "
  174. UPDATE timeEntries SET batchId = '".$this->db->real_escape_string($batchId)."' WHERE id = '".$this->db->real_escape_string($id)."'
  175. ";
  176. if($this->db->query($sql))
  177. {
  178. $audit = new auditModel();
  179. $audit->setUserId($userId);
  180. $audit->setAction('Single unlock');
  181. $audit->setItem($this->username." unlocked time entry ". $id);
  182. $audit->save();
  183. return true;
  184. }
  185. }
  186. }
  187. }
  188. }
  189. ?>