fleetmap-reports 1.0.400 → 1.0.403

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,787 +1,798 @@
1
- const automaticReports = require("./automaticReports")
2
- const {convertMS, getDates, getTranslations, weekDaySelected} = require("./util/utils")
1
+ const automaticReports = require('./automaticReports')
2
+ const { convertMS, getDates, getTranslations, weekDaySelected } = require('./util/utils')
3
3
  const jsPDF = require('jspdf')
4
4
  require('jspdf-autotable')
5
- const drivers = require("./util/driver")
6
- const {headerFromUser, addTable} = require("./util/pdfDocument")
7
- const {getStyle} = require("./reportStyle")
8
- const {getUserPartner} = require("fleetmap-partners")
9
- const traccar = require("./util/traccar")
10
- const {isInsideTimetable, isPartialInsideTimetable, calculateTrip} = require("./util/trips");
11
- const tripHelper = require("./util/trips");
12
- const {devicesToProcess} = require("./util/device");
5
+ const drivers = require('./util/driver')
6
+ const { headerFromUser, addTable } = require('./util/pdfDocument')
7
+ const { getStyle } = require('./reportStyle')
8
+ const { getUserPartner } = require('fleetmap-partners')
9
+ const traccar = require('./util/traccar')
10
+ const { isInsideTimetable, isPartialInsideTimetable, calculateTrip } = require('./util/trips')
11
+ const tripHelper = require('./util/trips')
12
+ const { devicesToProcess } = require('./util/device')
13
13
 
14
14
  const fileName = 'ActivityReport'
15
15
 
