diff --git a/lang/en_us.php b/lang/en_us.php index 1ae5a87..69b4e33 100644 --- a/lang/en_us.php +++ b/lang/en_us.php @@ -114,4 +114,7 @@ define("STRINGS", [ "punch saved" => "Punch saved.", "invalid datetime" => "Could not understand the dates/times given. Please use a standard format, such as \"January 1, 2018 3:30pm\".", "punch deleted" => "Punch deleted.", + "hours" => "Hours", + "hours:minutes" => "H:MM", + "totals" => "Totals" ]); \ No newline at end of file diff --git a/lib/reports.php b/lib/reports.php index a2f531c..232de08 100644 --- a/lib/reports.php +++ b/lib/reports.php @@ -167,6 +167,75 @@ function getPunchReport($user = null, $start = null, $end = null) { return $out; } +function getTotalsReport($user = null, $start = null, $end = null) { + global $database; + global $allowed_users; + $where = []; + if ((bool) strtotime($start) == TRUE) { + $where["OR #start"] = [ + "in[>=]" => date("Y-m-d", strtotime($start)), + "out[>=]" => date("Y-m-d", strtotime($start)) + ]; + } + if ((bool) strtotime($end) == TRUE) { + // Make the date be the end of the day, not the start + $where["in[<=]"] = date("Y-m-d", strtotime($end)) . " 23:59:59"; + } + if ($user != null && array_key_exists('uid', $user) && ($allowed_users === true || in_array($user['uid'], $allowed_users))) { + $where["uid"] = $user['uid']; + } else if ($user != null && array_key_exists('uid', $user) && $allowed_users !== true && !in_array($user['uid'], $allowed_users)) { + $where["uid"] = -1; + } else { + if ($allowed_users !== true) { + $where["uid"] = $allowed_users; + } + } + if (count($where) > 1) { + $where = ["AND" => $where]; + } + $punches = $database->select( + "punches", [ + "punchid", "uid", "in", "out" + ], $where + ); + $header = [lang("name", false), lang("punches", false), lang("hours:minutes", false), lang("hours", false)]; + $out = [$header]; + $usercache = []; + $totalseconds = []; + $totalpunches = []; + for ($i = 0; $i < count($punches); $i++) { + if (!array_key_exists($punches[$i]["uid"], $usercache)) { + $usercache[$punches[$i]["uid"]] = getUserByID($punches[$i]["uid"]); + } + if (!array_key_exists($punches[$i]["uid"], $totalseconds)) { + $totalseconds[$punches[$i]["uid"]] = 0.0; + $totalpunches[$punches[$i]["uid"]] = 0; + } + $insec = strtotime($punches[$i]["in"]); + if (is_null($punches[$i]["out"])) { + $outsec = time(); + } else { + $outsec = strtotime($punches[$i]["out"]); + } + $totalseconds[$punches[$i]["uid"]] += $outsec - $insec; + $totalpunches[$punches[$i]["uid"]] += 1; + } + + foreach ($totalseconds as $uid => $sec) { + if (!array_key_exists($uid, $usercache)) { + $usercache[$uid] = getUserByID($uid); + } + $hhmm = floor($sec / 3600) . ":" . str_pad(floor(($sec / 60) % 60), 2, "0", STR_PAD_LEFT); + $out[] = [ + $usercache[$uid]["name"] . " (" . $usercache[$uid]["username"] . ")", + $totalpunches[$uid] . "", + $hhmm, + round($sec / 60.0 / 60.0, 4) . "" + ]; + } + return $out; +} + function getReportData($type, $user = null, $start = null, $end = null) { switch ($type) { case "shifts": @@ -175,6 +244,9 @@ function getReportData($type, $user = null, $start = null, $end = null) { case "punches": return getPunchReport($user, $start, $end); break; + case "totals": + return getTotalsReport($user, $start, $end); + break; default: return [["error"]]; } diff --git a/pages/export.php b/pages/export.php index 75cc6dc..8b55fd1 100644 --- a/pages/export.php +++ b/pages/export.php @@ -22,6 +22,7 @@ if (!account_has_permission($_SESSION['username'], "QWIKCLOCK_MANAGE") && !accou