fleetmap-reports 2.0.101 → 2.0.103

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/lang/frFR.json CHANGED
@@ -192,6 +192,7 @@
192
192
  "titleIdleReport": "Rapports d'inactivité",
193
193
  "titleMachinesReport": "Rapport machine",
194
194
  "titleStopReport": "Arrêter le rapport",
195
+ "titleDelayedStartReport": "Rapport de Departs Tardifs",
195
196
  "from": "Dans",
196
197
  "to": "Le",
197
198
  "headerStartAddress": "lieu de départ",
@@ -263,6 +264,11 @@
263
264
  "saturday": "Samedi",
264
265
  "sunday": "Dimanche",
265
266
  "weekDay": "Jour de la semaine",
267
+ "day": "Jour",
268
+ "startDate": "Date de trajet",
269
+ "startTime": "Début de la conduite",
270
+ "delayDescription": "Catégorie de gravitá",
271
+ "endTime": "Heure d'arrêt",
266
272
  "unsubscribeTripReport": "Si vous ne souhaitez pas recevoir ces e-mails, veuillez accéder à %url% et supprimer le \"Rapport de voyage\" paramètres dans la section \"Rapports\".",
267
273
  "unsubscribeLocationsReport": "Si vous ne souhaitez pas recevoir ces e-mails, veuillez accéder à %url% et supprimer le \"Rapport de position\" paramètres dans la section \"Rapports\".",
268
274
  "unsubscribeSpeedingReport": "Si vous ne souhaitez pas recevoir ces e-mails, rendez-vous sur %url% et supprimez le \"rapport d'excès de vitesse\" paramètres dans la section \"Rapports\".",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "2.0.101",
3
+ "version": "2.0.103",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -213,5 +213,15 @@ function Reports (config, axios) {
213
213
  this.gpsJumpReportToExcel = (userData, reportData) => {
214
214
  return require('./partnerReports/gpsjump-report').exportGPSJumpReportToExcel(userData, reportData)
215
215
  }
216
+
217
+ this.delayedStartReport = (from, to, userData) => {
218
+ return require('./partnerReports/delayedstart-report').create(from, to, userData, this.traccar)
219
+ }
220
+ this.delayedStartReportToPDF = (userData, reportData) => {
221
+ return require('./partnerReports/delayedstart-report').exportToPDF(userData, reportData)
222
+ }
223
+ this.delayedStartReportToExcel = (userData, reportData) => {
224
+ return require('./partnerReports/delayedstart-report').exportToExcel(userData, reportData)
225
+ }
216
226
  }
217
227
  module.exports = Reports
