SubHolding.js 45.3 KB
Newer Older
faisalhamdi's avatar
faisalhamdi committed
1
import React, { Component } from 'react'
faisalhamdi's avatar
faisalhamdi committed
2
import { Typography, MenuItem, TextField, AppBar, Paper, Tabs, Tab, Snackbar, withStyles } from '@material-ui/core'
Deni Rinaldi's avatar
Deni Rinaldi committed
3 4 5 6 7 8
import Constant from '../../library/Constant'
import api from '../../api'
import { titleCase } from '../../library/Utils'
import { Autocomplete } from '@material-ui/lab'
import TableSubHolding from './TableSubHolding'
import { PropagateLoader } from 'react-spinners'
Deni Rinaldi's avatar
Deni Rinaldi committed
9 10
import ReactTooltip from 'react-tooltip'
import Images from '../../assets/Images'
faisalhamdi's avatar
faisalhamdi committed
11 12 13 14
import MuiAlert from '@material-ui/lab/Alert';

const Alert = withStyles({
})((props) => <MuiAlert elevation={6} variant="filled" {...props} />);
faisalhamdi's avatar
faisalhamdi committed
15 16 17 18 19 20 21 22

export default class SubHolding extends Component {
    constructor(props) {
        super(props)
        this.state = {
            periode: '2020',
            perusahaan: 'TAP Group',
            laporan: 'Balance Sheet - Montly',
Deni Rinaldi's avatar
Deni Rinaldi committed
23 24 25 26 27 28
            tab: 0,
            listCompany: null,
            company: null,
            listPeriode: null,
            periode: null,
            reportType: [
idlanirined's avatar
idlanirined committed
29
                { value: 2, label: 'DB Balance Sheet' },
Deni Rinaldi's avatar
Deni Rinaldi committed
30
                { value: 3, label: 'DB Profit & Loss' },
idlanirined's avatar
idlanirined committed
31
                { value: 1, label: 'DB Profit & Loss Detail' },
Deni Rinaldi's avatar
Deni Rinaldi committed
32
                { value: 4, label: 'DB Ratio' },
Deni Rinaldi's avatar
Deni Rinaldi committed
33 34
            ],
            report: null,
Deni Rinaldi's avatar
Deni Rinaldi committed
35 36
            loading: false,
            previewTable: false
faisalhamdi's avatar
faisalhamdi committed
37 38 39 40 41 42 43
        }
    }

    selectTab = (event, newEvent) => {
        this.setState({ tab: newEvent })
    }

Deni Rinaldi's avatar
Deni Rinaldi committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    componentDidMount() {
        this.getDetailUser()
        this.setState({ report: this.state.reportType[0], loading: true })
    }

    getDetailUser() {
        let userId = localStorage.getItem(Constant.USER)
        api.create().getDetailUser(userId).then((response) => {
            if (response.data) {
                if (response.ok) {
                    if (response.data.status === 'success') {
                        this.setState({ userCompany: response.data.data.company }, () => {
                            this.getCompanyActive()
                        })
                    }
                }
            }
        })
    }

