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.
@@ -1,11 +1,17 @@
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="timezone - check if it's running on client side">
4
+ <list default="true" id="eb665c86-c893-4333-bd2a-721dc27980b9" name="Changes" comment="fix kms-report by driver and fix activity report with time period">
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/activity-report.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/activity-report.js" afterDir="false" />
7
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/kms-report.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/kms-report.js" afterDir="false" />
10
+ <change beforePath="$PROJECT_DIR$/src/speeding-report.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/speeding-report.js" afterDir="false" />
8
11
  <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/driver.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/util/driver.js" afterDir="false" />
13
+ <change beforePath="$PROJECT_DIR$/src/util/traccar.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/util/traccar.js" afterDir="false" />
14
+ <change beforePath="$PROJECT_DIR$/src/util/trips.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/util/trips.js" afterDir="false" />
9
15
  </list>
10
16
  <option name="SHOW_DIALOG" value="false" />
11
17
  <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -218,7 +224,11 @@
218
224
  <workItem from="1640794844118" duration="10128000" />
219
225
  <workItem from="1640866364417" duration="67000" />
220
226
  <workItem from="1640866782794" duration="123000" />
221
- <workItem from="1640866964393" duration="478000" />
227
+ <workItem from="1640866964393" duration="1022000" />
228
+ <workItem from="1640870147277" duration="8236000" />
229
+ <workItem from="1641321068227" duration="14805000" />
230
+ <workItem from="1641387675790" duration="32947000" />
231
+ <workItem from="1641509412324" duration="41000" />
222
232
  </task>
223
233
  <task id="LOCAL-00001" summary="Fix getStyle">
224
234
  <created>1636983717385</created>
@@ -346,7 +356,35 @@
346
356
  <option name="project" value="LOCAL" />
347
357
  <updated>1640825795053</updated>
348
358
  </task>
349
- <option name="localTasksCounter" value="19" />
359
+ <task id="LOCAL-00019" summary="calculate avg speed">
360
+ <created>1640867522437</created>
361
+ <option name="number" value="00019" />
362
+ <option name="presentableId" value="LOCAL-00019" />
363
+ <option name="project" value="LOCAL" />
364
+ <updated>1640867522438</updated>
365
+ </task>
366
+ <task id="LOCAL-00020" summary="fix activity report by driver">
367
+ <created>1640872780966</created>
368
+ <option name="number" value="00020" />
369
+ <option name="presentableId" value="LOCAL-00020" />
370
+ <option name="project" value="LOCAL" />
371
+ <updated>1640872780968</updated>
372
+ </task>
373
+ <task id="LOCAL-00021" summary="activity report by driver grouped by day">
374
+ <created>1640878664617</created>
375
+ <option name="number" value="00021" />
376
+ <option name="presentableId" value="LOCAL-00021" />
377
+ <option name="project" value="LOCAL" />
378
+ <updated>1640878664617</updated>
379
+ </task>
380
+ <task id="LOCAL-00022" summary="fix kms-report by driver and fix activity report with time period">
381
+ <created>1641348028566</created>
382
+ <option name="number" value="00022" />
383
+ <option name="presentableId" value="LOCAL-00022" />
384
+ <option name="project" value="LOCAL" />
385
+ <updated>1641348028566</updated>
386
+ </task>
387
+ <option name="localTasksCounter" value="23" />
350
388
  <servers />
351
389
  </component>
352
390
  <component name="TypeScriptGeneratedFilesManager">
@@ -382,6 +420,10 @@
382
420
  <MESSAGE value="Fix reports by driver" />
383
421
  <MESSAGE value="location report - check if it's running on client side" />
384
422
  <MESSAGE value="timezone - check if it's running on client side" />
385
- <option name="LAST_COMMIT_MESSAGE" value="timezone - check if it's running on client side" />
423
+ <MESSAGE value="calculate avg speed" />
424
+ <MESSAGE value="fix activity report by driver" />
425
+ <MESSAGE value="activity report by driver grouped by day" />
426
+ <MESSAGE value="fix kms-report by driver and fix activity report with time period" />
427
+ <option name="LAST_COMMIT_MESSAGE" value="fix kms-report by driver and fix activity report with time period" />
386
428
  </component>
387
429
  </project>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "1.0.261",
