fleetmap-reports 1.0.399 → 1.0.402

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,797 @@
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)
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
- })
236
-
237
- return devicesResult
238
- }
237
+ }
238
+ }
239
239
 
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)
281
- }
282
- })
240
+ const deviceData = {
241
+ device: d,
242
+ summary
243
+ }
244
+ devicesResult.push(deviceData)
245
+ })
283
246
 
284
- return driversResult
247
+ return devicesResult
285
248
  }
286
249
 
287
- async function exportActivityReportToPDF(userData, reportData) {
288
- console.log('Export to PDF')
289
- const translations = getTranslations(userData)
290
-
291
- if (reportData.devices || reportData.drivers) {
292
- const doc = new jsPDF.jsPDF('l')
293
- await headerFromUser(doc, translations.report.titleActivityReport, userData.user)
294
-
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)
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
+ })
303
279
  }
304
-
305
- return doc
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)
306
291
  }
292
+ })
293
+
294
+ return driversResult
307
295
  }
308
296
 
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]
297
+ async function exportActivityReportToPDF (userData, reportData) {
298
+ console.log('Export to PDF')
299
+ const translations = getTranslations(userData)
317
300
 
318
- doc.setFontSize(10)
319
- doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, 30)
301
+ if (reportData.devices || reportData.drivers) {
302
+ const doc = new jsPDF.jsPDF('l')
303
+ await headerFromUser(doc, translations.report.titleActivityReport, userData.user)
320
304
 
321
- 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)
333
- })
334
-
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))]
305
+ if (userData.groupByDay && userData.byDriver) {
306
+ exportActivityReportToPDFDriverGroupByDay(doc, translations, reportData, userData)
307
+ } else if (userData.groupByDay) {
308
+ exportActivityReportToPDFVehicleGroupByDay(doc, translations, reportData, userData)
309
+ } else if (userData.byDriver) {
310
+ exportActivityReportToPDFDriver(doc, translations, reportData, userData)
311
+ } else {
312
+ exportActivityReportToPDFVehicle(doc, translations, reportData, userData)
313
+ }
341
314
 
342
- const style = getStyle(getUserPartner(userData.user))
343
- addTable(doc, headers, data, footValues, style, 35)
315
+ return doc
316
+ }
344
317
  }
345
318
 
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 = []
363
- 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)
319
+ function exportActivityReportToPDFDriver (doc, translations, reportData, userData) {
320
+ const headers = [translations.report.driver,
321
+ translations.report.start,
322
+ translations.report.end,
323
+ translations.report.distance,
324
+ translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)',
325
+ translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)',
326
+ translations.report.driverHours]
327
+
328
+ doc.setFontSize(10)
329
+ doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, 30)
330
+
331
+ const data = []
332
+ reportData.drivers.forEach(d => {
333
+ const temp = [
334
+ d.driver.name,
335
+ d.summary[0].startTime ? new Date(d.summary[0].startTime).toLocaleString() : '',
336
+ d.summary[0].endTime ? new Date(d.summary[0].endTime).toLocaleString() : '',
337
+ Number((d.summary[0].distance / 1000).toFixed(2)),
338
+ Math.round(d.summary[0].averageSpeed * 1.85200),
339
+ Math.round(d.summary[0].maxSpeed * 1.85200),
340
+ convertMS(d.summary[0].engineHours)
341
+ ]
342
+ data.push(temp)
343
+ })
344
+
345
+ const footValues = ['Total:' + reportData.drivers.length,
346
+ '', '',
347
+ (reportData.drivers.reduce((a, b) => a + b.summary[0].distance, 0) / 1000).toFixed(0),
348
+ getSumAvgSpeed(reportData.drivers, userData),
349
+ getSumMaxSpeed(reportData.drivers, userData),
350
+ convertMS(reportData.drivers.reduce((a, b) => a + b.summary[0].engineHours, 0))]
351
+
352
+ const style = getStyle(getUserPartner(userData.user))
353
+ addTable(doc, headers, data, footValues, style, 35)
393
354
  }
