PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/SERVER/timesheet/timesheet.php

https://github.com/joelbrock/is4c_nofc
PHP | 281 lines | 250 code | 23 blank | 8 comment | 34 complexity | 915974e5aa144024e1761c0a53271464 MD5 | raw file
  1. <?php
  2. $max = 5; // Max number of entries.
  3. require_once ('../define.conf');
  4. // mysql_select_db(DB_NAME, 'is4c_log');
  5. if (isset($_POST['submitted'])) { // If the form has been submitted.
  6. // Validate the data.
  7. $errors = array();
  8. //if (checkdate($_POST['month'], $_POST['date'], date('Y'))) {
  9. //2011-01-03 sdh - added a field to select a YEAR
  10. if (checkdate($_POST['month'], $_POST['date'],$_POST['year'])) {
  11. $date = $_POST['year'] . '-' . str_pad($_POST['month'], 2, 0, STR_PAD_LEFT) . '-' . $_POST['date'];
  12. } else {
  13. $errors[] = 'The date you have entered is not a valid date.';
  14. }
  15. if (strtotime($date) > strtotime(date('Y-m-d'))) {
  16. $errors[] = 'You can\'t enter hours for a future date.';
  17. }
  18. // Make sure we're in a valid pay period.
  19. $query = "SELECT periodID FROM is4c_log.payperiods WHERE '$date' BETWEEN DATE(periodStart) AND DATE(periodEnd)";
  20. $result = mysql_query($query);
  21. list($periodID) = mysql_fetch_row($result);
  22. $query = "SELECT DATEDIFF(CURDATE(), DATE(periodEnd)) FROM is4c_log.payperiods WHERE periodID = $periodID";
  23. $result = mysql_query($query);
  24. list($datediff) = mysql_fetch_row($result);
  25. if (!is_numeric($_POST['emp_no'])) {
  26. $errors[] = 'You didn\'t select your name.';
  27. } else {
  28. $emp_no = $_POST['emp_no'];
  29. }
  30. if ($datediff > 1) { // Bad.
  31. $errors[] = 'You can\'t add hours more than a day after the pay period has ended.';
  32. $date = NULL;
  33. }
  34. $entrycount = 0;
  35. for ($i = 1; $i <= $max; $i++) {
  36. if ((isset($_POST['in' . $i])) && (isset($_POST['out' . $i])) && (is_numeric($_POST['area' . $i]))) {
  37. $entrycount++;
  38. }
  39. }
  40. $lunch = $_POST['lunch'];
  41. $hour = array();
  42. $area = array();
  43. if ($entrycount == 0) {
  44. $errors[] = "You didn't enter any hours.";
  45. } else {
  46. for ($i = 1; $i <= $max; $i++) {
  47. if ((isset($_POST['in' . $i])) && (isset($_POST['out' . $i])) && (is_numeric($_POST['area' . $i]))) {
  48. if (strlen($_POST['in' . $i]) == 2 && is_numeric($_POST['in' . $i])) {
  49. $_POST['in' . $i] = $_POST['in' . $i] . ':00';
  50. } elseif (strlen($_POST['in' . $i]) == 4 && is_numeric($_POST['in' . $i])) {
  51. $_POST['in' . $i] = substr($_POST['in' . $i], 0, 2) . ':' . substr($_POST['in' . $i], 2, 2);
  52. } elseif (strlen($_POST['in' . $i]) == 3 && is_numeric($_POST['in' . $i])) {
  53. $_POST['in' . $i] = substr($_POST['in' . $i], 0, 1) . ':' . substr($_POST['in' . $i], 1, 2);
  54. } elseif (strlen($_POST['in' . $i]) == 1 && is_numeric($_POST['in' . $i])) {
  55. $_POST['in' . $i] = $_POST['in' . $i] . ':00';
  56. }
  57. if (strlen($_POST['out' . $i]) == 2 && is_numeric($_POST['out' . $i])) {
  58. $_POST['out' . $i] = $_POST['out' . $i] . ':00';
  59. } elseif (strlen($_POST['out' . $i]) == 4 && is_numeric($_POST['out' . $i])) {
  60. $_POST['out' . $i] = substr($_POST['out' . $i], 0, 2) . ':' . substr($_POST['out' . $i], 2, 2);
  61. } elseif (strlen($_POST['out' . $i]) == 3 && is_numeric($_POST['out' . $i])) {
  62. $_POST['out' . $i] = substr($_POST['out' . $i], 0, 1) . ':' . substr($_POST['out' . $i], 1, 2);
  63. } elseif (strlen($_POST['out' . $i]) == 1 && is_numeric($_POST['out' . $i])) {
  64. $_POST['out' . $i] = $_POST['out' . $i] . ':00';
  65. }
  66. $in = explode(':', $_POST['in' . $i]);
  67. $out = explode(':', $_POST['out' . $i]);
  68. if (($_POST['inmeridian' . $i] == 'PM') && ($in[0] < 12)) {
  69. $in[0] = $in[0] + 12;
  70. } elseif (($_POST['inmeridian' . $i] == 'AM') && ($in[0] == 12)) {
  71. $in[0] = 0;
  72. }
  73. if (($_POST['outmeridian' . $i] == 'PM') && ($out[0] < 12)) {
  74. $out[0] = $out[0] + 12;
  75. } elseif (($_POST['outmeridian' . $i] == 'AM') && ($out[0] == 12)) {
  76. $out[0] = 0;
  77. }
  78. $timein[$i] = $date . ' ' . $in[0] . ':' . $in[1] . ':00';
  79. $timeout[$i] = $date . ' ' . $out[0] . ':' . $out[1] . ':00';
  80. $area[$i] = $_POST['area' . $i];
  81. $sub[$i] = $_POST['sub' . $i] == 'on' ? 1 : 0;
  82. if (strtotime($timein[$i]) >= strtotime($timeout[$i])) {
  83. $errors[] = "You can't have gotten here after you finished work.</p><p>Or, you couldn't have finished work before you started work.";
  84. }
  85. }
  86. }
  87. }
  88. if (empty($errors)) { // All good.
  89. // First check to make sure they haven't already entered hours for this day.
  90. $query = "SELECT * FROM is4c_log.timesheet WHERE emp_no=$emp_no AND date='$date'";
  91. $result = mysql_query($query);
  92. if (mysql_num_rows($result) == 0) { // Success.
  93. $successcount = 0;
  94. for ($i = 1; $i <= $entrycount; $i++) {
  95. $query = "INSERT INTO is4c_log.timesheet (emp_no, time_in, time_out, area, date, periodID, sub)
  96. VALUES ($emp_no, '{$timein[$i]}', '{$timeout[$i]}', {$area[$i]}, '$date', $periodID, $sub[$i])";
  97. $result = mysql_query($query);
  98. if (mysql_affected_rows($dbc) == 1) {$successcount++;}
  99. }
  100. if ($successcount == $entrycount) {
  101. } else {
  102. $header = 'Timesheet Management';
  103. $page_title = 'Fannie - Administration Module';
  104. include ('../src/header.php');
  105. include ('./includes/header.html');
  106. echo '<p>The entered hours could not be added, please try again later.</p>';
  107. echo '<p>Error: ' . mysql_error() . '</p>';
  108. echo '<p>Query: ' . $query . '</p>';
  109. include ('../src/footer.php');
  110. exit();
  111. }
  112. $query = "INSERT INTO is4c_log.timesheet (emp_no, time_out, time_in, area, date, periodID)
  113. VALUES ($emp_no, '2008-01-01 00:00:00', '2008-01-01 " . $lunch . "', 0, '$date', $periodID)";
  114. $result = mysql_query($query);
  115. if (!$result) {
  116. $header = 'Timesheet Management';
  117. $page_title = 'Fannie - Administration Module';
  118. include ('../src/header.php');
  119. include ('./includes/header.html');
  120. echo '<p>The entered hours could not be added, please try again later.</p>';
  121. echo '<p>Error: ' . mysql_error() . '</p>';
  122. echo '<p>Query: ' . $query . '</p>';
  123. include ('../src/footer.php');
  124. exit();
  125. } else {
  126. // Start the redirect.
  127. $url = "/timesheet/viewsheet.php?emp_no=$emp_no&period=$periodID";
  128. header("Location: $url");
  129. exit();
  130. }
  131. } else {
  132. $header = 'Timesheet Management';
  133. $page_title = 'Fannie - Administration Module';
  134. include ('../src/header.php');
  135. include ('./includes/header.html');
  136. echo '<p>You have already entered hours for that day, please edit that day instead.</p>';
  137. }
  138. } else { // Report errors.
  139. $header = 'Timesheet Management';
  140. $page_title = 'Fannie - Administration Module';
  141. include ('../src/header.php');
  142. include ('./includes/header.html');
  143. echo '<p><font color="red">The following error(s) occurred:</font></p>';
  144. foreach ($errors AS $message) {
  145. echo "<p> - $message</p>";
  146. }
  147. echo '<p>Please try again.</p>';
  148. }
  149. } else { // Otherwise display the form.
  150. echo '<script type="text/javascript" language="javascript">
  151. window.onload = initAll;
  152. function initAll() {
  153. for (var i = 1; i <= 5 ; i++) {
  154. document.getElementById(i + "14").disabled = true;
  155. }
  156. }
  157. //this function was used by Matthaus (#7012) to hide certain Categories
  158. function updateshifts(sIndex) {
  159. if (sIndex == 7012) {
  160. for (var i = 1; i <= 5 ; i++) {
  161. document.getElementById(i + "14").disabled = false;
  162. }
  163. } else {
  164. for (var i = 1; i <= 5 ; i++) {
  165. document.getElementById(i + "14").disabled = true;
  166. }
  167. }
  168. }
  169. </script>';
  170. $header = 'Timesheet Management';
  171. $page_title = 'Fannie - Administration Module';
  172. // include ('../includes/header.html');
  173. include('../src/header.php');
  174. include ('./includes/header.html');
  175. $months = array(01=>'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
  176. echo '<form action="timesheet.php" method="POST" name="timesheet">
  177. <p>Name: <select name="emp_no" onchange="updateshifts(this.value);">
  178. <option value="error">Who are You?</option>' . "\n";
  179. $query = "SELECT FirstName, emp_no FROM is4c_op.employees where EmpActive=1 ORDER BY FirstName ASC";
  180. $result = mysql_query($query);
  181. while ($row = mysql_fetch_array($result)) {
  182. echo "<option value=\"$row[1]\">$row[0]</option>\n";
  183. }
  184. echo '</select></p>
  185. <p>Month: <select name="month">';
  186. foreach ($months AS $value => $key) {
  187. echo "<option value=\"$value\"";
  188. if (date('m')==$value) echo ' SELECTED';
  189. echo ">$key</option>\n";
  190. }
  191. echo '</select>
  192. Date: <select name="date">';
  193. for ($i = 1; $i <= 31; $i++) {
  194. $i = str_pad($i, 2, 0, STR_PAD_LEFT);
  195. echo "<option value=\"$i\"";
  196. if (date('d') == $i) echo ' SELECTED';
  197. echo ">$i</option>\n";
  198. }
  199. echo '</select>';
  200. echo ' Year: <select name="year">
  201. <option value="2011">2011</option>
  202. <option value="2010">2010</option>
  203. <option value="2009">2009</option>
  204. <option value="2008">2008</option>
  205. <option value="2007">2007</option>
  206. </select><br /> (Today is ';
  207. echo date('l\, F jS, Y');
  208. echo ')</p>';
  209. echo '<p>Lunch? <select name="lunch">
  210. <option value="00:00:00">None</option>
  211. <option value="00:15:00">15 Minutes</option>
  212. <option value="00:30:00">30 Minutes</option>
  213. <option value="00:45:00">45 Minutes</option>
  214. <option value="01:00:00">1 Hour</option>
  215. <option value="01:15:00">1 Hour, 15 Minutes</option>
  216. <option value="01:30:00">1 Hour, 30 Minutes</option>
  217. <option value="01:45:00">1 Hour, 45 Minutes</option>
  218. <option value="02:00:00">2 Hours</option>
  219. </select></p>';
  220. // echo "<p>Please use enter times in (HH:MM) format. For example 8:45, 12:30, etc.</p>";
  221. echo "<table><tr><th>Time In</th><th>Time Out</th><th>Area Worked</th><th>Sub?</th></tr>\n";
  222. for ($i = 1; $i <= $max; $i++) {
  223. $query = "SELECT * FROM is4c_log.shifts ORDER BY ShiftName ASC";
  224. $result = mysql_query($query);
  225. echo '<tr>
  226. <th><input type="text" name="in' . $i . '" size="5" maxlength="5">
  227. <select name="inmeridian' . $i . '">
  228. <option value="AM">AM</option>
  229. <option value="PM">PM</option>
  230. </select>
  231. </th>
  232. <th><input type="text" name="out' . $i . '" size="5" maxlength="5">
  233. <select name="outmeridian' . $i . '">
  234. <option value="AM">AM</option>
  235. <option value="PM" SELECTED>PM</option>
  236. </select>
  237. </th>
  238. <th><select name="area' . $i . '" id="area' . $i . '">
  239. <option>Please select an area of work.</option>';
  240. while ($row = mysql_fetch_row($result)) {
  241. echo "<option id =\"$i$row[1]\" value=\"$row[1]\">$row[0]</option>";
  242. }
  243. echo '</select></th>' . "\n";
  244. echo '<th><input type="checkbox" name="sub' . $i . '" /></th></tr>';
  245. }
  246. echo '</table>
  247. <button name="submit" type="submit">Submit</button>
  248. <input type="hidden" name="submitted" value="TRUE" />
  249. </form>';
  250. }
  251. include('../src/footer.php');
  252. ?>