fleetmap-reports 1.0.288 → 1.0.292

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,38 +1,45 @@
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
- const autotable = require('jspdf-autotable')
6
- const reportStyle = require("./reportStyle")
7
- const drivers = require("./util/driver");
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");
8
13
 
9
14
  let traccarInstance
15
+ let userData
10
16
 
11
17
  const fileName = 'ActivityReport'
12
18
 
13
- async function createActivityReport(from, to, userData, traccar) {
19
+ async function createActivityReport(from, to, reportUserData, traccar) {
14
20
  console.log('Create ActivityReport')
15
21
  traccarInstance = traccar
22
+ userData = reportUserData
16
23
  const reportData = []
17
24
 
18
25
  if(userData.byDriver){
19
- const allData = await createActivityReportByDriver(from, to, userData)
26
+ const allData = await createActivityReportByDriver(from, to)
20
27
  reportData.push(allData)
21
28
  }
22
29
  else if(userData.byGroup){
23
- const allGroupsData = await createActivityReportByGroup(from, to, userData)
30
+ const allGroupsData = await createActivityReportByGroup(from, to)
24
31
  allGroupsData.forEach(data => reportData.push(data))
25
- const withoutGroupDevices = await createActivityReportByDevice(from, to, userData)
32
+ const withoutGroupDevices = await createActivityReportByDevice(from, to)
26
33
  reportData.push(withoutGroupDevices)
27
34
  } else {
28
- const allData = await createActivityReportByDevice(from, to, userData)
35
+ const allData = await createActivityReportByDevice(from, to)
29
36
  reportData.push(allData)
30
37
  }
31
38
 
32
39
  return reportData
33
40
  }
34
41
 
35
- async function createActivityReportByGroup(from, to, userData){
42
+ async function createActivityReportByGroup(from, to){
36
43
  console.log("ByGroup")
37
44
  const reportData = []
38
45
  for (const g of userData.groups) {
@@ -67,49 +74,98 @@ async function createActivityReportByGroup(from, to, userData){
67
74
  return reportData
68
75
  }
69
76
 
70
- async function createActivityReportByDevice(from, to, userData){
77
+ async function createActivityReportByDevice(from, to){
71
78
  const groupIds = userData.groups.map(g => g.id)
72
- const withoutGroupDevices = userData.byGroup ? userData.devices.filter(d => !groupIds.includes(d.groupId)) : userData.devices
79
+ const devicesToProcess = userData.byGroup ? userData.devices.filter(d => !groupIds.includes(d.groupId)) : userData.devices
73
80
 
74
81
  const allData = {
75
82
  devices: [],
76
- xpert: withoutGroupDevices.filter(d => d.attributes.xpert).length > 0,
83
+ xpert: devicesToProcess.filter(d => d.attributes.xpert).length > 0,
77
84
  from: from,
78
85
  to: to
79
86
  }
80
87
 
81
- withoutGroupDevices.sort((a, b) => (a.name > b.name) ? 1 : -1)
82
- const devicesToSlice = withoutGroupDevices.slice()
88
+ //Get report data from traccar
89
+ devicesToProcess.sort((a, b) => (a.name > b.name) ? 1 : -1)
83
90
 
84
- const arrayOfArrays = automaticReports.sliceArray(devicesToSlice)
85
- let data = []
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)
91
+ // use the km report to calculate kms
92
+ const kmsReport = await createKmsReport(from, to, userData, traccarInstance)
90
93
 
91
- const responseTrips = await traccarInstance.reports.reportsTripsGet(from, to, a.map(d => d.id))
92
- trips = trips.concat(responseTrips.data)
94
+ 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)
93
151
  }
94
152
 
95
153
  console.log('Summary:' + data.length)
96
154
 
97
- if (data.length === 0) {
98
- return allData
155
+ //Process report data
156
+ if (data.length > 0) {
157
+ allData.devices = processDevices(devicesToProcess, data, kmsReport)
99
158
  }
100
159
 
101
- allData.devices = processDevices(withoutGroupDevices, data, trips)
102
-
103
160
  return allData
104
161
  }
105
162
 
106
- async function createActivityReportByDriver(from, to, userData){
107
- const deviceIds = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
108
- console.log(deviceIds)
163
+ async function createActivityReportByDriver(from, to){
164
+ const devices = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
165
+
109
166
  let tripsData = []
110
- if(deviceIds.length > 0) {
111
- const response = await traccarInstance.reports.reportsTripsGet(from, to, deviceIds)
112
- tripsData = response.data
167
+ if(devices.length > 0) {
168
+ tripsData = await traccar.getTrips(traccarInstance, from, to, devices)
113
169
  }
114
170
 
115
171
  const allData = {
@@ -118,24 +174,26 @@ async function createActivityReportByDriver(from, to, userData){
118
174
  to: to
119
175
  }
120
176
 
121
- allData.drivers = processDrivers(userData.drivers, tripsData)
177
+ allData.drivers = processDrivers(from, to, userData.drivers, tripsData)
122
178
 
123
179
  return allData
124
180
  }
125
181
 
126
182
 
127
- function processDevices(devices, data, trips) {
183
+ function processDevices(devices, data, kmsReport) {
128
184
  const devicesResult = []
129
185
 
130
186
  devices.forEach(d => {
131
187
  const summary = data.filter(s => s.deviceId === d.id)
132
- const deviceTrips = trips.filter(t => t.deviceId === d.id)
133
188
  if (summary) {
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)
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
+ })
136
194
  const deviceData = {
137
195
  device: d,
138
- summary: summary[0],
196
+ summary: summary,
139
197
  }
140
198
  devicesResult.push(deviceData)
141
199
  }
@@ -144,7 +202,7 @@ function processDevices(devices, data, trips) {
144
202
  return devicesResult
145
203
  }
146
204
 
147
- function processDrivers(drivers, data) {
205
+ function processDrivers(from, to, drivers, data) {
148
206
  console.log(data)
149
207
  const driversResult = []
150
208
  drivers.forEach(d => {
@@ -152,12 +210,32 @@ function processDrivers(drivers, data) {
152
210
  if (trips.length > 0) {
153
211
  const driverData = {
154
212
  driver: d,
155
- summary: {
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({
156
234
  distance: trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0),
157
235
  engineHours: trips.reduce((a, b) => a + b.duration, 0),
158
236
  maxSpeed: trips.reduce((a, b) => Math.max(a, b.maxSpeed), 0),
159
237
  averageSpeed: Math.round(trips.reduce((a, b) => a + b.averageSpeed, 0) / trips.length),
160
- },
238
+ })
161
239
  }
162
240
  driversResult.push(driverData)
163
241
  }
@@ -166,202 +244,456 @@ function processDrivers(drivers, data) {
166
244
  return driversResult
167
245
  }
168
246
 
169
- function exportActivityReportToPDF(userData, reportData) {
247
+ async function exportActivityReportToPDF(userData, reportData) {
170
248
  console.log('Export to PDF')
171
-
172
249
  const lang = userData.user.attributes.lang
173
250
  const translations = messages[lang] ? messages[lang] : messages['en-GB']
174
251
 
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)
192
- }
193
252
  if (reportData.devices || reportData.drivers) {
194
- const doc = new jsPDF.jsPDF('l');
195
- doc.setFontSize(14)
196
- doc.text(translations.report.titleActivityReport, 15, 15 )
197
-
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
- })
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)
212
262
  } 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
- })
263
+ exportActivityReportToPDF_Vehicle(doc, translations, reportData)
229
264
  }
230
265
 
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
- )
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()
251
374
  }
252
375
 
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 });
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
+ }
271
393
 