@@ -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,209 @@
1
+ const automaticReports = require('../automaticReports')
2
+ const traccarHelper = require('../util/traccar')
3
+ const {
4
+ getDates, getTranslations, convertMS, isClientSide, convertToLocaleString, convertToLocaleDateString,
5
+ convertToLocaleTimeString
6
+ } = require('../util/utils')
7
+ const { getUserPartner } = require('fleetmap-partners')
8
+ const { getDataByDay } = require('../util/trips')
9
+ const jsPDF = require('jspdf')
10
+ const { headerFromUser, addTable } = require('../util/pdfDocument')
11
+ const { getStyle } = require('../reportStyle')
12
+
13
+ async function createDelayedStartReport (from, to, userData, traccar) {
14
+ const reportData = {
15
+ devices: [],
16
+ from,
17
+ to
18
+ }
19
+
20
+ const devices = userData.devices
21
+
22
+ const sliced = automaticReports.sliceArray(devices, 5)
23
+ let deviceCount = 0
24
+ for (const slice of sliced) {
25
+ const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, false, true, true, false, deviceCount, devices.length)
26
+
27
+ for (const d of slice) {
28
+ const deviceData = await processDeviceData(from, to, allInOne, d, userData, traccar)
29
+ reportData.devices.push(deviceData)
30
+ }
31
+
32
+ deviceCount = deviceCount + slice.length
33
+ }
34
+
35
+ reportData.devices = reportData.devices.flat()
36
+
37
+ return reportData
38
+ }
39
+
40
+ function getDelayDescription (startTime, activityStart, lowDelay, mediumDelay, highDelay) {
41
+ const startTimeDate = new Date(startTime)
42
+ const activityStartDate = new Date(startTime)
43
+ activityStartDate.setHours(activityStart.substring(0, 2), activityStart.substring(3, 5))
44
+
45
+ if (startTimeDate >= activityStartDate) {
46
+ const delay = startTimeDate.getTime() - activityStartDate.getTime()
47
+ if (delay < (lowDelay * 60 * 1000)) {
48
+ return '-'
49
+ } else if (delay < (mediumDelay * 60 * 1000)) {
50
+ return convertMS(delay, false) + ' - Low (+' + lowDelay + ')'
51
+ } else if (delay < (highDelay * 60 * 1000)) {
52
+ return convertMS(delay, false) + ' - Medium (+' + mediumDelay + ')'
53
+ } else {
54
+ return convertMS(delay, false) + ' - High (+' + highDelay + ')'
55
+ }
56
+ }
57
+
58
+ return ''
59
+ }
60
+
61
+ async function processDeviceData (from, to, allInOne, d, userData, traccar) {
62
+ const trips = allInOne.trips.filter(t => t.deviceId === d.id)
63
+ const translations = getTranslations(userData)
64
+
65
+ const weekDays = [
66
+ translations.report.sunday,
67
+ translations.report.monday,
68
+ translations.report.tuesday,
69
+ translations.report.wednesday,
70
+ translations.report.thursday,
71
+ translations.report.friday,
72
+ translations.report.saturday
73
+ ]
74
+
75
+ const dates = getDates(from, to, userData.user.attributes.timezone || getUserPartner(userData.user).timezone)
76
+
77
+ const days = []
78
+ for (const date of dates) {
79
+ const { tripsByDay } = await getDataByDay(d, date, { trips }, userData, traccar)
80
+
81
+ if (tripsByDay.length > 0) {
82
+ const startTime = tripsByDay[0].startTime
83
+ const endTime = tripsByDay[tripsByDay.length - 1].endTime
84
+
85
+ const deviceDayData = {
86
+ device: d.name,
87
+ groupId: d.groupId,
88
+ licensePlate: d.attributes.license_plate,
89
+ driver: '',
90
+ weekDay: weekDays[new Date(date).getDay()],
91
+ startTime,
92
+ delayDescription: getDelayDescription(startTime, userData.activityStart, userData.lowDelay, userData.mediumDelay, userData.highDelay),
93
+ endTime
94
+ }
95
+
96
+ days.push(deviceDayData)
97
+ }
98
+ }
99
+
100
+ return days
101
+ }
102
+
103
+ async function exportDelayedStartReportToPDF (userData, reportData) {
104
+ console.log('exportDelayedStartReportToPDF')
105
+
106
+ const timezone = userData.user.attributes.timezone
107
+ const translations = getTranslations(userData)
108
+ const lang = userData.user.attributes.lang || (isClientSide() && navigator.language)
109
+ const reportDataRows = userData.byDriver ? reportData.drivers : reportData.devices
110
+
111
+ const headers = [
112
+ translations.report.group,
113
+ translations.report.name,
114
+ translations.report.driver,
115
+ translations.report.day,
116
+ translations.report.startDate,
117
+ translations.report.startTime,
118
+ translations.report.delayDescription,
119
+ translations.report.endTime
120
+ ]
121
+
122
+ if (reportDataRows) {
123
+ // eslint-disable-next-line new-cap
124
+ const doc = new jsPDF.jsPDF('l')
125
+ await headerFromUser(doc, translations.report.titleDelayedStartReport, userData.user)
126
+
127
+ doc.setFontSize(13)
128
+ doc.text(convertToLocaleString(reportData.from, lang, timezone, userData.user) + ' - ' + convertToLocaleString(reportData.to, lang, timezone, userData.user), 20, 25)
129
+
130
+ const data = []
131
+ reportDataRows.forEach(function (row) {
132
+ const group = userData.groups.find(g => row.groupId === g.id)
133
+
134
+ const temp = [
135
+ group,
136
+ row.device,
137
+ row.driver,
138
+ row.weekDay,
139
+ convertToLocaleDateString(row.startTime, lang, timezone, userData.user),
140
+ convertToLocaleTimeString(row.startTime, lang, timezone, userData.user),
141
+ row.delayDescription,
142
+ convertToLocaleTimeString(row.endTime, lang, timezone, userData.user)
143
+ ]
144
+ data.push(temp)
145
+ })
146
+ const footValues = []
147
+
148
+ const style = getStyle(getUserPartner(userData.user))
149
+
150
+ addTable(doc, headers, data, footValues, style, 20)
151
+ return doc
152
+ }
153
+ }
154
+
155
+ const fileName = 'DelayedStartReport'
156
+ async function exportDelayedStartReportToExcel (userData, reportData) {
157
+ console.log('Export to Excel')
158
+
159
+ const translations = getTranslations(userData)
160
+ const timezone = userData.user.attributes.timezone
161
+ const lang = userData.user.attributes.lang
162
+
163
+ const reportDataRows = reportData.devices
164
+
165
+ const settings = {
166
+ sheetName: translations.report.titleTripReport, // The name of the sheet
167
+ fileName // The name of the spreadsheet
168
+ }
169
+
170
+ const headers = [
171
+ { label: translations.report.group, value: 'group' },
172
+ { label: translations.report.name, value: 'name' },
173
+ { label: translations.report.driver, value: 'driver' },
174
+ { label: translations.report.day, value: 'day' },
175
+ { label: translations.report.startDate, value: 'startDate' },
176
+ { label: translations.report.startTime, value: 'startTime' },
177
+ { label: translations.report.delayDescription, value: 'delayDescription' },
178
+ { label: translations.report.endTime, value: 'endTime' }
179
+ ]
180
+
181
+ let data = []
182
+ if (reportDataRows) {
183
+ reportDataRows.forEach(row => {
184
+ const group = userData.groups.find(g => row.groupId === g.id)
185
+
186
+ data = data.concat([{}])
187
+ data = data.concat({
188
+ group,
189
+ name: row.device,
190
+ driver: row.driver,
191
+ day: row.weekDay,
192
+ startDate: convertToLocaleDateString(row.startTime, lang, timezone, userData.user),
193
+ startTime: convertToLocaleTimeString(row.startTime, lang, timezone, userData.user),
194
+ delayDescription: row.delayDescription,
195
+ endTime: convertToLocaleTimeString(row.endTime, lang, timezone, userData.user)
196
+ })
197
+ })
198
+ console.log(data)
199
+ return {
200
+ headers,
201
+ data,
202
+ settings
203
+ }
204
+ }
205
+ }
206
+
207
+ exports.create = createDelayedStartReport
208
+ exports.exportToPDF = exportDelayedStartReportToPDF
209
+ exports.exportToExcel = exportDelayedStartReportToExcel
@@ -63,3 +63,6 @@ async function exportGPSJumpReportToExcel (userData, reportData) {
63
63
  exports.createGPSJumpReport = createGPSJumpReport
64
64
  exports.exportGPSJumpReportToPDF = exportGPSJumpReportToPDF
65
65
  exports.exportGPSJumpReportToExcel = exportGPSJumpReportToExcel
66
+ exports.create = createGPSJumpReport
67
+ exports.exportToPDF = exportGPSJumpReportToPDF
68
+ exports.exportToExcel = exportGPSJumpReportToExcel
@@ -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
+ })
@@ -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
+ })
@@ -65,10 +65,11 @@ describe('Kms_Reports', function () {
65
65
  it('KmsReport by timezone marrocos', async () => {
66
66
  const reports = await getReports(process.env.email, process.env.password)
67
67
  const userData = await reports.getUserData()
68
+ userData.groupByDay = true
68
69
  userData.devices = userData.devices.filter(d => d.id === 93497)
69
70
  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
71
  userData)
71
72
  const device = data[0].devices.find(d => d.device.id === 93497)
72
- assert.equal(device, 1)
73
+ assert.equal(device.days.length, 1)
73
74
  }, 3000000)
74
75
  })
@@ -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
  })