16
- async function createActivityReport(from, to, userData, traccarInstance) {
17
- console.log('Create ActivityReport')
18
- const reportData = []
19
-
20
- if(userData.byDriver){
21
- const allData = await createActivityReportByDriver(from, to, userData, traccarInstance)
22
- reportData.push(allData)
23
- }
24
- else if(userData.byGroup){
25
- const allData = await createActivityReportByGroup(from, to, userData, traccarInstance)
26
- reportData.push(...allData)
27
- } else {
28
- const allData = await createActivityReportByDevice(from, to, userData, traccarInstance)
29
- reportData.push(allData)
30
- }
31
-
32
- return reportData
16
+ async function createActivityReport (from, to, userData, traccarInstance) {
17
+ console.log('Create ActivityReport')
18
+ const reportData = []
19
+
20
+ if (userData.byDriver) {
21
+ const allData = await createActivityReportByDriver(from, to, userData, traccarInstance)
22
+ reportData.push(allData)
23
+ } else if (userData.byGroup) {
24
+ const allData = await createActivityReportByGroup(from, to, userData, traccarInstance)
25
+ reportData.push(...allData)
26
+ } else {
27
+ const allData = await createActivityReportByDevice(from, to, userData, traccarInstance)
28
+ reportData.push(allData)
29
+ }
30
+
31
+ return reportData
33
32
  }
34
33
 
35
- async function createActivityReportByGroup(from, to, userData, traccarInstance){
36
- console.log("ByGroup")
37
- const reportData = []
38
- for (const g of userData.groups) {
39
- const devices = userData.devices.filter(d => d.groupId === g.id)
40
- console.log(g.name + ' devices:' + devices.length)
41
- if(devices.length > 0) {
42
- const groupData = {
43
- devices: [],
44
- group: g,
45
- xpert: devices.filter(d => d.attributes.xpert).length > 0,
46
- from: from,
47
- to: to
48
- }
49
-
50
- const response = await traccarInstance.reports.reportsSummaryGet(from, to, null, [g.id])
51
- const summaries = response.data
34
+ async function createActivityReportByGroup (from, to, userData, traccarInstance) {
35
+ console.log('ByGroup')
36
+ const reportData = []
37
+ for (const g of userData.groups) {
38
+ const devices = userData.devices.filter(d => d.groupId === g.id)
39
+ console.log(g.name + ' devices:' + devices.length)
40
+ if (devices.length > 0) {
41
+ const groupData = {
42
+ devices: [],
43
+ group: g,
44
+ xpert: devices.filter(d => d.attributes.xpert).length > 0,
45
+ from,
46
+ to
47
+ }
52
48
 
53
- const responseTrips = await traccarInstance.reports.reportsTripsGet(from, to, null, [g.id])
54
- const trips = responseTrips.data
49
+ const response = await traccarInstance.reports.reportsSummaryGet(from, to, null, [g.id])
50
+ const summaries = response.data
55
51
 
56
- devices.sort((a, b) => (a.name > b.name) ? 1 : -1)
52
+ const responseTrips = await traccarInstance.reports.reportsTripsGet(from, to, null, [g.id])
53
+ const trips = responseTrips.data
57
54
 
58
- if (summaries.length > 0) {
55
+ devices.sort((a, b) => (a.name > b.name) ? 1 : -1)
59
56
 
60
- console.log('Summary:' + summaries.length)
61
- groupData.devices = processDevices(from, to, devices, {summaries: summaries, trips: trips}, userData)
57
+ if (summaries.length > 0) {
58
+ console.log('Summary:' + summaries.length)
59
+ groupData.devices = processDevices(from, to, devices, { summaries, trips }, userData)
62
60
 
63
- reportData.push(groupData)
64
- }
65
- }
61
+ reportData.push(groupData)
62
+ }
66
63
  }
64
+ }
67
65
 
68
- const withoutGroupDevices = await createActivityReportByDevice(from, to, userData, traccarInstance)
69
- reportData.push(withoutGroupDevices)
66
+ const withoutGroupDevices = await createActivityReportByDevice(from, to, userData, traccarInstance)
67
+ reportData.push(withoutGroupDevices)
70
68
 
71
- return reportData
69
+ return reportData
72
70
  }
73
71
 
74
- async function createActivityReportByDevice(from, to, userData, traccarInstance){
75
- const devices = devicesToProcess(userData)
72
+ async function createActivityReportByDevice (from, to, userData, traccarInstance) {
73
+ const allDevices = devicesToProcess(userData)
76
74
 
77
- const allData = {
78
- devices: [],
79
- xpert: devices.filter(d => d.attributes.xpert).length > 0,
80
- from: from,
81
- to: to
82
- }
75
+ const allData = {
76
+ devices: [],
77
+ xpert: allDevices.filter(d => d.attributes.xpert).length > 0,
78
+ from,
79
+ to
80
+ }
83
81
 
82
+ const sliced = automaticReports.sliceArray(allDevices, 5)
83
+ let deviceCount = 0
84
+ for (const devices of sliced) {
84
85
  let summaries = []
85
- const {trips, route, summary} = await traccar.getAllInOne(traccarInstance, from, to, devices, true, true, false, !userData.groupByDay)
86
-
87
- if(userData.groupByDay) {
88
- console.log('trips:' + trips.length)
89
-
90
- if (trips.length) {
91
- tripHelper.checkTripsKms(traccarInstance, from, to, devices, {trips, route})
92
- }
93
-
94
- const dates = getDates(from, to)
95
- for(const date of dates){
96
- if(userData.allWeek || !userData.weekDays || !userData.dayHours){
97
- const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
98
- const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
99
-
100
- const summary = await traccar.getSummary(traccarInstance, fromByDay, toByDay, devices)
101
- summary.forEach(s => s.date = date)
102
- summaries = summaries.concat(summary)
103
- }
86
+ const {
87
+ trips,
88
+ route,
89
+ summary
90
+ } = await traccar.getAllInOne(traccarInstance, from, to, devices, true, true, false, !userData.groupByDay, deviceCount, allDevices.length)
91
+
92
+ if (userData.groupByDay) {
93
+ console.log('trips:' + trips.length)
94
+
95
+ if (trips.length) {
96
+ tripHelper.checkTripsKms(traccarInstance, from, to, devices, { trips, route })
97
+ }
98
+
99
+ const dates = getDates(from, to)
100
+ for (const date of dates) {
101
+ if (userData.allWeek || !userData.weekDays || !userData.dayHours) {
102
+ const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
103
+ const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
104
+
105
+ const summary = await traccar.getSummary(traccarInstance, fromByDay, toByDay, devices)
106
+ summary.forEach(s => {
107
+ s.date = date
108
+ })
109
+ summaries = summaries.concat(summary)
104
110
  }
111
+ }
105
112
  } else {
106
- summaries = summary
113
+ summaries = summary
107
114
  }
108
115
 
109
116
  console.log('Summary:' + summaries.length)
110
117
 
111
- //Process report data
118
+ // Process report data
112
119
  if (summaries.length || trips.length) {
113
- allData.devices = processDevices(from, to, devices, {summaries: summaries, trips, route}, userData)
120
+ allData.devices = allData.devices.concat(processDevices(from, to, devices, { summaries, trips, route }, userData))
114
121
  }
122
+ deviceCount += devices.length
123
+ }
115
124
 
116
- return allData
125
+ return allData
117
126
  }
118
127
 
119
- async function createActivityReportByDriver(from, to, userData, traccarInstance){
120
- const devices = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
128
+ async function createActivityReportByDriver (from, to, userData, traccarInstance) {
129
+ const devices = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
121
130
 
122
- let tripsData = []
123
- let routeData = []
124
- if(devices.length > 0) {
125
- const allInOne = await traccar.getAllInOne(traccarInstance, from, to, devices, true, true, false, false)
126
- tripsData = allInOne.trips
127
- routeData = allInOne.route
128
- }
131
+ let tripsData = []
132
+ let routeData = []
133
+ if (devices.length > 0) {
134
+ const allInOne = await traccar.getAllInOne(traccarInstance, from, to, devices, true, true, false, false)
135
+ tripsData = allInOne.trips
136
+ routeData = allInOne.route
137
+ }
129
138
 
130
- const allData = {
131
- drivers: [],
132
- from: from,
133
- to: to
134
- }
139
+ const allData = {
140
+ drivers: [],
141
+ from,
142
+ to
143
+ }
135
144
 
136
- allData.drivers = processDrivers(from, to, userData.drivers, {trips: tripsData, route: routeData}, userData)
145
+ allData.drivers = processDrivers(from, to, userData.drivers, { trips: tripsData, route: routeData }, userData)
137
146
 
138
- return allData
147
+ return allData
139
148
  }
140
149
 
141
- function processDevices(from, to, devices, data, userData) {
142
- const devicesResult = []
150
+ function processDevices (from, to, devices, data, userData) {
151
+ const devicesResult = []
143
152
 
144
- devices.forEach(d => {
145
- const deviceTrips = data.trips.filter(t => t.deviceId === d.id)
146
- const deviceRoute = data.route.filter(t => t.deviceId === d.id)
153
+ devices.forEach(d => {
154
+ const deviceTrips = data.trips.filter(t => t.deviceId === d.id)
155
+ const deviceRoute = data.route.filter(t => t.deviceId === d.id)
147
156
 
148
- let uncompletedTripRoute = []
149
- if(deviceRoute.length && deviceTrips.length && deviceRoute[deviceRoute.length-1].attributes.ignition) {
150
- uncompletedTripRoute = deviceRoute.filter(p => p.attributes.ignition && new Date(p.fixTime).getTime() > new Date(deviceTrips[deviceTrips.length - 1].endTime).getTime())
151
- if (uncompletedTripRoute.length) {
152
- deviceTrips.push(calculateTrip(d, uncompletedTripRoute))
153
- }
154
- }
157
+ let uncompletedTripRoute = []
158
+ if (deviceRoute.length && deviceTrips.length && deviceRoute[deviceRoute.length - 1].attributes.ignition) {
159
+ uncompletedTripRoute = deviceRoute.filter(p => p.attributes.ignition && new Date(p.fixTime).getTime() > new Date(deviceTrips[deviceTrips.length - 1].endTime).getTime())
160
+ if (uncompletedTripRoute.length) {
161
+ deviceTrips.push(calculateTrip(d, uncompletedTripRoute))
162
+ }
163
+ }
155
164
 
156
- const summary = []
157
- if(userData.groupByDay) {
158
- const trips = deviceTrips.filter(t => userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, deviceRoute))
159
-
160
- const dates = getDates(from, to)
161
- for(const date of dates){
162
- const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
163
- const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
164
-
165
- const tripsByDay = trips.filter(t => (new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay))
166
- const distance = tripsByDay.reduce((a, b) => a + b.distance, 0)
167
-
168
- if(!userData.allWeek && userData.weekDays && userData.dayHours) {
169
- if(weekDaySelected(date, userData.weekDays)) {
170
- summary.push({
171
- date: date,
172
- deviceId: d.id,
173
- averageSpeed: distance > 0 ? ((tripsByDay.reduce((a, b) => a + (b.averageSpeed * b.distance), 0)) / distance) : 0,
174
- distance: distance,
175
- engineHours: tripsByDay.reduce((a, b) => a + b.duration, 0),
176
- maxSpeed: tripsByDay.reduce((a, b) => {
177
- return a > b.maxSpeed ? a : b.maxSpeed
178
- }, 0),
179
- endOdometer: 0,
180
- startOdometer: 0,
181
- startTime: tripsByDay.length && tripsByDay[0].startTime,
182
- endTime: tripsByDay.length && tripsByDay[tripsByDay.length - 1].endTime
183
- })
184
- }
185
- } else {
186
- const summaryCurrentDay = data.summaries.find(s => {
187
- return s.deviceId === d.id && s.date.getTime() === date.getTime()})
188
-
189
- if (summaryCurrentDay) {
190
- summaryCurrentDay.distance = distance
191
- summaryCurrentDay.convertedSpentFuel = automaticReports.calculateSpentFuel(summaryCurrentDay.spentFuel, d)
192
- summaryCurrentDay.startTime = tripsByDay.length && tripsByDay[0].startTime
193
- summaryCurrentDay.endTime = tripsByDay.length && tripsByDay[tripsByDay.length-1].endTime
194
-
195
- summary.push(summaryCurrentDay)
196
- }
197
- }
198
- }
165
+ const summary = []
166
+ if (userData.groupByDay) {
167
+ const trips = deviceTrips.filter(t => userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, deviceRoute))
168
+
169
+ const dates = getDates(from, to)
170
+ for (const date of dates) {
171
+ const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
172
+ const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
173
+
174
+ const tripsByDay = trips.filter(t => (new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay))
175
+ const distance = tripsByDay.reduce((a, b) => a + b.distance, 0)
176
+
177
+ if (!userData.allWeek && userData.weekDays && userData.dayHours) {
178
+ if (weekDaySelected(date, userData.weekDays)) {
179
+ summary.push({
180
+ date,
181
+ deviceId: d.id,
182
+ averageSpeed: distance > 0 ? ((tripsByDay.reduce((a, b) => a + (b.averageSpeed * b.distance), 0)) / distance) : 0,
183
+ distance,
184
+ engineHours: tripsByDay.reduce((a, b) => a + b.duration, 0),
185
+ maxSpeed: tripsByDay.reduce((a, b) => {
186
+ return a > b.maxSpeed ? a : b.maxSpeed
187
+ }, 0),
188
+ endOdometer: 0,
189
+ startOdometer: 0,
190
+ startTime: tripsByDay.length && tripsByDay[0].startTime,
191
+ endTime: tripsByDay.length && tripsByDay[tripsByDay.length - 1].endTime
192
+ })
193
+ }
199
194
  } else {
200
- if(!userData.allWeek && userData.weekDays && userData.dayHours) {
201
- const trips = deviceTrips.filter(t => userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, deviceRoute))
202
- const distance = trips.reduce((a, b) => a + b.distance, 0)
203
- summary.push({
204
- deviceId: d.id,
205
- averageSpeed: distance > 0 ? ((trips.reduce((a, b) => a + (b.averageSpeed * b.distance), 0)) / distance) : 0,
206
- distance: distance,
207
- engineHours: trips.reduce((a, b) => a + b.duration, 0),
208
- maxSpeed: trips.reduce((a, b) => {
209
- return a > b.maxSpeed ? a : b.maxSpeed
210
- }, 0),
211
- startOdometer: trips.length ? trips[0].startOdometer : 0,
212
- endOdometer: trips.length ? trips[trips.length - 1].endOdometer : 0,
213
- startTime: trips.length && trips[0].startTime,
214
- endTime: trips.length && trips[trips.length - 1].endTime
215
- })
216
- } else {
217
- summary.push(...data.summaries.filter(s => s.deviceId === d.id))
218
-
219
- if (summary.length) {
220
- summary.forEach(s => {
221
- s.distance = deviceTrips.reduce((a, b) => a + b.distance, 0)
222
- s.convertedSpentFuel = automaticReports.calculateSpentFuel(s.spentFuel, d)
223
- s.startTime = deviceTrips.length && deviceTrips[0].startTime
224
- s.endTime = deviceTrips.length && deviceTrips[deviceTrips.length - 1].endTime
225
- })
226
- }
227
- }
195
+ const summaryCurrentDay = data.summaries.find(s => {
196
+ return s.deviceId === d.id && s.date.getTime() === date.getTime()
197
+ })
198
+
199
+ if (summaryCurrentDay) {
200
+ summaryCurrentDay.distance = distance
201
+ summaryCurrentDay.convertedSpentFuel = automaticReports.calculateSpentFuel(summaryCurrentDay.spentFuel, d)
202
+ summaryCurrentDay.startTime = tripsByDay.length && tripsByDay[0].startTime
203
+ summaryCurrentDay.endTime = tripsByDay.length && tripsByDay[tripsByDay.length - 1].endTime
204
+
205
+ summary.push(summaryCurrentDay)
206
+ }
228
207
  }
229
-
230
- const deviceData = {
231
- device: d,
232
- summary: summary,
208
+ }
209
+ } else {
210
+ if (!userData.allWeek && userData.weekDays && userData.dayHours) {
211
+ const trips = deviceTrips.filter(t => userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, deviceRoute))
212
+ const distance = trips.reduce((a, b) => a + b.distance, 0)
213
+ summary.push({
214
+ deviceId: d.id,
215
+ averageSpeed: distance > 0 ? ((trips.reduce((a, b) => a + (b.averageSpeed * b.distance), 0)) / distance) : 0,
216
+ distance,
217
+ engineHours: trips.reduce((a, b) => a + b.duration, 0),
218
+ maxSpeed: trips.reduce((a, b) => {
219
+ return a > b.maxSpeed ? a : b.maxSpeed
220
+ }, 0),
221
+ startOdometer: trips.length ? trips[0].startOdometer : 0,
222
+ endOdometer: trips.length ? trips[trips.length - 1].endOdometer : 0,
223
+ startTime: trips.length && trips[0].startTime,
224
+ endTime: trips.length && trips[trips.length - 1].endTime
225
+ })
226
+ } else {
227
+ summary.push(...data.summaries.filter(s => s.deviceId === d.id))
228
+
229
+ if (summary.length) {
230
+ summary.forEach(s => {
231
+ s.distance = deviceTrips.reduce((a, b) => a + b.distance, 0)
232
+ s.convertedSpentFuel = automaticReports.calculateSpentFuel(s.spentFuel, d)
233
+ s.startTime = deviceTrips.length && deviceTrips[0].startTime
234
+ s.endTime = deviceTrips.length && deviceTrips[deviceTrips.length - 1].endTime
235
+ })
233
236
  }
234
- devicesResult.push(deviceData)
235
- })
237
+ }
238
+ }
236
239
 
237
- return devicesResult
240
+ const deviceData = {
241
+ device: d,
242
+ summary
243
+ }
244
+ devicesResult.push(deviceData)
245
+ })
246
+
247
+ return devicesResult
238
248
  }
239
249
 
240
- function processDrivers(from, to, drivers, data, userData) {
241
- console.log(data)
242
- const driversResult = []
243
- drivers.forEach(d => {
244
- const route = data.route.filter(p => p.attributes.driverUniqueId === d.uniqueId)
245
- const trips = data.trips.filter(t => t.driverUniqueId === d.uniqueId)
246
- if (trips.length > 0) {
247
- const driverData = {
248
- driver: d,
249
- summary: [],
250
- }
251
- if(userData.groupByDay) {
252
- const dates = getDates(from, to)
253
- for(const date of dates){
254
- const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
255
- const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
256
-
257
- const tripsByDay = trips.filter(t => (new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay)
258
- && (userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, route)))
259
-
260
- driverData.summary.push({
261
- date: date,
262
- distance: tripsByDay.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0),
263
- engineHours: tripsByDay.reduce((a, b) => a + b.duration, 0),
264
- maxSpeed: tripsByDay.reduce((a, b) => Math.max(a, b.maxSpeed), 0),
265
- averageSpeed: tripsByDay.length ? Math.round(tripsByDay.reduce((a, b) => a + b.averageSpeed, 0) / tripsByDay.length) : 0,
266
- startTime: tripsByDay.length && tripsByDay[0].startTime,
267
- endTime: tripsByDay.length && tripsByDay[tripsByDay.length-1].endTime
268
- })
269
- }
270
- } else {
271
- driverData.summary.push({
272
- distance: trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0),
273
- engineHours: trips.reduce((a, b) => a + b.duration, 0),
274
- maxSpeed: trips.reduce((a, b) => Math.max(a, b.maxSpeed), 0),
275
- averageSpeed: Math.round(trips.reduce((a, b) => a + b.averageSpeed, 0) / trips.length),
276
- startTime: trips.length && trips[0].startTime,
277
- endTime: trips.length && trips[trips.length-1].endTime
278
- })
279
- }
280
- driversResult.push(driverData)
250
+ function processDrivers (from, to, drivers, data, userData) {
251
+ console.log(data)
252
+ const driversResult = []
253
+ drivers.forEach(d => {
254
+ const route = data.route.filter(p => p.attributes.driverUniqueId === d.uniqueId)
255
+ const trips = data.trips.filter(t => t.driverUniqueId === d.uniqueId)
256
+ if (trips.length > 0) {
257
+ const driverData = {
258
+ driver: d,
259
+ summary: []
260
+ }
261
+ if (userData.groupByDay) {
262
+ const dates = getDates(from, to)
263
+ for (const date of dates) {
264
+ const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
265
+ const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
266
+
267
+ const tripsByDay = trips.filter(t => (new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay) &&
268
+ (userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, route)))
269
+
270
+ driverData.summary.push({
271
+ date,
272
+ distance: tripsByDay.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0),
273
+ engineHours: tripsByDay.reduce((a, b) => a + b.duration, 0),
274
+ maxSpeed: tripsByDay.reduce((a, b) => Math.max(a, b.maxSpeed), 0),
275
+ averageSpeed: tripsByDay.length ? Math.round(tripsByDay.reduce((a, b) => a + b.averageSpeed, 0) / tripsByDay.length) : 0,
276
+ startTime: tripsByDay.length && tripsByDay[0].startTime,
277
+ endTime: tripsByDay.length && tripsByDay[tripsByDay.length - 1].endTime
278
+ })
281
279
  }
282
- })
280
+ } else {
281
+ driverData.summary.push({
282
+ distance: trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0),
283
+ engineHours: trips.reduce((a, b) => a + b.duration, 0),
284
+ maxSpeed: trips.reduce((a, b) => Math.max(a, b.maxSpeed), 0),
285
+ averageSpeed: Math.round(trips.reduce((a, b) => a + b.averageSpeed, 0) / trips.length),
286
+ startTime: trips.length && trips[0].startTime,
287
+ endTime: trips.length && trips[trips.length - 1].endTime
288
+ })
289
+ }
290
+ driversResult.push(driverData)
291
+ }
292
+ })
283
293
 
284
- return driversResult
294
+ return driversResult
285
295
  }
286
296
 
287
- async function exportActivityReportToPDF(userData, reportData) {
288
- console.log('Export to PDF')
289
- const translations = getTranslations(userData)
297
+ async function exportActivityReportToPDF (userData, reportData) {
298
+ console.log('Export to PDF')
299
+ const translations = getTranslations(userData)
300
+
301
+ if (reportData.devices || reportData.drivers) {
302
+ // eslint-disable-next-line new-cap
303
+ const doc = new jsPDF.jsPDF('l')
304
+ await headerFromUser(doc, translations.report.titleActivityReport, userData.user)
305
+
306
+ if (userData.groupByDay && userData.byDriver) {
307
+ exportActivityReportToPDFDriverGroupByDay(doc, translations, reportData, userData)
308
+ } else if (userData.groupByDay) {
309
+ exportActivityReportToPDFVehicleGroupByDay(doc, translations, reportData, userData)
310
+ } else if (userData.byDriver) {
311
+ exportActivityReportToPDFDriver(doc, translations, reportData, userData)
312
+ } else {
313
+ exportActivityReportToPDFVehicle(doc, translations, reportData, userData)
314
+ }
290
315
 
291
- if (reportData.devices || reportData.drivers) {
292
- const doc = new jsPDF.jsPDF('l')
293
- await headerFromUser(doc, translations.report.titleActivityReport, userData.user)
316
+ return doc
317
+ }
318
+ }
294
319
 
295
- if(userData.groupByDay && userData.byDriver) {
296
- exportActivityReportToPDF_Driver_GroupByDay(doc, translations, reportData, userData)
297
- } else if(userData.groupByDay) {
298
- exportActivityReportToPDF_Vehicle_GroupByDay(doc, translations, reportData, userData)
299
- } else if(userData.byDriver) {
300
- exportActivityReportToPDF_Driver(doc, translations, reportData, userData)
301
- } else {
302
- exportActivityReportToPDF_Vehicle(doc, translations, reportData, userData)
303
- }
320
+ function exportActivityReportToPDFDriver (doc, translations, reportData, userData) {
321
+ const headers = [translations.report.driver,
322
+ translations.report.start,
323
+ translations.report.end,
324
+ translations.report.distance,
325
+ translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)',
326
+ translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)',
327
+ translations.report.driverHours]
328
+
329
+ doc.setFontSize(10)
330
+ doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, 30)
331
+
332
+ const data = []
333
+ reportData.drivers.forEach(d => {
334
+ const temp = [
335
+ d.driver.name,
336
+ d.summary[0].startTime ? new Date(d.summary[0].startTime).toLocaleString() : '',
337
+ d.summary[0].endTime ? new Date(d.summary[0].endTime).toLocaleString() : '',
338
+ Number((d.summary[0].distance / 1000).toFixed(2)),
339
+ Math.round(d.summary[0].averageSpeed * 1.85200),
340
+ Math.round(d.summary[0].maxSpeed * 1.85200),
341
+ convertMS(d.summary[0].engineHours)
342
+ ]
343
+ data.push(temp)
344
+ })
345
+
346
+ const footValues = ['Total:' + reportData.drivers.length,
347
+ '', '',
348
+ (reportData.drivers.reduce((a, b) => a + b.summary[0].distance, 0) / 1000).toFixed(0),
349
+ getSumAvgSpeed(reportData.drivers, userData),
350
+ getSumMaxSpeed(reportData.drivers, userData),
351
+ convertMS(reportData.drivers.reduce((a, b) => a + b.summary[0].engineHours, 0))]
352
+
353
+ const style = getStyle(getUserPartner(userData.user))
354
+ addTable(doc, headers, data, footValues, style, 35)
355
+ }
304
356
 
305
- return doc
306
- }
357
+ function exportActivityReportToPDFVehicle (doc, translations, reportData, userData) {
358
+ const headers = [translations.report.vehicle,
359
+ translations.report.group,
360
+ translations.report.start,
361
+ translations.report.end,
362
+ translations.report.distance,
363
+ translations.report.start + ' ' + translations.report.odometer,
364
+ translations.report.end + ' ' + translations.report.odometer,
365
+ translations.report.avgSpeed + ' (Km/h)',
366
+ translations.report.maxSpeed + ' (Km/h)',
367
+ translations.report.engineHours,
368
+ translations.report.spentFuel]
369
+
370
+ doc.setFontSize(10)
371
+ doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, 30)
372
+
373
+ const data = []
374
+ reportData.devices.forEach(d => {
375
+ const group = userData.groups.find(g => d.device.groupId === g.id)
376
+
377
+ const temp = [
378
+ d.summary[0].deviceName,
379
+ group ? group.name : '',
380
+ d.summary[0].startTime ? new Date(d.summary[0].startTime).toLocaleString() : '',
381
+ d.summary[0].endTime ? new Date(d.summary[0].endTime).toLocaleString() : '',
382
+ Number((d.summary[0].distance / 1000).toFixed(2)),
383
+ Number((d.summary[0].startOdometer / 1000).toFixed(2)),
384
+ Number((d.summary[0].endOdometer / 1000).toFixed(2)),
385
+ Math.round(d.summary[0].averageSpeed * 1.85200),
386
+ Math.round(d.summary[0].maxSpeed * 1.85200),
387
+ convertMS(d.summary[0].engineHours),
388
+ d.summary[0].convertedSpentFuel >= 0 ? d.summary[0].convertedSpentFuel : 0
389
+ ]
390
+ data.push(temp)
391
+ })
392
+
393
+ const footValues = ['Total:' + reportData.devices.length,
394
+ '', '', '',
395
+ (reportData.devices.reduce((a, b) => a + b.summary[0].distance, 0) / 1000).toFixed(0),
396
+ '', '',
397
+ getSumAvgSpeed(reportData.devices, userData),
398
+ getSumMaxSpeed(reportData.devices, userData),
399
+ convertMS(reportData.devices.reduce((a, b) => a + b.summary[0].engineHours, 0)),
400
+ getSumSpentFuel(reportData.devices, userData)]
401
+
402
+ const style = getStyle(getUserPartner(userData.user))
403
+ addTable(doc, headers, data, footValues, style, 35)
307
404
  }
