fleetmap-reports 1.0.210 → 1.0.211

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,12 @@
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" />
6
7
  <change beforePath="$PROJECT_DIR$/src/index.test.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/index.test.js" afterDir="false" />
8
+ <change beforePath="$PROJECT_DIR$/src/kms-report.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/kms-report.js" afterDir="false" />
7
9
  <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
10
  </list>
10
11
  <option name="SHOW_DIALOG" value="false" />
11
12
  <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -138,7 +139,10 @@
138
139
  <workItem from="1636981640447" duration="4315000" />
139
140
  <workItem from="1637009407292" duration="1470000" />
140
141
  <workItem from="1637012863006" duration="5376000" />
141
- <workItem from="1637087797229" duration="4410000" />
142
+ <workItem from="1637087797229" duration="5701000" />
143
+ <workItem from="1637227826845" duration="1275000" />
144
+ <workItem from="1637441942952" duration="4543000" />
145
+ <workItem from="1637670815688" duration="2973000" />
142
146
  </task>
143
147
  <task id="LOCAL-00001" summary="Fix getStyle">
144
148
  <created>1636983717385</created>
@@ -154,7 +158,14 @@
154
158
  <option name="project" value="LOCAL" />
155
159
  <updated>1637020728356</updated>
156
160
  </task>
157
- <option name="localTasksCounter" value="3" />
161
+ <task id="LOCAL-00003" summary="try catch on pdf addImage">
162
+ <created>1637247013210</created>
163
+ <option name="number" value="00003" />
164
+ <option name="presentableId" value="LOCAL-00003" />
165
+ <option name="project" value="LOCAL" />
166
+ <updated>1637247013211</updated>
167
+ </task>
168
+ <option name="localTasksCounter" value="4" />
158
169
  <servers />
159
170
  </component>
160
171
  <component name="TypeScriptGeneratedFilesManager">
@@ -175,6 +186,7 @@
175
186
  <component name="VcsManagerConfiguration">
176
187
  <MESSAGE value="Fix getStyle" />
177
188
  <MESSAGE value="Fix getStyle in all reports" />
178
- <option name="LAST_COMMIT_MESSAGE" value="Fix getStyle in all reports" />
189
+ <MESSAGE value="try catch on pdf addImage" />
190
+ <option name="LAST_COMMIT_MESSAGE" value="try catch on pdf addImage" />
179
191
  </component>
180
192
  </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.211",
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.test.js CHANGED
@@ -23,15 +23,22 @@ 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.tripReportToPDF(userData, reportData)
34
41
 
35
- console.log(pdf)
42
+ //console.log(pdf)
36
43
  })
37
44
  })
package/src/kms-report.js CHANGED
@@ -3,18 +3,109 @@ 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");
8
9
 
9
10
 
10
11
  let traccarInstance
11
12
 
12
13
  const fileName = 'KmsReport'
13
14
 
15
+ async function createKmsReportByDevice(from, to, userData) {
16
+ const groupIds = userData.groups.map(g => g.id)
17
+ const devicesToProcess = userData.byGroup ? userData.devices.filter(d => !groupIds.includes(d.groupId)) : userData.devices
18
+
19
+ const allData = {
20
+ devices: []
21
+ }
22
+
23
+ devicesToProcess.sort((a, b) => (a.name > b.name) ? 1 : -1)
24
+ const devicesToSlice = devicesToProcess.slice()
25
+
26
+ const arrayOfArrays = automaticReports.sliceArray(devicesToSlice)
27
+ let data = []
28
+ for (const a of arrayOfArrays) {
29
+ const response = await traccarInstance.reports.reportsRouteGet(from, to, a.map(d => d.id))
30
+ data = data.concat(response.data)
31
+ }
32
+
33
+ console.log('locations:' + data.length)
34
+
35
+ if (data.length > 0) {
36
+ allData.devices = processDevices(from, to, devicesToProcess, data)
37
+ }
38
+
39
+ return allData
40
+ }
41
+
42
+ async function createKmsReportByDriver(from, to, userData) {
43
+
44
+ }
45
+
46
+ async function createKmsReportByGroup(from, to, userData) {
47
+
48
+ }
49
+
50
+ function processDevices(from, to, devices, data) {
51
+ const devicesResult = []
52
+
53
+ for (const d of devices) {
54
+ const locations = data.filter(t => t.deviceId === d.id)
55
+
56
+ if (locations.length > 0) {
57
+ locations.forEach(l => l.locationDate = l.fixTime.substring(0, 10))
58
+ const groupedLocations = locations.reduce(
59
+ (entryMap, e) => entryMap.set(e.locationDate, [...entryMap.get(e.locationDate)||[], e]),
60
+ new Map()
61
+ )
62
+
63
+ const days = []
64
+ const keys = Array.from(groupedLocations.keys())
65
+
66
+ keys.forEach(key => {
67
+ const dayLocations = groupedLocations.get(key)
68
+ const day = {
69
+ date: key,
70
+ kms: dayLocations.reduce((a, b) => a + b.attributes.distance, 0)
71
+ }
72
+ days.push(day)
73
+ })
74
+
75
+ devicesResult.push({
76
+ device: d,
77
+ days: days
78
+ })
79
+ }
80
+ }
81
+
82
+ return devicesResult
83
+ }
84
+
14
85
  async function createKmsReport(from, to, userData, traccar) {
15
86
  console.log('Create KmsReport')
16
87
  traccarInstance = traccar
17
- return await createActivityReport(from, to, userData, traccar)
88
+ if(userData.groupByDay){
89
+ const reportData = []
90
+
91
+ if(userData.byDriver){
92
+ const allData = await createKmsReportByDriver(from, to, userData)
93
+ reportData.push(allData)
94
+ }
95
+ else if(userData.byGroup){
96
+ const allGroupsData = await createKmsReportByGroup(from, to, userData)
97
+ allGroupsData.forEach(data => reportData.push(data))
98
+ const withoutGroupDevices = await createKmsReportByDevice(from, to, userData)
99
+ reportData.push(withoutGroupDevices)
100
+ } else {
101
+ const allData = await createKmsReportByDevice(from, to, userData)
102
+ reportData.push(allData)
103
+ }
104
+
105
+ return reportData
106
+ } else {
107
+ return await createActivityReport(from, to, userData, traccar)
108
+ }
18
109
  }
19
110
 
20
111
  async function exportKmsReportToPDF(userData, reportData) {
@@ -81,29 +172,8 @@ async function exportKmsReportToPDF(userData, reportData) {
81
172
  }
82
173
 
83
174
  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 });
105
-
106
- return doc
175
+
176
+ return addTable(doc, headers, data, footValues, style, 35)
107
177
  }
108
178
  }
109
179
 
@@ -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