import React, { Component } from 'react';
import { Typography, Paper, createMuiTheme, MuiThemeProvider, TableCell, FormControlLabel, TextField, Input, Snackbar, withStyles, Tooltip } from '@material-ui/core';
import MUIDataTable from 'mui-datatables';
import NumberFormat from 'react-number-format';
import api from '../../api';
import AddIcon from '@material-ui/icons/Add';
import { values } from 'ramda';
import PropagateLoader from "react-spinners/PropagateLoader"
import Images from '../../assets/Images';
import ReactTooltip from 'react-tooltip';
import UploadFile from "../../library/Upload";
import { ExcelRenderer } from 'react-excel-renderer';
import { Alert } from '@material-ui/lab';
import Constant from '../../library/Constant';
var ct = require("../../library/CustomTable");
const getMuiTheme = () => createMuiTheme(ct.customTable3());
const options = ct.customOptionsFixedColumn();
const style = {
position: "sticky",
left: 0,
zIndex: 101,
background: "white",
};
const style2 = {
position: "sticky",
background: "white",
zIndex: 100
};
const LightTooltip = withStyles((theme) => ({
tooltip: {
backgroundColor: theme.palette.common.white,
color: 'rgba(0, 0, 0, 0.87)',
boxShadow: theme.shadows[1],
fontSize: 11,
},
}))(Tooltip);
export default class OperatingIndicatorRO extends Component {
constructor(props) {
super(props)
this.state = {
dataTable: [],
loading: true,
visibleOI: true,
updateBy: '-',
notesUpdate: '-',
disabledSave: true,
editable: true,
emptyData: false,
templateNull: true,
judulColumn: null,
saveDraft: true,
viewOnly: true,
get_for: 'view'
}
this.handleValue = this.handleValue.bind(this)
this.fileHandler = this.fileHandler.bind(this);
}
componentDidMount() {
this.getOperatingIDRolling()
this.handleViewOnly()
// // console.log(this.props);
}
handleViewOnly() {
let checkCreate = this.props.permission.create
let checkEdit = this.props.permission.edit
let checkStatus = true
console.log(this.props.data.status);
if (String(this.props.data.status).toLocaleUpperCase() === 'CLOSED') {
checkStatus = false
}
console.log(checkStatus);
this.setState({ viewOnly: checkCreate && checkEdit && checkStatus})
}
handleGetFor(type) {
this.setState({ get_for: type }, () => {
// this.getLatestUpdate()
this.getOperatingIDRolling()
})
}
getOperatingIDRolling() {
// console.log(this.props.data.operatingIndID);
let payload = {
"company_id": this.props.data.company.company_id,
"periode": this.props.data.periode,
"quartal": this.props.quartal
}
console.log(payload);
api.create().getRollingOI(payload).then(response => {
console.log(response);
if (response) {
if (response.data.data) {
this.setState({ operatingIndIDRolling: response.data.data.operating_indicator_id }, () => {
this.getDataDetail()
this.getLatestUpdate()
})
} else {
this.setState({ operatingIndIDRolling: null }, () => {
this.getDataDetail()
this.getLatestUpdate()
})
}
}
})
}
getLatestUpdate() {
let payload = {
"operating_indicator_id": this.state.operatingIndIDRolling,
"report_id": this.props.data.report_id,
"company_id": this.props.data.company.company_id,
"periode": this.props.data.periode,
"quartal": this.props.quartal
}
// console.log(payload);
api.create().getLastestUpdateROOI(payload).then(response => {
// console.log(response.data)
if (response.data) {
if (response.data.status === "success") {
this.setState({
updateBy: response.data.data.latest_update === null ? '-' : response.data.data.latest_update,
notesUpdate: response.data.data.notes_update === null ? '-' : response.data.data.notes_update
})
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning', loading: false }, () => {
if (response.data.message.includes("Someone Logged In")) {
setTimeout(() => {
localStorage.removeItem(Constant.TOKEN)
window.location.reload();
}, 1000);
}
})
}
} else {
this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error', loading: false })
}
})
}
getDataDetail() {
this.setState({ loading: true })
let payload = {
"operating_indicator_id": this.state.operatingIndIDRolling,
"report_id": this.props.data.report_id,
"company_id": this.props.data.company.company_id,
"periode": this.props.data.periode,
"quartal": this.props.quartal,
"get_for": "view"
}
console.log(payload)
api.create().getHierarkiRollingOI(payload).then(response => {
console.log(response)
let dataTable = []
let res = response.data.data
// // // console.log(res)
const handlePushChild = (item) => {
let indexIDzz = dataTable.findIndex((val) => val[1] === item.id)
if (indexIDzz === -1) {
dataTable.push([
item.type_report_id,
item.id,
item.parent,
item.formula,
item.level,
item.description,
item.uom,
item.rolling_outlook.january === "" ? "0" : item.rolling_outlook.january,
item.rolling_outlook.february === "" ? "0" : item.rolling_outlook.february,
item.rolling_outlook.march === "" ? "0" : item.rolling_outlook.march,
item.rolling_outlook.april === "" ? "0" : item.rolling_outlook.april,
item.rolling_outlook.may === "" ? "0" : item.rolling_outlook.may,
item.rolling_outlook.june === "" ? "0" : item.rolling_outlook.june,
item.rolling_outlook.july === "" ? "0" : item.rolling_outlook.july,
item.rolling_outlook.august === "" ? "0" : item.rolling_outlook.august,
item.rolling_outlook.september === "" ? "0" : item.rolling_outlook.september,
item.rolling_outlook.october === "" ? "0" : item.rolling_outlook.october,
item.rolling_outlook.november === "" ? "0" : item.rolling_outlook.november,
item.rolling_outlook.december === "" ? "0" : item.rolling_outlook.december,
item.rolling_outlook.total_current_year === "" ? "0" : item.rolling_outlook.total_current_year,
item.order
])
}
if (item.children !== null) {
if (item.children.length > 0) {
item.children.map((items, indexs) => {
handlePushChild(items)
})
}
}
}
res.map((item, index) => {
dataTable.push([
item.type_report_id,
item.id,
item.parent,
item.formula,
item.level,
item.description,
item.uom,
item.rolling_outlook.january === "" ? "0" : item.rolling_outlook.january,
item.rolling_outlook.february === "" ? "0" : item.rolling_outlook.february,
item.rolling_outlook.march === "" ? "0" : item.rolling_outlook.march,
item.rolling_outlook.april === "" ? "0" : item.rolling_outlook.april,
item.rolling_outlook.may === "" ? "0" : item.rolling_outlook.may,
item.rolling_outlook.june === "" ? "0" : item.rolling_outlook.june,
item.rolling_outlook.july === "" ? "0" : item.rolling_outlook.july,
item.rolling_outlook.august === "" ? "0" : item.rolling_outlook.august,
item.rolling_outlook.september === "" ? "0" : item.rolling_outlook.september,
item.rolling_outlook.october === "" ? "0" : item.rolling_outlook.october,
item.rolling_outlook.november === "" ? "0" : item.rolling_outlook.november,
item.rolling_outlook.december === "" ? "0" : item.rolling_outlook.december,
item.rolling_outlook.total_current_year === "" ? "0" : item.rolling_outlook.total_current_year,
item.order
])
if (item.children !== null) {
if (item.children.length > 0) {
item.children.map((items, indexs) => {
handlePushChild(items)
})
}
}
})
this.setState({ dataTable, loading: false, buttonError: true, saveDraft: true }, () => {
if (this.state.dataTable.length == 0) {
this.setState({ emptyData: true })
}
})
console.log(dataTable);
})
}
createData(type) {
let payloadRO = []
this.state.dataTable.map((item, index) => {
payloadRO.push({
"item_report_id": item[1],
"january": String(Number(item[7]).toFixed(1)),
"february": String(Number(item[8]).toFixed(1)),
"march": String(Number(item[9]).toFixed(1)),
"april": String(Number(item[10]).toFixed(1)),
"may": String(Number(item[11]).toFixed(1)),
"june": String(Number(item[12]).toFixed(1)),
"july": String(Number(item[13]).toFixed(1)),
"august": String(Number(item[14]).toFixed(1)),
"september": String(Number(item[15]).toFixed(1)),
"october": String(Number(item[16]).toFixed(1)),
"november": String(Number(item[17]).toFixed(1)),
"december": String(Number(item[18]).toFixed(1)),
"total_current_year": String(Number(item[19]).toFixed(1)),
})
})
let payload = {
"operating_indicator_id": this.state.operatingIndIDRolling,
"report_id": this.props.data.report_id,
"company_id": this.props.data.company.company_id,
"periode": this.props.data.periode,
"quartal": this.props.quartal,
"status": type,
"rolling_outlook": payloadRO
}
console.log(payload);
// // console.log('=========================')
// console.log(JSON.stringify(payload))
this.props.saveRollingOI(payload)
this.props.onClickClose()
}
handleValue(data) {
let total = 0
this.state.dataTable.map((item, index) => {
if (data.rowData[1] == item[2]) {
total = item[data.columnIndex] == undefined ? (Number(total) + 0) : (Number(total) + Number(item[data.columnIndex]))
}
})
let indexParent = this.state.dataTable.findIndex((val) => val[1] == this.state.dataTable[data.rowIndex][2])
let a = this.state.dataTable[data.rowIndex][data.columnIndex] = total
// // // console.log(indexParent);
return a
}
handleChange(value, tableMeta) {
let val = String(value).split(",").join("")
let data = this.state.dataTable
let indexParent = data.findIndex((val) => val[1] == data[tableMeta.rowIndex][2])
if (indexParent > 0) {
let a = data[tableMeta.rowIndex][tableMeta.columnIndex] = Number(val)
let jagain = data[indexParent][tableMeta.columnIndex]
a = data[indexParent][tableMeta.columnIndex] = jagain == undefined ? (0 + Number(val)) : (jagain + Number(val))
} else {
data[tableMeta.rowIndex][tableMeta.columnIndex] = Number(val)
}
}
backToMasterBudget(type) {
let data = []
this.state.dataTable.map(i => {
data.push({
item_report_id: i[1],
total_actual_before: String(i[6]),
january: String(i[7]),
february: String(i[8]),
march: String(i[9]),
april: String(i[10]),
may: String(i[11]),
june: String(i[12]),
july: String(i[13]),
august: String(i[14]),
september: String(i[15]),
october: String(i[16]),
november: String(i[17]),
december: String(i[18]),
total_current_year: String(i[19]),
total_next_year: String(i[20]),
total_more_year: String(i[21])
})
})
// let payload = {
// "submission_id": this.props.submissionID,
// "company_id": this.props.company.company_id,
// "periode": this.props.periode,
// "report_id": this.props.report_id,
// "status": type,
// "balance_sheet": data
// }
// // // // console.log(data);
// this.props.saveToMasterBudget(payload)
this.props.onClickClose()
}
downloadTemplate = async () => {
let res = await fetch(
`${process.env.REACT_APP_URL_MAIN_BE}/public/transaction/operating_indicator/rolling_outlook/download_template?report_id=${this.props.data.report_id}&&company_id=${this.props.data.company.company_id}&&year=${this.props.data.periode}&&quartal=${this.props.quartal}`
)
res = await res.blob()
// // console.log(res)
if (res.size > 0) {
let url = window.URL.createObjectURL(res);
let a = document.createElement('a');
a.href = url;
a.download = 'Template Rolling Outlook Operating Indicator.xlsx';
a.click();
}
setTimeout(() => {
this.setState({loading: false})
}, 500);
}
async downloadAllData() {
let url = `${process.env.REACT_APP_URL_MAIN_BE}/public/transaction/operating_indicator/rolling_outlook/export_rolling_outlook?operating_indicator_id=${this.state.operatingIndIDRolling == null ? "" : this.state.operatingIndIDRolling}&&report_id=${this.props.data.report_id}&&company_id=${this.props.data.company.company_id}&&year=${this.props.data.periode}&&quartal=${this.props.quartal}`
console.log(url);
let res = await fetch(
`${process.env.REACT_APP_URL_MAIN_BE}/public/transaction/operating_indicator/rolling_outlook/export_rolling_outlook?operating_indicator_id=${this.state.operatingIndIDRolling === null ? "" : this.state.operatingIndIDRolling}&&report_id=${this.props.data.report_id}&&company_id=${this.props.data.company.company_id}&&year=${this.props.data.periode}&&quartal=${this.props.quartal}`
)
res = await res.blob()
// // console.log(res)
this.setState({ loading: false })
if (res.size > 0) {
let url = window.URL.createObjectURL(res);
let a = document.createElement('a');
a.href = url;
a.download = 'Master Budget Operating Indicator.xlsx';
a.click();
}
setTimeout(() => {
this.setState({loading: false})
}, 500);
}
fileHandler = (event) => {
let fileObj = event
ExcelRenderer(fileObj, (err, resp) => {
// // // console.log(resp)
if (err) {
// // console.log(err);
}
else {
let isi = resp.rows.slice(3)
// // console.log(isi);
let payload = []
let reg = /^[-+]?(?:[0-9]+,)*[0-9]+(?:\.[0-9]+)?$/;
isi.map((i, index) => {
if (i.length > 0) {
payload.push({
order: i[0] === undefined ? "0" : String(i[0]).trim(),
item_report_id: i[1] === undefined ? "0" : String(i[1]).trim(),
item_report: i[2] === undefined ? "0" : String(i[2]).trim(),
uom: i[3] === undefined ? "" : String(i[3]).trim(),
january: i[4] === undefined ? "0" : reg.test(String(i[4])) === false ? "0" : String(i[4]).trim(),
february: i[5] === undefined ? "0" : reg.test(String(i[5])) === false ? "0" : String(i[5]).trim(),
march: i[6] === undefined ? "0" : reg.test(String(i[6])) === false ? "0" : String(i[6]).trim(),
april: i[7] === undefined ? "0" : reg.test(String(i[7])) === false ? "0" : String(i[7]).trim(),
may: i[8] === undefined ? "0" : reg.test(String(i[8])) === false ? "0" : String(i[8]).trim(),
june: i[9] === undefined ? "0" : reg.test(String(i[9])) === false ? "0" : String(i[9]).trim(),
july: i[10] === undefined ? "0" : reg.test(String(i[10])) === false ? "0" : String(i[10]).trim(),
august: i[11] === undefined ? "0" : reg.test(String(i[11])) === false ? "0" : String(i[11]).trim(),
september: i[12] === undefined ? "0" : reg.test(String(i[12])) === false ? "0" : String(i[12]).trim(),
october: i[13] === undefined ? "0" : reg.test(String(i[13])) === false ? "0" : String(i[13]).trim(),
november: i[14] === undefined ? "0" : reg.test(String(i[14])) === false ? "0" : String(i[14]).trim(),
december: i[15] === undefined ? "0" : reg.test(String(i[15])) === false ? "0" : String(i[15]).trim(),
total_current_year: i[16] === undefined ? "0" : reg.test(String(i[16])) === false ? "0" : String(i[16]).trim(),
})
}
})
let body = {
company_id: this.props.data.company.company_id,
operating_indicator_id: this.state.operatingIndIDRolling,
periode: this.props.data.periode,
quartal: this.props.quartal,
report_id: this.props.data.report_id,
rolling_outlook: payload,
status: 'submitted'
}
console.log(body)
console.log(resp.rows[1][0]);
this.setState({ payload: body, buttonError: false, judul: resp.rows[1][0], judulColumn: resp.rows[0][0] })
}
});
}
checkUpload() {
// // console.log(JSON.stringify(this.state.payload))
api.create().checkUploadRollingOutlookOI(this.state.payload).then(response => {
// // // console.log(JSON.stringify(this.state.payload));
// // console.log('===============')
console.log(response)
if (response.data) {
if (response.data.status === 'success') {
this.setState({ visibleUpload: false, visibleOI: false, loading: true })
let total = 0
let dataTable = response.data.data.map((item, index) => {
if (item.type_report_id != null) {
total += 1
}
return [
item.type_report_id,
item.item_report_id,
item.parent,
item.formula,
item.level,
item.item_report,
item.uom,
item.january,
item.february,
item.march,
item.april,
item.may,
item.june,
item.july,
item.august,
item.september,
item.october,
item.november,
item.december,
item.total_current_year,
item.order,
item.error
]
})
this.setState({ dataTable, dataLoaded: true, loading: false, templateNull: total > 0 ? true : false })
}
}
})
}
uploadOI(type) {
let data = []
// // console.log(this.state.dataTable)
this.state.dataTable.map(i => {
data.push({
item_report_id: i[1],
item_report: String(i[5]),
january: String(Number(i[7]).toFixed(1)),
february: String(Number(i[8]).toFixed(1)),
march: String(Number(i[9]).toFixed(1)),
april: String(Number(i[10]).toFixed(1)),
may: String(Number(i[11]).toFixed(1)),
june: String(Number(i[12]).toFixed(1)),
july: String(Number(i[13]).toFixed(1)),
august: String(Number(i[14]).toFixed(1)),
september: String(Number(i[15]).toFixed(1)),
october: String(Number(i[16]).toFixed(1)),
november: String(Number(i[17]).toFixed(1)),
december: String(Number(i[18]).toFixed(1)),
total_current_year: String(Number(i[19]).toFixed(1)),
})
})
let body = {
company_id: this.props.data.company.company_id,
operating_indicator_id: this.state.operatingIndIDRolling,
periode: this.props.data.periode,
quartal: this.props.quartal,
report_id: this.props.data.report_id,
status: type,
rolling_outlook: data
}
// // console.log(data);
// // console.log(JSON.stringify(body))
api.create('UPLOAD').uploadRollingOutlookOI(body).then(response => {
console.log(response);
this.setState({ loading: false })
if (response.data) {
if (response.data.status === "success") {
this.props.onClickClose()
this.props.getReport()
} else {
alert(response.data.status)
}
} else {
alert(response.problem)
}
})
}
closeAlert() {
this.setState({ alert: false })
}
render() {
let dataTable2 = this.state.dataTable
const handleChange = (value, tableMeta) => {
let val = String(value).split(",").join("")
let total = 0
dataTable2[tableMeta.rowIndex][tableMeta.columnIndex] = Number(val)
}
const handleValue = (value, tableMeta) => {
let val = String(value).split(",").join("")
let total = 0
dataTable2[tableMeta.rowIndex][tableMeta.columnIndex] = Number(val)
dataTable2[tableMeta.rowIndex].map((item, index) => {
if (index >= 7 && index <= 19) {
let valItem = item == undefined || item == "" ? 0 : item
total += Number(valItem)
}
})
dataTable2[tableMeta.rowIndex][19] = total
return total
}
const handleParent = (tableMeta) => {
// // console.log(tableMeta)
let total = 0
dataTable2.map((item, index) => {
if (item[2] == tableMeta.rowData[1]) {
// // console.log(total)
// // console.log(item[tableMeta.columnIndex])
total += Number(item[tableMeta.columnIndex])
}
})
dataTable2[tableMeta.rowIndex][tableMeta.columnIndex] = total
return total
}
const handleFormula = (tableMeta) => {
let arrayFormula = tableMeta.rowData[3].match(/[a-zA-Z]+|[0-9]+|[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]+(?:\.[0-9]+@|)/g)
let arrayFormulaConvert = []
// // // console.log(arrayFormula)
let opet = ""
let tambahan = false
arrayFormula.map((item, indexs) => {
if (item == 'X') {
tambahan = true
} else if (item == '-' || item == '+' || item == '/' || item == '*') {
arrayFormulaConvert.push(item)
} else {
let indexID = dataTable2.findIndex((val) => val[20] == item)
if (tambahan) {
if (item == '-' || item == '+' || item == '/' || item == '*') {
opet = item
} else {
arrayFormulaConvert.push(opet == '' ? Number(item) : Number(String(opet + String(item))))
tambahan = false
opet = ""
}
} else {
if (indexID !== -1) {
let valuezz = dataTable2[indexID][tableMeta.columnIndex]
// // // console.log(dataTable2[indexID])
arrayFormulaConvert.push(valuezz == "" ? 0 : valuezz)
}
}
}
})
// // console.log(arrayFormula)
// // console.log(arrayFormulaConvert)
let array = arrayFormulaConvert
let total = 0
let opt = ""
array.map((item, index) => {
if (item == "+") {
opt = "tambah"
} else if (item == "-") {
opt = "kurang"
} else if (item == "*") {
opt = "kali"
} else if (item == "/") {
opt = "bagi"
} else {
if (opt == "tambah") {
total = Number(total) + Number(item)
} else if (opt == "kurang") {
total = Number(total) - Number(item)
} else if (opt == "kali") {
total = Number(total) * Number(item)
} else if (opt == "bagi") {
total = Number(total) / Number(item)
} else {
total += Number(item)
}
}
})
let a = dataTable2[tableMeta.rowIndex][tableMeta.columnIndex] = Number(total)
return a
}
const columns = [{
name: "",
options: {
display: false
}
}, {
name: "",
options: {
display: false
}
}, {
name: "",
options: {
display: false
}
}, {
name: "",
options: {
display: false
}
}, {
name: "",
options: {
display: false
}
}, {
name: "Account",
options: {
customHeadRender: (columnMeta) => (