fleetmap-reports 1.0.262 → 1.0.266

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,14 +1,9 @@
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="calculate avg speed">
4
+ <list default="true" id="eb665c86-c893-4333-bd2a-721dc27980b9" name="Changes" comment="Refactor trip and speeding reports">
5
5
  <change beforePath="$PROJECT_DIR$/package-lock.json" beforeDir="false" afterPath="$PROJECT_DIR$/package-lock.json" afterDir="false" />
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" />
8
6
  <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/trip-report.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/trip-report.js" afterDir="false" />
11
- <change beforePath="$PROJECT_DIR$/src/util/traccar.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/util/traccar.js" afterDir="false" />
12
7
  </list>
13
8
  <option name="SHOW_DIALOG" value="false" />
14
9
  <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -222,7 +217,11 @@
222
217
  <workItem from="1640866364417" duration="67000" />
223
218
  <workItem from="1640866782794" duration="123000" />
224
219
  <workItem from="1640866964393" duration="1022000" />
225
- <workItem from="1640870147277" duration="2391000" />
220
+ <workItem from="1640870147277" duration="8236000" />
221
+ <workItem from="1641321068227" duration="14805000" />
222
+ <workItem from="1641387675790" duration="32947000" />
223
+ <workItem from="1641509412324" duration="41000" />
224
+ <workItem from="1641510744190" duration="1404000" />
226
225
  </task>
227
226
  <task id="LOCAL-00001" summary="Fix getStyle">
228
227
  <created>1636983717385</created>
@@ -357,7 +356,35 @@
357
356
  <option name="project" value="LOCAL" />
358
357
  <updated>1640867522438</updated>
359
358
  </task>
360
- <option name="localTasksCounter" value="20" />
359
+ <task id="LOCAL-00020" summary="fix activity report by driver">
360
+ <created>1640872780966</created>
361
+ <option name="number" value="00020" />
362
+ <option name="presentableId" value="LOCAL-00020" />
363
+ <option name="project" value="LOCAL" />
364
+ <updated>1640872780968</updated>
365
+ </task>
366
+ <task id="LOCAL-00021" summary="activity report by driver grouped by day">
367
+ <created>1640878664617</created>
368
+ <option name="number" value="00021" />
369
+ <option name="presentableId" value="LOCAL-00021" />
370
+ <option name="project" value="LOCAL" />
371
+ <updated>1640878664617</updated>
372
+ </task>
373
+ <task id="LOCAL-00022" summary="fix kms-report by driver and fix activity report with time period">
374
+ <created>1641348028566</created>
375
+ <option name="number" value="00022" />
376
+ <option name="presentableId" value="LOCAL-00022" />
377
+ <option name="project" value="LOCAL" />
378
+ <updated>1641348028566</updated>
379
+ </task>
380
+ <task id="LOCAL-00023" summary="Refactor trip and speeding reports">
381
+ <created>1641511586537</created>
382
+ <option name="number" value="00023" />
383
+ <option name="presentableId" value="LOCAL-00023" />
384
+ <option name="project" value="LOCAL" />
385
+ <updated>1641511586537</updated>
386
+ </task>
387
+ <option name="localTasksCounter" value="24" />
361
388
  <servers />
362
389
  </component>
363
390
  <component name="TypeScriptGeneratedFilesManager">
@@ -394,6 +421,10 @@
394
421
  <MESSAGE value="location report - check if it's running on client side" />
395
422
  <MESSAGE value="timezone - check if it's running on client side" />
396
423
  <MESSAGE value="calculate avg speed" />
397
- <option name="LAST_COMMIT_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
+ <MESSAGE value="Refactor trip and speeding reports" />
428
+ <option name="LAST_COMMIT_MESSAGE" value="Refactor trip and speeding reports" />
398
429
  </component>
399
430
  </project>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "1.0.262",
3
+ "version": "1.0.266",
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,11 +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
- tripsData = await traccar.getTrips(traccarInstance, from, to, deviceIds)
166
+ if(devices.length > 0) {
167
+ tripsData = await traccar.getTrips(traccarInstance, from, to, devices)
144
168
  }
145
169
 
146
170
  const allData = {
@@ -149,7 +173,7 @@ async function createActivityReportByDriver(from, to){
149
173
  to: to
150
174
  }
151
175
 
152
- allData.drivers = processDrivers(userData.drivers, tripsData)
176
+ allData.drivers = processDrivers(from, to, userData.drivers, tripsData)
153
177
 
154
178
  return allData
155
179
  }
