-
Notifications
You must be signed in to change notification settings - Fork 137
/
transplantingReport.html
620 lines (582 loc) · 31 KB
/
transplantingReport.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
<body>
<div id="transplantingReport" v-cloak>
<h1 class="text-center" data-cy="report-header">Transplanting Report</h1>
<error-banner data-cy="alert-err-handler" :visible="errBannerVisibility"></error-banner>
<fieldset :disabled="rowBeingEdited" class="panel panel-default center-block" style="width: 50%;">
<legend class="panel-heading" style="background-color: #d62a17; color: white;" data-cy="table-header">Set Dates</legend>
<div style="display: flex;width: 100%; padding: 7px">
<date-range-selection data-cy="date-range-selection" class="center-block" @start-date-changed='startDateChange' @end-date-changed='endDateChange'
@click='hide'
:set-start-date='startDate' :set-end-date='endDate'></date-range-selection>
<button data-cy="generate-rpt-btn" class="btn center-block btn-primary" v-if='!reportVisible' @click='seedingLogRequestWithDates'>Generate Report</button>
</div>
</fieldset>
<div v-if='reportVisible'>
<div v-if="transplantingLogs.length != 0">
<fieldset :disabled="rowBeingEdited" data-cy="filters-panel" class="center-block panel panel-default" style="width: 75%;">
<legend class="panel-heading" style="background-color: #d62a17; color: white;">Filters</legend>
<div style="display: flex; width: 100%;">
<dropdown-with-all data-cy="crop-dropdown" class="center-block" :dropdown-list='cropList' includes-all selected-val='All' @selection-changed='cropChange'>Crop:</dropdown-with-all>
<dropdown-with-all data-cy="area-dropdown" class="center-block" :dropdown-list='areaList' includes-all selected-val='All' @selection-changed='areaChange'>Area:</dropdown-with-all>
</div>
</fieldset>
<custom-table data-cy="report-table" :columns="tableColumns" :rows='tableRows' csv-name="transplantingReport_" @row-deleted="deleteRowLog" @row-edited='updateRow' @edit-clicked='disableFilters' @edit-canceled='enableFilters' can-edit can-delete></custom-table>
<br>
</div>
<div data-cy="loader" class="loader center-block" v-if="stillLoading"></div>
<div v-if="!stillLoading && transplantingLogs.length != 0">
<br>
<div style="display: flex; width: 100%;">
<fieldset data-cy="transplanting-summary" class="center-block panel panel-default" style="width: 50%;">
<legend class="panel-heading" style="background-color: #d62a17; color: white;">Transplanting Summary</legend>
<div>
<p style="margin-left: 5%;">Total Row Feet Planted: <b><span data-cy='transplant-total-rowft'>{{ totalRowFeet }}</span></b></p>
<p style="margin-left: 5%;">Total Bed Feet Planted: <b><span data-cy='transplant-total-bedft'>{{ totalBedFeet }}</span></b></p>
<p style="margin-left: 5%;">Total Number of Trays: <b><span data-cy='transplant-total-numTrays'>{{ totalNumTrays }}</span></b></p>
<p style="margin-left: 5%;" v-show="configToIDMap.labor != 'Hidden'">Total Hours: <b><span data-cy='transplant-total-hours'>{{ totalHours }}</span></b></p>
<p style="margin-left: 5%;" v-show="configToIDMap.labor != 'Hidden'">Average Row Feet/Hour: <b><span data-cy='transplant-total-rowft-hour'>{{ totalRowFeetPerHour }}</span></b></p>
<p style="margin-left: 5%;" v-show="configToIDMap.labor != 'Hidden'">Average Bed Feet/Hour: <b><span data-cy='transplant-total-bedfr-hour'>{{ totalBedFeetPerHour }}</span></b></p>
</div>
</fieldset>
</div>
</div>
<div v-if="!stillLoading && transplantingLogs.length == 0">
<div class="box" data-cy="no-logs-message" style="
border: 5px solid #d62a17;
background-color: white;
padding: 20px;
margin: auto;
width: 50%;
text-align: center;
font-size: large;">
No Logs Found in These Dates
</div>
</div>
</div>
<div data-cy="page-loaded" v-show=false>{{ pageLoaded }}</div>
</div>
<script>
var transplantingReport = new Vue ({
el: '#transplantingReport',
components: {
'custom-table': CustomTableComponent,
'date-range-selection': DateRangeSelectionComponent,
'dropdown-with-all': DropdownWithAllComponent,
'error-banner' : ErrorBannerComponent
},
data: {
startDate:dayjs().startOf('year').format('YYYY-MM-DD').toString(),
endDate: dayjs().format('YYYY-MM-DD').toString(),
stillLoading: true,
reportVisible: false,
transplantingLogs: [],
updateLog: [],
sessionToken: null,
selectedCrop: 'All',
selectedArea: 'All',
idToCropMap: new Map(),
cropToIDMap: new Map(),
idToUserMap: new Map(),
userToIDMap: new Map(),
areaToIDMap: new Map(),
unitToIDMap: new Map(),
configToIDMap: new Map(),
rowBeingEdited: false,
errBannerVisibility: false,
createdCount: 0,
},
methods: {
hide(){
this.reportVisible = false
this.transplantingLogs = []
},
startDateChange(selectedDate){
this.startDate = selectedDate
//this.reportVisible = false
//this.transplantingLogs = []
},
endDateChange(selectedDate){
this.endDate = selectedDate
//this.reportVisible = false
//this.transplantingLogs = []
},
seedingLogRequestWithDates(){
this.reportVisible = true
let link = '/log.json?type=farm_transplanting×tamp[ge]=' + dayjs(this.startDate).unix() + '×tamp[le]=' + dayjs(this.endDate).unix()
getAllPages(link, this.transplantingLogs)
.then(() => {
this.stillLoading = false
})
.catch((err) => {
this.errBannerVisibility = true
})
this.stillLoading = true
},
cropChange(selectedCrop){
this.selectedCrop = selectedCrop
},
areaChange(selectedArea){
this.selectedArea = selectedArea
},
/**
* updates the log that the user saved
*/
updateRow(updateObject, id){
this.updateLog = this.transplantingLogs.filter((x) => x.id === id)
let updateTransplantingObject = {}
let updatePlantObject = {}
/**
* The Table component returns an object with the cells that were updated. We don't know how many or wich ones where updated, so we check for all possible cells that could have been updated.
*/
let keys = Object.keys(updateObject)
if(keys.includes('Hours') || keys.includes('Workers') || keys.includes('Bed Feet') || keys.includes('Row Feet') ||
keys.includes('Rows/Bed') || keys.includes('Trays')){
updateTransplantingObject['quantity'] = this.quantityTransplantSeeding
}
for(i=0; i < keys.length; i++){
if(keys[i] == 'Date'){
updateTransplantingObject['timestamp'] = dayjs(updateObject['Date'], 'YYYY-MM-DD').unix()
this.updateLog[0].timestamp = dayjs(updateObject['Date'], 'YYYY-MM-DD').unix()
}
else if(keys[i] == 'Crop'){
updateTransplantingObject['data'] = {"crop_tid": this.cropToIDMap.get(updateObject['Crop'])}
updatePlantObject['crop'] = [{
"id": this.cropToIDMap.get(updateObject['crop']),
"resource": "taxonomy_term"
}]
this.updateLog[0].data = {"crop_tid": this.cropToIDMap.get(updateObject['Crop'])}
}
else if (keys[i] == 'Area'){
updateTransplantingObject['movement'] = { 'area': [{
"id": this.areaToIDMap.get(updateObject['Area']),
"resource": "taxonomy_term"}],
}
this.updateLog[0].movement = { 'area': [{
"uri": "http://localhost/taxonomy_term/" + this.areaToIDMap.get(updateObject['Area']),
"id": this.areaToIDMap.get(updateObject['Area']),
"resource": "taxonomy_term",
"name": updateObject['Area']}]
}
}
else if(keys[i] == 'Hours'){
updateTransplantingObject.quantity[quantityLocation(updateTransplantingObject.quantity, 'Labor')].value = updateObject['Hours']
this.updateLog[0].quantity[quantityLocation(this.updateLog[0].quantity, 'Labor')].value = updateObject['Hours']
}
else if(keys[i] == 'Workers'){
updateTransplantingObject.quantity[quantityLocation(updateTransplantingObject.quantity, 'Workers')].value = updateObject['Workers']
this.updateLog[0].quantity[quantityLocation(this.updateLog[0].quantity, 'Workers')].value = updateObject['Workers']
}
else if(keys[i] == 'Comments'){
updateTransplantingObject['notes'] = {'value': updateObject['Comments'],
'format': 'farm_format'}
this.updateLog[0].notes = {'value': updateObject['Comments'],
'format': 'farm_format'}
}
else if(keys[i] == 'User'){
updateTransplantingObject['uid'] = {
'id': this.userToIDMap.get(updateObject['User']),
'resource': 'user'
}
updatePlantObject['uid'] = {
'id': this.userToIDMap.get(updateObject['User']),
'resource': 'user'
}
this.updateLog[0].uid = {
'id': this.userToIDMap.get(updateObject['User']),
'resource': 'user'
}
}
else if(keys[i] == 'Row Feet'){
updateTransplantingObject.quantity[quantityLocation(updateTransplantingObject.quantity, 'Amount planted')].value = updateObject['Row Feet']
this.updateLog[0].quantity[quantityLocation(this.updateLog[0].quantity, 'Amount planted')].value = updateObject['Row Feet']
}
else if(keys[i] == 'Rows/Bed'){
updateTransplantingObject.quantity[quantityLocation(updateTransplantingObject.quantity, 'Rows/Bed')].value = updateObject['Rows/Bed']
this.updateLog[0].quantity[quantityLocation(this.updateLog[0].quantity, 'Rows/Bed')].value = updateObject['Rows/Bed']
}
else if(keys[i] == 'Trays'){
updateTransplantingObject.quantity[quantityLocation(updateTransplantingObject.quantity, 'Flats')].value = updateObject['Trays']
this.updateLog[0].quantity[quantityLocation(this.updateLog[0].quantity, 'Flats')].value = updateObject['Trays']
}
}
//replace old update log with current update log
if(keys.includes('Hours') || keys.includes('Workers') ||
keys.includes('Rows/Bed') || keys.includes('Row Feet') ||
keys.includes('Trays')){
for(var i=0; i < this.transplantingLogs.length; i++){
if(this.transplantingLogs[i].id === id){
this.transplantingLogs.splice(i, this.updateLog)
}
}
}
let transplantUrl = '/log/' + id
let plantingUrl = '/farm_asset/' + this.updateLog[0].asset[0].id
//call updateRecord function to update planting log
if(!(Object.entries(updatePlantObject).length === 0)){
updateRecord(plantingUrl, updatePlantObject, this.sessionToken)
}
//call updateRecord function to update seeding log
if(!(Object.entries(updateTransplantingObject).length === 0)){
//updates quantities and area separately because backend doesn't allow for one updateRecord call with both changes
if(keys.includes("Area") && (keys.includes("Row Feet") || keys.includes("Bed Feet")
|| keys.includes("Trays") || keys.includes("Workers") || keys.includes("Hours"))){
delete updateTransplantingObject.movement
let updateArea = {"movement":{"area":[{"id":this.areaToIDMap.get(updateObject['Area']),"resource":"taxonomy_term"}]}}
updateRecord(transplantUrl, updateArea, this.sessionToken)
.then(() =>{
updateRecord(transplantUrl, updateTransplantingObject, this.sessionToken)
})
}
else{
updateRecord(transplantUrl, updateTransplantingObject, this.sessionToken)
}
}
this.rowBeingEdited = false
},
/**
* Uses deleteRecord function to delete the record in the database. Also deletes the record from the current logs that are in transplantingLogs
*/
deleteRowLog(logID) {
for(let i = 0; i < logID.length; i++) {
let url = '/log/' + logID[i]
deleteRecord(url, this.sessionToken)
.then(() => {
for(let j = 0; j < this.seedingLogs.length; j++){
if(this.seedingLogs[j].id == logID[i]){
this.seedingLogs.splice(j, 1)
}
}
})
.catch((err => {
this.errBannerVisibility = true
}))
}
},
disableFilters() {
this.rowBeingEdited = true
},
enableFilters(){
this.rowBeingEdited = false
},
},
computed: {
/**
* tableRows() filters though transplantingLogs to get all the logs that have the correct crop, area, and/or type of seeding that has been selected by the user. Then takes the remaining logs and formats it so that the table component gets the necessary info
*/
tableRows() {
let filterRows = this.transplantingLogs.filter((x) => x)
if(this.selectedArea != 'All'){
filterRows = filterRows.filter((x) => x.movement.area[0].name === this.selectedArea)
}
if(this.selectedCrop != 'All'){
filterRows = filterRows.filter((x) => this.idToCropMap.get(x.data.crop_tid) === this.selectedCrop)
}
let $vm = this
let rows = filterRows.map(function(h) {
let rowFeet = h.quantity[0].value
let rowPerBed = h.quantity[1].value
let bedFeet = rowFeet/rowPerBed // calculation for bed feet
let numOfTrays = h.quantity[2].value
let hours = h.quantity[3].value
let numWorkers = h.quantity[4].value
return{
id: h.id,
data: [
dayjs.unix(h.timestamp).format('YYYY-MM-DD'),
$vm.idToCropMap.get(h.data.crop_tid),
h.movement.area[0].name,
rowFeet,
bedFeet,
rowPerBed,
numOfTrays,
(Math.round(numWorkers*100))/100,
((Math.round(hours*100))/100).toFixed(2),
h.notes.value,
$vm.idToUserMap.get(h.uid.id)
]
}
})
rows.sort(function compareNumbers(a, b) {
return new Date(a.data[0]).getTime() - new Date(b.data[0]).getTime();
})
return rows
},
/**
* returns a sorted array of all the crops in the current logs. If area does not equal 'All' then the array will only contain crops that have the selected area
*/
cropList(){
let cropArray = []
if(this.selectedArea != 'All'){
let areaFilter = this.transplantingLogs.filter((x) => x.movement.area[0].name === this.selectedArea)
cropArray = areaFilter.map(h =>
this.idToCropMap.get(h.data.crop_tid)
)
}
else{
cropArray = this.transplantingLogs.map(h=>
this.idToCropMap.get(h.data.crop_tid)
)
}
let crops = new Set(cropArray)
let newCropArray = Array.from(crops)
return newCropArray.sort()
},
/**
* returns a sorted array of all the areas in the current logs. If a crop does not equal 'All' then the array will only contain areas that have the selected crop
*/
areaList(){
let areaArray = []
if(this.selectedCrop != 'All'){
let cropFilter = this.transplantingLogs.filter((x) => this.idToCropMap.get(x.data.crop_tid) === this.selectedCrop)
areaArray = cropFilter.map(h =>
h.movement.area[0].name
)
}
else{
areaArray = this.transplantingLogs.map(h =>
h.movement.area[0].name
)
}
let areas = new Set(areaArray)
let newAreaArray = Array.from(areas)
return newAreaArray.sort()
},
/**
* returns the total row feet for Direct Seeding
*/
totalRowFeet(){
let tableData = this.tableRows
let sumRowFeet = 0
tableData.map(h => {
sumRowFeet = sumRowFeet + parseInt(h.data[4])
})
return (Math.round(sumRowFeet*100))/100
},
/**
* returns the total bed feet for Direct Seeding
*/
totalBedFeet(){
let tableData = this.tableRows
let sumBedFeet = 0
tableData.map(h => {
sumBedFeet = sumBedFeet + parseInt(h.data[3])
})
return sumBedFeet
},
/**
* returns the total hours worked for direct seedings. Calculated by mulpitplying hours by workers for each log
*/
totalHours(){
let tableData = this.tableRows
let sumHours = 0
tableData.map(h => {
hours = parseFloat(h.data[7]);
sumHours = sumHours + (h.data[7]*parseFloat(h.data[8]))
})
return (Math.round(sumHours*100))/100
},
/**
* returns the bed feet per hour for the direct seedings. Calculated by dividing total bedfeet by total hours
*/
totalBedFeetPerHour(){
let bedFeet = this.totalBedFeet
let hours = this.totalHours
if (hours == 0) {
return "N/A"
}
return (Math.round((bedFeet/hours)*100))/100
},
/**
* returns the row feet per hour for the direct seedings. Calculated by dividing total rowfeet by total hours
*/
totalRowFeetPerHour(){
let rowFeet = this.totalRowFeet
let hours = this.totalHours
if (hours == 0) {
return "N/A"
}
return (Math.round((rowFeet/hours)*100))/100
},
/**
* returns the total number of trays(flats) for the Tray Seedings logs
*/
totalNumTrays(){
let tableData = this.tableRows
let totalTrays = 0
tableData.map(h =>
totalTrays = totalTrays + parseFloat(h.data[6])
)
return (Math.round(totalTrays*100))/100
},
/**
* returns the structure for updating the quantity section of the Transplanting Seeding Logs. The values have not been updated yet.
*/
quantityTransplantSeeding(){
return [
{
"measure": "length",
"value": this.updateLog[0].quantity[0].value,
"unit": {
"id": this.unitToIDMap.get('ROW FEET'),
"resource": "taxonomy_term"
},
"label": "Amount planted"
},
{
"measure": "ratio",
"value": this.updateLog[0].quantity[1].value,
"unit": {
"id": this.unitToIDMap.get('ROWS/BED'),
"resource": "taxonomy_term"
},
"label": "Rows/Bed"
},
{
"measure": "count",
"value": this.updateLog[0].quantity[2].value,
"unit": {
"id": this.unitToIDMap.get('FLATS'),
"resource": "taxonomy_term"
},
"label": "Flats"
},
{
"measure": "time",
"value": this.updateLog[0].quantity[3].value,
"unit": {
"id": this.unitToIDMap.get('HOURS'),
"resource": "taxonomy_term"
},
"label": "Labor"
},
{
"measure": "count",
"value": this.updateLog[0].quantity[4].value,
"unit": {
"id": this.unitToIDMap.get('PEOPLE'),
"resource": "taxonomy_term"
},
"label": "Workers"
},
]
},
userNameArray(){
let userNameArray = []
const userKeyIterator = this.userToIDMap.keys()
for(i = 0; i < this.userToIDMap.size; i++){
userNameArray.push(userKeyIterator.next().value)
}
return userNameArray
},
areaNameArray(){
let areaNameArray = []
const areaKeyIterator = this.areaToIDMap.keys()
for(i = 0; i < this.areaToIDMap.size; i++){
areaNameArray.push(areaKeyIterator.next().value)
}
return areaNameArray
},
cropNameArray(){
let cropNameArray = []
for(let [key, info] of this.cropToIDMap){
cropNameArray.push(key)
}
return cropNameArray
},
/**
* returns the input options for each column in the table. Uses the maps in created to get the all crops, areas, and users in the database, in order to create respective dropdowns.
*/
tableColumns(){
return [
{"header": 'Date', "visible": true, "inputType" : {'type': 'date'}},
{"header": 'Crop', "visible": true, "inputType" : {'type': 'dropdown', value: this.cropNameArray}},
{"header": 'Area', "visible": true, "inputType" : {'type': 'dropdown', value: this.areaNameArray}},
{"header": 'Row Feet', "visible": true, "inputType" : {'type': 'regex', 'regex': '^[1-9]+[0-9]*$'}},
{"header": 'Bed Feet', "visible": true, "inputType" : {'type': 'no input'}},
{"header": 'Rows/Bed', "visible": true, "inputType" : {'type': 'regex', 'regex': '^[1-9]+[0-9]*$'}},
{"header": 'Trays', "visible": true, "inputType" : {'type': 'regex', 'regex': '^[1-9]+[0-9]*$'}},
{"header": 'Workers', "visible": true, "inputType" : {'type': 'regex', 'regex': '^[1-9]+[0-9]*$'}},
{"header": 'Hours', "visible": true, "inputType" : {'type': 'regex', 'regex': '^[1-9]+[0-9]*([.][0-9]{1,2}){0,1}$|^[0]{0,1}[.][1-9][0-9]{0,1}$|^[0]{0,1}[1-9]$|^[1-9]+[0-9]*\.$'}},
{"header": 'Comments', "visible": true, "inputType" : {'type': 'text'}},
{"header": 'User', "visible": true, "inputType" : {'type': 'dropdown', 'value': this.userNameArray}},
]
},
pageLoaded() {
// Check here that the correct number of API calls have completed.
return this.createdCount == 8
},
},
created() {
getSessionToken()
.then((response) => {
this.sessionToken = response
this.createdCount++
})
.catch((err) => {
this.errBannerVisibility = true
})
getIDToCropMap()
.then((response) => {
this.idToCropMap = new Map(response)
this.createdCount++
})
.catch((err) => {
this.errBannerVisibility = true
})
getCropToIDMap()
.then((response) => {
this.cropToIDMap = new Map(response)
this.createdCount++
})
.catch((err) => {
this.errBannerVisibility = true
})
getIDToUserMap()
.then((response) => {
this.idToUserMap = new Map (response)
this.createdCount++
})
.catch((err) => {
this.errBannerVisibility = true
})
getUserToIDMap()
.then((response) => {
this.userToIDMap = new Map (response)
this.createdCount++
})
.catch((err) => {
this.errBannerVisibility = true
})
getAreaToIDMap()
.then((response) => {
this.areaToIDMap = new Map(response)
this.createdCount++
})
.catch((err) => {
this.errBannerVisibility = true
})
getUnitToIDMap()
.then((response) => {
this.unitToIDMap = new Map(response)
this.createdCount++
})
.catch((err) => {
this.errBannerVisibility = true
})
getConfiguration()
.then((response) => {
this.configToIDMap = response.data
if(this.configToIDMap.labor == "Hidden") {
this.visibleColumns[7] = false
this.visibleColumns[8] = false
}
this.createdCount++
})
.catch((err) => {
this.errBannerVisibility = true
})
}
})
Vue.config.devtools = true;
</script>
</body>