CreateMasterDataCAT.js 26.2 KB
Newer Older
Riri Novita's avatar
Riri Novita committed
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
import React, { Component } from 'react';
import { TextField, Typography, Snackbar, withStyles } from '@material-ui/core';
import * as R from 'ramda';
import api from '../../../api';
import Autocomplete from '@material-ui/lab/Autocomplete';
import { titleCase } from '../../../library/Utils';
import format from "date-fns/format";
import { DatePicker } from '@material-ui/pickers';
import Images from '../../../assets/Images';
import MuiAlert from '@material-ui/lab/Alert';
import Constant from '../../../library/Constant';
const Alert = withStyles({
})((props) => <MuiAlert elevation={6} variant="filled" {...props} />);

export default class CreateMasterDataCAT extends Component {
    constructor(props) {
        super(props)
        this.state = {
            company: null,
            parent: null,
            reportType: null,
            weight: '',
            alert: false,
            tipeAlert: '',
            messageAlert: '',
rifkaki's avatar
rifkaki committed
26
            listPeriode: null,
27
            periode: null,
28 29
            errorWeight: false,
            msgErrorWeight: '',
d.arizona's avatar
d.arizona committed
30
            getParent: []
Riri Novita's avatar
Riri Novita committed
31 32 33 34
        }
    }

    componentDidMount() {
rifkaki's avatar
rifkaki committed
35
        this.getPeriode()
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        this.getDetailUser()
    }

    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) => {
            // console.log(response);
            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])
                        }
                    })

                    if (arrayBaru.length > 0) {
                        arrayBaru = arrayBaru.sort((a,b) => a.company_name.localeCompare(b.company_name))
                    }

                    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]) })
                } else {
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
d.arizona's avatar
d.arizona committed
88
                        if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
89 90 91 92 93 94 95 96 97 98 99
                            setTimeout(() => {
                                localStorage.removeItem(Constant.TOKEN)
                                window.location.reload();
                            }, 1000);
                        }
                    })
                }
            } else {
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error', listCompany: null, company: null })
            }
        })
Riri Novita's avatar
Riri Novita committed
100 101
    }

rifkaki's avatar
rifkaki committed
102 103 104 105 106 107 108 109 110 111
    getPeriode() {
        api.create().getPeriodeTransaction().then(response => {
            let currentYear = new Date().getFullYear()
            console.log(currentYear)
            console.log(response)
            if (response.data) {
                if (response.data.status === "success") {
                    let data = []
                    response.data.data.map((item) => {
                        // if (this.state.isApprover) {
d.arizona's avatar
d.arizona committed
112 113 114
                        if (item >= 2000 && item <= (Number(currentYear) + 1)) {
                            data.push(item)
                        }
rifkaki's avatar
rifkaki committed
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
                        // } 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 periode = String(Number(currentYear))
                    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], loading: false })
                }
            }
        })
    }

    getParentIR() {
        let payload = {
d.arizona's avatar
d.arizona committed
145 146
            "report_id": 3,
            "years": this.state.periode.periode
rifkaki's avatar
rifkaki committed
147 148 149 150 151 152
        }
        api.create().getParentItemReport(payload).then(response => {
            console.log(payload)
            console.log(response)
            if (response.data) {
                if (response.data.status === "success") {
d.arizona's avatar
d.arizona committed
153
                    this.setState({ getParent: response.data.data.filter((val) => String(val.type_item_report_name).toLocaleLowerCase() != 'break')})
rifkaki's avatar
rifkaki committed
154 155 156 157 158 159
                }
            } else {
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error', loading: false })
            }
        })
    }
Riri Novita's avatar
Riri Novita committed
160

d.arizona's avatar
d.arizona committed
161 162 163 164 165 166 167 168
    handleChange(e, item) {
        let dataSource = this.state.getParent
        let indexID = dataSource.findIndex((val) => val.item_report_id == item.item_report_id)
        // console.log(indexID)
        if (indexID != -1) {
            dataSource[indexID].weight = e.target.value
            this.setState({getParent: dataSource}, () => {
                console.log(this.state.getParent);
rifkaki's avatar
rifkaki committed
169
                this.clearMessage()
Riri Novita's avatar
Riri Novita committed
170 171
            })
        }
rifkaki's avatar
rifkaki committed
172
        // console.log(e.target.value)
Riri Novita's avatar
Riri Novita committed
173 174
    }