308
405
 
309
- function exportActivityReportToPDF_Driver(doc, translations, reportData, userData) {
310
- const headers = [translations.report.driver,
311
- translations.report.start,
312
- translations.report.end,
313
- translations.report.distance,
314
- translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)',
315
- translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)',
316
- translations.report.driverHours]
406
+ function exportActivityReportToPDFVehicleGroupByDay (doc, translations, reportData, userData) {
407
+ const headers = [translations.report.date,
408
+ translations.report.start,
409
+ translations.report.end,
410
+ translations.report.distance,
411
+ translations.report.start + ' ' + translations.report.odometer,
412
+ translations.report.end + ' ' + translations.report.odometer,
413
+ translations.report.avgSpeed + ' (Km/h)',
414
+ translations.report.maxSpeed + ' (Km/h)',
415
+ translations.report.engineHours,
416
+ translations.report.spentFuel]
417
+
418
+ const weekDays = [
419
+ translations.report.sunday,
420
+ translations.report.monday,
421
+ translations.report.tuesday,
422
+ translations.report.wednesday,
423
+ translations.report.thursday,
424
+ translations.report.friday,
425
+ translations.report.saturday
426
+ ]
427
+
428
+ reportData.devices.forEach(function (d, index) {
429
+ const name = userData.byDriver ? d.driver.name : d.device.name
430
+ const group = userData.byDriver ? userData.groups.find(g => g.drivers.includes(d.id)) : userData.groups.find(g => d.device.groupId === g.id)
431
+
432
+ const space = index === 0 ? 8 : 0
433
+ if (index) {
434
+ doc.addPage()
435
+ }
317
436
 
318
- doc.setFontSize(10)
319
- doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, 30)
437
+ doc.setFontSize(13)
438
+ doc.text(name, 20, space + 20)
439
+ doc.setFontSize(11)
440
+ doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
441
+ doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
442
+
443
+ if (!userData.allWeek && userData.weekDays) {
444
+ doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '') +
445
+ (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '') +
446
+ (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '') +
447
+ (userData.weekDays.thursday ? ' ' + translations.report.thursday + ',' : '') +
448
+ (userData.weekDays.friday ? ' ' + translations.report.friday + ',' : '') +
449
+ (userData.weekDays.saturday ? ' ' + translations.report.saturday + ',' : '') +
450
+ (userData.weekDays.sunday ? ' ' + translations.report.sunday + ',' : '') +
451
+ ' das ' +
452
+ userData.dayHours.startTime + ' - ' + userData.dayHours.endTime, 20, space + 30)
453
+ }
320
454
 
