fleetmap-reports 1.0.658 → 1.0.660

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "1.0.658",
3
+ "version": "1.0.660",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -2,7 +2,10 @@ const automaticReports = require('./automaticReports')
2
2
  const { convertMS, getDates, getTranslations, weekDaySelected } = require('./util/utils')
3
3
  const jsPDF = require('jspdf')
4
4
  require('jspdf-autotable')
5
- const { headerFromUser, addTable } = require('./util/pdfDocument')
5
+ const {
6
+ headerFromUser, addTable,
7
+ pdfPageByDay
8
+ } = require('./util/pdfDocument')
6
9
  const { getStyle } = require('./reportStyle')
7
10
  const { getUserPartner } = require('fleetmap-partners')
8
11
  const traccar = require('./util/traccar')
@@ -428,31 +431,7 @@ function exportActivityReportToPDFVehicleGroupByDay (doc, translations, reportDa
428
431
  ]
429
432
 
430
433
  reportData.devices.forEach(function (d, index) {
431
- const name = userData.byDriver ? d.driver.name : d.device.name
432
- const group = userData.byDriver ? userData.groups.find(g => g.drivers.includes(d.id)) : userData.groups.find(g => d.device.groupId === g.id)
433
-
434
- const space = index === 0 ? 8 : 0
435
- if (index) {
436
- doc.addPage()
437
- }
438
-
439
- doc.setFontSize(13)
440
- doc.text(name, 20, space + 20)
441
- doc.setFontSize(11)
442
- doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
443
- doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
444
-
445
- if (!userData.allWeek && userData.weekDays) {
446
- doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '') +
447
- (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '') +
448
- (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '') +
449
- (userData.weekDays.thursday ? ' ' + translations.report.thursday + ',' : '') +
450
- (userData.weekDays.friday ? ' ' + translations.report.friday + ',' : '') +
451
- (userData.weekDays.saturday ? ' ' + translations.report.saturday + ',' : '') +
452
- (userData.weekDays.sunday ? ' ' + translations.report.sunday + ',' : '') +
453
- ' das ' +
454
- userData.dayHours.startTime + ' - ' + userData.dayHours.endTime, 20, space + 30)
455
- }
434
+ pdfPageByDay(userData, d, index, doc, translations, reportData)
456
435
 
457
436
  const data = []
458
437
  d.summary.forEach(s => {
package/src/kms-report.js CHANGED
@@ -1,7 +1,10 @@
1
1
  const jsPDF = require('jspdf')
2
2
  require('jspdf-autotable')
3
3
  const { getStyle } = require('./reportStyle')
4
- const { headerFromUser, addTable } = require('./util/pdfDocument')
4
+ const {
5
+ headerFromUser, addTable,
6
+ pdfPageByDay
7
+ } = require('./util/pdfDocument')
5
8
  const { getUserPartner } = require('fleetmap-partners')
6
9
  const traccar = require('./util/traccar')
7
10
  const { getDates, convertFromUTC, getTranslations } = require('./util/utils')
@@ -165,53 +168,52 @@ function processDevices (from, to, devices, data, userData) {
165
168
  const trips = deviceTrips.filter(t => userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, deviceRoute))
166
169
 
167
170
  if (trips.length > 0 && userData.groupByDay) {
168
- trips.forEach(t => {
169
- t.tripDay = convertFromUTC(t.startTime, userData.user.attributes.timezone).toISOString().split('T')[0] + ' 12:00 PM'
170
- })
171
- const groupedTrips = trips.reduce(
172
- (entryMap, e) => entryMap.set(e.tripDay, [...entryMap.get(e.tripDay) || [], e]),
173
- new Map()
174
- )
175
-
176
- const allDates = getDates(from, to)
177
-
178
- const days = []
179
- let keys = Array.from(groupedTrips.keys())
180
- allDates.forEach(d => {
181
- const day = d.toISOString().split('T')[0] + ' 12:00 PM'
182
- if (!keys.includes(day)) {
183
- groupedTrips.set(day, [])
184
- }
185
- })
171
+ trips.forEach(t => {
172
+ t.tripDay = convertFromUTC(t.startTime, userData.user.attributes.timezone).toISOString().split('T')[0] + ' 12:00 PM'
173
+ })
174
+ const groupedTrips = trips.reduce(
175
+ (entryMap, e) => entryMap.set(e.tripDay, [...entryMap.get(e.tripDay) || [], e]),
176
+ new Map()
177
+ )
178
+
179
+ const allDates = getDates(from, to)
180
+
181
+ const days = []
182
+ let keys = Array.from(groupedTrips.keys())
183
+ allDates.forEach(d => {
184
+ const day = d.toISOString().split('T')[0] + ' 12:00 PM'
185
+ if (!keys.includes(day)) {
186
+ groupedTrips.set(day, [])
187
+ }
188
+ })
186
189
 
187
- keys = Array.from(groupedTrips.keys())
188
- keys.sort((a, b) => new Date(a).getTime() - new Date(b).getTime())
189
- keys.forEach(key => {
190
- const currentDate = new Date(key)
191
- const dayTrips = groupedTrips.get(key)
192
- const day = {
193
- date: currentDate,
194
- kms: 0
195
- }
190
+ keys = Array.from(groupedTrips.keys())
191
+ keys.sort((a, b) => new Date(a).getTime() - new Date(b).getTime())
192
+ keys.forEach(key => {
193
+ const currentDate = new Date(key)
194
+ const dayTrips = groupedTrips.get(key)
195
+ const day = {
196
+ date: currentDate,
197
+ kms: 0
198
+ }
196
199
 
197
- day.kms = dayTrips.reduce((a, b) => a + b.distance, 0)
200
+ day.kms = dayTrips.reduce((a, b) => a + b.distance, 0)
198
201
 
199
- days.push(day)
200
- })
201
- devicesResult.push({
202
- device: d,
203
- days
204
- })
205
- } else {
206
- devicesResult.push({
207
- device: d,
208
- summary: {
209
- distance: trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
210
- }
211
- })
212
- }
202
+ days.push(day)
203
+ })
204
+ devicesResult.push({
205
+ device: d,
206
+ days
207
+ })
208
+ } else {
209
+ devicesResult.push({
210
+ device: d,
211
+ summary: {
212
+ distance: trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
213
+ }
214
+ })
213
215
  }
214
-
216
+ }
215
217
 
