fleetmap-reports 1.0.580 → 1.0.582

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,10 +1,10 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "1.0.580",
3
+ "version": "1.0.582",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
7
- "test": "email=$email & password=$password & jest"
7
+ "test": "jest"
8
8
  },
9
9
  "keywords": [],
10
10
  "author": "",
@@ -20,15 +20,6 @@ function fuelSignalInverter (device) {
20
20
  return device.attributes.fuel_low_threshold <= device.attributes.fuel_high_threshold ? 1 : -1
21
21
  }
22
22
 
23
- function calculateFuelDiff (a, b, device) {
24
- const diff = (b - a) * fuelSignalInverter(device)
25
- if (device.attributes.xpert) {
26
- return diff
27
- } else {
28
- return (diff * 100) / Math.abs(device.attributes.fuel_low_threshold - device.attributes.fuel_high_threshold)
29
- }
30
- }
31
-
32
23
  function calculateXpertSpentFuel (date, positions) {
33
24
  const begin = moment(date, 'YYYY-MM-DD').startOf('day')
34
25
  const end = moment(date, 'YYYY-MM-DD').endOf('day')
@@ -66,7 +57,6 @@ function calculateSpentFuel (value, device) {
66
57
 
67
58
  exports.sliceArray = sliceArray
68
59
  exports.deviceWithFuelInfo = deviceWithFuelInfo
69
- exports.calculateFuelDiff = calculateFuelDiff
70
60
  exports.calculateSpentFuel = calculateSpentFuel
71
61
  exports.calculateXpertSpentFuel = calculateXpertSpentFuel
72
62
  exports.calculateXpertDistance = calculateXpertDistance
@@ -5,6 +5,7 @@ const distance = require('@turf/distance')
5
5
  const { getTranslations } = require('./util/utils')
6
6
  const odoo = require('./util/odoo')
7
7
  const traccarHelper = require('./util/traccar')
8
+ const { calculateFuelDiff, getRefuelingLiters } = require('./util/fuel')
8
9
 
9
10
  const positionsToCheck = 15
10
11
 
@@ -158,10 +159,10 @@ async function calculateRefuelingPositions (userData, d, data) {
158
159
  const after = (array.length > index + 5 ? array.slice(index, index + 1) : array.slice(index)).filter(b => b.attributes.ignition && b.attributes.fuel)
159
160
  const newValue = Math.round(after.reduce((a, b) => a + b.attributes.fuel, 0) / after.length)
160
161
 
161
- const diff = automaticReports.calculateFuelDiff(currentValue, newValue, d)
162
+ const diff = calculateFuelDiff(currentValue, newValue, d, element)
162
163
  if (diff > 20) {
163
164
  // New refueling detected
164
- const value = Math.round(diff * d.attributes.fuel_tank_capacity / 100)
165
+ const value = getRefuelingLiters(d, element, diff)
165
166
  refuelingPositions.push({ position: element, date: element.fixTime, diff: value })
166
167
  lastRefuelingIndex = index
167
168
  positionsChecked = positionsToCheck + 1 // to reset values
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable no-undef */
2
- const { getReports } = require('./tests')
2
+ const { getReports } = require('./index')
3
3
  const assert = require('assert')
4
4
 
5
5
  const deviceId = 122575
@@ -15,8 +15,12 @@ const axios = require('axios').create({ ...traccarConfig.baseOptions, baseURL: t
15
15
  axiosCookieJarSupport(axios)
16
16
 
17
17
  const getReports = async () => {
18
- await new SessionApi(traccarConfig, null, axios).sessionPost(process.env.email, process.env.password)
19
- return new Index(traccarConfig, axios, cookieJar)
18
+ try {
19
+ await new SessionApi(traccarConfig, null, axios).sessionPost(process.env.email, process.env.password)
20
+ return new Index(traccarConfig, axios, cookieJar)
21
+ } catch (e) {
22
+ console.error(e)
23
+ }
20
24
  }
21
25
 
22
26
  exports.getReports = getReports
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable no-undef */
2
- const { getReports } = require('./tests')
2
+ const { getReports } = require('./index')
3
3
  const assert = require('assert')
4
- const utils = require('./util/utils')
4
+ const utils = require('../util/utils')
5
5
 
6
6
  async function getSpeedingReport (report, userData) {
7
7
  const data = await report.speedingReport(
@@ -300,7 +300,7 @@ describe('Test_Reports', function () {
300
300
  console.log('Start')
301
301
  const reports = await getReports()
302
302
  const userData = await reports.getUserData()
303
- const r = await require('./util/traccar').getAllInOne(reports.traccar,
303
+ const r = await require('../util/traccar').getAllInOne(reports.traccar,
304
304
  new Date(2022, 1, 1, 0, 0, 0, 0),
305
305
  new Date(2022, 1, 4, 23, 59, 59, 0),
306
306
  userData.devices,
@@ -163,15 +163,10 @@ function processDevices (from, to, devices, data, userData, traccar) {
163
163
  addNearestPOIs(trips, userData)
164
164
 
165
165
  trips.forEach(trip => {
166
- if (deviceRoute[0].protocol === 'teltonika') {
166
+ if ((deviceRoute[0].protocol === 'teltonika' && deviceRoute[0].attributes.fuel) ||
167
+ automaticReports.deviceWithFuelInfo(d)) {
167
168
  trip.fuelConsumption = trip.spentFuel
168
- trip.avgFuelConsumption = trip.totalKms > 0 ? Math.round(trip.fuelConsumption * 100 / trip.totalKms) : 0
169
- } else if (automaticReports.deviceWithFuelInfo(d)) {
170
- trip.fuelConsumption = Math.round((trip.spentFuel * d.attributes.fuel_tank_capacity) / 100)
171
- trip.avgFuelConsumption = trip.totalKms > 0 ? Math.round(trip.fuelConsumption * 100 / trip.totalKms) : 0
172
- } else {
173
- trip.fuelConsumption = '-'
174
- trip.avgFuelConsumption = '-'
169
+ trip.avgFuelConsumption = trip.totalKms > 0 ? trip.fuelConsumption * 100 / trip.totalKms : 0
175
170
  }
176
171
 
177
172
  const stop = getStop(new Date(trip.startTime), stops)
@@ -201,9 +196,11 @@ function processDevices (from, to, devices, data, userData, traccar) {
201
196
  }, 0),
202
197
  trips
203
198
  }
204
- if (d.attributes.xpert) {
199
+
200
+ if (trips[0].fuelConsumption !== undefined) {
205
201
  deviceData.totalFuelConsumption = trips.reduce((a, b) => a + b.fuelConsumption, 0)
206
- deviceData.avgFuelConsumption = deviceData.totalDistance > 0 ? Math.round((deviceData.totalFuelConsumption * 100) / (deviceData.totalDistance / 1000)) : 0
202
+ deviceData.avgFuelConsumption = deviceData.totalDistance > 0 ? (deviceData.totalFuelConsumption * 100) / (deviceData.totalDistance / 1000) : 0
203
+ console.log(deviceData)
207
204
  }
208
205
 
209
206
  devicesResult.push(deviceData)
@@ -257,26 +254,28 @@ async function exportTripReportToPDF (userData, reportData) {
257
254
  translations.report.start,
258
255
  translations.report.end,
259
256
  translations.report.endAddress,
260
- translations.report.tripTime
257
+ translations.report.tripTime,
258
+ translations.report.idleTime,
259
+ translations.report.stopTime,
260
+ translations.report.distance,
261
+ translations.report.avgSpeed,
262
+ translations.report.maxSpeed
261
263
  ]
262
264
 
263
- if (userData.byDriver) {
264
- headers.push(translations.report.idleTime,
265
- translations.report.stopTime,
266
- translations.report.distance,
267
- translations.report.avgSpeed,
268
- translations.report.maxSpeed,
269
- translations.report.name)
270
- } else {
271
- headers.push(translations.report.idleTime,
272
- translations.report.stopTime,
273
- translations.report.distance,
274
- translations.report.avgSpeed,
275
- translations.report.maxSpeed,
276
- translations.report.driver)
277
- }
278
-
279
265
  if (tripsData) {
266
+ const withFuelData = tripsData.reduce((a, b) => a || b.totalFuelConsumption !== undefined)
267
+
268
+ if (withFuelData) {
269
+ headers.push(translations.report.consumption,
270
+ translations.report.avg_consumption)
271
+ }
272
+
273
+ if (userData.byDriver) {
274
+ headers.push(translations.report.name)
275
+ } else {
276
+ headers.push(translations.report.driver)
277
+ }
278
+
280
279
  // eslint-disable-next-line new-cap
281
280
  const doc = new jsPDF.jsPDF('l')
282
281
  await headerFromUser(doc, translations.report.titleTripReport, userData.user)
@@ -326,6 +325,10 @@ async function exportTripReportToPDF (userData, reportData) {
326
325
  Math.round(a.averageSpeed * 1.85200),
327
326
  Math.round(a.maxSpeed * 1.85200))
328
327
 
328
+ if (withFuelData) {
329
+ temp.push(a.fuelConsumption.toFixed(1), a.avgFuelConsumption.toFixed(1))
330
+ }
331
+
329
332
  if (userData.byDriver) {
330
333
  temp.push(a.deviceName)
331
334
  } else {
@@ -346,8 +349,21 @@ async function exportTripReportToPDF (userData, reportData) {
346
349
  getSumAvgSpeed(d.trips),
347
350
  getMaxSpeed(d.trips))
348
351
 
352
+ if (withFuelData) {
353
+ footValues.push(parseFloat(d.totalFuelConsumption).toFixed(1), parseFloat(d.avgFuelConsumption).toFixed(1))
354
+ }
355
+
349
356
  const style = getStyle(getUserPartner(userData.user))
350
- addTable(doc, headers, data, footValues, style, space + (userData.allWeek ? 40 : 45))
357
+ addTable(doc,
358
+ headers, data, footValues, style,
359
+ space + (userData.allWeek ? 40 : 45),
360
+ {
361
+ 7: { halign: 'right' },
362
+ 8: { halign: 'right' },
363
+ 9: { halign: 'right' },
364
+ 10: { halign: 'right' },
365
+ 11: { halign: 'right' }
366
+ })
351
367
  })
352
368
  return doc
353
369
  }
@@ -364,37 +380,37 @@ function exportTripReportToExcel (userData, reportData) {
364
380
  sheetName: translations.report.titleTripReport, // The name of the sheet
365
381
  fileName // The name of the spreadsheet
366
382
  }
367
- const headers = userData.byDriver
368
- ? [
369
- { label: translations.report.driver, value: 'name' },
370
- { label: translations.report.date, value: 'date' },
371
- { label: translations.report.start, value: 'start' },
372
- { label: translations.report.end, value: 'end' },
373
- { label: translations.report.endAddress, value: 'endAddress' },
374
- { label: translations.report.tripTime, value: 'tripTime' },
375
- { label: translations.report.idleTime, value: 'idleTime' },
376
- { label: translations.report.stopTime, value: 'stopTime' },
377
- { label: translations.report.distance, value: 'distance' },
378
- { label: translations.report.avgSpeed, value: 'averageSpeed' },
379
- { label: translations.report.maxSpeed, value: 'maxSpeed' },
380
- { label: translations.report.vehicle, value: 'subname' }
381
- ]
382
- : [
383
- { label: translations.report.vehicle, value: 'name' },
384
- { label: translations.report.date, value: 'date' },
385
- { label: translations.report.start, value: 'start' },
386
- { label: translations.report.end, value: 'end' },
387
- { label: translations.report.endAddress, value: 'endAddress' },
388
- { label: translations.report.tripTime, value: 'tripTime' },
389
- { label: translations.report.idleTime, value: 'idleTime' },
390
- { label: translations.report.stopTime, value: 'stopTime' },
391
- { label: translations.report.distance, value: 'distance' },
392
- { label: translations.report.avgSpeed, value: 'averageSpeed' },
393
- { label: translations.report.maxSpeed, value: 'maxSpeed' },
394
- { label: translations.report.driver, value: 'subname' }
395
- ]
383
+
384
+ const headers = [
385
+ { label: translations.report.date, value: 'date' },
386
+ { label: translations.report.start, value: 'start' },
387
+ { label: translations.report.end, value: 'end' },
388
+ { label: translations.report.endAddress, value: 'endAddress' },
389
+ { label: translations.report.tripTime, value: 'tripTime' },
390
+ { label: translations.report.idleTime, value: 'idleTime' },
391
+ { label: translations.report.stopTime, value: 'stopTime' },
392
+ { label: translations.report.distance, value: 'distance' },
393
+ { label: translations.report.avgSpeed, value: 'averageSpeed' },
394
+ { label: translations.report.maxSpeed, value: 'maxSpeed' }
395
+ ]
396
+
396
397
  let data = []
397
398
  if (tripsData) {
399
+ const withFuelData = tripsData.reduce((a, b) => a || b.totalFuelConsumption !== undefined)
400
+
401
+ if (withFuelData) {
402
+ headers.push({ label: translations.report.consumption, value: 'consumption' },
403
+ { label: translations.report.avg_consumption, value: 'avgConsumption' })
404
+ }
405
+
406
+ if (userData.byDriver) {
407
+ headers.unshift({ label: translations.report.driver, value: 'name' })
408
+ headers.push({ label: translations.report.vehicle, value: 'subname' })
409
+ } else {
410
+ headers.unshift({ label: translations.report.vehicle, value: 'name' })
411
+ headers.push({ label: translations.report.driver, value: 'subname' })
412
+ }
413
+
398
414
  tripsData.forEach(d => {
399
415
  data = data.concat([{}])
400
416
  data = data.concat(d.trips.map(a => {
@@ -410,6 +426,8 @@ function exportTripReportToExcel (userData, reportData) {
410
426
  distance: Number(a.totalKms.toFixed(2)),
411
427
  averageSpeed: Math.round(a.averageSpeed * 1.85200),
412
428
  maxSpeed: Math.round(a.maxSpeed * 1.85200),
429
+ consumption: a.fuelConsumption.toFixed(1),
430
+ avgConsumption: a.avgFuelConsumption.toFixed(1),
413
431
  subname: userData.byDriver ? a.deviceName : getDriverName(userData.drivers, a.driverUniqueId)
414
432
  }
415
433
  }))
@@ -421,7 +439,9 @@ function exportTripReportToExcel (userData, reportData) {
421
439
  stopTime: convertMS(d.trips.reduce((a, b) => a + b.stopDuration, 0)),
422
440
  distance: getSumTotalKms(userData.user, d.trips),
423
441
  averageSpeed: getSumAvgSpeed(d.trips),
424
- maxSpeed: getMaxSpeed(d.trips)
442
+ maxSpeed: getMaxSpeed(d.trips),
443
+ consumption: parseFloat(d.totalFuelConsumption).toFixed(1),
444
+ avgConsumption: parseFloat(d.avgFuelConsumption).toFixed(1)
425
445
  }])
426
446
  data = data.concat([{}])
427
447
  })
@@ -0,0 +1,26 @@
1
+ function calculateFuelDiff (a, b, device, p) {
2
+ if (p.protocol === 'teltonika') {
3
+ return b - a
4
+ } else {
5
+ const diff = (b - a) * fuelSignalInverter(device)
6
+ if (device.attributes.xpert) {
7
+ return diff
8
+ } else {
9
+ return (diff * 100) / Math.abs(device.attributes.fuel_low_threshold - device.attributes.fuel_high_threshold)
10
+ }
11
+ }
12
+ }
13
+
14
+ function fuelSignalInverter (device) {
15
+ return device.attributes.fuel_low_threshold <= device.attributes.fuel_high_threshold ? 1 : -1
16
+ }
17
+
18
+ function getRefuelingLiters (d, position, diff) {
19
+ if (position.protocol === 'teltonika' && !position.attributes.io89) {
20
+ return diff
21
+ }
22
+ return Math.round(diff * d.attributes.fuel_tank_capacity / 100)
23
+ }
24
+
25
+ exports.calculateFuelDiff = calculateFuelDiff
26
+ exports.getRefuelingLiters = getRefuelingLiters
@@ -25,7 +25,7 @@ async function headerFromUser (doc, title, user, orientation = 'l') {
25
25
  return header(doc, title, partner.host, getStyle(partner), orientation)
26
26
  }
27
27
 
28
- function addTable (doc, headers, data, footValues, style, startY) {
28
+ function addTable (doc, headers, data, footValues, style, startY, columnStyles = {}) {
29
29
  doc.autoTable({
30
30
  head: [headers],
31
31
  body: data,
@@ -45,7 +45,20 @@ function addTable (doc, headers, data, footValues, style, startY) {
45
45
  fillColor: style.pdfFooterColor,
46
46
  textColor: style.pdfFooterTextColor,
47
47
  fontSize: 9
48
+
49
+ },
50
+ didDrawCell: (data) => {
51
+ console.log(data)
52
+ data.table.foot.forEach((footRow) => {
53
+ const keys = Object.keys(columnStyles)
54
+ console.log(keys)
55
+ keys.forEach((colFormat, i) => {
56
+ console.log(footRow)
57
+ footRow.cells[i].styles.halign = 'right'
58
+ })
59
+ })
48
60
  },
61
+ columnStyles,
49
62
  startY
50
63
  })
51
64
  }