fleetmap-reports 1.0.976 → 1.0.977

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.
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "1.0.976",
3
+ "version": "1.0.977",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -19,6 +19,7 @@ function Reports (config, axios, cookieJar) {
19
19
  drivers: await this.traccar.drivers.driversGet().then(d => d.data),
20
20
  geofences: await this.traccar.geofences.geofencesGet().then(d => d.data),
21
21
  byGroup: false,
22
+ allWeek: true,
22
23
  dayHours: { startTime: '00:00', endTime: '23:59' }
23
24
  }
24
25
  }
@@ -183,5 +184,25 @@ function Reports (config, axios, cookieJar) {
183
184
  this.stopReportToExcel = (userData, reportData) => {
184
185
  return require('./stop-report').exportStopReportToExcel(userData, reportData)
185
186
  }
187
+
188
+ this.performanceReport = (from, to, userData) => {
189
+ return require('./partnerReports/performance-report').createPerformanceReport(from, to, userData, this.traccar)
190
+ }
191
+ this.performanceReportToPDF = (userData, reportData) => {
192
+ return require('./partnerReports/performance-report').exportPerformanceReportToPDF(userData, reportData)
193
+ }
194
+ this.performanceReportToExcel = (userData, reportData) => {
195
+ return require('./partnerReports/performance-report').exportPerformanceReportToExcel(userData, reportData)
196
+ }
197
+
198
+ this.dailyUseReport = (from, to, userData) => {
199
+ return require('./partnerReports/dailyuse-report').createDailyUseReport(from, to, userData, this.traccar)
200
+ }
201
+ this.dailyUseReportToPDF = (userData, reportData) => {
202
+ return require('./partnerReports/dailyuse-report').exportDailyUseReportToPDF(userData, reportData)
203
+ }
204
+ this.dailyUseReportToExcel = (userData, reportData) => {
205
+ return require('./partnerReports/dailyuse-report').exportDailyUseReportToExcel(userData, reportData)
206
+ }
186
207
  }
187
208
  module.exports = Reports
@@ -1,7 +1,11 @@
1
1
  const traccarHelper = require('../util/traccar')
2
2
  const { isInsideTimetable } = require('../util/trips')
3
- const { convertFromUTCDate } = require('../util/utils')
3
+ const { convertFromUTCDate, getTranslations, convertMS } = require('../util/utils')
4
4
  const automaticReports = require('../automaticReports')
5
+ const jsPDF = require('jspdf')
6
+ const { getStyle } = require('../reportStyle')
7
+ const { getUserPartner } = require('fleetmap-partners')
8
+ const { addTable, headerFromUser } = require('../util/pdfDocument')
5
9
 
6
10
  async function createDailyUseReport (from, to, userData, traccar) {
7
11
  const reportData = []
@@ -77,4 +81,78 @@ function processDeviceData (allInOne, d, userData) {
77
81
  }
78
82
  }
79
83
 