@@ -163,9 +187,6 @@ function processDevices(devices, data, kmsReport) {
163
187
  if (summary) {
164
188
  summary.forEach(s => {
165
189
  const deviceKms = kmsReport[0].devices.filter(r => r.device.id === d.id)
166
-
167
- console.log(deviceKms)
168
-
169
190
  s.convertedSpentFuel = automaticReports.calculateSpentFuel(s.spentFuel, d)
170
191
  s.distance = deviceKms.length ? (userData.groupByDay ? deviceKms[0].days.find(d => d.date.getTime() === s.date.getTime()).kms : deviceKms[0].summary.distance) : 0
171
192
  })
@@ -180,7 +201,7 @@ function processDevices(devices, data, kmsReport) {
180
201
  return devicesResult
181
202
  }
182
203
 
183
- function processDrivers(drivers, data) {
204
+ function processDrivers(from, to, drivers, data) {
184
205
  console.log(data)
185
206
  const driversResult = []
186
207
  drivers.forEach(d => {
@@ -188,12 +209,63 @@ function processDrivers(drivers, data) {
188
209
  if (trips.length > 0) {
189
210
  const driverData = {
190
211
  driver: d,
191
- summary: [{
212
+ summary: [],
213
+ }
214
+ if(userData.groupByDay) {
215
+ const dates = getDates(from, to)
216
+ for(const date of dates){
217
+ if(userData.allWeek || !userData.weekDays){
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({
192
264
  distance: trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0),
193
265
  engineHours: trips.reduce((a, b) => a + b.duration, 0),
194
266
  maxSpeed: trips.reduce((a, b) => Math.max(a, b.maxSpeed), 0),
195
267
  averageSpeed: Math.round(trips.reduce((a, b) => a + b.averageSpeed, 0) / trips.length),
196
- }],
268
+ })
197
269
  }
198
270
  driversResult.push(driverData)
199
271
  }
@@ -211,9 +283,11 @@ async function exportActivityReportToPDF(userData, reportData) {
211
283
  const doc = new jsPDF.jsPDF('l')
212
284
  await headerFromUser(doc, translations.report.titleActivityReport, userData.user)
213
285
 
214
- if(userData.groupByDay){
286
+ if(userData.groupByDay && userData.byDriver) {
287
+ exportActivityReportToPDF_Driver_GroupByDay(doc, translations, reportData)
288
+ } else if(userData.groupByDay) {
215
289
  exportActivityReportToPDF_Vehicle_GroupByDay(doc, translations, reportData)
216
- } else if(userData.byDriver){
290
+ } else if(userData.byDriver) {
217
291
  exportActivityReportToPDF_Driver(doc, translations, reportData)
218
292
  } else {
219
293
  exportActivityReportToPDF_Vehicle(doc, translations, reportData)
@@ -335,7 +409,7 @@ function exportActivityReportToPDF_Vehicle_GroupByDay(doc, translations, reportD
335
409
  doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
336
410
  doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
337
411
 
338
- if (!userData.allWeek) {
412
+ if (!userData.allWeek && userData.weekDays) {
339
413
  doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '')
340
414
  + (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '')
341
415
  + (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '')
@@ -430,6 +504,122 @@ function exportActivityReportToExcel_Vehicle_GroupByDay(settings, translations,
430
504
  }
431
505
  }
432
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 && userData.weekDays) {
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
+
433
623
  function exportActivityReportToExcel_Driver(settings, translations, reportData) {
434
624
  const headers = [{label: translations.report.driver, value: 'driver'},
435
625
  {label: translations.report.distance, value: 'distance'},
@@ -505,7 +695,9 @@ function exportActivityReportToExcel(reportUserData, reportData) {
505
695
  fileName: fileName // The name of the spreadsheet
506
696
  }
507
697
 
508
- if(userData.groupByDay){
698
+ if(userData.groupByDay && userData.byDriver){
699
+ return exportActivityReportToExcel_Driver_GroupByDay(settings, translations, reportData)
700
+ } else if(userData.groupByDay){
509
701
  return exportActivityReportToExcel_Vehicle_GroupByDay(settings, translations, reportData)
510
702
  } else if(userData.byDriver){
511
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
@@ -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.map(d => d.id))
30
+ const tripsData = await traccar.getTrips(traccarInstance, from, to, devicesToProcess)
30
31
 
31
32
  console.log('trips:' + tripsData.length)
32
33
 
@@ -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
 
@@ -81,7 +153,7 @@ function processDevices(from, to, devices, route, trips) {
81
153
  kms: 0
82
154
  }
83
155
 
84
- if(userData.allWeek){
156
+ if(userData.allWeek || !userData.weekDays){
85
157
  day.kms = dayTrips.reduce((a, b) => a + b.distance, 0) || 0
86
158
  } else {
87
159
  if((currentDate.getDay() === 0 && userData.weekDays.sunday) ||
@@ -230,7 +302,7 @@ async function exportKmsReportToPDF(userData, reportData) {
230
302
  doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
231
303
  doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
232
304
 
233
- if(!userData.allWeek) {
305
+ if(!userData.allWeek && userData.weekDays) {
234
306
  doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '')
235
307
  + (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '')
236
308
  + (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '')