fleetmap-reports 1.0.249 → 1.0.253

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/index.js CHANGED
@@ -19,8 +19,8 @@ function Reports(config, axios) {
19
19
  byGroup: false
20
20
  }
21
21
  }
22
- this.speedingReport = (from, to, userData, roadSpeedLimits) => {
23
- return require('./speeding-report').createSpeedingReport(from, to, userData, this.traccar, roadSpeedLimits)
22
+ this.speedingReport = (from, to, userData) => {
23
+ return require('./speeding-report').createSpeedingReport(from, to, userData, this.traccar)
24
24
  }
25
25
 
26
26
  this.speedingReportToPDF = (userData, reportData) => {
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))