fleetmap-reports 1.0.248 → 1.0.252

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.
@@ -6,7 +6,7 @@ require('jspdf-autotable')
6
6
  const {coordsDistance} = require("./util/utils")
7
7
  const drivers = require("./util/driver")
8
8
  const {getUserPartner} = require("fleetmap-partners")
9
- const {headerFromUser} = require("./util/pdfDocument")
9
+ const {headerFromUser, addTable} = require("./util/pdfDocument")
10
10
  const {getStyle} = require("./reportStyle")
11
11
  const traccar = require("./util/traccar")
12
12
 
@@ -116,7 +116,7 @@ function processDevices(from, to, devices, data, stopsData, userData) {
116
116
  const devicesResult = []
117
117
 
118
118
  devices.forEach(d => {
119
- const trips = data.filter(t => t.deviceId === d.id)
119
+ const trips = data.filter(t => t.deviceId === d.id && (userData.allWeek || isInsideTimetable(t, userData)))
120
120
  const stops = stopsData.filter(s => s.deviceId === d.id)
121
121
 
122
122
  trips.forEach(t => {
@@ -146,8 +146,8 @@ function processDevices(from, to, devices, data, stopsData, userData) {
146
146
  })
147
147
 
148
148
  if (trips.length > 0) {
149
- trips.forEach(function(trip, index) {
150
- const stop = stops.length+1 > index ? stops[index+1] : null
149
+ trips.forEach(trip => {
150
+ const stop = getStop(new Date(trip.startTime), stops)
151
151
 
152
152
  if (stop) {
153
153
  trip.stopDuration = (new Date(stop.endTime) - new Date(stop.startTime))
@@ -240,27 +240,38 @@ async function exportTripReportToPDF(userData, reportData) {
240
240
  }
241
241
 
242
242
  if (tripsData) {
243
- let first = true
244
243
  const doc = new jsPDF.jsPDF('l');
245
244
  await headerFromUser(doc, translations.report.titleTripReport, userData.user)
246
- for(const d of tripsData){
245
+
246
+ tripsData.forEach(function (d, index) {
247
247
  const data = []
248
248
  const name = userData.byDriver ? d.driver.name : deviceName(d.device)
249
249
  const group = userData.byDriver ? userData.groups.find(g => g.drivers.includes(d.id)) : userData.groups.find(g => d.device.groupId === g.id)
250
250
 
251
- let space = 0
252
- if(!first) {
251
+ let space = index === 0 ? 8 : 0
252
+ if(index){
253
253
  doc.addPage()
254
- } else {
255
- first = false
256
- space = 8
257
254
  }
255
+
258
256
  doc.setFontSize(13)
259
257
  doc.text(name, 20, space+20 )
260
258
  doc.setFontSize(11)
261
259
  doc.text(group ? translations.report.group + ': ' + group.name : '', 200, space+20 )
262
260
  doc.text(new Date(d.from).toLocaleString() + ' - ' + new Date(d.to).toLocaleString(), 20, space+25 )
263
- doc.text(translations.report.headerStartAddress + ': ' + d.trips[0].startAddress, 20, space+30 )
261
+ if(!userData.allWeek) {
262
+ doc.text((userData.weekDays.monday ? ' ' + translations.report.monday + ',' : '')
263
+ + (userData.weekDays.tuesday ? ' ' + translations.report.tuesday + ',' : '')
264
+ + (userData.weekDays.wednesday ? ' ' + translations.report.wednesday + ',' : '')
265
+ + (userData.weekDays.thursday ? ' ' + translations.report.thursday + ',' : '')
266
+ + (userData.weekDays.friday ? ' ' + translations.report.friday + ',' : '')
267
+ + (userData.weekDays.saturday ? ' ' + translations.report.saturday + ',' : '')
268
+ + (userData.weekDays.sunday ? ' ' + translations.report.sunday + ',' : '')
269
+ + ' das '
270
+ + userData.dayHours.startTime + ' - ' + userData.dayHours.endTime, 20, space + 30)
271
+ doc.text(translations.report.headerStartAddress + ': ' + d.trips[0].startAddress, 20, space+40 )
272
+ } else {
273
+ doc.text(translations.report.headerStartAddress + ': ' + d.trips[0].startAddress, 20, space+35 )
274
+ }
264
275
 
265
276
  d.trips.map(a => {
266
277
  const temp = [
@@ -306,28 +317,8 @@ async function exportTripReportToPDF(userData, reportData) {
306
317
  }
307
318
 
308
319
  const style = getStyle(getUserPartner(userData.user))
309
- doc.autoTable({
310
- head: [headers],
311
- body: data,
312
- foot: [footValues],
313
- showFoot: 'lastPage',
314
- headStyles: {
315
- fillColor: style.pdfHeaderColor,
316
- textColor: style.pdfHeaderTextColor,
317
- fontSize: 10
318
- },
319
- bodyStyles: {
320
- fillColor: style.pdfBodyColor,
321
- textColor: style.pdfBodyTextColor,
322
- fontSize: 8
323
- },
324
- footStyles: {
325
- fillColor: style.pdfFooterColor,
326
- textColor: style.pdfFooterTextColor,
327
- fontSize: 9
328
- },
329
- startY: space+35 })
330
- }
320
+ addTable(doc, headers, data, footValues, style, space + (userData.allWeek ? 40 : 45))
321
+ })
331
322
  return doc
332
323
  }
333
324
  }
@@ -451,6 +442,39 @@ function getMaxSpeed(data) {
451
442
  return Math.round(max * 1.85200)
452
443
  }
453
444
 
445
+ function getStop(tripEndDate, stops){
446
+ const stopList = stops.filter(s => new Date(s.startTime).getTime() >= tripEndDate.getTime())
447
+ if(stopList.length){
448
+ return stopList[0]
449
+ }
450
+
451
+ return null
452
+ }
453
+
454
+ function isInsideTimetable(t, userData){
455
+ const tripStart = new Date(t.startTime)
456
+ const tripEnd = new Date(t.endTime)
457
+
458
+ if((tripStart.getDay() === 0 && userData.weekDays.sunday) ||
459
+ (tripStart.getDay() === 1 && userData.weekDays.monday) ||
460
+ (tripStart.getDay() === 2 && userData.weekDays.tuesday) ||
461
+ (tripStart.getDay() === 3 && userData.weekDays.wednesday) ||
462
+ (tripStart.getDay() === 4 && userData.weekDays.thursday) ||
463
+ (tripStart.getDay() === 5 && userData.weekDays.friday) ||
464
+ (tripStart.getDay() === 6 && userData.weekDays.saturday)) {
465
+
466
+ const startDate = new Date(new Date(tripStart).toISOString().split('T')[0] + ' '+userData.dayHours.startTime)
467
+ const endDate = new Date(new Date(tripStart).toISOString().split('T')[0] + ' '+userData.dayHours.endTime)
468
+
469
+ console.log(tripStart, startDate, endDate)
470
+
471
+ //Trips inside time period
472
+ return tripStart.getTime() > startDate.getTime() && tripEnd.getTime() < endDate.getTime()
473
+ }
474
+
475
+ return false
476
+ }
477
+
454
478
  exports.createTripReport = createTripReport;
455
479
  exports.exportTripReportToPDF = exportTripReportToPDF;
456
480
  exports.exportTripReportToExcel = exportTripReportToExcel;
@@ -52,7 +52,21 @@ async function getEvents(traccar, from, to, devices, events){
52
52
  return result.map(r => r.data).flat()
53
53
  }
54
54
 
55
+ async function getSummary(traccar, from, to, devices){
56
+ const devicesToSlice = devices.slice()
57
+ const arrayOfArrays = automaticReports.sliceArray(devicesToSlice)
58
+ let requests = []
59
+ for (const a of arrayOfArrays) {
60
+ const promise = traccar.reports.reportsSummaryGet(from, to, a.map(d => d.id))
61
+ requests = requests.concat(promise)
62
+ }
63
+
64
+ const result = await Promise.all(requests)
65
+ return result.map(r => r.data).flat()
66
+ }
67
+
55
68
  exports.getRoute = getRoute
56
69
  exports.getTrips = getTrips
57
70
  exports.getStops = getStops
58
71
  exports.getEvents = getEvents
72
+ exports.getSummary = getSummary
package/src/util/utils.js CHANGED
@@ -29,6 +29,21 @@ function coordsDistance(lon1, lat1, lon2, lat2) {
29
29
  return (turf.distance(from, to, 'meters'))
30
30
  }
31
31
 
32
+ function getDates(startDate, endDate) {
33
+ const dates = []
34
+ let currentDate = new Date(startDate.toISOString().split('T')[0] + ' 12:00 PM')
35
+ const addDays = function (days) {
36
+ const date = new Date(this.valueOf())
37
+ date.setDate(date.getDate() + days)
38
+ return date
39
+ }
40
+ while (currentDate <= endDate) {
41
+ dates.push(currentDate)
42
+ currentDate = addDays.call(currentDate, 1)
43
+ }
44
+ return dates
45
+ }
46
+
32
47
  async function getImgFromUrl(hostname) {
33
48
  try {
34
49
  const img = new Image()
@@ -43,4 +58,5 @@ async function getImgFromUrl(hostname) {
43
58
  exports.getImgFromUrl = getImgFromUrl
44
59
  exports.convertMS = convertMS
45
60
  exports.coordsDistance = coordsDistance
61
+ exports.getDates = getDates
46
62