fleetmap-reports 1.0.946 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "1.0.946",
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
 
package/src/util/trips.js CHANGED
@@ -111,32 +111,25 @@ function isPartialInsideTimetable (t, userData, route) {
111
111
  }
112
112
 
113
113
  async function getDataByDay (device, date, data, userData, traccarInstance) {
114
- const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
115
- const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
116
- const convertedFromByDay = isClientSide() ? fromByDay : convertFromLocal(fromByDay, userData.user.attributes.timezone)
117
- const convertedToByDay = isClientSide() ? toByDay : convertFromLocal(toByDay, userData.user.attributes.timezone)
118
- console.log(fromByDay, toByDay)
119
- const tripsByDay = data.trips.filter(t => new Date(t.endTime) > convertedFromByDay && new Date(t.startTime) < convertedToByDay).map(t => {
120
- return { ...t }
121
- })
122
- console.log(date, tripsByDay.length)
123
114
  const startDateLocal = new Date(convertFromUTC(date, userData.user.attributes.timezone).toISOString().split('T')[0] + ' ' + userData.dayHours.startTime)
124
115
  const endDateLocal = new Date(convertFromUTC(date, userData.user.attributes.timezone).toISOString().split('T')[0] + ' ' + userData.dayHours.endTime)
125
116
  const startDate = isClientSide() ? startDateLocal : convertFromLocal(startDateLocal, userData.user.attributes.timezone)
126
117
  const endDate = isClientSide() ? endDateLocal : convertFromLocal(endDateLocal, userData.user.attributes.timezone)
118
+
119
+ const tripsByDay = data.trips.filter(t => new Date(t.endTime) > startDate && new Date(t.startTime) < endDate).map(t => {
120
+ return { ...t }
121
+ })
122
+
127
123
  for (const t of tripsByDay) {
128
124
  await calculatePartialTrip(device, startDate, endDate, data.route, t, traccarInstance)
129
125
  }
130
126
 
131
- const routeByDay = data.route ? data.route.filter(p => (new Date(p.fixTime) > fromByDay && new Date(p.fixTime) < toByDay)) : []
127
+ const routeByDay = data.route ? data.route.filter(p => (new Date(p.fixTime) > startDate && new Date(p.fixTime) < endDate)) : []
132
128
  return { tripsByDay, routeByDay }
133
129
  }
134
130
 
135
131
  function getKms (trips) {
136
132
  const odometerKms = trips.length > 0 ? trips[trips.length - 1].endOdometer - trips[0].startOdometer : 0
137
- if (trips.length > 0) {
138
- console.log(trips[trips.length - 1].endOdometer, trips[0].startOdometer, odometerKms)
139
- }
140
133
  return odometerKms > 0 ? odometerKms : trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
141
134
  }
142
135