fleetmap-reports 2.0.170 → 2.0.172

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": "2.0.170",
3
+ "version": "2.0.172",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -1,7 +1,10 @@
1
1
  const automaticReports = require('./automaticReports')
2
2
  const traccarHelper = require('./util/traccar')
3
3
  const { devicesToProcess, deviceName } = require('./util/device')
4
- const { getTranslations, isClientSide, convertToLocaleString, convertMS, getDates } = require('./util/utils')
4
+ const {
5
+ getTranslations, isClientSide, convertToLocaleString, convertMS, getDates, convertToLocaleDateString,
6
+ convertToLocaleTimeString
7
+ } = require('./util/utils')
5
8
  const jsPDF = require('jspdf')
6
9
  const { headerFromUser, addTable } = require('./util/pdfDocument')
7
10
  const { getStyle } = require('./reportStyle')
@@ -47,7 +50,7 @@ function processDevices (from, to, devices, data, userData) {
47
50
  if (userData.groupByDay) {
48
51
  const dates = getDates(from, to, userData.user.attributes.timezone)
49
52
 
50
- const data = groupByDay(rows, dates, userData)
53
+ const data = userData.groupBySensor ? groupByDayAndSensor(rows, dates) : groupByDay(rows, dates)
51
54
 
52
55
  result.push({
53
56
  device: d,
@@ -145,6 +148,70 @@ function groupByDay (rows, dates) {
145
148
  return Object.entries(groupedByDay)
146
149
  }
147
150
 
151
+ function groupByDayAndSensor (rows, dates) {
152
+ const groupedData = {}
153
+
154
+ dates.forEach(dateStr => {
155
+ const dayStart = new Date(dateStr)
156
+ dayStart.setHours(0, 0, 0, 0)
157
+ const dayEnd = new Date(dayStart.getTime() + 86400000 - 1) // 23:59:59
158
+
159
+ if (!groupedData[dateStr]) {
160
+ groupedData[dateStr] = {
161
+ firstActivation: null,
162
+ lastActivation: null,
163
+ totalDuration: 0
164
+ }
165
+ }
166
+
167
+ const activePeriods = []
168
+
169
+ rows.forEach(row => {
170
+ if (row.value) {
171
+ const rowStart = new Date(row.startTime).getTime()
172
+ const rowEnd = new Date(row.endTime).getTime()
173
+
174
+ if (rowStart <= dayEnd.getTime() && rowEnd >= dayStart.getTime()) {
175
+ const periodStart = Math.max(rowStart, dayStart.getTime())
176
+ const periodEnd = Math.min(rowEnd, dayEnd.getTime())
177
+
178
+ activePeriods.push([periodStart, periodEnd]) // Store activation period
179
+ }
180
+ }
181
+ })
182
+
183
+ // Merge overlapping activation periods
184
+ activePeriods.sort((a, b) => a[0] - b[0]) // Sort by start time
185
+ const mergedPeriods = []
186
+ let currentPeriod = null
187
+
188
+ activePeriods.forEach(([start, end]) => {
189
+ if (!currentPeriod) {
190
+ currentPeriod = [start, end]
191
+ } else if (start <= currentPeriod[1]) {
192
+ // Extend the current period if overlapping
193
+ currentPeriod[1] = Math.max(currentPeriod[1], end)
194
+ } else {
195
+ // Store the finished period and start a new one
196
+ mergedPeriods.push(currentPeriod)
197
+ currentPeriod = [start, end]
198
+ }
199
+ })
200
+ if (currentPeriod) {
201
+ mergedPeriods.push(currentPeriod)
202
+ }
203
+
204
+ const totalDuration = mergedPeriods.reduce((sum, [start, end]) => sum + (end - start), 0)
205
+
206
+ if (mergedPeriods.length > 0) {
207
+ groupedData[dateStr].firstActivation = mergedPeriods[0][0]
208
+ groupedData[dateStr].lastActivation = mergedPeriods[mergedPeriods.length - 1][1]
209
+ groupedData[dateStr].totalDuration = totalDuration
210
+ }
211
+ })
212
+
213
+ return Object.entries(groupedData)
214
+ }
148
215
  async function exportToPDF (userData, reportData) {
149
216
  console.log('exportSensorReportToPDF')
150
217
 
@@ -153,14 +220,29 @@ async function exportToPDF (userData, reportData) {
153
220
  const lang = userData.user.attributes.lang || (isClientSide() && navigator.language)
154
221
  const reportDataRows = reportData.devices
155
222
 
156
- const headers = [
157
- 'Sensor',
158
- translations.report.start,
159
- translations.report.end,
160
- 'Estado',
161
- translations.report.duration,
162
- translations.report.endAddress
163
- ]
223
+ const headers = userData.groupByDay && userData.groupBySensor
224
+ ? [
225
+ translations.report.date,
226
+ translations.report.start,
227
+ translations.report.end,
228
+ translations.report.duration
229
+ ]
230
+ : (userData.groupByDay
231
+ ? [
232
+ translations.report.date,
233
+ 'Sensor',
234
+ translations.report.start,
235
+ translations.report.end,
236
+ translations.report.duration
237
+ ]
238
+ : [
239
+ 'Sensor',
240
+ translations.report.start,
241
+ translations.report.end,
242
+ 'Estado',
243
+ translations.report.duration,
244
+ translations.report.endAddress
245
+ ])
164
246
 
165
247
  if (reportDataRows) {
166
248
  // eslint-disable-next-line new-cap
@@ -185,25 +267,52 @@ async function exportToPDF (userData, reportData) {
185
267
  doc.text(group ? translations.report.group + ': ' + group.name : '', 200, space + 20)
186
268
  doc.text(convertToLocaleString(d.from, lang, timezone, userData.user) + ' - ' + convertToLocaleString(d.to, lang, timezone, userData.user), 20, space + 25)
187
269
 
188
- d.rows.forEach(row => {
189
- const temp = [
190
- row.name,
191
- convertToLocaleString(row.startTime, lang, timezone, userData.user),
192
- convertToLocaleString(row.endTime, lang, timezone, userData.user),
193
- row.valueDescription,
194
- convertMS(row.duration),
195
- row.endAddress
196
- ]
197
- data.push(temp)
198
- })
270
+ if (userData.groupByDay && userData.groupBySensor) {
271
+ d.rows.forEach(day => {
272
+ const temp = [
273
+ convertToLocaleDateString(day[0], lang, timezone, userData.user),
274
+ day[1].firstActivation ? convertToLocaleTimeString(day[1].firstActivation, lang, timezone, userData.user) : '-',
275
+ day[1].lastActivation ? convertToLocaleTimeString(day[1].lastActivation, lang, timezone, userData.user) : '-',
276
+ convertMS(day[1].totalDuration)
277
+ ]
278
+ data.push(temp)
279
+ })
280
+ } else if (userData.groupByDay) {
281
+ d.rows.forEach(day => {
282
+ const sensors = Object.entries(day[1])
283
+ sensors.forEach(sensor => {
284
+ const temp = [
285
+ convertToLocaleDateString(day[0], lang, timezone, userData.user),
286
+ sensor[0],
287
+ sensor[1].firstActivation ? convertToLocaleTimeString(sensor[1].firstActivation, lang, timezone, userData.user) : '-',
288
+ sensor[1].lastActivation ? convertToLocaleTimeString(sensor[1].lastActivation, lang, timezone, userData.user) : '-',
289
+ convertMS(sensor[1].totalDuration)
290
+ ]
291
+ data.push(temp)
292
+ })
293
+ })
294
+ } else {
295
+ d.rows.forEach(row => {
296
+ const temp = [
297
+ row.name,
298
+ convertToLocaleString(row.startTime, lang, timezone, userData.user),
299
+ convertToLocaleString(row.endTime, lang, timezone, userData.user),
300
+ row.valueDescription,
301
+ convertMS(row.duration),
302
+ row.endAddress
303
+ ]
304
+ data.push(temp)
305
+ })
306
+ }
307
+
308
+ const totalDuration = (userData.groupByDay && userData.groupBySensor) ? getTotalDuration(d.rows) : 0
199
309
 
200
- const footValues = []
310
+ const footValues = (userData.groupByDay && userData.groupBySensor) ? ['', '', '', totalDuration] : []
201
311
 
202
312
  const style = getStyle(getUserPartner(userData.user))
203
313
 
204
- addTable(doc, headers, data, footValues, style, 40, {
205
- 4: { halign: 'right' }
206
- })
314
+ addTable(doc, headers, data, footValues, style, 40,
315
+ (userData.groupByDay && userData.groupBySensor ? { 3: { halign: 'right' } } : { 4: { halign: 'right' } }))
207
316
 
208
317
  index++
209
318
  }
@@ -212,6 +321,10 @@ async function exportToPDF (userData, reportData) {
212
321
  }
213
322
  }
214
323
 
324
+ function getTotalDuration (rows) {
325
+ return convertMS(rows.reduce((a, b) => a + b[1].totalDuration, 0), false)
326
+ }
327
+
215
328
  function exportToExcel (userData, reportData) {}
216
329
 
217
330
  exports.exportToPDF = exportToPDF
package/src/util/trips.js CHANGED
@@ -217,7 +217,8 @@ function shouldRecalculate (trip, route) {
217
217
 
218
218
  function getTripTotalKms (trip, stop, route) {
219
219
  // we need to add stop kms to ensure that the total kms for the day, calculated with the odometer difference, will be equal to the sum of kms of all trips
220
- const stopKms = stop && (stop.endOdometer - stop.startOdometer)
220
+ const stopKms = stop &&
221
+ !route.find(p => p.attributes.putAccumulators) && (stop.endOdometer - stop.startOdometer)
221
222
  let distance = trip.distance + (stop && stopKms > 0 ? stopKms : 0)
222
223
  if (shouldRecalculate(trip, route)) {
223
224
  distance = 0