272
- return doc
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
+ }
273
474
  }
274
475
  }
275
476
 
276
- function exportActivityReportToExcel(userData, reportData) {
277
- console.log('Export to Excel')
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
+ }
278
502
 
279
- const lang = userData.user.attributes.lang
280
- const translations = messages[lang] ? messages[lang] : messages['en-GB']
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)
519
+ }
281
520
 
282
- const settings = {
283
- sheetName: translations.report.titleActivityReport, // The name of the sheet
284
- fileName: fileName // The name of the spreadsheet
285
- }
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
+ }
286
544
 
287
- const headers = []
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
+ ]
288
564
 
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'})
296
- } else {
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'})
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
589
+ }
307
590
  }
591
+ }
592
+
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'}]
308
599
 
309
600
  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
- }
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
327
616
  }
328
- } else {
329
- if (reportData.devices) {
330
- reportData.devices.forEach(d => {
331
- const group = userData.groups.find(g => d.device.groupId === g.id)
617
+ }
618
+ }
332
619
 
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
- }
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
351
653
  }
352
654
  }
353
655
  }
354
656
 
657
+ function exportActivityReportToExcel(reportUserData, reportData) {
658
+ console.log('Export to Excel')
659
+ userData = reportUserData
660
+ const lang = userData.user.attributes.lang
661
+ const translations = messages[lang] ? messages[lang] : messages['en-GB']
662
+
663
+ const settings = {
664
+ sheetName: translations.report.titleActivityReport, // The name of the sheet
665
+ fileName: fileName // The name of the spreadsheet
666
+ }
667
+
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)
674
+ } else {
675
+ return exportActivityReportToExcel_Vehicle(settings, translations, reportData)
676
+ }
677
+ }
678
+
355
679
  function getSumAvgSpeed(data){
356
680
  const values = data.map(item => {
357
- return Math.round(item.summary.averageSpeed * 1.85200)
358
- })
359
- return Math.round((values.reduce((a, b) => a + b, 0)) / data.length)
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)
360
688
  }
361
689
 
362
690
  function getSumMaxSpeed(data){
363
691
  const values = data.map(item => {
364
- return item.summary.maxSpeed
692
+ if(userData.groupByDay){
693
+ return item.maxSpeed
694
+ } else {
695
+ return item.summary[0].maxSpeed
696
+ }
365
697
  })
366
698
  const max = values.reduce(function (a, b) {
367
699
  return Math.max(a, b);
@@ -371,7 +703,11 @@ function getSumMaxSpeed(data){
371
703
 
372
704
  function getSumSpentFuel(data){
373
705
  const values = data.map(item => {
374
- return item.summary.convertedSpentFuel >= 0 ? item.summary.convertedSpentFuel : 0
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
+ }
375
711
  })
376
712
  return values.reduce((a, b) => a + b, 0)
377
713
  }