fleetmap-reports 1.0.248 → 1.0.252

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,40 +1,44 @@
1
1
  const automaticReports = require("./automaticReports")
2
2
  const messages = require('../lang')
3
- const {convertMS} = require("./util/utils")
3
+ const {convertMS, getDates} = require("./util/utils")
4
4
  const jsPDF = require('jspdf')
5
5
  require('jspdf-autotable')
6
- const drivers = require("./util/driver");
7
- const {headerFromUser} = require("./util/pdfDocument");
8
- const {getStyle} = require("./reportStyle");
9
- const {getUserPartner} = require("fleetmap-partners");
6
+ const drivers = require("./util/driver")
7
+ const {headerFromUser, addTable} = require("./util/pdfDocument")
8
+ const {getStyle} = require("./reportStyle")
9
+ const {getUserPartner} = require("fleetmap-partners")
10
+ const traccar = require("./util/traccar")
11
+ const {createKmsReport} = require("./kms-report");
10
12
 
11
13
  let traccarInstance
14
+ let userData
12
15
 
13
16
  const fileName = 'ActivityReport'
14
17
 
15
- async function createActivityReport(from, to, userData, traccar) {
18
+ async function createActivityReport(from, to, reportUserData, traccar) {
16
19
  console.log('Create ActivityReport')
17
20
  traccarInstance = traccar
21
+ userData = reportUserData
18
22
  const reportData = []
19
23
 
20
24
  if(userData.byDriver){
21
- const allData = await createActivityReportByDriver(from, to, userData)
25
+ const allData = await createActivityReportByDriver(from, to)
22
26
  reportData.push(allData)
23
27
  }
24
28
  else if(userData.byGroup){
25
- const allGroupsData = await createActivityReportByGroup(from, to, userData)
29
+ const allGroupsData = await createActivityReportByGroup(from, to)
26
30
  allGroupsData.forEach(data => reportData.push(data))
27
- const withoutGroupDevices = await createActivityReportByDevice(from, to, userData)
31
+ const withoutGroupDevices = await createActivityReportByDevice(from, to)
28
32
  reportData.push(withoutGroupDevices)
29
33
  } else {
30
- const allData = await createActivityReportByDevice(from, to, userData)
34
+ const allData = await createActivityReportByDevice(from, to)
31
35
  reportData.push(allData)
32
36
  }
33
37
 
34
38
  return reportData
35
39
  }
36
40
 
37
- async function createActivityReportByGroup(from, to, userData){
41
+ async function createActivityReportByGroup(from, to){
38
42
  console.log("ByGroup")
39
43
  const reportData = []
40
44
  for (const g of userData.groups) {
@@ -69,7 +73,7 @@ async function createActivityReportByGroup(from, to, userData){
69
73
  return reportData
70
74
  }
71
75
 
72
- async function createActivityReportByDevice(from, to, userData){
76
+ async function createActivityReportByDevice(from, to){
73
77
  const groupIds = userData.groups.map(g => g.id)
74
78
  const devicesToProcess = userData.byGroup ? userData.devices.filter(d => !groupIds.includes(d.groupId)) : userData.devices
75
79
 
@@ -82,34 +86,61 @@ async function createActivityReportByDevice(from, to, userData){
82
86
 
83
87
  //Get report data from traccar
84
88
  devicesToProcess.sort((a, b) => (a.name > b.name) ? 1 : -1)
85
- const devicesToSlice = devicesToProcess.slice()
86
- const arrayOfArrays = automaticReports.sliceArray(devicesToSlice)
87
- let data = []
88
- let trips = []
89
- for (const a of arrayOfArrays) {
90
- const responseData = await traccarInstance.reports.reportsSummaryGet(from, to, a.map(d => d.id))
91
- data = data.concat(responseData.data)
92
89
 
93
- const responseTrips = await traccarInstance.reports.reportsTripsGet(from, to, a.map(d => d.id))
94
- trips = trips.concat(responseTrips.data)
90
+ let data = []
91
+ if(userData.groupByDay) {
92
+ const dates = getDates(from, to)
93
+ for(const date of dates){
94
+ if(userData.allWeek){
95
+ const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
96
+ const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
97
+
98
+ const summary = await traccar.getSummary(traccarInstance, fromByDay, toByDay, devicesToProcess)
99
+ summary.forEach(s => s.date = date)
100
+ data = data.concat(summary)
101
+ } else {
102
+ if((date.getDay() === 0 && userData.weekDays.sunday) ||
103
+ (date.getDay() === 1 && userData.weekDays.monday) ||
104
+ (date.getDay() === 2 && userData.weekDays.tuesday) ||
105
+ (date.getDay() === 3 && userData.weekDays.wednesday) ||
106
+ (date.getDay() === 4 && userData.weekDays.thursday) ||
107
+ (date.getDay() === 5 && userData.weekDays.friday) ||
108
+ (date.getDay() === 6 && userData.weekDays.saturday)) {
109
+
110
+ const fromByDay = new Date(new Date(date).toISOString().split('T')[0] + ' ' + userData.dayHours.startTime)
111
+ const toByDay = new Date(new Date(date).toISOString().split('T')[0] + ' ' + userData.dayHours.endTime)
112
+
113
+ const summary = await traccar.getSummary(traccarInstance, fromByDay, toByDay, devicesToProcess)
114
+ summary.forEach(s => s.date = date)
115
+ data = data.concat(summary)
116
+ }else {
117
+ data = data.concat({date: date})
118
+ }
119
+ }
120
+ }
121
+ } else {
122
+ data = await traccar.getSummary(traccarInstance, from, to, devicesToProcess)
95
123
  }
96
124
 
125
+ // use the km report to calculate kms
126
+ const kmsReport = await createKmsReport(from, to, userData, traccarInstance)
127
+
97
128
  console.log('Summary:' + data.length)
98
129
 
99
130
  //Process report data
100
131
  if (data.length > 0) {
101
- allData.devices = processDevices(devicesToProcess, data, trips)
132
+ allData.devices = processDevices(devicesToProcess, data, kmsReport)
102
133
  }
103
134
 
104
135
  return allData
105
136
  }
106
137
 
107
- async function createActivityReportByDriver(from, to, userData){
138
+ async function createActivityReportByDriver(from, to){
108
139
  const deviceIds = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
109
140
  console.log(deviceIds)
110
141
  let tripsData = []
111
142
  if(deviceIds.length > 0) {
112
- const response = await traccarInstance.reports.reportsTripsGet(from, to, deviceIds)
143
+ const response = await traccar.getTrips(traccarInstance, from, to, deviceIds)
113
144
  tripsData = response.data
114
145
  }
115
146
 
@@ -125,18 +156,23 @@ async function createActivityReportByDriver(from, to, userData){
125
156
  }
126
157
 
127
158
 
128
- function processDevices(devices, data, trips) {
159
+ function processDevices(devices, data, kmsReport) {
129
160
  const devicesResult = []
130
161
 
131
162
  devices.forEach(d => {
132
163
  const summary = data.filter(s => s.deviceId === d.id)
133
- const deviceTrips = trips.filter(t => t.deviceId === d.id)
134
164
  if (summary) {
135
- summary[0].convertedSpentFuel = automaticReports.calculateSpentFuel(summary[0].spentFuel, d)
136
- summary[0].distance = deviceTrips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
165
+ summary.forEach(s => {
166
+ const deviceKms = kmsReport[0].devices.filter(r => r.device.id === d.id)
167
+
168
+ console.log(deviceKms)
169
+
170
+ s.convertedSpentFuel = automaticReports.calculateSpentFuel(s.spentFuel, d)
171
+ s.distance = deviceKms.length ? (userData.groupByDay ? deviceKms[0].days.find(d => d.date.getTime() === s.date.getTime()).kms : deviceKms[0].summary.distance) : 0
172
+ })
137
173
  const deviceData = {
138
174
  device: d,
139
- summary: summary[0],
175
+ summary: summary,
140
176
  }
141
177
  devicesResult.push(deviceData)
142
178
  }
@@ -169,117 +205,299 @@ function processDrivers(drivers, data) {
169
205
 
170
206
  async function exportActivityReportToPDF(userData, reportData) {
171
207
  console.log('Export to PDF')
172
-
173
208
  const lang = userData.user.attributes.lang
174
209
  const translations = messages[lang] ? messages[lang] : messages['en-GB']
175
210
 
176
- const headers = []
177
- if(userData.byDriver) {
178
- headers.push(translations.report.driver,
179
- translations.report.distance,
180
- translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)',
181
- translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)',
182
- translations.report.driverHours)
183
- } else {
184
- headers.push(translations.report.name,
185
- translations.report.group,
186
- translations.report.distance,
187
- translations.report.start + ' ' + translations.report.odometer,
188
- translations.report.end + ' ' + translations.report.odometer,
189
- translations.report.avgSpeed + ' (Km/h)',
190
- translations.report.maxSpeed + ' (Km/h)',
191
- translations.report.engineHours,
192
- translations.report.spentFuel)
193
- }
194
211
  if (reportData.devices || reportData.drivers) {
195
212
  const doc = new jsPDF.jsPDF('l')
196
213
  await headerFromUser(doc, translations.report.titleActivityReport, userData.user)
197
214
 
198
- let data = []
199
- doc.setFontSize(10)
200
- doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, 30 )
201
- if(userData.byDriver) {
202
- reportData.drivers.forEach(d => {
203
- const temp = [
204
- d.driver.name,
205
- Number((d.summary.distance / 1000).toFixed(2)),
206
- Math.round(d.summary.averageSpeed * 1.85200),
207
- Math.round(d.summary.maxSpeed * 1.85200),
208
- convertMS(d.summary.engineHours)
209
- ]
210
- data.push(temp)
211
- })
215
+ if(userData.groupByDay){
216
+ exportActivityReportToPDF_Vehicle_GroupByDay(doc, translations, reportData)
217
+ } else if(userData.byDriver){
218
+ exportActivityReportToPDF_Driver(doc, translations, reportData)
212
219
  } else {
213
- reportData.devices.forEach(d => {
214
- const group = userData.groups.find(g => d.device.groupId === g.id)
215
-
216
- const temp = [
217
- d.summary.deviceName,
218
- group ? group.name : '',
219
- Number((d.summary.distance / 1000).toFixed(2)),
220
- Number((d.summary.startOdometer / 1000).toFixed(2)),
221
- Number((d.summary.endOdometer / 1000).toFixed(2)),
222
- Math.round(d.summary.averageSpeed * 1.85200),
223
- Math.round(d.summary.maxSpeed * 1.85200),
224
- convertMS(d.summary.engineHours),
225
- d.summary.convertedSpentFuel >= 0 ? d.summary.convertedSpentFuel : 0,
226
- ]
227
- data.push(temp)
228
- })
220
+ exportActivityReportToPDF_Vehicle(doc, translations, reportData)
229
221
  }
230
222
 
231
- const footValues = []
232
- if(userData.byDriver) {
233
- footValues.push(
234
- 'Total:' + reportData.drivers.length,
235
- (reportData.drivers.reduce((a, b) => a + b.summary.distance, 0)/1000).toFixed(0),
236
- getSumAvgSpeed(reportData.drivers),
237
- getSumMaxSpeed(reportData.drivers),
238
- convertMS(reportData.drivers.reduce((a, b) => a + b.summary.engineHours, 0)),
239
- )
240
- } else {
241
- footValues.push(
242
- 'Total:' + reportData.devices.length,
243
- '',
244
- (reportData.devices.reduce((a, b) => a + b.summary.distance, 0)/1000).toFixed(0),
245
- '','',
246
- getSumAvgSpeed(reportData.devices),
247
- getSumMaxSpeed(reportData.devices),
248
- convertMS(reportData.devices.reduce((a, b) => a + b.summary.engineHours, 0)),
249
- getSumSpentFuel(reportData.devices)
250
- )
223
+ return doc
224
+ }
225
+ }
226
+
227
+ function exportActivityReportToPDF_Driver(doc, translations, reportData) {
228
+ const headers = [translations.report.driver,
229
+ translations.report.distance,
230
+ translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)',
231
+ translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)',
232
+ translations.report.driverHours]
233
+
234
+ doc.setFontSize(10)
235
+ doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, 30)
236
+
237
+ const data = []
238
+ reportData.drivers.forEach(d => {
239
+ const temp = [
240
+ d.driver.name,
241
+ Number((d.summary[0].distance / 1000).toFixed(2)),
242
+ Math.round(d.summary[0].averageSpeed * 1.85200),
243
+ Math.round(d.summary[0].maxSpeed * 1.85200),
244
+ convertMS(d.summary[0].engineHours)
245
+ ]
246
+ data.push(temp)
247
+ })
248
+
249
+ const footValues = ['Total:' + reportData.drivers.length,
250
+ (reportData.drivers.reduce((a, b) => a + b.summary[0].distance, 0) / 1000).toFixed(0),
251
+ getSumAvgSpeed(reportData.drivers),
252
+ getSumMaxSpeed(reportData.drivers),
253
+ convertMS(reportData.drivers.reduce((a, b) => a + b.summary[0].engineHours, 0))]
254
+
255
+ const style = getStyle(getUserPartner(userData.user))
256
+ addTable(doc, headers, data, footValues, style, 35)
257
+ }
258
+
259
+ function exportActivityReportToPDF_Vehicle(doc, translations, reportData) {
260
+ const headers = [translations.report.name,
261
+ translations.report.group,
262
+ translations.report.distance,
263
+ translations.report.start + ' ' + translations.report.odometer,
264
+ translations.report.end + ' ' + translations.report.odometer,
265
+ translations.report.avgSpeed + ' (Km/h)',
266
+ translations.report.maxSpeed + ' (Km/h)',
267
+ translations.report.engineHours,
268
+ translations.report.spentFuel]
269
+
270
+ doc.setFontSize(10)
271
+ doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, 30)
272
+
273
+ const data = []
274
+ reportData.devices.forEach(d => {
275
+ const group = userData.groups.find(g => d.device.groupId === g.id)
276
+
277
+ const temp = [
278
+ d.summary.deviceName,
279
+ group ? group.name : '',
280
+ Number((d.summary[0].distance / 1000).toFixed(2)),
281
+ Number((d.summary[0].startOdometer / 1000).toFixed(2)),
282
+ Number((d.summary[0].endOdometer / 1000).toFixed(2)),
283
+ Math.round(d.summary[0].averageSpeed * 1.85200),
284
+ Math.round(d.summary[0].maxSpeed * 1.85200),
285
+ convertMS(d.summary[0].engineHours),
286
+ d.summary[0].convertedSpentFuel >= 0 ? d.summary[0].convertedSpentFuel : 0,
287
+ ]
288
+ data.push(temp)
289
+ })
290
+
291
+ const footValues = ['Total:' + reportData.devices.length,
292
+ '',
293
+ (reportData.devices.reduce((a, b) => a + b.summary[0].distance, 0) / 1000).toFixed(0),
294
+ '', '',
295
+ getSumAvgSpeed(reportData.devices),
296
+ getSumMaxSpeed(reportData.devices),
297
+ convertMS(reportData.devices.reduce((a, b) => a + b.summary[0].engineHours, 0)),
298
+ getSumSpentFuel(reportData.devices)]
299
+
300
+ const style = getStyle(getUserPartner(userData.user))
301
+ addTable(doc, headers, data, footValues, style, 35)
302
+ }
303
+
304
+ function exportActivityReportToPDF_Vehicle_GroupByDay(doc, translations, reportData) {
305
+ const headers = [translations.report.date,
306
+ translations.report.distance,
307
+ translations.report.start + ' ' + translations.report.odometer,
308
+ translations.report.end + ' ' + translations.report.odometer,
309
+ translations.report.avgSpeed + ' (Km/h)',
310
+ translations.report.maxSpeed + ' (Km/h)',
311
+ translations.report.engineHours,
312
+ translations.report.spentFuel]
313
+
314
+ const weekDays = [
315
+ translations.report.sunday,
316
+ translations.report.monday,
317
+ translations.report.tuesday,
318
+ translations.report.wednesday,
319
+ translations.report.thursday,
320
+ translations.report.friday,
321
+ translations.report.saturday
322
+ ]
323
+
324
+ reportData.devices.forEach(function (d, index) {
325
+ const name = userData.byDriver ? d.driver.name : d.device.name
326
+ const group = userData.byDriver ? userData.groups.find(g => g.drivers.includes(d.id)) : userData.groups.find(g => d.device.groupId === g.id)
327
+
328
+ let space = index === 0 ? 8 : 0
329
+ if(index){
330
+ doc.addPage()
251
331
  }
252
332
 
333
+ doc.setFontSize(13)
334
+ doc.text(name, 20, space + 20)
335
+ doc.setFontSize(11)
336
+ doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
337
+ doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
338
+
339
+ if (!userData.allWeek) {
340
+ doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '')
341
+ + (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '')
342
+ + (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '')
343
+ + (userData.weekDays.thursday ? ' ' + translations.report.thursday + ',' : '')
344
+ + (userData.weekDays.friday ? ' ' + translations.report.friday + ',' : '')
345
+ + (userData.weekDays.saturday ? ' ' + translations.report.saturday + ',' : '')
346
+ + (userData.weekDays.sunday ? ' ' + translations.report.sunday + ',' : '')
347
+ + ' das '
348
+ + userData.dayHours.startTime + ' - ' + userData.dayHours.endTime, 20, space + 30)
349
+ }
350
+
351
+ const data = []
352
+ d.summary.forEach(s => {
353
+ const temp = [
354
+ new Date(s.date).toLocaleDateString() + ' - ' + weekDays[s.date.getDay()],
355
+ Number((s.distance / 1000).toFixed(2)),
356
+ Number((s.startOdometer / 1000).toFixed(2)),
357
+ Number((s.endOdometer / 1000).toFixed(2)),
358
+ Math.round(s.averageSpeed * 1.85200),
359
+ Math.round(s.maxSpeed * 1.85200),
360
+ convertMS(s.engineHours),
361
+ s.convertedSpentFuel >= 0 ? s.convertedSpentFuel : 0
362
+ ]
363
+
364
+ data.push(temp)
365
+ })
366
+
367
+ const footValues = ['Total:' + d.summary.length,
368
+ (d.summary.reduce((a, b) => a + b.distance, 0) / 1000).toFixed(0),
369
+ '', '',
370
+ getSumAvgSpeed(d.summary),
371
+ getSumMaxSpeed(d.summary),
372
+ convertMS(d.summary.reduce((a, b) => a + b.engineHours, 0)),
373
+ getSumSpentFuel(d.summary)]
374
+
253
375
  const style = getStyle(getUserPartner(userData.user))
254
- doc.autoTable({
255
- head: [headers],
256
- body: data,
257
- foot: [footValues],
258
- showFoot: 'lastPage',
259
- headStyles: {
260
- fillColor: style.pdfHeaderColor,
261
- textColor: style.pdfHeaderTextColor,
262
- fontSize: 10
263
- },
264
- bodyStyles: {
265
- fillColor: style.pdfBodyColor,
266
- textColor: style.pdfBodyTextColor,
267
- fontSize: 8
268
- },
269
- footStyles: {
270
- fillColor: style.pdfFooterColor,
271
- textColor: style.pdfFooterTextColor,
272
- fontSize: 9
273
- },
274
- startY: 35 });
376
+ addTable(doc, headers, data, footValues, style, 40)
377
+ })
378
+ }
275
379
 
276
- return doc
380
+ function exportActivityReportToExcel_Vehicle_GroupByDay(settings, translations, reportData) {
381
+ const headers = [{label: translations.report.vehicle, value: 'name'},
382
+ {label: translations.report.group, value: 'group'},
383
+ {label: translations.report.date, value: 'date'},
384
+ {label: translations.report.weekDay, value: 'weekday'},
385
+ {label: translations.report.distance, value: 'distance'},
386
+ {label: translations.report.start + ' ' + translations.report.odometer, value: 'startOdometer'},
387
+ {label: translations.report.end + ' ' + translations.report.odometer, value: 'endOdometer'},
388
+ {label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed'},
389
+ {label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed'},
390
+ {label: translations.report.engineHours, value: 'engineHours'},
391
+ {label: translations.report.spentFuel, value: 'spentFuel'}]
392
+
393
+ const weekDays = [
394
+ translations.report.sunday,
395
+ translations.report.monday,
396
+ translations.report.tuesday,
397
+ translations.report.wednesday,
398
+ translations.report.thursday,
399
+ translations.report.friday,
400
+ translations.report.saturday
401
+ ]
402
+
403
+ let data = []
404
+ if (reportData.devices) {
405
+ reportData.devices.forEach(d => {
406
+ const group = userData.groups.find(g => d.device.groupId === g.id)
407
+
408
+ data = data.concat([{}])
409
+ data = data.concat(d.summary.map(s => {
410
+ return {
411
+ name: s.deviceName,
412
+ group: group ? group.name : '',
413
+ date: new Date(s.date).toLocaleDateString(),
414
+ weekday: weekDays[s.date.getDay()],
415
+ distance: Number((s.distance / 1000).toFixed(0)),
416
+ startOdometer: Number((s.startOdometer / 1000).toFixed(0)),
417
+ endOdometer: Number((s.endOdometer / 1000).toFixed(0)),
418
+ avgSpeed: Math.round(s.averageSpeed * 1.85200),
419
+ maxSpeed: Math.round(s.maxSpeed * 1.85200),
420
+ engineHours: convertMS(s.engineHours),
421
+ spentFuel: s.convertedSpentFuel >= 0 ? s.convertedSpentFuel : 0
422
+ }
423
+ }))
424
+ })
425
+ console.log(data)
426
+ return {
427
+ headers,
428
+ data,
429
+ settings
430
+ }
277
431
  }
278
432
  }
279
433
 
280
- function exportActivityReportToExcel(userData, reportData) {
281
- console.log('Export to Excel')
434
+ function exportActivityReportToExcel_Driver(settings, translations, reportData) {
435
+ const headers = [{label: translations.report.driver, value: 'driver'},
436
+ {label: translations.report.distance, value: 'distance'},
437
+ {label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed'},
438
+ {label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed'},
439
+ {label: translations.report.driverHours, value: 'engineHours'}]
440
+
441
+ let data = []
442
+ if (reportData.drivers) {
443
+ reportData.drivers.forEach(d => {
444
+ data = data.concat([{
445
+ driver: d.driver.name,
446
+ distance: Number((d.summary.distance / 1000).toFixed(0)),
447
+ avgSpeed: Math.round(d.summary.averageSpeed * 1.85200),
448
+ maxSpeed: Math.round(d.summary.maxSpeed * 1.85200),
449
+ engineHours: convertMS(d.summary.engineHours)
450
+ }])
451
+ })
452
+ console.log(data)
453
+ return {
454
+ headers,
455
+ data,
456
+ settings
457
+ }
458
+ }
459
+ }
282
460
 
461
+ function exportActivityReportToExcel_Vehicle(settings, translations, reportData) {
462
+ const headers = [{label: translations.report.vehicle, value: 'name'},
463
+ {label: translations.report.group, value: 'group'},
464
+ {label: translations.report.distance, value: 'distance'},
465
+ {label: translations.report.start + ' ' + translations.report.odometer, value: 'startOdometer'},
466
+ {label: translations.report.end + ' ' + translations.report.odometer, value: 'endOdometer'},
467
+ {label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed'},
468
+ {label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed'},
469
+ {label: translations.report.engineHours, value: 'engineHours'},
470
+ {label: translations.report.spentFuel, value: 'spentFuel'}]
471
+
472
+ if (reportData.devices) {
473
+ let data = []
474
+ reportData.devices.forEach(d => {
475
+ const group = userData.groups.find(g => d.device.groupId === g.id)
476
+
477
+ data = data.concat([{
478
+ name: d.summary.deviceName,
479
+ group: group ? group.name : '',
480
+ distance: Number((d.summary.distance / 1000).toFixed(0)),
481
+ startOdometer: Number((d.summary.startOdometer / 1000).toFixed(0)),
482
+ endOdometer: Number((d.summary.endOdometer / 1000).toFixed(0)),
483
+ avgSpeed: Math.round(d.summary.averageSpeed * 1.85200),
484
+ maxSpeed: Math.round(d.summary.maxSpeed * 1.85200),
485
+ engineHours: convertMS(d.summary.engineHours),
486
+ spentFuel: d.summary.convertedSpentFuel >= 0 ? d.summary.convertedSpentFuel : 0,
487
+ }])
488
+ })
489
+ console.log(data)
490
+ return {
491
+ headers,
492
+ data,
493
+ settings
494
+ }
495
+ }
496
+ }
497
+
498
+ function exportActivityReportToExcel(reportUserData, reportData) {
499
+ console.log('Export to Excel')
500
+ userData = reportUserData
283
501
  const lang = userData.user.attributes.lang
284
502
  const translations = messages[lang] ? messages[lang] : messages['en-GB']
285
503
 
@@ -288,84 +506,33 @@ function exportActivityReportToExcel(userData, reportData) {
288
506
  fileName: fileName // The name of the spreadsheet
289
507
  }
290
508
 
291
- const headers = []
292
-
293
- if(userData.byDriver) {
294
- headers.push(
295
- {label: translations.report.driver, value: 'driver'},
296
- {label: translations.report.distance, value: 'distance'},
297
- {label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed'},
298
- {label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed'},
299
- {label: translations.report.driverHours, value: 'engineHours'})
300
- } else {
301
- headers.push(
302
- {label: translations.report.vehicle, value: 'name'},
303
- {label: translations.report.group, value: 'group'},
304
- {label: translations.report.distance, value: 'distance'},
305
- {label: translations.report.start + ' ' + translations.report.odometer, value: 'startOdometer'},
306
- {label: translations.report.end + ' ' + translations.report.odometer, value: 'endOdometer'},
307
- {label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed'},
308
- {label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed'},
309
- {label: translations.report.engineHours, value: 'engineHours'},
310
- {label: translations.report.spentFuel, value: 'spentFuel'})
311
- }
312
-
313
- let data = []
314
- if(userData.byDriver){
315
- if (reportData.drivers) {
316
- reportData.drivers.forEach(d => {
317
- data = data.concat([{
318
- driver: d.driver.name,
319
- distance: Number((d.summary.distance / 1000).toFixed(0)),
320
- avgSpeed: Math.round(d.summary.averageSpeed * 1.85200),
321
- maxSpeed: Math.round(d.summary.maxSpeed * 1.85200),
322
- engineHours: convertMS(d.summary.engineHours)
323
- }])
324
- })
325
- console.log(data)
326
- return {
327
- headers,
328
- data,
329
- settings
330
- }
331
- }
509
+ if(userData.groupByDay){
510
+ return exportActivityReportToExcel_Vehicle_GroupByDay(settings, translations, reportData)
511
+ } else if(userData.byDriver){
512
+ return exportActivityReportToExcel_Driver(settings, translations, reportData)
332
513
  } else {
333
- if (reportData.devices) {
334
- reportData.devices.forEach(d => {
335
- const group = userData.groups.find(g => d.device.groupId === g.id)
336
-
337
- data = data.concat([{
338
- name: d.summary.deviceName,
339
- group: group ? group.name : '',
340
- distance: Number((d.summary.distance / 1000).toFixed(0)),
341
- startOdometer: Number((d.summary.startOdometer / 1000).toFixed(0)),
342
- endOdometer: Number((d.summary.endOdometer / 1000).toFixed(0)),
343
- avgSpeed: Math.round(d.summary.averageSpeed * 1.85200),
344
- maxSpeed: Math.round(d.summary.maxSpeed * 1.85200),
345
- engineHours: convertMS(d.summary.engineHours),
346
- spentFuel: d.summary.convertedSpentFuel >= 0 ? d.summary.convertedSpentFuel : 0,
347
- }])
348
- })
349
- console.log(data)
350
- return {
351
- headers,
352
- data,
353
- settings
354
- }
355
- }
514
+ return exportActivityReportToExcel_Vehicle(settings, translations, reportData)
356
515
  }
357
516
  }
358
517
 
359
518
  function getSumAvgSpeed(data){
360
519
  const values = data.map(item => {
361
- return Math.round(item.summary.averageSpeed * 1.85200)
362
- })
363
- return Math.round((values.reduce((a, b) => a + b, 0)) / data.length)
520
+ if(userData.groupByDay){
521
+ return Math.round(item.averageSpeed * 1.85200)
522
+ } else {
523
+ return Math.round(item.summary[0].averageSpeed * 1.85200)
524
+ }
525
+ }).filter(v => v !== 0)
526
+ return Math.round((values.reduce((a, b) => a + b, 0)) / values.length)
364
527
  }
365
528
 
366
529
  function getSumMaxSpeed(data){
367
530
  const values = data.map(item => {
368
- return item.summary.maxSpeed
531
+ if(userData.groupByDay){
532
+ return item.maxSpeed
533
+ } else {
534
+ return item.summary[0].maxSpeed
535
+ }
369
536
  })
370
537
  const max = values.reduce(function (a, b) {
371
538
  return Math.max(a, b);
@@ -375,7 +542,11 @@ function getSumMaxSpeed(data){
375
542
 
376
543
  function getSumSpentFuel(data){
377
544
  const values = data.map(item => {
378
- return item.summary.convertedSpentFuel >= 0 ? item.summary.convertedSpentFuel : 0
545
+ if(userData.groupByDay){
546
+ return item.convertedSpentFuel >= 0 ? item.convertedSpentFuel : 0
547
+ } else {
548
+ return item.summary[0].convertedSpentFuel >= 0 ? item.summary[0].convertedSpentFuel : 0
549
+ }
379
550
  })
380
551
  return values.reduce((a, b) => a + b, 0)
381
552
  }