blob: 6c6e1d9f74b4cee5069c7ec157b4a09e0eaa1ecb [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001<?php
2class workers {
3 const AFFILIATION_STATUS_NOTWORKING = 0;
4 const AFFILIATION_STATUS_WORKING = 1;
5 const AFFILIATION_STATUS_AUTO_NOTWORKING = 2;
6 const AFFILIATION_STATUS_AUTO_WORKING = 3;
7
8 public static $affiliationStatuses = [self::AFFILIATION_STATUS_NOTWORKING, self::AFFILIATION_STATUS_WORKING, self::AFFILIATION_STATUS_AUTO_NOTWORKING, self::AFFILIATION_STATUS_AUTO_WORKING];
9 public static $affiliationStatusesNotWorking = [self::AFFILIATION_STATUS_NOTWORKING, self::AFFILIATION_STATUS_AUTO_NOTWORKING];
10 public static $affiliationStatusesWorking = [self::AFFILIATION_STATUS_WORKING, self::AFFILIATION_STATUS_AUTO_WORKING];
11 public static $affiliationStatusesAutomatic = [self::AFFILIATION_STATUS_AUTO_WORKING, self::AFFILIATION_STATUS_AUTO_NOTWORKING];
12 public static $affiliationStatusesManual = [self::AFFILIATION_STATUS_WORKING, self::AFFILIATION_STATUS_NOTWORKING];
13
14 private static $return = "w.id id, w.person person, w.company company, c.name companyname, p.name name, p.dni dni";
15
16 public static function get($id) {
17 global $con;
18
19 $sid = (int)$id;
20
21 $query = mysqli_query($con, "SELECT ".self::$return."
22 FROM workers w
23 LEFT JOIN companies c ON w.company = c.id
24 LEFT JOIN people p ON w.person = p.id
25 WHERE w.id = $sid");
26
27 if (!mysqli_num_rows($query)) return false;
28
29 return mysqli_fetch_assoc($query);
30 }
31
32 public static function sqlAddonToGetStatusAttribute($id) {
33 $sid = (int)$id;
34
35 return "LEFT JOIN workhistory h ON w.id = h.worker
36 WHERE
37 w.person = $sid AND
38 (
39 (
40 SELECT COUNT(*)
41 FROM workhistory
42 WHERE worker = w.id AND day <= UNIX_TIMESTAMP()
43 LIMIT 1
44 ) = 0 OR
45
46 h.id = (
47 SELECT id
48 FROM workhistory
49 WHERE worker = w.id AND day <= UNIX_TIMESTAMP()
50 ORDER BY day DESC
51 LIMIT 1
52 )
53 )";
54 }
55
56 public static function getPersonWorkers($person, $simplify = false) {
57 global $con;
58
59 $query = mysqli_query($con, "SELECT ".($simplify ? "w.id id" : self::$return).", h.status status, h.day lastupdated
60 FROM workers w
61 LEFT JOIN companies c ON w.company = c.id
62 LEFT JOIN people p ON w.person = p.id
63 ".self::sqlAddonToGetStatusAttribute($person)."
64 ORDER BY
65 w.id ASC");
66
67 $results = [];
68
69 while ($row = mysqli_fetch_assoc($query)) {
70 $row["hidden"] = self::isHidden($row["status"]);
71 $results[] = ($simplify ? $row["id"] : $row);
72 }
73
74 return $results;
75 }
76
77 public static function isHidden($status) {
78 return (in_array($status, self::$affiliationStatusesWorking) ? 0 : 1);
79 }
80
81 public static function affiliationStatusHelper($status) {
82 return (self::isHidden($status) ? "Baja" : "Alta");
83 }
84
85 public static function affiliationStatusIcon($status) {
86 return (self::isHidden($status) ? "work_off" : "work");
87 }
88
89 public static function isAutomaticAffiliation($status) {
90 return in_array($status, self::$affiliationStatusesAutomatic);
91 }
92
93 public static function getWorkHistory($id) {
94 global $con;
95
96 $sid = (int)$id;
97
98 $query = mysqli_query($con, "SELECT * FROM workhistory WHERE worker = $sid ORDER BY day DESC");
99 if ($query === false) return false;
100
101 $items = [];
102 while ($row = mysqli_fetch_assoc($query)) {
103 $items[] = $row;
104 }
105
106 return $items;
107 }
108
109 public static function getWorkHistoryItem($id) {
110 global $con;
111
112 $sid = (int)$id;
113
114 $query = mysqli_query($con, "SELECT * FROM workhistory WHERE id = $sid");
115 if ($query === false || !mysqli_num_rows($query)) return false;
116
117 return mysqli_fetch_assoc($query);
118 }
119
120 public static function addWorkHistoryItem($id, $day, $status, $internal = false) {
121 global $con;
122
123 $sid = (int)$id;
124 $stime = (int)$day;
125 $sstatus = (int)$status;
126
127 if ((!$internal && !in_array($sstatus, self::$affiliationStatusesManual)) || ($internal && !in_array($affiliationStatuses))) return false;
128
129 if (!workers::exists($sid)) return false;
130
131 return mysqli_query($con, "INSERT INTO workhistory (worker, day, status) VALUES ($sid, $stime, $sstatus)");
132 }
133
134 public static function editWorkHistoryItem($id, $day, $status, $internal = false) {
135 global $con;
136
137 $sid = (int)$id;
138 $stime = (int)$day;
139 $sstatus = (int)$status;
140
141 if ((!$internal && !in_array($sstatus, self::$affiliationStatusesManual)) || ($internal && !in_array($affiliationStatuses))) return false;
142
143 if (!self::existsWorkHistoryItem($id)) return false;
144
145 return mysqli_query($con, "UPDATE workhistory SET day = $stime, status = $sstatus WHERE id = $sid LIMIT 1");
146 }
147
148 public static function deleteWorkHistoryItem($id) {
149 global $con;
150
151 $sid = (int)$id;
152
153 return mysqli_query($con, "DELETE FROM workhistory WHERE id = $sid LIMIT 1");
154 }
155
156 public static function exists($id) {
157 global $con;
158
159 $query = mysqli_query($con, "SELECT id FROM workers WHERE id = ".(int)$id);
160
161 return (mysqli_num_rows($query) > 0);
162 }
163
164 public static function existsWorkHistoryItem($id) {
165 global $con;
166
167 $query = mysqli_query($con, "SELECT 1 FROM workhistory WHERE id = ".(int)$id);
168
169 return (mysqli_num_rows($query) > 0);
170 }
171}