fleetmap-reports 1.0.465 → 1.0.469

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/lang/enGB.js CHANGED
@@ -226,6 +226,7 @@ module.exports = {
226
226
  endOdometer: 'End (Km)',
227
227
  spentFuel: 'Spent Fuel',
228
228
  engineHours: 'Use (H:m)',
229
+ totalEngineHours: 'Total Use (H:m)',
229
230
  driverHours: 'Driving Time (H:m)',
230
231
  refueling: 'Refueling',
231
232
  fueldrop: 'Fuel Drop (L)',
package/lang/esCL.js CHANGED
@@ -215,6 +215,7 @@ module.exports = {
215
215
  endOdometer: 'End (Km)',
216
216
  spentFuel: 'Consumo',
217
217
  engineHours: 'Utilización (HH:mm)',
218
+ totalEngineHours: 'Utilización Total (H:m)',
218
219
  driverHours: 'Tiempo de Conducción (H:m)',
219
220
  refueling: 'Cargas',
220
221
  fueldrop: 'Combustível Perdido (L)',
package/lang/ptBR.js CHANGED
@@ -218,6 +218,7 @@ module.exports = {
218
218
  endOdometer: 'Fim (Km)',
219
219
  spentFuel: 'Consumo (L)',
220
220
  engineHours: 'Utilização (H:m)',
221
+ totalEngineHours: 'Total Use (H:m)',
221
222
  driverHours: 'Tempo de Condução (H:m)',
222
223
  refueling: 'Abastecimentos',
223
224
  fueldrop: 'Combustível Perdido (L)',
package/lang/ptPT.js CHANGED
@@ -224,6 +224,7 @@ module.exports = {
224
224
  endOdometer: 'Fim (Km)',
225
225
  spentFuel: 'Consumo (L)',
226
226
  engineHours: 'Utilização (H:m)',
227
+ totalEngineHours: 'Total Use (H:m)',
227
228
  driverHours: 'Tempo de Condução (H:m)',
228
229
  refueling: 'Abastecimentos',
229
230
  fueldrop: 'Combustível Perdido (L)',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "1.0.465",
3
+ "version": "1.0.469",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -1,8 +1,7 @@
1
1
  const { devicesToProcess } = require('./util/device')
2
2
  const automaticReports = require('./automaticReports')
3
3
  const traccarHelper = require('./util/traccar')
4
- const { calculateTrip, isInsideTimetable, isPartialInsideTimetable } = require('./util/trips')
5
- const { getDates, weekDaySelected, convertMS, getTranslations } = require('./util/utils')
4
+ const { getDates, convertMS, getTranslations, convertFromUTC } = require('./util/utils')
6
5
  const { getStyle } = require('./reportStyle')
7
6
  const { getUserPartner } = require('fleetmap-partners')
8
7
  const { addTable, headerFromUser } = require('./util/pdfDocument')
@@ -36,15 +35,13 @@ async function createMachinesReportByDevice (from, to, userData, traccar) {
36
35
 
37
36
  let deviceCount = 0
38
37
  for (const slice of sliced) {
39
- const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, true, true, false, false, deviceCount, devices.length)
40
- const tripsData = allInOne.trips
38
+ const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, true, false, false, false, deviceCount, devices.length)
41
39
  const routeData = allInOne.route
42
40
 
43
- console.log('Trips:' + tripsData.length)
41
+ console.log('Route:' + routeData.length)
44
42
 
45
- if (tripsData.length > 0) {
43
+ if (routeData.length > 0) {
46
44
  allData.devices.push(...processDevices(from, to, slice, {
47
- trips: tripsData,
48
45
  route: routeData
49
46
  }, userData))
50
47
  }
@@ -54,58 +51,60 @@ async function createMachinesReportByDevice (from, to, userData, traccar) {
54
51
  return allData
55
52
  }
56
53
 
57
- function createSummary (date, d, tripsByDay) {
58
- return {
59
- date,
60
- deviceId: d.id,
61
- engineHours: tripsByDay.reduce((a, b) => a + b.duration, 0),
62
- startTime: tripsByDay.length && tripsByDay[0].startTime,
63
- endTime: tripsByDay.length && tripsByDay[tripsByDay.length - 1].endTime
64
- }
65
- }
66
-
67
54
  function processDevices (from, to, devices, data, userData) {
68
55
  const devicesResult = []
69
56
 
70
57
  devices.forEach(d => {
71
- const deviceTrips = data.trips.filter(t => t.deviceId === d.id)
72
58
  const deviceRoute = data.route.filter(t => t.deviceId === d.id)
73
59
 
74
- if (deviceRoute.length > 0) {
75
- let uncompletedTripRoute = []
76
- if (deviceRoute.length && deviceTrips.length && deviceRoute[deviceRoute.length - 1].attributes.ignition) {
77
- uncompletedTripRoute = deviceRoute.filter(p => p.attributes.ignition && new Date(p.fixTime).getTime() > new Date(deviceTrips[deviceTrips.length - 1].endTime).getTime())
78
- if (uncompletedTripRoute.length) {
79
- deviceTrips.push(calculateTrip(d, uncompletedTripRoute))
80
- }
81
- }
82
-
60
+ if (deviceRoute.filter(p => p.attributes.ignition && p.valid).length > 0) {
83
61
  const summary = []
84
- const trips = deviceTrips.filter(t => userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, deviceRoute))
85
62
 
86
- const dates = getDates(from, to)
63
+ const convertFrom = new Date(convertFromUTC(from, userData.user.attributes.timezone))
64
+ const convertTo = new Date(convertFromUTC(to, userData.user.attributes.timezone))
65
+
66
+ const dates = getDates(convertFrom, convertTo)
67
+
87
68
  for (const date of dates) {
88
69
  const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
89
70
  const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
90
71
 
91
- const tripsByDay = trips.filter(t => (new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay))
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())
92
75
 
93
- if (!userData.allWeek && userData.weekDays && userData.dayHours) {
94
- if (weekDaySelected(date, userData.weekDays)) {
95
- const trips = tripsByDay.filter(t => isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, deviceRoute))
96
- summary.push(createSummary(date, d, trips))
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
97
82
  }
98
- } else {
99
- summary.push(createSummary(date, d, tripsByDay))
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
100
96
  }
97
+
98
+ summary.push(dayData)
101
99
  }
102
100
 
103
- const deviceData = {
104
- device: d,
105
- summary,
106
- totalEngineHours: deviceRoute[deviceRoute.length - 1].attributes.hours
101
+ if (summary.length) {
102
+ const deviceData = {
103
+ device: d,
104
+ summary
105
+ }
106
+ devicesResult.push(deviceData)
107
107
  }
108
- devicesResult.push(deviceData)
109
108
  }
110
109
  })
111
110
 
@@ -121,7 +120,8 @@ async function exportMachinesReportToPDF (userData, reportData) {
121
120
  const headers = [translations.report.date,
122
121
  translations.report.start,
123
122
  translations.report.end,
124
- translations.report.engineHours]
123
+ translations.report.engineHours,
124
+ translations.report.totalEngineHours]
125
125
 
126
126
  const weekDays = [
127
127
  translations.report.sunday,
@@ -137,7 +137,7 @@ async function exportMachinesReportToPDF (userData, reportData) {
137
137
  const name = userData.byDriver ? d.driver.name : d.device.name
138
138
  const group = userData.byDriver ? userData.groups.find(g => g.drivers.includes(d.id)) : userData.groups.find(g => d.device.groupId === g.id)
139
139
 
140
- const space = index === 0 ? 13 : 0
140
+ const space = index === 0 ? 8 : 0
141
141
  if (index) {
142
142
  doc.addPage()
143
143
  }
@@ -147,7 +147,6 @@ async function exportMachinesReportToPDF (userData, reportData) {
147
147
  doc.setFontSize(11)
148
148
  doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
149
149
  doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
150
- doc.text('Total de horas de uso: ' + convertMS(d.totalEngineHours), 20, space + 30)
151
150
 
152
151
  const data = []
153
152
  d.summary.forEach(s => {
@@ -155,7 +154,8 @@ async function exportMachinesReportToPDF (userData, reportData) {
155
154
  new Date(s.date).toLocaleDateString() + ' - ' + weekDays[s.date.getDay()],
156
155
  s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
157
156
  s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
158
- convertMS(s.engineHours)
157
+ convertMS(s.engineHours),
158
+ s.totalHours ? convertMS(s.totalHours) : ''
159
159
  ]
160
160
 
161
161
  data.push(temp)
@@ -196,7 +196,8 @@ async function exportMachinesReportToExcel (userData, reportData) {
196
196
  { label: translations.report.date, value: 'date' },
197
197
  { label: translations.report.start, value: 'start' },
198
198
  { label: translations.report.end, value: 'end' },
199
- { label: translations.report.engineHours, value: 'engineHours' }]
199
+ { label: translations.report.engineHours, value: 'engineHours' },
200
+ { label: translations.report.totalEngineHours, value: 'totalEngineHours' }]
200
201
 
201
202
  let data = []
202
203
  reportData.devices.forEach(d => {
@@ -211,7 +212,8 @@ async function exportMachinesReportToExcel (userData, reportData) {
211
212
  weekday: weekDays[s.date.getDay()],
212
213
  start: s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
213
214
  end: s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
214
- engineHours: convertMS(s.engineHours)
215
+ engineHours: convertMS(s.engineHours),
216
+ totalEngineHours: s.totalHours ? convertMS(s.totalHours) : ''
215
217
  }
216
218
  }))
217
219
  })
package/src/util/utils.js CHANGED
@@ -32,7 +32,7 @@ function coordsDistance (lon1, lat1, lon2, lat2) {
32
32
  const from = turf.point([lon1, lat1])
33
33
  const to = turf.point([lon2, lat2])
34
34
 
35
- return distance.default(from, to, {units: 'meters'})
35
+ return distance.default(from, to, { units: 'meters' })
36
36
  }
37
37
 
38
38
  function convertPositionToFeature (position) {
@@ -54,9 +54,9 @@ function convertToFeature (geofence) {
54
54
  }
55
55
  }
56
56
  const str = geofence.area.substring('POLYGON(('.length, geofence.area.length - 2)
57
- const coord_list = str.split(',')
58
- for (const i in coord_list) {
59
- const coord = coord_list[i].trim().split(' ')
57
+ const coordList = str.split(',')
58
+ for (const i in coordList) {
59
+ const coord = coordList[i].trim().split(' ')
60
60
  feature.geometry.coordinates[0].push([parseFloat(coord[1]), parseFloat(coord[0])])
61
61
  }
62
62
  return feature
@@ -64,19 +64,19 @@ function convertToFeature (geofence) {
64
64
 
65
65
  if (geofence.area.startsWith('CIRCLE')) {
66
66
  const str = geofence.area.substring('CIRCLE ('.length, geofence.area.length - 1)
67
- const coord_list = str.split(',')
68
- const coord = coord_list[0].trim().split(' ')
67
+ const coordList = str.split(',')
68
+ const coord = coordList[0].trim().split(' ')
69
69
  const point = turf.point([parseFloat(coord[1]), parseFloat(coord[0])])
70
70
  point.properties.geofence = geofence
71
- point.properties.distance = coord_list[1].trim()
71
+ point.properties.distance = coordList[1].trim()
72
72
  console.log(point)
73
73
  return point
74
74
  }
75
75
 
76
76
  if (geofence.area.startsWith('LINESTRING')) {
77
77
  const str = geofence.area.substring('LINESTRING ('.length, geofence.area.length - 1)
78
- const coord_list = str.split(',')
79
- const coordinates = coord_list.map(c => {
78
+ const coordList = str.split(',')
79
+ const coordinates = coordList.map(c => {
80
80
  const coord = c.trim().split(' ')
81
81
  return [parseFloat(coord[1]), parseFloat(coord[0])]
82
82
  })
@@ -95,7 +95,7 @@ function convertToFeature (geofence) {
95
95
 
96
96
  function getDates (startDate, endDate) {
97
97
  const dates = []
98
- let currentDate = new Date(startDate.toISOString().split('T')[0] + ' 12:00:00.000 PM')
98
+ let currentDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate(), 12, 0, 0, 0)
99
99
  const addDays = function (days) {
100
100
  const date = new Date(this.valueOf())
101
101
  date.setDate(date.getDate() + days)