fleetmap-reports 2.0.76 → 2.0.78

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.
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "2.0.76",
3
+ "version": "2.0.78",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -136,12 +136,12 @@ function exportFuelConsumptionReportToExcel (userData, reportData) {
136
136
  return {
137
137
  name: d.device.name,
138
138
  date: r.date.toLocaleDateString(),
139
- kms: formatNumber(r.distance / 1000, lang),
140
- consumption: r.spentFuel > 0 ? formatNumber(r.spentFuel, lang) : 0,
141
- avg_consumption: formatNumber(lang === 'pt-BR' ? r.avgConsumption.byLiters : r.avgConsumption.byKms, lang),
139
+ kms: formatNumber(r.distance / 1000),
140
+ consumption: r.spentFuel > 0 ? formatNumber(r.spentFuel) : 0,
141
+ avg_consumption: formatNumber(lang === 'pt-BR' ? r.avgConsumption.byLiters : r.avgConsumption.byKms),
142
142
  duration: convertMS(r.duration),
143
- accumulated_kms: formatNumber((r.endOdometer / 100), lang),
144
- refueling: formatNumber(r.refueling, lang)
143
+ accumulated_kms: formatNumber((r.endOdometer / 100)),
144
+ refueling: formatNumber(r.refueling)
145
145
  }
146
146
  }))
147
147
  // Totals
