fleetmap-reports 1.0.470 → 1.0.471

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.471",
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.on, ignition.off)
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, ignitions.length ? ignitions[0].on : undefined, ignitions.length ? ignitions[ignitions.length - 1].off : undefined)
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,50 @@ function processDevices (from, to, devices, data, userData) {
111
94
  return devicesResult
112
95
  }
113
96
 
97
+ function getSummaryData (date, device, route, start, end) {
98
+ return {
99
+ date,
100
+ deviceId: device.id,
101
+ engineHours: start ? route[route.length - 1].attributes.hours - route[0].attributes.hours : undefined,
102
+ totalHours: route.length ? route[route.length - 1].attributes.hours : undefined,
103
+ startTime: start ? start.fixTime : undefined,
104
+ endTime: end ? end.fixTime : undefined
105
+ }
106
+ }
107
+
108
+ function calculateIgnitions (routeOfDay) {
109
+ let indexStart
110
+ let last
111
+ const ignitions = []
112
+ routeOfDay.forEach((p, index) => {
113
+ // difference between last position and current position is bigger than 5 minutes
114
+ if ((last && last.attributes.ignition && (new Date(p.fixTime).getTime() - new Date(last.fixTime).getTime()) > 300000) &&
115
+ ignitions.length &&
116
+ !ignitions[ignitions.length - 1].off) {
117
+ ignitions[ignitions.length - 1].off = last
118
+ ignitions[ignitions.length - 1].route = routeOfDay.slice(indexStart, index)
119
+ }
120
+
121
+ if (p.attributes.ignition && (!ignitions.length || ignitions[ignitions.length - 1].off)) {
122
+ ignitions.push({ on: p })
123
+ indexStart = index
124
+ }
125
+
126
+ // ignition is off
127
+ // or is last route position
128
+ if ((!p.attributes.ignition || index + 1 === routeOfDay.length) &&
129
+ ignitions.length &&
130
+ !ignitions[ignitions.length - 1].off) {
131
+ ignitions[ignitions.length - 1].off = p
132
+ ignitions[ignitions.length - 1].route = (index + 1) === routeOfDay.length ? routeOfDay.slice(indexStart) : routeOfDay.slice(indexStart, index + 1)
133
+ }
134
+
135
+ last = p
136
+ })
137
+
138
+ return ignitions
139
+ }
140
+
114
141
  async function exportMachinesReportToPDF (userData, reportData) {
115
142
  const doc = new jsPDF.jsPDF('l')
116
143
  const translations = getTranslations(userData)
@@ -163,7 +190,7 @@ async function exportMachinesReportToPDF (userData, reportData) {
163
190
 
164
191
  const footValues = ['',
165
192
  '', '',
166
- convertMS(d.summary.reduce((a, b) => a + b.engineHours, 0))]
193
+ convertMS(d.summary.reduce((a, b) => a + (b.engineHours || 0), 0))]
167
194
 
168
195
  const style = getStyle(getUserPartner(userData.user))
169
196
  addTable(doc, headers, data, footValues, style, 40)