fleetmap-reports 1.0.287 → 1.0.288

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,45 +1,38 @@
1
1
  const automaticReports = require("./automaticReports")
2
2
  const messages = require('../lang')
3
- const {convertMS, getDates} = require("./util/utils")
3
+ const {convertMS} = require("./util/utils")
4
4
  const jsPDF = require('jspdf')
5
- require('jspdf-autotable')
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");
12
- const {isInsideTimetable} = require("./util/trips");
5
+ const autotable = require('jspdf-autotable')
6
+ const reportStyle = require("./reportStyle")
7
+ const drivers = require("./util/driver");
13
8
 
14
9
  let traccarInstance
15
- let userData
16
10
 
17
11
  const fileName = 'ActivityReport'
18
12
 
19
- async function createActivityReport(from, to, reportUserData, traccar) {
13
+ async function createActivityReport(from, to, userData, traccar) {
20
14
  console.log('Create ActivityReport')
21
15
  traccarInstance = traccar
22
- userData = reportUserData
23
16
  const reportData = []
24
17
 
25
18
  if(userData.byDriver){
26
- const allData = await createActivityReportByDriver(from, to)
19
+ const allData = await createActivityReportByDriver(from, to, userData)
27
20
  reportData.push(allData)
28
21
  }
29
22
  else if(userData.byGroup){
30
- const allGroupsData = await createActivityReportByGroup(from, to)
23
+ const allGroupsData = await createActivityReportByGroup(from, to, userData)
31
24
  allGroupsData.forEach(data => reportData.push(data))
32
- const withoutGroupDevices = await createActivityReportByDevice(from, to)
25
+ const withoutGroupDevices = await createActivityReportByDevice(from, to, userData)
33
26
  reportData.push(withoutGroupDevices)
34
27
  } else {
35
- const allData = await createActivityReportByDevice(from, to)
28
+ const allData = await createActivityReportByDevice(from, to, userData)
36
29
  reportData.push(allData)
37
30
  }
38
31
 
39
32
  return reportData
40
33
  }
41
34
 
42
- async function createActivityReportByGroup(from, to){
35
+ async function createActivityReportByGroup(from, to, userData){
43
36
  console.log("ByGroup")
44
37
  const reportData = []
45
38
  for (const g of userData.groups) {
@@ -74,98 +67,49 @@ async function createActivityReportByGroup(from, to){
74
67
  return reportData
75
68
  }
76
69
 
77
- async function createActivityReportByDevice(from, to){
70
+ async function createActivityReportByDevice(from, to, userData){
78
71
  const groupIds = userData.groups.map(g => g.id)
79
- const devicesToProcess = userData.byGroup ? userData.devices.filter(d => !groupIds.includes(d.groupId)) : userData.devices
72
+ const withoutGroupDevices = userData.byGroup ? userData.devices.filter(d => !groupIds.includes(d.groupId)) : userData.devices
80
73
 
81
74
  const allData = {
82
75
  devices: [],
83
- xpert: devicesToProcess.filter(d => d.attributes.xpert).length > 0,
76
+ xpert: withoutGroupDevices.filter(d => d.attributes.xpert).length > 0,
84
77
  from: from,
85
78
  to: to
86
79
  }
87
80
 
88
- //Get report data from traccar
89
- devicesToProcess.sort((a, b) => (a.name > b.name) ? 1 : -1)
90
-
91
- // use the km report to calculate kms
92
- const kmsReport = await createKmsReport(from, to, userData, traccarInstance)
81
+ withoutGroupDevices.sort((a, b) => (a.name > b.name) ? 1 : -1)
82
+ const devicesToSlice = withoutGroupDevices.slice()
93
83
 
84
+ const arrayOfArrays = automaticReports.sliceArray(devicesToSlice)
94
85
  let data = []
95
- if(userData.groupByDay) {
96
- const dates = getDates(from, to)
97
- for(const date of dates){
98
- if(userData.allWeek || !userData.weekDays){
99
- const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
100
- const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
101
-
102
- const summary = await traccar.getSummary(traccarInstance, fromByDay, toByDay, devicesToProcess)
103
- summary.forEach(s => s.date = date)
104
- data = data.concat(summary)
105
- } else {
106
- if((date.getDay() === 0 && userData.weekDays.sunday) ||
107
- (date.getDay() === 1 && userData.weekDays.monday) ||
108
- (date.getDay() === 2 && userData.weekDays.tuesday) ||
109
- (date.getDay() === 3 && userData.weekDays.wednesday) ||
110
- (date.getDay() === 4 && userData.weekDays.thursday) ||
111
- (date.getDay() === 5 && userData.weekDays.friday) ||
112
- (date.getDay() === 6 && userData.weekDays.saturday)) {
113
-
114
- const fromByDay = new Date(new Date(date).toISOString().split('T')[0] + ' ' + userData.dayHours.startTime)
115
- const toByDay = new Date(new Date(date).toISOString().split('T')[0] + ' ' + userData.dayHours.endTime)
116
-
117
- if(fromByDay.getTime() < toByDay.getTime()) {
118
- const summary = await traccar.getSummary(traccarInstance, new Date(fromByDay.toUTCString()), new Date(toByDay.toUTCString()), devicesToProcess)
119
- summary.forEach(s => s.date = date)
120
- data = data.concat(summary)
121
- } else {
122
- const dayStart = new Date(new Date(date).toISOString().split('T')[0] + ' 00:00:00')
123
- const dayEnd = new Date(new Date(date).toISOString().split('T')[0] + ' 23:59:59')
124
-
125
- const summaryStart = await traccar.getSummary(traccarInstance, new Date(dayStart.toUTCString()), new Date(toByDay.toUTCString()), devicesToProcess)
126
- const summaryEnd = await traccar.getSummary(traccarInstance, new Date(fromByDay.toUTCString()), new Date(dayEnd.toUTCString()), devicesToProcess)
127
-
128
- const summary = []
129
- summaryStart.forEach(s => {
130
- const sEnd = summaryEnd.find(a => s.deviceId === a.deviceId)
131
- summary.push({
132
- date: date,
133
- deviceId: s.deviceId,
134
- averageSpeed: (s.averageSpeed + sEnd.averageSpeed) / 2,
135
- distance: s.distance + sEnd.distance,
136
- engineHours: s.engineHours + sEnd.engineHours,
137
- maxSpeed: s.maxSpeed > sEnd.maxSpeed ? s.maxSpeed : sEnd.maxSpeed,
138
- endOdometer: 0,
139
- startOdometer: 0
140
- })
141
- })
142
- data = data.concat(summary)
143
- }
144
- }else {
145
- data = data.concat({date: date})
146
- }
147
- }
148
- }
149
- } else {
150
- data = await traccar.getSummary(traccarInstance, from, to, devicesToProcess)
86
+ let trips = []
87
+ for (const a of arrayOfArrays) {
88
+ const responseData = await traccarInstance.reports.reportsSummaryGet(from, to, a.map(d => d.id))
89
+ data = data.concat(responseData.data)
90
+
91
+ const responseTrips = await traccarInstance.reports.reportsTripsGet(from, to, a.map(d => d.id))
92
+ trips = trips.concat(responseTrips.data)
151
93
  }
152
94
 
153
95
  console.log('Summary:' + data.length)
154
96
 
155
- //Process report data
156
- if (data.length > 0) {
157
- allData.devices = processDevices(devicesToProcess, data, kmsReport)
97
+ if (data.length === 0) {
98
+ return allData
158
99
  }
159
100
 
101
+ allData.devices = processDevices(withoutGroupDevices, data, trips)
102
+
160
103
  return allData
161
104
  }
162
105
 
163
- async function createActivityReportByDriver(from, to){
164
- const devices = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
165
-
106
+ async function createActivityReportByDriver(from, to, userData){
107
+ const deviceIds = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
108
+ console.log(deviceIds)
166
109
  let tripsData = []
167
- if(devices.length > 0) {
168
- tripsData = await traccar.getTrips(traccarInstance, from, to, devices)
110
+ if(deviceIds.length > 0) {
111
+ const response = await traccarInstance.reports.reportsTripsGet(from, to, deviceIds)
112
+ tripsData = response.data
169
113
  }
170
114
 
171
115
  const allData = {
@@ -174,26 +118,24 @@ async function createActivityReportByDriver(from, to){
174
118
  to: to
175
119
  }
176
120
 
177
- allData.drivers = processDrivers(from, to, userData.drivers, tripsData)
121
+ allData.drivers = processDrivers(userData.drivers, tripsData)
178
122
 
179
123
  return allData
180
124
  }
181
125
 
182
126
 
183
- function processDevices(devices, data, kmsReport) {
127
+ function processDevices(devices, data, trips) {
184
128
  const devicesResult = []
185
129
 
186
130
  devices.forEach(d => {
187
131
  const summary = data.filter(s => s.deviceId === d.id)
132
+ const deviceTrips = trips.filter(t => t.deviceId === d.id)
188
133
  if (summary) {
189
- summary.forEach(s => {
190
- const deviceKms = kmsReport[0].devices.filter(r => r.device.id === d.id)
191
- s.convertedSpentFuel = automaticReports.calculateSpentFuel(s.spentFuel, d)
192
- s.distance = deviceKms.length ? (userData.groupByDay ? deviceKms[0].days.find(d => d.date.getTime() === s.date.getTime()).kms : deviceKms[0].summary.distance) : 0
193
- })
134
+ summary[0].convertedSpentFuel = automaticReports.calculateSpentFuel(summary[0].spentFuel, d)
135
+ summary[0].distance = deviceTrips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
194
136
  const deviceData = {
195
137
  device: d,
196
- summary: summary,
138
+ summary: summary[0],
197
139
  }
198
140
  devicesResult.push(deviceData)
199
141
  }
@@ -202,7 +144,7 @@ function processDevices(devices, data, kmsReport) {
202
144
  return devicesResult
203
145
  }
204
146
 
205
- function processDrivers(from, to, drivers, data) {
147
+ function processDrivers(drivers, data) {
206
148
  console.log(data)
207
149
  const driversResult = []
208
150
  drivers.forEach(d => {
@@ -210,32 +152,12 @@ function processDrivers(from, to, drivers, data) {
210
152
  if (trips.length > 0) {
211
153
  const driverData = {
212
154
  driver: d,
213
- summary: [],
214
- }
215
- if(userData.groupByDay) {
216
- const dates = getDates(from, to)
217
- for(const date of dates){
218
- const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
219
- const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
220
-
221
- const tripsByDay = trips.filter(t => (new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay)
222
- && (userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData)))
223
-
224
- driverData.summary.push({
225
- date: date,
226
- distance: tripsByDay.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0),
227
- engineHours: tripsByDay.reduce((a, b) => a + b.duration, 0),
228
- maxSpeed: tripsByDay.reduce((a, b) => Math.max(a, b.maxSpeed), 0),
229
- averageSpeed: tripsByDay.length > 0 ? Math.round(tripsByDay.reduce((a, b) => a + b.averageSpeed, 0) / tripsByDay.length) : 0,
230
- })
231
- }
232
- } else {
233
- driverData.summary.push({
155
+ summary: {
234
156
  distance: trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0),
235
157
  engineHours: trips.reduce((a, b) => a + b.duration, 0),
236
158
  maxSpeed: trips.reduce((a, b) => Math.max(a, b.maxSpeed), 0),
237
159
  averageSpeed: Math.round(trips.reduce((a, b) => a + b.averageSpeed, 0) / trips.length),
238
- })
160
+ },
239
161
  }
240
162
  driversResult.push(driverData)
241
163
  }
@@ -244,419 +166,116 @@ function processDrivers(from, to, drivers, data) {
244
166
  return driversResult
245
167
  }
246
168
 
247
- async function exportActivityReportToPDF(userData, reportData) {
169
+ function exportActivityReportToPDF(userData, reportData) {
248
170
  console.log('Export to PDF')
171
+
249
172
  const lang = userData.user.attributes.lang
250
173
  const translations = messages[lang] ? messages[lang] : messages['en-GB']
251
174
 
252
- if (reportData.devices || reportData.drivers) {
253
- const doc = new jsPDF.jsPDF('l')
254
- await headerFromUser(doc, translations.report.titleActivityReport, userData.user)
255
-
256
- if(userData.groupByDay && userData.byDriver) {
257
- exportActivityReportToPDF_Driver_GroupByDay(doc, translations, reportData)
258
- } else if(userData.groupByDay) {
259
- exportActivityReportToPDF_Vehicle_GroupByDay(doc, translations, reportData)
260
- } else if(userData.byDriver) {
261
- exportActivityReportToPDF_Driver(doc, translations, reportData)
262
- } else {
263
- exportActivityReportToPDF_Vehicle(doc, translations, reportData)
264
- }
265
-
266
- return doc
267
- }
268
- }
269
-
270
- function exportActivityReportToPDF_Driver(doc, translations, reportData) {
271
- const headers = [translations.report.driver,
272
- translations.report.distance,
273
- translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)',
274
- translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)',
275
- translations.report.driverHours]
276
-
277
- doc.setFontSize(10)
278
- doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, 30)
279
-
280
- const data = []
281
- reportData.drivers.forEach(d => {
282
- const temp = [
283
- d.driver.name,
284
- Number((d.summary[0].distance / 1000).toFixed(2)),
285
- Math.round(d.summary[0].averageSpeed * 1.85200),
286
- Math.round(d.summary[0].maxSpeed * 1.85200),
287
- convertMS(d.summary[0].engineHours)
288
- ]
289
- data.push(temp)
290
- })
291
-
292
- const footValues = ['Total:' + reportData.drivers.length,
293
- (reportData.drivers.reduce((a, b) => a + b.summary[0].distance, 0) / 1000).toFixed(0),
294
- getSumAvgSpeed(reportData.drivers),
295
- getSumMaxSpeed(reportData.drivers),
296
- convertMS(reportData.drivers.reduce((a, b) => a + b.summary[0].engineHours, 0))]
297
-
298
- const style = getStyle(getUserPartner(userData.user))
299
- addTable(doc, headers, data, footValues, style, 35)
300
- }
301
-
302
- function exportActivityReportToPDF_Vehicle(doc, translations, reportData) {
303
- const headers = [translations.report.vehicle,
304
- translations.report.group,
305
- translations.report.distance,
306
- translations.report.start + ' ' + translations.report.odometer,
307
- translations.report.end + ' ' + translations.report.odometer,
308
- translations.report.avgSpeed + ' (Km/h)',
309
- translations.report.maxSpeed + ' (Km/h)',
310
- translations.report.engineHours,
311
- translations.report.spentFuel]
312
-
313
- doc.setFontSize(10)
314
- doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, 30)
315
-
316
- const data = []
317
- reportData.devices.forEach(d => {
318
- const group = userData.groups.find(g => d.device.groupId === g.id)
319
-
320
- const temp = [
321
- d.summary[0].deviceName,
322
- group ? group.name : '',
323
- Number((d.summary[0].distance / 1000).toFixed(2)),
324
- Number((d.summary[0].startOdometer / 1000).toFixed(2)),
325
- Number((d.summary[0].endOdometer / 1000).toFixed(2)),
326
- Math.round(d.summary[0].averageSpeed * 1.85200),
327
- Math.round(d.summary[0].maxSpeed * 1.85200),
328
- convertMS(d.summary[0].engineHours),
329
- d.summary[0].convertedSpentFuel >= 0 ? d.summary[0].convertedSpentFuel : 0,
330
- ]
331
- data.push(temp)
332
- })
333
-
334
- const footValues = ['Total:' + reportData.devices.length,
335
- '',
336
- (reportData.devices.reduce((a, b) => a + b.summary[0].distance, 0) / 1000).toFixed(0),
337
- '', '',
338
- getSumAvgSpeed(reportData.devices),
339
- getSumMaxSpeed(reportData.devices),
340
- convertMS(reportData.devices.reduce((a, b) => a + b.summary[0].engineHours, 0)),
341
- getSumSpentFuel(reportData.devices)]
342
-
343
- const style = getStyle(getUserPartner(userData.user))
344
- addTable(doc, headers, data, footValues, style, 35)
345
- }
346
-
347
- function exportActivityReportToPDF_Vehicle_GroupByDay(doc, translations, reportData) {
348
- const headers = [translations.report.date,
349
- translations.report.distance,
350
- translations.report.start + ' ' + translations.report.odometer,
351
- translations.report.end + ' ' + translations.report.odometer,
352
- translations.report.avgSpeed + ' (Km/h)',
353
- translations.report.maxSpeed + ' (Km/h)',
354
- translations.report.engineHours,
355
- translations.report.spentFuel]
356
-
357
- const weekDays = [
358
- translations.report.sunday,
359
- translations.report.monday,
360
- translations.report.tuesday,
361
- translations.report.wednesday,
362
- translations.report.thursday,
363
- translations.report.friday,
364
- translations.report.saturday
365
- ]
366
-
367
- reportData.devices.forEach(function (d, index) {
368
- const name = userData.byDriver ? d.driver.name : d.device.name
369
- const group = userData.byDriver ? userData.groups.find(g => g.drivers.includes(d.id)) : userData.groups.find(g => d.device.groupId === g.id)
370
-
371
- let space = index === 0 ? 8 : 0
372
- if(index){
373
- doc.addPage()
374
- }
375
-
376
- doc.setFontSize(13)
377
- doc.text(name, 20, space + 20)
378
- doc.setFontSize(11)
379
- doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
380
- doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
381
-
382
- if (!userData.allWeek && userData.weekDays) {
383
- doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '')
384
- + (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '')
385
- + (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '')
386
- + (userData.weekDays.thursday ? ' ' + translations.report.thursday + ',' : '')
387
- + (userData.weekDays.friday ? ' ' + translations.report.friday + ',' : '')
388
- + (userData.weekDays.saturday ? ' ' + translations.report.saturday + ',' : '')
389
- + (userData.weekDays.sunday ? ' ' + translations.report.sunday + ',' : '')
390
- + ' das '
391
- + userData.dayHours.startTime + ' - ' + userData.dayHours.endTime, 20, space + 30)
392
- }
393
-
394
- const data = []
395
- d.summary.forEach(s => {
396
- const temp = [
397
- new Date(s.date).toLocaleDateString() + ' - ' + weekDays[s.date.getDay()],
398
- Number((s.distance / 1000).toFixed(2)),
399
- Number((s.startOdometer / 1000).toFixed(2)),
400
- Number((s.endOdometer / 1000).toFixed(2)),
401
- Math.round(s.averageSpeed * 1.85200),
402
- Math.round(s.maxSpeed * 1.85200),
403
- convertMS(s.engineHours),
404
- s.convertedSpentFuel >= 0 ? s.convertedSpentFuel : 0
405
- ]
406
-
407
- data.push(temp)
408
- })
409
-
410
- const footValues = ['Total:' + d.summary.length,
411
- (d.summary.reduce((a, b) => a + b.distance, 0) / 1000).toFixed(0),
412
- '', '',
413
- getSumAvgSpeed(d.summary),
414
- getSumMaxSpeed(d.summary),
415
- convertMS(d.summary.reduce((a, b) => a + b.engineHours, 0)),
416
- getSumSpentFuel(d.summary)]
417
-
418
- const style = getStyle(getUserPartner(userData.user))
419
- addTable(doc, headers, data, footValues, style, 40)
420
- })
421
- }
422
-
423
- function exportActivityReportToExcel_Vehicle_GroupByDay(settings, translations, reportData) {
424
- const headers = [{label: translations.report.vehicle, value: 'name'},
425
- {label: translations.report.group, value: 'group'},
426
- {label: translations.report.date, value: 'date'},
427
- {label: translations.report.weekDay, value: 'weekday'},
428
- {label: translations.report.distance, value: 'distance'},
429
- {label: translations.report.start + ' ' + translations.report.odometer, value: 'startOdometer'},
430
- {label: translations.report.end + ' ' + translations.report.odometer, value: 'endOdometer'},
431
- {label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed'},
432
- {label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed'},
433
- {label: translations.report.engineHours, value: 'engineHours'},
434
- {label: translations.report.spentFuel, value: 'spentFuel'}]
435
-
436
- const weekDays = [
437
- translations.report.sunday,
438
- translations.report.monday,
439
- translations.report.tuesday,
440
- translations.report.wednesday,
441
- translations.report.thursday,
442
- translations.report.friday,
443
- translations.report.saturday
444
- ]
445
-
446
- let data = []
447
- if (reportData.devices) {
448
- reportData.devices.forEach(d => {
449
- const group = userData.groups.find(g => d.device.groupId === g.id)
450
-
451
- data = data.concat([{}])
452
- data = data.concat(d.summary.map(s => {
453
- return {
454
- name: s.deviceName,
455
- group: group ? group.name : '',
456
- date: new Date(s.date).toLocaleDateString(),
457
- weekday: weekDays[s.date.getDay()],
458
- distance: Number((s.distance / 1000).toFixed(0)),
459
- startOdometer: Number((s.startOdometer / 1000).toFixed(0)),
460
- endOdometer: Number((s.endOdometer / 1000).toFixed(0)),
461
- avgSpeed: Math.round(s.averageSpeed * 1.85200),
462
- maxSpeed: Math.round(s.maxSpeed * 1.85200),
463
- engineHours: convertMS(s.engineHours),
464
- spentFuel: s.convertedSpentFuel >= 0 ? s.convertedSpentFuel : 0
465
- }
466
- }))
467
- })
468
- console.log(data)
469
- return {
470
- headers,
471
- data,
472
- settings
473
- }
175
+ const headers = []
176
+ if(userData.byDriver) {
177
+ headers.push(translations.report.driver,
178
+ translations.report.distance,
179
+ translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)',
180
+ translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)',
181
+ translations.report.driverHours)
182
+ } else {
183
+ headers.push(translations.report.name,
184
+ translations.report.group,
185
+ translations.report.distance,
186
+ translations.report.start + ' ' + translations.report.odometer,
187
+ translations.report.end + ' ' + translations.report.odometer,
188
+ translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)',
189
+ translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)',
190
+ translations.report.engineHours,
191
+ translations.report.spentFuel)
474
192
  }
475
- }
476
-
477
- function exportActivityReportToPDF_Driver_GroupByDay(doc, translations, reportData) {
478
- const headers = [translations.report.date,
479
- translations.report.distance,
480
- translations.report.avgSpeed + ' (Km/h)',
481
- translations.report.maxSpeed + ' (Km/h)',
482
- translations.report.engineHours]
483
-
484
- const weekDays = [
485
- translations.report.sunday,
486
- translations.report.monday,
487
- translations.report.tuesday,
488
- translations.report.wednesday,
489
- translations.report.thursday,
490
- translations.report.friday,
491
- translations.report.saturday
492
- ]
493
-
494
- reportData.drivers.forEach(function (d, index) {
495
- const name = d.driver.name
496
- const group = userData.groups.find(g => g.drivers.includes(d.id))
497
-
498
- let space = index === 0 ? 8 : 0
499
- if(index){
500
- doc.addPage()
501
- }
193
+ if (reportData.devices || reportData.drivers) {
194
+ const doc = new jsPDF.jsPDF('l');
195
+ doc.setFontSize(14)
196
+ doc.text(translations.report.titleActivityReport, 15, 15 )
502
197
 
503
- doc.setFontSize(13)
504
- doc.text(name, 20, space + 20)
505
- doc.setFontSize(11)
506
- doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
507
- doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
508
-
509
- if (!userData.allWeek && userData.weekDays) {
510
- doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '')
511
- + (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '')
512
- + (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '')
513
- + (userData.weekDays.thursday ? ' ' + translations.report.thursday + ',' : '')
514
- + (userData.weekDays.friday ? ' ' + translations.report.friday + ',' : '')
515
- + (userData.weekDays.saturday ? ' ' + translations.report.saturday + ',' : '')
516
- + (userData.weekDays.sunday ? ' ' + translations.report.sunday + ',' : '')
517
- + ' das '
518
- + userData.dayHours.startTime + ' - ' + userData.dayHours.endTime, 20, space + 30)
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
+ })
212
+ } 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
+ })
519
229
  }
520
230
 
521
- const data = []
522
- d.summary.forEach(s => {
523
- const temp = [
524
- new Date(s.date).toLocaleDateString() + ' - ' + weekDays[s.date.getDay()],
525
- Number((s.distance / 1000).toFixed(2)),
526
- Math.round(s.averageSpeed * 1.85200),
527
- Math.round(s.maxSpeed * 1.85200),
528
- convertMS(s.engineHours)
529
- ]
530
-
531
- data.push(temp)
532
- })
533
-
534
- const footValues = ['Total:' + d.summary.length,
535
- (d.summary.reduce((a, b) => a + b.distance, 0) / 1000).toFixed(0),
536
- getSumAvgSpeed(d.summary),
537
- getSumMaxSpeed(d.summary),
538
- convertMS(d.summary.reduce((a, b) => a + b.engineHours, 0))]
539
-
540
- const style = getStyle(getUserPartner(userData.user))
541
- addTable(doc, headers, data, footValues, style, 40)
542
- })
543
- }
544
-
545
- function exportActivityReportToExcel_Driver_GroupByDay(settings, translations, reportData) {
546
- const headers = [{label: translations.report.driver, value: 'name'},
547
- {label: translations.report.group, value: 'group'},
548
- {label: translations.report.date, value: 'date'},
549
- {label: translations.report.weekDay, value: 'weekday'},
550
- {label: translations.report.distance, value: 'distance'},
551
- {label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed'},
552
- {label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed'},
553
- {label: translations.report.engineHours, value: 'engineHours'}]
554
-
555
- const weekDays = [
556
- translations.report.sunday,
557
- translations.report.monday,
558
- translations.report.tuesday,
559
- translations.report.wednesday,
560
- translations.report.thursday,
561
- translations.report.friday,
562
- translations.report.saturday
563
- ]
564
-
565
- let data = []
566
- if (reportData.drivers) {
567
- reportData.drivers.forEach(d => {
568
- const group = userData.groups.find(g => g.drivers.includes(d.id))
569
-
570
- data = data.concat([{}])
571
- data = data.concat(d.summary.map(s => {
572
- return {
573
- name: d.driver.name,
574
- group: group ? group.name : '',
575
- date: new Date(s.date).toLocaleDateString(),
576
- weekday: weekDays[s.date.getDay()],
577
- distance: Number((s.distance / 1000).toFixed(0)),
578
- avgSpeed: Math.round(s.averageSpeed * 1.85200),
579
- maxSpeed: Math.round(s.maxSpeed * 1.85200),
580
- engineHours: convertMS(s.engineHours)
581
- }
582
- }))
583
- })
584
- console.log(data)
585
- return {
586
- headers,
587
- data,
588
- settings
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
+ )
589
251
  }
590
- }
591
- }
592
252
 
593
- function exportActivityReportToExcel_Driver(settings, translations, reportData) {
594
- const headers = [{label: translations.report.driver, value: 'driver'},
595
- {label: translations.report.distance, value: 'distance'},
596
- {label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed'},
597
- {label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed'},
598
- {label: translations.report.driverHours, value: 'engineHours'}]
253
+ doc.autoTable({
254
+ head: [headers],
255
+ body: data,
256
+ foot: [footValues],
257
+ showFoot: 'lastPage',
258
+ headStyles: {
259
+ fillColor: reportStyle.pdfHeaderColor,
260
+ textColor: reportStyle.pdfHeaderTextColor
261
+ },
262
+ bodyStyles: {
263
+ fillColor: reportStyle.pdfBodyColor,
264
+ textColor: reportStyle.pdfBodyTextColor
265
+ },
266
+ footStyles: {
267
+ fillColor: reportStyle.pdfFooterColor,
268
+ textColor: reportStyle.pdfFooterTextColor
269
+ },
270
+ startY: 35 });
599
271
 
600
- let data = []
601
- if (reportData.drivers) {
602
- reportData.drivers.forEach(d => {
603
- data = data.concat([{
604
- driver: d.driver.name,
605
- distance: Number((d.summary[0].distance / 1000).toFixed(0)),
606
- avgSpeed: Math.round(d.summary[0].averageSpeed * 1.85200),
607
- maxSpeed: Math.round(d.summary[0].maxSpeed * 1.85200),
608
- engineHours: convertMS(d.summary[0].engineHours)
609
- }])
610
- })
611
- console.log(data)
612
- return {
613
- headers,
614
- data,
615
- settings
616
- }
617
- }
618
- }
619
-
620
- function exportActivityReportToExcel_Vehicle(settings, translations, reportData) {
621
- const headers = [{label: translations.report.vehicle, value: 'name'},
622
- {label: translations.report.group, value: 'group'},
623
- {label: translations.report.distance, value: 'distance'},
624
- {label: translations.report.start + ' ' + translations.report.odometer, value: 'startOdometer'},
625
- {label: translations.report.end + ' ' + translations.report.odometer, value: 'endOdometer'},
626
- {label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed'},
627
- {label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed'},
628
- {label: translations.report.engineHours, value: 'engineHours'},
629
- {label: translations.report.spentFuel, value: 'spentFuel'}]
630
-
631
- if (reportData.devices) {
632
- let data = []
633
- reportData.devices.forEach(d => {
634
- const group = userData.groups.find(g => d.device.groupId === g.id)
635
-
636
- data = data.concat([{
637
- name: d.summary[0].deviceName,
638
- group: group ? group.name : '',
639
- distance: Number((d.summary[0].distance / 1000).toFixed(0)),
640
- startOdometer: Number((d.summary[0].startOdometer / 1000).toFixed(0)),
641
- endOdometer: Number((d.summary[0].endOdometer / 1000).toFixed(0)),
642
- avgSpeed: Math.round(d.summary[0].averageSpeed * 1.85200),
643
- maxSpeed: Math.round(d.summary[0].maxSpeed * 1.85200),
644
- engineHours: convertMS(d.summary[0].engineHours),
645
- spentFuel: d.summary[0].convertedSpentFuel >= 0 ? d.summary[0].convertedSpentFuel : 0,
646
- }])
647
- })
648
- console.log(data)
649
- return {
650
- headers,
651
- data,
652
- settings
653
- }
272
+ return doc
654
273
  }
655
274
  }
656
275
 
657
- function exportActivityReportToExcel(reportUserData, reportData) {
276
+ function exportActivityReportToExcel(userData, reportData) {
658
277
  console.log('Export to Excel')
659
- userData = reportUserData
278
+
660
279
  const lang = userData.user.attributes.lang
661
280
  const translations = messages[lang] ? messages[lang] : messages['en-GB']
662
281
 
@@ -665,35 +284,84 @@ function exportActivityReportToExcel(reportUserData, reportData) {
665
284
  fileName: fileName // The name of the spreadsheet
666
285
  }
667
286
 
668
- if(userData.groupByDay && userData.byDriver){
669
- return exportActivityReportToExcel_Driver_GroupByDay(settings, translations, reportData)
670
- } else if(userData.groupByDay){
671
- return exportActivityReportToExcel_Vehicle_GroupByDay(settings, translations, reportData)
672
- } else if(userData.byDriver){
673
- return exportActivityReportToExcel_Driver(settings, translations, reportData)
287
+ const headers = []
288
+
289
+ if(userData.byDriver) {
290
+ headers.push(
291
+ {label: translations.report.driver, value: 'driver'},
292
+ {label: translations.report.distance, value: 'distance'},
293
+ {label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed'},
294
+ {label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed'},
295
+ {label: translations.report.driverHours, value: 'engineHours'})
674
296
  } else {
675
- return exportActivityReportToExcel_Vehicle(settings, translations, reportData)
297
+ headers.push(
298
+ {label: translations.report.vehicle, value: 'name'},
299
+ {label: translations.report.group, value: 'group'},
300
+ {label: translations.report.distance, value: 'distance'},
301
+ {label: translations.report.start + ' ' + translations.report.odometer, value: 'startOdometer'},
302
+ {label: translations.report.end + ' ' + translations.report.odometer, value: 'endOdometer'},
303
+ {label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed'},
304
+ {label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed'},
305
+ {label: translations.report.engineHours, value: 'engineHours'},
306
+ {label: translations.report.spentFuel, value: 'spentFuel'})
307
+ }
308
+
309
+ let data = []
310
+ if(userData.byDriver){
311
+ if (reportData.drivers) {
312
+ reportData.drivers.forEach(d => {
313
+ data = data.concat([{
314
+ driver: d.driver.name,
315
+ distance: Number((d.summary.distance / 1000).toFixed(0)),
316
+ avgSpeed: Math.round(d.summary.averageSpeed * 1.85200),
317
+ maxSpeed: Math.round(d.summary.maxSpeed * 1.85200),
318
+ engineHours: convertMS(d.summary.engineHours)
319
+ }])
320
+ })
321
+ console.log(data)
322
+ return {
323
+ headers,
324
+ data,
325
+ settings
326
+ }
327
+ }
328
+ } else {
329
+ if (reportData.devices) {
330
+ reportData.devices.forEach(d => {
331
+ const group = userData.groups.find(g => d.device.groupId === g.id)
332
+
333
+ data = data.concat([{
334
+ name: d.summary.deviceName,
335
+ group: group ? group.name : '',
336
+ distance: Number((d.summary.distance / 1000).toFixed(0)),
337
+ startOdometer: Number((d.summary.startOdometer / 1000).toFixed(0)),
338
+ endOdometer: Number((d.summary.endOdometer / 1000).toFixed(0)),
339
+ avgSpeed: Math.round(d.summary.averageSpeed * 1.85200),
340
+ maxSpeed: Math.round(d.summary.maxSpeed * 1.85200),
341
+ engineHours: convertMS(d.summary.engineHours),
342
+ spentFuel: d.summary.convertedSpentFuel >= 0 ? d.summary.convertedSpentFuel : 0,
343
+ }])
344
+ })
345
+ console.log(data)
346
+ return {
347
+ headers,
348
+ data,
349
+ settings
350
+ }
351
+ }
676
352
  }
677
353
  }
678
354
 
679
355
  function getSumAvgSpeed(data){
680
356
  const values = data.map(item => {
681
- if(userData.groupByDay){
682
- return Math.round(item.averageSpeed * 1.85200)
683
- } else {
684
- return Math.round(item.summary[0].averageSpeed * 1.85200)
685
- }
686
- }).filter(v => v !== 0)
687
- return Math.round((values.reduce((a, b) => a + b, 0)) / values.length)
357
+ return Math.round(item.summary.averageSpeed * 1.85200)
358
+ })
359
+ return Math.round((values.reduce((a, b) => a + b, 0)) / data.length)
688
360
  }
689
361
 
690
362
  function getSumMaxSpeed(data){
691
363
  const values = data.map(item => {
692
- if(userData.groupByDay){
693
- return item.maxSpeed
694
- } else {
695
- return item.summary[0].maxSpeed
696
- }
364
+ return item.summary.maxSpeed
697
365
  })
698
366
  const max = values.reduce(function (a, b) {
699
367
  return Math.max(a, b);
@@ -703,11 +371,7 @@ function getSumMaxSpeed(data){
703
371
 
704
372
  function getSumSpentFuel(data){
705
373
  const values = data.map(item => {
706
- if(userData.groupByDay){
707
- return item.convertedSpentFuel >= 0 ? item.convertedSpentFuel : 0
708
- } else {
709
- return item.summary[0].convertedSpentFuel >= 0 ? item.summary[0].convertedSpentFuel : 0
710
- }
374
+ return item.summary.convertedSpentFuel >= 0 ? item.summary.convertedSpentFuel : 0
711
375
  })
712
376
  return values.reduce((a, b) => a + b, 0)
713
377
  }