|
1
|
|
|
import React, { Fragment, useEffect, useState } from 'react'; |
|
2
|
|
|
import { |
|
3
|
|
|
Breadcrumb, |
|
4
|
|
|
BreadcrumbItem, |
|
5
|
|
|
Row, |
|
6
|
|
|
Col, |
|
7
|
|
|
Card, |
|
8
|
|
|
CardBody, |
|
9
|
|
|
Button, |
|
10
|
|
|
ButtonGroup, |
|
11
|
|
|
Form, |
|
12
|
|
|
FormGroup, |
|
13
|
|
|
Input, |
|
14
|
|
|
Label, |
|
15
|
|
|
CustomInput, |
|
16
|
|
|
Spinner, |
|
17
|
|
|
} from 'reactstrap'; |
|
18
|
|
|
import Datetime from 'react-datetime'; |
|
19
|
|
|
import moment from 'moment'; |
|
20
|
|
|
import loadable from '@loadable/component'; |
|
21
|
|
|
import Cascader from 'rc-cascader'; |
|
22
|
|
|
import { getCookieValue, createCookie } from '../../../helpers/utils'; |
|
23
|
|
|
import withRedirect from '../../../hoc/withRedirect'; |
|
24
|
|
|
import { withTranslation } from 'react-i18next'; |
|
25
|
|
|
import { toast } from 'react-toastify'; |
|
26
|
|
|
import ButtonIcon from '../../common/ButtonIcon'; |
|
27
|
|
|
import { APIBaseURL } from '../../../config'; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
const DetailedDataTable = loadable(() => import('../common/DetailedDataTable')); |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
const ShopfloorBatch = ({ setRedirect, setRedirectUrl, t }) => { |
|
34
|
|
|
let current_moment = moment(); |
|
35
|
|
|
useEffect(() => { |
|
36
|
|
|
let is_logged_in = getCookieValue('is_logged_in'); |
|
37
|
|
|
let user_name = getCookieValue('user_name'); |
|
38
|
|
|
let user_display_name = getCookieValue('user_display_name'); |
|
39
|
|
|
let user_uuid = getCookieValue('user_uuid'); |
|
40
|
|
|
let token = getCookieValue('token'); |
|
41
|
|
|
if (is_logged_in === null || !is_logged_in) { |
|
42
|
|
|
setRedirectUrl(`/authentication/basic/login`); |
|
43
|
|
|
setRedirect(true); |
|
44
|
|
|
} else { |
|
45
|
|
|
//update expires time of cookies |
|
46
|
|
|
createCookie('is_logged_in', true, 1000 * 60 * 60 * 8); |
|
47
|
|
|
createCookie('user_name', user_name, 1000 * 60 * 60 * 8); |
|
48
|
|
|
createCookie('user_display_name', user_display_name, 1000 * 60 * 60 * 8); |
|
49
|
|
|
createCookie('user_uuid', user_uuid, 1000 * 60 * 60 * 8); |
|
50
|
|
|
createCookie('token', token, 1000 * 60 * 60 * 8); |
|
51
|
|
|
} |
|
52
|
|
|
}); |
|
53
|
|
|
// State |
|
54
|
|
|
// Query Parameters |
|
55
|
|
|
const [selectedSpaceName, setSelectedSpaceName] = useState(undefined); |
|
56
|
|
|
const [selectedSpaceID, setSelectedSpaceID] = useState(undefined); |
|
57
|
|
|
const [shopfloorList, setShopfloorList] = useState([]); |
|
58
|
|
|
const [reportingPeriodBeginsDatetime, setReportingPeriodBeginsDatetime] = useState(current_moment.clone().startOf('month')); |
|
59
|
|
|
const [reportingPeriodEndsDatetime, setReportingPeriodEndsDatetime] = useState(current_moment); |
|
60
|
|
|
const [cascaderOptions, setCascaderOptions] = useState(undefined); |
|
61
|
|
|
|
|
62
|
|
|
// buttons |
|
63
|
|
|
const [submitButtonDisabled, setSubmitButtonDisabled] = useState(false); |
|
64
|
|
|
const [spinnerHidden, setSpinnerHidden] = useState(true); |
|
65
|
|
|
const [exportButtonHidden, setExportButtonHidden] = useState(true); |
|
66
|
|
|
|
|
67
|
|
|
//Results |
|
68
|
|
|
const [detailedDataTableColumns, setDetailedDataTableColumns] = useState( |
|
69
|
|
|
[{dataField: 'name', text: t('Name'), sort: true}, {dataField: 'space', text: t('Space'), sort: true}]); |
|
70
|
|
|
const [excelBytesBase64, setExcelBytesBase64] = useState(undefined); |
|
71
|
|
|
|
|
72
|
|
|
useEffect(() => { |
|
73
|
|
|
let isResponseOK = false; |
|
74
|
|
|
fetch(APIBaseURL + '/spaces/tree', { |
|
75
|
|
|
method: 'GET', |
|
76
|
|
|
headers: { |
|
77
|
|
|
"Content-type": "application/json", |
|
78
|
|
|
"User-UUID": getCookieValue('user_uuid'), |
|
79
|
|
|
"Token": getCookieValue('token') |
|
80
|
|
|
}, |
|
81
|
|
|
body: null, |
|
82
|
|
|
|
|
83
|
|
|
}).then(response => { |
|
84
|
|
|
console.log(response); |
|
85
|
|
|
if (response.ok) { |
|
86
|
|
|
isResponseOK = true; |
|
87
|
|
|
} |
|
88
|
|
|
return response.json(); |
|
89
|
|
|
}).then(json => { |
|
90
|
|
|
console.log(json); |
|
91
|
|
|
if (isResponseOK) { |
|
92
|
|
|
// rename keys |
|
93
|
|
|
json = JSON.parse(JSON.stringify([json]).split('"id":').join('"value":').split('"name":').join('"label":')); |
|
94
|
|
|
setCascaderOptions(json); |
|
95
|
|
|
// set the default selected space |
|
96
|
|
|
setSelectedSpaceName([json[0]].map(o => o.label)); |
|
97
|
|
|
setSelectedSpaceID([json[0]].map(o => o.value)); |
|
98
|
|
|
|
|
99
|
|
|
setSubmitButtonDisabled(false); |
|
100
|
|
|
setSpinnerHidden(true); |
|
101
|
|
|
} else { |
|
102
|
|
|
toast.error(json.description); |
|
103
|
|
|
} |
|
104
|
|
|
}).catch(err => { |
|
105
|
|
|
console.log(err); |
|
106
|
|
|
}); |
|
107
|
|
|
|
|
108
|
|
|
}, []); |
|
109
|
|
|
|
|
110
|
|
|
const labelClasses = 'ls text-uppercase text-600 font-weight-semi-bold mb-0'; |
|
111
|
|
|
|
|
112
|
|
|
let onSpaceCascaderChange = (value, selectedOptions) => { |
|
113
|
|
|
setSelectedSpaceName(selectedOptions.map(o => o.label).join('/')); |
|
114
|
|
|
setSelectedSpaceID(value[value.length - 1]); |
|
115
|
|
|
setShopfloorList([]); |
|
116
|
|
|
setExportButtonHidden(true); |
|
117
|
|
|
setSubmitButtonDisabled(false); |
|
118
|
|
|
} |
|
119
|
|
|
let onReportingPeriodBeginsDatetimeChange = (newDateTime) => { |
|
120
|
|
|
setReportingPeriodBeginsDatetime(newDateTime); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
let onReportingPeriodEndsDatetimeChange = (newDateTime) => { |
|
124
|
|
|
setReportingPeriodEndsDatetime(newDateTime); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
var getValidReportingPeriodBeginsDatetimes = function (currentDate) { |
|
128
|
|
|
return currentDate.isBefore(moment(reportingPeriodEndsDatetime, 'MM/DD/YYYY, hh:mm:ss a')); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
var getValidReportingPeriodEndsDatetimes = function (currentDate) { |
|
132
|
|
|
return currentDate.isAfter(moment(reportingPeriodBeginsDatetime, 'MM/DD/YYYY, hh:mm:ss a')); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
// Handler |
|
136
|
|
|
const handleSubmit = e => { |
|
137
|
|
|
e.preventDefault(); |
|
138
|
|
|
console.log('handleSubmit'); |
|
139
|
|
|
console.log(selectedSpaceID); |
|
140
|
|
|
console.log(reportingPeriodBeginsDatetime.format('YYYY-MM-DDTHH:mm:ss')); |
|
141
|
|
|
console.log(reportingPeriodEndsDatetime.format('YYYY-MM-DDTHH:mm:ss')); |
|
142
|
|
|
|
|
143
|
|
|
// disable submit button |
|
144
|
|
|
setSubmitButtonDisabled(true); |
|
145
|
|
|
// show spinner |
|
146
|
|
|
setSpinnerHidden(false); |
|
147
|
|
|
// hide export buttion |
|
148
|
|
|
setExportButtonHidden(true) |
|
149
|
|
|
|
|
150
|
|
|
// Reinitialize tables |
|
151
|
|
|
setShopfloorList([]); |
|
152
|
|
|
|
|
153
|
|
|
let isResponseOK = false; |
|
154
|
|
|
fetch(APIBaseURL + '/reports/shopfloorbatch?' + |
|
155
|
|
|
'spaceid=' + selectedSpaceID + |
|
156
|
|
|
'&reportingperiodstartdatetime=' + reportingPeriodBeginsDatetime.format('YYYY-MM-DDTHH:mm:ss') + |
|
157
|
|
|
'&reportingperiodenddatetime=' + reportingPeriodEndsDatetime.format('YYYY-MM-DDTHH:mm:ss'), { |
|
158
|
|
|
method: 'GET', |
|
159
|
|
|
headers: { |
|
160
|
|
|
"Content-type": "application/json", |
|
161
|
|
|
"User-UUID": getCookieValue('user_uuid'), |
|
162
|
|
|
"Token": getCookieValue('token') |
|
163
|
|
|
}, |
|
164
|
|
|
body: null, |
|
165
|
|
|
|
|
166
|
|
|
}).then(response => { |
|
167
|
|
|
if (response.ok) { |
|
168
|
|
|
isResponseOK = true; |
|
169
|
|
|
}; |
|
170
|
|
|
return response.json(); |
|
171
|
|
|
}).then(json => { |
|
172
|
|
|
if (isResponseOK) { |
|
173
|
|
|
console.log(json) |
|
174
|
|
|
let shopfloors = []; |
|
175
|
|
|
if (json['shopfloors'].length > 0) { |
|
176
|
|
|
json['shopfloors'].forEach((currentShopfloor, index) => { |
|
177
|
|
|
let detailed_value = {}; |
|
178
|
|
|
detailed_value['id'] = currentShopfloor['id']; |
|
179
|
|
|
detailed_value['name'] = currentShopfloor['shopfloor_name']; |
|
180
|
|
|
detailed_value['space'] = currentShopfloor['space_name']; |
|
181
|
|
|
detailed_value['costcenter'] = currentShopfloor['cost_center_name']; |
|
182
|
|
|
currentShopfloor['values'].forEach((currentValue, energyCategoryIndex) => { |
|
183
|
|
|
detailed_value['a' + energyCategoryIndex] = currentValue.toFixed(2); |
|
184
|
|
|
}); |
|
185
|
|
|
shopfloors.push(detailed_value); |
|
186
|
|
|
}); |
|
187
|
|
|
}; |
|
188
|
|
|
|
|
189
|
|
|
setShopfloorList(shopfloors); |
|
190
|
|
|
|
|
191
|
|
|
let detailed_column_list = []; |
|
192
|
|
|
detailed_column_list.push({ |
|
193
|
|
|
dataField: 'name', |
|
194
|
|
|
text: t('Name'), |
|
195
|
|
|
sort: true |
|
196
|
|
|
}); |
|
197
|
|
|
detailed_column_list.push({ |
|
198
|
|
|
dataField: 'space', |
|
199
|
|
|
text: t('Space'), |
|
200
|
|
|
sort: true |
|
201
|
|
|
}); |
|
202
|
|
|
json['energycategories'].forEach((currentValue, index) => { |
|
203
|
|
|
detailed_column_list.push({ |
|
204
|
|
|
dataField: 'a' + index, |
|
205
|
|
|
text: currentValue['name'] + ' (' + currentValue['unit_of_measure'] + ')', |
|
206
|
|
|
sort: true |
|
207
|
|
|
}) |
|
208
|
|
|
}); |
|
209
|
|
|
setDetailedDataTableColumns(detailed_column_list); |
|
210
|
|
|
|
|
211
|
|
|
setExcelBytesBase64(json['excel_bytes_base64']); |
|
212
|
|
|
|
|
213
|
|
|
// enable submit button |
|
214
|
|
|
setSubmitButtonDisabled(false); |
|
215
|
|
|
// hide spinner |
|
216
|
|
|
setSpinnerHidden(true); |
|
217
|
|
|
// show export buttion |
|
218
|
|
|
setExportButtonHidden(false); |
|
219
|
|
|
|
|
220
|
|
|
} else { |
|
221
|
|
|
toast.error(json.description) |
|
222
|
|
|
} |
|
223
|
|
|
}).catch(err => { |
|
224
|
|
|
console.log(err); |
|
225
|
|
|
}); |
|
226
|
|
|
}; |
|
227
|
|
|
|
|
228
|
|
|
const handleExport = e => { |
|
229
|
|
|
e.preventDefault(); |
|
230
|
|
|
const mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' |
|
231
|
|
|
const fileName = 'shopfloorbatch.xlsx' |
|
232
|
|
|
var fileUrl = "data:" + mimeType + ";base64," + excelBytesBase64; |
|
233
|
|
|
fetch(fileUrl) |
|
234
|
|
|
.then(response => response.blob()) |
|
235
|
|
|
.then(blob => { |
|
236
|
|
|
var link = window.document.createElement("a"); |
|
237
|
|
|
link.href = window.URL.createObjectURL(blob, { type: mimeType }); |
|
238
|
|
|
link.download = fileName; |
|
239
|
|
|
document.body.appendChild(link); |
|
240
|
|
|
link.click(); |
|
241
|
|
|
document.body.removeChild(link); |
|
242
|
|
|
}); |
|
243
|
|
|
}; |
|
244
|
|
|
|
|
245
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
return ( |
|
248
|
|
|
<Fragment> |
|
249
|
|
|
<div> |
|
250
|
|
|
<Breadcrumb> |
|
251
|
|
|
<BreadcrumbItem>{t('Shopfloor Data')}</BreadcrumbItem><BreadcrumbItem active>{t('Batch Analysis')}</BreadcrumbItem> |
|
252
|
|
|
</Breadcrumb> |
|
253
|
|
|
</div> |
|
254
|
|
|
<Card className="bg-light mb-3"> |
|
255
|
|
|
<CardBody className="p-3"> |
|
256
|
|
|
<Form onSubmit={handleSubmit}> |
|
257
|
|
|
<Row form> |
|
258
|
|
|
<Col xs={6} sm={3}> |
|
259
|
|
|
<FormGroup className="form-group"> |
|
260
|
|
|
<Label className={labelClasses} for="space"> |
|
261
|
|
|
{t('Space')} |
|
262
|
|
|
</Label> |
|
263
|
|
|
<br /> |
|
264
|
|
|
<Cascader options={cascaderOptions} |
|
265
|
|
|
onChange={onSpaceCascaderChange} |
|
266
|
|
|
changeOnSelect |
|
267
|
|
|
expandTrigger="hover"> |
|
268
|
|
|
<Input value={selectedSpaceName || ''} readOnly /> |
|
269
|
|
|
</Cascader> |
|
270
|
|
|
</FormGroup> |
|
271
|
|
|
</Col> |
|
272
|
|
|
|
|
273
|
|
|
<Col xs={6} sm={3}> |
|
274
|
|
|
<FormGroup className="form-group"> |
|
275
|
|
|
<Label className={labelClasses} for="reportingPeriodBeginsDatetime"> |
|
276
|
|
|
{t('Reporting Period Begins')} |
|
277
|
|
|
</Label> |
|
278
|
|
|
<Datetime id='reportingPeriodBeginsDatetime' |
|
279
|
|
|
value={reportingPeriodBeginsDatetime} |
|
280
|
|
|
onChange={onReportingPeriodBeginsDatetimeChange} |
|
281
|
|
|
isValidDate={getValidReportingPeriodBeginsDatetimes} |
|
282
|
|
|
closeOnSelect={true} /> |
|
283
|
|
|
</FormGroup> |
|
284
|
|
|
</Col> |
|
285
|
|
|
<Col xs={6} sm={3}> |
|
286
|
|
|
<FormGroup className="form-group"> |
|
287
|
|
|
<Label className={labelClasses} for="reportingPeriodEndsDatetime"> |
|
288
|
|
|
{t('Reporting Period Ends')} |
|
289
|
|
|
</Label> |
|
290
|
|
|
<Datetime id='reportingPeriodEndsDatetime' |
|
291
|
|
|
value={reportingPeriodEndsDatetime} |
|
292
|
|
|
onChange={onReportingPeriodEndsDatetimeChange} |
|
293
|
|
|
isValidDate={getValidReportingPeriodEndsDatetimes} |
|
294
|
|
|
closeOnSelect={true} /> |
|
295
|
|
|
</FormGroup> |
|
296
|
|
|
</Col> |
|
297
|
|
|
<Col xs="auto"> |
|
298
|
|
|
<FormGroup> |
|
299
|
|
|
<br></br> |
|
300
|
|
|
<ButtonGroup id="submit"> |
|
301
|
|
|
<Button color="success" disabled={submitButtonDisabled} >{t('Submit')}</Button> |
|
302
|
|
|
</ButtonGroup> |
|
303
|
|
|
</FormGroup> |
|
304
|
|
|
</Col> |
|
305
|
|
|
<Col xs="auto"> |
|
306
|
|
|
<FormGroup> |
|
307
|
|
|
<br></br> |
|
308
|
|
|
<Spinner color="primary" hidden={spinnerHidden} /> |
|
309
|
|
|
</FormGroup> |
|
310
|
|
|
</Col> |
|
311
|
|
|
<Col xs="auto"> |
|
312
|
|
|
<br></br> |
|
313
|
|
|
<ButtonIcon icon="external-link-alt" transform="shrink-3 down-2" color="falcon-default" |
|
314
|
|
|
hidden={exportButtonHidden} |
|
315
|
|
|
onClick={handleExport} > |
|
316
|
|
|
{t('Export')} |
|
317
|
|
|
</ButtonIcon> |
|
318
|
|
|
</Col> |
|
319
|
|
|
</Row> |
|
320
|
|
|
</Form> |
|
321
|
|
|
</CardBody> |
|
322
|
|
|
</Card> |
|
323
|
|
|
<DetailedDataTable data={shopfloorList} title={t('Detailed Data')} columns={detailedDataTableColumns} pagesize={50} > |
|
324
|
|
|
</DetailedDataTable> |
|
325
|
|
|
|
|
326
|
|
|
</Fragment> |
|
327
|
|
|
); |
|
328
|
|
|
}; |
|
329
|
|
|
|
|
330
|
|
|
export default withTranslation()(withRedirect(ShopfloorBatch)); |
|
331
|
|
|
|