-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent-script.js
278 lines (257 loc) · 8.62 KB
/
content-script.js
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
report id:
1- hours report
2- general day off - add only by admin
3- holiday
4- half holyday
5- resurve duty
6- sickday
*/
const momentHeb = moment().local('he');
const tokenObj = localStorage['TOKEN'];
const token = tokenObj && JSON.parse(tokenObj).access_token;
const beginOfMonth = moment().startOf('month').format('YYYY-MM-DD');
const endOfMonth = moment().endOf('month').format('YYYY-MM-DD');
const monthDisplay = momentHeb.format('MMMM');
const beginOfQuarter = moment().startOf('quarter').format('YYYY-MM-DD');
const endOfQuarter = moment().endOf('quarter').format('YYYY-MM-DD');
const quarterDisplay = momentHeb.format('Q');
const fullDayHours = 8.8,
halfDayHours = fullDayHours / 2;
const refreshImgSrc = chrome.runtime.getURL('images/refresh.png');
let loadingMonth = false;
let loadingQuarter = false;
function hourFormat(time) {
const dur = moment.duration(time, 'hours');
const hours = Math.floor(dur.asHours());
const mins = Math.floor(dur.asMinutes()) - hours * 60;
return hours + ':' + (mins < 10 ? '0' + mins : mins);
}
function fetchData() {
loadingMonth = true;
loadingQuarter = true;
//month call
token &&
$.ajax({
type: 'GET',
url: `https://reportapi.codevalue.net/api/calendardays?fromDate=${beginOfMonth}&toDate=${endOfMonth}&includeEmptyDays=true&includeReports=true`,
headers: {
Authorization: 'Bearer ' + token,
},
dataType: 'json',
success: function (result, status, xhr) {
// console.log(result);
loadingMonth = false;
calculateMonthlyHours(result);
},
error: function (xhr, status, error) {
loadingMonth = false;
console.error('CHV EXTENSION - ERROR GET DATA - Massage:', error);
},
});
//quarter call
token &&
$.ajax({
type: 'GET',
url: `https://reportapi.codevalue.net/api/calendardays?fromDate=${beginOfQuarter}&toDate=${endOfQuarter}&includeEmptyDays=true&includeReports=true`,
headers: {
Authorization: 'Bearer ' + token,
},
dataType: 'json',
success: function (result, status, xhr) {
// console.log(result);
loadingQuarter = false;
calculateQuarterHours(result);
},
error: function (xhr, status, error) {
loadingQuarter = false;
console.error('CHV EXTENSION - ERROR GET DATA - Massage:', error);
},
});
}
function calculateMonthlyHours(data) {
let totalRequireHours = 0,
requireHours = 0,
currentHours = 0,
meanHours = 0,
currentWorkingDays = 0,
jobPercent = 0,
totalWorkingDays = 0;
data.forEach(day => {
// console.log(el);
day.hourReports
.filter(r => !!r.startTime && !!r.endTime)
.forEach(rep => {
const hours = moment(rep.endTime).diff(
// calculate how many hours for this day
moment(rep.startTime),
'hours',
true
);
currentHours += hours;
// if the user was on day off
if (
rep.reportTypeId === 3 ||
rep.reportTypeId === 5 ||
rep.reportTypeId === 6
) {
currentHours += fullDayHours;
}
if (rep.reportTypeId === 4) {
currentHours += halfDayHours;
}
if (rep.reportTypeId === 2) {
if (day.holidayType === 'NoHoliday') {
currentHours += fullDayHours;
}
if (day.holidayType === 'HalfHoliday') {
currentHours += halfDayHours;
}
}
});
if (day.hourReports.length && !moment(day.date).isSame(new Date(), 'day')) {
// !moment(day.date).isSame(new Date(), "day") = not today
currentWorkingDays++;
}
if (day.isRequireReport) {
totalWorkingDays++;
}
if (day.holidayType === 'NoHoliday') {
totalRequireHours += fullDayHours;
if (moment(day.date).isBefore(new Date(), 'day')) {
requireHours += fullDayHours;
}
}
if (day.holidayType === 'HalfHoliday') {
totalRequireHours += halfDayHours;
if (moment(day.date).isBefore(new Date(), 'day')) {
requireHours += halfDayHours;
}
}
});
meanHours =
(totalRequireHours - currentHours) /
(totalWorkingDays - currentWorkingDays);
jobPercent = requireHours === 0 ? 100 : (currentHours / requireHours) * 100; // if first day of the month
// console.log('todayRequireHours: ', requireHours);
// console.log('totalRequireHours: ', totalRequireHours);
// console.log('currentHours: ', currentHours);
$('.cvh-current-hours').text(hourFormat(currentHours));
$('.cvh-mean-hours').text(hourFormat(meanHours));
$('.cvh-total-hours').text(hourFormat(totalRequireHours));
// $('.cvh-current-hours').text(currentHours.toFixed(2));
// $('.cvh-mean-hours').text(meanHours.toFixed(2));
// $('.cvh-total-hours').text(totalRequireHours.toFixed(2));
$('.cvh-monthly-job-percent').text(jobPercent.toFixed(0) + '%');
$('.cvh-display-month').text(monthDisplay + ':');
}
function calculateQuarterHours(month) {
let totalRequireHours = 0,
requireHours = 0,
bonus = 540,
bonusHours = 0,
currentHours = 0;
(meanHours = 0), (currentWorkingDays = 0);
totalWorkingDays = 0;
month.forEach(day => {
// console.log(el);
day.hourReports
.filter(r => !!r.startTime && !!r.endTime)
.forEach(rep => {
const hours = moment(rep.endTime).diff(
moment(rep.startTime),
'hours',
true
);
currentHours += hours;
});
if (day.hourReports.length && !moment(day.date).isSame(new Date(), 'day')) {
// !moment(day.date).isSame(new Date(), "day") = not today
currentWorkingDays++;
}
if (day.isRequireReport) {
totalWorkingDays++;
}
if (day.holidayType === 'NoHoliday') {
totalRequireHours += fullDayHours;
if (moment(day.date).isBefore(new Date(), 'day')) {
requireHours += fullDayHours;
}
}
if (day.holidayType === 'HalfHoliday') {
totalRequireHours += halfDayHours;
if (moment(day.date).isBefore(new Date(), 'day')) {
requireHours += halfDayHours;
}
}
});
bonusHours = Math.min(bonus, totalRequireHours);
meanHours =
(bonusHours - currentHours) / (totalWorkingDays - currentWorkingDays);
jobPercent = requireHours === 0 ? 100 : (currentHours / requireHours) * 100; // if first day of the month
$('.cvh-quarter-total-hours').text(hourFormat(bonusHours));
$('.cvh-quarter-current-hours').text(hourFormat(currentHours));
$('.cvh-quarter-mean-hours').text(hourFormat(meanHours));
$('.cvh-display-quarter').text('רבעון ' + quarterDisplay + ':');
// $('.cvh-quarter-total-hours').text(bonusHours.toFixed(2));
// $('.cvh-quarter-current-hours').text(currentHours.toFixed(2));
// $('.cvh-quarter-mean-hours').text(meanHours.toFixed(2));
// // $('.cvh-quarter-job-percent').text(jobPercent.toFixed(2));
}
const handleRefresh = () => {
console.log('[CVH] refresh');
fetchData();
};
$('.full-height.desktop-main')
.prepend(
$(/*html*/ `
<div class="cvh-wrap">
<div class="cvh-wrap month-bg">
<div class="chv-cell">
<div class="cvh-title cvh-display-month"></div>
</div>
<div class="chv-cell">
<div class="cvh-title">דרישה חודשית: </div>
<div class="cvh-total-hours"></div>
</div>
<div class="chv-cell">
<div class="cvh-title">שעות שנעשו החודש: </div>
<div class="cvh-current-hours"></div>
</div>
<div class="chv-cell">
<div class="cvh-title">ממוצע יומי חודשי דרוש: </div>
<div class="cvh-mean-hours"></div>
</div>
<div class="chv-cell">
<div class="cvh-title">אחוז משרה כרגע: </div>
<div class="cvh-monthly-job-percent"></div>
</div>
</div>
<div class="cvh-wrap quarter-bg">
<div class="chv-cell">
<div class="cvh-title cvh-display-quarter"></div>
</div>
<div class="chv-cell">
<div class="cvh-title">דרישה רבעונית: </div>
<div class="cvh-quarter-total-hours"></div>
</div>
<div class="chv-cell">
<div class="cvh-title">שעות שנעשו הרבעון: </div>
<div class="cvh-quarter-current-hours"></div>
</div>
<div class="chv-cell">
<div class="cvh-title">ממוצע יומי רבעוני דרוש: </div>
<div class="cvh-quarter-mean-hours"></div>
</div>
</div>
<div class="cvh-refresh">
<img class="cvh-refresh-image" src="${refreshImgSrc}" alt="refresh" title="refresh">
</div>
</div>`)
)
.ready(function () {
$('.cvh-refresh-image').click(function () {
handleRefresh();
});
});
fetchData();