84
+ async function exportDailyUseReportToPDF (userData, reportData) {
85
+ const translations = getTranslations(userData)
86
+
87
+ const headers = [
88
+ translations.report.vehicle,
89
+ 'Utilisation',
90
+ 'Conduite',
91
+ 'Ralenti',
92
+ 'Totale (L)',
93
+ 'Conduite (L)',
94
+ 'Ralenti (L)',
95
+ 'Conduite (Km)',
96
+ 'Économique',
97
+ 'Économique (L)',
98
+ 'Économique (Km)',
99
+ 'Cruise Control',
100
+ 'Cruise Control (L)',
101
+ 'Cruise Control (Km)',
102
+ 'Tour moteur élevé',
103
+ 'Accéleration brusque (Sum)',
104
+ 'Freinage brusque (Sum)',
105
+ 'Virages brusque'
106
+ ]
107
+
108
+ const data = []
109
+ reportData.forEach(d => {
110
+ const row = [
111
+ d.device,
112
+ convertMS((d.drivingTime + d.idleTime) * 1000),
113
+ convertMS(d.drivingTime * 1000),
114
+ convertMS(d.idleTime * 1000),
115
+ d.drivingConsumption + d.idleConsumption,
116
+ d.drivingConsumption,
117
+ d.idleConsumption,
118
+ d.drivingDistance.toFixed(2),
119
+ convertMS(d.economicTime * 1000),
120
+ d.economicConsumption.toFixed(1),
121
+ d.economicDistance.toFixed(2),
122
+ convertMS(d.cruiseControlTime * 1000),
123
+ d.cruiseControlConsumption.toFixed(1),
124
+ d.cruiseControlDistance.toFixed(2),
125
+ convertMS(d.highEngineRPM * 1000),
126
+ d.hardAcceleration,
127
+ d.hardBraking,
128
+ d.hardCornering
129
+ ]
130
+ data.push(row)
131
+ })
132
+
133
+ const doc = new jsPDF.jsPDF('l')
134
+ await headerFromUser(doc, 'Rapport de Performance', userData.user)
135
+ const style = getStyle(getUserPartner(userData.user))
136
+
137
+ const footValues = []
138
+
139
+ doc.setFontSize(11)
140
+ doc.text(new Date(userData.from).toLocaleString() + ' - ' + new Date(userData.to).toLocaleString(), 20, 33)
141
+
142
+ style.headerFontSize = 8
143
+ style.bodyFontSize = 7
144
+ const columnStyles = {}
145
+ for (let i = 1; i < 18; i++) {
146
+ columnStyles[i] = { halign: 'right' }
147
+ }
148
+ addTable(doc, headers, data, footValues, style, 40, columnStyles)
149
+ return doc
150
+ }
151
+
152
+ function exportDailyUseReportToExcel (userData, reportData) {
153
+
154
+ }
155
+
80
156
  exports.createDailyUseReport = createDailyUseReport