    getCompanyActive() {
        api.create().getPerusahaanActive().then((response) => {
Deni Rinaldi's avatar
Deni Rinaldi committed
66
            // console.log(response);
Deni Rinaldi's avatar
Deni Rinaldi committed
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
            if (response.data) {
                if (response.data.status === 'success') {
                    let data = response.data.data
                    let comID = this.state.rawData ? this.state.rawData.company_id : 0
                    let companyData = data.map((item) => {
                        return {
                            company_id: item.company_id,
                            company_name: item.company_name,
                        }
                    })

                    let arrayBaru = []
                    this.state.userCompany.map((item, index) => {
                        let indexID = companyData.findIndex((val) => val.company_id == item)
                        if (indexID !== -1) {
                            arrayBaru.push(companyData[indexID])
                        }
                    })

                    let defaultProps = {
                        options: arrayBaru,
                        getOptionLabel: (option) => titleCase(option.company_name),
                    };
                    let index = arrayBaru.findIndex((val) => val.company_id == comID)
                    this.setState({ listCompany: defaultProps, company: arrayBaru.length < 1 ? companyData[0] : (index == -1 ? arrayBaru[0] : arrayBaru[index]) }, () => {
                        this.getLastPeriod()
                    })
                } else {
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
                        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', listCompany: null, company: null })
            }
        })
    }

    getLastPeriod() {
        api.create().getLastPeriod(this.state.company.company_id).then(response => {
Deni Rinaldi's avatar
Deni Rinaldi committed
112
            // console.log(response);
Deni Rinaldi's avatar
Deni Rinaldi committed
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
            if (response.data.status === "success") {
                this.setState({ lastPeriod: response.data.data.last_periode, latestPeriode: response.data.data.latest_periode }, () => {
                    this.getPeriode()
                })
            }
        })
    }

    getPeriode() {
        api.create().getPeriodeTransaction().then(response => {
            let currentYear = new Date().getFullYear()
            // console.log(currentYear)
            if (response.data) {
                if (response.data.status === "success") {
                    let data = []
                    response.data.data.map((item) => {
                        if (this.state.isApprover) {
                            if (item >= 2000 && item <= (Number(currentYear) + 1)) {
                                data.push(item)
                            }
                        } else {
                            if ((item >= 2000) && (item == this.state.lastPeriod || item < this.state.lastPeriod)) {
                                data.push(item)
                            }
                        }
                    })
                    let periodeData = data.map((item) => {
                        return {
                            periode: item,
                        }
                    })
                    let defaultProps = {
                        options: periodeData,
                        getOptionLabel: (option) => option.periode,
                    };
                    let periode = (this.state.lastPeriod == "" ? String(Number(currentYear) + 1) : this.state.lastPeriod)
                    let index = data.sort((a, b) => a - b).findIndex((val) => val === periode)
                    // console.log(data)
                    // console.log(this.state.latestPeriode)
                    // console.log(periodeData)
                    // console.log(index)
                    this.setState({ listPeriode: defaultProps, periode: index === -1 ? periodeData[0] : periodeData[index] }, () => {
Deni Rinaldi's avatar
Deni Rinaldi committed
155
                        // this.getDataTable()
156 157
                        // this.getSubmission()
                        this.getReportType()
Deni Rinaldi's avatar
Deni Rinaldi committed
158 159 160 161 162 163 164 165 166 167 168
                        // if (this.state.isApprover === true) {
                        //     this.getCompanySubmitted()
                        // } else {
                        //     this.getRevision()
                        // }
                    })
                }
            }
        })
    }

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
    getReportType() {
        let payload = {
            "company_id": this.state.company.company_id,
            "periode": this.state.periode.periode,
            "report_type": "Master Budget",
        }
        api.create().getReportTypeBody(payload).then(response => {
            console.log(response);
            if (response.data) {
                if (response.data.status === "success") {
                    if (this.state.report.value === 2) {
                        response.data.data.map((item, index) => {
                            if (item.report_name === 'Balance Sheet') {
                                this.setState({ revisionType: item.revision }, ()=> {
                                    this.getSubmission()
                                })
                            }
                        })
                    } else if (this.state.report.value === 1) {
                        response.data.data.map((item, index) => {
                            if (item.report_name === 'Profit Loss') {
                                this.setState({ revisionType: item.revision }, ()=> {
                                    this.getSubmission()
                                })
                            }
                        })
                    } else {
                        this.setState({ revisionType: 0 }, ()=> {
                            this.getSubmission()
                        })
                    }
faisalhamdi's avatar
faisalhamdi committed
200 201 202 203 204 205 206 207 208
                } 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);
                        }
                    })
209
                }
faisalhamdi's avatar
faisalhamdi committed
210 211
            } else {
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error', loading: false })   
212 213 214 215
            }
        })
    }

Deni Rinaldi's avatar
Deni Rinaldi committed
216 217
    getSubmission() {
        this.setState({ loading: true })
Deni Rinaldi's avatar
Deni Rinaldi committed
218 219
        let payload = {
            "company_id": this.state.company.company_id,
Deni Rinaldi's avatar
Deni Rinaldi committed
220 221
            "periode": this.state.periode.periode,
            "is_approver": true
Deni Rinaldi's avatar
Deni Rinaldi committed
222
        }
Deni Rinaldi's avatar
Deni Rinaldi committed
223
        api.create().getSubmission(payload).then(response => {
224
            console.log(response)
Deni Rinaldi's avatar
Deni Rinaldi committed
225 226 227 228 229 230 231 232 233 234
            if (response.data) {
                if (response.data.status === "success") {
                    this.setState({
                        submissionID: response.data.data.submission_id,
                        lastRevision: response.data.data.last_revision,
                    }, () => {
                        this.getDataTable()
                    })
                } else {
                    this.setState({ submissionID: null, loading: false })
Deni Rinaldi's avatar
Deni Rinaldi committed
235
                }
Deni Rinaldi's avatar
Deni Rinaldi committed
236 237 238 239 240 241 242
            }
        })
    }

    getDataTable() {
        let payload = {
            "report_id": this.state.report.value,
243
            "revision": this.state.revisionType,
Deni Rinaldi's avatar
Deni Rinaldi committed
244 245 246 247
            "periode": this.state.periode.periode,
            "company_id": this.state.company.company_id,
            "submission_id": this.state.submissionID
        }
idlanirined's avatar
idlanirined committed
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
        if (this.state.report.value === 1) {
            api.create().getDetailReportMB(payload).then(response => {
                // console.log(response);
                let dataTable = []
                if (response.data) {
                    let res = response.data.data
                    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.profit_loss.total_actual_before === null ? "0" : item.profit_loss.total_actual_before === "" ? "0" : item.profit_loss.total_actual_before,
                                item.profit_loss.january,
                                item.profit_loss.february,
                                item.profit_loss.march,
                                item.profit_loss.april,
                                item.profit_loss.may,
                                item.profit_loss.june,
                                item.profit_loss.july,
                                item.profit_loss.august,
                                item.profit_loss.september,
                                item.profit_loss.october,
                                item.profit_loss.november,
                                item.profit_loss.december,
                                item.profit_loss.total_current_year,
                                item.profit_loss.total_next_year,
                                item.profit_loss.total_more_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.profit_loss.total_actual_before === null ? "0" : item.profit_loss.total_actual_before === "" ? "0" : item.profit_loss.total_actual_before,
                            item.profit_loss.january,
                            item.profit_loss.february,
                            item.profit_loss.march,
                            item.profit_loss.april,
                            item.profit_loss.may,
                            item.profit_loss.june,
                            item.profit_loss.july,
                            item.profit_loss.august,
                            item.profit_loss.september,
                            item.profit_loss.october,
                            item.profit_loss.november,
                            item.profit_loss.december,
                            item.profit_loss.total_current_year,
                            item.profit_loss.total_next_year,
                            item.profit_loss.total_more_year,
                            item.order
                        ])
                        if (item.children !== null) {
                            if (item.children.length > 0) {
                                item.children.map((items, indexs) => {
                                    handlePushChild(items)
                                })
                            }
                        }
                    })
                    // console.log(dataTable)
                    this.setState({ dataTable, previewTable: true, loading: false, previewDownload: true })
                } else {
                    this.setState({ dataTable: [], previewTable: false, loading: false, previewDownload: false })
                }
            })
        } else if (this.state.report.value === 2) {
            api.create().getDetailReportMB(payload).then(response => {
faisalhamdi's avatar
faisalhamdi committed
333 334
                console.log(payload);
                console.log(response);
idlanirined's avatar
idlanirined committed
335
                let dataTable = []
Deni Rinaldi's avatar
Deni Rinaldi committed
336 337 338 339 340 341 342 343 344 345 346 347 348
                if (response.data) {
                    let res = response.data.data
                    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.balance_sheet.total_actual_before === null ? "0.0" : item.balance_sheet.total_actual_before === "" ? "0.0" : item.balance_sheet.total_actual_before,
Deni Rinaldi's avatar
Deni Rinaldi committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
                                Number(item.balance_sheet.january).toFixed(1),
                                Number(item.balance_sheet.february).toFixed(1),
                                Number(item.balance_sheet.march).toFixed(1),
                                Number(item.balance_sheet.april).toFixed(1),
                                Number(item.balance_sheet.may).toFixed(1),
                                Number(item.balance_sheet.june).toFixed(1),
                                Number(item.balance_sheet.july).toFixed(1),
                                Number(item.balance_sheet.august).toFixed(1),
                                Number(item.balance_sheet.september).toFixed(1),
                                Number(item.balance_sheet.october).toFixed(1),
                                Number(item.balance_sheet.november).toFixed(1),
                                Number(item.balance_sheet.december).toFixed(1),
                                Number(item.balance_sheet.total_current_year).toFixed(1),
                                Number(item.balance_sheet.total_next_year).toFixed(1),
                                Number(item.balance_sheet.total_more_year).toFixed(1),
Deni Rinaldi's avatar
Deni Rinaldi committed
364 365 366 367 368 369 370 371 372 373 374 375
                                item.order,
                                item.condition_it_should_be,
                                item.condition_if_wrong
                            ])
                        }
                        if (item.children !== null) {
                            if (item.children.length > 0) {
                                item.children.map((items, indexs) => {
                                    handlePushChild(items)
                                })
                            }
                        }
Deni Rinaldi's avatar
Deni Rinaldi committed
376
                    }
Deni Rinaldi's avatar
Deni Rinaldi committed
377 378 379 380 381 382 383 384 385
                    res.map((item, index) => {
                        dataTable.push([
                            item.type_report_id,
                            item.id,
                            item.parent,
                            item.formula,
                            item.level,
                            item.description,
                            item.balance_sheet.total_actual_before === null ? "0.0" : item.balance_sheet.total_actual_before === "" ? "0.0" : item.balance_sheet.total_actual_before,
Deni Rinaldi's avatar
Deni Rinaldi committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
                            Number(item.balance_sheet.january).toFixed(1),
                            Number(item.balance_sheet.february).toFixed(1),
                            Number(item.balance_sheet.march).toFixed(1),
                            Number(item.balance_sheet.april).toFixed(1),
                            Number(item.balance_sheet.may).toFixed(1),
                            Number(item.balance_sheet.june).toFixed(1),
                            Number(item.balance_sheet.july).toFixed(1),
                            Number(item.balance_sheet.august).toFixed(1),
                            Number(item.balance_sheet.september).toFixed(1),
                            Number(item.balance_sheet.october).toFixed(1),
                            Number(item.balance_sheet.november).toFixed(1),
                            Number(item.balance_sheet.december).toFixed(1),
                            Number(item.balance_sheet.total_current_year).toFixed(1),
                            Number(item.balance_sheet.total_next_year).toFixed(1),
                            Number(item.balance_sheet.total_more_year).toFixed(1),
Deni Rinaldi's avatar
Deni Rinaldi committed
401 402 403 404 405 406 407 408 409 410 411 412
                            item.order,
                            item.condition_it_should_be,
                            item.condition_if_wrong
                        ])
                        if (item.children !== null) {
                            if (item.children.length > 0) {
                                item.children.map((items, indexs) => {
                                    handlePushChild(items)
                                })
                            }
                        }
                    })