394
355
 
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
356
+ function exportActivityReportToPDFVehicle (doc, translations, reportData, userData) {
357
+ const headers = [translations.report.vehicle,
358
+ translations.report.group,
359
+ translations.report.start,
360
+ translations.report.end,
361
+ translations.report.distance,
362
+ translations.report.start + ' ' + translations.report.odometer,
363
+ translations.report.end + ' ' + translations.report.odometer,
364
+ translations.report.avgSpeed + ' (Km/h)',
365
+ translations.report.maxSpeed + ' (Km/h)',
366
+ translations.report.engineHours,
367
+ translations.report.spentFuel]
368
+
369
+ doc.setFontSize(10)
370
+ doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, 30)
371
+
372
+ const data = []
373
+ reportData.devices.forEach(d => {
374
+ const group = userData.groups.find(g => d.device.groupId === g.id)
375
+
376
+ const temp = [
377
+ d.summary[0].deviceName,
378
+ group ? group.name : '',
379
+ d.summary[0].startTime ? new Date(d.summary[0].startTime).toLocaleString() : '',
380
+ d.summary[0].endTime ? new Date(d.summary[0].endTime).toLocaleString() : '',
381
+ Number((d.summary[0].distance / 1000).toFixed(2)),
382
+ Number((d.summary[0].startOdometer / 1000).toFixed(2)),
383
+ Number((d.summary[0].endOdometer / 1000).toFixed(2)),
384
+ Math.round(d.summary[0].averageSpeed * 1.85200),
385
+ Math.round(d.summary[0].maxSpeed * 1.85200),
386
+ convertMS(d.summary[0].engineHours),
387
+ d.summary[0].convertedSpentFuel >= 0 ? d.summary[0].convertedSpentFuel : 0
415
388
  ]
389
+ data.push(temp)
390
+ })
391
+
392
+ const footValues = ['Total:' + reportData.devices.length,
393
+ '', '', '',
394
+ (reportData.devices.reduce((a, b) => a + b.summary[0].distance, 0) / 1000).toFixed(0),
395
+ '', '',
396
+ getSumAvgSpeed(reportData.devices, userData),
397
+ getSumMaxSpeed(reportData.devices, userData),
398
+ convertMS(reportData.devices.reduce((a, b) => a + b.summary[0].engineHours, 0)),
399
+ getSumSpentFuel(reportData.devices, userData)]
400
+
401
+ const style = getStyle(getUserPartner(userData.user))
402
+ addTable(doc, headers, data, footValues, style, 35)
403
+ }
416
404
 
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)
405
+ function exportActivityReportToPDFVehicleGroupByDay (doc, translations, reportData, userData) {
406
+ const headers = [translations.report.date,
407
+ translations.report.start,
408
+ translations.report.end,
409
+ translations.report.distance,
410
+ translations.report.start + ' ' + translations.report.odometer,
411
+ translations.report.end + ' ' + translations.report.odometer,
412
+ translations.report.avgSpeed + ' (Km/h)',
413
+ translations.report.maxSpeed + ' (Km/h)',
414
+ translations.report.engineHours,
415
+ translations.report.spentFuel]
416
+
417
+ const weekDays = [
418
+ translations.report.sunday,
419
+ translations.report.monday,
420
+ translations.report.tuesday,
421
+ translations.report.wednesday,
422
+ translations.report.thursday,
423
+ translations.report.friday,
424
+ translations.report.saturday
425
+ ]
426
+
427
+ reportData.devices.forEach(function (d, index) {
428
+ const name = userData.byDriver ? d.driver.name : d.device.name
429
+ const group = userData.byDriver ? userData.groups.find(g => g.drivers.includes(d.id)) : userData.groups.find(g => d.device.groupId === g.id)
430
+
431
+ const space = index === 0 ? 8 : 0
432
+ if (index) {
433
+ doc.addPage()
434
+ }
420
435
 
421
- let space = index === 0 ? 8 : 0
422
- if(index){
423
- doc.addPage()
424
- }
436
+ doc.setFontSize(13)
437
+ doc.text(name, 20, space + 20)
438
+ doc.setFontSize(11)
439
+ doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
440
+ doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
441
+
442
+ if (!userData.allWeek && userData.weekDays) {
443
+ doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '') +
444
+ (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '') +
445
+ (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '') +
446
+ (userData.weekDays.thursday ? ' ' + translations.report.thursday + ',' : '') +
447
+ (userData.weekDays.friday ? ' ' + translations.report.friday + ',' : '') +
448
+ (userData.weekDays.saturday ? ' ' + translations.report.saturday + ',' : '') +
449
+ (userData.weekDays.sunday ? ' ' + translations.report.sunday + ',' : '') +
450
+ ' das ' +
451
+ userData.dayHours.startTime + ' - ' + userData.dayHours.endTime, 20, space + 30)
452
+ }
425
453
 
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
- }
454
+ const data = []
455
+ d.summary.forEach(s => {
456
+ const temp = [
457
+ new Date(s.date).toLocaleDateString() + ' - ' + weekDays[s.date.getDay()],
458
+ s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
459
+ s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
460
+ Number((s.distance / 1000).toFixed(2)),
461
+ Number((s.startOdometer / 1000).toFixed(2)),
462
+ Number((s.endOdometer / 1000).toFixed(2)),
463
+ Math.round(s.averageSpeed * 1.85200),
464
+ Math.round(s.maxSpeed * 1.85200),
465
+ convertMS(s.engineHours),
466
+ s.convertedSpentFuel >= 0 ? s.convertedSpentFuel : 0
467
+ ]
468
+
469
+ data.push(temp)
470
+ })
443
471
 
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
- })
472
+ const footValues = ['Total:' + d.summary.length,
473
+ '', '',
474
+ (d.summary.reduce((a, b) => a + b.distance, 0) / 1000).toFixed(0),
475
+ '', '',
476
+ getSumAvgSpeed(d.summary, userData),
477
+ getSumMaxSpeed(d.summary, userData),
478
+ convertMS(d.summary.reduce((a, b) => a + b.engineHours, 0)),
479
+ getSumSpentFuel(d.summary, userData)]
461
480
 
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
- })
481
+ const style = getStyle(getUserPartner(userData.user))
482
+ addTable(doc, headers, data, footValues, style, 40)
483
+ })
474
484
  }