321
455
  const data = []
322
- reportData.drivers.forEach(d => {
323
- const temp = [
324
- d.driver.name,
325
- d.summary[0].startTime ? new Date(d.summary[0].startTime).toLocaleString() : '',
326
- d.summary[0].endTime ? new Date(d.summary[0].endTime).toLocaleString() : '',
327
- Number((d.summary[0].distance / 1000).toFixed(2)),
328
- Math.round(d.summary[0].averageSpeed * 1.85200),
329
- Math.round(d.summary[0].maxSpeed * 1.85200),
330
- convertMS(d.summary[0].engineHours)
331
- ]
332
- data.push(temp)
456
+ d.summary.forEach(s => {
457
+ const temp = [
458
+ new Date(s.date).toLocaleDateString() + ' - ' + weekDays[s.date.getDay()],
459
+ s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
460
+ s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
461
+ Number((s.distance / 1000).toFixed(2)),
462
+ Number((s.startOdometer / 1000).toFixed(2)),
463
+ Number((s.endOdometer / 1000).toFixed(2)),
464
+ Math.round(s.averageSpeed * 1.85200),
465
+ Math.round(s.maxSpeed * 1.85200),
466
+ convertMS(s.engineHours),
467
+ s.convertedSpentFuel >= 0 ? s.convertedSpentFuel : 0
468
+ ]
469
+
470
+ data.push(temp)
333
471
  })
334
472
 
335
- const footValues = ['Total:' + reportData.drivers.length,
336
- '','',
337
- (reportData.drivers.reduce((a, b) => a + b.summary[0].distance, 0) / 1000).toFixed(0),
338
- getSumAvgSpeed(reportData.drivers, userData),
339
- getSumMaxSpeed(reportData.drivers, userData),
340
- convertMS(reportData.drivers.reduce((a, b) => a + b.summary[0].engineHours, 0))]
473
+ const footValues = ['Total:' + d.summary.length,
474
+ '', '',
475
+ (d.summary.reduce((a, b) => a + b.distance, 0) / 1000).toFixed(0),
476
+ '', '',
477
+ getSumAvgSpeed(d.summary, userData),
478
+ getSumMaxSpeed(d.summary, userData),
479
+ convertMS(d.summary.reduce((a, b) => a + b.engineHours, 0)),
480
+ getSumSpentFuel(d.summary, userData)]
341
481
 
342
482
  const style = getStyle(getUserPartner(userData.user))
343
- addTable(doc, headers, data, footValues, style, 35)
483
+ addTable(doc, headers, data, footValues, style, 40)
484
+ })
344
485
  }