Deni Rinaldi's avatar
Deni Rinaldi committed
413
                    this.setState({ dataTable, previewTable: true, loading: false, previewDownload: true })
Deni Rinaldi's avatar
Deni Rinaldi committed
414
                } else {
Deni Rinaldi's avatar
Deni Rinaldi committed
415
                    this.setState({ dataTable: [], previewTable: false, loading: false, previewDownload: false })
Deni Rinaldi's avatar
Deni Rinaldi committed
416
                }
idlanirined's avatar
idlanirined committed
417 418 419 420
            })
        } else if (this.state.report.value === 3) {
            api.create().getReportHierarkiPL(payload).then(response => {
                // console.log(response);
Deni Rinaldi's avatar
Deni Rinaldi committed
421
                if (response.data) {
idlanirined's avatar
idlanirined committed
422 423
                    let dataTable = []
                    console.log(response)
Deni Rinaldi's avatar
Deni Rinaldi committed
424 425
                    let res = response.data.data
                    const handlePushChild = (item) => {
idlanirined's avatar
idlanirined committed
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
                        dataTable.push([
                            item.type_report_id,
                            item.id,
                            item.parent,
                            item.formula,
                            item.level,
                            item.description,
                            item.profit_detail.total_actual_before === null ? "0" : item.profit_detail.total_actual_before === "" ? "0" : item.profit_detail.total_actual_before,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.january, formula: item.profit_detail.january_formula } : item.profit_detail.january,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.february, formula: item.profit_detail.february_formula } : item.profit_detail.february,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.march, formula: item.profit_detail.march_formula } : item.profit_detail.march,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.april, formula: item.profit_detail.april_formula } : item.profit_detail.april,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.may, formula: item.profit_detail.may_formula } : item.profit_detail.may,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.june, formula: item.profit_detail.june_formula } : item.profit_detail.june,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.july, formula: item.profit_detail.july_formula } : item.profit_detail.july,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.august, formula: item.profit_detail.august_formula } : item.profit_detail.august,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.september, formula: item.profit_detail.september_formula } : item.profit_detail.september,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.october, formula: item.profit_detail.october_formula } : item.profit_detail.october,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.november, formula: item.profit_detail.november_formula } : item.profit_detail.november,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.december, formula: item.profit_detail.december_formula } : item.profit_detail.december,
                            item.profit_detail.total_current_year,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? 0 : item.profit_detail.total_next_year,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? 0 : item.profit_detail.total_more_year,
                            item.order,
                            item.condition_it_should_be,
                            item.condition_if_wrong,
                            item.profit_detail.forecast_formula == null ? [] : item.profit_detail.forecast_formula
                        ])

Deni Rinaldi's avatar
Deni Rinaldi committed
455 456 457 458 459 460 461
                        if (item.children !== null) {
                            if (item.children.length > 0) {
                                item.children.map((items, indexs) => {
                                    handlePushChild(items)
                                })
                            }
                        }
Deni Rinaldi's avatar
Deni Rinaldi committed
462
                    }
Deni Rinaldi's avatar
Deni Rinaldi committed
463 464 465 466 467 468 469 470
                    res.map((item, index) => {
                        dataTable.push([
                            item.type_report_id,
                            item.id,
                            item.parent,
                            item.formula,
                            item.level,
                            item.description,
idlanirined's avatar
idlanirined committed
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
                            item.profit_detail.total_actual_before === null ? "0" : item.profit_detail.total_actual_before === "" ? "0" : item.profit_detail.total_actual_before,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.january, formula: item.profit_detail.january_formula } : item.profit_detail.january,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.february, formula: item.profit_detail.february_formula } : item.profit_detail.february,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.march, formula: item.profit_detail.march_formula } : item.profit_detail.march,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.april, formula: item.profit_detail.april_formula } : item.profit_detail.april,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.may, formula: item.profit_detail.may_formula } : item.profit_detail.may,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.june, formula: item.profit_detail.june_formula } : item.profit_detail.june,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.july, formula: item.profit_detail.july_formula } : item.profit_detail.july,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.august, formula: item.profit_detail.august_formula } : item.profit_detail.august,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.september, formula: item.profit_detail.september_formula } : item.profit_detail.september,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.october, formula: item.profit_detail.october_formula } : item.profit_detail.october,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.november, formula: item.profit_detail.november_formula } : item.profit_detail.november,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? { value: item.profit_detail.december, formula: item.profit_detail.december_formula } : item.profit_detail.december,
                            item.profit_detail.total_current_year,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? 0 : item.profit_detail.total_next_year,
                            item.type_report_id == 5 || item.type_report_id == 6 || item.type_report_id == 7 ? 0 : item.profit_detail.total_more_year,
                            item.order,
                            item.condition_it_should_be,
                            item.condition_if_wrong,
                            item.profit_detail.forecast_formula == null ? [] : item.profit_detail.forecast_formula
