fleetmap-reports 1.0.947 → 1.0.948

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/kms-report.js +66 -16
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "1.0.947",
3
+ "version": "1.0.948",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/kms-report.js CHANGED
@@ -7,7 +7,7 @@ const {
7
7
  } = require('./util/pdfDocument')
8
8
  const { getUserPartner } = require('fleetmap-partners')
9
9
  const traccar = require('./util/traccar')
10
- const { getDates, getTranslations, isClientSide, executeServerSide } = require('./util/utils')
10
+ const { getDates, getTranslations, isClientSide, executeServerSide, convertFromUTC, convertFromLocal } = require('./util/utils')
11
11
  const drivers = require('./util/driver')
12
12
  const { isInsideTimetable, isPartialInsideTimetable, calculateTrip, getDataByDay, checkTripsKms, getKms } = require('./util/trips')
13
13
  const traccarHelper = require('./util/traccar')
@@ -34,6 +34,30 @@ async function createKmsReport (from, to, userData, traccarInstance) {
34
34
  return reportData
35
35
  }
36
36
 
37
+ async function getSummaryByDay (dates, userData, traccarInstance, allDevices) {
38
+ const days = []
39
+ let counter = 0
40
+ for (const date of dates) {
41
+ const startDateLocal = new Date(convertFromUTC(date, userData.user.attributes.timezone).toISOString().split('T')[0] + ' ' + userData.dayHours.startTime)
42
+ const endDateLocal = new Date(convertFromUTC(date, userData.user.attributes.timezone).toISOString().split('T')[0] + ' ' + userData.dayHours.endTime)
43
+ const startDate = isClientSide() ? startDateLocal : convertFromLocal(startDateLocal, userData.user.attributes.timezone)
44
+ const endDate = isClientSide() ? endDateLocal : convertFromLocal(endDateLocal, userData.user.attributes.timezone)
45
+
46
+ const {
47
+ summary
48
+ } = await traccar.getAllInOne(traccarInstance, startDate, endDate, allDevices, false, false, false, true, 0, allDevices.length, allDevices.length, allDevices.length, undefined, true)
49
+
50
+ counter = counter + 1
51
+ console.log(`PROGRESS_PERC:${counter / dates.length * 100}`)
52
+ days.push({
53
+ date,
54
+ summary
55
+ })
56
+ }
57
+
58
+ return days
59
+ }
60
+
37
61
  async function createKmsReportByDevice (from, to, userData, traccarInstance) {
38
62
  const allDevices = devicesToProcess(userData)
39
63
 
@@ -47,24 +71,50 @@ async function createKmsReportByDevice (from, to, userData, traccarInstance) {
47
71
  const sliced = automaticReports.sliceArray(allDevices, 1)
48
72
  await executeServerSide('kms-report', allData, sliced, from, to, userData, allDevices.length)
49
73
  } else {
50
- let deviceCount = 0
51
- const sliced = automaticReports.sliceArray(allDevices, 20)
52
- for (const devices of sliced) {
53
- const needRoute = !userData.allWeek
54
- const {
55
- trips,
56
- route
57
- } = await traccar.getAllInOne(traccarInstance, from, to, devices, needRoute, true, false, false, deviceCount, allDevices.length, 10, 2)
58
-
59
- // Process report data
60
- if (trips.length) {
61
- checkTripsKms(traccarInstance, from, to, devices, { trips, route })
62
- allData.devices = allData.devices.concat(await processDevices(from, to, devices, {
74
+ if (userData.allWeek && userData.groupByDay) {
75
+ const dates = getDates(from, to, userData.user.attributes.timezone || getUserPartner(userData.user).timezone)
76
+ const summary = await getSummaryByDay(dates, userData, traccarInstance, allDevices)
77
+ for (const device of allDevices) {
78
+ const days = []
79
+ dates.forEach(date => {
80
+ const summaryByDay = summary.find(a => a.date === date).summary
81
+ const summaryByDevice = summaryByDay.find(a => a.deviceId === device.id)
82
+ days.push({
83
+ date,
84
+ kms: summaryByDevice.distance,
85
+ startOdometer: summaryByDevice.startOdometer,
86
+ endOdometer: summaryByDevice.endOdometer
87
+ })
88
+ })
89
+ const daysWithOdometer = days.filter(a => a.startOdometer > 0)
90
+ allData.devices.push({
91
+ device,
92
+ startOdometer: daysWithOdometer.length > 0 ? daysWithOdometer[0].startOdometer : 0,
93
+ endOdometer: daysWithOdometer.length > 0 ? daysWithOdometer[daysWithOdometer.length - 1].endOdometer : 0,
94
+ days
95
+ })
96
+ }
97
+ } else {
98
+ let deviceCount = 0
99
+ const sliced = automaticReports.sliceArray(allDevices,
100
+ 20)
101
+ for (const devices of sliced) {
102
+ const needRoute = !userData.allWeek
103
+ const {
63
104
  trips,
64
105
  route
65
- }, userData, traccarInstance))
106
+ } = await traccar.getAllInOne(traccarInstance, from, to, devices, needRoute, true, false, false, deviceCount, allDevices.length, 10, 2)
107
+
108
+ // Process report data
109
+ if (trips.length) {
110
+ checkTripsKms(traccarInstance, from, to, devices, { trips, route })
111
+ allData.devices = allData.devices.concat(await processDevices(from, to, devices, {
112
+ trips,
113
+ route
114
+ }, userData, traccarInstance))
115
+ }
116
+ deviceCount += devices.length
66
117
  }
67
- deviceCount += devices.length
68
118
  }
69
119
  }
70
120