fleetmap-reports 1.0.260 → 1.0.264

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.
@@ -1,16 +1,13 @@
1
1
  <?xml version="1.0" encoding="UTF-8"?>
2
2
  <project version="4">
3
3
  <component name="ChangeListManager">
4
- <list default="true" id="eb665c86-c893-4333-bd2a-721dc27980b9" name="Changes" comment="location report - check if it's running on client side">
4
+ <list default="true" id="eb665c86-c893-4333-bd2a-721dc27980b9" name="Changes" comment="activity report by driver grouped by day">
5
5
  <change beforePath="$PROJECT_DIR$/package-lock.json" beforeDir="false" afterPath="$PROJECT_DIR$/package-lock.json" afterDir="false" />
6
6
  <change beforePath="$PROJECT_DIR$/package.json" beforeDir="false" afterPath="$PROJECT_DIR$/package.json" afterDir="false" />
7
- <change beforePath="$PROJECT_DIR$/src/events-report.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/events-report.js" afterDir="false" />
7
+ <change beforePath="$PROJECT_DIR$/src/activity-report.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/activity-report.js" afterDir="false" />
8
8
  <change beforePath="$PROJECT_DIR$/src/index.test.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/index.test.js" afterDir="false" />
9
- <change beforePath="$PROJECT_DIR$/src/location-report.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/location-report.js" afterDir="false" />
10
- <change beforePath="$PROJECT_DIR$/src/speeding-report.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/speeding-report.js" afterDir="false" />
9
+ <change beforePath="$PROJECT_DIR$/src/kms-report.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/kms-report.js" afterDir="false" />
11
10
  <change beforePath="$PROJECT_DIR$/src/trip-report.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/trip-report.js" afterDir="false" />
12
- <change beforePath="$PROJECT_DIR$/src/util/utils.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/util/utils.js" afterDir="false" />
13
- <change beforePath="$PROJECT_DIR$/src/zone-report.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/zone-report.js" afterDir="false" />
14
11
  </list>
15
12
  <option name="SHOW_DIALOG" value="false" />
16
13
  <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -220,7 +217,12 @@
220
217
  <workItem from="1639954341221" duration="11986000" />
221
218
  <workItem from="1640272520380" duration="2253000" />
222
219
  <workItem from="1640789226071" duration="1935000" />
223
- <workItem from="1640794844118" duration="8847000" />
220
+ <workItem from="1640794844118" duration="10128000" />
221
+ <workItem from="1640866364417" duration="67000" />
222
+ <workItem from="1640866782794" duration="123000" />
223
+ <workItem from="1640866964393" duration="1022000" />
224
+ <workItem from="1640870147277" duration="8236000" />
225
+ <workItem from="1641321068227" duration="13602000" />
224
226
  </task>
225
227
  <task id="LOCAL-00001" summary="Fix getStyle">
226
228
  <created>1636983717385</created>
@@ -341,7 +343,35 @@
341
343
  <option name="project" value="LOCAL" />
342
344
  <updated>1640795243288</updated>
343
345
  </task>
344
- <option name="localTasksCounter" value="18" />
346
+ <task id="LOCAL-00018" summary="timezone - check if it's running on client side">
347
+ <created>1640825795052</created>
348
+ <option name="number" value="00018" />
349
+ <option name="presentableId" value="LOCAL-00018" />
350
+ <option name="project" value="LOCAL" />
351
+ <updated>1640825795053</updated>
352
+ </task>
353
+ <task id="LOCAL-00019" summary="calculate avg speed">
354
+ <created>1640867522437</created>
355
+ <option name="number" value="00019" />
356
+ <option name="presentableId" value="LOCAL-00019" />
357
+ <option name="project" value="LOCAL" />
358
+ <updated>1640867522438</updated>
359
+ </task>
360
+ <task id="LOCAL-00020" summary="fix activity report by driver">
361
+ <created>1640872780966</created>
362
+ <option name="number" value="00020" />
363
+ <option name="presentableId" value="LOCAL-00020" />
364
+ <option name="project" value="LOCAL" />
365
+ <updated>1640872780968</updated>
366
+ </task>
367
+ <task id="LOCAL-00021" summary="activity report by driver grouped by day">
368
+ <created>1640878664617</created>
369
+ <option name="number" value="00021" />
370
+ <option name="presentableId" value="LOCAL-00021" />
371
+ <option name="project" value="LOCAL" />
372
+ <updated>1640878664617</updated>
373
+ </task>
374
+ <option name="localTasksCounter" value="22" />
345
375
  <servers />