Deni Rinaldi's avatar
Deni Rinaldi committed
491 492 493 494 495 496 497 498 499
                        ])
                        if (item.children !== null) {
                            if (item.children.length > 0) {
                                item.children.map((items, indexs) => {
                                    handlePushChild(items)
                                })
                            }
                        }
                    })
Deni Rinaldi's avatar
Deni Rinaldi committed
500
                    this.setState({ dataTable, previewTable: true, loading: false, previewDownload: true })
Deni Rinaldi's avatar
Deni Rinaldi committed
501
                } else {
Deni Rinaldi's avatar
Deni Rinaldi committed
502
                    this.setState({ dataTable: [], previewTable: false, loading: false, previewDownload: false })
Deni Rinaldi's avatar
Deni Rinaldi committed
503
                }
idlanirined's avatar
idlanirined committed
504
            })
faisalhamdi's avatar
faisalhamdi committed
505
        } else if (this.state.report.value === 4) {
faisalhamdi's avatar
faisalhamdi committed
506 507
            api.create().getReportHierarkiFR(payload).then(response => {
                // console.log(payload);
faisalhamdi's avatar
faisalhamdi committed
508
                // console.log(response);
faisalhamdi's avatar
faisalhamdi committed
509 510
                let dataTable = []
                if (response.data) {
faisalhamdi's avatar
faisalhamdi committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524
                    if (response.data.status === 'success') {
                        let res = response.data.data
                        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.ratio.total_actual_before === null ? "0.0" : item.ratio.total_actual_before === "" ? "0.0" : item.ratio.total_actual_before,
Deni Rinaldi's avatar
Deni Rinaldi committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538
                                    Number(item.ratio.january).toFixed(1),
                                    Number(item.ratio.february).toFixed(1),
                                    Number(item.ratio.march).toFixed(1),
                                    Number(item.ratio.april).toFixed(1),
                                    Number(item.ratio.may).toFixed(1),
                                    Number(item.ratio.june).toFixed(1),
                                    Number(item.ratio.july).toFixed(1),
                                    Number(item.ratio.august).toFixed(1),
                                    Number(item.ratio.september).toFixed(1),
                                    Number(item.ratio.october).toFixed(1),
                                    Number(item.ratio.november).toFixed(1),
                                    Number(item.ratio.december).toFixed(1),
                                    Number(item.ratio.total_next_year).toFixed(1),
                                    Number(item.ratio.total_more_year).toFixed(1),
faisalhamdi's avatar
faisalhamdi committed
539 540 541 542 543 544 545 546 547 548 549 550 551 552
                                    item.order,
                                    item.condition_it_should_be,
                                    item.condition_if_wrong
                                ])
                            }
                            if (item.children !== null) {
                                if (item.children.length > 0) {
                                    item.children.map((items, indexs) => {
                                        handlePushChild(items)
                                    })
                                }
                            }
                        }
                        res.map((item, index) => {
faisalhamdi's avatar
faisalhamdi committed
553 554 555 556 557 558 559
                            dataTable.push([
                                item.type_report_id,
                                item.id,
                                item.parent,
                                item.formula,
                                item.level,
                                item.description,
faisalhamdi's avatar
faisalhamdi committed
560
                                item.oum,
faisalhamdi's avatar
faisalhamdi committed
561
                                item.ratio.total_actual_before === null ? "0.0" : item.ratio.total_actual_before === "" ? "0.0" : item.ratio.total_actual_before,
Deni Rinaldi's avatar
Deni Rinaldi committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575
                                Number(item.ratio.january).toFixed(1),
                                Number(item.ratio.february).toFixed(1),
                                Number(item.ratio.march).toFixed(1),
                                Number(item.ratio.april).toFixed(1),
                                Number(item.ratio.may).toFixed(1),
                                Number(item.ratio.june).toFixed(1),
                                Number(item.ratio.july).toFixed(1),
                                Number(item.ratio.august).toFixed(1),
                                Number(item.ratio.september).toFixed(1),
                                Number(item.ratio.october).toFixed(1),
                                Number(item.ratio.november).toFixed(1),
                                Number(item.ratio.december).toFixed(1),
                                Number(item.ratio.total_next_year).toFixed(1),
                                Number(item.ratio.total_more_year).toFixed(1),
faisalhamdi's avatar
faisalhamdi committed
576 577 578 579
                                item.order,
                                item.condition_it_should_be,
                                item.condition_if_wrong
                            ])
faisalhamdi's avatar
faisalhamdi committed
580 581 582 583 584 585
                            if (item.children !== null) {
                                if (item.children.length > 0) {
                                    item.children.map((items, indexs) => {
                                        handlePushChild(items)
                                    })
                                }
faisalhamdi's avatar
faisalhamdi committed
586
                            }
faisalhamdi's avatar
faisalhamdi committed
587 588 589 590 591 592 593 594 595
                        })
                        this.setState({ dataTable, previewTable: true, loading: false, previewDownload: true })
                    } 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);
