fleetmap-reports 1.0.440 → 1.0.443

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.440",
3
+ "version": "1.0.443",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.test.js CHANGED
@@ -15,10 +15,12 @@ async function getSpeedingReport (report, userData) {
15
15
 
16
16
  // eslint-disable-next-line no-undef
17
17
  describe('Test_Reports', function () {
18
+ this.timeout(500000)
18
19
  // eslint-disable-next-line no-undef
19
20
  it('Speeding by device', async () => {
20
21
  const report = await getReports()
21
22
  const userData = await report.getUserData()
23
+ userData.roadSpeedLimit = true
22
24
  const { device, totalDistance, totalEventTime } = await getSpeedingReport(report, userData)
23
25
  assert.equal(device.alerts.length, 15) // Total Alerts
24
26
  assert.equal(totalDistance, 19.59984677533689) // Total Kms
package/src/kms-report.js CHANGED
@@ -1,442 +1,445 @@
1
1
  const messages = require('../lang')
2
2
  const jsPDF = require('jspdf')
3
3
  require('jspdf-autotable')
4
- const {getStyle} = require("./reportStyle")
5
- const {headerFromUser,addTable} = require("./util/pdfDocument")
6
- const {getUserPartner} = require("fleetmap-partners")
7
- const traccar = require("./util/traccar")
8
- const {getDates, convertFromUTC} = require("./util/utils")
9
- const trips = require("./util/trips")
10
- const drivers = require("./util/driver")
11
- const { isInsideTimetable, isPartialInsideTimetable, calculateTrip} = require("./util/trips")
12
- const traccarHelper = require("./util/traccar");
13
- const {devicesToProcess} = require("./util/device");
4
+ const { getStyle } = require('./reportStyle')
5
+ const { headerFromUser, addTable } = require('./util/pdfDocument')
6
+ const { getUserPartner } = require('fleetmap-partners')
7
+ const traccar = require('./util/traccar')
8
+ const { getDates, convertFromUTC } = require('./util/utils')
9
+ const trips = require('./util/trips')
10
+ const drivers = require('./util/driver')
11
+ const { isInsideTimetable, isPartialInsideTimetable, calculateTrip } = require('./util/trips')
12
+ const traccarHelper = require('./util/traccar')
13
+ const { devicesToProcess } = require('./util/device')
14
14
 
15
15
  const fileName = 'KmsReport'
16
16
 
17
- async function createKmsReport(from, to, userData, traccarInstance) {
18
- console.log('Create KmsReport')
19
-
20
- console.log(from, to)
21
- const reportData = []
22
- if(userData.byDriver){
23
- console.log("ByDriver")
24
- const allData = await createKmsReportByDriver(from, to, userData, traccarInstance)
25
- reportData.push(allData)
26
- }
27
- else if(userData.byGroup){
28
- console.log("ByGroup")
29
- const allData = await createKmsReportByGroup(from, to, userData, traccarInstance)
30
- reportData.push(...allData)
31
- } else {
32
- console.log("ByDevice")
33
- const allData = await createKmsReportByDevice(from, to, userData, traccarInstance)
34
- reportData.push(allData)
35
- }
36
-
37
- return reportData
17
+ async function createKmsReport (from, to, userData, traccarInstance) {
18
+ console.log('Create KmsReport')
19
+
20
+ console.log(from, to)
21
+ const reportData = []
22
+ if (userData.byDriver) {
23
+ console.log('ByDriver')
24
+ const allData = await createKmsReportByDriver(from, to, userData, traccarInstance)
25
+ reportData.push(allData)
26
+ } else if (userData.byGroup) {
27
+ console.log('ByGroup')
28
+ const allData = await createKmsReportByGroup(from, to, userData, traccarInstance)
29
+ reportData.push(...allData)
30
+ } else {
31
+ console.log('ByDevice')
32
+ const allData = await createKmsReportByDevice(from, to, userData, traccarInstance)
33
+ reportData.push(allData)
34
+ }
35
+
36
+ return reportData
38
37
  }
39
38
 
40
- async function createKmsReportByDevice(from, to, userData, traccarInstance) {
41
- const devices = devicesToProcess(userData)
39
+ async function createKmsReportByDevice (from, to, userData, traccarInstance) {
40
+ const devices = devicesToProcess(userData)
42
41
 
43
- const allData = {
44
- devices: [],
45
- from: from,
46
- to: to
47
- }
42
+ const allData = {
43
+ devices: [],
44
+ from,
45
+ to
46
+ }
48
47
 
49
- const allInOne = await traccarHelper.getAllInOne(traccarInstance, from, to, devices, true, true, false, false)
50
- const tripsData = allInOne.trips
51
- const routeData = allInOne.route
48
+ const allInOne = await traccarHelper.getAllInOne(traccarInstance, from, to, devices, true, true, false, false)
49
+ const tripsData = allInOne.trips
50
+ const routeData = allInOne.route
52
51
 
53
- console.log('trips:' + tripsData.length)
52
+ console.log('trips:' + tripsData.length)
54
53
 
55
- if (tripsData.length > 0) {
56
- trips.checkTripsKms(traccarInstance, from, to, devices, {trips: tripsData, route: routeData})
57
- allData.devices = processDevices(from, to, devices, {trips: tripsData, route: routeData}, userData)
58
- }
54
+ if (tripsData.length > 0) {
55
+ trips.checkTripsKms(traccarInstance, from, to, devices, { trips: tripsData, route: routeData })
56
+ allData.devices = processDevices(from, to, devices, { trips: tripsData, route: routeData }, userData)
57
+ }
59
58
 
60
- return allData
59
+ return allData
61
60
  }
62
61
 
63
- async function createKmsReportByDriver(from, to, userData, traccarInstance) {
64
- const devices = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
65
- console.log(devices.length)
66
-
67
- if(!devices.length) {
68
- //empty report
69
- return { drivers: [] }
70
- }
62
+ async function createKmsReportByDriver (from, to, userData, traccarInstance) {
63
+ const devices = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
64
+ console.log(devices.length)
71
65
 
72
- const allInOne = await traccarHelper.getAllInOne(traccarInstance, from, to, devices, true, true, false, false)
73
- const tripsData = allInOne.trips
74
- const routeData = allInOne.route
66
+ if (!devices.length) {
67
+ // empty report
68
+ return { drivers: [] }
69
+ }
75
70
 
71
+ const allInOne = await traccarHelper.getAllInOne(traccarInstance, from, to, devices, true, true, false, false)
72
+ const tripsData = allInOne.trips
73
+ const routeData = allInOne.route
76
74
 
77
- return { drivers: processDrivers(from, to, userData, {trips: tripsData, route: routeData}) }
75
+ return { drivers: processDrivers(from, to, userData, { trips: tripsData, route: routeData }) }
78
76
  }
79
77
 
80
- async function createKmsReportByGroup(from, to, userData, traccarInstance) {
81
- const reportData = []
82
- for (const g of userData.groups) {
83
- const devices = userData.devices.filter(d => d.groupId === g.id)
84
- console.log(g.name + ' devices:' + devices.length)
85
- if(devices.length > 0) {
86
- const groupData = {
87
- devices: [],
88
- group: g,
89
- xpert: devices.filter(d => d.attributes.xpert).length > 0
90
- }
78
+ async function createKmsReportByGroup (from, to, userData, traccarInstance) {
79
+ const reportData = []
80
+ for (const g of userData.groups) {
81
+ const devices = userData.devices.filter(d => d.groupId === g.id)
82
+ console.log(g.name + ' devices:' + devices.length)
83
+ if (devices.length > 0) {
84
+ const groupData = {
85
+ devices: [],
86
+ group: g,
87
+ xpert: devices.filter(d => d.attributes.xpert).length > 0
88
+ }
91
89
 
92
- const route = userData.allWeek ? [] : await traccar.getRoute(traccarInstance, from, to, devices)
93
- const tripsData = await traccar.getTrips(traccarInstance, from, to, devices)
90
+ const route = userData.allWeek ? [] : await traccar.getRoute(traccarInstance, from, to, devices)
91
+ const tripsData = await traccar.getTrips(traccarInstance, from, to, devices)
94
92
 
95
- devices.sort((a, b) => (a.name > b.name) ? 1 : -1)
93
+ devices.sort((a, b) => (a.name > b.name) ? 1 : -1)
96
94
 
97
- console.log('trips:' + tripsData.length)
95
+ console.log('trips:' + tripsData.length)
98
96
 
99
- if (tripsData.length > 0) {
100
- trips.checkTripsKms(traccarInstance, from, to, devices, {trips: tripsData, route: route})
101
- groupData.devices = processDevices(from, to, devices, {trips: tripsData, route: route}, userData)
102
- }
97
+ if (tripsData.length > 0) {
98
+ trips.checkTripsKms(traccarInstance, from, to, devices, { trips: tripsData, route })
99
+ groupData.devices = processDevices(from, to, devices, { trips: tripsData, route }, userData)
100
+ }
103
101
 
104
- reportData.push(groupData)
105
- }
102
+ reportData.push(groupData)
106
103
  }
104
+ }
107
105
 
108
- const withoutGroupData = await createKmsReportByDevice(from, to, userData, traccarInstance)
109
- reportData.push(withoutGroupData)
106
+ const withoutGroupData = await createKmsReportByDevice(from, to, userData, traccarInstance)
107
+ reportData.push(withoutGroupData)
110
108
 
111
- return reportData
109
+ return reportData
112
110
  }
113
111
 
114
- function processDrivers(from, to, userData, data) {
115
- console.log(data)
116
- const driversResult = []
117
- userData.drivers.forEach(d => {
118
- const route = data.route.filter(p => p.attributes.driverUniqueId === d.uniqueId)
119
- const trips = data.trips.filter(t => t.driverUniqueId === d.uniqueId
120
- && (userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, route)))
121
-
122
- if (trips.length > 0) {
123
- const driverData = {
124
- driver: d
125
- }
126
- if(userData.groupByDay) {
127
- driverData.days = []
128
- const dates = getDates(from, to)
129
- for(const date of dates){
130
- const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
131
- const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
132
-
133
- const tripsByDay = trips.filter(t => (new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay))
134
-
135
- driverData.days.push({
136
- date: date,
137
- kms: tripsByDay.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
138
- })
139
- }
140
- } else {
141
- driverData.summary = {
142
- distance: trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
143
- }
144
- }
145
- driversResult.push(driverData)
112
+ function processDrivers (from, to, userData, data) {
113
+ console.log(data)
114
+ const driversResult = []
115
+ userData.drivers.forEach(d => {
116
+ const route = data.route.filter(p => p.attributes.driverUniqueId === d.uniqueId)
117
+ const trips = data.trips.filter(t => t.driverUniqueId === d.uniqueId &&
118
+ (userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, route)))
119
+
120
+ if (trips.length > 0) {
121
+ const driverData = {
122
+ driver: d
123
+ }
124
+ if (userData.groupByDay) {
125
+ driverData.days = []
126
+ const dates = getDates(from, to)
127
+ for (const date of dates) {
128
+ const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
129
+ const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
130
+
131
+ const tripsByDay = trips.filter(t => (new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay))
132
+
133
+ driverData.days.push({
134
+ date,
135
+ kms: tripsByDay.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
136
+ })
146
137
  }
147
- })
138
+ } else {
139
+ driverData.summary = {
140
+ distance: trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
141
+ }
142
+ }
143
+ driversResult.push(driverData)
144
+ }
145
+ })
148
146
 
149
- return driversResult
147
+ return driversResult
150
148
  }
151
149
 
152
- function processDevices(from, to, devices, data, userData) {
153
- const devicesResult = []
150
+ function processDevices (from, to, devices, data, userData) {
151
+ const devicesResult = []
154
152
 
155
- for (const d of devices) {
156
- const deviceRoute = data.route.filter(t => t.deviceId === d.id)
157
- const deviceTrips = data.trips.filter(t => t.deviceId === d.id)
153
+ for (const d of devices) {
154
+ const deviceRoute = data.route.filter(t => t.deviceId === d.id)
155
+ const deviceTrips = data.trips.filter(t => t.deviceId === d.id)
158
156
 
159
- let uncompletedTripRoute = []
160
- if(deviceRoute.length && deviceTrips.length && deviceRoute[deviceRoute.length-1].attributes.ignition) {
161
- uncompletedTripRoute = deviceRoute.filter(p => p.attributes.ignition && new Date(p.fixTime).getTime() > new Date(deviceTrips[deviceTrips.length-1].endTime).getTime())
162
- if(uncompletedTripRoute.length) {
163
- deviceTrips.push(calculateTrip(d, uncompletedTripRoute))
164
- }
165
- }
157
+ let uncompletedTripRoute = []
158
+ if (deviceRoute.length && deviceTrips.length && deviceRoute[deviceRoute.length - 1].attributes.ignition) {
159
+ uncompletedTripRoute = deviceRoute.filter(p => p.attributes.ignition && new Date(p.fixTime).getTime() > new Date(deviceTrips[deviceTrips.length - 1].endTime).getTime())
160
+ if (uncompletedTripRoute.length) {
161
+ deviceTrips.push(calculateTrip(d, uncompletedTripRoute))
162
+ }
163
+ }
166
164
 
167
- const trips = deviceTrips.filter(t => userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, deviceRoute))
168
-
169
- if (trips.length > 0) {
170
- if(userData.groupByDay) {
171
- trips.forEach(t => t.tripDay = convertFromUTC(t.startTime, userData.user.attributes.timezone).toISOString().split('T')[0] + ' 12:00 PM')
172
- const groupedTrips = trips.reduce(
173
- (entryMap, e) => entryMap.set(e.tripDay, [...entryMap.get(e.tripDay)||[], e]),
174
- new Map()
175
- )
176
-
177
- const allDates = getDates(from, to)
178
-
179
- const days = []
180
- let keys = Array.from(groupedTrips.keys())
181
- allDates.forEach(d => {
182
- const day = d.toISOString().split('T')[0] + ' 12:00 PM'
183
- if(!keys.includes(day)){
184
- groupedTrips.set(day, [])
185
- }
186
- })
187
-
188
- keys = Array.from(groupedTrips.keys())
189
- keys.sort((a,b)=>new Date(a).getTime()-new Date(b).getTime());
190
- keys.forEach(key => {
191
- const currentDate = new Date(key)
192
- const dayTrips = groupedTrips.get(key)
193
- const day = {
194
- date: currentDate,
195
- kms: 0
196
- }
197
-
198
- day.kms = dayTrips.reduce((a, b) => a + b.distance, 0)
199
-
200
- days.push(day)
201
- })
202
- devicesResult.push({
203
- device: d,
204
- days: days
205
- })
206
- } else {
207
- devicesResult.push({
208
- device: d,
209
- summary: {
210
- distance: trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0),
211
- }
212
- })
213
- }
214
- }
165
+ const trips = deviceTrips.filter(t => userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, deviceRoute))
166
+
167
+ if (trips.length > 0) {
168
+ if (userData.groupByDay) {
169
+ trips.forEach(t => {
170
+ t.tripDay = convertFromUTC(t.startTime, userData.user.attributes.timezone).toISOString().split('T')[0] + ' 12:00 PM'
171
+ })
172
+ const groupedTrips = trips.reduce(
173
+ (entryMap, e) => entryMap.set(e.tripDay, [...entryMap.get(e.tripDay) || [], e]),
174
+ new Map()
175
+ )
176
+
177
+ const allDates = getDates(from, to)
178
+
179
+ const days = []
180
+ let keys = Array.from(groupedTrips.keys())
181
+ allDates.forEach(d => {
182
+ const day = d.toISOString().split('T')[0] + ' 12:00 PM'
183
+ if (!keys.includes(day)) {
184
+ groupedTrips.set(day, [])
185
+ }
186
+ })
187
+
188
+ keys = Array.from(groupedTrips.keys())
189
+ keys.sort((a, b) => new Date(a).getTime() - new Date(b).getTime())
190
+ keys.forEach(key => {
191
+ const currentDate = new Date(key)
192
+ const dayTrips = groupedTrips.get(key)
193
+ const day = {
194
+ date: currentDate,
195
+ kms: 0
196
+ }
197
+
198
+ day.kms = dayTrips.reduce((a, b) => a + b.distance, 0)
199
+
200
+ days.push(day)
201
+ })
202
+ devicesResult.push({
203
+ device: d,
204
+ days
205
+ })
206
+ } else {
207
+ devicesResult.push({
208
+ device: d,
209
+ summary: {
210
+ distance: trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
211
+ }
212
+ })
213
+ }
215
214
  }
215
+ }
216
216
 
217
- return devicesResult
217
+ return devicesResult
218
218
  }
219
219
 
220
- async function exportKmsReportToPDF(userData, reportData) {
221
- const lang = userData.user.attributes.lang || (navigator && navigator.language)
222
- const translations = messages[lang] ? messages[lang] : messages['en-GB']
223
- const kmsData = userData.byDriver ? reportData.drivers : reportData.devices
224
-
225
- const weekDays = [
226
- translations.report.sunday,
227
- translations.report.monday,
228
- translations.report.tuesday,
229
- translations.report.wednesday,
230
- translations.report.thursday,
231
- translations.report.friday,
232
- translations.report.saturday
233
- ]
234
-
235
- const headers = []
236
- if(userData.groupByDay) {
237
- headers.push(translations.report.date,
238
- translations.report.distance)
239
- }
240
- else if(userData.byDriver) {
241
- headers.push(translations.report.driver,
242
- translations.report.group,
243
- translations.report.distance)
244
- } else {
245
- headers.push(translations.report.name,
246
- translations.settings.vehicle_model,
247
- translations.report.group,
248
- translations.report.distance)
249
- }
250
- if (kmsData) {
251
- const orientation = userData.groupByDay ? 'p' : 'l'
252
- const doc = new jsPDF.jsPDF(orientation)
253
- await headerFromUser(doc, translations.report.titleKmsReport, userData.user, orientation)
254
-
255
- if (userData.groupByDay) {
256
- kmsData.forEach(function (d, index) {
257
- const name = userData.byDriver ? d.driver.name : d.device.name
258
- const group = userData.byDriver ? userData.groups.find(g => g.drivers.includes(d.id)) : userData.groups.find(g => d.device.groupId === g.id)
259
-
260
- let space = index === 0 ? 8 : 0
261
- if(index){
262
- doc.addPage()
263
- }
264
-
265
- doc.setFontSize(13)
266
- doc.text(name, 20, space + 20)
267
- doc.setFontSize(11)
268
- doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
269
- doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
270
-
271
- if(!userData.allWeek && userData.weekDays) {
272
- doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '')
273
- + (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '')
274
- + (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '')
275
- + (userData.weekDays.thursday ? ' ' + translations.report.thursday + ',' : '')
276
- + (userData.weekDays.friday ? ' ' + translations.report.friday + ',' : '')
277
- + (userData.weekDays.saturday ? ' ' + translations.report.saturday + ',' : '')
278
- + (userData.weekDays.sunday ? ' ' + translations.report.sunday + ',' : '')
279
- + ' das '
280
- + userData.dayHours.startTime + ' - ' + userData.dayHours.endTime, 20, space + 30)
281
- }
282
-
283
- const data = []
284
- d.days.forEach(day => {
285
- const temp = [
286
- new Date(day.date).toLocaleDateString() + ' - ' + weekDays[day.date.getDay()],
287
- (day.kms/1000).toFixed(0)
288
- ]
289
-
290
- data.push(temp)
291
- })
292
-
293
- const footValues = []
294
- footValues.push(
295
- '',
296
- (d.days.reduce((a, b) => a + b.kms, 0)/1000).toFixed(0)
297
- )
298
-
299
- const style = getStyle(getUserPartner(userData.user))
300
- addTable(doc, headers, data, footValues, style, 40)
301
- })
302
- } else {
303
- const data = []
304
- if (userData.byDriver) {
305
- reportData.drivers.forEach(d => {
306
- const group = userData.groups.find(g => g.drivers.includes(d.driver.id))
307
-
308
- const temp = [
309
- d.driver.name,
310
- group ? group.name : '',
311
- Number((d.summary.distance / 1000).toFixed(0)),
312
- ]
313
- data.push(temp)
314
- })
315
- } else {
316
- reportData.devices.forEach(d => {
317
- const group = userData.groups.find(g => d.device.groupId === g.id)
318
-
319
- const temp = [
320
- d.device.name,
321
- d.device.model,
322
- group ? group.name : '',
323
- Number((d.summary.distance / 1000).toFixed(0))
324
- ]
325
- data.push(temp)
326
- })
327
- }
328
-
329
- const footValues = []
330
- if(userData.byDriver) {
331
- footValues.push(
332
- 'Total:' + reportData.drivers.length,
333
- '',
334
- (reportData.drivers.reduce((a, b) => a + b.summary.distance, 0)/1000).toFixed(0)
335
- )
336
- } else {
337
- footValues.push(
338
- 'Total:' + reportData.devices.length,
339
- '',
340
- '',
341
- (reportData.devices.reduce((a, b) => a + b.summary.distance, 0)/1000).toFixed(0),
342
- )
343
- }
344
-
345
- const style = getStyle(getUserPartner(userData.user))
346
-
347
- addTable(doc, headers, data, footValues, style, 35)
220
+ async function exportKmsReportToPDF (userData, reportData) {
221
+ const lang = userData.user.attributes.lang || (navigator && navigator.language)
222
+ const translations = messages[lang] ? messages[lang] : messages['en-GB']
223
+ const kmsData = userData.byDriver ? reportData.drivers : reportData.devices
224
+
225
+ const weekDays = [
226
+ translations.report.sunday,
227
+ translations.report.monday,
228
+ translations.report.tuesday,
229
+ translations.report.wednesday,
230
+ translations.report.thursday,
231
+ translations.report.friday,
232
+ translations.report.saturday
233
+ ]
234
+
235
+ const headers = []
236
+ if (userData.groupByDay) {
237
+ headers.push(translations.report.date,
238
+ translations.report.distance)
239
+ } else if (userData.byDriver) {
240
+ headers.push(translations.report.driver,
241
+ translations.report.group,
242
+ translations.report.distance)
243
+ } else {
244
+ headers.push(translations.report.name,
245
+ translations.settings.vehicle_model,
246
+ translations.report.group,
247
+ translations.report.distance)
248
+ }
249
+ if (kmsData) {
250
+ const orientation = userData.groupByDay ? 'p' : 'l'
251
+ const doc = new jsPDF.jsPDF(orientation)
252
+ await headerFromUser(doc, translations.report.titleKmsReport, userData.user, orientation)
253
+
254
+ if (userData.groupByDay) {
255
+ kmsData.forEach(function (d, index) {
256
+ const name = userData.byDriver ? d.driver.name : d.device.name
257
+ const group = userData.byDriver ? userData.groups.find(g => g.drivers.includes(d.id)) : userData.groups.find(g => d.device.groupId === g.id)
258
+
259
+ const space = index === 0 ? 8 : 0
260
+ if (index) {
261
+ doc.addPage()
348
262
  }
349
- return doc
350
- }
351
- }
352
263
 
353
- function exportKmsReportToExcel(userData, reportData) {
354
- console.log('Export to Excel')
355
-
356
- const lang = userData.user.attributes.lang || (navigator && navigator.language)
357
- const translations = messages[lang] ? messages[lang] : messages['en-GB']
358
-
359
- const weekDays = [
360
- translations.report.sunday,
361
- translations.report.monday,
362
- translations.report.tuesday,
363
- translations.report.wednesday,
364
- translations.report.thursday,
365
- translations.report.friday,
366
- translations.report.saturday
367
- ]
368
-
369
- const settings = {
370
- sheetName: translations.report.titleKmsReport, // The name of the sheet
371
- fileName: fileName // The name of the spreadsheet
372
- }
264
+ doc.setFontSize(13)
265
+ doc.text(name, 20, space + 20)
266
+ doc.setFontSize(11)
267
+ doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
268
+ doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
269
+
270
+ if (!userData.allWeek && userData.weekDays) {
271
+ doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '') +
272
+ (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '') +
273
+ (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '') +
274
+ (userData.weekDays.thursday ? ' ' + translations.report.thursday + ',' : '') +
275
+ (userData.weekDays.friday ? ' ' + translations.report.friday + ',' : '') +
276
+ (userData.weekDays.saturday ? ' ' + translations.report.saturday + ',' : '') +
277
+ (userData.weekDays.sunday ? ' ' + translations.report.sunday + ',' : '') +
278
+ ' das ' +
279
+ userData.dayHours.startTime + ' - ' + userData.dayHours.endTime, 20, space + 30)
280
+ }
373
281
 
374
- const headers = []
375
-
376
- if(userData.groupByDay) {
377
- headers.push(
378
- {label: translations.report.vehicle, value: 'name'},
379
- {label: translations.report.licensePlate, value: 'licenseplate'},
380
- {label: translations.report.group, value: 'group'},
381
- {label: translations.report.date, value: 'date'},
382
- {label: translations.report.weekDay, value: 'weekday'},
383
- {label: translations.report.distance, value: 'distance'})
384
- } else if(userData.byDriver) {
385
- headers.push(
386
- {label: translations.report.driver, value: 'name'},
387
- {label: translations.report.group, value: 'group'},
388
- {label: translations.report.distance, value: 'distance'})
389
- } else {
390
- headers.push(
391
- {label: translations.report.vehicle, value: 'name'},
392
- {label: translations.settings.vehicle_licenseplate, value: 'licenseplate'},
393
- {label: translations.report.group, value: 'group'},
394
- {label: translations.report.distance, value: 'distance'})
395
- }
282
+ const data = []
283
+ d.days.forEach(day => {
284
+ const temp = [
285
+ new Date(day.date).toLocaleDateString() + ' - ' + weekDays[day.date.getDay()],
286
+ (day.kms / 1000).toFixed(0)
287
+ ]
396
288
 
397
- let data = []
398
- const info = userData.byDriver ? reportData.drivers : reportData.devices
399
- info.forEach(d => {
400
- const group = userData.byDriver ? userData.groups.find(g => g.drivers.includes(d.driver.id)) :
401
- userData.groups.find(g => d.device.groupId === g.id)
402
-
403
- if (userData.groupByDay) {
404
- data = data.concat([{}])
405
- data = data.concat(d.days.map(a => {
406
- return {
407
- name: d.device.name,
408
- group: group ? group.name : '',
409
- licenseplate: d.device.attributes.license_plate,
410
- date: a.date,
411
- weekday: weekDays[a.date.getDay()],
412
- distance: Number((a.kms / 1000).toFixed(0))
413
- }
414
- }))
415
- } else {
416
- data = data.concat([{
417
- name: userData.byDriver ? d.driver.name : d.device.name,
418
- group: group ? group.name : '',
419
- licenseplate: userData.byDriver ? '' : d.device.attributes.license_plate,
420
- distance: Number((d.summary.distance / 1000).toFixed(0))
421
- }])
422
- }
423
- })
289
+ data.push(temp)
290
+ })
424
291
 
425
- // add total
426
- if (data.length) {
427
- data = data.concat({
428
- name: '',
429
- group: '',
430
- licenseplate: 'Total',
431
- distance: data.reduce((prev, curr) => curr.distance ? {distance: (prev.distance || 0) + curr.distance} : prev).distance
292
+ const footValues = []
293
+ footValues.push(
294
+ '',
295
+ (d.days.reduce((a, b) => a + b.kms, 0) / 1000).toFixed(0)
296
+ )
297
+
298
+ const style = getStyle(getUserPartner(userData.user))
299
+ addTable(doc, headers, data, footValues, style, 40)
300
+ })
301
+ } else {
302
+ doc.setFontSize(11)
303
+ doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, 33)
304
+
305
+ const data = []
306
+ if (userData.byDriver) {
307
+ reportData.drivers.forEach(d => {
308
+ const group = userData.groups.find(g => g.drivers.includes(d.driver.id))
309
+
310
+ const temp = [
311
+ d.driver.name,
312
+ group ? group.name : '',
313
+ Number((d.summary.distance / 1000).toFixed(0))
314
+ ]
315
+ data.push(temp)
316
+ })
317
+ } else {
318
+ reportData.devices.forEach(d => {
319
+ const group = userData.groups.find(g => d.device.groupId === g.id)
320
+
321
+ const temp = [
322
+ d.device.name,
323
+ d.device.model,
324
+ group ? group.name : '',
325
+ Number((d.summary.distance / 1000).toFixed(0))
326
+ ]
327
+ data.push(temp)
432
328
  })
329
+ }
330
+
331
+ const footValues = []
332
+ if (userData.byDriver) {
333
+ footValues.push(
334
+ 'Total:' + reportData.drivers.length,
335
+ '',
336
+ (reportData.drivers.reduce((a, b) => a + b.summary.distance, 0) / 1000).toFixed(0)
337
+ )
338
+ } else {
339
+ footValues.push(
340
+ 'Total:' + reportData.devices.length,
341
+ '',
342
+ '',
343
+ (reportData.devices.reduce((a, b) => a + b.summary.distance, 0) / 1000).toFixed(0)
344
+ )
345
+ }
346
+
347
+ const style = getStyle(getUserPartner(userData.user))
348
+
349
+ addTable(doc, headers, data, footValues, style, 35)
433
350
  }
351
+ return doc
352
+ }
353
+ }
434
354
 
435
- return {
436
- headers,
437
- data,
438
- settings
355
+ function exportKmsReportToExcel (userData, reportData) {
356
+ console.log('Export to Excel')
357
+
358
+ const lang = userData.user.attributes.lang || (navigator && navigator.language)
359
+ const translations = messages[lang] ? messages[lang] : messages['en-GB']
360
+
361
+ const weekDays = [
362
+ translations.report.sunday,
363
+ translations.report.monday,
364
+ translations.report.tuesday,
365
+ translations.report.wednesday,
366
+ translations.report.thursday,
367
+ translations.report.friday,
368
+ translations.report.saturday
369
+ ]
370
+
371
+ const settings = {
372
+ sheetName: translations.report.titleKmsReport, // The name of the sheet
373
+ fileName // The name of the spreadsheet
374
+ }
375
+
376
+ const headers = []
377
+
378
+ if (userData.groupByDay) {
379
+ headers.push(
380
+ { label: translations.report.vehicle, value: 'name' },
381
+ { label: translations.report.licensePlate, value: 'licenseplate' },
382
+ { label: translations.report.group, value: 'group' },
383
+ { label: translations.report.date, value: 'date' },
384
+ { label: translations.report.weekDay, value: 'weekday' },
385
+ { label: translations.report.distance, value: 'distance' })
386
+ } else if (userData.byDriver) {
387
+ headers.push(
388
+ { label: translations.report.driver, value: 'name' },
389
+ { label: translations.report.group, value: 'group' },
390
+ { label: translations.report.distance, value: 'distance' })
391
+ } else {
392
+ headers.push(
393
+ { label: translations.report.vehicle, value: 'name' },
394
+ { label: translations.settings.vehicle_licenseplate, value: 'licenseplate' },
395
+ { label: translations.report.group, value: 'group' },
396
+ { label: translations.report.distance, value: 'distance' })
397
+ }
398
+
399
+ let data = []
400
+ const info = userData.byDriver ? reportData.drivers : reportData.devices
401
+ info.forEach(d => {
402
+ const group = userData.byDriver
403
+ ? userData.groups.find(g => g.drivers.includes(d.driver.id))
404
+ : userData.groups.find(g => d.device.groupId === g.id)
405
+
406
+ if (userData.groupByDay) {
407
+ data = data.concat([{}])
408
+ data = data.concat(d.days.map(a => {
409
+ return {
410
+ name: d.device.name,
411
+ group: group ? group.name : '',
412
+ licenseplate: d.device.attributes.license_plate,
413
+ date: a.date,
414
+ weekday: weekDays[a.date.getDay()],
415
+ distance: Number((a.kms / 1000).toFixed(0))
416
+ }
417
+ }))
418
+ } else {
419
+ data = data.concat([{
420
+ name: userData.byDriver ? d.driver.name : d.device.name,
421
+ group: group ? group.name : '',
422
+ licenseplate: userData.byDriver ? '' : d.device.attributes.license_plate,
423
+ distance: Number((d.summary.distance / 1000).toFixed(0))
424
+ }])
439
425
  }
426
+ })
427
+
428
+ // add total
429
+ if (data.length) {
430
+ data = data.concat({
431
+ name: '',
432
+ group: '',
433
+ licenseplate: 'Total',
434
+ distance: data.reduce((prev, curr) => curr.distance ? { distance: (prev.distance || 0) + curr.distance } : prev).distance
435
+ })
436
+ }
437
+
438
+ return {
439
+ headers,
440
+ data,
441
+ settings
442
+ }
440
443
  }
441
444
 
442
445
  exports.createKmsReport = createKmsReport
@@ -76,7 +76,7 @@ async function createSpeedingReportByDevice (from, to, userData, traccarInstance
76
76
  xpert: devices.filter(d => d && d.attributes && d.attributes.xpert).length > 0
77
77
  }
78
78
 
79
- const sliced = automaticReports.sliceArray(devices, 20)
79
+ const sliced = automaticReports.sliceArray(devices, 10)
80
80
 
81
81
  let deviceCount = 0
82
82
  for (const slice of sliced) {
@@ -276,7 +276,7 @@ async function exportTripReportToPDF (userData, reportData) {
276
276
  tripsData.forEach(function (d, index) {
277
277
  const data = []
278
278
  const name = userData.byDriver ? d.driver.name : deviceName(d.device)
279
- const group = userData.byDriver ? userData.groups.find(g => g.drivers.includes(d.id)) : userData.groups.find(g => d.device.groupId === g.id)
279
+ const group = userData.byDriver ? userData.groups.find(g => g.drivers && g.drivers.includes(d.id)) : userData.groups.find(g => d.device.groupId === g.id)
280
280
 
281
281
  const space = index === 0 ? 8 : 0
282
282
  if (index) {
package/src/util/odoo.js CHANGED
@@ -1,14 +1,16 @@
1
1
 
2
- async function getOdooFuelServices(traccar, from, to) {
3
- const url = `/odoo/reports/refuelingServices?startDate=${from.toDateString()}&endDate=${to.toDateString()}`
2
+ async function getOdooFuelServices (traccar, from, to) {
3
+ const url = `/odoo/reports/refuelingServices?startDate=${from.toDateString()}&endDate=${to.toDateString()}`
4
4
 
5
- const {data} = await traccar.axios.get(url,{
6
- jar: traccar.cookieJar,
7
- withCredentials: true })
5
+ console.log('LOADING_MESSAGE:' + url)
6
+ const { data } = await traccar.axios.get(url, {
7
+ jar: traccar.cookieJar,
8
+ withCredentials: true
9
+ })
8
10
 
9
- console.log(data)
11
+ console.log(data)
10
12
 
11
- return data
13
+ return data
12
14
  }
13
15
 
14
16
  exports.getOdooFuelServices = getOdooFuelServices