346
376
  </component>
347
377
  <component name="TypeScriptGeneratedFilesManager">
@@ -376,6 +406,10 @@
376
406
  <MESSAGE value="Fix trips distance because imported positions generate trips with 0 kms" />
377
407
  <MESSAGE value="Fix reports by driver" />
378
408
  <MESSAGE value="location report - check if it's running on client side" />
379
- <option name="LAST_COMMIT_MESSAGE" value="location report - check if it's running on client side" />
409
+ <MESSAGE value="timezone - check if it's running on client side" />
410
+ <MESSAGE value="calculate avg speed" />
411
+ <MESSAGE value="fix activity report by driver" />
412
+ <MESSAGE value="activity report by driver grouped by day" />
413
+ <option name="LAST_COMMIT_MESSAGE" value="activity report by driver grouped by day" />
380
414
  </component>
381
415
  </project>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "1.0.260",
3
+ "version": "1.0.264",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -87,6 +87,9 @@ async function createActivityReportByDevice(from, to){
87
87
  //Get report data from traccar
88
88
  devicesToProcess.sort((a, b) => (a.name > b.name) ? 1 : -1)
89
89
 
90
+ // use the km report to calculate kms
91
+ const kmsReport = await createKmsReport(from, to, userData, traccarInstance)
92
+
90
93
  let data = []
91
94
  if(userData.groupByDay) {
92
95
  const dates = getDates(from, to)
@@ -110,9 +113,33 @@ async function createActivityReportByDevice(from, to){
110
113
  const fromByDay = new Date(new Date(date).toISOString().split('T')[0] + ' ' + userData.dayHours.startTime)
111
114
  const toByDay = new Date(new Date(date).toISOString().split('T')[0] + ' ' + userData.dayHours.endTime)
112
115
 
113
- const summary = await traccar.getSummary(traccarInstance, fromByDay, toByDay, devicesToProcess)
114
- summary.forEach(s => s.date = date)
115
- data = data.concat(summary)
116
+ if(fromByDay.getTime() < toByDay.getTime()) {
117
+ const summary = await traccar.getSummary(traccarInstance, new Date(fromByDay.toUTCString()), new Date(toByDay.toUTCString()), devicesToProcess)
118
+ summary.forEach(s => s.date = date)
119
+ data = data.concat(summary)
120
+ } else {
121
+ const dayStart = new Date(new Date(date).toISOString().split('T')[0] + ' 00:00:00')
122
+ const dayEnd = new Date(new Date(date).toISOString().split('T')[0] + ' 23:59:59')
123
+
124
+ const summaryStart = await traccar.getSummary(traccarInstance, new Date(dayStart.toUTCString()), new Date(toByDay.toUTCString()), devicesToProcess)
125
+ const summaryEnd = await traccar.getSummary(traccarInstance, new Date(fromByDay.toUTCString()), new Date(dayEnd.toUTCString()), devicesToProcess)
126
+
127
+ const summary = []
128
+ summaryStart.forEach(s => {
129
+ const sEnd = summaryEnd.find(a => s.deviceId === a.deviceId)
130
+ summary.push({
131
+ date: date,
132
+ deviceId: s.deviceId,
133
+ averageSpeed: (s.averageSpeed + sEnd.averageSpeed) / 2,
134
+ distance: s.distance + sEnd.distance,
135
+ engineHours: s.engineHours + sEnd.engineHours,
136
+ maxSpeed: s.maxSpeed > sEnd.maxSpeed ? s.maxSpeed : sEnd.maxSpeed,
137
+ endOdometer: 0,
138
+ startOdometer: 0
139
+ })
140
+ })
141
+ data = data.concat(summary)
142
+ }
116
143
  }else {
117
144
  data = data.concat({date: date})
118
145
  }
@@ -122,9 +149,6 @@ async function createActivityReportByDevice(from, to){
122
149
  data = await traccar.getSummary(traccarInstance, from, to, devicesToProcess)
123
150
  }
124
151
 
125
- // use the km report to calculate kms
126
- const kmsReport = await createKmsReport(from, to, userData, traccarInstance)
127
-
128
152
  console.log('Summary:' + data.length)
129
153
 
130
154
  //Process report data
@@ -140,8 +164,7 @@ async function createActivityReportByDriver(from, to){
140
164
  console.log(deviceIds)
141
165
  let tripsData = []
142
166
  if(deviceIds.length > 0) {
143
- const response = await traccar.getTrips(traccarInstance, from, to, deviceIds)
144
- tripsData = response.data
167
+ tripsData = await traccar.getTrips(traccarInstance, from, to, deviceIds)
145
168
  }
146
169
 
147
170
  const allData = {
@@ -150,7 +173,7 @@ async function createActivityReportByDriver(from, to){
150
173
  to: to
151
174
  }
152
175
 
153
- allData.drivers = processDrivers(userData.drivers, tripsData)
176
+ allData.drivers = processDrivers(from, to, userData.drivers, tripsData)
154
177
 
155
178
  return allData
156
179
  }
@@ -181,7 +204,7 @@ function processDevices(devices, data, kmsReport) {
181
204
  return devicesResult
182
205
  }
183
206
 
184
- function processDrivers(drivers, data) {
207
+ function processDrivers(from, to, drivers, data) {
185
208
  console.log(data)
186
209
  const driversResult = []
187
210
  drivers.forEach(d => {
@@ -189,12 +212,63 @@ function processDrivers(drivers, data) {
189
212
  if (trips.length > 0) {
190
213
  const driverData = {
191
214
  driver: d,
192
- summary: {
215
+ summary: [],
216
+ }
217
+ if(userData.groupByDay) {
218
+ const dates = getDates(from, to)
219
+ for(const date of dates){
220
+ if(userData.allWeek){
221
+ const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
222
+ const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
223
+
224
+ const tripsByDay = trips.filter(t => new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay)
225
+
226
+ driverData.summary.push({
227
+ date: date,
228
+ distance: tripsByDay.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0),
229
+ engineHours: tripsByDay.reduce((a, b) => a + b.duration, 0),
230
+ maxSpeed: tripsByDay.reduce((a, b) => Math.max(a, b.maxSpeed), 0),
231
+ averageSpeed: tripsByDay.length > 0 ? Math.round(tripsByDay.reduce((a, b) => a + b.averageSpeed, 0) / tripsByDay.length) : 0,
232
+ })
233
+ } else {
234
+ if((date.getDay() === 0 && userData.weekDays.sunday) ||
235
+ (date.getDay() === 1 && userData.weekDays.monday) ||
236
+ (date.getDay() === 2 && userData.weekDays.tuesday) ||
237
+ (date.getDay() === 3 && userData.weekDays.wednesday) ||
238
+ (date.getDay() === 4 && userData.weekDays.thursday) ||
239
+ (date.getDay() === 5 && userData.weekDays.friday) ||
240
+ (date.getDay() === 6 && userData.weekDays.saturday)) {
241
+
242
+ const fromByDay = new Date(new Date(date).toISOString().split('T')[0] + ' ' + userData.dayHours.startTime)
243
+ const toByDay = new Date(new Date(date).toISOString().split('T')[0] + ' ' + userData.dayHours.endTime)
244
+
245
+ const tripsByDay = trips.filter(t => new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay)
246
+
247
+ driverData.summary.push({
248
+ date: date,
249
+ distance: tripsByDay.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0),
250
+ engineHours: tripsByDay.reduce((a, b) => a + b.duration, 0),
251
+ maxSpeed: tripsByDay.reduce((a, b) => Math.max(a, b.maxSpeed), 0),
252
+ averageSpeed: tripsByDay.length > 0 ? Math.round(tripsByDay.reduce((a, b) => a + b.averageSpeed, 0) / tripsByDay.length) : 0,
253
+ })
254
+ }else {
255
+ driverData.summary.push({
256
+ date: date,
257
+ distance: 0,
258
+ engineHours: 0,
259
+ maxSpeed: 0,
260
+ averageSpeed: 0,
261
+ })
262
+ }
263
+ }
264
+ }
265
+ } else {
266
+ driverData.summary.push({
193
267
  distance: trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0),
194
268
  engineHours: trips.reduce((a, b) => a + b.duration, 0),
195
269
  maxSpeed: trips.reduce((a, b) => Math.max(a, b.maxSpeed), 0),
196
270
  averageSpeed: Math.round(trips.reduce((a, b) => a + b.averageSpeed, 0) / trips.length),
197
- },
271
+ })
198
272
  }
199
273
  driversResult.push(driverData)
200
274
  }
@@ -212,9 +286,11 @@ async function exportActivityReportToPDF(userData, reportData) {
212
286
  const doc = new jsPDF.jsPDF('l')
213
287
  await headerFromUser(doc, translations.report.titleActivityReport, userData.user)
214
288
 
215
- if(userData.groupByDay){
289
+ if(userData.groupByDay && userData.byDriver) {
290
+ exportActivityReportToPDF_Driver_GroupByDay(doc, translations, reportData)
291
+ } else if(userData.groupByDay) {
216
292
  exportActivityReportToPDF_Vehicle_GroupByDay(doc, translations, reportData)
217
- } else if(userData.byDriver){
293
+ } else if(userData.byDriver) {
218
294
  exportActivityReportToPDF_Driver(doc, translations, reportData)
219
295
  } else {
220
296
  exportActivityReportToPDF_Vehicle(doc, translations, reportData)
@@ -257,7 +333,7 @@ function exportActivityReportToPDF_Driver(doc, translations, reportData) {
257
333
  }
258
334
 
259
335
  function exportActivityReportToPDF_Vehicle(doc, translations, reportData) {
260
- const headers = [translations.report.name,
336
+ const headers = [translations.report.vehicle,
261
337
  translations.report.group,
262
338
  translations.report.distance,
263
339
  translations.report.start + ' ' + translations.report.odometer,
@@ -275,7 +351,7 @@ function exportActivityReportToPDF_Vehicle(doc, translations, reportData) {
275
351
  const group = userData.groups.find(g => d.device.groupId === g.id)
276
352
 
277
353
  const temp = [
278
- d.summary.deviceName,
354
+ d.summary[0].deviceName,
279
355
  group ? group.name : '',
280
356
  Number((d.summary[0].distance / 1000).toFixed(2)),
281
357
  Number((d.summary[0].startOdometer / 1000).toFixed(2)),
@@ -431,6 +507,122 @@ function exportActivityReportToExcel_Vehicle_GroupByDay(settings, translations,
431
507
  }
432
508
  }
433
509
 
510
+ function exportActivityReportToPDF_Driver_GroupByDay(doc, translations, reportData) {
511
+ const headers = [translations.report.date,
512
+ translations.report.distance,
513
+ translations.report.avgSpeed + ' (Km/h)',
514
+ translations.report.maxSpeed + ' (Km/h)',
515
+ translations.report.engineHours]
516
+
517
+ const weekDays = [
518
+ translations.report.sunday,
519
+ translations.report.monday,
520
+ translations.report.tuesday,
521
+ translations.report.wednesday,
522
+ translations.report.thursday,
523
+ translations.report.friday,
524
+ translations.report.saturday
525
+ ]
526
+
527
+ reportData.drivers.forEach(function (d, index) {
528
+ const name = d.driver.name
529
+ const group = userData.groups.find(g => g.drivers.includes(d.id))
530
+
531
+ let space = index === 0 ? 8 : 0
532
+ if(index){
533
+ doc.addPage()
534
+ }
535
+
536
+ doc.setFontSize(13)
537
+ doc.text(name, 20, space + 20)
538
+ doc.setFontSize(11)
539
+ doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
540
+ doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
541
+
542
+ if (!userData.allWeek) {
543
+ doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '')
544
+ + (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '')
545
+ + (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '')
546
+ + (userData.weekDays.thursday ? ' ' + translations.report.thursday + ',' : '')
547
+ + (userData.weekDays.friday ? ' ' + translations.report.friday + ',' : '')
548
+ + (userData.weekDays.saturday ? ' ' + translations.report.saturday + ',' : '')
549
+ + (userData.weekDays.sunday ? ' ' + translations.report.sunday + ',' : '')
550
+ + ' das '
551
+ + userData.dayHours.startTime + ' - ' + userData.dayHours.endTime, 20, space + 30)
552
+ }
553
+
554
+ const data = []
555
+ d.summary.forEach(s => {
556
+ const temp = [
557
+ new Date(s.date).toLocaleDateString() + ' - ' + weekDays[s.date.getDay()],
558
+ Number((s.distance / 1000).toFixed(2)),
559
+ Math.round(s.averageSpeed * 1.85200),
560
+ Math.round(s.maxSpeed * 1.85200),
561
+ convertMS(s.engineHours)
562
+ ]
563
+
564
+ data.push(temp)
565
+ })
566
+
567
+ const footValues = ['Total:' + d.summary.length,
568
+ (d.summary.reduce((a, b) => a + b.distance, 0) / 1000).toFixed(0),
569
+ getSumAvgSpeed(d.summary),
570
+ getSumMaxSpeed(d.summary),
571
+ convertMS(d.summary.reduce((a, b) => a + b.engineHours, 0))]
572
+
573
+ const style = getStyle(getUserPartner(userData.user))
574
+ addTable(doc, headers, data, footValues, style, 40)
575
+ })
576
+ }
577
+
578
+ function exportActivityReportToExcel_Driver_GroupByDay(settings, translations, reportData) {
579
+ const headers = [{label: translations.report.driver, value: 'name'},
580
+ {label: translations.report.group, value: 'group'},
581
+ {label: translations.report.date, value: 'date'},
582
+ {label: translations.report.weekDay, value: 'weekday'},
583
+ {label: translations.report.distance, value: 'distance'},
584
+ {label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed'},
585
+ {label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed'},
586
+ {label: translations.report.engineHours, value: 'engineHours'}]
587
+
588
+ const weekDays = [
589
+ translations.report.sunday,
590
+ translations.report.monday,
591
+ translations.report.tuesday,
592
+ translations.report.wednesday,
593
+ translations.report.thursday,
594
+ translations.report.friday,
595
+ translations.report.saturday
596
+ ]
597
+
598
+ let data = []
599
+ if (reportData.drivers) {
600
+ reportData.drivers.forEach(d => {
601
+ const group = userData.groups.find(g => g.drivers.includes(d.id))
602
+
603
+ data = data.concat([{}])
604
+ data = data.concat(d.summary.map(s => {
605
+ return {
606
+ name: d.driver.name,
607
+ group: group ? group.name : '',
608
+ date: new Date(s.date).toLocaleDateString(),
609
+ weekday: weekDays[s.date.getDay()],
610
+ distance: Number((s.distance / 1000).toFixed(0)),
611
+ avgSpeed: Math.round(s.averageSpeed * 1.85200),
612
+ maxSpeed: Math.round(s.maxSpeed * 1.85200),
613
+ engineHours: convertMS(s.engineHours)
614
+ }
615
+ }))
616
+ })
617
+ console.log(data)
618
+ return {
619
+ headers,
620
+ data,
621
+ settings
622
+ }
623
+ }
624
+ }
625
+
434
626
  function exportActivityReportToExcel_Driver(settings, translations, reportData) {
435
627
  const headers = [{label: translations.report.driver, value: 'driver'},
436
628
  {label: translations.report.distance, value: 'distance'},
@@ -443,10 +635,10 @@ function exportActivityReportToExcel_Driver(settings, translations, reportData)
443
635
  reportData.drivers.forEach(d => {
444
636
  data = data.concat([{
445
637
  driver: d.driver.name,
446
- distance: Number((d.summary.distance / 1000).toFixed(0)),
447
- avgSpeed: Math.round(d.summary.averageSpeed * 1.85200),
448
- maxSpeed: Math.round(d.summary.maxSpeed * 1.85200),
449
- engineHours: convertMS(d.summary.engineHours)
638
+ distance: Number((d.summary[0].distance / 1000).toFixed(0)),
639
+ avgSpeed: Math.round(d.summary[0].averageSpeed * 1.85200),
640
+ maxSpeed: Math.round(d.summary[0].maxSpeed * 1.85200),
641
+ engineHours: convertMS(d.summary[0].engineHours)
450
642
  }])
451
643
  })
452
644
  console.log(data)
@@ -475,15 +667,15 @@ function exportActivityReportToExcel_Vehicle(settings, translations, reportData)
475
667
  const group = userData.groups.find(g => d.device.groupId === g.id)
476
668
 
477
669
  data = data.concat([{
478
- name: d.summary.deviceName,
670
+ name: d.summary[0].deviceName,
479
671
  group: group ? group.name : '',
480
- distance: Number((d.summary.distance / 1000).toFixed(0)),
481
- startOdometer: Number((d.summary.startOdometer / 1000).toFixed(0)),
482
- endOdometer: Number((d.summary.endOdometer / 1000).toFixed(0)),
483
- avgSpeed: Math.round(d.summary.averageSpeed * 1.85200),
484
- maxSpeed: Math.round(d.summary.maxSpeed * 1.85200),
485
- engineHours: convertMS(d.summary.engineHours),
486
- spentFuel: d.summary.convertedSpentFuel >= 0 ? d.summary.convertedSpentFuel : 0,
672
+ distance: Number((d.summary[0].distance / 1000).toFixed(0)),
673
+ startOdometer: Number((d.summary[0].startOdometer / 1000).toFixed(0)),
674
+ endOdometer: Number((d.summary[0].endOdometer / 1000).toFixed(0)),
675
+ avgSpeed: Math.round(d.summary[0].averageSpeed * 1.85200),
676
+ maxSpeed: Math.round(d.summary[0].maxSpeed * 1.85200),
677
+ engineHours: convertMS(d.summary[0].engineHours),
678
+ spentFuel: d.summary[0].convertedSpentFuel >= 0 ? d.summary[0].convertedSpentFuel : 0,
487
679
  }])
488
680
  })
489
681
  console.log(data)
@@ -506,7 +698,9 @@ function exportActivityReportToExcel(reportUserData, reportData) {
506
698
  fileName: fileName // The name of the spreadsheet
507
699
  }
508
700
 
509
- if(userData.groupByDay){
701
+ if(userData.groupByDay && userData.byDriver){
702
+ return exportActivityReportToExcel_Driver_GroupByDay(settings, translations, reportData)
703
+ } else if(userData.groupByDay){
510
704
  return exportActivityReportToExcel_Vehicle_GroupByDay(settings, translations, reportData)
511
705
  } else if(userData.byDriver){
512
706
  return exportActivityReportToExcel_Driver(settings, translations, reportData)
package/src/kms-report.js CHANGED
@@ -7,6 +7,7 @@ const {getUserPartner} = require("fleetmap-partners");
7
7
  const traccar = require("./util/traccar");
8
8
  const {getDates} = require("./util/utils");
9
9
  const trips = require("./util/trips");
10
+ const drivers = require("./util/driver");
10
11
 
11
12
 
12
13
  let traccarInstance
@@ -26,7 +27,7 @@ async function createKmsReportByDevice(from, to) {
26
27
 
27
28
  devicesToProcess.sort((a, b) => (a.name > b.name) ? 1 : -1)
28
29
  const route = userData.allWeek ? [] : await traccar.getRoute(traccarInstance, from, to, devicesToProcess)
29
- const tripsData = await traccar.getTrips(traccarInstance, from, to, devicesToProcess)
30
+ const tripsData = await traccar.getTrips(traccarInstance, from, to, devicesToProcess.map(d => d.id))
30
31
 
31
32
  console.log('trips:' + tripsData.length)
32
33
 
@@ -39,13 +40,90 @@ async function createKmsReportByDevice(from, to) {
39
40
  }
40
41
 
41
42
  async function createKmsReportByDriver(from, to) {
43
+ const deviceIds = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
44
+ console.log(deviceIds.length)
42
45
 
46
+ let tripsData = []
47
+ if(deviceIds.length > 0) {
48
+ const response = await traccarInstance.reports.reportsTripsGet(from, to, deviceIds)
49
+ tripsData = response.data
50
+ }
51
+
52
+ const allData = {
53
+ drivers: []
54
+ }
55
+
56
+ allData.drivers = processDrivers(from, to, userData.drivers, tripsData)
57
+
58
+ return allData
43
59
  }
44
60
 
45
61
  async function createKmsReportByGroup(from, to) {
46
62
 
47
63
  }
48
64
 
65
+ function processDrivers(from, to, drivers, data) {
66
+ console.log(data)
67
+ const driversResult = []
68
+ drivers.forEach(d => {
69
+ const trips = data.filter(t => t.driverUniqueId === d.uniqueId)
70
+ if (trips.length > 0) {
71
+ const driverData = {
72
+ driver: d
73
+ }
74
+ if(userData.groupByDay) {
75
+ driverData.days = []
76
+ const dates = getDates(from, to)
77
+ for(const date of dates){
78
+ if(userData.allWeek){
79
+ const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
80
+ const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
81
+
82
+ const tripsByDay = trips.filter(t => new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay)
83
+
84
+ driverData.days.push({
85
+ date: date,
86
+ kms: tripsByDay.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
87
+ })
88
+ } else {
89
+ if((date.getDay() === 0 && userData.weekDays.sunday) ||
90
+ (date.getDay() === 1 && userData.weekDays.monday) ||
91
+ (date.getDay() === 2 && userData.weekDays.tuesday) ||
92
+ (date.getDay() === 3 && userData.weekDays.wednesday) ||
93
+ (date.getDay() === 4 && userData.weekDays.thursday) ||
94
+ (date.getDay() === 5 && userData.weekDays.friday) ||
95
+ (date.getDay() === 6 && userData.weekDays.saturday)) {
96
+
97
+ const fromByDay = new Date(new Date(date).toISOString().split('T')[0] + ' ' + userData.dayHours.startTime)
98
+ const toByDay = new Date(new Date(date).toISOString().split('T')[0] + ' ' + userData.dayHours.endTime)
99
+
100
+ const tripsByDay = trips.filter(t => new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay)
101
+
102
+ driverData.days.push({
103
+ date: date,
104
+ kms: tripsByDay.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
105
+ })
106
+ }else {
107
+ driverData.days.push({
108
+ date: date,
109
+ kms: 0
110
+ })
111
+ }
112
+ }
113
+ }
114
+ } else {
115
+ driverData.summary = {
116
+ distance: trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
117
+ }
118
+ }
119
+ driversResult.push(driverData)
120
+ }
121
+ })
122
+
123
+ return driversResult
124
+ }
125
+
126
+
49
127
  function processDevices(from, to, devices, route, trips) {
50
128
  const devicesResult = []
51
129
 
@@ -51,7 +51,7 @@ async function createTripReportByDevice(from, to, devices) {
51
51
  devices.sort((a, b) => (a.name > b.name) ? 1 : -1)
52
52
 
53
53
  const devicesToSlice = devices.slice()
54
- const data = await traccar.getTrips(traccarInstance, from, to, devicesToSlice)
54
+ const data = await traccar.getTrips(traccarInstance, from, to, devicesToSlice.map(d => d.id))
55
55
  const stopsData = await traccar.getStops(traccarInstance, from, to, devicesToSlice)
56
56
 
57
57
  console.log('Trips:' + data.length)
@@ -430,7 +430,8 @@ function getSumAvgSpeed(data) {
430
430
  const values = data.map(item => {
431
431
  return Math.round(item.averageSpeed * 1.85200) * item.totalKms
432
432
  })
433
- return Math.round((values.reduce((a, b) => a + b, 0)) / data.reduce((a, b) => a + b.totalKms, 0))
433
+ const totalKms = data.reduce((a, b) => a + b.totalKms, 0)
434
+ return totalKms > 0 ? Math.round((values.reduce((a, b) => a + b, 0)) / totalKms) : 0
434
435
  }
435
436
 
436
437
  function getMaxSpeed(data) {
@@ -470,7 +471,12 @@ function isInsideTimetable(t, userData){
470
471
  console.log(tripStart, startDate, endDate)
471
472
 
472
473
  //Trips inside time period
473
- return tripStart.getTime() > startDate.getTime() && tripEnd.getTime() < endDate.getTime()
474
+ if(startDate.getTime() < endDate.getTime()) {
475
+ return tripStart.getTime() > startDate.getTime() && tripEnd.getTime() < endDate.getTime()
476
+ } else {
477
+ return (tripStart.getTime() < endDate.getTime() && tripEnd.getTime() < endDate.getTime()) ||
478
+ (tripStart.getTime() > startDate.getTime())
479
+ }
474
480
  }
475
481
 
476
482
  return false
@@ -14,12 +14,12 @@ async function getRoute(traccar, from, to, devices){
14
14
  return result.map(r => r.data).flat()
15
15
  }
16
16
 
17
- async function getTrips(traccar, from, to, devices){
18
- const devicesToSlice = devices.slice()
17
+ async function getTrips(traccar, from, to, deviceIds){
18
+ const devicesToSlice = deviceIds.slice()
19
19
  const arrayOfArrays = automaticReports.sliceArray(devicesToSlice)
20
20
  let requests = []
21
21
  for (const a of arrayOfArrays) {
22
- const promise = traccar.reports.reportsTripsGet(from, to, a.map(d => d.id))
22
+ const promise = traccar.reports.reportsTripsGet(from, to, a)
23
23
  requests = requests.concat(promise)
24
24
  }
25
25