faisalhamdi's avatar
faisalhamdi committed
596
                            }
faisalhamdi's avatar
faisalhamdi committed
597 598
                        })
                    }                    
faisalhamdi's avatar
faisalhamdi committed
599
                } else {
faisalhamdi's avatar
faisalhamdi committed
600
                    this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error', loading: false })
faisalhamdi's avatar
faisalhamdi committed
601
                }
faisalhamdi's avatar
faisalhamdi committed
602
            })
603
        }
Deni Rinaldi's avatar
Deni Rinaldi committed
604 605
    }

Deni Rinaldi's avatar
Deni Rinaldi committed
606
    async downloadAllData() {
faisalhamdi's avatar
faisalhamdi committed
607
        if (this.state.report.value === 1) {
Deni Rinaldi's avatar
Deni Rinaldi committed
608
            let res = await fetch(
Riri Novita's avatar
Riri Novita committed
609
                `${process.env.REACT_APP_URL_MAIN_BE}/public/transaction/db_report/export_report?submission_id=${this.state.submissionID === null ? "" : this.state.submissionID}&&report_id=${this.state.report.value}&&company_id=${this.state.company.company_id}&&year=${this.state.periode.periode}&&revision=${this.state.revisionType}`
Deni Rinaldi's avatar
Deni Rinaldi committed
610 611 612 613 614 615 616
            )
            res = await res.blob()
            this.setState({ loading: false })
            if (res.size > 0) {
                let url = window.URL.createObjectURL(res);
                let a = document.createElement('a');
                a.href = url;
Deni Rinaldi's avatar
Deni Rinaldi committed
617
                a.download = 'Report DB Profit & Loss Detail.xlsx';
Deni Rinaldi's avatar
Deni Rinaldi committed
618 619
                a.click();
            }
faisalhamdi's avatar
faisalhamdi committed
620
        } else if (this.state.report.value === 2) {
Deni Rinaldi's avatar
Deni Rinaldi committed
621
            let res = await fetch(
Riri Novita's avatar
Riri Novita committed
622
                `${process.env.REACT_APP_URL_MAIN_BE}/public/transaction/db_report/export_report?submission_id=${this.state.submissionID === null ? "" : this.state.submissionID}&&report_id=${this.state.report.value}&&company_id=${this.state.company.company_id}&&year=${this.state.periode.periode}&&revision=${this.state.revisionType}`
Deni Rinaldi's avatar
Deni Rinaldi committed
623 624 625 626 627 628 629
            )
            res = await res.blob()
            this.setState({ loading: false })
            if (res.size > 0) {
                let url = window.URL.createObjectURL(res);
                let a = document.createElement('a');
                a.href = url;
Deni Rinaldi's avatar
Deni Rinaldi committed
630
                a.download = 'Report DB Balance Sheet.xlsx';
Deni Rinaldi's avatar
Deni Rinaldi committed
631 632 633 634
                a.click();
            }
        } else if (this.state.report.value === 3) {
            let res = await fetch(
Riri Novita's avatar
Riri Novita committed
635
                `${process.env.REACT_APP_URL_MAIN_BE}/public/transaction/db_profit_loss_detail/export_master_budget?submission_id=${this.state.submissionID === null ? "" : this.state.submissionID}&&report_id=${this.state.report.value}&&company_id=${this.state.company.company_id}&&year=${this.state.periode.periode}&&revision=${this.state.revisionType}`
Deni Rinaldi's avatar
Deni Rinaldi committed
636 637 638 639 640 641 642
            )
            res = await res.blob()
            this.setState({ loading: false })
            if (res.size > 0) {
                let url = window.URL.createObjectURL(res);
                let a = document.createElement('a');
                a.href = url;
Deni Rinaldi's avatar
Deni Rinaldi committed
643
                a.download = 'Report DB Profit & Loss.xlsx';
Deni Rinaldi's avatar
Deni Rinaldi committed
644 645
                a.click();
            }
faisalhamdi's avatar
faisalhamdi committed
646
        } else if (this.state.report.value === 4) {
Riri Novita's avatar
Riri Novita committed
647
            let url = `${process.env.REACT_APP_URL_MAIN_BE}/public/transaction/db_ratio/export_report?submission_id=${this.state.submissionID === null ? "" : this.state.submissionID}&&report_id=${this.state.report.value}&&company_id=${this.state.company.company_id}&&year=${this.state.periode.periode}&&revision=${this.state.revisionType}`
Deni Rinaldi's avatar
Deni Rinaldi committed
648
            console.log(url);
faisalhamdi's avatar
faisalhamdi committed
649
            let res = await fetch(
Riri Novita's avatar
Riri Novita committed
650
                `${process.env.REACT_APP_URL_MAIN_BE}/public/transaction/db_ratio/export_report?submission_id=${this.state.submissionID === null ? "" : this.state.submissionID}&&report_id=${this.state.report.value}&&company_id=${this.state.company.company_id}&&year=${this.state.periode.periode}&&revision=${this.state.revisionType}`
faisalhamdi's avatar
faisalhamdi committed
651 652 653 654 655 656 657 658 659 660
            )
            res = await res.blob()
            this.setState({ loading: false })
            if (res.size > 0) {
                let url = window.URL.createObjectURL(res);
                let a = document.createElement('a');
                a.href = url;
                a.download = 'Report DB Financial Ratio.xlsx';
                a.click();
            }
Deni Rinaldi's avatar
Deni Rinaldi committed
661 662 663 664
        } else {
            setTimeout(() => {
                this.setState({ loading: false })
            }, 1000);
Deni Rinaldi's avatar
Deni Rinaldi committed
665 666 667
        }
    }