157
+ exports.exportDailyUseReportToPDF = exportDailyUseReportToPDF
158
+ exports.exportDailyUseReportToExcel = exportDailyUseReportToExcel
@@ -0,0 +1,46 @@
1
+ async function createPassengerReport (from, to, userData, traccar) {
2
+ //const allDevices = await traccar.axios.get('/devices').then(d => d.data)
3
+ const devices = userData.devices
4
+ const drivers = await traccar.axios.get('/drivers').then(d => d.data)
5
+ const groups = await traccar.axios.get('/groups').then(d => d.data)
6
+ await Promise.all(groups.map(async g => {
7
+ const driversByGroup = await traccar.axios.get(`/drivers?groupId=${g.id}`).then(d => d.data)
8
+ driversByGroup.forEach(d => {
9
+ const _driver = drivers.find(a => a.id === d.id)
10
+ if (_driver) {
11
+ _driver.groupName = g.name
12
+ }
13
+ })
14
+ }))
15
+ const eventsUrl = `/reports/events?${devices.map(d => 'deviceId=' + d.id).join('&')
16
+ }&from=${from.toISOString()
17
+ }&to=${to.toISOString()
18
+ }`
19
+ const positionsUrl = `/reports/route?${devices.map(d => 'deviceId=' + d.id).join('&')
20
+ }&from=${from.toISOString()
21
+ }&to=${to.toISOString()
22
+ }`
23
+ const events = await traccar.axios.get(eventsUrl).then(d => d.data)
24
+ const positions = await traccar.axios.get(positionsUrl).then(d => d.data)
25
+ events.forEach(e => {
26
+ delete e.attributes
27
+ const driver = getDriver(e, positions, drivers)
28
+ const position = positions.find(p => p.id === e.positionId)
29
+ e.deviceName = devices.find(d => d.id === e.deviceId).name
30
+ e.groupName = driver.groupName || ''
31
+ e.fixtime = position && new Date(position.fixTime)
32
+ e.notes = driver && driver.attributes.notes
33
+ e.driverName = (driver && driver.name) || (position && position.attributes.driverUniqueId)
34
+ })
35
+ return events
36
+ }
37
+
38
+ function getDriver (event, positions, drivers) {
39
+ const p = positions.find(p => p.id === event.positionId)
40
+ if (!p) { return '' }
41
+ const d = drivers.find(d => d.uniqueId === p.attributes.driverUniqueId)
42
+ if (!d) { return '' }
43
+ return d
44
+ }
45
+
46
+ exports.createPassengerReport = createPassengerReport
@@ -0,0 +1,11 @@
1
+ const traccarHelper = require('./util/traccar')
2
+ const { devicesToProcess } = require('./util/device')
3
+
4
+ async function createTemperatureReport (from, to, userData, traccar) {
5
+ const devices = devicesToProcess(userData)
6
+
7
+ const allInOne = await traccarHelper.getAllInOne(traccar, from, to, devices, true, false, false, false)
8
+ const routeData = allInOne.route
9
+
10
+
11
+ }
@@ -35,8 +35,8 @@ describe('activity report', function () {
35
35
  const report = await getReports()
36
36
  const userData = await report.getUserData()
37
37
  userData.groupByDay = true
38
- const data = await report.activityReport(new Date(2022, 0, 1, 0, 0, 0, 0),
39
- new Date(2022, 0, 20, 23, 59, 59, 0),
38
+ const data = await report.activityReport(new Date(2023, 9, 1, 0, 0, 0, 0),
39
+ new Date(2023, 9, 20, 23, 59, 59, 0),
40
40
  userData)
41
41
  assert.equal(data.length, 1)
42
42
  const device = data[0].devices.find(d => d.device.id === 22326)
@@ -0,0 +1,19 @@
1
+ const { getReports } = require('./index')
2
+ const assert = require('assert')
3
+ const { createDailyUseReport } = require('../partnerReports/dailyuse-report')
4
+
5
+ describe('dailyuse', function () {
6
+ // eslint-disable-next-line no-undef
7
+ it('dailyuse report', async () => {
8
+ const report = await getReports()
9
+ const userData = await report.getUserData()
10
+
11
+ const data = await createDailyUseReport(
12
+ new Date(Date.UTC(2023, 10, 1, 0, 0, 0, 0)),
13
+ new Date(Date.UTC(2023, 10, 6, 23, 59, 59, 0)),
14
+ userData,
15
+ report.traccar)
16
+ console.log(data)
17
+ assert.equal(data[0].consumption, 19.799999999999997)
18
+ }, 8000000)
19
+ })
@@ -0,0 +1,23 @@
1
+ const { getReports } = require('./index')
2
+ const assert = require('assert')
3
+ const { convertMS } = require('../util/utils')
4
+ const { createGPSJumpReport } = require('../partnerReports/gpsjump-report')
5
+
6
+ // eslint-disable-next-line no-undef
7
+ describe('gpsjump', function () {
8
+ // eslint-disable-next-line no-undef
9
+ it('gpsjump report', async () => {
10
+ const report = await getReports()
11
+ const userData = await report.getUserData()
12
+ userData.devices = userData.devices.filter(d => d.id === 22326)
13
+ const data = await createGPSJumpReport(
14
+ new Date(Date.UTC(2023, 6, 1, 0, 0, 0, 0)),
15
+ new Date(Date.UTC(2023, 6, 17, 23, 59, 59, 0)),
16
+ userData,
17
+ report.traccar)
18
+ console.log(data)
19
+ assert.equal(data[0].consumption, 19.799999999999997)
20
+ assert.equal(data[0].distance, 326.7657)
21
+ assert.equal(convertMS(data[1].time * 1000, false), '08:19')
22
+ }, 8000000)
23
+ })
@@ -1,7 +1,12 @@
1
1
  const { getReports } = require('./index')
2
- const { createPerformanceReport } = require('../partnerReports/performance-report')
2
+ const { createPerformanceReport, exportPerformanceReportToExcel } = require('../partnerReports/performance-report')
3
3
  const assert = require('assert')
4
4
  const { convertMS } = require('../util/utils')
5
+ const xlsx = require('@jcardus/json-as-xlsx')
6
+ const { createActivityReport } = require('../activity-report')
7
+ const { createFuelConsumptionReport } = require('../fuelconsumption-report')
8
+ const { createPassengerReport } = require('../partnerReports/passenger-report')
9
+
5
10
  // eslint-disable-next-line no-undef
6
11
  describe('performance', function () {
7
12
  // eslint-disable-next-line no-undef
@@ -15,22 +20,59 @@ describe('performance', function () {
15
20
  userData,
16
21
  report.traccar)
17
22
  console.log(data)
23
+
18
24
  assert.equal(data[0].consumption, 19.799999999999997)
19
- assert.equal(data[0].distance, 326.7657)
20
- assert.equal(convertMS(data[1].time * 1000, false), '08:19')
25
+ assert.equal(data[0].distance, 326.8844199999999)
26
+ assert.equal(convertMS(data[1].time * 1000, false), '08:31')
21
27
  }, 8000000)
22
28
 
23
29
  // eslint-disable-next-line no-undef
24
30
  it('performance report inofleet', async () => {
25
31
  const report = await getReports()
26
32
  const userData = await report.getUserData()
27
- userData.devices = userData.devices.filter(d => d.id === 69114)
33
+ userData.devices = userData.devices.filter(d => d.id === 126442)
28
34
  const data = await createPerformanceReport(
29
- new Date(Date.UTC(2023, 5, 11, 0, 0, 0, 0)),
30
- new Date(Date.UTC(2023, 6, 11, 23, 59, 59, 0)),
35
+ new Date(Date.UTC(2023, 11, 9, 14, 58, 0, 0)),
36
+ new Date(Date.UTC(2023, 11, 18, 9, 28, 0, 0)),
31
37
  userData,
32
38
  report.traccar)
33
39
  console.log(data)
40
+
41
+ const data2 = await createFuelConsumptionReport(
42
+ new Date(Date.UTC(2023, 11, 9, 14, 58, 0, 0)),
43
+ new Date(Date.UTC(2023, 11, 18, 9, 28, 0, 0)),
44
+ userData,
45
+ report.traccar)
46
+ console.log(data2)
47
+
34
48
  assert.equal(data[0].consumption, 168.51989999999998)
35
49
  }, 8000000)
50
+
51
+ it('performance report export excel', async () => {
52
+ const report = await getReports()
53
+ const userData = await report.getUserData()
54
+ const data = await createPerformanceReport(
55
+ new Date(Date.UTC(2023, 6, 1, 0, 0, 0, 0)),
56
+ new Date(Date.UTC(2023, 6, 17, 23, 59, 59, 0)),
57
+ userData,
58
+ report.traccar)
59
+ console.log(data)
60
+ const excel = exportPerformanceReportToExcel(userData, data)
61
+ xlsx([{
62
+ sheet: 'Sheet1',
63
+ columns: excel.headers,
64
+ content: excel.data
65
+ }], excel.settings)
66
+ }, 8000000)
67
+
68
+ it('passenger report export excel', async () => {
69
+ const report = await getReports()
70
+ const userData = await report.getUserData()
71
+ const data = await createPassengerReport(
72
+ new Date(Date.UTC(2023, 11, 1, 0, 0, 0, 0)),
73
+ new Date(Date.UTC(2023, 11, 20, 23, 59, 59, 0)),
74
+ userData,
75
+ report.traccar)
76
+ console.log(data)
77
+ }, 8000000)
36
78
  })
@@ -139,7 +139,11 @@ async function createTripReportByDriver (from, to, userData, traccar) {
139
139
  d.totalDuration = d.trips.reduce((a, b) => a + b.duration, 0)
140
140
  d.totalDistance = d.trips.reduce((a, b) => a + b.distance, 0)
141
141
  d.maxSpeed = d.trips.reduce((a, b) => { return a > b.maxSpeed ? a : b.maxSpeed }, 0)
142
- d.trips.sort((a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime())
142
+ d.trips = d.trips.sort((a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime())
143
+
144
+ d.trips.forEach((trip, i) => {
145
+ trip.stopDuration = (i !== d.trips.length - 1 ? (new Date(d.trips[i + 1].startTime) - new Date(trip.endTime)) : (to - new Date(trip.endTime))) - trip.stopEngineHours
146
+ })
143
147
  })
144
148
 
145
149
  return report
@@ -274,8 +278,8 @@ async function processDrivers (from, to, userData, data) {
274
278
  filteredTrips.push(t)
275
279
  }
276
280
  }
277
- filteredTrips.sort((a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime())
278
- filteredTrips.forEach(function (trip, i) {
281
+
282
+ filteredTrips.forEach(trip => {
279
283
  trip.totalKms = trip.distance / 1000
280
284
 
281
285
  const nearestPOIs = getNearestPOIs(trip.endLon, trip.endLat, userData.geofences)
@@ -286,7 +290,6 @@ async function processDrivers (from, to, userData, data) {
286
290
  const stop = data.stops.find(s => s.deviceId === trip.deviceId && (new Date(s.startTime).getTime() === new Date(trip.endTime).getTime()))
287
291
 
288
292
  trip.stopEngineHours = getTripIdleTime(trip, idleEvents) + (stop ? stop.engineHours : 0)
289
- trip.stopDuration = (i !== trips.length - 1 ? (new Date(trips[i + 1].startTime) - new Date(trip.endTime)) : (to - new Date(trip.endTime))) - trip.stopEngineHours
290
293
  })
291
294
 
292
295
  if (filteredTrips.length > 0) {
@@ -307,33 +307,33 @@ function calculateDistanceIn (zoneInOutDayData, deviceRoute, dayRoute, fromByDay
307
307
  return distanceIn
308
308
  }
309
309
 
310
- function calculateDistanceOut (zoneInOutDayData, dayRoute, fromByDay, toByDay, field, device) {
310
+ function calculateDistanceOut (zoneInOutDayData, dayRoute, from, to, field, device) {
311
311
  if (zoneInOutDayData.length) {
312
- const dataInsideDay = zoneInOutDayData.filter(z => z.outTime && new Date(z.outTime.fixTime) < toByDay)
312
+ const dataInsideDay = zoneInOutDayData.filter(z => z.outTime && new Date(z.outTime.fixTime) < to)
313
313
  let distanceOut = dataInsideDay.reduce((a, b) => a + (b[field] || 0), 0)
314
314
 
315
- if (zoneInOutDayData[0].inTime && new Date(zoneInOutDayData[0].inTime.fixTime) > fromByDay && (field !== 'distanceOutAny' || zoneInOutDayData[0].anyLastExit)) {
315
+ if (zoneInOutDayData[0].inTime && new Date(zoneInOutDayData[0].inTime.fixTime) > from && (field !== 'distanceOutAny' || zoneInOutDayData[0].anyLastExit)) {
316
316
  // Add distanceOut before the first entry
317
317
  const routeOut = dayRoute.filter(p =>
318
318
  new Date(p.fixTime).getTime() < new Date(zoneInOutDayData[0].inTime.fixTime).getTime() &&
319
- new Date(p.fixTime).getTime() >= fromByDay.getTime()
319
+ new Date(p.fixTime).getTime() >= from.getTime()
320
320
  )
321
321
  distanceOut = distanceOut + calculateDistance(routeOut, device.attributes['report.ignoreOdometer'])
322
322
  }
323
323
 
324
- if (zoneInOutDayData[zoneInOutDayData.length - 1].outTime && new Date(zoneInOutDayData[zoneInOutDayData.length - 1].outTime.fixTime) < toByDay && (field !== 'distanceOutAny' || zoneInOutDayData[zoneInOutDayData.length - 1].anyNextIn)) {
324
+ if (zoneInOutDayData[zoneInOutDayData.length - 1].outTime && new Date(zoneInOutDayData[zoneInOutDayData.length - 1].outTime.fixTime) < to && (field !== 'distanceOutAny' || zoneInOutDayData[zoneInOutDayData.length - 1].anyNextIn)) {
325
325
  // Add distanceOut after last exit
326
326
  distanceOut = distanceOut - zoneInOutDayData[zoneInOutDayData.length - 1][field]
327
327
  const routeOut = dayRoute.filter(p =>
328
328
  new Date(p.fixTime) >= new Date(zoneInOutDayData[zoneInOutDayData.length - 1].outTime.fixTime).getTime() &&
329
- new Date(p.fixTime) < toByDay.getTime())
329
+ new Date(p.fixTime) < to.getTime())
330
330
  distanceOut = distanceOut + calculateDistance(routeOut, device.attributes['report.ignoreOdometer'])
331
331
  }
332
332
 
333
333
  return distanceOut
334
334
  }
335
335
 
336
- return calculateDistance(dayRoute.filter(p => new Date(p.fixTime) >= fromByDay && new Date(p.fixTime) < toByDay), device.attributes['report.ignoreOdometer'])
336
+ return calculateDistance(dayRoute.filter(p => new Date(p.fixTime) >= from && new Date(p.fixTime) < to), device.attributes['report.ignoreOdometer'])
337
337
  }
338
338
 
339
339
  function analyseAlerts (alerts, deviceRoute, userData, from, to, device) {