fleetmap-reports 1.0.515 → 1.0.517

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.515",
3
+ "version": "1.0.517",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -93,11 +93,16 @@ async function createFuelConsumptionReport (from, to, userData, traccar) {
93
93
  function getOdooAvgConsumption (refuelingPositions, trips) {
94
94
  const odooTotalfuel = refuelingPositions.reduce((a, b) => a + b.diff, 0)
95
95
  const totalKms = trips.reduce((a, b) => a + b.distance, 0)
96
- return Math.round(odooTotalfuel * 100 / (totalKms / 1000))
96
+
97
+ if (totalKms === 0 || odooTotalfuel === 0) { return { byKms: 0, byLiters: 0 } }
98
+
99
+ return { byKms: Math.round(odooTotalfuel * 100 / (totalKms / 1000)), byLiters: Math.round((totalKms / 1000) / odooTotalfuel) }
97
100
  }
98
101
 
99
102
  function getCanAvgConsumption (userData, distance, spentFuel) {
100
- return distance > 0 && spentFuel > 0 ? Math.round(spentFuel * 100 / (distance / 1000)) : 0
103
+ if (distance === 0 || spentFuel === 0) { return { byKms: 0, byLiters: 0 } }
104
+
105
+ return { byKms: Math.round(spentFuel * 100 / (distance / 1000)), byLiters: Math.round((distance / 1000) / spentFuel) }
101
106
  }
102
107
 
103
108
  function calculateConsumption (userData, d, day, avgConsumption, data) {
@@ -105,7 +110,7 @@ function calculateConsumption (userData, d, day, avgConsumption, data) {
105
110
  return automaticReports.calculateXpertSpentFuel(day, data.route)
106
111
  }
107
112
  if (userData.withOdooServices) {
108
- return Math.round((avgConsumption * (data.trips.reduce((a, b) => a + b.distance, 0) / 1000)) / 100)
113
+ return Math.round((avgConsumption.byKms * (data.trips.reduce((a, b) => a + b.distance, 0) / 1000)) / 100)
109
114
  }
110
115
  return automaticReports.calculateSpentFuel(data.trips.reduce((a, b) => a + b.spentFuel, 0), d)
111
116
  }
package/src/index.test.js CHANGED
@@ -351,33 +351,18 @@ describe('Test_Reports', function () {
351
351
  it('Refueling Report', async () => {
352
352
  const report = await getReports()
353
353
  const userData = await report.getUserData()
354
-
354
+ userData.withOdooServices = true
355
355
  const data = await report.refuelingReport(
356
- new Date(Date.UTC(2022, 1, 2, 0, 0, 0, 0)),
357
- new Date(Date.UTC(2022, 1, 2, 23, 59, 59, 0)),
358
- userData)
356
+ new Date(Date.UTC(2022, 9, 1, 0, 0, 0, 0)),
357
+ new Date(Date.UTC(2022, 9, 31, 23, 59, 59, 0)),
358
+ userData)
359
359
 
360
360
  assert.equal(data.length, 1)
361
361
  console.log(data[0])
362
- const device = data[0].devices.find(d => d.device.id === 16245)
362
+ const device = data[0].devices.find(d => d.device.id === 22326)
363
363
  assert.equal(device.refuelings.length, 2)
364
364
  }, 30000)
365
365
  // eslint-disable-next-line no-undef
366
- it('Refueling2 Report', async () => {
367
- const report = await getReports()
368
- const userData = await report.getUserData()
369
-
370
- const data = await report.refuelingReport(new Date(2022, 1, 21, 0, 0, 0, 0),
371
- new Date(2022, 1, 25, 23, 59, 59, 0),
372
- userData)
373
-
374
- assert.equal(data.length, 1)
375
- console.log(data[0])
376
- const device = data[0].devices.find(d => d.device.id === 16245)
377
- console.log(device.refuelings)
378
- assert.equal(device.refuelings.length, 4)
379
- }, 30000)
380
- // eslint-disable-next-line no-undef
381
366
  it('FuelConsumption Report', async () => {
382
367
  const report = await getReports()
383
368
  const userData = await report.getUserData()
@@ -27,7 +27,7 @@ async function createRefuelingReport (from, to, userData, traccar) {
27
27
  const data = []
28
28
  for (const a of arrayOfArrays) {
29
29
  const allInOne = await traccarHelper.getAllInOne(traccar, from, to, a, true, false, false, false)
30
- data.push(allInOne.route)
30
+ data.push(...allInOne.route)
31
31
  }
32
32
 
33
33
  const fuelServicesData = []
@@ -51,14 +51,16 @@ async function createRefuelingReport (from, to, userData, traccar) {
51
51
  const refuelingPositions = await this.calculateRefuelingPositions(userData, d, { route: positions, fuelServices: deviceFuelServices })
52
52
 
53
53
  if (userData.drivers.length > 0) {
54
- refuelingPositions.forEach(p => {
55
- const geofence = findNearestPOI(p.position, userData.geofences)
56
- if (geofence) {
57
- p.position.geofenceName = geofence.name
58
- }
59
- const driver = userData.drivers.find(d => d.uniqueId === p.position.attributes.driverUniqueId)
60
- if (driver) {
61
- p.position.driverName = driver.name
54
+ refuelingPositions.forEach(r => {
55
+ if (r.position) {
56
+ const geofence = findNearestPOI(r.position, userData.geofences)
57
+ if (geofence) {
58
+ r.position.geofenceName = geofence.name
59
+ }
60
+ const driver = userData.drivers.find(d => d.uniqueId === r.position.attributes.driverUniqueId)
61
+ if (driver) {
62
+ r.position.driverName = driver.name
63
+ }
62
64
  }
63
65
  })
64
66
  }