fleetmap-reports 1.0.464 → 1.0.467

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',
@@ -225,6 +226,7 @@ module.exports = {
225
226
  endOdometer: 'End (Km)',
226
227
  spentFuel: 'Spent Fuel',
227
228
  engineHours: 'Use (H:m)',
229
+ totalEngineHours: 'Total Use (H:m)',
228
230
  driverHours: 'Driving Time (H:m)',
229
231
  refueling: 'Refueling',
230
232
  fueldrop: 'Fuel Drop (L)',
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',
@@ -214,6 +215,7 @@ module.exports = {
214
215
  endOdometer: 'End (Km)',
215
216
  spentFuel: 'Consumo',
216
217
  engineHours: 'Utilización (HH:mm)',
218
+ totalEngineHours: 'Utilización Total (H:m)',
217
219
  driverHours: 'Tiempo de Conducción (H:m)',
218
220
  refueling: 'Cargas',
219
221
  fueldrop: 'Combustível Perdido (L)',
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',
@@ -217,6 +218,7 @@ module.exports = {
217
218
  endOdometer: 'Fim (Km)',
218
219
  spentFuel: 'Consumo (L)',
219
220
  engineHours: 'Utilização (H:m)',
221
+ totalEngineHours: 'Total Use (H:m)',
220
222
  driverHours: 'Tempo de Condução (H:m)',
221
223
  refueling: 'Abastecimentos',
222
224
  fueldrop: 'Combustível Perdido (L)',
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',
@@ -223,6 +224,7 @@ module.exports = {
223
224
  endOdometer: 'Fim (Km)',
224
225
  spentFuel: 'Consumo (L)',
225
226
  engineHours: 'Utilização (H:m)',
227
+ totalEngineHours: 'Total Use (H:m)',
226
228
  driverHours: 'Tempo de Condução (H:m)',
227
229
  refueling: 'Abastecimentos',
228
230
  fueldrop: 'Combustível Perdido (L)',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "1.0.464",
3
+ "version": "1.0.467",
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,230 @@
1
+ const { devicesToProcess } = require('./util/device')
2
+ const automaticReports = require('./automaticReports')
3
+ const traccarHelper = require('./util/traccar')
4
+ const { getDates, convertMS, getTranslations, convertFromUTC } = require('./util/utils')
5
+ const { getStyle } = require('./reportStyle')
6
+ const { getUserPartner } = require('fleetmap-partners')
7
+ const { addTable, headerFromUser } = require('./util/pdfDocument')
8
+ const jsPDF = require('jspdf')
9
+ require('jspdf-autotable')
10
+
11
+ const fileName = 'MachineReport'
12
+
13
+ async function createMachinesReport (from, to, userData, traccar) {
14
+ console.log('Create MachineReport')
15
+ const reportData = []
16
+
17
+ console.log('ByDevice')
18
+ const report = await createMachinesReportByDevice(from, to, userData, traccar)
19
+ reportData.push(report)
20
+
21
+ return reportData
22
+ }
23
+
24
+ async function createMachinesReportByDevice (from, to, userData, traccar) {
25
+ const devices = devicesToProcess(userData)
26
+
27
+ const allData = {
28
+ devices: [],
29
+ from,
30
+ to,
31
+ xpert: devices.filter(d => d.attributes.xpert).length > 0
32
+ }
33
+
34
+ const sliced = automaticReports.sliceArray(devices, 5)
35
+
36
+ let deviceCount = 0
37
+ for (const slice of sliced) {
38
+ const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, true, false, false, false, deviceCount, devices.length)
39
+ const routeData = allInOne.route
40
+
41
+ console.log('Route:' + routeData.length)
42
+
43
+ if (routeData.length > 0) {
44
+ allData.devices.push(...processDevices(from, to, slice, {
45
+ route: routeData
46
+ }, userData))
47
+ }
48
+ deviceCount = deviceCount + slice.length
49
+ }
50
+
51
+ return allData
52
+ }
53
+
54
+ function processDevices (from, to, devices, data, userData) {
55
+ const devicesResult = []
56
+
57
+ devices.forEach(d => {
58
+ const deviceRoute = data.route.filter(t => t.deviceId === d.id)
59
+
60
+ if (deviceRoute.filter(p => p.attributes.ignition && p.valid).length > 0) {
61
+ const summary = []
62
+
63
+ const convertFrom = new Date(convertFromUTC(from, userData.user.attributes.timezone))
64
+ const convertTo = new Date(convertFromUTC(to, userData.user.attributes.timezone))
65
+
66
+ const dates = getDates(convertFrom, convertTo)
67
+
68
+ for (const date of dates) {
69
+ const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
70
+ const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
71
+
72
+ const routeByDay = deviceRoute.filter(p => (new Date(p.fixTime) > fromByDay && new Date(p.fixTime) < toByDay) && p.valid && p.attributes.hours !== undefined)
73
+
74
+ routeByDay.sort((a, b) => new Date(a.fixTime).getTime() - new Date(b.fixTime).getTime())
75
+
76
+ let firstOn
77
+ let lastOff
78
+
79
+ routeByDay.forEach(p => {
80
+ if (p.attributes.ignition || (firstOn && !p.attributes.ignition && lastOff.attributes.ignition)) {
81
+ lastOff = p
82
+ }
83
+
84
+ if (p.attributes.ignition && !firstOn) {
85
+ firstOn = p
86
+ }
87
+ })
88
+
89
+ const dayData = {
90
+ date,
91
+ deviceId: d.id,
92
+ engineHours: firstOn ? routeByDay[routeByDay.length - 1].attributes.hours - routeByDay[0].attributes.hours : 0,
93
+ totalHours: routeByDay.length ? routeByDay[routeByDay.length - 1].attributes.hours : undefined,
94
+ startTime: firstOn ? firstOn.fixTime : undefined,
95
+ endTime: lastOff ? lastOff.fixTime : undefined
96
+ }
97
+
98
+ summary.push(dayData)
99
+ }
100
+
101
+ if (summary.length) {
102
+ const deviceData = {
103
+ device: d,
104
+ summary
105
+ }
106
+ devicesResult.push(deviceData)
107
+ }
108
+ }
109
+ })
110
+
111
+ return devicesResult
112
+ }
113
+
114
+ async function exportMachinesReportToPDF (userData, reportData) {
115
+ const doc = new jsPDF.jsPDF('l')
116
+ const translations = getTranslations(userData)
117
+ await headerFromUser(doc, translations.report.titleMachinesReport, userData.user)
118
+
119
+ console.log('Export to PDF')
120
+ const headers = [translations.report.date,
121
+ translations.report.start,
122
+ translations.report.end,
123
+ translations.report.engineHours,
124
+ translations.report.totalEngineHours]
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 ? 8 : 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
+
151
+ const data = []
152
+ d.summary.forEach(s => {
153
+ const temp = [
154
+ new Date(s.date).toLocaleDateString() + ' - ' + weekDays[s.date.getDay()],
155
+ s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
156
+ s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
157
+ convertMS(s.engineHours),
158
+ s.totalHours ? convertMS(s.totalHours) : ''
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
+ { label: translations.report.totalEngineHours, value: 'totalEngineHours' }]
201
+
202
+ let data = []
203
+ reportData.devices.forEach(d => {
204
+ const group = userData.groups.find(g => d.device.groupId === g.id)
205
+
206
+ data = data.concat([{}])
207
+ data = data.concat(d.summary.map(s => {
208
+ return {
209
+ name: d.device.name,
210
+ group: group ? group.name : '',
211
+ date: new Date(s.date).toLocaleDateString(),
212
+ weekday: weekDays[s.date.getDay()],
213
+ start: s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
214
+ end: s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
215
+ engineHours: convertMS(s.engineHours),
216
+ totalEngineHours: s.totalHours ? convertMS(s.totalHours) : ''
217
+ }
218
+ }))
219
+ })
220
+ console.log(data)
221
+ return {
222
+ headers,
223
+ data,
224
+ settings
225
+ }
226
+ }
227
+
228
+ exports.createMachinesReport = createMachinesReport
229
+ exports.exportMachinesReportToPDF = exportMachinesReportToPDF
230
+ exports.exportMachinesReportToExcel = exportMachinesReportToExcel