3
+ "version": "1.0.265",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -87,11 +87,14 @@ 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)
93
96
  for(const date of dates){
94
- if(userData.allWeek){
97
+ if(userData.allWeek || !userData.weekDays){
95
98
  const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
96
99
  const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
97
100
 
@@ -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
@@ -136,12 +160,11 @@ async function createActivityReportByDevice(from, to){
136
160
  }
137
161
 
138
162
  async function createActivityReportByDriver(from, to){
139
- const deviceIds = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
140
- console.log(deviceIds)
163
+ const devices = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
164
+
141
165
  let tripsData = []
142
- if(deviceIds.length > 0) {
143
- const response = await traccar.getTrips(traccarInstance, from, to, deviceIds)
144
- tripsData = response.data
166
+ if(devices.length > 0) {
167
+ tripsData = await traccar.getTrips(traccarInstance, from, to, devices)
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
  }
@@ -164,9 +187,6 @@ function processDevices(devices, data, kmsReport) {
164
187
  if (summary) {
165
188
  summary.forEach(s => {
166
189
  const deviceKms = kmsReport[0].devices.filter(r => r.device.id === d.id)
167
-
168
- console.log(deviceKms)
169
-
170
190
  s.convertedSpentFuel = automaticReports.calculateSpentFuel(s.spentFuel, d)
171
191
  s.distance = deviceKms.length ? (userData.groupByDay ? deviceKms[0].days.find(d => d.date.getTime() === s.date.getTime()).kms : deviceKms[0].summary.distance) : 0
172
192
  })
@@ -181,7 +201,7 @@ function processDevices(devices, data, kmsReport) {
181
201
  return devicesResult
182
202
  }
183
203
 
184
- function processDrivers(drivers, data) {
204
+ function processDrivers(from, to, drivers, data) {
185
205
  console.log(data)
186
206
  const driversResult = []
187
207
  drivers.forEach(d => {
@@ -189,12 +209,63 @@ function processDrivers(drivers, data) {
189
209
  if (trips.length > 0) {
190
210
  const driverData = {
191
211
  driver: d,
192
- summary: {
212
+ summary: [],
213
+ }
214
+ if(userData.groupByDay) {
215
+ const dates = getDates(from, to)
216
+ for(const date of dates){
217
+ if(userData.allWeek){
218
+ const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
219
+ const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
220
+
221
+ const tripsByDay = trips.filter(t => new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay)
222
+
223
+ driverData.summary.push({
224
+ date: date,
225
+ distance: tripsByDay.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0),
226
+ engineHours: tripsByDay.reduce((a, b) => a + b.duration, 0),
227
+ maxSpeed: tripsByDay.reduce((a, b) => Math.max(a, b.maxSpeed), 0),
228
+ averageSpeed: tripsByDay.length > 0 ? Math.round(tripsByDay.reduce((a, b) => a + b.averageSpeed, 0) / tripsByDay.length) : 0,
229
+ })
230
+ } else {
231
+ if((date.getDay() === 0 && userData.weekDays.sunday) ||
232
+ (date.getDay() === 1 && userData.weekDays.monday) ||
233
+ (date.getDay() === 2 && userData.weekDays.tuesday) ||
234
+ (date.getDay() === 3 && userData.weekDays.wednesday) ||
235
+ (date.getDay() === 4 && userData.weekDays.thursday) ||
236
+ (date.getDay() === 5 && userData.weekDays.friday) ||
237
+ (date.getDay() === 6 && userData.weekDays.saturday)) {
238
+
239
+ const fromByDay = new Date(new Date(date).toISOString().split('T')[0] + ' ' + userData.dayHours.startTime)
240
+ const toByDay = new Date(new Date(date).toISOString().split('T')[0] + ' ' + userData.dayHours.endTime)
241
+
242
+ const tripsByDay = trips.filter(t => new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay)
243
+
244
+ driverData.summary.push({
245
+ date: date,
246
+ distance: tripsByDay.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0),
247
+ engineHours: tripsByDay.reduce((a, b) => a + b.duration, 0),
248
+ maxSpeed: tripsByDay.reduce((a, b) => Math.max(a, b.maxSpeed), 0),
249
+ averageSpeed: tripsByDay.length > 0 ? Math.round(tripsByDay.reduce((a, b) => a + b.averageSpeed, 0) / tripsByDay.length) : 0,
250
+ })
251
+ }else {
252
+ driverData.summary.push({
253
+ date: date,
254
+ distance: 0,
255
+ engineHours: 0,
256
+ maxSpeed: 0,
257
+ averageSpeed: 0,
258
+ })
259
+ }
260
+ }
261
+ }
262
+ } else {
263
+ driverData.summary.push({
193
264
  distance: trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0),
194
265
  engineHours: trips.reduce((a, b) => a + b.duration, 0),
195
266
  maxSpeed: trips.reduce((a, b) => Math.max(a, b.maxSpeed), 0),
196
267
  averageSpeed: Math.round(trips.reduce((a, b) => a + b.averageSpeed, 0) / trips.length),
197
- },
268
+ })
198
269
  }
199
270
  driversResult.push(driverData)
200
271
  }
@@ -212,9 +283,11 @@ async function exportActivityReportToPDF(userData, reportData) {
212
283
  const doc = new jsPDF.jsPDF('l')
213
284
  await headerFromUser(doc, translations.report.titleActivityReport, userData.user)
214
285
 
215
- if(userData.groupByDay){
286
+ if(userData.groupByDay && userData.byDriver) {
287
+ exportActivityReportToPDF_Driver_GroupByDay(doc, translations, reportData)
288
+ } else if(userData.groupByDay) {
216
289
  exportActivityReportToPDF_Vehicle_GroupByDay(doc, translations, reportData)
217
- } else if(userData.byDriver){
290
+ } else if(userData.byDriver) {
218
291
  exportActivityReportToPDF_Driver(doc, translations, reportData)
219
292
  } else {
220
293
  exportActivityReportToPDF_Vehicle(doc, translations, reportData)
@@ -257,7 +330,7 @@ function exportActivityReportToPDF_Driver(doc, translations, reportData) {
257
330
  }
258
331
 
259
332
  function exportActivityReportToPDF_Vehicle(doc, translations, reportData) {
260
- const headers = [translations.report.name,
333
+ const headers = [translations.report.vehicle,
261
334
  translations.report.group,
262
335
  translations.report.distance,
263
336
  translations.report.start + ' ' + translations.report.odometer,
@@ -275,7 +348,7 @@ function exportActivityReportToPDF_Vehicle(doc, translations, reportData) {
275
348
  const group = userData.groups.find(g => d.device.groupId === g.id)
276
349
 
277
350
  const temp = [
278
- d.summary.deviceName,
351
+ d.summary[0].deviceName,
279
352
  group ? group.name : '',
280
353
  Number((d.summary[0].distance / 1000).toFixed(2)),
281
354
  Number((d.summary[0].startOdometer / 1000).toFixed(2)),
@@ -431,6 +504,122 @@ function exportActivityReportToExcel_Vehicle_GroupByDay(settings, translations,
431
504
  }
432
505
  }
433
506
 
507
+ function exportActivityReportToPDF_Driver_GroupByDay(doc, translations, reportData) {
508
+ const headers = [translations.report.date,
509
+ translations.report.distance,
510
+ translations.report.avgSpeed + ' (Km/h)',
511
+ translations.report.maxSpeed + ' (Km/h)',
512
+ translations.report.engineHours]
513
+
514
+ const weekDays = [
515
+ translations.report.sunday,
516
+ translations.report.monday,
517
+ translations.report.tuesday,
518
+ translations.report.wednesday,
519
+ translations.report.thursday,
520
+ translations.report.friday,
521
+ translations.report.saturday
522
+ ]
523
+
524
+ reportData.drivers.forEach(function (d, index) {
525
+ const name = d.driver.name
526
+ const group = userData.groups.find(g => g.drivers.includes(d.id))
527
+
528
+ let space = index === 0 ? 8 : 0
529
+ if(index){
530
+ doc.addPage()
531
+ }
532
+
533
+ doc.setFontSize(13)
534
+ doc.text(name, 20, space + 20)
535
+ doc.setFontSize(11)
536
+ doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
537
+ doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
538
+
539
+ if (!userData.allWeek) {
540
+ doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '')
541
+ + (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '')
542
+ + (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '')
543
+ + (userData.weekDays.thursday ? ' ' + translations.report.thursday + ',' : '')
544
+ + (userData.weekDays.friday ? ' ' + translations.report.friday + ',' : '')
545
+ + (userData.weekDays.saturday ? ' ' + translations.report.saturday + ',' : '')
546
+ + (userData.weekDays.sunday ? ' ' + translations.report.sunday + ',' : '')
547
+ + ' das '
548
+ + userData.dayHours.startTime + ' - ' + userData.dayHours.endTime, 20, space + 30)
549
+ }
550
+
551
+ const data = []
552
+ d.summary.forEach(s => {
553
+ const temp = [
554
+ new Date(s.date).toLocaleDateString() + ' - ' + weekDays[s.date.getDay()],
555
+ Number((s.distance / 1000).toFixed(2)),
556
+ Math.round(s.averageSpeed * 1.85200),
557
+ Math.round(s.maxSpeed * 1.85200),
558
+ convertMS(s.engineHours)
559
+ ]
560
+
561
+ data.push(temp)
562
+ })
563
+
564
+ const footValues = ['Total:' + d.summary.length,
565
+ (d.summary.reduce((a, b) => a + b.distance, 0) / 1000).toFixed(0),
566
+ getSumAvgSpeed(d.summary),
567
+ getSumMaxSpeed(d.summary),
568
+ convertMS(d.summary.reduce((a, b) => a + b.engineHours, 0))]
569
+
570
+ const style = getStyle(getUserPartner(userData.user))
571
+ addTable(doc, headers, data, footValues, style, 40)
572
+ })
573
+ }
574
+
575
+ function exportActivityReportToExcel_Driver_GroupByDay(settings, translations, reportData) {
576
+ const headers = [{label: translations.report.driver, value: 'name'},
577
+ {label: translations.report.group, value: 'group'},
578
+ {label: translations.report.date, value: 'date'},
579
+ {label: translations.report.weekDay, value: 'weekday'},
580
+ {label: translations.report.distance, value: 'distance'},
581
+ {label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed'},
582
+ {label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed'},
583
+ {label: translations.report.engineHours, value: 'engineHours'}]
584
+
585
+ const weekDays = [
586
+ translations.report.sunday,
587
+ translations.report.monday,
588
+ translations.report.tuesday,
589
+ translations.report.wednesday,
590
+ translations.report.thursday,
591
+ translations.report.friday,
592
+ translations.report.saturday
593
+ ]
594
+
595
+ let data = []
596
+ if (reportData.drivers) {
597
+ reportData.drivers.forEach(d => {
598
+ const group = userData.groups.find(g => g.drivers.includes(d.id))
599
+
600
+ data = data.concat([{}])
601
+ data = data.concat(d.summary.map(s => {
602
+ return {
603
+ name: d.driver.name,
604
+ group: group ? group.name : '',
605
+ date: new Date(s.date).toLocaleDateString(),
606
+ weekday: weekDays[s.date.getDay()],
607
+ distance: Number((s.distance / 1000).toFixed(0)),
608
+ avgSpeed: Math.round(s.averageSpeed * 1.85200),
609
+ maxSpeed: Math.round(s.maxSpeed * 1.85200),
610
+ engineHours: convertMS(s.engineHours)
611
+ }
612
+ }))
613
+ })
614
+ console.log(data)
615
+ return {
616
+ headers,
617
+ data,
618
+ settings
619
+ }
620
+ }
621
+ }
622
+
434
623
  function exportActivityReportToExcel_Driver(settings, translations, reportData) {
435
624
  const headers = [{label: translations.report.driver, value: 'driver'},
436
625
  {label: translations.report.distance, value: 'distance'},
@@ -443,10 +632,10 @@ function exportActivityReportToExcel_Driver(settings, translations, reportData)
443
632
  reportData.drivers.forEach(d => {
444
633
  data = data.concat([{
445
634
  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)
635
+ distance: Number((d.summary[0].distance / 1000).toFixed(0)),
636
+ avgSpeed: Math.round(d.summary[0].averageSpeed * 1.85200),
637
+ maxSpeed: Math.round(d.summary[0].maxSpeed * 1.85200),
638
+ engineHours: convertMS(d.summary[0].engineHours)
450
639
  }])
451
640
  })
452
641
  console.log(data)
@@ -475,15 +664,15 @@ function exportActivityReportToExcel_Vehicle(settings, translations, reportData)
475
664
  const group = userData.groups.find(g => d.device.groupId === g.id)
476
665
 
477
666
  data = data.concat([{
478
- name: d.summary.deviceName,
667
+ name: d.summary[0].deviceName,
479
668
  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,
669
+ distance: Number((d.summary[0].distance / 1000).toFixed(0)),
670
+ startOdometer: Number((d.summary[0].startOdometer / 1000).toFixed(0)),
671
+ endOdometer: Number((d.summary[0].endOdometer / 1000).toFixed(0)),
672
+ avgSpeed: Math.round(d.summary[0].averageSpeed * 1.85200),
673
+ maxSpeed: Math.round(d.summary[0].maxSpeed * 1.85200),
674
+ engineHours: convertMS(d.summary[0].engineHours),
675
+ spentFuel: d.summary[0].convertedSpentFuel >= 0 ? d.summary[0].convertedSpentFuel : 0,
487
676
  }])
488
677
  })
489
678
  console.log(data)
@@ -506,7 +695,9 @@ function exportActivityReportToExcel(reportUserData, reportData) {
506
695
  fileName: fileName // The name of the spreadsheet
507
696
  }
508
697
 
509
- if(userData.groupByDay){
698
+ if(userData.groupByDay && userData.byDriver){
699
+ return exportActivityReportToExcel_Driver_GroupByDay(settings, translations, reportData)
700
+ } else if(userData.groupByDay){
510
701
  return exportActivityReportToExcel_Vehicle_GroupByDay(settings, translations, reportData)
511
702
  } else if(userData.byDriver){
512
703
  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
@@ -39,13 +40,84 @@ async function createKmsReportByDevice(from, to) {
39
40
  }
40
41
 
41
42
  async function createKmsReportByDriver(from, to) {
43
+ const devices = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
44
+ console.log(devices.length)
42
45
 
46
+ if(!devices.length) {
47
+ //empty report
48
+ return { drivers: [] }
49
+ }
50
+
51
+ const tripsData = await traccar.getTrips(traccarInstance, from, to, devices)
52
+ return { drivers: processDrivers(from, to, userData.drivers, tripsData) }
43
53
  }
44
54
 
45
55
  async function createKmsReportByGroup(from, to) {
46
56
 
47
57
  }
48
58
 
59
+ function processDrivers(from, to, drivers, data) {
60
+ console.log(data)
61
+ const driversResult = []
62
+ drivers.forEach(d => {
63
+ const trips = data.filter(t => t.driverUniqueId === d.uniqueId)
64
+ if (trips.length > 0) {
65
+ const driverData = {
66
+ driver: d
67
+ }
68
+ if(userData.groupByDay) {
69
+ driverData.days = []
70
+ const dates = getDates(from, to)
71
+ for(const date of dates){
72
+ if(userData.allWeek || !userData.weekDays){
73
+ const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
74
+ const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
75
+
76
+ const tripsByDay = trips.filter(t => new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay)
77
+
78
+ driverData.days.push({
79
+ date: date,
80
+ kms: tripsByDay.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
81
+ })
82
+ } else {
83
+ if((date.getDay() === 0 && userData.weekDays.sunday) ||
84
+ (date.getDay() === 1 && userData.weekDays.monday) ||
85
+ (date.getDay() === 2 && userData.weekDays.tuesday) ||
86
+ (date.getDay() === 3 && userData.weekDays.wednesday) ||
87
+ (date.getDay() === 4 && userData.weekDays.thursday) ||
88
+ (date.getDay() === 5 && userData.weekDays.friday) ||
89
+ (date.getDay() === 6 && userData.weekDays.saturday)) {
90
+
91
+ const fromByDay = new Date(new Date(date).toISOString().split('T')[0] + ' ' + userData.dayHours.startTime)
92
+ const toByDay = new Date(new Date(date).toISOString().split('T')[0] + ' ' + userData.dayHours.endTime)
93
+
94
+ const tripsByDay = trips.filter(t => new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay)
95
+
96
+ driverData.days.push({
97
+ date: date,
98
+ kms: tripsByDay.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
99
+ })
100
+ }else {
101
+ driverData.days.push({
102
+ date: date,
103
+ kms: 0
104
+ })
105
+ }
106
+ }
107
+ }
108
+ } else {
109
+ driverData.summary = {
110
+ distance: trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
111
+ }
112
+ }
113
+ driversResult.push(driverData)
114
+ }
115
+ })
116
+
117
+ return driversResult
118
+ }
119
+
120
+
49
121
  function processDevices(from, to, devices, route, trips) {
50
122
  const devicesResult = []
51
123