fleetmap-reports 1.0.210 → 1.0.214

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.
@@ -1,11 +1,13 @@
1
1
  <?xml version="1.0" encoding="UTF-8"?>
2
2
  <project version="4">
3
3
  <component name="ChangeListManager">
4
- <list default="true" id="eb665c86-c893-4333-bd2a-721dc27980b9" name="Changes" comment="Fix getStyle in all reports">
4
+ <list default="true" id="eb665c86-c893-4333-bd2a-721dc27980b9" name="Changes" comment="try catch on pdf addImage">
5
5
  <change beforePath="$PROJECT_DIR$/package.json" beforeDir="false" afterPath="$PROJECT_DIR$/package.json" afterDir="false" />
6
+ <change beforePath="$PROJECT_DIR$/src/activity-report.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/activity-report.js" afterDir="false" />
7
+ <change beforePath="$PROJECT_DIR$/src/index.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/index.js" afterDir="false" />
6
8
  <change beforePath="$PROJECT_DIR$/src/index.test.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/index.test.js" afterDir="false" />
9
+ <change beforePath="$PROJECT_DIR$/src/kms-report.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/kms-report.js" afterDir="false" />
7
10
  <change beforePath="$PROJECT_DIR$/src/util/pdfDocument.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/util/pdfDocument.js" afterDir="false" />
8
- <change beforePath="$PROJECT_DIR$/src/util/utils.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/util/utils.js" afterDir="false" />
9
11
  </list>
10
12
  <option name="SHOW_DIALOG" value="false" />
11
13
  <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -138,7 +140,11 @@
138
140
  <workItem from="1636981640447" duration="4315000" />
139
141
  <workItem from="1637009407292" duration="1470000" />
140
142
  <workItem from="1637012863006" duration="5376000" />
141
- <workItem from="1637087797229" duration="4410000" />
143
+ <workItem from="1637087797229" duration="5701000" />
144
+ <workItem from="1637227826845" duration="1275000" />
145
+ <workItem from="1637441942952" duration="4543000" />
146
+ <workItem from="1637670815688" duration="2973000" />
147
+ <workItem from="1637674107929" duration="10975000" />
142
148
  </task>
143
149
  <task id="LOCAL-00001" summary="Fix getStyle">
144
150
  <created>1636983717385</created>
@@ -154,7 +160,14 @@
154
160
  <option name="project" value="LOCAL" />
155
161
  <updated>1637020728356</updated>
156
162
  </task>
157
- <option name="localTasksCounter" value="3" />
163
+ <task id="LOCAL-00003" summary="try catch on pdf addImage">
164
+ <created>1637247013210</created>
165
+ <option name="number" value="00003" />
166
+ <option name="presentableId" value="LOCAL-00003" />
167
+ <option name="project" value="LOCAL" />
168
+ <updated>1637247013211</updated>
169
+ </task>
170
+ <option name="localTasksCounter" value="4" />
158
171
  <servers />
159
172
  </component>
160
173
  <component name="TypeScriptGeneratedFilesManager">
@@ -175,6 +188,7 @@
175
188
  <component name="VcsManagerConfiguration">
176
189
  <MESSAGE value="Fix getStyle" />
177
190
  <MESSAGE value="Fix getStyle in all reports" />
178
- <option name="LAST_COMMIT_MESSAGE" value="Fix getStyle in all reports" />
191
+ <MESSAGE value="try catch on pdf addImage" />
192
+ <option name="LAST_COMMIT_MESSAGE" value="try catch on pdf addImage" />
179
193
  </component>
180
194
  </project>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "1.0.210",
