fleetmap-reports 1.0.261 → 1.0.265

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.
@@ -3,13 +3,13 @@ const messages = require('../lang')
3
3
  const jsPDF = require('jspdf')
4
4
  const {convertMS, convertToLocaleString, convertToLocaleDateString, convertToLocaleTimeString} = require("./util/utils")
5
5
  require('jspdf-autotable')
6
- const {coordsDistance} = require("./util/utils")
7
6
  const drivers = require("./util/driver")
8
7
  const {getUserPartner} = require("fleetmap-partners")
9
8
  const {headerFromUser, addTable} = require("./util/pdfDocument")
10
9
  const {getStyle} = require("./reportStyle")
11
10
  const traccar = require("./util/traccar")
12
11
  const trips = require("./util/trips");
12
+ const {isInsideTimetable, addNearestPOIs} = require("./util/trips");
13
13
 
14
14
 
15
15
  let traccarInstance
@@ -23,26 +23,26 @@ async function createTripReport(from, to, reportUserData, traccar) {
23
23
  traccarInstance = traccar
24
24
  const reportData = []
25
25
 
26
- if(userData.byDriver){
27
- const allData = await createTripReportByDriver(from, to)
28
- reportData.push(allData)
26
+ if (userData.byDriver) {
27
+ console.log("ByDriver")
28
+ const report = await createTripReportByDriver(from, to)
29
+ reportData.push(report)
29
30
  }
30
- else if(userData.byGroup){
31
+ else if (userData.byGroup) {
31
32
  console.log("ByGroup")
32
33
  const allData = await createTripReportByGroup(from, to)
33
34
  reportData.push(...allData)
35
+ } else {
36
+ console.log("ByDevice")
37
+ const report = await createTripReportByDevice(from, to)
38
+ reportData.push(report)
34
39
  }
35
40
 
36
- const groupIds = userData.groups.map(g => g.id)
37
- const withoutGroupDevices = userData.byGroup ? userData.devices.filter(d => !groupIds.includes(d.groupId)) : userData.devices
38
-
39
- const allData = await createTripReportByDevice(from, to, withoutGroupDevices)
40
- reportData.push(allData)
41
-
42
41
  return reportData
43
42
  }
44
43
 
45
- async function createTripReportByDevice(from, to, devices) {
44
+ async function createTripReportByDevice(from, to) {
45
+ const devices = userData.devices
46
46
  const allData = {
47
47
  devices: [],
48
48
  xpert: devices.filter(d => d.attributes.xpert).length > 0
@@ -77,90 +77,75 @@ async function createTripReportByGroup(from, to) {
77
77
  }
78
78
 
79
79
  const response = await traccarInstance.reports.reportsTripsGet(from, to, null, [g.id])
80
- const data = response.data
80
+ const tripsData = response.data
81
81
 
82
82
  const stopsResponse = await traccarInstance.reports.reportsStopsGet(from, to, null, [g.id])
83
83
  const stopsData = stopsResponse.data
84
84
 
85
85
  devices.sort((a, b) => (a.name > b.name) ? 1 : -1)
86
86
 
87
- if (data.length > 0) {
88
-
89
- console.log('Trips:' + data.length)
90
- groupData.devices = processDevices(from, to, devices, data, stopsData, userData)
87
+ if (tripsData.length > 0) {
88
+ console.log('Trips:' + tripsData.length)
89
+ groupData.devices = processDevices(from, to, devices, tripsData, stopsData, userData)
91
90
 
92
91
  reportData.push(groupData)
93
92
  }
94
93
  }
95
94
  }
95
+
96
+ const groupIds = userData.groups.map(g => g.id)
97
+ const withoutGroupDevices = userData.devices.filter(d => !groupIds.includes(d.groupId))
98
+
99
+ const allData = await createTripReportByDevice(from, to, withoutGroupDevices)
100
+ reportData.push(allData)
101
+
96
102
  return reportData
97
103
  }
98
104
 
99
105
  async function createTripReportByDriver(from, to){
100
- const deviceIds = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
101
- console.log(deviceIds.length)
102
-
103
- let tripsData = []
104
- if(deviceIds.length > 0) {
105
- const response = await traccarInstance.reports.reportsTripsGet(from, to, deviceIds)
106
- tripsData = response.data
107
- }
106
+ const devices = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
107
+ console.log(devices.length)
108
108
 
109
- const allData = {
110
- drivers: []
109
+ if(!devices.length){
110
+ //Empty report
111
+ return { drivers:[] }
111
112
  }
112
113
 
113
- allData.drivers = processDrivers(from, to, userData.drivers, tripsData)
114
+ const tripsData = await traccar.getTrips(traccarInstance, from, to, devices)
114
115
 
115
- return allData
116
+ return { drivers: processDrivers(from, to, userData.drivers, tripsData)}
116
117
  }
117
118
 
118
119
  function processDevices(from, to, devices, data, stopsData) {
119
120
  const devicesResult = []
120
121
 
121
122
  devices.forEach(d => {
122
- const trips = data.filter(t => t.deviceId === d.id && (userData.allWeek || isInsideTimetable(t, userData)))
123
+ const trips = data.filter(t => t.deviceId === d.id
124
+ && (userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData)))
123
125
  const stops = stopsData.filter(s => s.deviceId === d.id)
124
126
 
125
- trips.forEach(t => {
126
- t.totalKms = t.distance / 1000
127
+ addNearestPOIs(trips, userData)
128
+
129
+ trips.forEach(trip => {
127
130
  if (automaticReports.deviceWithFuelInfo(d)) {
128
- t.fuelConsumption = Math.round((t.spentFuel * d.attributes.fuel_tank_capacity) / 100)
129
- t.avgFuelConsumption = t.totalKms > 0 ? Math.round(t.fuelConsumption * 100 / t.totalKms) : 0
131
+ trip.fuelConsumption = Math.round((t.spentFuel * d.attributes.fuel_tank_capacity) / 100)
132
+ trip.avgFuelConsumption = t.totalKms > 0 ? Math.round(t.fuelConsumption * 100 / t.totalKms) : 0
130
133
  } else {
131
- t.fuelConsumption = '-'
132
- t.avgFuelConsumption = '-'
134
+ trip.fuelConsumption = '-'
135
+ trip.avgFuelConsumption = '-'
133
136
  }
134
137
 
135
- const distance = userData.geofences
136
- .filter(g => g && g.area.startsWith('CIRCLE'))
137
- .map(g => {
138
- const str = g.area.substring('CIRCLE ('.length, g.area.indexOf(','))
139
- const coord = str.trim().split(' ')
140
- return {
141
- p: g,
142
- distance: Math.round(coordsDistance(parseFloat(coord[1]), parseFloat(coord[0]), t.endLon, t.endLat))
143
- }
144
- })
145
- const nearestPOIs = distance.filter(a => a.distance < 100).sort((a, b) => (a.distance > b.distance) ? 1 : -1)
146
- if (nearestPOIs.length > 0) {
147
- t.endPOIName = nearestPOIs[0].p.name
138
+ const stop = getStop(new Date(trip.startTime), stops)
139
+ if (stop) {
140
+ trip.stopDuration = (new Date(stop.endTime) - new Date(stop.startTime))
141
+ trip.stopEngineHours = stop.engineHours
142
+ } else {
143
+ trip.stopDuration = 0
144
+ trip.stopEngineHours = 0
148
145
  }
149
146
  })
150
147
 
151
148
  if (trips.length > 0) {
152
- trips.forEach(trip => {
153
- const stop = getStop(new Date(trip.startTime), stops)
154
-
155
- if (stop) {
156
- trip.stopDuration = (new Date(stop.endTime) - new Date(stop.startTime))
157
- trip.stopEngineHours = stop.engineHours
158
- } else {
159
- trip.stopDuration = 0
160
- trip.stopEngineHours = 0
161
- }
162
- })
163
-
164
149
  const deviceData = {
165
150
  device: d,
166
151
  from: from,
@@ -188,11 +173,12 @@ function processDrivers(from, to, drivers, data) {
188
173
  console.log(data)
189
174
  const driversResult = []
190
175
  drivers.forEach(d => {
191
- const trips = data.filter(t => t.driverUniqueId === d.uniqueId)
176
+ const trips = data.filter(t => t.driverUniqueId === d.uniqueId
177
+ && (userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData)))
192
178
 
193
- trips.forEach(t => {
194
- t.totalKms = t.distance / 1000
195
- })
179
+ trips.sort((a,b)=>new Date(a.startTime).getTime()-new Date(b.startTime).getTime())
180
+
181
+ addNearestPOIs(trips, userData)
196
182
 
197
183
  if (trips.length > 0) {
198
184
  const driverData = {
@@ -201,14 +187,13 @@ function processDrivers(from, to, drivers, data) {
201
187
  to: to,
202
188
  totalDuration: trips.reduce((a, b) => a + b.duration, 0),
203
189
  totalDistance: trips.reduce((a, b) => a + b.distance, 0),
204
- maxSpeed: trips.reduce((a, b) => {
205
- return a > b.maxSpeed ? a : b.maxSpeed
206
- }, 0),
190
+ maxSpeed: trips.reduce((a, b) => { return a > b.maxSpeed ? a : b.maxSpeed }, 0),
207
191
  trips: trips,
208
192
  }
209
193
  driversResult.push(driverData)
210
194
  }
211
195
  })
196
+ console.log(driversResult)
212
197
 
213
198
  return driversResult
214
199
  }
@@ -453,30 +438,6 @@ function getStop(tripEndDate, stops){
453
438
  return null
454
439
  }
455
440
 
456
- function isInsideTimetable(t, userData){
457
- const tripStart = new Date(t.startTime)
458
- const tripEnd = new Date(t.endTime)
459
-
460
- if((tripStart.getDay() === 0 && userData.weekDays.sunday) ||
461
- (tripStart.getDay() === 1 && userData.weekDays.monday) ||
462
- (tripStart.getDay() === 2 && userData.weekDays.tuesday) ||
463
- (tripStart.getDay() === 3 && userData.weekDays.wednesday) ||
464
- (tripStart.getDay() === 4 && userData.weekDays.thursday) ||
465
- (tripStart.getDay() === 5 && userData.weekDays.friday) ||
466
- (tripStart.getDay() === 6 && userData.weekDays.saturday)) {
467
-
468
- const startDate = new Date(new Date(tripStart).toISOString().split('T')[0] + ' '+userData.dayHours.startTime)
469
- const endDate = new Date(new Date(tripStart).toISOString().split('T')[0] + ' '+userData.dayHours.endTime)
470
-
471
- console.log(tripStart, startDate, endDate)
472
-
473
- //Trips inside time period
474
- return tripStart.getTime() > startDate.getTime() && tripEnd.getTime() < endDate.getTime()
475
- }
476
-
477
- return false
478
- }
479
-
480
441
  exports.createTripReport = createTripReport;
481
442
  exports.exportTripReportToPDF = exportTripReportToPDF;
482
443
  exports.exportTripReportToExcel = exportTripReportToExcel;
@@ -9,10 +9,9 @@ async function devicesByDriver(traccarInstance, from, to, drivers, devices){
9
9
  return e.attributes.driverUniqueId === d.uniqueId
10
10
  })
11
11
 
12
- deviceIds.push(events.map(e => e.deviceId))
12
+ deviceIds.push(...events.map(e => e.deviceId))
13
13
  }
14
-
15
- return [...new Set(deviceIds.flat())]
14
+ return devices.filter(d => deviceIds.includes(d.id))
16
15
  }
17
16
 
18
17
  exports.devicesByDriver = devicesByDriver
package/src/util/trips.js CHANGED
@@ -1,8 +1,30 @@
1
1
  const traccar = require("./traccar");
2
+ const {coordsDistance} = require("./utils");
2
3
 
3
4
  const minDistance = 0
4
5
  const minAvgSpeed = 0
5
6
 
7
+ function addNearestPOIs(trips, userData){
8
+ trips.forEach(t => {
9
+ t.totalKms = t.distance / 1000
10
+
11
+ const distance = userData.geofences
12
+ .filter(g => g && g.area.startsWith('CIRCLE'))
13
+ .map(g => {
14
+ const str = g.area.substring('CIRCLE ('.length, g.area.indexOf(','))
15
+ const coord = str.trim().split(' ')
16
+ return {
17
+ p: g,
18
+ distance: Math.round(coordsDistance(parseFloat(coord[1]), parseFloat(coord[0]), t.endLon, t.endLat))
19
+ }
20
+ })
21
+ const nearestPOIs = distance.filter(a => a.distance < 100).sort((a, b) => (a.distance > b.distance) ? 1 : -1)
22
+ if (nearestPOIs.length > 0) {
23
+ t.endPOIName = nearestPOIs[0].p.name
24
+ }
25
+ })
26
+ }
27
+
6
28
  async function checkTripsKms(traccarInstance, from, to, routeData, tripsData, devices) {
7
29
  console.log('checkTripsKms')
8
30
  const trips = tripsData.filter(t => t.distance === minDistance && t.averageSpeed > minAvgSpeed)
@@ -20,4 +42,35 @@ async function checkTripsKms(traccarInstance, from, to, routeData, tripsData, de
20
42
  }
21
43
  }
22
44
 
45
+ function isInsideTimetable(t, userData){
46
+ const tripStart = new Date(t.startTime)
47
+ const tripEnd = new Date(t.endTime)
48
+
49
+ if((tripStart.getDay() === 0 && userData.weekDays.sunday) ||
50
+ (tripStart.getDay() === 1 && userData.weekDays.monday) ||
51
+ (tripStart.getDay() === 2 && userData.weekDays.tuesday) ||
52
+ (tripStart.getDay() === 3 && userData.weekDays.wednesday) ||
53
+ (tripStart.getDay() === 4 && userData.weekDays.thursday) ||
54
+ (tripStart.getDay() === 5 && userData.weekDays.friday) ||
55
+ (tripStart.getDay() === 6 && userData.weekDays.saturday)) {
56
+
57
+ const startDate = new Date(new Date(tripStart).toISOString().split('T')[0] + ' '+userData.dayHours.startTime)
58
+ const endDate = new Date(new Date(tripStart).toISOString().split('T')[0] + ' '+userData.dayHours.endTime)
59
+
60
+ console.log(tripStart, startDate, endDate)
61
+
62
+ //Trips inside time period
63
+ if(startDate.getTime() < endDate.getTime()) {
64
+ return tripStart.getTime() > startDate.getTime() && tripEnd.getTime() < endDate.getTime()
65
+ } else {
66
+ return (tripStart.getTime() < endDate.getTime() && tripEnd.getTime() < endDate.getTime()) ||
67
+ (tripStart.getTime() > startDate.getTime())
68
+ }
69
+ }
70
+
71
+ return false
72
+ }
73
+
23
74
  exports.checkTripsKms = checkTripsKms
75
+ exports.isInsideTimetable = isInsideTimetable
76
+ exports.addNearestPOIs = addNearestPOIs