blob: f004286e6544e7da7b635323eec4de3162aa5539 [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001<?php
Adrià Vilanova Martínez5af86512023-12-02 20:44:16 +01002/*
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 botbe50d492023-11-30 00:16:42 +010021class 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
146 if ((!$internal && !in_array($sstatus, self::$affiliationStatusesManual)) || ($internal && !in_array($affiliationStatuses))) return false;
147
148 if (!workers::exists($sid)) return false;
149
150 return mysqli_query($con, "INSERT INTO workhistory (worker, day, status) VALUES ($sid, $stime, $sstatus)");
151 }
152
153 public static function editWorkHistoryItem($id, $day, $status, $internal = false) {
154 global $con;
155
156 $sid = (int)$id;
157 $stime = (int)$day;
158 $sstatus = (int)$status;
159
160 if ((!$internal && !in_array($sstatus, self::$affiliationStatusesManual)) || ($internal && !in_array($affiliationStatuses))) return false;
161
162 if (!self::existsWorkHistoryItem($id)) return false;
163
164 return mysqli_query($con, "UPDATE workhistory SET day = $stime, status = $sstatus WHERE id = $sid LIMIT 1");
165 }
166
167 public static function deleteWorkHistoryItem($id) {
168 global $con;
169
170 $sid = (int)$id;
171
172 return mysqli_query($con, "DELETE FROM workhistory WHERE id = $sid LIMIT 1");
173 }
174
175 public static function exists($id) {
176 global $con;
177
178 $query = mysqli_query($con, "SELECT id FROM workers WHERE id = ".(int)$id);
179
180 return (mysqli_num_rows($query) > 0);
181 }
182
183 public static function existsWorkHistoryItem($id) {
184 global $con;
185
186 $query = mysqli_query($con, "SELECT 1 FROM workhistory WHERE id = ".(int)$id);
187
188 return (mysqli_num_rows($query) > 0);
189 }
190}