@@ -203,12 +203,12 @@ async function exportFuelConsumptionReportToPDF (userData, reportData) {
203
203
  d.days.forEach(r =>
204
204
  data.push([
205
205
  r.date.toLocaleDateString(),
206
- formatNumber(r.distance / 1000, lang),
207
- r.spentFuel > 0 ? formatNumber(r.spentFuel, lang) : 0,
208
- formatNumber(lang === 'pt-BR' ? r.avgConsumption.byLiters : r.avgConsumption.byKms, lang),
206
+ formatNumber(r.distance / 1000),
207
+ r.spentFuel > 0 ? formatNumber(r.spentFuel) : 0,
208
+ formatNumber(lang === 'pt-BR' ? r.avgConsumption.byLiters : r.avgConsumption.byKms),
209
209
  convertMS(r.duration),
210
- formatNumber((r.endOdometer / 1000), lang),
211
- formatNumber(r.refueling, lang)
210
+ formatNumber((r.endOdometer / 1000)),
211
+ formatNumber(r.refueling)
212
212
  ]))
213
213
 
214
214
  const footValues = [
@@ -1,7 +1,11 @@
1
1
  const traccarHelper = require('../util/traccar')
2
2
  const { isInsideTimetable } = require('../util/trips')
3
- const { convertFromUTCDate } = require('../util/utils')
3
+ const { convertFromUTCDate, getTranslations, convertMS, convertToLocaleTimeString, isClientSide } = require('../util/utils')
4
4
  const automaticReports = require('../automaticReports')
5
+ const jsPDF = require('jspdf')
6
+ const { getStyle } = require('../reportStyle')
7
+ const { getUserPartner } = require('fleetmap-partners')
8
+ const { addTable, headerFromUser } = require('../util/pdfDocument')
5
9
 
6
10
  async function createDailyUseReport (from, to, userData, traccar) {
7
11
  const reportData = []
@@ -77,4 +81,72 @@ function processDeviceData (allInOne, d, userData) {
77
81
  }
78
82
  }
79
83
 
84
+ async function exportDailyUseReportToPDF (userData, reportData) {
85
+ const translations = getTranslations(userData)
86
+ const timezone = userData.user.attributes.timezone
87
+ const lang = userData.user.attributes.lang || (isClientSide() && navigator.language)
88
+
89
+ const headers = [
90
+ translations.report.vehicle,
91
+ 'Immatriculation',
92
+ 'Groupe',
93
+ 'Matin départ',
94
+ 'Matin arrêt',
95
+ 'Matin temps',
96
+ 'Déjeuner',
97
+ 'Après-midi départ',
98
+ 'Après-midi arrêt',
99
+ 'Après-midi temps',
100
+ 'Total temps',
101
+ 'Total conduit',
102
+ 'Total arrêts',
103
+ 'Total kms'
104
+ ]
105
+
106
+ const data = []
107
+ reportData.forEach(d => {
108
+ const row = [
109
+ d.device,
110
+ d.licensePlate,
111
+ d.group,
112
+ convertToLocaleTimeString(d.morningStart, lang, timezone, userData.user),
113
+ convertToLocaleTimeString(d.morningEnd, lang, timezone, userData.user),
114
+ convertMS(d.morningTime),
115
+ convertMS(d.lunch),
116
+ convertToLocaleTimeString(d.afternoonStart, lang, timezone, userData.user),
117
+ convertToLocaleTimeString(d.afternoonEnd, lang, timezone, userData.user),
118
+ convertMS(d.afternoonTime),
119
+ convertMS(d.totalTime),
120
+ convertMS(d.totalDrivingTime),
121
+ d.totalStops,
122
+ (d.totalDistance / 100).toFixed(1)
123
+ ]
124
+ data.push(row)
125
+ })
126
+
127
+ const doc = new jsPDF.jsPDF('l')
128
+ await headerFromUser(doc, 'Rapport de Performance', userData.user)
129
+ const style = getStyle(getUserPartner(userData.user))
130
+
131
+ const footValues = []
132
+
133
+ doc.setFontSize(11)
134
+ doc.text(new Date(userData.from).toLocaleString() + ' - ' + new Date(userData.to).toLocaleString(), 20, 33)
135
+
136
+ style.headerFontSize = 8
137
+ style.bodyFontSize = 7
138
+ const columnStyles = {}
139
+ for (let i = 3; i < 14; i++) {
140
+ columnStyles[i] = { halign: 'right' }
141
+ }
142
+ addTable(doc, headers, data, footValues, style, 40, columnStyles)
143
+ return doc
144
+ }
145
+
146
+ function exportDailyUseReportToExcel (userData, reportData) {
147
+
148
+ }
149
+
80
150
  exports.createDailyUseReport = createDailyUseReport
151
+ exports.exportDailyUseReportToPDF = exportDailyUseReportToPDF
152
+ exports.exportDailyUseReportToExcel = exportDailyUseReportToExcel
@@ -0,0 +1,69 @@
1
+ const automaticReports = require('../automaticReports')
2
+ const traccarHelper = require('../util/traccar')
3
+ const { getDates, getTranslations } = require('../util/utils')
4
+ const { getUserPartner } = require('fleetmap-partners')
5
+ const { getDataByDay } = require('../util/trips')
6
+
7
+ async function createDelayedStartReport (from, to, userData, traccar) {
8
+ const reportData = []
9
+
10
+ const devices = userData.devices
11
+
12
+ const sliced = automaticReports.sliceArray(devices, 5)
13
+ let deviceCount = 0
14
+ for (const slice of sliced) {
15
+ const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, false, true, true, false, deviceCount, devices.length)
16
+
17
+ for (const d of slice) {
18
+ const deviceData = await processDeviceData(from, to, allInOne, d, userData, traccar)
19
+ reportData.push(deviceData)
20
+ }
21
+
22
+ deviceCount = deviceCount + slice.length
23
+ }
24
+
25
+ return reportData
26
+ }
27
+
28
+ async function processDeviceData (from, to, allInOne, d, userData, traccar) {
29
+ const group = userData.groups.find(g => g.id === d.groupId)
30
+ const trips = allInOne.trips.filter(t => t.deviceId === d.id)
31
+ const translations = getTranslations(userData)
32
+
33
+ const weekDays = [
34
+ translations.report.sunday,
35
+ translations.report.monday,
36
+ translations.report.tuesday,
37
+ translations.report.wednesday,
38
+ translations.report.thursday,
39
+ translations.report.friday,
40
+ translations.report.saturday
41
+ ]
42
+
43
+ const dates = getDates(from, to, userData.user.attributes.timezone || getUserPartner(userData.user).timezone)
44
+
45
+ const deviceData = {
46
+
47
+ }
48
+
49
+ for (const date of dates) {
50
+ const { tripsByDay } = await getDataByDay(d, date, { trips }, userData, traccar)
51
+
52
+ if (tripsByDay.length > 0) {
53
+ const startTime = tripsByDay[0].startTime
54
+ const stopTime = tripsByDay[tripsByDay.length].endTime
55
+
56
+ const deviceDayData = {
57
+ device: d.name,
58
+ licensePlate: d.attributes.license_plate,
59
+ group: group ? group.name : '',
60
+ driver: '',
61
+ weekDay: weekDays[new Date(date).getDay()],
62
+ startDate: '',
63
+ startTime: '',
64
+ delayDescription: '',
65
+ stopTime: ''
66
+ }
67
+ }
68
+ }
69
+ }
@@ -0,0 +1,46 @@
1
+ async function createPassengerReport (from, to, userData, traccar) {
2
+ //const allDevices = await traccar.axios.get('/devices').then(d => d.data)
3
+ const devices = userData.devices
4
+ const drivers = await traccar.axios.get('/drivers').then(d => d.data)
5
+ const groups = await traccar.axios.get('/groups').then(d => d.data)
6
+ await Promise.all(groups.map(async g => {
7
+ const driversByGroup = await traccar.axios.get(`/drivers?groupId=${g.id}`).then(d => d.data)
8
+ driversByGroup.forEach(d => {
9
+ const _driver = drivers.find(a => a.id === d.id)
10
+ if (_driver) {
11
+ _driver.groupName = g.name
12
+ }
13
+ })
14
+ }))
15
+ const eventsUrl = `/reports/events?${devices.map(d => 'deviceId=' + d.id).join('&')
16
+ }&from=${from.toISOString()
17
+ }&to=${to.toISOString()
18
+ }`
19
+ const positionsUrl = `/reports/route?${devices.map(d => 'deviceId=' + d.id).join('&')
20
+ }&from=${from.toISOString()
21
+ }&to=${to.toISOString()
22
+ }`
23
+ const events = await traccar.axios.get(eventsUrl).then(d => d.data)
24
+ const positions = await traccar.axios.get(positionsUrl).then(d => d.data)
25
+ events.forEach(e => {
26
+ delete e.attributes
27
+ const driver = getDriver(e, positions, drivers)
28
+ const position = positions.find(p => p.id === e.positionId)
29
+ e.deviceName = devices.find(d => d.id === e.deviceId).name
30
+ e.groupName = driver.groupName || ''
31
+ e.fixtime = position && new Date(position.fixTime)
32
+ e.notes = driver && driver.attributes.notes
33
+ e.driverName = (driver && driver.name) || (position && position.attributes.driverUniqueId)
34
+ })
35
+ return events
36
+ }
37
+
38
+ function getDriver (event, positions, drivers) {
39
+ const p = positions.find(p => p.id === event.positionId)
40
+ if (!p) { return '' }
41
+ const d = drivers.find(d => d.uniqueId === p.attributes.driverUniqueId)
42
+ if (!d) { return '' }
43
+ return d
44
+ }
45
+
46
+ exports.createPassengerReport = createPassengerReport
@@ -0,0 +1,19 @@
1
+ const { getReports } = require('./index')
2
+ const assert = require('assert')
3
+ const { createDailyUseReport } = require('../partnerReports/dailyuse-report')
4
+
5
+ describe('dailyuse', function () {
6
+ // eslint-disable-next-line no-undef
7
+ it('dailyuse report', async () => {
8
+ const report = await getReports()
9
+ const userData = await report.getUserData()
10
+
11
+ const data = await createDailyUseReport(
12
+ new Date(Date.UTC(2023, 10, 1, 0, 0, 0, 0)),
13
+ new Date(Date.UTC(2023, 10, 6, 23, 59, 59, 0)),
14
+ userData,
15
+ report.traccar)
16
+ console.log(data)
17
+ assert.equal(data[0].consumption, 19.799999999999997)
18
+ }, 8000000)
19
+ })
@@ -20,8 +20,8 @@ describe('Test_Reports', function () {
20
20
  }, 30000)
21
21
 
22
22
  it('Fuel Consumption 1', async () => {
23
- console.log('trip by device')
24
- const report = await getReports('aghani@moviflotte.com', 'X8Tm<QT*')
23
+ console.log('Fuel Consumption')
24
+ const report = await getReports(process.env.email, process.env.password)
25
25
  const userData = await report.getUserData()
26
26
  userData.devices = userData.devices.filter(d => d.id === 133608)
27
27
 
@@ -0,0 +1,23 @@
1
+ const { getReports } = require('./index')
2
+ const assert = require('assert')
3
+ const { convertMS } = require('../util/utils')
4
+ const { createGPSJumpReport } = require('../partnerReports/gpsjump-report')
5
+
6
+ // eslint-disable-next-line no-undef
7
+ describe('gpsjump', function () {
8
+ // eslint-disable-next-line no-undef
9
+ it('gpsjump report', async () => {
10
+ const report = await getReports()
11
+ const userData = await report.getUserData()
12
+ userData.devices = userData.devices.filter(d => d.id === 22326)
13
+ const data = await createGPSJumpReport(
14
+ new Date(Date.UTC(2023, 6, 1, 0, 0, 0, 0)),
15
+ new Date(Date.UTC(2023, 6, 17, 23, 59, 59, 0)),
16
+ userData,
17
+ report.traccar)
18
+ console.log(data)
19
+ assert.equal(data[0].consumption, 19.799999999999997)
20
+ assert.equal(data[0].distance, 326.7657)
21
+ assert.equal(convertMS(data[1].time * 1000, false), '08:19')
22
+ }, 8000000)
23
+ })
@@ -1,6 +1,7 @@
1
1
  /* eslint-disable no-undef */
2
2
  const { getReports } = require('./index')
3
3
  const assert = require('assert')
4
+ const { convertFromLocal } = require('../util/utils')
4
5
 
5
6
  describe('Kms_Reports', function () {
6
7
  // eslint-disable-next-line no-undef
@@ -52,4 +53,22 @@ describe('Kms_Reports', function () {
52
53
  assert.equal(device.days.length, 30) // Total Kms
53
54
  assert.equal(device.days[29].kms, 136174.93281451002) // Total Kms
54
55
  }, 30000)
56
+ it('KmsReport by driver', async () => {
57
+ const reports = await getReports(process.env.USER_CTM_MOVIFLOTTE, process.env.PASS_CTM_MOVIFLOTTE)
58
+ const userData = await reports.getUserData()
59
+ userData.byDriver = true
60
+ console.log(userData.drivers)
61
+ const data = await reports.kmsReport(new Date(2024, 0, 1, 0, 0, 0), new Date(2024, 0, 31, 23, 59, 59),
62
+ userData)
63
+ assert.equal(data.length, 1)
64
+ }, 3000000)
65
+ it('KmsReport by timezone marrocos', async () => {
66
+ const reports = await getReports(process.env.email, process.env.password)
67
+ const userData = await reports.getUserData()
68
+ userData.devices = userData.devices.filter(d => d.id === 93497)
69
+ const data = await reports.kmsReport(convertFromLocal(new Date(2024, 2, 18, 0, 0, 0), userData.user.attributes.timezone), convertFromLocal(new Date(2024, 2, 18, 23, 59, 59), userData.user.attributes.timezone),
70
+ userData)
71
+ const device = data[0].devices.find(d => d.device.id === 93497)
72
+ assert.equal(device, 1)
73
+ }, 3000000)
55
74
  })
@@ -159,4 +159,21 @@ describe('zones', function () {
159
159
  console.log(first)
160
160
  assert.equal(first.days[6].distanceOut, 867.0554430738234)
161
161
  }, 4000000)
162
+
163
+ it('works with casais zones in columns 7', async () => {
164
+ const report = await getReports()
165
+ const userData = await report.getUserData()
166
+ userData.zonesByColumn = true
167
+ userData.devices = userData.devices.filter(d => d.id === 81166)
168
+ userData.geofences = userData.geofences.filter(g => g.name === 'baliza 1 - raio verde ' ||
169
+ g.name === 'baliza 2 - raio amarelo')
170
+ userData.onlyWithKmsOut = false
171
+ const result = await report.zoneReport(
172
+ new Date(Date.UTC(2023, 9, 9, 0, 0, 0, 0)),
173
+ new Date(Date.UTC(2023, 9, 31, 23, 59, 59, 0)),
174
+ userData)
175
+ const first = result[0].devices[0]
176
+ console.log(first)
177
+ assert.equal(first.days[17].distanceOut, 720.7985320478997)
178
+ }, 4000000)
162
179
  })
package/src/util/trips.js CHANGED
@@ -138,9 +138,15 @@ async function getDataByDay (device, date, data, userData, traccarInstance) {
138
138
  return { tripsByDay, routeByDay, refuelingPositionsByDay, zoneInOutDayData }
139
139
  }
140
140
 
141
+ function isValidAvgSpeed (kms, startTime, endTime) {
142
+ const totalTime = new Date(endTime).getTime() - new Date(startTime).getTime()
143
+ const avgSpeed = (kms / 1000) / (totalTime / (1000 * 60 * 60))
144
+ return avgSpeed < 200
145
+ }
146
+
141
147
  function getKms (trips) {
142
148
  const odometerKms = trips.length > 0 ? trips[trips.length - 1].endOdometer - trips[0].startOdometer : 0
143
- return odometerKms > 0 ? odometerKms : trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
149
+ return odometerKms > 0 && isValidAvgSpeed(odometerKms, trips[0].startTime, trips[trips.length - 1].endTime) ? odometerKms : trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
144
150
  }
145
151
 
146
152
  function isInsideTimetable (t, userData) {
package/src/util/utils.js CHANGED
@@ -41,8 +41,8 @@ function convertMS (duration, withSeconds) {
41
41
  return withSeconds ? hours + ':' + minutes + ':' + seconds : hours + ':' + minutes
42
42
  }
43
43
 
44
- exports.formatNumber = function formatNumber (value, lang) {
45
- return Intl.NumberFormat(lang, { maximumFractionDigits: 1, minimumFractionDigits: 1 }).format(value)
44
+ exports.formatNumber = function formatNumber (value) {
45
+ return Number(value.toFixed(1))
46
46
  }
47
47
 
48
48
  function coordsDistance (lon1, lat1, lon2, lat2) {