175 176 177 178 179 180 181 182 183 184 185 186 187 188
    handleReportName(item) {
        let indexString = String(item).indexOf('-')
        let gantiNama = String(item).substr(0, indexString)
        return gantiNama
    }

    validasi(getData) {
        console.log(getData)
        let jumlah = 0
        getData.map((item) => {
            jumlah += Number(item.weight)
        })
        console.log(jumlah)
        if ( jumlah > 100/100 ) {
syadziy's avatar
syadziy committed
189
            this.setState({ errorWeight: true, msgErrorWeight: 'Weight more than 100%' })
190
        } else if (jumlah < 100/100) {
syadziy's avatar
syadziy committed
191
            this.setState({ errorWeight: true, msgErrorWeight: 'Weight less than 100%' })
Riri Novita's avatar
Riri Novita committed
192 193
        }
        else {
194
            this.addReportItems(getData)
Riri Novita's avatar
Riri Novita committed
195 196 197
        }
    }

198
    addReportItems(getData) {
Riri Novita's avatar
Riri Novita committed
199
        // alert("test")
200 201 202 203 204 205 206 207 208
        let simpan = []
        getData.map((item) => {
            simpan.push({
                "report_id": item.report_id,
                "item_report_id": item.item_report_id,
                "weight": item.weight
            })
        })
        console.log(simpan)
Riri Novita's avatar
Riri Novita committed
209 210
        let payload = {
            "company_id": this.state.company.company_id,
211 212
            "years": this.state.periode.periode,
            "detail": simpan
Riri Novita's avatar
Riri Novita committed
213
        }
214
        console.log(payload)
Riri Novita's avatar
Riri Novita committed
215 216 217 218 219 220
        this.props.createReportItems(payload)
    }

    clearMessage() {
        this.setState({
            errorFormula: false, msgErrorFormula: '',
rifkaki's avatar
rifkaki committed
221
            errorWeight: false, msgErrorWeight: ''
Riri Novita's avatar
Riri Novita committed
222 223 224 225 226 227 228 229 230 231 232
        })
    }

    closeAlert() {
        this.setState({ alert: false })
    }

    render() {

        return (
            <div className="test app-popup-show">
rifkaki's avatar
rifkaki committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
                <div className="popup-content background-white border-radius" style={{ borderRadius: 8, maxHeight: 600 }}>
                    <div>
                        <div className="popup-panel grid grid-2x main-color" style={{ height: 64, borderTopRightRadius: 8, borderTopLeftRadius: 8 }}>
                            <div className="col-1" style={{ maxWidth: "inherit", display: 'flex', alignItems: 'center' }}>
                                <div className="popup-title">
                                    <span style={{ color: '#fff', fontSize: 16, fontWeight: 'bold' }}>Create Data</span>
                                </div>
                            </div>
                            <div className="col-2 content-right" style={{ maxWidth: "inherit", alignSelf: 'center' }}>
                                <button
                                    type="button"
                                    className="btn btn-circle btn-white"
                                    onClick={() => this.props.onClickClose()}
                                >
                                    <img src={Images.close} />
                                </button>
Riri Novita's avatar
Riri Novita committed
249 250 251
                            </div>
                        </div>
                    </div>
rifkaki's avatar
rifkaki committed
252 253 254 255 256 257
                    <div style={{ maxHeight: 500, overflow: 'scroll' }}>
                        <Snackbar open={this.state.alert} autoHideDuration={6000} onClose={() => this.closeAlert()}>
                            <Alert onClose={() => this.closeAlert()} severity={this.state.tipeAlert}>
                                {this.state.messageAlert}
                            </Alert>
                        </Snackbar>
Riri Novita's avatar
Riri Novita committed
258

rifkaki's avatar
rifkaki committed
259 260 261
                        <div className="grid grid-2x grid-mobile-none gap-15px" style={{ paddingLeft: 20, paddingRight: 20 }}>
                            <div className="column-1">
                                <div className="margin-top-10px" style={{ padding: 10, borderRadius: 5 }} >
262 263 264 265 266
                                    <Autocomplete
                                        {...this.state.listCompany}
                                        id="company"
                                        disabled={this.state.intent === 'Home' ? true : false}
                                        onChange={(event, newInputValue) => this.setState({ company: newInputValue, loading: true, previewTable: false }, () => {
267
                                            this.getParentIR()
268 269 270
                                        })}
                                        disableClearable
                                        style={{ width: 250 }}
rifkaki's avatar
rifkaki committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
                                        renderInput={(params) =>
                                            <TextField {...params}
                                                label="Company Name"
                                                InputLabelProps={{
                                                    style: {
                                                        fontSize: 11,
                                                        fontFamily: 'Nunito Sans, sans-serif',
                                                        color: '#7e8085'
                                                    }
                                                }}
                                                error={this.state.errorPerusahaan}
                                                helperText={this.state.msgErrorPerusahaan}
                                                InputProps={{ ...params.InputProps, style: { fontSize: 11, fontFamily: 'Nunito Sans, sans-serif' } }}
                                            />}
                                        value={this.state.company}
                                    />
                                </div>
Riri Novita's avatar
Riri Novita committed
288
                            </div>
rifkaki's avatar
rifkaki committed
289 290 291
                            <div className="column-2">
                                <div className="margin-top-10px" style={{ padding: 10, borderRadius: 5 }}>
                                    <Autocomplete
rifkaki's avatar
rifkaki committed
292 293 294 295
                                        {...this.state.listPeriode}
                                        id="year"
                                        onChange={(event, newInputValue) => this.setState({ periode: newInputValue, loading: true, previewTable: false }, () => {
                                            this.getParentIR()
rifkaki's avatar
rifkaki committed
296
                                        })}
297
                                        disableClearable
rifkaki's avatar
rifkaki committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311
                                        renderInput={(params) =>
                                            <TextField {...params}
                                                label="Year"
                                                InputLabelProps={{
                                                    style: {
                                                        fontSize: 11,
                                                        fontFamily: 'Nunito Sans, sans-serif',
                                                        color: '#7e8085'
                                                    }
                                                }}
                                                error={this.state.errorJenisLaporan}
                                                helperText={this.state.msgErrorJenisLaporan}
                                                InputProps={{ ...params.InputProps, style: { fontSize: 11, fontFamily: 'Nunito Sans, sans-serif' } }}
                                            />}
rifkaki's avatar
rifkaki committed
312
                                        value={this.state.periode}
rifkaki's avatar
rifkaki committed
313 314
                                    />
                                </div>
Riri Novita's avatar
Riri Novita committed
315 316 317
                            </div>
                        </div>

d.arizona's avatar
d.arizona committed
318 319 320 321 322 323
                        {this.state.getParent.map((item, index) => {
                            return (
                                <div>
                                    <div style={{ borderBottom: 'solid 1px #c4c4c4', margin: 20, marginBottom: 10 }} >
                                        <Typography style={{ fontSize: '12px', color: '#4b4b4b', padding: 10, fontWeight: 'bold' }}>{`Item Report - ${index + 1}`}</Typography>
                                    </div>
Riri Novita's avatar
Riri Novita committed
324

d.arizona's avatar
d.arizona committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
                                    <div className="grid grid-2x grid-mobile-none gap-15px" style={{ paddingLeft: 20, paddingRight: 20 }}>
                                        <div className="column-1">
                                            <div className="margin-top-10px" style={{ backgroundColor: '#e8e8e8', padding: 10, borderRadius: 5 }}>
                                                <TextField
                                                    style={{ width: '100%' }}
                                                    value={item.item_report_id}
                                                    id="ID"
                                                    label="ID"
                                                    disabled
                                                    inputProps={{
                                                        style: {
                                                            fontSize: 11,
                                                            fontFamily: 'Nunito Sans, sans-serif',
                                                        }
                                                    }}
                                                    InputLabelProps={{
                                                        style: {
                                                            fontSize: 11,
                                                            fontFamily: 'Nunito Sans, sans-serif',
                                                            color: '#7e8085',
                                                        }
                                                    }}
                                                />
                                            </div>
                                        </div>
                                        <div className="column-2">
                                            <div className="margin-top-10px" style={{ padding: 10, borderRadius: 5 }}>
                                                <TextField
                                                    style={{ width: '100%' }}
                                                    id="reportName1"
                                                    name="reportName1"
                                                    label="Report Name"
                                                    disabled
                                                    value={item.report_name}
                                                    inputProps={{
                                                        min: 0,
                                                        style: {
                                                            fontSize: 11,
                                                            fontFamily: 'Nunito Sans, sans-serif',
                                                        }
                                                    }}
                                                    InputLabelProps={{
                                                        style: {
                                                            fontSize: 11,
                                                            fontFamily: 'Nunito Sans, sans-serif',
                                                            color: '#7e8085',
                                                        }
                                                    }}
                                                >
                                                </TextField>
                                            </div>
                                        </div>
                                    </div>
Riri Novita's avatar
Riri Novita committed
378

d.arizona's avatar
d.arizona committed
379 380 381 382 383 384 385 386 387
                                    <div className="grid grid-2x grid-mobile-none gap-15px" style={{ paddingLeft: 20, paddingRight: 20 }}>
                                        <div className="column-1">
                                            <div className="margin-top-10px" style={{ padding: 10, borderRadius: 5 }}>
                                                <TextField
                                                    style={{ width: '100%' }}
                                                    id="itemReportName1"
                                                    name="itemReportName1"
                                                    label="Item Report Name"
                                                    disabled
388
                                                    value={this.handleReportName(item.description)}
d.arizona's avatar
d.arizona committed
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
                                                    inputProps={{
                                                        min: 0,
                                                        style: {
                                                            fontSize: 11,
                                                            fontFamily: 'Nunito Sans, sans-serif',
                                                        }
                                                    }}
                                                    InputLabelProps={{
                                                        style: {
                                                            fontSize: 11,
                                                            fontFamily: 'Nunito Sans, sans-serif',
                                                            color: '#7e8085',
                                                        }
                                                    }}
                                                >
                                                </TextField>
                                            </div>
                                        </div>
                                        <div className="column-2">
                                            <div className="margin-top-10px" style={{ padding: 10, borderRadius: 5 }}>
                                                <TextField
                                                    style={{ width: '100%' }}
                                                    id="weight1"
                                                    name="weight1"
                                                    label="Weight"
                                                    value={item.weight}
415 416
                                                    error={this.state.errorWeight}
                                                    helperText={this.state.msgErrorWeight}
d.arizona's avatar
d.arizona committed
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
                                                    onChange={(e) => this.handleChange(e, item)}
                                                    inputProps={{
                                                        style: {
                                                            fontSize: 11,
                                                            fontFamily: 'Nunito Sans, sans-serif',
                                                        }
                                                    }}
                                                    InputLabelProps={{
                                                        style: {
                                                            fontSize: 11,
                                                            fontFamily: 'Nunito Sans, sans-serif',
                                                            color: '#7e8085',
                                                        }
                                                    }}
                                                >
                                                </TextField>
                                            </div>
                                        </div>
                                    </div>
rifkaki's avatar
rifkaki committed
436
                                </div>
d.arizona's avatar
d.arizona committed
437 438
                            )
                        })}
Riri Novita's avatar
Riri Novita committed
439

rifkaki's avatar
rifkaki committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
                        <div className="grid grid-2x grid-mobile-none gap-15px" style={{ paddingLeft: 20, paddingRight: 20 }}>
                            <div className="column-1">
                                <div className="margin-top-10px" style={{ backgroundColor: '#e8e8e8', padding: 10, borderRadius: 5 }}>
                                    <TextField
                                        style={{ width: '100%' }}
                                        defaultValue={"active"}
                                        id="status"
                                        label="Status"
                                        disabled
                                        inputProps={{
                                            style: {
                                                fontSize: 11,
                                                fontFamily: 'Nunito Sans, sans-serif',
                                            }
                                        }}
                                        InputLabelProps={{
                                            style: {
                                                fontSize: 11,
                                                fontFamily: 'Nunito Sans, sans-serif',
                                                color: '#7e8085',
                                            }
                                        }}
                                    />
                                </div>
Riri Novita's avatar
Riri Novita committed
464 465 466
                            </div>
                        </div>

rifkaki's avatar
rifkaki committed
467 468 469 470 471 472 473
                        <div className="margin-top-10px" style={{ paddingTop: 10, paddingBottom: 30, paddingRight: 30, paddingLeft: 30 }}>
                            <div style={{ display: 'flex' }}>
                                <Typography style={{ fontSize: 11, width: '13%' }}>Created By :</Typography>
                            </div>
                            <div style={{ display: 'flex' }}>
                                <Typography style={{ fontSize: 11, width: '13%' }}>Updated By :</Typography>
                            </div>
Riri Novita's avatar
Riri Novita committed
474 475
                        </div>
                    </div>
rifkaki's avatar
rifkaki committed
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
                    <div>
                        <div className="border-top grid grid-2x" style={{ height: 56, backgroundColor: '#f5f5f5', paddingLeft: 20, paddingRight: 20 }}>
                            <div className="column-1" style={{ alignSelf: 'center' }}>
                                <button
                                    type="button"
                                    onClick={() => this.props.onClickClose()}
                                >
                                    <div style={{ width: 102, height: 30, border: 'solid 1px #354960', borderRadius: 5, alignItems: 'center', display: 'flex', justifyContent: 'center' }}>
                                        <span style={{ color: '#354960', fontSize: 11 }}>Cancel</span>
                                    </div>
                                </button>
                            </div>
                            <div className="column-2" style={{ display: 'flex', justifyContent: 'flex-end', alignItems: 'center' }}>
                                <button
                                    type="button"
491
                                    onClick={() => this.validasi(this.state.getParent)}
rifkaki's avatar
rifkaki committed
492 493 494 495 496 497
                                >
                                    <div style={{ width: 102, height: 30, backgroundColor: '#354960', borderRadius: 5, alignItems: 'center', display: 'flex', justifyContent: 'center' }}>
                                        <span style={{ color: '#fff', fontSize: 11 }}>Save</span>
                                    </div>
                                </button>
                            </div>
Riri Novita's avatar
Riri Novita committed
498 499 500 501 502 503 504
                        </div>
                    </div>
                </div>
            </div>
        )
    }
}