-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
185 lines (154 loc) · 6.2 KB
/
test.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
const xlsx = require('xlsx');
const fs = require('fs');
const path = require('path');
// Function to extract data from the 'logs' sheet of the Excel file
async function extractLogsSheet(filePath, settings) {
// Read the Excel file
const workbook = xlsx.readFile(filePath);
console.log(settings)
// Check if the 'logs' sheet exists
const sheetName = 'Logs';
if (!workbook.SheetNames.includes(sheetName)) {
return;
}
// Get the worksheet
const worksheet = workbook.Sheets[sheetName];
fs.unlinkSync(filePath);
return extractData(worksheet, settings)
}
function extractData(worksheet, settings){
// Extract the period date from cell C3
const period = worksheet['C3'] ? worksheet['C3'].v : '';
// Initialize data structure
const logsData = {
period: period,
users: []
};
// Iterate over the worksheet to extract user data
const range = xlsx.utils.decode_range(worksheet['!ref']);
let currentUser = null;
const rowData = [];
for (let C = range.s.c; C <= range.e.c; C++) {
const cellAddress = xlsx.utils.encode_cell({ r: 4 - 1, c: C }); // Subtract 1 for 0-based index
const cell = worksheet[cellAddress];
// Add cell value to rowData array if it exists
rowData.push(cell ? cell.v : null);
}
for (let R = range.s.r; R <= range.e.r; R++) {
// Read user information
const noCell = worksheet[xlsx.utils.encode_cell({ r: R, c: 0 })];
const nameCell = worksheet[xlsx.utils.encode_cell({ r: R, c: 10 })];
const deptCell = worksheet[xlsx.utils.encode_cell({ r: R, c: 20 })];
if (noCell && noCell.v === 'No :') {
// If we encounter 'No:', it indicates a new user section
const year = period.substring(0, 4);
const month = period.substring(5, 7);
// Create a new user object
let userTotalmin = 0
currentUser = {
number: worksheet[xlsx.utils.encode_cell({ r: R, c: 2 })].v,
name: nameCell ? nameCell.v : '',
department: deptCell ? deptCell.v : '',
totalMinutes: 0,
logs: {}
};
for (let C = 0; C <= range.e.c; C++) {
let accountingSetting = settings.find(setting => setting.department == 'Others');
if(currentUser && currentUser.department == "Accounting"){
accountingSetting = settings.find(setting => setting.department == 'Accounting');
}
let day = worksheet[xlsx.utils.encode_cell({ r: R-1, c: C })]?.v;
const date = `${year}/${month}/${day}`
const newdate = new Date(date);
const dayName = new Intl.DateTimeFormat('en-US', { weekday: 'long' }).format(newdate);
day = dayName + "-" + date
if (day) {
let times = worksheet[xlsx.utils.encode_cell({ r: R + 1, c: C })]?.v || '';
let start = times.split('\n')[0] ?? "";
let time = times.split('\n');
const len = time.length
let end = time[1] ?? ""
let penalty = 0
let overTime = 0
let total = 0
let min = 0
if (start !== "" && end !== "") {
// Both sign-in and sign-out are present
min = calculateTotalMinutes(start, end)
let maxTime = calculateTotalMinutes(accountingSetting.workScheduleStartTime, accountingSetting.workScheduleEndTime)
total = min
if (min > maxTime && accountingSetting.noOvertime) {
overTime = calculateTotalMinutes(accountingSetting.workScheduleEndTime, end)
total = maxTime
}
} else if (start === "" && end === "") {
// Both sign-in and sign-out are missing
total = 0
penalty = 0
} else {
// Only one of sign-in or sign-out is missing
if (start === "") {
penalty = accountingSetting.noSignInPenalty;
total = calculateTotalMinutes(accountingSetting.workScheduleStartTime, end);
} else if (end === "") {
penalty = accountingSetting.noSignOutPenalty;
let effectiveStart = isTimeGreater(start, accountingSetting.workScheduleStartTime) ? start : accountingSetting.workScheduleStartTime;
total = calculateTotalMinutes(effectiveStart, accountingSetting.workScheduleEndTime);
}
// Deduct penalty from total minutes worked
total = Math.max(0, total - penalty);
}
userTotalmin = userTotalmin + total
currentUser.logs[day] = {
'start': start,
'end': end,
'min': min,
'totalMin': total,
'overTime': overTime < 0 ? 0 : overTime,
'penaltyMin': penalty
}
}
}
currentUser.totalMinutes = userTotalmin
logsData.users.push(currentUser);
// Skip to the next row
R += 1;
}
}
// Push the last user data if present
if (currentUser) {
logsData.users.push(currentUser);
}
return logsData;
}
// Path to the uploaded Excel file
const excelFilePath = path.resolve(__dirname, '001_2024_7_MON.xls');
const outputJsonPath = path.resolve(__dirname, 'line4Data.json');
function writeDataToJsonFile(data, outputPath) {
fs.writeFileSync(outputPath, JSON.stringify(data, null, 2), 'utf-8');
}
function timeStringToDate(timeString) {
const [hours, minutes] = timeString.split(':').map(Number);
const date = new Date();
date.setHours(hours, minutes, 0, 0); // Set hours and minutes
return date;
}
// Function to compare two time strings
function isTimeGreater(time1, time2) {
const date1 = timeStringToDate(time1);
const date2 = timeStringToDate(time2);
return date1 > date2;
}
function calculateTotalMinutes(startTime, endTime) {
// Split start time into hours and minutes
const [startHours, startMinutes] = startTime.split(':').map(Number);
// Split end time into hours and minutes
const [endHours, endMinutes] = endTime.split(':').map(Number);
// Convert both times to total minutes since midnight
const startTotalMinutes = startHours * 60 + startMinutes;
const endTotalMinutes = endHours * 60 + endMinutes;
// Calculate the difference in minutes
const totalMinutes = endTotalMinutes - startTotalMinutes;
return isNaN(totalMinutes) ? 0 : totalMinutes;
}
module.exports = { extractLogsSheet, writeDataToJsonFile };