-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflexhours.php
executable file
·180 lines (152 loc) · 4.27 KB
/
flexhours.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/local/opt/php56/bin/php
<?php
// date() will nag without this.
date_default_timezone_set('Europe/Helsinki');
// Exit function.
register_shutdown_function(function() {
// Always print one empty line at the end.
echo PHP_EOL;
});
// Defaults.
$credentials = NULL;
$hours_per_day = 7.5;
$treshold = 2/60; // 2 mins.
$start_string = 'first day of January';
$end_string = 'today';
// Load config file.
include dirname(__FILE__) . '/config.php';
// Nowhere to go without credentials.
if (!$credentials) {
echo "Credentials missing. You did check the configuration in config.php didn't you?" . PHP_EOL;
exit();
}
/**
* Get arguments.
*
* -s "date dtring", --start="date string": Range start.
* -e "date string", --end="date string": Range end.
* -h, --help: Show help.
*/
$opts = getopt('s:e:h', array('start:', 'end:', 'help'));
// Start date.
if (!empty($opts['s']) || !empty($opts['start'])) {
$start_string = !empty($opts['s']) ? $opts['s'] : $opts['start'];
}
// End date.
if (!empty($opts['e']) || !empty($opts['end'])) {
$end_string = !empty($opts['e']) ? $opts['e'] : $opts['end'];
}
// Help.
if (isset($opts['h']) || isset($opts['help'])) {
showHelp();
exit;
}
// Notify range.
echo "Checking flex hours from $start_string to $end_string." . PHP_EOL;
// Headers.
$headers = array(
"Content-type: application/json",
"Accept: application/json",
"Authorization: Basic " . base64_encode($credentials),
);
// Fetch profile info.
$url = "https://mearra.harvestapp.com/account/who_am_i";
$data = doCurl($url);
if (!empty($data->user->id)) {
$user_id = $data->user->id;
// Range.
$start = new DateTime($start_string);
$end = new DateTime($end_string);
// Fetch hours.
$url = "https://mearra.harvestapp.com/people/" . $user_id . "/entries?from=" . $start->format('Ymd') . "&to=" . $end->format('Ymd');
$data = doCurl($url);
$tracked = 0;
foreach ($data as $record) {
if ($record->day_entry->hours > $treshold) {
$tracked += $record->day_entry->hours;
}
}
// Calculate week hours so far.
$calculated = 0;
$oneday = new DateInterval("P1D");
foreach(new DatePeriod($start, $oneday, $end->add($oneday)) as $day) {
$day_num = $day->format("N"); /* 'N' number days 1 (mon) to 7 (sun) */
if($day_num < 6) { /* weekday */
$calculated += $hours_per_day;
}
}
$flex = $tracked - $calculated;
echo "Flex hour status: " . convertTime($flex) . PHP_EOL;
}
else {
echo "Error: " . (!empty($data->message) ? $data->message : 'No idea why..');
}
/**
* Helper function for curl calls.
*/
function doCurl($url) {
global $headers;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
if (curl_errno($ch)) {
echo "Curl error: " . curl_error($ch) . PHP_EOL;
}
else if ($data_var = json_decode($data)) {
return $data_var;
}
else {
print "Error: Empty or invalid data received!" . PHP_EOL;
}
curl_close($ch);
}
/**
* Helper function to convert decimal hours into hours and minutes.
*/
function convertTime($dec)
{
// Make this a positive number for hour and minute separation.
if ($negative = ($dec < 0)) {
$dec = -1 * $dec;
}
$hour = floor($dec);
$min = round(60*($dec - $hour));
// Return negativity.
if ($negative) {
$hour = -1 * $hour;
}
// Add a '+'-sign if it's a positive.
if ($hour > 0) {
$hour = '+' . $hour;
}
// Make sure we always have two digits for minutes.
if ($min < 10) {
$min = '0' . $min;
}
// Spit it out.
return $hour . ':' . $min;
}
/**
* Helper function to show help.
*/
function showHelp() {
// Help triggers.
$triggers = array(
'-s <date string>, --start <date string>' => 'Start date string',
'-e <date string>, --end <date string>' => 'End date string',
'-h, --help' => 'Show this help',
);
// First column width.
$lengths = array_map('strlen', array_keys($triggers));
$col_width = max($lengths) + 4;
echo "Usage: ./flexhours.php [options]" . PHP_EOL;
echo "Options:" . PHP_EOL;
foreach ($triggers as $trigger => $info) {
echo " " . str_pad($trigger, $col_width, " ") . $info . PHP_EOL;
}
}
?>