Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 1 | <?php |
Adrià Vilanova Martínez | 5af8651 | 2023-12-02 20:44:16 +0100 | [diff] [blame] | 2 | /* |
| 3 | * hores |
| 4 | * Copyright (c) 2023 Adrià Vilanova Martínez |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as |
| 8 | * published by the Free Software Foundation, either version 3 of the |
| 9 | * License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public |
| 17 | * License along with this program. |
| 18 | * If not, see http://www.gnu.org/licenses/. |
| 19 | */ |
| 20 | |
Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 21 | class workers { |
| 22 | const AFFILIATION_STATUS_NOTWORKING = 0; |
| 23 | const AFFILIATION_STATUS_WORKING = 1; |
| 24 | const AFFILIATION_STATUS_AUTO_NOTWORKING = 2; |
| 25 | const AFFILIATION_STATUS_AUTO_WORKING = 3; |
| 26 | |
| 27 | public static $affiliationStatuses = [self::AFFILIATION_STATUS_NOTWORKING, self::AFFILIATION_STATUS_WORKING, self::AFFILIATION_STATUS_AUTO_NOTWORKING, self::AFFILIATION_STATUS_AUTO_WORKING]; |
| 28 | public static $affiliationStatusesNotWorking = [self::AFFILIATION_STATUS_NOTWORKING, self::AFFILIATION_STATUS_AUTO_NOTWORKING]; |
| 29 | public static $affiliationStatusesWorking = [self::AFFILIATION_STATUS_WORKING, self::AFFILIATION_STATUS_AUTO_WORKING]; |
| 30 | public static $affiliationStatusesAutomatic = [self::AFFILIATION_STATUS_AUTO_WORKING, self::AFFILIATION_STATUS_AUTO_NOTWORKING]; |
| 31 | public static $affiliationStatusesManual = [self::AFFILIATION_STATUS_WORKING, self::AFFILIATION_STATUS_NOTWORKING]; |
| 32 | |
| 33 | private static $return = "w.id id, w.person person, w.company company, c.name companyname, p.name name, p.dni dni"; |
| 34 | |
| 35 | public static function get($id) { |
| 36 | global $con; |
| 37 | |
| 38 | $sid = (int)$id; |
| 39 | |
| 40 | $query = mysqli_query($con, "SELECT ".self::$return." |
| 41 | FROM workers w |
| 42 | LEFT JOIN companies c ON w.company = c.id |
| 43 | LEFT JOIN people p ON w.person = p.id |
| 44 | WHERE w.id = $sid"); |
| 45 | |
| 46 | if (!mysqli_num_rows($query)) return false; |
| 47 | |
| 48 | return mysqli_fetch_assoc($query); |
| 49 | } |
| 50 | |
| 51 | public static function sqlAddonToGetStatusAttribute($id) { |
| 52 | $sid = (int)$id; |
| 53 | |
| 54 | return "LEFT JOIN workhistory h ON w.id = h.worker |
| 55 | WHERE |
| 56 | w.person = $sid AND |
| 57 | ( |
| 58 | ( |
| 59 | SELECT COUNT(*) |
| 60 | FROM workhistory |
| 61 | WHERE worker = w.id AND day <= UNIX_TIMESTAMP() |
| 62 | LIMIT 1 |
| 63 | ) = 0 OR |
| 64 | |
| 65 | h.id = ( |
| 66 | SELECT id |
| 67 | FROM workhistory |
| 68 | WHERE worker = w.id AND day <= UNIX_TIMESTAMP() |
| 69 | ORDER BY day DESC |
| 70 | LIMIT 1 |
| 71 | ) |
| 72 | )"; |
| 73 | } |
| 74 | |
| 75 | public static function getPersonWorkers($person, $simplify = false) { |
| 76 | global $con; |
| 77 | |
| 78 | $query = mysqli_query($con, "SELECT ".($simplify ? "w.id id" : self::$return).", h.status status, h.day lastupdated |
| 79 | FROM workers w |
| 80 | LEFT JOIN companies c ON w.company = c.id |
| 81 | LEFT JOIN people p ON w.person = p.id |
| 82 | ".self::sqlAddonToGetStatusAttribute($person)." |
| 83 | ORDER BY |
| 84 | w.id ASC"); |
| 85 | |
| 86 | $results = []; |
| 87 | |
| 88 | while ($row = mysqli_fetch_assoc($query)) { |
| 89 | $row["hidden"] = self::isHidden($row["status"]); |
| 90 | $results[] = ($simplify ? $row["id"] : $row); |
| 91 | } |
| 92 | |
| 93 | return $results; |
| 94 | } |
| 95 | |
| 96 | public static function isHidden($status) { |
| 97 | return (in_array($status, self::$affiliationStatusesWorking) ? 0 : 1); |
| 98 | } |
| 99 | |
| 100 | public static function affiliationStatusHelper($status) { |
| 101 | return (self::isHidden($status) ? "Baja" : "Alta"); |
| 102 | } |
| 103 | |
| 104 | public static function affiliationStatusIcon($status) { |
| 105 | return (self::isHidden($status) ? "work_off" : "work"); |
| 106 | } |
| 107 | |
| 108 | public static function isAutomaticAffiliation($status) { |
| 109 | return in_array($status, self::$affiliationStatusesAutomatic); |
| 110 | } |
| 111 | |
| 112 | public static function getWorkHistory($id) { |
| 113 | global $con; |
| 114 | |
| 115 | $sid = (int)$id; |
| 116 | |
| 117 | $query = mysqli_query($con, "SELECT * FROM workhistory WHERE worker = $sid ORDER BY day DESC"); |
| 118 | if ($query === false) return false; |
| 119 | |
| 120 | $items = []; |
| 121 | while ($row = mysqli_fetch_assoc($query)) { |
| 122 | $items[] = $row; |
| 123 | } |
| 124 | |
| 125 | return $items; |
| 126 | } |
| 127 | |
| 128 | public static function getWorkHistoryItem($id) { |
| 129 | global $con; |
| 130 | |
| 131 | $sid = (int)$id; |
| 132 | |
| 133 | $query = mysqli_query($con, "SELECT * FROM workhistory WHERE id = $sid"); |
| 134 | if ($query === false || !mysqli_num_rows($query)) return false; |
| 135 | |
| 136 | return mysqli_fetch_assoc($query); |
| 137 | } |
| 138 | |
| 139 | public static function addWorkHistoryItem($id, $day, $status, $internal = false) { |
| 140 | global $con; |
| 141 | |
| 142 | $sid = (int)$id; |
| 143 | $stime = (int)$day; |
| 144 | $sstatus = (int)$status; |
| 145 | |
Adrià Vilanova Martínez | 9dbe500 | 2023-12-04 00:06:09 +0100 | [diff] [blame] | 146 | if (!$internal && !in_array($sstatus, self::$affiliationStatusesManual)) return false; |
| 147 | if ($internal && !in_array($sstatus, self::$affiliationStatuses)) return false; |
Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 148 | |
| 149 | if (!workers::exists($sid)) return false; |
| 150 | |
| 151 | return mysqli_query($con, "INSERT INTO workhistory (worker, day, status) VALUES ($sid, $stime, $sstatus)"); |
| 152 | } |
| 153 | |
| 154 | public static function editWorkHistoryItem($id, $day, $status, $internal = false) { |
| 155 | global $con; |
| 156 | |
| 157 | $sid = (int)$id; |
| 158 | $stime = (int)$day; |
| 159 | $sstatus = (int)$status; |
| 160 | |
Adrià Vilanova Martínez | 9dbe500 | 2023-12-04 00:06:09 +0100 | [diff] [blame] | 161 | if (!$internal && !in_array($sstatus, self::$affiliationStatusesManual)) return false; |
| 162 | if ($internal && !in_array($sstatus, self::$affiliationStatuses)) return false; |
Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 163 | |
| 164 | if (!self::existsWorkHistoryItem($id)) return false; |
| 165 | |
| 166 | return mysqli_query($con, "UPDATE workhistory SET day = $stime, status = $sstatus WHERE id = $sid LIMIT 1"); |
| 167 | } |
| 168 | |
| 169 | public static function deleteWorkHistoryItem($id) { |
| 170 | global $con; |
| 171 | |
| 172 | $sid = (int)$id; |
| 173 | |
| 174 | return mysqli_query($con, "DELETE FROM workhistory WHERE id = $sid LIMIT 1"); |
| 175 | } |
| 176 | |
| 177 | public static function exists($id) { |
| 178 | global $con; |
| 179 | |
| 180 | $query = mysqli_query($con, "SELECT id FROM workers WHERE id = ".(int)$id); |
| 181 | |
| 182 | return (mysqli_num_rows($query) > 0); |
| 183 | } |
| 184 | |
| 185 | public static function existsWorkHistoryItem($id) { |
| 186 | global $con; |
| 187 | |
| 188 | $query = mysqli_query($con, "SELECT 1 FROM workhistory WHERE id = ".(int)$id); |
| 189 | |
| 190 | return (mysqli_num_rows($query) > 0); |
| 191 | } |
| 192 | } |