fleetmap-reports 1.0.464 → 1.0.465

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/lang/enGB.js CHANGED
@@ -188,6 +188,7 @@ module.exports = {
188
188
  titleActivityReport: 'Activity Report',
189
189
  titleKmsReport: 'Kms Report',
190
190
  titleIdleReport: 'Idle Report',
191
+ titleMachinesReport: 'Machines Report',
191
192
  from: 'From',
192
193
  to: 'to',
193
194
  headerStartAddress: 'Start address',
package/lang/esCL.js CHANGED
@@ -177,7 +177,8 @@ module.exports = {
177
177
  titleEventsReport: 'Informe de Eventos',
178
178
  titleActivityReport: 'Informe de Actividad',
179
179
  titleKmsReport: 'Informe de Kms',
180
- titleIdleReport: 'Relatórios de Ralenti',
180
+ titleIdleReport: 'Informe de Ralenti',
181
+ titleMachinesReport: 'Informe de Maquinas',
181
182
  from: 'De',
182
183
  to: 'a',
183
184
  headerStartAddress: 'Lugar de início',
package/lang/ptBR.js CHANGED
@@ -180,6 +180,7 @@ module.exports = {
180
180
  titleActivityReport: 'Relatório de Actividade',
181
181
  titleKmsReport: 'Relatórios de Kms',
182
182
  titleIdleReport: 'Relatórios de Ralenti',
183
+ titleMachinesReport: 'Relatório de Máquinas',
183
184
  from: 'De',
184
185
  to: 'a',
185
186
  headerStartAddress: 'Local de início',
package/lang/ptPT.js CHANGED
@@ -186,6 +186,7 @@ module.exports = {
186
186
  titleActivityReport: 'Relatório de Actividade',
187
187
  titleKmsReport: 'Relatórios de Kms',
188
188
  titleIdleReport: 'Relatórios de Ralenti',
189
+ titleMachinesReport: 'Relatório de Máquinas',
189
190
  from: 'De',
190
191
  to: 'a',
191
192
  headerStartAddress: 'Local de início',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "1.0.464",
3
+ "version": "1.0.465",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -162,5 +162,15 @@ function Reports (config, axios, cookieJar) {
162
162
  this.idleReportToExcel = (userData, reportData) => {
163
163
  return require('./idle-report').exportIdleReportToExcel(userData, reportData)
164
164
  }
165
+
166
+ this.machinesReport = (from, to, userData) => {
167
+ return require('./machines-report').createMachinesReport(from, to, userData, this.traccar)
168
+ }
169
+ this.machinesReportToPDF = (userData, reportData) => {
170
+ return require('./machines-report').exportMachinesReportToPDF(userData, reportData)
171
+ }
172
+ this.machinesReportToExcel = (userData, reportData) => {
173
+ return require('./machines-report').exportMachinesReportToExcel(userData, reportData)
174
+ }
165
175
  }
166
176
  module.exports = Reports
@@ -0,0 +1,228 @@
1
+ const { devicesToProcess } = require('./util/device')
2
+ const automaticReports = require('./automaticReports')
3
+ const traccarHelper = require('./util/traccar')
4
+ const { calculateTrip, isInsideTimetable, isPartialInsideTimetable } = require('./util/trips')
5
+ const { getDates, weekDaySelected, convertMS, getTranslations } = require('./util/utils')
6
+ const { getStyle } = require('./reportStyle')
7
+ const { getUserPartner } = require('fleetmap-partners')
8
+ const { addTable, headerFromUser } = require('./util/pdfDocument')
9
+ const jsPDF = require('jspdf')
10
+ require('jspdf-autotable')
11
+
12
+ const fileName = 'MachineReport'
13
+
14
+ async function createMachinesReport (from, to, userData, traccar) {
15
+ console.log('Create MachineReport')
16
+ const reportData = []
17
+
18
+ console.log('ByDevice')
19
+ const report = await createMachinesReportByDevice(from, to, userData, traccar)
20
+ reportData.push(report)
21
+
22
+ return reportData
23
+ }
24
+
25
+ async function createMachinesReportByDevice (from, to, userData, traccar) {
26
+ const devices = devicesToProcess(userData)
27
+
28
+ const allData = {
29
+ devices: [],
30
+ from,
31
+ to,
32
+ xpert: devices.filter(d => d.attributes.xpert).length > 0
33
+ }
34
+
35
+ const sliced = automaticReports.sliceArray(devices, 5)
36
+
37
+ let deviceCount = 0
38
+ for (const slice of sliced) {
39
+ const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, true, true, false, false, deviceCount, devices.length)
40
+ const tripsData = allInOne.trips
41
+ const routeData = allInOne.route
42
+
43
+ console.log('Trips:' + tripsData.length)
44
+
45
+ if (tripsData.length > 0) {
46
+ allData.devices.push(...processDevices(from, to, slice, {
47
+ trips: tripsData,
48
+ route: routeData
49
+ }, userData))
50
+ }
51
+ deviceCount = deviceCount + slice.length
52
+ }
53
+
54
+ return allData
55
+ }
56
+
57
+ function createSummary (date, d, tripsByDay) {
58
+ return {
59
+ date,
60
+ deviceId: d.id,
61
+ engineHours: tripsByDay.reduce((a, b) => a + b.duration, 0),
62
+ startTime: tripsByDay.length && tripsByDay[0].startTime,
63
+ endTime: tripsByDay.length && tripsByDay[tripsByDay.length - 1].endTime
64
+ }
65
+ }
66
+
67
+ function processDevices (from, to, devices, data, userData) {
68
+ const devicesResult = []
69
+
70
+ devices.forEach(d => {
71
+ const deviceTrips = data.trips.filter(t => t.deviceId === d.id)
72
+ const deviceRoute = data.route.filter(t => t.deviceId === d.id)
73
+
74
+ if (deviceRoute.length > 0) {
75
+ let uncompletedTripRoute = []
76
+ if (deviceRoute.length && deviceTrips.length && deviceRoute[deviceRoute.length - 1].attributes.ignition) {
77
+ uncompletedTripRoute = deviceRoute.filter(p => p.attributes.ignition && new Date(p.fixTime).getTime() > new Date(deviceTrips[deviceTrips.length - 1].endTime).getTime())
78
+ if (uncompletedTripRoute.length) {
79
+ deviceTrips.push(calculateTrip(d, uncompletedTripRoute))
80
+ }
81
+ }
82
+
83
+ const summary = []
84
+ const trips = deviceTrips.filter(t => userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, deviceRoute))
85
+
86
+ const dates = getDates(from, to)
87
+ for (const date of dates) {
88
+ const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
89
+ const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
90
+
91
+ const tripsByDay = trips.filter(t => (new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay))
92
+
93
+ if (!userData.allWeek && userData.weekDays && userData.dayHours) {
94
+ if (weekDaySelected(date, userData.weekDays)) {
95
+ const trips = tripsByDay.filter(t => isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, deviceRoute))
96
+ summary.push(createSummary(date, d, trips))
97
+ }
98
+ } else {
99
+ summary.push(createSummary(date, d, tripsByDay))
100
+ }
101
+ }
102
+
103
+ const deviceData = {
104
+ device: d,
105
+ summary,
106
+ totalEngineHours: deviceRoute[deviceRoute.length - 1].attributes.hours
107
+ }
108
+ devicesResult.push(deviceData)
109
+ }
110
+ })
111
+
112
+ return devicesResult
113
+ }
114
+
115
+ async function exportMachinesReportToPDF (userData, reportData) {
116
+ const doc = new jsPDF.jsPDF('l')
117
+ const translations = getTranslations(userData)
118
+ await headerFromUser(doc, translations.report.titleMachinesReport, userData.user)
119
+
120
+ console.log('Export to PDF')
121
+ const headers = [translations.report.date,
122
+ translations.report.start,
123
+ translations.report.end,
124
+ translations.report.engineHours]
125
+
126
+ const weekDays = [
127
+ translations.report.sunday,
128
+ translations.report.monday,
129
+ translations.report.tuesday,
130
+ translations.report.wednesday,
131
+ translations.report.thursday,
132
+ translations.report.friday,
133
+ translations.report.saturday
134
+ ]
135
+
136
+ reportData.devices.forEach(function (d, index) {
137
+ const name = userData.byDriver ? d.driver.name : d.device.name
138
+ const group = userData.byDriver ? userData.groups.find(g => g.drivers.includes(d.id)) : userData.groups.find(g => d.device.groupId === g.id)
139
+
140
+ const space = index === 0 ? 13 : 0
141
+ if (index) {
142
+ doc.addPage()
143
+ }
144
+
145
+ doc.setFontSize(13)
146
+ doc.text(name, 20, space + 20)
147
+ doc.setFontSize(11)
148
+ doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
149
+ doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, space + 25)
150
+ doc.text('Total de horas de uso: ' + convertMS(d.totalEngineHours), 20, space + 30)
151
+
152
+ const data = []
153
+ d.summary.forEach(s => {
154
+ const temp = [
155
+ new Date(s.date).toLocaleDateString() + ' - ' + weekDays[s.date.getDay()],
156
+ s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
157
+ s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
158
+ convertMS(s.engineHours)
159
+ ]
160
+
161
+ data.push(temp)
162
+ })
163
+
164
+ const footValues = ['',
165
+ '', '',
166
+ convertMS(d.summary.reduce((a, b) => a + b.engineHours, 0))]
167
+
168
+ const style = getStyle(getUserPartner(userData.user))
169
+ addTable(doc, headers, data, footValues, style, 40)
170
+ })
171
+
172
+ return doc
173
+ }
174
+
175
+ async function exportMachinesReportToExcel (userData, reportData) {
176
+ console.log('Export to Excel')
177
+ const translations = getTranslations(userData)
178
+
179
+ const settings = {
180
+ sheetName: translations.report.titleMachinesReport, // The name of the sheet
181
+ fileName // The name of the spreadsheet
182
+ }
183
+
184
+ const weekDays = [
185
+ translations.report.sunday,
186
+ translations.report.monday,
187
+ translations.report.tuesday,
188
+ translations.report.wednesday,
189
+ translations.report.thursday,
190
+ translations.report.friday,
191
+ translations.report.saturday
192
+ ]
193
+
194
+ const headers = [{ label: translations.report.vehicle, value: 'name' },
195
+ { label: translations.report.group, value: 'group' },
196
+ { label: translations.report.date, value: 'date' },
197
+ { label: translations.report.start, value: 'start' },
198
+ { label: translations.report.end, value: 'end' },
199
+ { label: translations.report.engineHours, value: 'engineHours' }]
200
+
201
+ let data = []
202
+ reportData.devices.forEach(d => {
203
+ const group = userData.groups.find(g => d.device.groupId === g.id)
204
+
205
+ data = data.concat([{}])
206
+ data = data.concat(d.summary.map(s => {
207
+ return {
208
+ name: d.device.name,
209
+ group: group ? group.name : '',
210
+ date: new Date(s.date).toLocaleDateString(),
211
+ weekday: weekDays[s.date.getDay()],
212
+ start: s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
213
+ end: s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
214
+ engineHours: convertMS(s.engineHours)
215
+ }
216
+ }))
217
+ })
218
+ console.log(data)
219
+ return {
220
+ headers,
221
+ data,
222
+ settings
223
+ }
224
+ }
225
+
226
+ exports.createMachinesReport = createMachinesReport
227
+ exports.exportMachinesReportToPDF = exportMachinesReportToPDF
228
+ exports.exportMachinesReportToExcel = exportMachinesReportToExcel