fleetmap-reports 1.0.684 → 1.0.686

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "1.0.684",
3
+ "version": "1.0.686",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -55,12 +55,11 @@ async function createFuelConsumptionReport (from, to, userData, traccar) {
55
55
  new Map()
56
56
  )
57
57
 
58
- const odooAvgConsumption = useOdooFuelData(d, route) ? getOdooAvgConsumption(refuelingPositions, trips) : 0
59
-
60
58
  const days = []
61
59
  const keys = Array.from(groupedTripsByDay.keys())
62
60
 
63
61
  keys.forEach(day => {
62
+ const odooAvgConsumption = useOdooFuelData(d, route) ? getOdooAvgConsumption(day, refuelingPositions, trips) : 0
64
63
  const dayTrips = groupedTripsByDay.get(day)
65
64
  const dayRefueling = groupedRefuelingsByDay.get(day)
66
65
  const distance = dayTrips.reduce((a, b) => a + b.distance, 0)
@@ -94,13 +93,21 @@ async function createFuelConsumptionReport (from, to, userData, traccar) {
94
93
  return reportData
95
94
  }
96
95
 
97
- function getOdooAvgConsumption (refuelingPositions, trips) {
98
- const odooTotalfuel = refuelingPositions.reduce((a, b) => a + b.diff, 0)
99
- const totalKms = trips.reduce((a, b) => a + b.distance, 0)
96
+ function getOdooAvgConsumption (day, allRefuelingPositions, trips) {
97
+ const refuelingData = allRefuelingPositions.filter(r => new Date(r.date).getTime() > new Date(day).getTime())
98
+ let odooFuel
99
+ let kms
100
+ if (!refuelingData.length) {
101
+ odooFuel = allRefuelingPositions.reduce((a, b) => a + b.diff, 0)
102
+ kms = trips.reduce((a, b) => a + b.distance, 0)
103
+ } else {
104
+ odooFuel = allRefuelingPositions.filter(r => new Date(r.date).getTime() <= new Date(refuelingData[0].date).getTime()).reduce((a, b) => a + b.diff, 0)
105
+ kms = trips.filter(t => new Date(t.startDate).getTime() < new Date(refuelingData[0].date).getTime()).reduce((a, b) => a + b.distance, 0)
106
+ }
100
107
 
101
- if (totalKms === 0 || odooTotalfuel === 0) { return { byKms: 0, byLiters: 0 } }
108
+ if (kms === 0 || odooFuel === 0) { return { byKms: 0, byLiters: 0 } }
102
109
 
103
- return calculateAvgConsumption(odooTotalfuel, totalKms)
110
+ return calculateAvgConsumption(odooFuel, kms)
104
111
  }
105
112
 
106
113
  function getCanAvgConsumption (distance, spentFuel) {
@@ -110,7 +117,7 @@ function getCanAvgConsumption (distance, spentFuel) {
110
117
  }
111
118
 
112
119
  function calculateAvgConsumption (fuel, distance) {
113
- return { byKms: Math.round(fuel * 100 / (distance / 1000)), byLiters: Math.round((distance / 1000) / fuel) }
120
+ return { byKms: fuel * 100 / (distance / 1000), byLiters: (distance / 1000) / fuel }
114
121
  }
115
122
 
116
123
  function calculateConsumption (userData, d, day, avgConsumption, data) {
@@ -118,7 +125,7 @@ function calculateConsumption (userData, d, day, avgConsumption, data) {
118
125
  return automaticReports.calculateXpertSpentFuel(day, data.route)
119
126
  }
120
127
  if (useOdooFuelData(d, data.route)) {
121
- return Math.round((avgConsumption.byKms * (data.trips.reduce((a, b) => a + b.distance, 0) / 1000)) / 100)
128
+ return (avgConsumption.byKms * (data.trips.reduce((a, b) => a + b.distance, 0) / 1000) / 100)
122
129
  }
123
130
  return automaticReports.calculateSpentFuel(data.trips.reduce((a, b) => a + b.spentFuel, 0), d)
124
131
  }
@@ -136,7 +143,7 @@ function exportFuelConsumptionReportToExcel (userData, reportData) {
136
143
  { label: translations.report.date, value: 'date' },
137
144
  { label: translations.report.distance, value: 'kms' },
138
145
  { label: translations.report.consumption, value: 'consumption' },
139
- { label: translations.report.avg_consumption, value: 'avg_consumption' },
146
+ { label: (lang === 'pt-BR' ? translations.report.avg_consumption_by_liters : translations.report.avg_consumption), value: 'avg_consumption' },
140
147
  { label: translations.report.duration, value: 'duration' },
141
148
  { label: translations.report.odometer, value: 'accumulated_kms' },
142
149
  { label: translations.report.refueling, value: 'refueling' }
@@ -152,7 +159,7 @@ function exportFuelConsumptionReportToExcel (userData, reportData) {
152
159
  consumption: r.spentFuel > 0 ? formatNumber(r.spentFuel, lang) : 0,
153
160
  avg_consumption: formatNumber(lang === 'pt-BR' ? r.avgConsumption.byLiters : r.avgConsumption.byKms, lang),
154
161
  duration: convertMS(r.duration),
155
- accumulated_kms: (r.endOdometer / 100).toFixed(1),
162
+ accumulated_kms: formatNumber((r.endOdometer / 100), lang),
156
163
  refueling: formatNumber(r.refueling, lang)
157
164
  }
158
165
  }))
@@ -189,7 +196,7 @@ async function exportFuelConsumptionReportToPDF (userData, reportData) {
189
196
  translations.report.date,
190
197
  translations.report.distance,
191
198
  translations.report.consumption,
192
- translations.report.avg_consumption,
199
+ (lang === 'pt-BR' ? translations.report.avg_consumption_by_liters : translations.report.avg_consumption),
193
200
  translations.report.duration,
194
201
  translations.report.odometer,
195
202
  translations.report.refueling
@@ -212,7 +219,6 @@ async function exportFuelConsumptionReportToPDF (userData, reportData) {
212
219
  doc.text(group ? translations.report.group + ': ' + group.name : '', 200, space + 20)
213
220
  doc.text(convertToLocaleString(reportData.from, lang, timezone) + ' - ' + convertToLocaleString(reportData.to, lang, timezone), 20, space + 25)
214
221
 
215
- console.log(d.days)
216
222
  d.days.forEach(r =>
217
223
  data.push([
218
224
  r.date,
@@ -220,10 +226,10 @@ async function exportFuelConsumptionReportToPDF (userData, reportData) {
220
226
  r.spentFuel > 0 ? formatNumber(r.spentFuel, lang) : 0,
221
227
  formatNumber(lang === 'pt-BR' ? r.avgConsumption.byLiters : r.avgConsumption.byKms, lang),
222
228
  convertMS(r.duration),
223
- (r.endOdometer / 1000).toFixed(1),
224
- r.refueling
229
+ formatNumber((r.endOdometer / 1000), lang),
230
+ formatNumber(r.refueling, lang)
225
231
  ]))
226
- console.log(data)
232
+
227
233
  const footValues = [
228
234
  'Total:' + d.days.length,
229
235
  getTotalKms(userData.user, d.days),
@@ -235,7 +241,15 @@ async function exportFuelConsumptionReportToPDF (userData, reportData) {
235
241
  ]
236
242
 
237
243
  const style = getStyle(getUserPartner(userData.user))
238
- addTable(doc, headers, data, footValues, style, space + (userData.allWeek ? 40 : 45))
244
+ addTable(doc, headers, data, footValues, style, space + (userData.allWeek ? 40 : 45),
245
+ {
246
+ 1: { halign: 'right' },
247
+ 2: { halign: 'right' },
248
+ 3: { halign: 'right' },
249
+ 4: { halign: 'right' },
250
+ 5: { halign: 'right' },
251
+ 6: { halign: 'right' }
252
+ })
239
253
  })
240
254
 
241
255
  return doc
@@ -271,7 +285,7 @@ function getTotalRefueling (user, data) {
271
285
  }
272
286
 
273
287
  function getTotalAvgConsumption (user, data) {
274
- const values = data.map(a => a.distance * a.avgConsumption.byKms).reduce((a, b) => a + b, 0)
288
+ const values = data.map(a => a.distance * (user.attributes.lang === 'pt-BR' ? a.avgConsumption.byLiters : a.avgConsumption.byKms)).reduce((a, b) => a + b, 0)
275
289
  const totalKms = data.reduce((a, b) => a + b.distance, 0)
276
290
 
277
291
  const totalAvgConsumption = values / totalKms
package/src/util/utils.js CHANGED
@@ -39,7 +39,7 @@ function convertMS (duration, withSeconds) {
39
39
  }
40
40
 
41
41
  exports.formatNumber = function formatNumber (value, lang) {
42
- return Intl.NumberFormat(lang, { maximumFractionDigits: 1 }).format(value)
42
+ return Intl.NumberFormat(lang, { maximumFractionDigits: 1, minimumFractionDigits: 1 }).format(value)
43
43
  }
44
44
 
45
45
  function coordsDistance (lon1, lat1, lon2, lat2) {