faisalhamdi's avatar
faisalhamdi committed
668 669 670 671
    closeAlert() {
        this.setState({ alert: false })
    }

faisalhamdi's avatar
faisalhamdi committed
672
    render() {
Deni Rinaldi's avatar
Deni Rinaldi committed
673
        const loadingComponent = (
Deni Rinaldi's avatar
Deni Rinaldi committed
674
            <div style={{ position: 'fixed', zIndex: 110, top: 0, left: 0, width: '100%', height: '100%', display: 'flex', justifyContent: 'center', alignItems: 'center', background: 'rgba(255,255,255,0.8)' }}>
Deni Rinaldi's avatar
Deni Rinaldi committed
675 676 677 678 679 680 681 682
                <PropagateLoader
                    // css={override}
                    size={20}
                    color={"#274B80"}
                    loading={this.state.loading}
                />
            </div>
        );
faisalhamdi's avatar
faisalhamdi committed
683 684 685 686 687 688
        return (
            <div style={{ flex: 1, backgroundColor: '#f8f8f8' }} ref={this.myRef}>
                <div>
                    <div className={"main-color"} style={{ height: 78, display: 'flex', alignItems: 'center', paddingLeft: 20 }}>
                        <Typography style={{ fontSize: '16px', color: 'white' }}>Sub Holding Report</Typography>
                    </div>
faisalhamdi's avatar
faisalhamdi committed
689 690 691 692 693
                    <Snackbar open={this.state.alert} autoHideDuration={6000} onClose={() => this.closeAlert()}>
                        <Alert onClose={() => this.closeAlert()} severity={this.state.tipeAlert}>
                            {this.state.messageAlert}
                        </Alert>
                    </Snackbar>
faisalhamdi's avatar
faisalhamdi committed
694 695 696 697 698
                    <div style={{ padding: 20, width: '100%' }}>
                        <Paper style={{ paddingTop: 10 }}>
                            <div style={{ borderBottom: 'solid 1px #c4c4c4' }} >
                                <Typography style={{ fontSize: '12px', color: '#4b4b4b', margin: 10 }}>Sub Holding</Typography>
                            </div>
Deni Rinaldi's avatar
Deni Rinaldi committed
699 700
                            <div style={{ minWidth: 'max-content', padding: '20px 20px 0px 20px' }}>
                                <div style={{ marginTop: 15 }}>
Deni Rinaldi's avatar
Deni Rinaldi committed
701 702
                                    <Autocomplete
                                        options={this.state.reportType}
Deni Rinaldi's avatar
Deni Rinaldi committed
703
                                        getOptionLabel={(option) => titleCase(option.label)}
Deni Rinaldi's avatar
Deni Rinaldi committed
704
                                        id="typereport"
idlanirined's avatar
idlanirined committed
705
                                        onChange={(event, newInputValue) => this.setState({ report: newInputValue, loading: true, previewTable: false }, () => {
Deni Rinaldi's avatar
Deni Rinaldi committed
706
                                            this.getReportType()
Deni Rinaldi's avatar
Deni Rinaldi committed
707
                                        })}
Deni Rinaldi's avatar
Deni Rinaldi committed
708
                                        disableClearable
faisalhamdi's avatar
faisalhamdi committed
709
                                        style={{ width: 250 }}
Deni Rinaldi's avatar
Deni Rinaldi committed
710 711 712
                                        renderInput={(params) => <TextField {...params} label="Report Type" margin="normal" style={{ marginTop: 7 }} />}
                                        value={this.state.report}
                                    />
faisalhamdi's avatar
faisalhamdi committed
713
                                </div>
Deni Rinaldi's avatar
Deni Rinaldi committed
714
                                <div style={{ marginTop: 15 }}>
Deni Rinaldi's avatar
Deni Rinaldi committed
715 716 717 718
                                    <Autocomplete
                                        {...this.state.listCompany}
                                        id="company"
                                        disabled={this.state.intent === 'Home' ? true : false}
idlanirined's avatar
idlanirined committed
719
                                        onChange={(event, newInputValue) => this.setState({ company: newInputValue, loading: true, previewTable: false }, () => {
Deni Rinaldi's avatar
Deni Rinaldi committed
720
                                            this.getReportType()
Deni Rinaldi's avatar
Deni Rinaldi committed
721
                                        })}
Deni Rinaldi's avatar
Deni Rinaldi committed
722 723 724 725 726
                                        disableClearable
                                        style={{ width: 250 }}
                                        renderInput={(params) => <TextField {...params} label="Company" margin="normal" style={{ marginTop: 7 }} />}
                                        value={this.state.company}
                                    />
faisalhamdi's avatar
faisalhamdi committed
727
                                </div>
Deni Rinaldi's avatar
Deni Rinaldi committed
728
                                <div style={{ marginTop: 15 }}>
Deni Rinaldi's avatar
Deni Rinaldi committed
729 730
                                    <Autocomplete
                                        {...this.state.listPeriode}
faisalhamdi's avatar
faisalhamdi committed
731
                                        id="periode"
idlanirined's avatar
idlanirined committed
732
                                        onChange={(event, newInputValue) => this.setState({ periode: newInputValue, loading: true, previewTable: false }, () => {
Deni Rinaldi's avatar
Deni Rinaldi committed
733
                                            this.getReportType()
Deni Rinaldi's avatar
Deni Rinaldi committed
734 735 736 737 738 739 740
                                        })}
                                        disabled={this.state.intent === 'Home' ? true : false}
                                        disableClearable
                                        style={{ width: 250 }}
                                        renderInput={(params) =>
                                            <TextField {...params} label="Period" margin="normal" style={{ marginTop: 7 }}
                                            />}
faisalhamdi's avatar
faisalhamdi committed
741
                                        value={this.state.periode}
Deni Rinaldi's avatar
Deni Rinaldi committed
742
                                    />
faisalhamdi's avatar
faisalhamdi committed
743
                                </div>
Deni Rinaldi's avatar
Deni Rinaldi committed
744
                            </div>
Deni Rinaldi's avatar
Deni Rinaldi committed
745
                            <div>
Deni Rinaldi's avatar
Deni Rinaldi committed
746
                                <div style={{ display: 'flex', justifyContent: 'space-between', padding: '0px 20px 10px 20px' }}>
Deni Rinaldi's avatar
Deni Rinaldi committed
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
                                    <div></div>
                                    {this.state.previewDownload && (
                                        <div style={{ width: '50%', justifyContent: 'flex-end', display: 'flex', flexFlow: 'wrap' }}>
                                            <a data-tip={'Download'} data-for="download">
                                                <button
                                                    style={{
                                                        backgroundColor: 'transparent',
                                                        cursor: 'pointer',
                                                        borderColor: 'transparent',
                                                        margin: 5,
                                                        outline: 'none'
                                                    }}
                                                    onClick={() => this.setState({ loading: true }, () => {
                                                        setTimeout(() => {
                                                            this.downloadAllData()
                                                        }, 100);
                                                    })}
                                                >
                                                    <img src={Images.download} />
                                                </button>
                                            </a>
                                            <ReactTooltip border={true} id="download" place="bottom" type="light" effect="solid" />
                                        </div>
                                    )}
                                </div>
Deni Rinaldi's avatar
Deni Rinaldi committed
772
                                {this.state.loading && loadingComponent}
Deni Rinaldi's avatar
Deni Rinaldi committed
773
                                {this.state.previewTable && (
Deni Rinaldi's avatar
Deni Rinaldi committed
774 775 776 777
                                    <TableSubHolding
                                        width={this.props.width}
                                        height={this.props.height}
                                        open={this.props.open}
Deni Rinaldi's avatar
Deni Rinaldi committed
778
                                        type={this.state.report ? this.state.report.value : 1}
Deni Rinaldi's avatar
Deni Rinaldi committed
779 780 781 782
                                        dataTable={this.state.dataTable}
                                        periode={this.state.periode ? this.state.periode.periode : null}
                                    />
                                )}
faisalhamdi's avatar
faisalhamdi committed
783 784 785 786 787 788 789 790
                            </div>
                        </Paper>
                    </div>
                </div>
            </div>
        )
    }
}