3
+ "version": "1.0.214",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -71,18 +71,18 @@ async function createActivityReportByGroup(from, to, userData){
71
71
 
72
72
  async function createActivityReportByDevice(from, to, userData){
73
73
  const groupIds = userData.groups.map(g => g.id)
74
- const withoutGroupDevices = userData.byGroup ? userData.devices.filter(d => !groupIds.includes(d.groupId)) : userData.devices
74
+ const devicesToProcess = userData.byGroup ? userData.devices.filter(d => !groupIds.includes(d.groupId)) : userData.devices
75
75
 
76
76
  const allData = {
77
77
  devices: [],
78
- xpert: withoutGroupDevices.filter(d => d.attributes.xpert).length > 0,
78
+ xpert: devicesToProcess.filter(d => d.attributes.xpert).length > 0,
79
79
  from: from,
80
80
  to: to
81
81
  }
82
82
 
83
- withoutGroupDevices.sort((a, b) => (a.name > b.name) ? 1 : -1)
84
- const devicesToSlice = withoutGroupDevices.slice()
85
-
83
+ //Get report data from traccar
84
+ devicesToProcess.sort((a, b) => (a.name > b.name) ? 1 : -1)
85
+ const devicesToSlice = devicesToProcess.slice()
86
86
  const arrayOfArrays = automaticReports.sliceArray(devicesToSlice)
87
87
  let data = []
88
88
  let trips = []
@@ -96,12 +96,11 @@ async function createActivityReportByDevice(from, to, userData){
96
96
 
97
97
  console.log('Summary:' + data.length)
98
98
 
99
- if (data.length === 0) {
100
- return allData
99
+ //Process report data
100
+ if (data.length > 0) {
101
+ allData.devices = processDevices(devicesToProcess, data, trips)
101
102
  }
102
103
 
103
- allData.devices = processDevices(withoutGroupDevices, data, trips)
104
-
105
104
  return allData
106
105
  }
107
106
 
package/src/index.js CHANGED
@@ -23,7 +23,7 @@ function Reports(config, axios) {
23
23
  return require('./speeding-report').createSpeedingReport(from, to, userData, this.traccar, roadSpeedLimits)
24
24
  }
25
25
 
26
- this.speedingReportToPDF = (userData, reportData) => {
26
+ this.speedingReportToPDF = async (userData, reportData) => {
27
27
  return require('./speeding-report').exportSpeedingReportToPDF(userData, reportData)
28
28
  }
29
29
 
package/src/index.test.js CHANGED
@@ -23,14 +23,21 @@ describe('test reports', function() {
23
23
  await new SessionApi(traccarConfig, null, axios).sessionPost('jppenas@gmail.com', 'penas46881')
24
24
  const report = new Index(traccarConfig, axios)
25
25
  const userData = await report.getUserData()
26
-
27
- const reportData = await report.tripReport(new Date(2021, 2, 30, 0, 0, 0, 0),
28
- new Date(2021, 2, 30, 23, 59, 59, 0),
26
+ userData.groupByDay = true
27
+ const reportData = await report.kmsReport(new Date(2021, 10, 19, 0, 0, 0, 0),
28
+ new Date(2021, 10, 21, 23, 59, 59, 0),
29
29
  userData)
30
30
 
31
- console.log(reportData)
31
+ const reportResult = reportData[0]
32
+ reportResult.devices.forEach(d => {
33
+ console.log(d.device.name)
34
+ d.days.forEach(day => {
35
+ console.log(day.date + ' - ' + day.kms)
36
+ })
37
+ })
38
+
32
39
 
33
- const pdf = report.tripReportToPDF(userData, reportData)
40
+ const pdf = report.kmsReportToPDF(userData, reportResult)
34
41
 
35
42
  console.log(pdf)
36
43
  })
package/src/kms-report.js CHANGED
@@ -3,26 +3,123 @@ const messages = require('../lang')
3
3
  const jsPDF = require('jspdf')
4
4
  require('jspdf-autotable')
5
5
  const {getStyle} = require("./reportStyle");
6
- const {headerFromUser} = require("./util/pdfDocument");
6
+ const {headerFromUser,addTable} = require("./util/pdfDocument");
7
7
  const {getUserPartner} = require("fleetmap-partners");
8
+ const automaticReports = require("./automaticReports");
9
+ const {convertMS} = require("./util/utils");
8
10
 
9
11
 
10
12
  let traccarInstance
11
13
 
12
14
  const fileName = 'KmsReport'
13
15
 
16
+ async function createKmsReportByDevice(from, to, userData) {
17
+ const groupIds = userData.groups.map(g => g.id)
18
+ const devicesToProcess = userData.byGroup ? userData.devices.filter(d => !groupIds.includes(d.groupId)) : userData.devices
19
+
20
+ const allData = {
21
+ devices: []
22
+ }
23
+
24
+ devicesToProcess.sort((a, b) => (a.name > b.name) ? 1 : -1)
25
+ const devicesToSlice = devicesToProcess.slice()
26
+
27
+ const arrayOfArrays = automaticReports.sliceArray(devicesToSlice)
28
+ let data = []
29
+ for (const a of arrayOfArrays) {
30
+ const response = await traccarInstance.reports.reportsRouteGet(from, to, a.map(d => d.id))
31
+ data = data.concat(response.data)
32
+ }
33
+
34
+ console.log('locations:' + data.length)
35
+
36
+ if (data.length > 0) {
37
+ allData.devices = processDevices(from, to, devicesToProcess, data)
38
+ }
39
+
40
+ return allData
41
+ }
42
+
43
+ async function createKmsReportByDriver(from, to, userData) {
44
+
45
+ }
46
+
47
+ async function createKmsReportByGroup(from, to, userData) {
48
+
49
+ }
50
+
51
+ function processDevices(from, to, devices, data) {
52
+ const devicesResult = []
53
+
54
+ for (const d of devices) {
55
+ const locations = data.filter(t => t.deviceId === d.id)
56
+
57
+ if (locations.length > 0) {
58
+ locations.forEach(l => l.locationDate = l.fixTime.substring(0, 10))
59
+ const groupedLocations = locations.reduce(
60
+ (entryMap, e) => entryMap.set(e.locationDate, [...entryMap.get(e.locationDate)||[], e]),
61
+ new Map()
62
+ )
63
+
64
+ const days = []
65
+ const keys = Array.from(groupedLocations.keys())
66
+
67
+ keys.forEach(key => {
68
+ const dayLocations = groupedLocations.get(key)
69
+ const day = {
70
+ date: key,
71
+ kms: dayLocations.reduce((a, b) => a + b.attributes.distance, 0)
72
+ }
73
+ days.push(day)
74
+ })
75
+
76
+ devicesResult.push({
77
+ device: d,
78
+ days: days
79
+ })
80
+ }
81
+ }
82
+
83
+ return devicesResult
84
+ }
85
+
14
86
  async function createKmsReport(from, to, userData, traccar) {
15
87
  console.log('Create KmsReport')
16
88
  traccarInstance = traccar
17
- return await createActivityReport(from, to, userData, traccar)
89
+ if(userData.groupByDay){
90
+ const reportData = []
91
+
92
+ if(userData.byDriver){
93
+ const allData = await createKmsReportByDriver(from, to, userData)
94
+ reportData.push(allData)
95
+ }
96
+ else if(userData.byGroup){
97
+ const allGroupsData = await createKmsReportByGroup(from, to, userData)
98
+ allGroupsData.forEach(data => reportData.push(data))
99
+ const withoutGroupDevices = await createKmsReportByDevice(from, to, userData)
100
+ reportData.push(withoutGroupDevices)
101
+ } else {
102
+ const allData = await createKmsReportByDevice(from, to, userData)
103
+ reportData.push(allData)
104
+ }
105
+
106
+ return reportData
107
+ } else {
108
+ return await createActivityReport(from, to, userData, traccar)
109
+ }
18
110
  }
19
111
 
20
112
  async function exportKmsReportToPDF(userData, reportData) {
21
113
  const lang = userData.user.attributes.lang
22
114
  const translations = messages[lang] ? messages[lang] : messages['en-GB']
115
+ const kmsData = userData.byDriver ? reportData.drivers : reportData.devices
23
116
 
24
117
  const headers = []
25
- if(userData.byDriver) {
118
+ if(userData.groupByDay) {
119
+ headers.push(translations.report.date,
120
+ translations.report.distance)
121
+ }
122
+ else if(userData.byDriver) {
26
123
  headers.push(translations.report.driver,
27
124
  translations.report.group,
28
125
  translations.report.distance)
@@ -32,77 +129,94 @@ async function exportKmsReportToPDF(userData, reportData) {
32
129
  translations.report.group,
33
130
  translations.report.distance)
34
131
  }
35
- if (reportData.devices || reportData.drivers) {
36
- const doc = new jsPDF.jsPDF('l');
132
+ if (kmsData) {
133
+ const doc = userData.groupByDay ? new jsPDF.jsPDF() : new jsPDF.jsPDF('l')
37
134
  await headerFromUser(doc, translations.report.titleSpeedingReport, userData.user)
38
135
 
39
- let data = []
40
- doc.setFontSize(10)
41
- doc.text(new Date(reportData.from).toLocaleString() + ' - ' + new Date(reportData.to).toLocaleString(), 20, 30 )
42
- if(userData.byDriver) {
43
- reportData.drivers.forEach(d => {
44
- const group = userData.groups.find(g => g.drivers.includes(d.driver.id))
45
-
46
- const temp = [
47
- d.driver.name,
48
- group ? group.name : '',
49
- Number((d.summary.distance / 1000).toFixed(0)),
50
- ]
51
- data.push(temp)
52
- })
53
- } else {
54
- reportData.devices.forEach(d => {
55
- const group = userData.groups.find(g => d.device.groupId === g.id)
56
-
57
- const temp = [
58
- d.summary.deviceName,
59
- d.device.model,
60
- group ? group.name : '',
61
- Number((d.summary.distance / 1000).toFixed(0))
62
- ]
136
+ if (userData.groupByDay) {
137
+ for (const d of kmsData) {
138
+ const name = userData.byDriver ? d.driver.name : d.device.name
139
+ const group = userData.byDriver ? userData.groups.find(g => g.drivers.includes(d.id)) : userData.groups.find(g => d.device.groupId === g.id)
140
+
141
+ let space = 0
142
+ if (!first) {
143
+ doc.addPage()
144
+ } else {
145
+ first = false
146
+ space = 8
147
+ }
148
+ doc.setFontSize(13)
149
+ doc.text(name, 20, space + 20)
150
+ doc.setFontSize(11)
151
+ doc.text(group ? translations.report.group + ': ' + group.name : '', 200, space + 20)
152
+ doc.text(new Date(d.from).toLocaleString() + ' - ' + new Date(d.to).toLocaleString(), 20, space + 25)
153
+
154
+ const data = []
155
+ d.days.forEach(day => {
156
+ const temp = [
157
+ new Date(day).toLocaleDateString(),
158
+ (day.kms/1000).toFixed(0)
159
+ ]
160
+ })
161
+
63
162
  data.push(temp)
64
- })
65
- }
66
163
 
67
- const footValues = []
68
- if(userData.byDriver) {
69
- footValues.push(
70
- 'Total:' + reportData.drivers.length,
71
- '',
72
- (reportData.drivers.reduce((a, b) => a + b.summary.distance, 0)/1000).toFixed(0)
73
- )
164
+ const footValues = []
165
+ footValues.push(
166
+ '',
167
+ (d.days.reduce((a, b) => a + b.kms, 0)/1000).toFixed(0)
168
+ )
169
+
170
+ const style = getStyle(getUserPartner(userData.user))
171
+
172
+ addTable(doc, headers, data, footValues, style, 35)
173
+ }
74
174
  } else {
75
- footValues.push(
76
- 'Total:' + reportData.devices.length,
77
- '',
78
- '',
79
- (reportData.devices.reduce((a, b) => a + b.summary.distance, 0)/1000).toFixed(0),
80
- )
81
- }
175
+ if (userData.byDriver) {
176
+ reportData.drivers.forEach(d => {
177
+ const group = userData.groups.find(g => g.drivers.includes(d.driver.id))
178
+
179
+ const temp = [
180
+ d.driver.name,
181
+ group ? group.name : '',
182
+ Number((d.summary.distance / 1000).toFixed(0)),
183
+ ]
184
+ data.push(temp)
185
+ })
186
+ } else {
187
+ reportData.devices.forEach(d => {
188
+ const group = userData.groups.find(g => d.device.groupId === g.id)
82
189
 
83
- const style = getStyle(getUserPartner(userData.user))
84
- doc.autoTable({
85
- head: [headers],
86
- body: data,
87
- foot: [footValues],
88
- showFoot: 'lastPage',
89
- headStyles: {
90
- fillColor: style.pdfHeaderColor,
91
- textColor: style.pdfHeaderTextColor,
92
- fontSize: 10
93
- },
94
- bodyStyles: {
95
- fillColor: style.pdfBodyColor,
96
- textColor: style.pdfBodyTextColor,
97
- fontSize: 8
98
- },
99
- footStyles: {
100
- fillColor: style.pdfFooterColor,
101
- textColor: style.pdfFooterTextColor,
102
- fontSize: 9
103
- },
104
- startY: 35 });
190
+ const temp = [
191
+ d.summary.deviceName,
192
+ d.device.model,
193
+ group ? group.name : '',
194
+ Number((d.summary.distance / 1000).toFixed(0))
195
+ ]
196
+ data.push(temp)
197
+ })
198
+ }
199
+
200
+ const footValues = []
201
+ if(userData.byDriver) {
202
+ footValues.push(
203
+ 'Total:' + reportData.drivers.length,
204
+ '',
205
+ (reportData.drivers.reduce((a, b) => a + b.summary.distance, 0)/1000).toFixed(0)
206
+ )
207
+ } else {
208
+ footValues.push(
209
+ 'Total:' + reportData.devices.length,
210
+ '',
211
+ '',
212
+ (reportData.devices.reduce((a, b) => a + b.summary.distance, 0)/1000).toFixed(0),
213
+ )
214
+ }
105
215
 
216
+ const style = getStyle(getUserPartner(userData.user))
217
+
218
+ addTable(doc, headers, data, footValues, style, 35)
219
+ }
106
220
  return doc
107
221
  }
108
222
  }
@@ -120,55 +234,48 @@ function exportKmsReportToExcel(userData, reportData) {
120
234
 
121
235
  const headers = []
122
236
 
123
- if(userData.byDriver) {
237
+ if(userData.groupByDay){
124
238
  headers.push(
125
- {label: translations.report.driver, value: 'driver'},
239
+ {label: translations.report.vehicle, value: 'name'},
126
240
  {label: translations.report.group, value: 'group'},
241
+ {label: translations.report.date, value: 'date'},
127
242
  {label: translations.report.distance, value: 'distance'})
128
- } else {
243
+ } else if(userData.byDriver) {
129
244
  headers.push(
130
- {label: translations.report.vehicle, value: 'name'},
245
+ {label: translations.report.driver, value: 'name'},
131
246
  {label: translations.report.group, value: 'group'},
132
247
  {label: translations.report.distance, value: 'distance'})
133
248
  }
134
249
 
135
250
  let data = []
136
- if(userData.byDriver){
137
- if (reportData.drivers) {
138
- reportData.drivers.forEach(d => {
139
- const group = userData.groups.find(g => g.drivers.includes(d.driver.id))
251
+ const info = userData.byDriver ? reportData.drivers : reportData.devices
252
+ info.forEach(d => {
253
+ const group = userData.byDriver ? userData.groups.find(g => g.drivers.includes(d.driver.id)) :
254
+ userData.groups.find(g => d.device.groupId === g.id)
140
255
 
256
+ if(userData.groupByDay) {
257
+ data = data.concat([{}])
258
+ data = data.concat(d.days.map(a => {
259
+ return {
260
+ name: userData.byDriver ? d.driver.name : d.device.name,
261
+ group: group ? group.name : '',
262
+ date: a.date,
263
+ distance: Number((a.kms / 1000).toFixed(0))
264
+ }
265
+ }))
266
+ } else {
141
267
  data = data.concat([{
142
- driver: d.driver.name,
268
+ name: userData.byDriver ? d.driver.name : d.summary.deviceName,
143
269
  group: group ? group.name : '',
144
270
  distance: Number((d.summary.distance / 1000).toFixed(0))
145
271
  }])
146
- })
147
- console.log(data)
148
- return {
149
- headers,
150
- data,
151
- settings
152
272
  }
153
- }
154
- } else {
155
- if (reportData.devices) {
156
- reportData.devices.forEach(d => {
157
- const group = userData.groups.find(g => d.device.groupId === g.id)
273
+ })
158
274
 
159
- data = data.concat([{
160
- name: d.summary.deviceName,
161
- group: group ? group.name : '',
162
- distance: Number((d.summary.distance / 1000).toFixed(0))
163
- }])
164
- })
165
- console.log(data)
166
- return {
167
- headers,
168
- data,
169
- settings
170
- }
171
- }
275
+ return {
276
+ headers,
277
+ data,
278
+ settings
172
279
  }
173
280
  }
174
281
 
@@ -1,3 +1,4 @@
1
+ require('jspdf-autotable')
1
2
  const {getStyle} = require("../reportStyle");
2
3
  const {getUserPartner} = require("fleetmap-partners");
3
4
  const {getImgFromUrl} = require("./utils")
@@ -24,5 +25,30 @@ async function headerFromUser(doc, title, user) {
24
25
  return header(doc, title, partner.host, getStyle(partner))
25
26
  }
26
27
 
28
+ function addTable(doc, headers, data, footValues, style, startY){
29
+ doc.autoTable({
30
+ head: [headers],
31
+ body: data,
32
+ foot: [footValues],
33
+ showFoot: 'lastPage',
34
+ headStyles: {
35
+ fillColor: style.pdfHeaderColor,
36
+ textColor: style.pdfHeaderTextColor,
37
+ fontSize: 10
38
+ },
39
+ bodyStyles: {
40
+ fillColor: style.pdfBodyColor,
41
+ textColor: style.pdfBodyTextColor,
42
+ fontSize: 8
43
+ },
44
+ footStyles: {
45
+ fillColor: style.pdfFooterColor,
46
+ textColor: style.pdfFooterTextColor,
47
+ fontSize: 9
48
+ },
49
+ startY: startY });
50
+ }
51
+
27
52
  exports.header = header
28
53
  exports.headerFromUser = headerFromUser
54
+ exports.addTable = addTable