fleetmap-reports 1.0.470 → 1.0.472

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.470",
3
+ "version": "1.0.472",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -38,8 +38,6 @@ async function createMachinesReportByDevice (from, to, userData, traccar) {
38
38
  const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, true, false, false, false, deviceCount, devices.length)
39
39
  const routeData = allInOne.route
40
40
 
41
- console.log('Route:' + routeData.length)
42
-
43
41
  if (routeData.length > 0) {
44
42
  allData.devices.push(...processDevices(from, to, slice, {
45
43
  route: routeData
@@ -63,41 +61,26 @@ function processDevices (from, to, devices, data, userData) {
63
61
  const convertFrom = new Date(convertFromUTC(from, userData.user.attributes.timezone))
64
62
  const convertTo = new Date(convertFromUTC(to, userData.user.attributes.timezone))
65
63
 
66
- const dates = getDates(convertFrom, convertTo)
67
-
68
- for (const date of dates) {
69
- const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
70
- const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
71
-
72
- const routeByDay = deviceRoute.filter(p => (new Date(p.fixTime) > fromByDay && new Date(p.fixTime) < toByDay) && p.valid && p.attributes.hours !== undefined)
73
-
74
- routeByDay.sort((a, b) => new Date(a.fixTime).getTime() - new Date(b.fixTime).getTime())
75
-
76
- let firstOn
77
- let lastOff
78
-
79
- routeByDay.forEach(p => {
80
- if (p.attributes.ignition || (firstOn && !p.attributes.ignition && lastOff.attributes.ignition)) {
81
- lastOff = p
82
- }
83
-
84
- if (p.attributes.ignition && !firstOn) {
85
- firstOn = p
86
- }
87
- })
88
-
89
- const dayData = {
90
- date,
91
- deviceId: d.id,
92
- engineHours: firstOn ? routeByDay[routeByDay.length - 1].attributes.hours - routeByDay[0].attributes.hours : 0,
93
- totalHours: routeByDay.length ? routeByDay[routeByDay.length - 1].attributes.hours : undefined,
94
- startTime: firstOn ? firstOn.fixTime : undefined,
95
- endTime: lastOff ? lastOff.fixTime : undefined
64
+ if (userData.withDetails) {
65
+ const ignitions = calculateIgnitions(deviceRoute)
66
+ for (const ignition of ignitions) {
67
+ const dayData = getSummaryData(new Date(ignition.on.fixTime), d, ignition.route, ignition, undefined)
68
+ summary.push(dayData)
69
+ }
70
+ } else {
71
+ const dates = getDates(convertFrom, convertTo)
72
+ for (const date of dates) {
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 routeByDay = deviceRoute.filter(p => (new Date(p.fixTime) > fromByDay && new Date(p.fixTime) < toByDay) && p.valid && p.attributes.hours !== undefined)
77
+ routeByDay.sort((a, b) => new Date(a.fixTime).getTime() - new Date(b.fixTime).getTime())
78
+
79
+ const ignitions = calculateIgnitions(routeByDay)
80
+ const dayData = getSummaryData(date, d, routeByDay, undefined, ignitions)
81
+ summary.push(dayData)
96
82
  }
97
-
98
- summary.push(dayData)
99
83
  }
100
-
101
84
  if (summary.length) {
102
85
  const deviceData = {
103
86
  device: d,
@@ -111,6 +94,56 @@ function processDevices (from, to, devices, data, userData) {
111
94
  return devicesResult
112
95
  }
113
96
 
97
+ function getSummaryData (date, device, route, ignition, ignitions) {
98
+ const startTime = ignition ? ignition.on.fixTime : (ignitions.length ? ignitions[0].on.fixTime : undefined)
99
+ const endTime = ignition ? ignition.off.fixTime : (ignitions.length ? ignitions[ignitions.length - 1].off.fixTime : undefined)
100
+ const engineHours = ignition
101
+ ? ignition.route[ignition.route.length - 1].attributes.hours - ignition.route[0].attributes.hours
102
+ : (ignitions ? ignitions.reduce((a, b) => a + (b.route[b.route.length - 1].attributes.hours - b.route[0].attributes.hours), 0) : undefined)
103
+
104
+ return {
105
+ date,
106
+ deviceId: device.id,
107
+ engineHours,
108
+ totalHours: route.length ? route[route.length - 1].attributes.hours : undefined,
109
+ startTime,
110
+ endTime
111
+ }
112
+ }
113
+
114
+ function calculateIgnitions (routeOfDay) {
115
+ let indexStart
116
+ let last
117
+ const ignitions = []
118
+ routeOfDay.forEach((p, index) => {
119
+ // difference between last position and current position is bigger than 5 minutes
120
+ if ((last && last.attributes.ignition && (new Date(p.fixTime).getTime() - new Date(last.fixTime).getTime()) > 300000) &&
121
+ ignitions.length &&
122
+ !ignitions[ignitions.length - 1].off) {
123
+ ignitions[ignitions.length - 1].off = last
124
+ ignitions[ignitions.length - 1].route = routeOfDay.slice(indexStart, index)
125
+ }
126
+
127
+ if (p.attributes.ignition && (!ignitions.length || ignitions[ignitions.length - 1].off)) {
128
+ ignitions.push({ on: p })
129
+ indexStart = index
130
+ }
131
+
132
+ // ignition is off
133
+ // or is last route position
134
+ if ((!p.attributes.ignition || index + 1 === routeOfDay.length) &&
135
+ ignitions.length &&
136
+ !ignitions[ignitions.length - 1].off) {
137
+ ignitions[ignitions.length - 1].off = p
138
+ ignitions[ignitions.length - 1].route = (index + 1) === routeOfDay.length ? routeOfDay.slice(indexStart) : routeOfDay.slice(indexStart, index + 1)
139
+ }
140
+
141
+ last = p
142
+ })
143
+
144
+ return ignitions
145
+ }
146
+
114
147
  async function exportMachinesReportToPDF (userData, reportData) {
115
148
  const doc = new jsPDF.jsPDF('l')
116
149
  const translations = getTranslations(userData)
@@ -163,7 +196,7 @@ async function exportMachinesReportToPDF (userData, reportData) {
163
196
 
164
197
  const footValues = ['',
165
198
  '', '',
166
- convertMS(d.summary.reduce((a, b) => a + b.engineHours, 0))]
199
+ convertMS(d.summary.reduce((a, b) => a + (b.engineHours || 0), 0))]
167
200
 
168
201
  const style = getStyle(getUserPartner(userData.user))
169
202
  addTable(doc, headers, data, footValues, style, 40)