475
485
 
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
- ]
486
+ function exportActivityReportToExcelVehicleGroupByDay (settings, translations, reportData, userData) {
487
+ const headers = [{ label: translations.report.vehicle, value: 'name' },
488
+ { label: translations.report.group, value: 'group' },
489
+ { label: translations.report.date, value: 'date' },
490
+ { label: translations.report.weekDay, value: 'weekday' },
491
+ { label: translations.report.start, value: 'start' },
492
+ { label: translations.report.end, value: 'end' },
493
+ { label: translations.report.distance, value: 'distance' },
494
+ { label: translations.report.start + ' ' + translations.report.odometer, value: 'startOdometer' },
495
+ { label: translations.report.end + ' ' + translations.report.odometer, value: 'endOdometer' },
496
+ { label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed' },
497
+ { label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed' },
498
+ { label: translations.report.engineHours, value: 'engineHours' },
499
+ { label: translations.report.spentFuel, value: 'spentFuel' }]
500
+
501
+ const weekDays = [
502
+ translations.report.sunday,
503
+ translations.report.monday,
504
+ translations.report.tuesday,
505
+ translations.report.wednesday,
506
+ translations.report.thursday,
507
+ translations.report.friday,
508
+ translations.report.saturday
509
+ ]
510
+
511
+ let data = []
512
+ if (reportData.devices) {
513
+ reportData.devices.forEach(d => {
514
+ const group = userData.groups.find(g => d.device.groupId === g.id)
500
515
 
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)
516
+ data = data.concat([{}])
517
+ data = data.concat(d.summary.map(s => {
526
518
  return {
527
- headers,
528
- data,
529
- settings
519
+ name: s.deviceName,
520
+ group: group ? group.name : '',
521
+ date: new Date(s.date).toLocaleDateString(),
522
+ weekday: weekDays[s.date.getDay()],
523
+ start: s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
524
+ end: s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
525
+ distance: Number((s.distance / 1000).toFixed(0)),
526
+ startOdometer: Number((s.startOdometer / 1000).toFixed(0)),
527
+ endOdometer: Number((s.endOdometer / 1000).toFixed(0)),
528
+ avgSpeed: Math.round(s.averageSpeed * 1.85200),
529
+ maxSpeed: Math.round(s.maxSpeed * 1.85200),
530
+ engineHours: convertMS(s.engineHours),
531
+ spentFuel: s.convertedSpentFuel >= 0 ? s.convertedSpentFuel : 0
530
532
  }
533
+ }))
534
+ })
535
+ console.log(data)
536
+ return {
537
+ headers,
538
+ data,
539
+ settings
531
540
  }
541
+ }
532
542
  }
533
543
 
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
- }
544
+ function exportActivityReportToPDFDriverGroupByDay (doc, translations, reportData, userData) {
545
+ const headers = [translations.report.date,
546
+ translations.report.start,
547
+ translations.report.end,
548
+ translations.report.distance,
549
+ translations.report.avgSpeed + ' (Km/h)',
550
+ translations.report.maxSpeed + ' (Km/h)',
551
+ translations.report.engineHours]
552
+
553
+ const weekDays = [
554
+ translations.report.sunday,
555
+ translations.report.monday,
556
+ translations.report.tuesday,
557
+ translations.report.wednesday,
558
+ translations.report.thursday,
559
+ translations.report.friday,
560
+ translations.report.saturday
561
+ ]
562
+
563
+ reportData.drivers.forEach(function (d, index) {
564
+ const name = d.driver.name
565
+ const group = userData.groups.find(g => g.drivers.includes(d.id))
566
+
567
+ const space = index === 0 ? 8 : 0
568
+ if (index) {
569
+ doc.addPage()
570
+ }
561
571
 
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
- }
572
+ doc.setFontSize(13)
573
+ doc.text(name, 20, space + 20)
574
+ doc.setFontSize(11)
575
+ doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
576
+ doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
577
+
578
+ if (!userData.allWeek && userData.weekDays) {
579
+ doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '') +
580
+ (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '') +
581
+ (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '') +
582
+ (userData.weekDays.thursday ? ' ' + translations.report.thursday + ',' : '') +
583
+ (userData.weekDays.friday ? ' ' + translations.report.friday + ',' : '') +
584
+ (userData.weekDays.saturday ? ' ' + translations.report.saturday + ',' : '') +
585
+ (userData.weekDays.sunday ? ' ' + translations.report.sunday + ',' : '') +
586
+ ' das ' +
587
+ userData.dayHours.startTime + ' - ' + userData.dayHours.endTime, 20, space + 30)
588
+ }
579
589
 
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
- })
590
+ const data = []
591
+ d.summary.forEach(s => {
592
+ const temp = [
593
+ new Date(s.date).toLocaleDateString() + ' - ' + weekDays[s.date.getDay()],
594
+ s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
595
+ s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
596
+ Number((s.distance / 1000).toFixed(2)),
597
+ Math.round(s.averageSpeed * 1.85200),
598
+ Math.round(s.maxSpeed * 1.85200),
599
+ convertMS(s.engineHours)
600
+ ]
601
+
602
+ data.push(temp)
603
+ })
594
604
 
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))]
605
+ const footValues = ['Total:' + d.summary.length,
606
+ '', '',
607
+ (d.summary.reduce((a, b) => a + b.distance, 0) / 1000).toFixed(0),
608
+ getSumAvgSpeed(d.summary, userData),
609
+ getSumMaxSpeed(d.summary, userData),
610
+ convertMS(d.summary.reduce((a, b) => a + b.engineHours, 0))]
601
611
 