345
486
 
346
- function exportActivityReportToPDF_Vehicle(doc, translations, reportData, userData) {
347
- const headers = [translations.report.vehicle,
348
- translations.report.group,
349
- translations.report.start,
350
- translations.report.end,
351
- translations.report.distance,
352
- translations.report.start + ' ' + translations.report.odometer,
353
- translations.report.end + ' ' + translations.report.odometer,
354
- translations.report.avgSpeed + ' (Km/h)',
355
- translations.report.maxSpeed + ' (Km/h)',
356
- translations.report.engineHours,
357
- translations.report.spentFuel]
358
-
359
- doc.setFontSize(10)
360
- doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, 30)
361
-
362
- const data = []
487
+ function exportActivityReportToExcelVehicleGroupByDay (settings, translations, reportData, userData) {
488
+ const headers = [{ label: translations.report.vehicle, value: 'name' },
489
+ { label: translations.report.group, value: 'group' },
490
+ { label: translations.report.date, value: 'date' },
491
+ { label: translations.report.weekDay, value: 'weekday' },
492
+ { label: translations.report.start, value: 'start' },
493
+ { label: translations.report.end, value: 'end' },
494
+ { label: translations.report.distance, value: 'distance' },
495
+ { label: translations.report.start + ' ' + translations.report.odometer, value: 'startOdometer' },
496
+ { label: translations.report.end + ' ' + translations.report.odometer, value: 'endOdometer' },
497
+ { label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed' },
498
+ { label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed' },
499
+ { label: translations.report.engineHours, value: 'engineHours' },
500
+ { label: translations.report.spentFuel, value: 'spentFuel' }]
501
+
502
+ const weekDays = [
503
+ translations.report.sunday,
504
+ translations.report.monday,
505
+ translations.report.tuesday,
506
+ translations.report.wednesday,
507
+ translations.report.thursday,
508
+ translations.report.friday,
509
+ translations.report.saturday
510
+ ]
511
+
512
+ let data = []
513
+ if (reportData.devices) {
363
514
  reportData.devices.forEach(d => {
364
- const group = userData.groups.find(g => d.device.groupId === g.id)
365
-
366
- const temp = [
367
- d.summary[0].deviceName,
368
- group ? group.name : '',
369
- d.summary[0].startTime ? new Date(d.summary[0].startTime).toLocaleString() : '',
370
- d.summary[0].endTime ? new Date(d.summary[0].endTime).toLocaleString() : '',
371
- Number((d.summary[0].distance / 1000).toFixed(2)),
372
- Number((d.summary[0].startOdometer / 1000).toFixed(2)),
373
- Number((d.summary[0].endOdometer / 1000).toFixed(2)),
374
- Math.round(d.summary[0].averageSpeed * 1.85200),
375
- Math.round(d.summary[0].maxSpeed * 1.85200),
376
- convertMS(d.summary[0].engineHours),
377
- d.summary[0].convertedSpentFuel >= 0 ? d.summary[0].convertedSpentFuel : 0,
378
- ]
379
- data.push(temp)
380
- })
381
-
382
- const footValues = ['Total:' + reportData.devices.length,
383
- '','','',
384
- (reportData.devices.reduce((a, b) => a + b.summary[0].distance, 0) / 1000).toFixed(0),
385
- '', '',
386
- getSumAvgSpeed(reportData.devices, userData),
387
- getSumMaxSpeed(reportData.devices, userData),
388
- convertMS(reportData.devices.reduce((a, b) => a + b.summary[0].engineHours, 0)),
389
- getSumSpentFuel(reportData.devices, userData)]
390
-
391
- const style = getStyle(getUserPartner(userData.user))
392
- addTable(doc, headers, data, footValues, style, 35)
393
- }
394
-
395
- function exportActivityReportToPDF_Vehicle_GroupByDay(doc, translations, reportData, userData) {
396
- const headers = [translations.report.date,
397
- translations.report.start,
398
- translations.report.end,
399
- translations.report.distance,
400
- translations.report.start + ' ' + translations.report.odometer,
401
- translations.report.end + ' ' + translations.report.odometer,
402
- translations.report.avgSpeed + ' (Km/h)',
403
- translations.report.maxSpeed + ' (Km/h)',
404
- translations.report.engineHours,
405
- translations.report.spentFuel]
406
-
407
- const weekDays = [
408
- translations.report.sunday,
409
- translations.report.monday,
410
- translations.report.tuesday,
411
- translations.report.wednesday,
412
- translations.report.thursday,
413
- translations.report.friday,
414
- translations.report.saturday
415
- ]
416
-
417
- reportData.devices.forEach(function (d, index) {
418
- const name = userData.byDriver ? d.driver.name : d.device.name
419
- const group = userData.byDriver ? userData.groups.find(g => g.drivers.includes(d.id)) : userData.groups.find(g => d.device.groupId === g.id)
420
-
421
- let space = index === 0 ? 8 : 0
422
- if(index){
423
- doc.addPage()
424
- }
425
-
426
- doc.setFontSize(13)
427
- doc.text(name, 20, space + 20)
428
- doc.setFontSize(11)
429
- doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
430
- doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
431
-
432
- if (!userData.allWeek && userData.weekDays) {
433
- doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '')
434
- + (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '')
435
- + (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '')
436
- + (userData.weekDays.thursday ? ' ' + translations.report.thursday + ',' : '')
437
- + (userData.weekDays.friday ? ' ' + translations.report.friday + ',' : '')
438
- + (userData.weekDays.saturday ? ' ' + translations.report.saturday + ',' : '')
439
- + (userData.weekDays.sunday ? ' ' + translations.report.sunday + ',' : '')
440
- + ' das '
441
- + userData.dayHours.startTime + ' - ' + userData.dayHours.endTime, 20, space + 30)
442
- }
443
-
444
- const data = []
445
- d.summary.forEach(s => {
446
- const temp = [
447
- new Date(s.date).toLocaleDateString() + ' - ' + weekDays[s.date.getDay()],
448
- s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
449
- s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
450
- Number((s.distance / 1000).toFixed(2)),
451
- Number((s.startOdometer / 1000).toFixed(2)),
452
- Number((s.endOdometer / 1000).toFixed(2)),
453
- Math.round(s.averageSpeed * 1.85200),
454
- Math.round(s.maxSpeed * 1.85200),
455
- convertMS(s.engineHours),
456
- s.convertedSpentFuel >= 0 ? s.convertedSpentFuel : 0
457
- ]
458
-
459
- data.push(temp)
460
- })
461
-
462
- const footValues = ['Total:' + d.summary.length,
463
- '','',
464
- (d.summary.reduce((a, b) => a + b.distance, 0) / 1000).toFixed(0),
465
- '', '',
466
- getSumAvgSpeed(d.summary, userData),
467
- getSumMaxSpeed(d.summary, userData),
468
- convertMS(d.summary.reduce((a, b) => a + b.engineHours, 0)),
469
- getSumSpentFuel(d.summary, userData)]
470
-
471
- const style = getStyle(getUserPartner(userData.user))
472
- addTable(doc, headers, data, footValues, style, 40)
473
- })
474
- }
515
+ const group = userData.groups.find(g => d.device.groupId === g.id)
475
516
 
476
- function exportActivityReportToExcel_Vehicle_GroupByDay(settings, translations, reportData, userData) {
477
- const headers = [{label: translations.report.vehicle, value: 'name'},
478
- {label: translations.report.group, value: 'group'},
479
- {label: translations.report.date, value: 'date'},
480
- {label: translations.report.weekDay, value: 'weekday'},
481
- {label: translations.report.start, value: 'start'},
482
- {label: translations.report.end, value: 'end'},
483
- {label: translations.report.distance, value: 'distance'},
484
- {label: translations.report.start + ' ' + translations.report.odometer, value: 'startOdometer'},
485
- {label: translations.report.end + ' ' + translations.report.odometer, value: 'endOdometer'},
486
- {label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed'},
487
- {label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed'},
488
- {label: translations.report.engineHours, value: 'engineHours'},
489
- {label: translations.report.spentFuel, value: 'spentFuel'}]
490
-
491
- const weekDays = [
492
- translations.report.sunday,
493
- translations.report.monday,
494
- translations.report.tuesday,
495
- translations.report.wednesday,
496
- translations.report.thursday,
497
- translations.report.friday,
498
- translations.report.saturday
499
- ]
500
-
501
- let data = []
502
- if (reportData.devices) {
503
- reportData.devices.forEach(d => {
504
- const group = userData.groups.find(g => d.device.groupId === g.id)
505
-
506
- data = data.concat([{}])
507
- data = data.concat(d.summary.map(s => {
508
- return {
509
- name: s.deviceName,
510
- group: group ? group.name : '',
511
- date: new Date(s.date).toLocaleDateString(),
512
- weekday: weekDays[s.date.getDay()],
513
- start: s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
514
- end:s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
515
- distance: Number((s.distance / 1000).toFixed(0)),
516
- startOdometer: Number((s.startOdometer / 1000).toFixed(0)),
517
- endOdometer: Number((s.endOdometer / 1000).toFixed(0)),
518
- avgSpeed: Math.round(s.averageSpeed * 1.85200),
519
- maxSpeed: Math.round(s.maxSpeed * 1.85200),
520
- engineHours: convertMS(s.engineHours),
521
- spentFuel: s.convertedSpentFuel >= 0 ? s.convertedSpentFuel : 0
522
- }
523
- }))
524
- })
525
- console.log(data)
517
+ data = data.concat([{}])
518
+ data = data.concat(d.summary.map(s => {
526
519
  return {
527
- headers,
528
- data,
529
- settings
520
+ name: s.deviceName,
521
+ group: group ? group.name : '',
522
+ date: new Date(s.date).toLocaleDateString(),
523
+ weekday: weekDays[s.date.getDay()],
524
+ start: s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
525
+ end: s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
526
+ distance: Number((s.distance / 1000).toFixed(0)),
527
+ startOdometer: Number((s.startOdometer / 1000).toFixed(0)),
528
+ endOdometer: Number((s.endOdometer / 1000).toFixed(0)),
529
+ avgSpeed: Math.round(s.averageSpeed * 1.85200),
530
+ maxSpeed: Math.round(s.maxSpeed * 1.85200),
531
+ engineHours: convertMS(s.engineHours),
532
+ spentFuel: s.convertedSpentFuel >= 0 ? s.convertedSpentFuel : 0
530
533
  }
534
+ }))
535
+ })
536
+ console.log(data)
537
+ return {
538
+ headers,
539
+ data,
540
+ settings
531
541
  }
542
+ }
532
543
  }
533
544
 
534
- function exportActivityReportToPDF_Driver_GroupByDay(doc, translations, reportData, userData) {
535
- const headers = [translations.report.date,
536
- translations.report.start,
537
- translations.report.end,
538
- translations.report.distance,
539
- translations.report.avgSpeed + ' (Km/h)',
540
- translations.report.maxSpeed + ' (Km/h)',
541
- translations.report.engineHours]
542
-
543
- const weekDays = [
544
- translations.report.sunday,
545
- translations.report.monday,
546
- translations.report.tuesday,
547
- translations.report.wednesday,
548
- translations.report.thursday,
549
- translations.report.friday,
550
- translations.report.saturday
551
- ]
552
-
553
- reportData.drivers.forEach(function (d, index) {
554
- const name = d.driver.name
555
- const group = userData.groups.find(g => g.drivers.includes(d.id))
556
-
557
- let space = index === 0 ? 8 : 0
558
- if(index){
559
- doc.addPage()
560
- }
545
+ function exportActivityReportToPDFDriverGroupByDay (doc, translations, reportData, userData) {
546
+ const headers = [translations.report.date,
547
+ translations.report.start,
548
+ translations.report.end,
549
+ translations.report.distance,
550
+ translations.report.avgSpeed + ' (Km/h)',
551
+ translations.report.maxSpeed + ' (Km/h)',
552
+ translations.report.engineHours]
553
+
554
+ const weekDays = [
555
+ translations.report.sunday,
556
+ translations.report.monday,
557
+ translations.report.tuesday,
558
+ translations.report.wednesday,
559
+ translations.report.thursday,
560
+ translations.report.friday,
561
+ translations.report.saturday
562
+ ]
563
+
564
+ reportData.drivers.forEach(function (d, index) {
565
+ const name = d.driver.name
566
+ const group = userData.groups.find(g => g.drivers.includes(d.id))
567
+
568
+ const space = index === 0 ? 8 : 0
569
+ if (index) {
570
+ doc.addPage()
571
+ }
561
572
 
562
- doc.setFontSize(13)
563
- doc.text(name, 20, space + 20)
564
- doc.setFontSize(11)
565
- doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
566
- doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
567
-
568
- if (!userData.allWeek && userData.weekDays) {
569
- doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '')
570
- + (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '')
571
- + (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '')
572
- + (userData.weekDays.thursday ? ' ' + translations.report.thursday + ',' : '')
573
- + (userData.weekDays.friday ? ' ' + translations.report.friday + ',' : '')
574
- + (userData.weekDays.saturday ? ' ' + translations.report.saturday + ',' : '')
575
- + (userData.weekDays.sunday ? ' ' + translations.report.sunday + ',' : '')
576
- + ' das '
577
- + userData.dayHours.startTime + ' - ' + userData.dayHours.endTime, 20, space + 30)
578
- }
573
+ doc.setFontSize(13)
574
+ doc.text(name, 20, space + 20)
575
+ doc.setFontSize(11)
576
+ doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
577
+ doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
578
+
579
+ if (!userData.allWeek && userData.weekDays) {
580
+ doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '') +
581
+ (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '') +
582
+ (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '') +
583
+ (userData.weekDays.thursday ? ' ' + translations.report.thursday + ',' : '') +
584
+ (userData.weekDays.friday ? ' ' + translations.report.friday + ',' : '') +
585
+ (userData.weekDays.saturday ? ' ' + translations.report.saturday + ',' : '') +
586
+ (userData.weekDays.sunday ? ' ' + translations.report.sunday + ',' : '') +
587
+ ' das ' +
588
+ userData.dayHours.startTime + ' - ' + userData.dayHours.endTime, 20, space + 30)
589
+ }
579
590
 
580
- const data = []
581
- d.summary.forEach(s => {
582
- const temp = [
583
- new Date(s.date).toLocaleDateString() + ' - ' + weekDays[s.date.getDay()],
584
- s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
585
- s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
586
- Number((s.distance / 1000).toFixed(2)),
587
- Math.round(s.averageSpeed * 1.85200),
588
- Math.round(s.maxSpeed * 1.85200),
589
- convertMS(s.engineHours)
590
- ]
591
-
592
- data.push(temp)
593
- })
591
+ const data = []
592
+ d.summary.forEach(s => {
593
+ const temp = [
594
+ new Date(s.date).toLocaleDateString() + ' - ' + weekDays[s.date.getDay()],
595
+ s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
596
+ s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
597
+ Number((s.distance / 1000).toFixed(2)),
598
+ Math.round(s.averageSpeed * 1.85200),
599
+ Math.round(s.maxSpeed * 1.85200),
600
+ convertMS(s.engineHours)
601
+ ]
602
+
603
+ data.push(temp)
604
+ })
594
605
 
595
- const footValues = ['Total:' + d.summary.length,
596
- '','',
597
- (d.summary.reduce((a, b) => a + b.distance, 0) / 1000).toFixed(0),
598
- getSumAvgSpeed(d.summary, userData),
599
- getSumMaxSpeed(d.summary, userData),
600
- convertMS(d.summary.reduce((a, b) => a + b.engineHours, 0))]
606
+ const footValues = ['Total:' + d.summary.length,
607
+ '', '',
608
+ (d.summary.reduce((a, b) => a + b.distance, 0) / 1000).toFixed(0),
609
+ getSumAvgSpeed(d.summary, userData),
610
+ getSumMaxSpeed(d.summary, userData),
611
+ convertMS(d.summary.reduce((a, b) => a + b.engineHours, 0))]
601
612
 
602
- const style = getStyle(getUserPartner(userData.user))
603
- addTable(doc, headers, data, footValues, style, 40)
604
- })
613
+ const style = getStyle(getUserPartner(userData.user))
614
+ addTable(doc, headers, data, footValues, style, 40)
615
+ })
605
616
  }
