RepotrCafrm.js 20.4 KB
Newer Older
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
import React, {Component} from 'react';
import api from "../../api";
import {format} from "date-fns";
import Constant from "../../library/Constant";
import {
    createMuiTheme,
    FormControlLabel,
    Input,
    MuiThemeProvider,
    TableCell,
    Typography,
    Paper,
    TextField,
    Snackbar
} from "@material-ui/core";
import MUIDataTable from "mui-datatables";
import {PropagateLoader} from 'react-spinners';
import {Alert} from '@material-ui/lab';
import ReactTooltip from "react-tooltip";
import Autocomplete from '@material-ui/lab/Autocomplete';
import Images from '../../assets/Images';

var ct = require("../../library/CustomTable");
const getMuiTheme = () => createMuiTheme(ct.customTable4());
const options = ct.customOptionsFixedColumnMonitoring();
const style = {
    position: "sticky",
    left: 0,
    zIndex: 101,
    background: "white",
};
const style2 = {
    position: "sticky",
    background: "white",
    zIndex: 100,
};

class RepotrCafrm extends Component {
    constructor(props) {
        super(props)
        this.state = {
            dataTable: [],
            curMonth: 0,
            curYear: 0,
            data: false
        }
        console.log(this.state.periodeMB);
        console.log(this.state.month);
    }

    async getMonth() {
        this.setState({loading: true})
        await api.create().getMonthTransaction().then(response => {
            let dateNow = new Date()
55
            dateNow.setMonth(dateNow.getMonth());
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
            let month = format(dateNow, 'MMMM')
            if (response.data) {
                if (response.data.status === "success") {
                    let data = response.data.data
                    let monthData = data.map((item) => {
                        return {
                            month_id: item.id,
                            month_value: String(item.month_name).substr(0, 3)
                        }
                    })
                    let defaultProps = {
                        options: monthData,
                        getOptionLabel: (option) => option.month_value,
                    };
                    console.log("month ");
                    console.log(defaultProps);
72
                    console.log(monthData[0]);
73 74 75 76
                    let index = data.findIndex((val) => val.month_name == month)
                    this.setState({
                        listMonth: defaultProps,
                        month: index == -1 ? monthData[0] : monthData[index],
77 78 79
                        curMonth: monthData[index]['month_id']
                    }, async () => {
                        await this.getPeriode()
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
                    })
                } else {
                    this.setState({alert: true, messageAlert: response.data.message, tipeAlert: 'warning'}, () => {
                        if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
                            setTimeout(() => {
                                localStorage.removeItem(Constant.TOKEN)
                                window.location.reload();
                            }, 1000);
                        }
                    })
                }
            } else {
                this.setState({alert: true, messageAlert: response.data.message, tipeAlert: 'error'})
            }
        })
    }

97
    async getPeriode() {
98 99 100 101 102 103 104 105 106 107 108 109 110 111
        let currentYear = new Date().getFullYear()
        let MB = []
        for (var i = 2000; i <= currentYear; i++) {
            MB.push({name: String(i), value: i})
            if (i == currentYear) {
                MB.push({name: String(i + 1), value: i + 1})
            }
        }

        let defaultPropsMB = {
            options: MB,
            getOptionLabel: (option) => option.name,
        };

112
        await this.setState({
113
            listPeriodeMB: defaultPropsMB,
114 115
            periodeMB: MB[MB.length - 2],
            curYear: currentYear,
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        }, async () => {
            await this.getStatus()
        })
    }

    async getStatus(){
        let listStatusVal = [
            {name: "All", value: 2},
            {name: "Completed", value: 1},
            {name: "Not-Yet", value: 0}
        ];

        let listStatus = {
            options: listStatusVal,
            getOptionLabel: (option) => option.name,
        };

        await this.setState({
            listStatus,
            defaultStatus : listStatusVal[0],
            curStatus: listStatusVal[0].value
137 138
        }, async () => {
            await this.getFromCallback()
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
        })
    }

    async getData(payload) {
        console.log("getData +1323")
        console.log(payload);
        await api.create().getMonitoringCafrm(payload).then((res) => {
            console.log(res);
            if (res.data) {
                if (res.data.status === 'success') {
                    let data = res.data.data;
                    this.setState({
                        dataTable: data,
                        data: true,
                        loading: false
                    })
                } else {
                    this.setState({
                        alert: true,
                        messageAlert: res.data.message,
                        tipeAlert: 'warning',
                        loading: false
                    }, () => {
                        if (res.data.message.includes("Someone Logged In") || res.data.message.includes("Token Expired")) {
                            setTimeout(() => {
                                localStorage.removeItem(Constant.TOKEN)
                                window.location.reload();
                            }, 1000);
                        }
                    })
                }
            } else {
                this.setState({alert: true, messageAlert: res.data.message, tipeAlert: 'error', loading: false})
            }
        });
    }

    async getFromCallback() {
        if (this.state.curYear >= 2000 && this.state.curMonth > 0) {
            let month = 0;
179
            if(this.state.curMonth >= 1 && this.state.curMonth <= 9 && this.state.curMonth.toString().length == 1){
180 181 182 183
                month = '0'+this.state.curMonth;
            }else{
                month = this.state.curMonth;
            }
184
        let payload = {year: this.state.curYear, month: month, status: this.state.curStatus};
185 186 187 188 189 190 191 192 193 194 195

            await this.getData(payload);
            console.log(`month ${this.state.curMonth} year ${this.state.curYear}`)
            this.setState({
                curMonth: month,
                loading: false
            })
        }
    }

    async downloadData() {
muhammad ridwan's avatar
muhammad ridwan committed
196
        let url = `${process.env.REACT_APP_URL_MAIN_BE}/public/transaction/monitoring/export_cafrm_report?periode=${this.state.curYear}&months=${this.state.curMonth}&status=${this.state.curStatus}`
197 198
        console.log(url);
        let res = await fetch(
muhammad ridwan's avatar
muhammad ridwan committed
199
            `${process.env.REACT_APP_URL_MAIN_BE}/public/transaction/monitoring/export_cafrm_report?periode=${this.state.curYear}&months=${this.state.curMonth}&status=${this.state.curStatus}`
200 201 202 203 204 205 206
        )
        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;
muhammad ridwan's avatar
muhammad ridwan committed
207
            a.download = `Progress Report CARFM ${this.state.curYear}-${this.state.curMonth}-${this.state.defaultStatus.name}.xlsx`;
208 209 210 211 212 213
            a.click();
        }
    }

    async componentDidMount() {
        await this.getMonth();
214 215 216
        // this.setState({
        //     loading: false
        // })
217 218
    }