602
- const style = getStyle(getUserPartner(userData.user))
603
- addTable(doc, headers, data, footValues, style, 40)
604
- })
612
+ const style = getStyle(getUserPartner(userData.user))
613
+ addTable(doc, headers, data, footValues, style, 40)
614
+ })
605
615
  }
606
616
 
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
- ]
617
+ function exportActivityReportToExcelDriverGroupByDay (settings, translations, reportData, userData) {
618
+ const headers = [{ label: translations.report.driver, value: 'name' },
619
+ { label: translations.report.group, value: 'group' },
620
+ { label: translations.report.date, value: 'date' },
621
+ { label: translations.report.weekDay, value: 'weekday' },
622
+ { label: translations.report.start, value: 'start' },
623
+ { label: translations.report.end, value: 'end' },
624
+ { label: translations.report.distance, value: 'distance' },
625
+ { label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed' },
626
+ { label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed' },
627
+ { label: translations.report.engineHours, value: 'engineHours' }]
628
+
629
+ const weekDays = [
630
+ translations.report.sunday,
631
+ translations.report.monday,
632
+ translations.report.tuesday,
633
+ translations.report.wednesday,
634
+ translations.report.thursday,
635
+ translations.report.friday,
636
+ translations.report.saturday
637
+ ]
638
+
639
+ let data = []
640
+ if (reportData.drivers) {
641
+ reportData.drivers.forEach(d => {
642
+ const group = userData.groups.find(g => g.drivers.includes(d.id))
628
643
 
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)
644
+ data = data.concat([{}])
645
+ data = data.concat(d.summary.map(s => {
651
646
  return {
652
- headers,
653
- data,
654
- settings
647
+ name: d.driver.name,
648
+ group: group ? group.name : '',
649
+ date: new Date(s.date).toLocaleDateString(),
650
+ weekday: weekDays[s.date.getDay()],
651
+ start: s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
652
+ end: s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
653
+ distance: Number((s.distance / 1000).toFixed(0)),
654
+ avgSpeed: Math.round(s.averageSpeed * 1.85200),
655
+ maxSpeed: Math.round(s.maxSpeed * 1.85200),
656
+ engineHours: convertMS(s.engineHours)
655
657
  }
658
+ }))
659
+ })
660
+ console.log(data)
661
+ return {
662
+ headers,
663
+ data,
664
+ settings
656
665
  }
666
+ }
657
667
  }
658
668
 
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
- }
669
+ function exportActivityReportToExcelDriver (settings, translations, reportData) {
670
+ const headers = [{ label: translations.report.driver, value: 'driver' },
671
+ { label: translations.report.start, value: 'start' },
672
+ { label: translations.report.end, value: 'end' },
673
+ { label: translations.report.distance, value: 'distance' },
674
+ { label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed' },
675
+ { label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed' },
676
+ { label: translations.report.driverHours, value: 'engineHours' }]
677
+
678
+ let data = []
679
+ if (reportData.drivers) {
680
+ reportData.drivers.forEach(d => {
681
+ data = data.concat([{
682
+ driver: d.driver.name,
683
+ start: d.summary[0].startTime ? new Date(d.summary[0].startTime).toLocaleString() : '',
684
+ end: d.summary[0].endTime ? new Date(d.summary[0].endTime).toLocaleString() : '',
685
+ distance: Number((d.summary[0].distance / 1000).toFixed(0)),
686
+ avgSpeed: Math.round(d.summary[0].averageSpeed * 1.85200),
687
+ maxSpeed: Math.round(d.summary[0].maxSpeed * 1.85200),
688
+ engineHours: convertMS(d.summary[0].engineHours)
689
+ }])
690
+ })
691
+ console.log(data)
692
+ return {
693
+ headers,
694
+ data,
695
+ settings
687
696
  }
697
+ }
688
698
  }
689
699
 
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
- }
700
+ function exportActivityReportToExcelVehicle (settings, translations, reportData, userData) {
701
+ const headers = [{ label: translations.report.vehicle, value: 'name' },
702
+ { label: translations.report.group, value: 'group' },
703
+ { label: translations.report.start, value: 'start' },
704
+ { label: translations.report.end, value: 'end' },
705
+ { label: translations.report.distance, value: 'distance' },
706
+ { label: translations.report.start + ' ' + translations.report.odometer, value: 'startOdometer' },
707
+ { label: translations.report.end + ' ' + translations.report.odometer, value: 'endOdometer' },
708
+ { label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed' },
709
+ { label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed' },
710
+ { label: translations.report.engineHours, value: 'engineHours' },
711
+ { label: translations.report.spentFuel, value: 'spentFuel' }]
712
+
713
+ if (reportData.devices) {
714
+ let data = []
715
+ reportData.devices.forEach(d => {
716
+ const group = userData.groups.find(g => d.device.groupId === g.id)
717
+
718
+ data = data.concat([{
719
+ name: d.summary[0].deviceName,
720
+ group: group ? group.name : '',
721
+ start: d.summary[0].startTime ? new Date(d.summary[0].startTime).toLocaleString() : '',
722
+ end: d.summary[0].endTime ? new Date(d.summary[0].endTime).toLocaleString() : '',
723
+ distance: Number((d.summary[0].distance / 1000).toFixed(0)),
724
+ startOdometer: Number((d.summary[0].startOdometer / 1000).toFixed(0)),
725
+ endOdometer: Number((d.summary[0].endOdometer / 1000).toFixed(0)),
726
+ avgSpeed: Math.round(d.summary[0].averageSpeed * 1.85200),
727
+ maxSpeed: Math.round(d.summary[0].maxSpeed * 1.85200),
728
+ engineHours: convertMS(d.summary[0].engineHours),
729
+ spentFuel: d.summary[0].convertedSpentFuel >= 0 ? d.summary[0].convertedSpentFuel : 0
730
+ }])
731
+ })
732
+ console.log(data)
733
+ return {
734
+ headers,
735
+ data,
736
+ settings
728
737
  }
738
+ }
729
739
  }
730
740
 
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
- }
741
+ function exportActivityReportToExcel (userData, reportData) {
742
+ console.log('Export to Excel')
743
+ const translations = getTranslations(userData)
744
+
745
+ const settings = {
746
+ sheetName: translations.report.titleActivityReport, // The name of the sheet
747
+ fileName // The name of the spreadsheet
748
+ }
749
+
750
+ if (userData.groupByDay && userData.byDriver) {
751
+ return exportActivityReportToExcelDriverGroupByDay(settings, translations, reportData, userData)
752
+ } else if (userData.groupByDay) {
753
+ return exportActivityReportToExcelVehicleGroupByDay(settings, translations, reportData, userData)
754
+ } else if (userData.byDriver) {
755
+ return exportActivityReportToExcelDriver(settings, translations, reportData, userData)
756
+ } else {
757
+ return exportActivityReportToExcelVehicle(settings, translations, reportData, userData)
758
+ }
759
+ }
739
760
 
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)
761
+ function getSumAvgSpeed (data, userData) {
762
+ const values = data.map(item => {
763
+ if (userData.groupByDay) {
764
+ return Math.round(item.averageSpeed * 1.85200)
746
765
  } else {
747
- return exportActivityReportToExcel_Vehicle(settings, translations, reportData, userData)
766
+ return Math.round(item.summary[0].averageSpeed * 1.85200)
748
767
  }
768
+ }).filter(v => v !== 0)
769
+ return Math.round((values.reduce((a, b) => a + b, 0)) / values.length)
749
770
  }
750
771
 
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)
772
+ function getSumMaxSpeed (data, userData) {
773
+ const values = data.map(item => {
774
+ if (userData.groupByDay) {
775
+ return item.maxSpeed
776
+ } else {
777
+ return item.summary[0].maxSpeed
778
+ }
779
+ })
780
+ const max = values.reduce(function (a, b) {
781
+ return Math.max(a, b)
782
+ })
783
+ return Math.round(max * 1.85200)
774
784
  }
775
785
 
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)
786
+ function getSumSpentFuel (data, userData) {
787
+ const values = data.map(item => {
788
+ if (userData.groupByDay) {
789
+ return item.convertedSpentFuel >= 0 ? item.convertedSpentFuel : 0
790
+ } else {
791
+ return item.summary[0].convertedSpentFuel >= 0 ? item.summary[0].convertedSpentFuel : 0
792
+ }
793
+ })
794
+ return values.reduce((a, b) => a + b, 0)
785
795
  }
786
796
 
787
797
  exports.createActivityReport = createActivityReport