216
218
  return devicesResult
217
219
  }
@@ -246,55 +248,34 @@ async function exportKmsReportToPDF (userData, reportData) {
246
248
  }
247
249
  if (kmsData) {
248
250
  const orientation = userData.groupByDay ? 'p' : 'l'
251
+ // eslint-disable-next-line new-cap
249
252
  const doc = new jsPDF.jsPDF(orientation)
250
253
  await headerFromUser(doc, translations.report.titleKmsReport, userData.user, orientation)
251
254
 
252
255
  if (userData.groupByDay) {
253
256
  kmsData.forEach(function (d, index) {
254
- const name = userData.byDriver ? d.driver.name : d.device.name
255
- const group = userData.byDriver ? userData.groups.find(g => g.drivers.includes(d.id)) : userData.groups.find(g => d.device.groupId === g.id)
256
-
257
- const space = index === 0 ? 8 : 0
258
- if (index) {
259
- doc.addPage()
260
- }
261
-
262
- doc.setFontSize(13)
263
- doc.text(name, 20, space + 20)
264
- doc.setFontSize(11)
265
- doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
266
- doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
267
-
268
- if (!userData.allWeek && userData.weekDays) {
269
- doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '') +
270
- (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '') +
271
- (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '') +
272
- (userData.weekDays.thursday ? ' ' + translations.report.thursday + ',' : '') +
273
- (userData.weekDays.friday ? ' ' + translations.report.friday + ',' : '') +
274
- (userData.weekDays.saturday ? ' ' + translations.report.saturday + ',' : '') +
275
- (userData.weekDays.sunday ? ' ' + translations.report.sunday + ',' : '') +
276
- ' das ' +
277
- userData.dayHours.startTime + ' - ' + userData.dayHours.endTime, 20, space + 30)
278
- }
257
+ pdfPageByDay(userData, d, index, doc, translations, reportData)
279
258
 
280
259
  const data = []
281
- d.days.forEach(day => {
282
- const temp = [
283
- new Date(day.date).toLocaleDateString() + ' - ' + weekDays[day.date.getDay()],
284
- (day.kms / 1000).toFixed(0)
285
- ]
260
+ if (d.days) {
261
+ d.days.forEach(day => {
262
+ const temp = [
263
+ new Date(day.date).toLocaleDateString() + ' - ' + weekDays[day.date.getDay()],
264
+ (day.kms / 1000).toFixed(0)
265
+ ]
266
+
267
+ data.push(temp)
268
+ })
286
269
 
287
- data.push(temp)
288
- })
270
+ const footValues = []
271
+ footValues.push(
272
+ '',
273
+ (d.days.reduce((a, b) => a + b.kms, 0) / 1000).toFixed(0)
274
+ )
289
275
 
290
- const footValues = []
291
- footValues.push(
292
- '',
293
- (d.days.reduce((a, b) => a + b.kms, 0) / 1000).toFixed(0)
294
- )
295
-
296
- const style = getStyle(getUserPartner(userData.user))
297
- addTable(doc, headers, data, footValues, style, 40)
276
+ const style = getStyle(getUserPartner(userData.user))
277
+ addTable(doc, headers, data, footValues, style, 40)
278
+ }
298
279
  })
