fleetmap-reports 1.0.250 → 1.0.255

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/src/kms-report.js CHANGED
@@ -1,4 +1,3 @@
1
- const {createActivityReport} = require("./activity-report");
2
1
  const messages = require('../lang')
3
2
  const jsPDF = require('jspdf')
4
3
  require('jspdf-autotable')
@@ -7,6 +6,7 @@ const {headerFromUser,addTable} = require("./util/pdfDocument");
7
6
  const {getUserPartner} = require("fleetmap-partners");
8
7
  const traccar = require("./util/traccar");
9
8
  const {getDates} = require("./util/utils");
9
+ const trips = require("./util/trips");
10
10
 
11
11
 
12
12
  let traccarInstance
@@ -26,12 +26,13 @@ async function createKmsReportByDevice(from, to) {
26
26
 
27
27
  devicesToProcess.sort((a, b) => (a.name > b.name) ? 1 : -1)
28
28
  const route = userData.allWeek ? [] : await traccar.getRoute(traccarInstance, from, to, devicesToProcess)
29
- const trips = await traccar.getTrips(traccarInstance, from, to, devicesToProcess)
29
+ const tripsData = await traccar.getTrips(traccarInstance, from, to, devicesToProcess)
30
30
 
31
- console.log('trips:' + trips.length)
31
+ console.log('trips:' + tripsData.length)
32
32
 
33
- if (trips.length > 0) {
34
- allData.devices = processDevices(from, to, devicesToProcess, route, trips)
33
+ if (tripsData.length > 0) {
34
+ await trips.checkTripsKms(traccarInstance, from, to, route, tripsData, devicesToProcess)
35
+ allData.devices = processDevices(from, to, devicesToProcess, route, tripsData)
35
36
  }
36
37
 
37
38
  return allData
@@ -52,84 +53,102 @@ function processDevices(from, to, devices, route, trips) {
52
53
  const deviceTrips = trips.filter(t => t.deviceId === d.id)
53
54
 
54
55
  if (deviceTrips.length > 0) {
55
- deviceTrips.forEach(t => t.tripDay = new Date(t.startTime).toISOString().split('T')[0] + ' 12:00 PM')
56
- const groupedTrips = deviceTrips.reduce(
57
- (entryMap, e) => entryMap.set(e.tripDay, [...entryMap.get(e.tripDay)||[], e]),
58
- new Map()
59
- )
60
-
61
- const allDates = getDates(from, to)
62
-
63
- const days = []
64
- let keys = Array.from(groupedTrips.keys())
65
- allDates.forEach(d => {
66
- const day = d.toISOString().split('T')[0] + ' 12:00 PM'
67
- if(!keys.includes(day)){
68
- groupedTrips.set(day, [])
69
- }
70
- })
71
-
72
- keys = Array.from(groupedTrips.keys())
73
- keys.sort((a,b)=>new Date(a).getTime()-new Date(b).getTime());
74
- keys.forEach(key => {
75
- const currentDate = new Date(key)
76
- let dayTrips = groupedTrips.get(key)
77
- const day = {
78
- date: currentDate,
79
- kms: 0
80
- }
81
-
82
- if(userData.allWeek){
83
- day.kms = dayTrips.reduce((a, b) => a + b.distance, 0) || 0
84
- } else {
85
- if((currentDate.getDay() === 0 && userData.weekDays.sunday) ||
86
- (currentDate.getDay() === 1 && userData.weekDays.monday) ||
87
- (currentDate.getDay() === 2 && userData.weekDays.tuesday) ||
88
- (currentDate.getDay() === 3 && userData.weekDays.wednesday) ||
89
- (currentDate.getDay() === 4 && userData.weekDays.thursday) ||
90
- (currentDate.getDay() === 5 && userData.weekDays.friday) ||
91
- (currentDate.getDay() === 6 && userData.weekDays.saturday)) {
92
-
93
- const startDate = new Date(new Date(key).toISOString().split('T')[0] + ' '+userData.dayHours.startTime)
94
- const endDate = new Date(new Date(key).toISOString().split('T')[0] + ' '+userData.dayHours.endTime)
95
-
96
- console.log(key, startDate, endDate)
97
-
98
- //Trips inside time period
99
- const filteredTrips = dayTrips.filter(t => new Date(t.startTime).getTime() > startDate.getTime()
100
- && new Date(t.endTime).getTime() < endDate.getTime())
56
+ if(userData.groupByDay) {
57
+ deviceTrips.forEach(t => t.tripDay = new Date(t.startTime).toISOString().split('T')[0] + ' 12:00 PM')
58
+ const groupedTrips = deviceTrips.reduce(
59
+ (entryMap, e) => entryMap.set(e.tripDay, [...entryMap.get(e.tripDay)||[], e]),
60
+ new Map()
61
+ )
101
62
 
102
- //Trip starts outside time period and ends inside time period
103
- const startIncompleteTrip = dayTrips.filter(t => new Date(t.startTime).getTime() < startDate.getTime()
104
- && new Date(t.endTime).getTime() > startDate.getTime())
63
+ const allDates = getDates(from, to)
64
+
65
+ const days = []
66
+ let keys = Array.from(groupedTrips.keys())
67
+ allDates.forEach(d => {
68
+ const day = d.toISOString().split('T')[0] + ' 12:00 PM'
69
+ if(!keys.includes(day)){
70
+ groupedTrips.set(day, [])
71
+ }
72
+ })
73
+
74
+ keys = Array.from(groupedTrips.keys())
75
+ keys.sort((a,b)=>new Date(a).getTime()-new Date(b).getTime());
76
+ keys.forEach(key => {
77
+ const currentDate = new Date(key)
78
+ let dayTrips = groupedTrips.get(key)
79
+ const day = {
80
+ date: currentDate,
81
+ kms: 0
82
+ }
105
83
 
106
- //Trip starts inside time period and ends outside time period
107
- const endIncompleteTrip = dayTrips.filter(t => new Date(t.startTime).getTime() < endDate.getTime()
84
+ if(userData.allWeek){
85
+ day.kms = dayTrips.reduce((a, b) => a + b.distance, 0) || 0
86
+ } else {
87
+ if((currentDate.getDay() === 0 && userData.weekDays.sunday) ||
88
+ (currentDate.getDay() === 1 && userData.weekDays.monday) ||
89
+ (currentDate.getDay() === 2 && userData.weekDays.tuesday) ||
90
+ (currentDate.getDay() === 3 && userData.weekDays.wednesday) ||
91
+ (currentDate.getDay() === 4 && userData.weekDays.thursday) ||
92
+ (currentDate.getDay() === 5 && userData.weekDays.friday) ||
93
+ (currentDate.getDay() === 6 && userData.weekDays.saturday)) {
94
+
95
+ const startDate = new Date(new Date(key).toISOString().split('T')[0] + ' '+userData.dayHours.startTime)
96
+ const endDate = new Date(new Date(key).toISOString().split('T')[0] + ' '+userData.dayHours.endTime)
97
+
98
+ console.log(key, startDate, endDate)
99
+
100
+ if(startDate.getTime() < endDate.getTime()) {
101
+ //Trips inside time period
102
+ const filteredTrips = dayTrips.filter(t => new Date(t.startTime).getTime() > startDate.getTime()
103
+ && new Date(t.endTime).getTime() < endDate.getTime())
104
+
105
+ day.kms = filteredTrips.reduce((a, b) => a + b.distance, 0) || 0
106
+ console.log('Inside trips:' + day.kms)
107
+ } else {
108
+ //Trips inside time period
109
+ const filteredTrips = dayTrips.filter(t => new Date(t.startTime).getTime() > startDate.getTime()
110
+ || new Date(t.endTime).getTime() < endDate.getTime())
111
+
112
+ day.kms = filteredTrips.reduce((a, b) => a + b.distance, 0) || 0
113
+ console.log('Inside trips:' + day.kms)
114
+ }
115
+
116
+ //Trip starts outside time period and ends inside time period
117
+ const startIncompleteTrip = dayTrips.filter(t => new Date(t.startTime).getTime() < startDate.getTime()
118
+ && new Date(t.endTime).getTime() > startDate.getTime())
119
+
120
+ //Trip starts inside time period and ends outside time period
121
+ const endIncompleteTrip = dayTrips.filter(t => new Date(t.startTime).getTime() < endDate.getTime()
108
122
  && new Date(t.endTime).getTime() > endDate.getTime())
109
123
 
110
- day.kms = filteredTrips.reduce((a, b) => a + b.distance, 0) || 0
111
- console.log('Inside trips:'+day.kms)
112
-
113
- if(startIncompleteTrip.length){
114
- const routeInside = route.filter(p => new Date(p.fixTime).getTime() > startDate.getTime() && new Date(p.fixTime).getTime() < new Date(startIncompleteTrip[0].endTime).getTime())
115
- day.kms = day.kms + (routeInside.reduce((a, b) => a + b.distance, 0) || 0)
116
- console.log('startIncompleteTrip:'+day.kms)
117
- }
118
-
119
- if(endIncompleteTrip.length){
120
- const routeInside = route.filter(p => new Date(p.fixTime).getTime() < endDate.getTime() && new Date(p.fixTime).getTime() > new Date(endIncompleteTrip[0].startTime).getTime())
121
- day.kms = day.kms + (routeInside.reduce((a, b) => a + b.distance, 0) || 0)
122
- console.log('endIncompleteTrip:'+day.kms)
124
+ if (startIncompleteTrip.length) {
125
+ const routeInside = route.filter(p => new Date(p.fixTime).getTime() > startDate.getTime() && new Date(p.fixTime).getTime() < new Date(startIncompleteTrip[0].endTime).getTime())
126
+ day.kms = day.kms + (routeInside.reduce((a, b) => a + b.distance, 0) || 0)
127
+ console.log('startIncompleteTrip:' + day.kms)
128
+ }
129
+
130
+ if (endIncompleteTrip.length) {
131
+ const routeInside = route.filter(p => new Date(p.fixTime).getTime() < endDate.getTime() && new Date(p.fixTime).getTime() > new Date(endIncompleteTrip[0].startTime).getTime())
132
+ day.kms = day.kms + (routeInside.reduce((a, b) => a + b.distance, 0) || 0)
133
+ console.log('endIncompleteTrip:' + day.kms)
134
+ }
123
135
  }
124
136
  }
125
- }
126
137
 
127
- days.push(day)
128
- })
129
- devicesResult.push({
130
- device: d,
131
- days: days
132
- })
138
+ days.push(day)
139
+ })
140
+ devicesResult.push({
141
+ device: d,
142
+ days: days
143
+ })
144
+ } else {
145
+ devicesResult.push({
146
+ device: d,
147
+ summary: {
148
+ distance: deviceTrips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0),
149
+ }
150
+ })
151
+ }
133
152
  }
134
153
  }
135
154
 
@@ -142,27 +161,22 @@ async function createKmsReport(from, to, reportUserData, traccar) {
142
161
  console.log(from, to)
143
162
  traccarInstance = traccar
144
163
  userData = reportUserData
145
- if(userData.groupByDay){
146
- const reportData = []
147
-
148
- if(userData.byDriver){
149
- const allData = await createKmsReportByDriver(from, to)
150
- reportData.push(allData)
151
- }
152
- else if(userData.byGroup){
153
- const allGroupsData = await createKmsReportByGroup(from, to)
154
- allGroupsData.forEach(data => reportData.push(data))
155
- const withoutGroupDevices = await createKmsReportByDevice(from, to)
156
- reportData.push(withoutGroupDevices)
157
- } else {
158
- const allData = await createKmsReportByDevice(from, to)
159
- reportData.push(allData)
160
- }
161
-
162
- return reportData
164
+ const reportData = []
165
+ if(userData.byDriver){
166
+ const allData = await createKmsReportByDriver(from, to)
167
+ reportData.push(allData)
168
+ }
169
+ else if(userData.byGroup){
170
+ const allGroupsData = await createKmsReportByGroup(from, to)
171
+ allGroupsData.forEach(data => reportData.push(data))
172
+ const withoutGroupDevices = await createKmsReportByDevice(from, to)
173
+ reportData.push(withoutGroupDevices)
163
174
  } else {
164
- return await createActivityReport(from, to, userData, traccar)
175
+ const allData = await createKmsReportByDevice(from, to)
176
+ reportData.push(allData)
165
177
  }
178
+
179
+ return reportData
166
180
  }
167
181
 
168
182
  async function exportKmsReportToPDF(userData, reportData) {
@@ -170,6 +184,16 @@ async function exportKmsReportToPDF(userData, reportData) {
170
184
  const translations = messages[lang] ? messages[lang] : messages['en-GB']
171
185
  const kmsData = userData.byDriver ? reportData.drivers : reportData.devices
172
186
 
187
+ const weekDays = [
188
+ translations.report.sunday,
189
+ translations.report.monday,
190
+ translations.report.tuesday,
191
+ translations.report.wednesday,
192
+ translations.report.thursday,
193
+ translations.report.friday,
194
+ translations.report.saturday
195
+ ]
196
+
173
197
  const headers = []
174
198
  if(userData.groupByDay) {
175
199
  headers.push(translations.report.date,
@@ -186,32 +210,42 @@ async function exportKmsReportToPDF(userData, reportData) {
186
210
  translations.report.distance)
187
211
  }
188
212
  if (kmsData) {
189
- let first = true
190
- const doc = userData.groupByDay ? new jsPDF.jsPDF('p') : new jsPDF.jsPDF('l')
191
- await headerFromUser(doc, translations.report.titleKmsReport, userData.user, 'p')
213
+ const orientation = userData.groupByDay ? 'p' : 'l'
214
+ const doc = new jsPDF.jsPDF(orientation)
215
+ await headerFromUser(doc, translations.report.titleKmsReport, userData.user, orientation)
192
216
 
193
217
  if (userData.groupByDay) {
194
- for (const d of kmsData) {
218
+ kmsData.forEach(function (d, index) {
195
219
  const name = userData.byDriver ? d.driver.name : d.device.name
196
220
  const group = userData.byDriver ? userData.groups.find(g => g.drivers.includes(d.id)) : userData.groups.find(g => d.device.groupId === g.id)
197
221
 
198
- let space = 0
199
- if (!first) {
222
+ let space = index === 0 ? 8 : 0
223
+ if(index){
200
224
  doc.addPage()
201
- } else {
202
- first = false
203
- space = 8
204
225
  }
226
+
205
227
  doc.setFontSize(13)
206
228
  doc.text(name, 20, space + 20)
207
229
  doc.setFontSize(11)
208
230
  doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
209
231
  doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
210
232
 
233
+ if(!userData.allWeek) {
234
+ doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '')
235
+ + (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '')
236
+ + (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '')
237
+ + (userData.weekDays.thursday ? ' ' + translations.report.thursday + ',' : '')
238
+ + (userData.weekDays.friday ? ' ' + translations.report.friday + ',' : '')
239
+ + (userData.weekDays.saturday ? ' ' + translations.report.saturday + ',' : '')
240
+ + (userData.weekDays.sunday ? ' ' + translations.report.sunday + ',' : '')
241
+ + ' das '
242
+ + userData.dayHours.startTime + ' - ' + userData.dayHours.endTime, 20, space + 30)
243
+ }
244
+
211
245
  const data = []
212
246
  d.days.forEach(day => {
213
247
  const temp = [
214
- new Date(day.date).toLocaleDateString(),
248
+ new Date(day.date).toLocaleDateString() + ' - ' + weekDays[day.date.getDay()],
215
249
  (day.kms/1000).toFixed(0)
216
250
  ]
217
251
 
@@ -225,9 +259,8 @@ async function exportKmsReportToPDF(userData, reportData) {
225
259
  )
226
260
 
227
261
  const style = getStyle(getUserPartner(userData.user))
228
-
229
- addTable(doc, headers, data, footValues, style, 35)
230
- }
262
+ addTable(doc, headers, data, footValues, style, 40)
263
+ })
231
264
  } else {
232
265
  const data = []
233
266
  if (userData.byDriver) {
@@ -246,7 +279,7 @@ async function exportKmsReportToPDF(userData, reportData) {
246
279
  const group = userData.groups.find(g => d.device.groupId === g.id)
247
280
 
248
281
  const temp = [
249
- d.summary.deviceName,
282
+ d.device.name,
250
283
  d.device.model,
251
284
  group ? group.name : '',
252
285
  Number((d.summary.distance / 1000).toFixed(0))
@@ -285,6 +318,16 @@ function exportKmsReportToExcel(userData, reportData) {
285
318
  const lang = userData.user.attributes.lang || (navigator && navigator.language)
286
319
  const translations = messages[lang] ? messages[lang] : messages['en-GB']
287
320
 
321
+ const weekDays = [
322
+ translations.report.sunday,
323
+ translations.report.monday,
324
+ translations.report.tuesday,
325
+ translations.report.wednesday,
326
+ translations.report.thursday,
327
+ translations.report.friday,
328
+ translations.report.saturday
329
+ ]
330
+
288
331
  const settings = {
289
332
  sheetName: translations.report.titleKmsReport, // The name of the sheet
290
333
  fileName: fileName // The name of the spreadsheet
@@ -298,6 +341,7 @@ function exportKmsReportToExcel(userData, reportData) {
298
341
  {label: translations.report.licensePlate, value: 'licenseplate'},
299
342
  {label: translations.report.group, value: 'group'},
300
343
  {label: translations.report.date, value: 'date'},
344
+ {label: translations.report.weekDay, value: 'weekday'},
301
345
  {label: translations.report.distance, value: 'distance'})
302
346
  } else if(userData.byDriver) {
303
347
  headers.push(
@@ -326,12 +370,13 @@ function exportKmsReportToExcel(userData, reportData) {
326
370
  group: group ? group.name : '',
327
371
  licenseplate: d.device.attributes.license_plate,
328
372
  date: a.date,
373
+ weekday: weekDays[a.date.getDay()],
329
374
  distance: Number((a.kms / 1000).toFixed(0))
330
375
  }
331
376
  }))
332
377
  } else {
333
378
  data = data.concat([{
334
- name: userData.byDriver ? d.driver.name : d.summary.deviceName,
379
+ name: userData.byDriver ? d.driver.name : d.device.name,
335
380
  group: group ? group.name : '',
336
381
  licenseplate: userData.byDriver ? '' : d.device.attributes.license_plate,
337
382
  distance: Number((d.summary.distance / 1000).toFixed(0))
@@ -331,7 +331,7 @@ async function exportSpeedingReportToPDF(userData, reportData) {
331
331
  translations.report.duration,
332
332
  ]
333
333
 
334
- if(userData.useRoadSpeedLimits) {
334
+ if(userData.roadSpeedLimits) {
335
335
  headers.splice(2, 0, translations.report.roadSpeedLimit)
336
336
  }
337
337
 
@@ -366,7 +366,7 @@ async function exportSpeedingReportToPDF(userData, reportData) {
366
366
  doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space+20 )
367
367
  doc.text(new Date(d.from).toLocaleString() + ' - ' + new Date(d.to).toLocaleString(), 20, space+25 )
368
368
  if (d.alerts[0] && !userData.byDriver) {
369
- doc.text(translations.report.speedLimit + ": " + Math.round(d.alerts[0].attributes.speedLimit * 1.85200) + ' Km/h', 20, space + 30)
369
+ doc.text(translations.report.speedLimit + ": " + (userData.useVehicleSpeedLimit ? Math.round(d.alerts[0].attributes.speedLimit * 1.85200) : userData.customSpeed) + ' Km/h', 20, space + 30)
370
370
  }
371
371
  d.alerts.map(a => {
372
372
  const temp = [
@@ -379,7 +379,7 @@ async function exportSpeedingReportToPDF(userData, reportData) {
379
379
  userData.byDriver ? a.deviceName : a.driver
380
380
  ]
381
381
 
382
- if(userData.useRoadSpeedLimits){
382
+ if(userData.roadSpeedLimits){
383
383
  temp.splice(2, 0, a.roadSpeedLimit)
384
384
  }
385
385
 
@@ -395,7 +395,7 @@ async function exportSpeedingReportToPDF(userData, reportData) {
395
395
  convertMS(d.alerts.reduce((a, b) => a + b.eventTime, 0), true)
396
396
  ]
397
397
 
398
- if(userData.useRoadSpeedLimits){
398
+ if(userData.roadSpeedLimits){
399
399
  footValues.splice(2, 0, '')
400
400
  }
401
401
 
@@ -454,7 +454,7 @@ function exportSpeedingReportToExcel(userData, reportData) {
454
454
  userData.byDriver ? {label: translations.report.vehicle, value:'name'} : {label: translations.report.driver, value:'driver'}
455
455
  ]
456
456
 
457
- if (userData.useRoadSpeedLimits){
457
+ if (userData.roadSpeedLimits){
458
458
  headers.splice(3, 0, {label: translations.report.roadSpeedLimit, value:'roadSpeedLimit'})
459
459
  }
460
460
 
@@ -6,40 +6,43 @@ require('jspdf-autotable')
6
6
  const {coordsDistance} = require("./util/utils")
7
7
  const drivers = require("./util/driver")
8
8
  const {getUserPartner} = require("fleetmap-partners")
9
- const {headerFromUser} = require("./util/pdfDocument")
9
+ const {headerFromUser, addTable} = require("./util/pdfDocument")
10
10
  const {getStyle} = require("./reportStyle")
11
11
  const traccar = require("./util/traccar")
12
+ const trips = require("./util/trips");
12
13
 
13
14
 
14
15
  let traccarInstance
16
+ let userData
15
17
 
16
18
  const fileName = 'TripReport'
17
19
 
18
- async function createTripReport(from, to, userData, traccar) {
20
+ async function createTripReport(from, to, reportUserData, traccar) {
19
21
  console.log('Create TripReport')
22
+ userData = reportUserData
20
23
  traccarInstance = traccar
21
24
  const reportData = []
22
25
 
23
26
  if(userData.byDriver){
24
- const allData = await createTripReportByDriver(from, to, userData)
27
+ const allData = await createTripReportByDriver(from, to)
25
28
  reportData.push(allData)
26
29
  }
27
30
  else if(userData.byGroup){
28
31
  console.log("ByGroup")
29
- const allData = await createTripReportByGroup(from, to, userData)
32
+ const allData = await createTripReportByGroup(from, to)
30
33
  reportData.push(...allData)
31
34
  }
32
35
 
33
36
  const groupIds = userData.groups.map(g => g.id)
34
37
  const withoutGroupDevices = userData.byGroup ? userData.devices.filter(d => !groupIds.includes(d.groupId)) : userData.devices
35
38
 
36
- const allData = await createTripReportByDevice(from, to, userData, withoutGroupDevices)
39
+ const allData = await createTripReportByDevice(from, to, withoutGroupDevices)
37
40
  reportData.push(allData)
38
41
 
39
42
  return reportData
40
43
  }
41
44
 
42
- async function createTripReportByDevice(from, to, userData, devices) {
45
+ async function createTripReportByDevice(from, to, devices) {
43
46
  const allData = {
44
47
  devices: [],
45
48
  xpert: devices.filter(d => d.attributes.xpert).length > 0
@@ -54,14 +57,14 @@ async function createTripReportByDevice(from, to, userData, devices) {
54
57
  console.log('Trips:' + data.length)
55
58
 
56
59
  if (data.length > 0) {
57
- allData.devices = processDevices(from, to, devices, data, stopsData, userData)
58
-
60
+ await trips.checkTripsKms(traccarInstance, from, to,[], data, devices)
61
+ allData.devices = processDevices(from, to, devices, data, stopsData)
59
62
  }
60
63
 
61
64
  return allData
62
65
  }
63
66
 
64
- async function createTripReportByGroup(from, to, userData) {
67
+ async function createTripReportByGroup(from, to) {
65
68
  const reportData = []
66
69
  for (const g of userData.groups) {
67
70
  const devices = userData.devices.filter(d => d.groupId === g.id)
@@ -93,7 +96,7 @@ async function createTripReportByGroup(from, to, userData) {
93
96
  return reportData
94
97
  }
95
98
 
96
- async function createTripReportByDriver(from, to, userData){
99
+ async function createTripReportByDriver(from, to){
97
100
  const deviceIds = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
98
101
  console.log(deviceIds.length)
99
102
 
@@ -112,11 +115,11 @@ async function createTripReportByDriver(from, to, userData){
112
115
  return allData
113
116
  }
114
117
 
115
- function processDevices(from, to, devices, data, stopsData, userData) {
118
+ function processDevices(from, to, devices, data, stopsData) {
116
119
  const devicesResult = []
117
120
 
118
121
  devices.forEach(d => {
119
- const trips = data.filter(t => t.deviceId === d.id)
122
+ const trips = data.filter(t => t.deviceId === d.id && (userData.allWeek || isInsideTimetable(t, userData)))
120
123
  const stops = stopsData.filter(s => s.deviceId === d.id)
121
124
 
122
125
  trips.forEach(t => {
@@ -146,8 +149,8 @@ function processDevices(from, to, devices, data, stopsData, userData) {
146
149
  })
147
150
 
148
151
  if (trips.length > 0) {
149
- trips.forEach(function(trip, index) {
150
- const stop = stops.length+1 > index ? stops[index+1] : null
152
+ trips.forEach(trip => {
153
+ const stop = getStop(new Date(trip.startTime), stops)
151
154
 
152
155
  if (stop) {
153
156
  trip.stopDuration = (new Date(stop.endTime) - new Date(stop.startTime))
@@ -240,27 +243,38 @@ async function exportTripReportToPDF(userData, reportData) {
240
243
  }
241
244
 
242
245
  if (tripsData) {
243
- let first = true
244
246
  const doc = new jsPDF.jsPDF('l');
245
247
  await headerFromUser(doc, translations.report.titleTripReport, userData.user)
246
- for(const d of tripsData){
248
+
249
+ tripsData.forEach(function (d, index) {
247
250
  const data = []
248
251
  const name = userData.byDriver ? d.driver.name : deviceName(d.device)
249
252
  const group = userData.byDriver ? userData.groups.find(g => g.drivers.includes(d.id)) : userData.groups.find(g => d.device.groupId === g.id)
250
253
 
251
- let space = 0
252
- if(!first) {
254
+ let space = index === 0 ? 8 : 0
255
+ if(index){
253
256
  doc.addPage()
254
- } else {
255
- first = false
256
- space = 8
257
257
  }
258
+
258
259
  doc.setFontSize(13)
259
260
  doc.text(name, 20, space+20 )
260
261
  doc.setFontSize(11)
261
262
  doc.text(group ? translations.report.group + ': ' + group.name : '', 200, space+20 )
262
263
  doc.text(new Date(d.from).toLocaleString() + ' - ' + new Date(d.to).toLocaleString(), 20, space+25 )
263
- doc.text(translations.report.headerStartAddress + ': ' + d.trips[0].startAddress, 20, space+30 )
264
+ if(!userData.allWeek) {
265
+ doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '')
266
+ + (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '')
267
+ + (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '')
268
+ + (userData.weekDays.thursday ? ' ' + translations.report.thursday + ',' : '')
269
+ + (userData.weekDays.friday ? ' ' + translations.report.friday + ',' : '')
270
+ + (userData.weekDays.saturday ? ' ' + translations.report.saturday + ',' : '')
271
+ + (userData.weekDays.sunday ? ' ' + translations.report.sunday + ',' : '')
272
+ + ' das '
273
+ + userData.dayHours.startTime + ' - ' + userData.dayHours.endTime, 20, space + 30)
274
+ doc.text(translations.report.headerStartAddress + ': ' + d.trips[0].startAddress, 20, space+40 )
275
+ } else {
276
+ doc.text(translations.report.headerStartAddress + ': ' + d.trips[0].startAddress, 20, space+35 )
277
+ }
264
278
 
265
279
  d.trips.map(a => {
266
280
  const temp = [
@@ -306,28 +320,8 @@ async function exportTripReportToPDF(userData, reportData) {
306
320
  }
307
321
 
308
322
  const style = getStyle(getUserPartner(userData.user))
309
- doc.autoTable({
310
- head: [headers],
311
- body: data,
312
- foot: [footValues],
313
- showFoot: 'lastPage',
314
- headStyles: {
315
- fillColor: style.pdfHeaderColor,
316
- textColor: style.pdfHeaderTextColor,
317
- fontSize: 10
318
- },
319
- bodyStyles: {
320
- fillColor: style.pdfBodyColor,
321
- textColor: style.pdfBodyTextColor,
322
- fontSize: 8
323
- },
324
- footStyles: {
325
- fillColor: style.pdfFooterColor,
326
- textColor: style.pdfFooterTextColor,
327
- fontSize: 9
328
- },
329
- startY: space+35 })
330
- }
323
+ addTable(doc, headers, data, footValues, style, space + (userData.allWeek ? 40 : 45))
324
+ })
331
325
  return doc
332
326
  }
333
327
  }
@@ -451,6 +445,39 @@ function getMaxSpeed(data) {
451
445
  return Math.round(max * 1.85200)
452
446
  }
453
447
 
448
+ function getStop(tripEndDate, stops){
449
+ const stopList = stops.filter(s => new Date(s.startTime).getTime() >= tripEndDate.getTime())
450
+ if(stopList.length){
451
+ return stopList[0]
452
+ }
453
+
454
+ return null
455
+ }
456
+
457
+ function isInsideTimetable(t, userData){
458
+ const tripStart = new Date(t.startTime)
459
+ const tripEnd = new Date(t.endTime)
460
+
461
+ if((tripStart.getDay() === 0 && userData.weekDays.sunday) ||
462
+ (tripStart.getDay() === 1 && userData.weekDays.monday) ||
463
+ (tripStart.getDay() === 2 && userData.weekDays.tuesday) ||
464
+ (tripStart.getDay() === 3 && userData.weekDays.wednesday) ||
465
+ (tripStart.getDay() === 4 && userData.weekDays.thursday) ||
466
+ (tripStart.getDay() === 5 && userData.weekDays.friday) ||
467
+ (tripStart.getDay() === 6 && userData.weekDays.saturday)) {
468
+
469
+ const startDate = new Date(new Date(tripStart).toISOString().split('T')[0] + ' '+userData.dayHours.startTime)
470
+ const endDate = new Date(new Date(tripStart).toISOString().split('T')[0] + ' '+userData.dayHours.endTime)
471
+
472
+ console.log(tripStart, startDate, endDate)
473
+
474
+ //Trips inside time period
475
+ return tripStart.getTime() > startDate.getTime() && tripEnd.getTime() < endDate.getTime()
476
+ }
477
+
478
+ return false
479
+ }
480
+
454
481
  exports.createTripReport = createTripReport;
455
482
  exports.exportTripReportToPDF = exportTripReportToPDF;
456
483
  exports.exportTripReportToExcel = exportTripReportToExcel;
@@ -52,7 +52,21 @@ async function getEvents(traccar, from, to, devices, events){
52
52
  return result.map(r => r.data).flat()
53
53
  }
54
54
 
55
+ async function getSummary(traccar, from, to, devices){
56
+ const devicesToSlice = devices.slice()
57
+ const arrayOfArrays = automaticReports.sliceArray(devicesToSlice)
58
+ let requests = []
59
+ for (const a of arrayOfArrays) {
60
+ const promise = traccar.reports.reportsSummaryGet(from, to, a.map(d => d.id))
61
+ requests = requests.concat(promise)
62
+ }
63
+
64
+ const result = await Promise.all(requests)
65
+ return result.map(r => r.data).flat()
66
+ }
67
+
55
68
  exports.getRoute = getRoute
56
69
  exports.getTrips = getTrips
57
70
  exports.getStops = getStops
58
71
  exports.getEvents = getEvents
72
+ exports.getSummary = getSummary