606
617
 
607
- function exportActivityReportToExcel_Driver_GroupByDay(settings, translations, reportData, userData) {
608
- const headers = [{label: translations.report.driver, value: 'name'},
609
- {label: translations.report.group, value: 'group'},
610
- {label: translations.report.date, value: 'date'},
611
- {label: translations.report.weekDay, value: 'weekday'},
612
- {label: translations.report.start, value: 'start'},
613
- {label: translations.report.end, value: 'end'},
614
- {label: translations.report.distance, value: 'distance'},
615
- {label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed'},
616
- {label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed'},
617
- {label: translations.report.engineHours, value: 'engineHours'}]
618
-
619
- const weekDays = [
620
- translations.report.sunday,
621
- translations.report.monday,
622
- translations.report.tuesday,
623
- translations.report.wednesday,
624
- translations.report.thursday,
625
- translations.report.friday,
626
- translations.report.saturday
627
- ]
618
+ function exportActivityReportToExcelDriverGroupByDay (settings, translations, reportData, userData) {
619
+ const headers = [{ label: translations.report.driver, value: 'name' },
620
+ { label: translations.report.group, value: 'group' },
621
+ { label: translations.report.date, value: 'date' },
622
+ { label: translations.report.weekDay, value: 'weekday' },
623
+ { label: translations.report.start, value: 'start' },
624
+ { label: translations.report.end, value: 'end' },
625
+ { label: translations.report.distance, value: 'distance' },
626
+ { label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed' },
627
+ { label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed' },
628
+ { label: translations.report.engineHours, value: 'engineHours' }]
629
+
630
+ const weekDays = [
631
+ translations.report.sunday,
632
+ translations.report.monday,
633
+ translations.report.tuesday,
634
+ translations.report.wednesday,
635
+ translations.report.thursday,
636
+ translations.report.friday,
637
+ translations.report.saturday
638
+ ]
639
+
640
+ let data = []
641
+ if (reportData.drivers) {
642
+ reportData.drivers.forEach(d => {
643
+ const group = userData.groups.find(g => g.drivers.includes(d.id))
628
644
 
629
- let data = []
630
- if (reportData.drivers) {
631
- reportData.drivers.forEach(d => {
632
- const group = userData.groups.find(g => g.drivers.includes(d.id))
633
-
634
- data = data.concat([{}])
635
- data = data.concat(d.summary.map(s => {
636
- return {
637
- name: d.driver.name,
638
- group: group ? group.name : '',
639
- date: new Date(s.date).toLocaleDateString(),
640
- weekday: weekDays[s.date.getDay()],
641
- start: s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
642
- end:s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
643
- distance: Number((s.distance / 1000).toFixed(0)),
644
- avgSpeed: Math.round(s.averageSpeed * 1.85200),
645
- maxSpeed: Math.round(s.maxSpeed * 1.85200),
646
- engineHours: convertMS(s.engineHours)
647
- }
648
- }))
649
- })
650
- console.log(data)
645
+ data = data.concat([{}])
646
+ data = data.concat(d.summary.map(s => {
651
647
  return {
652
- headers,
653
- data,
654
- settings
648
+ name: d.driver.name,
649
+ group: group ? group.name : '',
650
+ date: new Date(s.date).toLocaleDateString(),
651
+ weekday: weekDays[s.date.getDay()],
652
+ start: s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
653
+ end: s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
654
+ distance: Number((s.distance / 1000).toFixed(0)),
655
+ avgSpeed: Math.round(s.averageSpeed * 1.85200),
656
+ maxSpeed: Math.round(s.maxSpeed * 1.85200),
657
+ engineHours: convertMS(s.engineHours)
655
658
  }
659
+ }))
660
+ })
661
+ console.log(data)
662
+ return {
663
+ headers,
664
+ data,
665
+ settings
656
666
  }
667
+ }
657
668
  }
658
669
 
659
- function exportActivityReportToExcel_Driver(settings, translations, reportData) {
660
- const headers = [{label: translations.report.driver, value: 'driver'},
661
- {label: translations.report.start, value: 'start'},
662
- {label: translations.report.end, value: 'end'},
663
- {label: translations.report.distance, value: 'distance'},
664
- {label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed'},
665
- {label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed'},
666
- {label: translations.report.driverHours, value: 'engineHours'}]
667
-
668
- let data = []
669
- if (reportData.drivers) {
670
- reportData.drivers.forEach(d => {
671
- data = data.concat([{
672
- driver: d.driver.name,
673
- start: d.summary[0].startTime ? new Date(d.summary[0].startTime).toLocaleString() : '',
674
- end: d.summary[0].endTime ? new Date(d.summary[0].endTime).toLocaleString() : '',
675
- distance: Number((d.summary[0].distance / 1000).toFixed(0)),
676
- avgSpeed: Math.round(d.summary[0].averageSpeed * 1.85200),
677
- maxSpeed: Math.round(d.summary[0].maxSpeed * 1.85200),
678
- engineHours: convertMS(d.summary[0].engineHours)
679
- }])
680
- })
681
- console.log(data)
682
- return {
683
- headers,
684
- data,
685
- settings
686
- }
670
+ function exportActivityReportToExcelDriver (settings, translations, reportData) {
671
+ const headers = [{ label: translations.report.driver, value: 'driver' },
672
+ { label: translations.report.start, value: 'start' },
673
+ { label: translations.report.end, value: 'end' },
674
+ { label: translations.report.distance, value: 'distance' },
675
+ { label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed' },
676
+ { label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed' },
677
+ { label: translations.report.driverHours, value: 'engineHours' }]
678
+
679
+ let data = []
680
+ if (reportData.drivers) {
681
+ reportData.drivers.forEach(d => {
682
+ data = data.concat([{
683
+ driver: d.driver.name,
684
+ start: d.summary[0].startTime ? new Date(d.summary[0].startTime).toLocaleString() : '',
685
+ end: d.summary[0].endTime ? new Date(d.summary[0].endTime).toLocaleString() : '',
686
+ distance: Number((d.summary[0].distance / 1000).toFixed(0)),
687
+ avgSpeed: Math.round(d.summary[0].averageSpeed * 1.85200),
688
+ maxSpeed: Math.round(d.summary[0].maxSpeed * 1.85200),
689
+ engineHours: convertMS(d.summary[0].engineHours)
690
+ }])
691
+ })
692
+ console.log(data)
693
+ return {
694
+ headers,
695
+ data,
696
+ settings
687
697
  }
698
+ }
688
699
  }
689
700
 
690
- function exportActivityReportToExcel_Vehicle(settings, translations, reportData, userData) {
691
- const headers = [{label: translations.report.vehicle, value: 'name'},
692
- {label: translations.report.group, value: 'group'},
693
- {label: translations.report.start, value: 'start'},
694
- {label: translations.report.end, value: 'end'},
695
- {label: translations.report.distance, value: 'distance'},
696
- {label: translations.report.start + ' ' + translations.report.odometer, value: 'startOdometer'},
697
- {label: translations.report.end + ' ' + translations.report.odometer, value: 'endOdometer'},
698
- {label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed'},
699
- {label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed'},
700
- {label: translations.report.engineHours, value: 'engineHours'},
701
- {label: translations.report.spentFuel, value: 'spentFuel'}]
702
-
703
- if (reportData.devices) {
704
- let data = []
705
- reportData.devices.forEach(d => {
706
- const group = userData.groups.find(g => d.device.groupId === g.id)
707
-
708
- data = data.concat([{
709
- name: d.summary[0].deviceName,
710
- group: group ? group.name : '',
711
- start: d.summary[0].startTime ? new Date(d.summary[0].startTime).toLocaleString() : '',
712
- end: d.summary[0].endTime ? new Date(d.summary[0].endTime).toLocaleString() : '',
713
- distance: Number((d.summary[0].distance / 1000).toFixed(0)),
714
- startOdometer: Number((d.summary[0].startOdometer / 1000).toFixed(0)),
715
- endOdometer: Number((d.summary[0].endOdometer / 1000).toFixed(0)),
716
- avgSpeed: Math.round(d.summary[0].averageSpeed * 1.85200),
717
- maxSpeed: Math.round(d.summary[0].maxSpeed * 1.85200),
718
- engineHours: convertMS(d.summary[0].engineHours),
719
- spentFuel: d.summary[0].convertedSpentFuel >= 0 ? d.summary[0].convertedSpentFuel : 0,
720
- }])
721
- })
722
- console.log(data)
723
- return {
724
- headers,
725
- data,
726
- settings
727
- }
701
+ function exportActivityReportToExcelVehicle (settings, translations, reportData, userData) {
702
+ const headers = [{ label: translations.report.vehicle, value: 'name' },
703
+ { label: translations.report.group, value: 'group' },
704
+ { label: translations.report.start, value: 'start' },
705
+ { label: translations.report.end, value: 'end' },
706
+ { label: translations.report.distance, value: 'distance' },
707
+ { label: translations.report.start + ' ' + translations.report.odometer, value: 'startOdometer' },
708
+ { label: translations.report.end + ' ' + translations.report.odometer, value: 'endOdometer' },
709
+ { label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed' },
710
+ { label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed' },
711
+ { label: translations.report.engineHours, value: 'engineHours' },
712
+ { label: translations.report.spentFuel, value: 'spentFuel' }]
713
+
714
+ if (reportData.devices) {
715
+ let data = []
716
+ reportData.devices.forEach(d => {
717
+ const group = userData.groups.find(g => d.device.groupId === g.id)
718
+
719
+ data = data.concat([{
720
+ name: d.summary[0].deviceName,
721
+ group: group ? group.name : '',
722
+ start: d.summary[0].startTime ? new Date(d.summary[0].startTime).toLocaleString() : '',
723
+ end: d.summary[0].endTime ? new Date(d.summary[0].endTime).toLocaleString() : '',
724
+ distance: Number((d.summary[0].distance / 1000).toFixed(0)),
725
+ startOdometer: Number((d.summary[0].startOdometer / 1000).toFixed(0)),
726
+ endOdometer: Number((d.summary[0].endOdometer / 1000).toFixed(0)),
727
+ avgSpeed: Math.round(d.summary[0].averageSpeed * 1.85200),
728
+ maxSpeed: Math.round(d.summary[0].maxSpeed * 1.85200),
729
+ engineHours: convertMS(d.summary[0].engineHours),
730
+ spentFuel: d.summary[0].convertedSpentFuel >= 0 ? d.summary[0].convertedSpentFuel : 0
731
+ }])
732
+ })
733
+ console.log(data)
734
+ return {
735
+ headers,
736
+ data,
737
+ settings
728
738
  }
739
+ }
729
740
  }
730
741
 
731
- function exportActivityReportToExcel(userData, reportData) {
732
- console.log('Export to Excel')
733
- const translations = getTranslations(userData)
734
-
735
- const settings = {
736
- sheetName: translations.report.titleActivityReport, // The name of the sheet
737
- fileName: fileName // The name of the spreadsheet
738
- }
742
+ function exportActivityReportToExcel (userData, reportData) {
743
+ console.log('Export to Excel')
744
+ const translations = getTranslations(userData)
745
+
746
+ const settings = {
747
+ sheetName: translations.report.titleActivityReport, // The name of the sheet
748
+ fileName // The name of the spreadsheet
749
+ }
750
+
751
+ if (userData.groupByDay && userData.byDriver) {
752
+ return exportActivityReportToExcelDriverGroupByDay(settings, translations, reportData, userData)
753
+ } else if (userData.groupByDay) {
754
+ return exportActivityReportToExcelVehicleGroupByDay(settings, translations, reportData, userData)
755
+ } else if (userData.byDriver) {
756
+ return exportActivityReportToExcelDriver(settings, translations, reportData, userData)
757
+ } else {
758
+ return exportActivityReportToExcelVehicle(settings, translations, reportData, userData)
759
+ }
760
+ }
739
761
 
740
- if(userData.groupByDay && userData.byDriver){
741
- return exportActivityReportToExcel_Driver_GroupByDay(settings, translations, reportData, userData)
742
- } else if(userData.groupByDay){
743
- return exportActivityReportToExcel_Vehicle_GroupByDay(settings, translations, reportData, userData)
744
- } else if(userData.byDriver){
745
- return exportActivityReportToExcel_Driver(settings, translations, reportData, userData)
762
+ function getSumAvgSpeed (data, userData) {
763
+ const values = data.map(item => {
764
+ if (userData.groupByDay) {
765
+ return Math.round(item.averageSpeed * 1.85200)
746
766
  } else {
747
- return exportActivityReportToExcel_Vehicle(settings, translations, reportData, userData)
767
+ return Math.round(item.summary[0].averageSpeed * 1.85200)
748
768
  }
769
+ }).filter(v => v !== 0)
770
+ return Math.round((values.reduce((a, b) => a + b, 0)) / values.length)
749
771
  }
750
772
 
751
- function getSumAvgSpeed(data, userData){
752
- const values = data.map(item => {
753
- if(userData.groupByDay){
754
- return Math.round(item.averageSpeed * 1.85200)
755
- } else {
756
- return Math.round(item.summary[0].averageSpeed * 1.85200)
757
- }
758
- }).filter(v => v !== 0)
759
- return Math.round((values.reduce((a, b) => a + b, 0)) / values.length)
760
- }
761
-
762
- function getSumMaxSpeed(data, userData){
763
- const values = data.map(item => {
764
- if(userData.groupByDay){
765
- return item.maxSpeed
766
- } else {
767
- return item.summary[0].maxSpeed
768
- }
769
- })
770
- const max = values.reduce(function (a, b) {
771
- return Math.max(a, b);
772
- });
773
- return Math.round(max * 1.85200)
773
+ function getSumMaxSpeed (data, userData) {
774
+ const values = data.map(item => {
775
+ if (userData.groupByDay) {
776
+ return item.maxSpeed
777
+ } else {
778
+ return item.summary[0].maxSpeed
779
+ }
780
+ })
781
+ const max = values.reduce(function (a, b) {
782
+ return Math.max(a, b)
783
+ })
784
+ return Math.round(max * 1.85200)
774
785
  }
775
786
 
776
- function getSumSpentFuel(data, userData){
777
- const values = data.map(item => {
778
- if(userData.groupByDay){
779
- return item.convertedSpentFuel >= 0 ? item.convertedSpentFuel : 0
780
- } else {
781
- return item.summary[0].convertedSpentFuel >= 0 ? item.summary[0].convertedSpentFuel : 0
782
- }
783
- })
784
- return values.reduce((a, b) => a + b, 0)
787
+ function getSumSpentFuel (data, userData) {
788
+ const values = data.map(item => {
789
+ if (userData.groupByDay) {
790
+ return item.convertedSpentFuel >= 0 ? item.convertedSpentFuel : 0
791
+ } else {
792
+ return item.summary[0].convertedSpentFuel >= 0 ? item.summary[0].convertedSpentFuel : 0
793
+ }
794
+ })
795
+ return values.reduce((a, b) => a + b, 0)
785
796
  }
786
797
 
787
798
  exports.createActivityReport = createActivityReport