299
280
  } else {
300
281
  doc.setFontSize(11)
@@ -7,6 +7,7 @@ const { convertToLocaleString, getTranslations } = require('./util/utils')
7
7
  const traccarHelper = require('./util/traccar')
8
8
  const { devicesToProcess } = require('./util/device')
9
9
  const automaticReports = require('./automaticReports')
10
+ const { getDriverName } = require('./util/driver')
10
11
 
11
12
  const fileName = 'LocationReport'
12
13
 
@@ -248,7 +249,7 @@ async function exportLocationReportToPDF (userData, reportData) {
248
249
  a.address,
249
250
  Math.round(a.speed * 1.85200),
250
251
  a.attributes.ignition ? translations.report.ignitionOn : translations.report.ignitionOff,
251
- userData.byDriver ? a.vehicleName : getDriverName(a, userData.drivers)
252
+ userData.byDriver ? a.vehicleName : getDriverName(a.attributes.driverUniqueId, a.fixTime, userData)
252
253
  ]
253
254
 
254
255
  if (userData.includeTemperature) {
@@ -389,7 +390,7 @@ function exportLocationReportToExcel (userData, reportData) {
389
390
  address: a.address,
390
391
  speed: Math.round(a.speed * 1.85200),
391
392
  ignition: a.attributes.ignition ? translations.report.ignitionOn : translations.report.ignitionOff,
392
- driver: userData.byDriver ? d.driver.name : getDriverName(a, userData.drivers),
393
+ driver: userData.byDriver ? d.driver.name : getDriverName(a.attributes.driverUniqueId, a.fixTime, userData),
393
394
  temperature: getTempValue(a),
394
395
  digitalport1: getDigitalPortValue(a, 1, translations, d.device.attributes),
395
396
  digitalport2: getDigitalPortValue(a, 2, translations, d.device.attributes),
@@ -426,15 +427,6 @@ function getLocationDate (location, user) {
426
427
  return convertToLocaleString(location.fixTime, user.attributes.lang, user.attributes.timezone)
427
428
  }
428
429
 
429
- function getDriverName (location, drivers) {
430
- if (location.attributes.driverUniqueId) {
431
- const driver = drivers.find(d => d.uniqueId === location.attributes.driverUniqueId)
432
- return driver ? driver.name : location.attributes.driverUniqueId
433
- }
434
-
435
- return ''
436
- }
437
-
438
430
  function getMaxSpeed (data) {
439
431
  const values = data.map(item => {
440
432
  return item.speed
@@ -33,15 +33,11 @@ async function createStopReportByDevice (from, to, userData, traccar) {
33
33
 
34
34
  let deviceCount = 0
35
35
  for (const slice of sliced) {
36
- const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, false, false, true, false, deviceCount, devices.length)
37
- const stopsData = allInOne.stops
36
+ const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, true, false, true, false, deviceCount, devices.length)
37
+ console.log('Stops:' + allInOne.stops.length)
38
38
 
39
- console.log('Stops:' + stopsData.length)
40
-
41
- if (stopsData.length > 0) {
42
- allData.devices.push(...processDevices(from, to, slice, {
43
- stops: stopsData
44
- }, userData))
39
+ if (allInOne.stops.length > 0) {
40
+ allData.devices.push(...processDevices(from, to, slice, allInOne, userData))
45
41
  }
46
42
  deviceCount = deviceCount + slice.length
47
43
  }
@@ -54,6 +50,7 @@ function processDevices (from, to, devices, data, userData) {
54
50
 
55
51
  devices.forEach(d => {
56
52
  const deviceStops = data.stops.filter(t => t.deviceId === d.id)
53
+ const route = data.route.filter(t => t.deviceId === d.id)
57
54
 
58
55
  deviceStops.forEach(stop => {
59
56
  const nearestPOIs = getNearestPOIs(stop.longitude, stop.latitude, userData.geofences)
@@ -75,6 +72,10 @@ function processDevices (from, to, devices, data, userData) {
75
72
  })
76
73
 
77
74
  if (deviceStops.length > 0) {
75
+ if (route.length && !route[0].attributes.ignition) {
76
+ deviceStops[0].startTime = undefined
77
+ }
78
+
78
79
  const deviceData = {
79
80
  device: d,
80
81
  from,
@@ -211,11 +212,11 @@ function exportStopReportToExcel (userData, reportData) {
211
212
  }
212
213
 
213
214
  function getStopStart (user, stop) {
214
- return convertToLocaleString(stop.startTime, user.attributes.lang, user.attributes.timezone)
215
+ return stop.startTime ? convertToLocaleString(stop.startTime, user.attributes.lang, user.attributes.timezone) : ''
215
216
  }
216
217
 
217
218
  function getStopEnd (user, stop) {
218
- return convertToLocaleString(stop.endTime, user.attributes.lang, user.attributes.timezone)
219
+ return stop.endTime ? convertToLocaleString(stop.endTime, user.attributes.lang, user.attributes.timezone) : ''
219
220
  }
220
221
 
221
222
  exports.createStopReport = createStopReport
@@ -10,7 +10,7 @@ const trips = require('./util/trips')
10
10
  const { isInsideTimetable, isPartialInsideTimetable, calculateTrip, getTripIdleTime } = require('./util/trips')
11
11
  const { devicesToProcess, deviceName } = require('./util/device')
12
12
  const { getIdleEvents } = require('./util/route')
13
- const { reportByDriver } = require('./util/driver')
13
+ const { reportByDriver, getDriverName } = require('./util/driver')
14
14
  const { getNearestPOIs } = require('./util/geofence')
15
15
 
16
16
  const fileName = 'TripReport'
@@ -346,7 +346,7 @@ async function exportTripReportToPDF (userData, reportData) {
346
346
  if (userData.byDriver) {
347
347
  temp.push(a.deviceName)
348
348
  } else {
349
- temp.push(getDriverName(userData.drivers, a.driverUniqueId))
349
+ temp.push(getDriverName(a.driverUniqueId, a.startTime, userData))
350
350
  }
351
351
 
352
352
  data.push(temp)
@@ -436,7 +436,7 @@ function exportTripReportToExcel (userData, reportData) {
436
436
  data = data.concat([{}])
437
437
  data = data.concat(d.trips.map(a => {
438
438
  return {
439
- name: userData.byDriver ? getDriverName(userData.drivers, a.driverUniqueId) : a.deviceName,
439
+ name: userData.byDriver ? d.driver.name : a.deviceName,
440
440
  date: getTripDate(userData.user, a),
441
441
  start: getTripStart(userData.user, a),
442
442
  end: getTripEnd(userData.user, a),
@@ -449,7 +449,7 @@ function exportTripReportToExcel (userData, reportData) {
449
449
  maxSpeed: Math.round(a.maxSpeed * 1.85200),
450
450
  consumption: a.fuelConsumption && a.fuelConsumption.toFixed(1),
451
451
  avgConsumption: a.avgFuelConsumption && a.avgFuelConsumption.toFixed(1),
452
- subname: userData.byDriver ? a.deviceName : getDriverName(userData.drivers, a.driverUniqueId)
452
+ subname: userData.byDriver ? a.deviceName : getDriverName(a.driverUniqueId, a.startTime, userData)
453
453
  }
454
454
  }))
455
455
  // Totals
@@ -487,17 +487,6 @@ function getTripEnd (user, trip) {
487
487
  return convertToLocaleTimeString(trip.endTime, user.attributes.lang, user.attributes.timezone)
488
488
  }
489
489
 
490
- function getDriverName (drivers, driverUniqueId) {
491
- if (driverUniqueId) {
492
- const driver = drivers.find(d => d.uniqueId === driverUniqueId)
493
- return driver
494
- ? driver.name + ' ' + ((driver.attributes && driver.attributes.notes) || '')
495
- : driverUniqueId
496
- }
497
-
498
- return ''
499
- }
500
-
501
490
  function getSumTotalKms (user, data) {
502
491
  const values = data.map(item => {
503
492
  return item.totalKms ? parseFloat(item.totalKms) : 0
@@ -37,5 +37,30 @@ async function reportByDriver (devices, traccar, from, to, userData, processor)
37
37
  return report
38
38
  }
39
39
 
40
+ function getDriverUniqueIdTimeline (driverUniqueIdTimeline, driverUniqueId, date) {
41
+ return driverUniqueIdTimeline.find(d => d.uniqueId === driverUniqueId &&
42
+ new Date(d.startDate) <= new Date(date) &&
43
+ new Date(d.endDate) >= new Date(date))
44
+ }
45
+
46
+ function getDriverName (driverUniqueId, date, userData) {
47
+ if (driverUniqueId) {
48
+ const timeline = getDriverUniqueIdTimeline(userData.driverUniqueIdTimeline, driverUniqueId, date)
49
+
50
+ if (timeline) {
51
+ return timeline.driverName
52
+ }
53
+
54
+ const driver = userData.drivers.find(d => d.uniqueId === driverUniqueId)
55
+ return driver
56
+ ? driver.name + ' ' + ((driver.attributes && driver.attributes.notes) || '')
57
+ : driverUniqueId
58
+ }
59
+
60
+ return ''
61
+ }
62
+
63
+ exports.getDriverName = getDriverName
64
+ exports.getDriverUniqueIdTimeline = getDriverUniqueIdTimeline
40
65
  exports.devicesByDriver = devicesByDriver
41
66
  exports.reportByDriver = reportByDriver