219 220 221 222
    closeAlert() {
        this.setState({ alert: false })
    }

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
    render() {

        const columnMR = [
            {
                name: "company_name",
                label: "Company",
                options : {
                    customHeadRender: (columnMeta) => (
                        <TableCell key={columnMeta.index} style={{ ...style, top: 0, zIndex: 103, backgroundColor: "#1c71b8", width: 200, borderRight: "1px #fff solid", borderBottom: "1px #fff solid", }}>
                            <Typography style={{ color: "white", fontSize: 12, fontWeight: "bold", textAlign: "center" }}>{columnMeta.label}</Typography>
                        </TableCell>
                    ),
                    setCellProps: () => ({ style }),
                    customBodyRender: (val) => {
                        return <div style={{ width: 300, textAlign: 'left', paddingLeft: 20 }}>{val}</div>;
                    },
                }
            },
            {
                name: 'internal_audit',
                label: "Internal Audit",
                options : {
                    customHeadRender : (columnMeta) => (
                        <TableCell key={columnMeta.index} style={{ ...style, top: 0, zIndex: 103, backgroundColor: "#1c71b8", width: 200, borderRight: "1px #fff solid", borderBottom: "1px #fff solid", }}>
                            <Typography style={{ color: "white", fontSize: 12, fontWeight: "bold", textAlign: "center" }}>{columnMeta.label}</Typography>
                        </TableCell>
                    ),
                    setCellProps: () => ({style2}),
                    customBodyRender: (val) => {
                        return (
                            <div className="column-1" style={{ placeSelf: 'center', textAlign: 'center', padding: 5 }}>
                                <div style={{ textAlign: 'center' }}>{val}</div>
                            </div>
                        )
                    }
                }
            },
            {
                name: 'anti_fraud',
                label: "Anti Fraud",
                options : {
                    customHeadRender : (columnMeta) => (
                        <TableCell key={columnMeta.index} style={{ ...style, top: 0, zIndex: 103, backgroundColor: "#1c71b8", width: 200, borderRight: "1px #fff solid", borderBottom: "1px #fff solid", }}>
                            <Typography style={{ color: "white", fontSize: 12, fontWeight: "bold", textAlign: "center" }}>{columnMeta.label}</Typography>
                        </TableCell>
                    ),
                    setCellProps: () => ({style2}),
                    customBodyRender: (val) => {
                        return (
                            <div className="column-1" style={{ placeSelf: 'center', textAlign: 'center', padding: 5 }}>
                                <div style={{ textAlign: 'center' }}>{val}</div>
                            </div>
                        )
                    }
                }
            },
            {
                name: 'risk_management',
                label: "Risk Management",
                options : {
                    customHeadRender : (columnMeta) => (
                        <TableCell key={columnMeta.index} style={{ ...style, top: 0, zIndex: 103, backgroundColor: "#1c71b8", width: 200, borderRight: "1px #fff solid", borderBottom: "1px #fff solid", }}>
                            <Typography style={{ color: "white", fontSize: 12, fontWeight: "bold", textAlign: "center" }}>{columnMeta.label}</Typography>
                        </TableCell>
                    ),
                    setCellProps: () => ({style2}),
                    customBodyRender: (val) => {
                        return (
                            <div className="column-1" style={{ placeSelf: 'center', textAlign: 'center', padding: 5 }}>
                                <div style={{ textAlign: 'center' }}>{val}</div>
                            </div>
                        )
                    }
                }
            }
        ];

        const loadingComponent = (
            <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)'
            }}>
                <PropagateLoader
                    // css={override}
                    size={20}
                    color={"#274B80"}
                    loading={this.state.loading}
                />
            </div>
        );


        return (
            <div style={{flex: 1, backgroundColor: '#f8f8f8', minHeight: this.props.height}}>
                <Snackbar open={this.state.alert} autoHideDuration={6000} onClose={() => this.closeAlert()}>
                    <Alert onClose={() => this.closeAlert()} severity={this.state.tipeAlert}>
                        {this.state.messageAlert}
                    </Alert>
                </Snackbar>
                <div>
                    <div className={"main-color"}
                         style={{height: 78, display: 'flex', alignItems: 'center', paddingLeft: 20}}>
muhammad ridwan's avatar
muhammad ridwan committed
333
                        <Typography style={{fontSize: '16px', color: 'white'}}>Report Status Progress
muhammad ridwan's avatar
muhammad ridwan committed
334
                            Monitoring CAFRM</Typography>
335 336 337 338
                    </div>
                    <div style={{padding: 20, width: '100%'}}>
                        <Paper style={{paddingTop: 10, paddingBottom: 50}}>
                            <div style={{borderBottom: 'solid 1px #c4c4c4'}}>
muhammad ridwan's avatar
muhammad ridwan committed
339
                                <Typography style={{fontSize: '12px', color: '#4b4b4b', margin: 10}}>Report Status Progress CAFRM</Typography>
340
                            </div>
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

                            {this.state.curMonth > 0 && this.state.curYear > 2000 && !this.state.loading && (
                                <div style={{minWidth: 'max-content', padding: '20px 20px 0px 20px'}}>
                                    <div style={{marginTop: 15, display: 'flex'}}>
                                        <Autocomplete
                                            {...this.state.listPeriodeMB}
                                            id="periode"
                                            onChange={(event, newInputValue) => this.setState({
                                                periodeMB: newInputValue,
                                                curYear: newInputValue['value'],
                                                loading: true,
                                            }, async () => {
                                                await this.getFromCallback()
                                            })}
                                            disableClearable
                                            style={{minWidth: 210, marginRight: 20}}
                                            renderInput={(params) => <TextField {...params} label="Periode" margin="normal" style={{marginTop: 7}}/>}
                                            defaultValue={this.state.periodeMB}
                                            value={this.state.periodeMB}
                                        />
                                        <Autocomplete
                                            {...this.state.listMonth}
                                            id="month"
                                            onChange={(event, newInputValue) => this.setState({
                                                month: newInputValue,
                                                curMonth: newInputValue['month_id'],
                                                loading: true
                                            }, async () => {
                                                await this.getFromCallback()
                                            })}
                                            disableClearable
                                            style={{minWidth: 210, marginRight: 20}}
                                            renderInput={(params) => <TextField {...params} label="Month" margin="normal" style={{marginTop: 7}}/>}
                                            value={this.state.month}
                                        />
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
                                        <Autocomplete
                                            {...this.state.listStatus}
                                            id="status"
                                            onChange={(event, newInputValue) => this.setState({
                                                defaultStatus: newInputValue,
                                                curStatus: newInputValue['value'],
                                                loading: true
                                            }, async () => {
                                                await this.getFromCallback()
                                            })}
                                            disableClearable
                                            style={{minWidth: 210, marginRight: 20}}
                                            renderInput={(params) => <TextField {...params} label="Status" margin="normal" style={{marginTop: 7}}/>}
                                            value={this.state.defaultStatus}
                                        />
391 392

                                    </div>
393
                                </div>
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
                            <div style={{marginTop: 20, marginBottom: 20}}>
                                <div style={{
                                    display: 'flex',
                                    justifyContent: 'space-between',
                                    padding: '0px 20px 10px 20px'
                                }}>
                                    <div></div>
                                    {this.state.curMonth > 0 && this.state.curYear > 2000 && !this.state.loading && (
                                    <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.downloadData()
                                                    }, 100);
                                                })}
                                            >
                                                <img src={Images.download}/>
                                            </button>
                                        </a>
                                        <ReactTooltip border={true} id="download" place="bottom" type="light"
                                                      effect="solid"/>
                                    </div>
                                    )}
                                </div>
                                {this.state.loading && loadingComponent}
                                {this.state.curMonth > 0 && this.state.curYear > 2000 && !this.state.loading && (
                                    <div style={{
                                        padding: "0px 20px 20px 20px",
                                        width: this.props.width - (this.props.open === true ? 350 : 100)
                                    }}>
                                        <MuiThemeProvider theme={getMuiTheme()}>
                                            <MUIDataTable
                                                data={this.state.dataTable}
                                                columns={columnMR}
                                                options={options}
                                            />
                                        </MuiThemeProvider>
                                    </div>
                                )}
                            </div>
                        </Paper>
                    </div>
                </div>
            </div>
        );
    }
}

export default RepotrCafrm;