fleetmap-reports 1.0.304 → 1.0.308
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/.idea/aws.xml +17 -0
- package/.idea/codeStyles/Project.xml +7 -0
- package/{fleetmap-reports.iml → .idea/fleetmap-reports.iml} +2 -1
- package/.idea/jsLibraryMappings.xml +1 -1
- package/.idea/modules.xml +1 -1
- package/.idea/vcs.xml +1 -1
- package/package.json +1 -1
- package/src/activity-report.js +93 -106
- package/src/index.js +2 -3
- package/src/kms-report.js +1 -2
- package/src/trip-report.js +0 -2
- package/src/util/utils.js +31 -5
- package/src/word/index.js +66 -12
package/.idea/aws.xml
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<project version="4">
|
|
3
|
+
<component name="accountSettings">
|
|
4
|
+
<option name="activeProfile" value="profile:default" />
|
|
5
|
+
<option name="activeRegion" value="us-east-1" />
|
|
6
|
+
<option name="recentlyUsedProfiles">
|
|
7
|
+
<list>
|
|
8
|
+
<option value="profile:default" />
|
|
9
|
+
</list>
|
|
10
|
+
</option>
|
|
11
|
+
<option name="recentlyUsedRegions">
|
|
12
|
+
<list>
|
|
13
|
+
<option value="us-east-1" />
|
|
14
|
+
</list>
|
|
15
|
+
</option>
|
|
16
|
+
</component>
|
|
17
|
+
</project>
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<module type="
|
|
2
|
+
<module type="JAVA_MODULE" version="4">
|
|
3
3
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
|
4
4
|
<exclude-output />
|
|
5
5
|
<content url="file://$MODULE_DIR$" />
|
|
6
|
+
<orderEntry type="inheritedJdk" />
|
|
6
7
|
<orderEntry type="sourceFolder" forTests="false" />
|
|
7
8
|
</component>
|
|
8
9
|
</module>
|
package/.idea/modules.xml
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
<project version="4">
|
|
3
3
|
<component name="ProjectModuleManager">
|
|
4
4
|
<modules>
|
|
5
|
-
<module fileurl="file://$PROJECT_DIR
|
|
5
|
+
<module fileurl="file://$PROJECT_DIR$/.idea/fleetmap-reports.iml" filepath="$PROJECT_DIR$/.idea/fleetmap-reports.iml" />
|
|
6
6
|
</modules>
|
|
7
7
|
</component>
|
|
8
8
|
</project>
|
package/.idea/vcs.xml
CHANGED
package/package.json
CHANGED
package/src/activity-report.js
CHANGED
|
@@ -8,10 +8,9 @@ const {headerFromUser, addTable} = require("./util/pdfDocument")
|
|
|
8
8
|
const {getStyle} = require("./reportStyle")
|
|
9
9
|
const {getUserPartner} = require("fleetmap-partners")
|
|
10
10
|
const traccar = require("./util/traccar")
|
|
11
|
-
const {
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
import { saveAs } from "file-saver"
|
|
11
|
+
const {isInsideTimetable, isPartialInsideTimetable} = require("./util/trips");
|
|
12
|
+
const trips = require("./util/trips");
|
|
13
|
+
|
|
15
14
|
let traccarInstance
|
|
16
15
|
let userData
|
|
17
16
|
|
|
@@ -28,10 +27,8 @@ async function createActivityReport(from, to, reportUserData, traccar) {
|
|
|
28
27
|
reportData.push(allData)
|
|
29
28
|
}
|
|
30
29
|
else if(userData.byGroup){
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
const withoutGroupDevices = await createActivityReportByDevice(from, to)
|
|
34
|
-
reportData.push(withoutGroupDevices)
|
|
30
|
+
const allData = await createActivityReportByGroup(from, to)
|
|
31
|
+
reportData.push(...allData)
|
|
35
32
|
} else {
|
|
36
33
|
const allData = await createActivityReportByDevice(from, to)
|
|
37
34
|
reportData.push(allData)
|
|
@@ -56,22 +53,26 @@ async function createActivityReportByGroup(from, to){
|
|
|
56
53
|
}
|
|
57
54
|
|
|
58
55
|
const response = await traccarInstance.reports.reportsSummaryGet(from, to, null, [g.id])
|
|
59
|
-
const
|
|
56
|
+
const summaries = response.data
|
|
60
57
|
|
|
61
58
|
const responseTrips = await traccarInstance.reports.reportsTripsGet(from, to, null, [g.id])
|
|
62
59
|
const trips = responseTrips.data
|
|
63
60
|
|
|
64
61
|
devices.sort((a, b) => (a.name > b.name) ? 1 : -1)
|
|
65
62
|
|
|
66
|
-
if (
|
|
63
|
+
if (summaries.length > 0) {
|
|
67
64
|
|
|
68
|
-
console.log('Summary:' +
|
|
69
|
-
groupData.devices = processDevices(devices,
|
|
65
|
+
console.log('Summary:' + summaries.length)
|
|
66
|
+
groupData.devices = processDevices(from, to, devices, {summaries: summaries, trips: trips})
|
|
70
67
|
|
|
71
68
|
reportData.push(groupData)
|
|
72
69
|
}
|
|
73
70
|
}
|
|
74
71
|
}
|
|
72
|
+
|
|
73
|
+
const withoutGroupDevices = await createActivityReportByDevice(from, to)
|
|
74
|
+
reportData.push(withoutGroupDevices)
|
|
75
|
+
|
|
75
76
|
return reportData
|
|
76
77
|
}
|
|
77
78
|
|
|
@@ -89,73 +90,38 @@ async function createActivityReportByDevice(from, to){
|
|
|
89
90
|
//Get report data from traccar
|
|
90
91
|
devicesToProcess.sort((a, b) => (a.name > b.name) ? 1 : -1)
|
|
91
92
|
|
|
92
|
-
|
|
93
|
-
|
|
93
|
+
let summaries = []
|
|
94
|
+
let routeData = []
|
|
95
|
+
const tripsData = await traccar.getTrips(traccarInstance, from, to, devicesToProcess)
|
|
94
96
|
|
|
95
|
-
let data = []
|
|
96
97
|
if(userData.groupByDay) {
|
|
98
|
+
routeData = userData.allWeek ? [] : await traccar.getRoute(traccarInstance, from, to, devicesToProcess)
|
|
99
|
+
console.log('trips:' + tripsData.length)
|
|
100
|
+
|
|
101
|
+
if (tripsData.length > 0) {
|
|
102
|
+
trips.checkTripsKms(traccarInstance, from, to, devicesToProcess, {trips: tripsData, route: routeData})
|
|
103
|
+
}
|
|
104
|
+
|
|
97
105
|
const dates = getDates(from, to)
|
|
98
106
|
for(const date of dates){
|
|
99
|
-
if(userData.allWeek || !userData.weekDays){
|
|
107
|
+
if(userData.allWeek || !userData.weekDays || !userData.dayHours){
|
|
100
108
|
const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
|
|
101
109
|
const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
|
|
102
110
|
|
|
103
111
|
const summary = await traccar.getSummary(traccarInstance, fromByDay, toByDay, devicesToProcess)
|
|
104
112
|
summary.forEach(s => s.date = date)
|
|
105
|
-
|
|
106
|
-
} else {
|
|
107
|
-
if((date.getDay() === 0 && userData.weekDays.sunday) ||
|
|
108
|
-
(date.getDay() === 1 && userData.weekDays.monday) ||
|
|
109
|
-
(date.getDay() === 2 && userData.weekDays.tuesday) ||
|
|
110
|
-
(date.getDay() === 3 && userData.weekDays.wednesday) ||
|
|
111
|
-
(date.getDay() === 4 && userData.weekDays.thursday) ||
|
|
112
|
-
(date.getDay() === 5 && userData.weekDays.friday) ||
|
|
113
|
-
(date.getDay() === 6 && userData.weekDays.saturday)) {
|
|
114
|
-
|
|
115
|
-
const fromByDay = new Date(new Date(date).toISOString().split('T')[0] + ' ' + userData.dayHours.startTime)
|
|
116
|
-
const toByDay = new Date(new Date(date).toISOString().split('T')[0] + ' ' + userData.dayHours.endTime)
|
|
117
|
-
|
|
118
|
-
if(fromByDay.getTime() < toByDay.getTime()) {
|
|
119
|
-
const summary = await traccar.getSummary(traccarInstance, new Date(fromByDay.toUTCString()), new Date(toByDay.toUTCString()), devicesToProcess)
|
|
120
|
-
summary.forEach(s => s.date = date)
|
|
121
|
-
data = data.concat(summary)
|
|
122
|
-
} else {
|
|
123
|
-
const dayStart = new Date(new Date(date).toISOString().split('T')[0] + ' 00:00:00')
|
|
124
|
-
const dayEnd = new Date(new Date(date).toISOString().split('T')[0] + ' 23:59:59')
|
|
125
|
-
|
|
126
|
-
const summaryStart = await traccar.getSummary(traccarInstance, new Date(dayStart.toUTCString()), new Date(toByDay.toUTCString()), devicesToProcess)
|
|
127
|
-
const summaryEnd = await traccar.getSummary(traccarInstance, new Date(fromByDay.toUTCString()), new Date(dayEnd.toUTCString()), devicesToProcess)
|
|
128
|
-
|
|
129
|
-
const summary = []
|
|
130
|
-
summaryStart.forEach(s => {
|
|
131
|
-
const sEnd = summaryEnd.find(a => s.deviceId === a.deviceId)
|
|
132
|
-
summary.push({
|
|
133
|
-
date: date,
|
|
134
|
-
deviceId: s.deviceId,
|
|
135
|
-
averageSpeed: (s.averageSpeed + sEnd.averageSpeed) / 2,
|
|
136
|
-
distance: s.distance + sEnd.distance,
|
|
137
|
-
engineHours: s.engineHours + sEnd.engineHours,
|
|
138
|
-
maxSpeed: s.maxSpeed > sEnd.maxSpeed ? s.maxSpeed : sEnd.maxSpeed,
|
|
139
|
-
endOdometer: 0,
|
|
140
|
-
startOdometer: 0
|
|
141
|
-
})
|
|
142
|
-
})
|
|
143
|
-
data = data.concat(summary)
|
|
144
|
-
}
|
|
145
|
-
}else {
|
|
146
|
-
data = data.concat({date: date})
|
|
147
|
-
}
|
|
113
|
+
summaries = summaries.concat(summary)
|
|
148
114
|
}
|
|
149
115
|
}
|
|
150
116
|
} else {
|
|
151
|
-
|
|
117
|
+
summaries = await traccar.getSummary(traccarInstance, from, to, devicesToProcess)
|
|
152
118
|
}
|
|
153
119
|
|
|
154
|
-
console.log('Summary:' +
|
|
120
|
+
console.log('Summary:' + summaries.length)
|
|
155
121
|
|
|
156
122
|
//Process report data
|
|
157
|
-
if (
|
|
158
|
-
allData.devices = processDevices(devicesToProcess,
|
|
123
|
+
if (summaries.length || tripsData.length) {
|
|
124
|
+
allData.devices = processDevices(from, to, devicesToProcess, {summaries: summaries, trips: tripsData, route: routeData})
|
|
159
125
|
}
|
|
160
126
|
|
|
161
127
|
return allData
|
|
@@ -165,8 +131,10 @@ async function createActivityReportByDriver(from, to){
|
|
|
165
131
|
const devices = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
|
|
166
132
|
|
|
167
133
|
let tripsData = []
|
|
134
|
+
let routeData = []
|
|
168
135
|
if(devices.length > 0) {
|
|
169
136
|
tripsData = await traccar.getTrips(traccarInstance, from, to, devices)
|
|
137
|
+
routeData = await traccar.getRoute(traccarInstance, from, to, devices)
|
|
170
138
|
}
|
|
171
139
|
|
|
172
140
|
const allData = {
|
|
@@ -175,28 +143,74 @@ async function createActivityReportByDriver(from, to){
|
|
|
175
143
|
to: to
|
|
176
144
|
}
|
|
177
145
|
|
|
178
|
-
allData.drivers = processDrivers(from, to, userData.drivers, tripsData)
|
|
146
|
+
allData.drivers = processDrivers(from, to, userData.drivers, {trips: tripsData, route: routeData})
|
|
179
147
|
|
|
180
148
|
return allData
|
|
181
149
|
}
|
|
182
150
|
|
|
183
|
-
|
|
151
|
+
|
|
152
|
+
function processDevices(from, to, devices, data) {
|
|
184
153
|
const devicesResult = []
|
|
185
154
|
|
|
186
155
|
devices.forEach(d => {
|
|
187
|
-
const summary =
|
|
188
|
-
if
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
156
|
+
const summary = []
|
|
157
|
+
if(userData.groupByDay) {
|
|
158
|
+
const route = data.route.filter(t => t.deviceId === d.id)
|
|
159
|
+
const deviceTrips = data.trips.filter(t => t.deviceId === d.id
|
|
160
|
+
&& (userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, route)))
|
|
161
|
+
|
|
162
|
+
const dates = getDates(from, to)
|
|
163
|
+
for(const date of dates){
|
|
164
|
+
const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
|
|
165
|
+
const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
|
|
166
|
+
|
|
167
|
+
const tripsByDay = deviceTrips.filter(t => (new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay))
|
|
168
|
+
const distance = tripsByDay.reduce((a, b) => a + b.distance, 0)
|
|
169
|
+
|
|
170
|
+
if(!userData.allWeek && userData.weekDays && userData.dayHours) {
|
|
171
|
+
summary.push({
|
|
172
|
+
date: date,
|
|
173
|
+
deviceId: d.id,
|
|
174
|
+
averageSpeed: distance > 0 ? ((tripsByDay.reduce((a, b) => a + (b.averageSpeed * b.distance), 0)) / distance) : 0,
|
|
175
|
+
distance: distance,
|
|
176
|
+
engineHours: tripsByDay.reduce((a, b) => a + b.duration, 0),
|
|
177
|
+
maxSpeed: tripsByDay.reduce((a, b) => {
|
|
178
|
+
return a > b.maxSpeed ? a : b.maxSpeed
|
|
179
|
+
}, 0),
|
|
180
|
+
endOdometer: 0,
|
|
181
|
+
startOdometer: 0
|
|
182
|
+
})
|
|
183
|
+
} else {
|
|
184
|
+
const summaryCurrentDay = data.summaries.filter(s => {
|
|
185
|
+
console.log(s.date.getTime(), date.getTime(), s.date.getTime() === date.getTime())
|
|
186
|
+
return s.deviceId === d.id && s.date.getTime() === date.getTime()})
|
|
187
|
+
|
|
188
|
+
if (summaryCurrentDay.length) {
|
|
189
|
+
summaryCurrentDay.forEach(s => {
|
|
190
|
+
s.distance = distance
|
|
191
|
+
s.convertedSpentFuel = automaticReports.calculateSpentFuel(s.spentFuel, d)
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
summary.push(...summaryCurrentDay)
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
} else {
|
|
199
|
+
summary.push(...data.summaries.filter(s => s.deviceId === d.id))
|
|
200
|
+
|
|
201
|
+
if (summary.length) {
|
|
202
|
+
summary.forEach(s => {
|
|
203
|
+
s.distance = data.trips.filter(t => t.deviceId === d.id).reduce((a, b) => a + b.distance, 0)
|
|
204
|
+
s.convertedSpentFuel = automaticReports.calculateSpentFuel(s.spentFuel, d)
|
|
205
|
+
})
|
|
197
206
|
}
|
|
198
|
-
devicesResult.push(deviceData)
|
|
199
207
|
}
|
|
208
|
+
|
|
209
|
+
const deviceData = {
|
|
210
|
+
device: d,
|
|
211
|
+
summary: summary,
|
|
212
|
+
}
|
|
213
|
+
devicesResult.push(deviceData)
|
|
200
214
|
})
|
|
201
215
|
|
|
202
216
|
return devicesResult
|
|
@@ -206,7 +220,8 @@ function processDrivers(from, to, drivers, data) {
|
|
|
206
220
|
console.log(data)
|
|
207
221
|
const driversResult = []
|
|
208
222
|
drivers.forEach(d => {
|
|
209
|
-
const
|
|
223
|
+
const route = data.route.filter(p => p.attributes.driverUniqueId === d.uniqueId)
|
|
224
|
+
const trips = data.trips.filter(t => t.driverUniqueId === d.uniqueId)
|
|
210
225
|
if (trips.length > 0) {
|
|
211
226
|
const driverData = {
|
|
212
227
|
driver: d,
|
|
@@ -219,7 +234,7 @@ function processDrivers(from, to, drivers, data) {
|
|
|
219
234
|
const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
|
|
220
235
|
|
|
221
236
|
const tripsByDay = trips.filter(t => (new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay)
|
|
222
|
-
&& (userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData)))
|
|
237
|
+
&& (userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, route)))
|
|
223
238
|
|
|
224
239
|
driverData.summary.push({
|
|
225
240
|
date: date,
|
|
@@ -267,34 +282,6 @@ async function exportActivityReportToPDF(userData, reportData) {
|
|
|
267
282
|
}
|
|
268
283
|
}
|
|
269
284
|
|
|
270
|
-
function exportActivityReportToWord(userData, reportData) {
|
|
271
|
-
const doc = new Document({
|
|
272
|
-
sections: [{
|
|
273
|
-
properties: {},
|
|
274
|
-
children: [
|
|
275
|
-
new Paragraph({
|
|
276
|
-
children: [
|
|
277
|
-
new TextRun("Hello World"),
|
|
278
|
-
new TextRun({
|
|
279
|
-
text: "Foo Bar",
|
|
280
|
-
bold: true,
|
|
281
|
-
}),
|
|
282
|
-
new TextRun({
|
|
283
|
-
text: "\tGithub is the best",
|
|
284
|
-
bold: true,
|
|
285
|
-
}),
|
|
286
|
-
],
|
|
287
|
-
}),
|
|
288
|
-
],
|
|
289
|
-
}],
|
|
290
|
-
})
|
|
291
|
-
Packer.toBlob(doc).then(blob => {
|
|
292
|
-
console.log(blob);
|
|
293
|
-
saveAs(blob, "example.docx");
|
|
294
|
-
console.log("Document created successfully");
|
|
295
|
-
})
|
|
296
|
-
}
|
|
297
|
-
|
|
298
285
|
function exportActivityReportToPDF_Driver(doc, translations, reportData) {
|
|
299
286
|
const headers = [translations.report.driver,
|
|
300
287
|
translations.report.distance,
|
package/src/index.js
CHANGED
|
@@ -40,9 +40,6 @@ function Reports(config, axios) {
|
|
|
40
40
|
this.tripReportToExcel = (userData, reportData) => {
|
|
41
41
|
return require('./trip-report').exportTripReportToExcel(userData, reportData)
|
|
42
42
|
}
|
|
43
|
-
this.tripReportToWord = (userData, reportData) => {
|
|
44
|
-
return require('./trip-report').exportTripReportToWord(userData, reportData)
|
|
45
|
-
}
|
|
46
43
|
|
|
47
44
|
this.zoneReport = (from, to, userData) => {
|
|
48
45
|
return require('./zone-report').createZoneReport(from, to, userData, this.traccar)
|
|
@@ -112,6 +109,8 @@ function Reports(config, axios) {
|
|
|
112
109
|
return require('./kms-report').exportKmsReportToPDF(userData, reportData)
|
|
113
110
|
}
|
|
114
111
|
|
|
112
|
+
this.reportToWord = require('./word').reportToWord
|
|
113
|
+
|
|
115
114
|
this.kmsReportToExcel = (userData, reportData) => {
|
|
116
115
|
return require('./kms-report').exportKmsReportToExcel(userData, reportData)
|
|
117
116
|
}
|
package/src/kms-report.js
CHANGED
|
@@ -133,8 +133,7 @@ function processDrivers(from, to, drivers, data) {
|
|
|
133
133
|
const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
|
|
134
134
|
const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
|
|
135
135
|
|
|
136
|
-
const tripsByDay = trips.filter(t => (new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay)
|
|
137
|
-
&& (userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData)))
|
|
136
|
+
const tripsByDay = trips.filter(t => (new Date(t.startTime) > fromByDay && new Date(t.endTime) < toByDay))
|
|
138
137
|
|
|
139
138
|
driverData.days.push({
|
|
140
139
|
date: date,
|
package/src/trip-report.js
CHANGED
|
@@ -10,7 +10,6 @@ const {getStyle} = require("./reportStyle")
|
|
|
10
10
|
const traccar = require("./util/traccar")
|
|
11
11
|
const trips = require("./util/trips");
|
|
12
12
|
const {isInsideTimetable, addNearestPOIs, isPartialInsideTimetable} = require("./util/trips")
|
|
13
|
-
const word = require('./word')
|
|
14
13
|
|
|
15
14
|
|
|
16
15
|
let traccarInstance
|
|
@@ -449,4 +448,3 @@ function getStop(tripEndDate, stops){
|
|
|
449
448
|
exports.createTripReport = createTripReport
|
|
450
449
|
exports.exportTripReportToPDF = exportTripReportToPDF
|
|
451
450
|
exports.exportTripReportToExcel = exportTripReportToExcel
|
|
452
|
-
exports.exportTripReportToWord = word.exportTripReportToWord
|
package/src/util/utils.js
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
const axios = require('axios')
|
|
2
2
|
const turf = require('turf')
|
|
3
|
+
const {getUserPartner} = require("fleetmap-partners")
|
|
4
|
+
const messages = require("../../lang")
|
|
5
|
+
|
|
6
|
+
exports.getTranslations = (userData) => {
|
|
7
|
+
const lang = userData.user.attributes.lang || (navigator && navigator.language)
|
|
8
|
+
return messages[lang] ? messages[lang] : messages['en-GB']
|
|
9
|
+
}
|
|
3
10
|
|
|
4
11
|
function convertMS(duration, withSeconds) {
|
|
5
12
|
|
|
@@ -31,7 +38,7 @@ function coordsDistance(lon1, lat1, lon2, lat2) {
|
|
|
31
38
|
|
|
32
39
|
function getDates(startDate, endDate) {
|
|
33
40
|
const dates = []
|
|
34
|
-
let currentDate = new Date(startDate.toISOString().split('T')[0] + ' 12:00 PM')
|
|
41
|
+
let currentDate = new Date(startDate.toISOString().split('T')[0] + ' 12:00:00.000 PM')
|
|
35
42
|
const addDays = function (days) {
|
|
36
43
|
const date = new Date(this.valueOf())
|
|
37
44
|
date.setDate(date.getDate() + days)
|
|
@@ -44,19 +51,34 @@ function getDates(startDate, endDate) {
|
|
|
44
51
|
return dates
|
|
45
52
|
}
|
|
46
53
|
|
|
54
|
+
function getImage(url) {
|
|
55
|
+
return axios.get(url, {responseType: 'arraybuffer'}).then(d => d.data)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function getImageBase64(url) {
|
|
59
|
+
const response = await getImage(url);
|
|
60
|
+
return Buffer.from(response, 'binary').toString('base64')
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function getLogoUrl(hostname) {
|
|
64
|
+
const path = `/img/logos/${hostname}.png`
|
|
65
|
+
return window ? path : `https://${hostname}${path}`
|
|
66
|
+
}
|
|
67
|
+
|
|
47
68
|
async function getImgFromUrl(hostname) {
|
|
69
|
+
const url = getLogoUrl(hostname)
|
|
48
70
|
try {
|
|
49
71
|
const img = new Image()
|
|
50
|
-
img.src =
|
|
72
|
+
img.src = url
|
|
51
73
|
return img
|
|
52
74
|
} catch (e) {
|
|
53
|
-
|
|
54
|
-
return Buffer.from(response.data, 'binary').toString('base64')
|
|
75
|
+
return await getImageBase64(url);
|
|
55
76
|
}
|
|
56
77
|
}
|
|
57
78
|
|
|
58
79
|
function isClientSide() { return (typeof window !== "undefined") }
|
|
59
80
|
|
|
81
|
+
|
|
60
82
|
function convertToLocaleString(value, lang, timezone){
|
|
61
83
|
if(isClientSide()){
|
|
62
84
|
return new Date(value).toLocaleString()
|
|
@@ -97,4 +119,8 @@ exports.getDates = getDates
|
|
|
97
119
|
exports.convertToLocaleString = convertToLocaleString
|
|
98
120
|
exports.convertToLocaleDateString = convertToLocaleDateString
|
|
99
121
|
exports.convertToLocaleTimeString = convertToLocaleTimeString
|
|
100
|
-
|
|
122
|
+
exports.isClientSide = isClientSide
|
|
123
|
+
exports.getLogo = (user) => {
|
|
124
|
+
const host = window ? window.location.hostname : getUserPartner(user).host
|
|
125
|
+
return getImage(getLogoUrl(host))
|
|
126
|
+
}
|
package/src/word/index.js
CHANGED
|
@@ -1,27 +1,81 @@
|
|
|
1
|
-
const {Document, Paragraph, Packer, TableRow, TableCell, Table
|
|
1
|
+
const {Document, Paragraph, Packer, TableRow, TableCell, Table, WidthType, ImageRun, Header, TextRun, HeadingLevel,
|
|
2
|
+
AlignmentType
|
|
3
|
+
} = require("docx")
|
|
2
4
|
const { saveAs } = require('file-saver')
|
|
3
|
-
const
|
|
5
|
+
const utils = require('../util/utils')
|
|
6
|
+
const {getPartnerData} = require("fleetmap-partners");
|
|
7
|
+
const colWidth = 3505
|
|
4
8
|
|
|
5
|
-
|
|
6
|
-
|
|
9
|
+
function formatCellValue(value) {
|
|
10
|
+
return value.toLocaleDateString ? value.toLocaleDateString() : value + ''
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
exports.reportToWord = async (userData, dateRange, excel, title) => {
|
|
14
|
+
console.log('excel', excel)
|
|
15
|
+
const partnerData = getPartnerData(userData.user)
|
|
16
|
+
|
|
17
|
+
if (!excel.data.length && utils.isClientSide()) {
|
|
18
|
+
alert('Não há dados')
|
|
19
|
+
return
|
|
20
|
+
}
|
|
21
|
+
const logo = await utils.getLogo(userData.user)
|
|
22
|
+
console.log('logo', logo)
|
|
7
23
|
const table = new Table({
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
24
|
+
columnWidths: excel.headers.map(() => colWidth),
|
|
25
|
+
rows: [
|
|
26
|
+
new TableRow({
|
|
27
|
+
children: excel.headers.map(r => new TableCell({
|
|
28
|
+
children: [new Paragraph(r.label)],
|
|
29
|
+
width: {
|
|
30
|
+
size: colWidth,
|
|
31
|
+
type: WidthType.DXA,
|
|
32
|
+
}
|
|
33
|
+
}))}),
|
|
34
|
+
...excel.data.filter(r=>Object.keys(r).length).map(r => new TableRow({
|
|
35
|
+
children: Object.keys(r).map(k =>
|
|
36
|
+
new TableCell({
|
|
37
|
+
children: [new Paragraph(formatCellValue(r[k]))],
|
|
38
|
+
width: {
|
|
39
|
+
size: colWidth,
|
|
40
|
+
type: WidthType.DXA,
|
|
41
|
+
}
|
|
42
|
+
}))})),
|
|
43
|
+
]
|
|
14
44
|
})
|
|
15
|
-
|
|
16
45
|
const doc = new Document({
|
|
17
46
|
sections: [{
|
|
47
|
+
headers: {
|
|
48
|
+
default: new Header({
|
|
49
|
+
children: [
|
|
50
|
+
new Paragraph({
|
|
51
|
+
children: [
|
|
52
|
+
new ImageRun({
|
|
53
|
+
data: logo,
|
|
54
|
+
transformation: {
|
|
55
|
+
width: (partnerData.reports && partnerData.reports.logoWidth*2) || 200,
|
|
56
|
+
height: (partnerData.reports && partnerData.reports.logoHeight*2) || 50
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
]
|
|
60
|
+
})
|
|
61
|
+
]
|
|
62
|
+
})
|
|
63
|
+
},
|
|
18
64
|
properties: {},
|
|
19
65
|
children: [
|
|
66
|
+
new Paragraph({
|
|
67
|
+
children: [new TextRun(title)],
|
|
68
|
+
heading: HeadingLevel.HEADING_1,
|
|
69
|
+
alignment: AlignmentType.CENTER,
|
|
70
|
+
}),
|
|
71
|
+
new Paragraph({}),
|
|
72
|
+
new Paragraph(new Date(dateRange[0]).toLocaleString() + ' - ' + new Date(dateRange[1]).toLocaleString()),
|
|
73
|
+
new Paragraph({}),
|
|
20
74
|
table
|
|
21
75
|
],
|
|
22
76
|
}],
|
|
23
77
|
})
|
|
24
78
|
Packer.toBlob(doc).then(blob => {
|
|
25
|
-
saveAs(blob, "
|
|
79
|
+
saveAs(blob, "report.docx");
|
|
26
80
|
})
|
|
27
81
|
}
|