Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2.  
  3. import Button from '@material-ui/core/Button';
  4. import Grid from '@material-ui/core/Grid';
  5. import withStyles from '@material-ui/core/styles/withStyles';
  6. import Dialog from '@material-ui/core/Dialog';
  7. import DialogTitle from '@material-ui/core/DialogTitle';
  8. import DialogActions from '@material-ui/core/DialogActions';
  9.  
  10. import PropTypes from 'prop-types';
  11. import { connect } from 'react-redux';
  12. import compose from 'recompose/compose';
  13. import format from 'date-fns/fp/format';
  14. import compareDesc from 'date-fns/compareDesc';
  15. import susun from 'lodash/fp/compose';
  16.  
  17. import { nameAddressSelector } from 'utils/selector/profileSelector';
  18. import getHospitalId from 'utils/selector/hospitalIdSelector';
  19. import { getExpiredDateByMedicineId } from 'api/apotek';
  20. import { getPracticeListByHospitalId } from 'api/setting';
  21. import Modal from 'components/Modal';
  22. import exportXL from 'components/office/contents/report/utils/exportXL';
  23.  
  24. const tanggal = format('DD-MM-YYYY');
  25.  
  26. const styles = () => ({
  27.   paper: {
  28.     position: 'absolute',
  29.     border: '1px solid black',
  30.   },
  31.   titleSection: {
  32.     padding: '0 32',
  33.     display: 'flex',
  34.     justifyContent: 'flex-start',
  35.     alignItems: 'center',
  36.     flexDirection: 'column',
  37.     borderBottom: '1px solid black',
  38.   },
  39.   titleItem: {
  40.     padding: 0,
  41.     margin: 0,
  42.   },
  43.   font: {
  44.     margin: '0 !important',
  45.     padding: '0 !important',
  46.   },
  47. });
  48.  
  49. const Styled = withStyles(styles);
  50.  
  51. const getLastExpiredDate = expiredDates => (resep) => {
  52.   if (expiredDates.length > 0) {
  53.     const lastExpired = expiredDates.sort((exp1, exp2) =>
  54.       compareDesc(exp1.expiredDate, exp2.expiredDate),
  55.     )[0].expiredDate;
  56.     const newResep = { ...resep, expiredDate: lastExpired };
  57.     return newResep;
  58.   }
  59.   return resep;
  60. };
  61.  
  62. const getMatchingPracticeList = practiceList => (resep) => {
  63.   console.log({ practiceList, resep });
  64.   if (practiceList.items.length > 0) {
  65.     const praktik = practiceList.items.find(
  66.       praktek => praktek.id === resep.practiceId,
  67.     );
  68.     console.log({ praktik });
  69.     return { ...resep, SIP: praktik.Dokters.registrasiSIP[0].nomer };
  70.   }
  71.   return resep;
  72. };
  73.  
  74. const LabelObat = Styled((props) => {
  75.   const { classes, obat, i, title, ukuran } = props;
  76.   const font = {
  77.     fontSize: '11px',
  78.   };
  79.   return (
  80.     <div
  81.       className={`${classes.paper} printStruk`}
  82.       style={{
  83.         top: i !== 0 ? `${i * (ukuran.height + 20)}mm` : '0',
  84.         width: `${ukuran.width === 50 ? ukuran.width + 25 : ukuran.width + 35}mm `,
  85.         height: `${ukuran.height + 20}mm `,
  86.       }}
  87.       key={obat.id}
  88.     >
  89.       <div className={classes.titleSection}>
  90.         <p style={font} className={classes.titleItem}>{title.name}</p>
  91.         <p style={font} className={classes.titleItem}>{title.address}</p>
  92.         <p style={font} className={classes.titleItem}>
  93.           Apoteker: {obat.updatedName ? obat.updatedName : '-'}
  94.         </p>
  95.         <p style={font} className={classes.titleItem}>SIK: {obat.SIP}</p>
  96.       </div>
  97.       <Grid container style={{ margin: 5 }}>
  98.         <Grid className={classes.font} style={font} item xs={8}>
  99.           No. {obat.milis}
  100.         </Grid>
  101.         <Grid className={classes.font} style={font} item xs={4}>
  102.           Tgl. {tanggal(new Date(obat.createdAt))}
  103.         </Grid>
  104.       </Grid>
  105.       <Grid container>
  106.         <Grid item xs={2}>
  107.           &nbsp;
  108.         </Grid>
  109.         <Grid className={classes.font} style={font} item xs={8}>
  110.           Nama: {obat.Mrs.Patients.nama}({obat.mrNo})
  111.         </Grid>
  112.         <Grid className={classes.font} style={font} item xs={2}>
  113.          &nbsp;
  114.         </Grid>
  115.       </Grid>
  116.       <Grid container>
  117.         <Grid item xs={2}>
  118.           &nbsp;
  119.         </Grid>
  120.         <Grid className={classes.font} style={font} item xs={4}>
  121.           {obat.medicineName}
  122.         </Grid>
  123.         <Grid className={classes.font} style={font} item xs={4}>
  124.           {obat.quantity} {obat.unit}
  125.         </Grid>
  126.         <Grid className={classes.font} style={font} item xs={2}>
  127.           &nbsp;
  128.         </Grid>
  129.       </Grid>
  130.       <Grid container>
  131.         <Grid item xs={2}>
  132.           &nbsp;
  133.         </Grid>
  134.         <Grid className={classes.font} style={font} item xs={8}>
  135.           X
  136.         </Grid>
  137.         <Grid className={classes.font} style={font} item xs={2}>
  138.           &nbsp;
  139.         </Grid>
  140.       </Grid>
  141.       <div style={{ ...font, padding: '0 1em', margin: 0 }}>
  142.         ED: {obat.expiredDate ? tanggal(new Date(obat.expiredDate)) : null}
  143.       </div>
  144.       <div
  145.         className={classes.font}
  146.         style={{
  147.           ...font,
  148.           display: 'flex',
  149.           justifyContent: 'center',
  150.           flexDirection: 'column',
  151.           alignItems: 'center',
  152.           alignContent: 'stretch',
  153.           padding: 0,
  154.           margin: 0,
  155.         }}
  156.       >
  157.         <p style={{ margin: 0 }}>Sebelum / Saat / Sesudah Makan</p>
  158.         <p style={{ margin: 0 }}>Semoga Lekas Sembuh</p>
  159.         <p style={{ margin: 0, marginBottom: 5 }}>Jauhkan Dari Jangkauan Anak Anak</p>
  160.       </div>
  161.     </div>
  162.   );
  163. });
  164.  
  165. class PrintLabel extends React.Component {
  166.   state = {
  167.     openPrint: false,
  168.     openDialog: false,
  169.     obat: [],
  170.     ukuran: {
  171.       width: 50,
  172.       height: 40,
  173.     },
  174.     jumlahPrint: 0,
  175.     isFromEmr: !!this.props.dataObat.Mrs.Appointments,
  176.   };
  177.  
  178.   componentDidMount() {
  179.     const close = susun(
  180.       this.closePrint,
  181.       this.closeDialog,
  182.     );
  183.     window.addEventListener('afterprint', close);
  184.   }
  185.  
  186.   componentWillUnmount() {
  187.     const close = susun(
  188.       this.closePrint,
  189.       this.closeDialog,
  190.     );
  191.     window.addEventListener('afterprint', close);
  192.   }
  193.  
  194.   setUkuranKertas = payload => this.setState({ ukuran: payload })
  195.  
  196.   openDialog = () => this.setState({ openDialog: true })
  197.   closeDialog = () => this.setState({ openDialog: false })
  198.  
  199.   generateData = (cb) => {
  200.     const { dataObat, hospitalId } = this.props;
  201.     const obat = dataObat.Mrs.Prescriptions.map(resep =>
  202.       Promise.all([
  203.         getExpiredDateByMedicineId(resep.medicineId),
  204.         getPracticeListByHospitalId(hospitalId),
  205.       ])
  206.         .then(([expiredDates, practiceList]) =>
  207.           Promise.resolve([expiredDates.data, practiceList.data]),
  208.         )
  209.         .then(([expiredDates, practiceList]) => {
  210.           const resepWithExpiredDates = getLastExpiredDate(expiredDates);
  211.           const praktikSIP = getMatchingPracticeList(practiceList);
  212.           const execute = susun(resepWithExpiredDates, praktikSIP);
  213.           return execute(resep);
  214.         })
  215.         .catch(err => console.error(err)),
  216.     );
  217.     const racik = dataObat.Mrs.ScourPrescriptions.map(racikan =>
  218.       getPracticeListByHospitalId(hospitalId)
  219.         .then(practiceList => Promise.resolve(practiceList.data))
  220.         .then(practiceList => getMatchingPracticeList(practiceList)(racikan))
  221.         .catch(err => console.error(err)),
  222.     );
  223.     Promise.all([...obat, ...racik])
  224.       .then(resep => cb(resep))
  225.       .catch(err => console.error(err));
  226.   }
  227.  
  228.   openPrint = ukuran => () => {
  229.     this.generateData(resep => this.setState(
  230.       { obat: resep, openPrint: true, ukuran },
  231.     ));
  232.   };
  233.   closePrint = () => this.setState({ openPrint: false });
  234.  
  235.   formatObat = (payload) => {
  236.     const { title, dataObat } = this.props;
  237.     const formatObat = resepObat => ({
  238.       'Nama Klinik': title.name,
  239.       'Alamat Klinik': title.address,
  240.       Apoteker: resepObat.updatedName || '-',
  241.       SIK: resepObat.SIP,
  242.       Nomor: resepObat.milis,
  243.       Tanggal: tanggal(new Date(resepObat.createdAt)),
  244.       'Nama Pasien': resepObat.Mrs.Patients.nama,
  245.       'Nomor MR Pasien': resepObat.mrNo,
  246.       'Nama Obat': resepObat.medicineName,
  247.       'Jumlah Obat': resepObat.quantity,
  248.       'Satuan Obat': resepObat.unit,
  249.       'Tanggal Kadaluarsa': resepObat.expiredDate ? tanggal(new Date(resepObat.expiredDate)) : '-',
  250.       'Keterangan 1': 'Sebelum / Sesudah Makan',
  251.       'Keterangan 2': 'Semoga Lekas Sembuh',
  252.       'Keterangan 3': 'Jauhkan dari Jangkauan Anak-Anak',
  253.     });
  254.     const presc = payload.map((resep) => {
  255.       const obat = { ...dataObat, ...resep };
  256.       return formatObat(obat);
  257.     });
  258.  
  259.     return presc;
  260.   }
  261.  
  262.   exportExcel = () => {
  263.     this.generateData((resep) => {
  264.       const json = this.formatObat(resep);
  265.       exportXL('Label Obat', json);
  266.     });
  267.   }
  268.  
  269.   openPrintDialog = payload => () => {
  270.     const { title, dataObat, hospitalId } = this.props;
  271.     const { jumlahPrint } = this.state;
  272.     let obat = dataObat.Mrs.Prescriptions.map(resep =>
  273.     Promise.all([
  274.       getExpiredDateByMedicineId(resep.medicineId),
  275.       getPracticeListByHospitalId(hospitalId),
  276.     ])
  277.       .then(([expiredDates, practiceList]) =>
  278.         Promise.resolve([expiredDates.data, practiceList.data]),
  279.       )
  280.       .then(([expiredDates, practiceList]) => {
  281.         const resepWithExpiredDates = getLastExpiredDate(expiredDates);
  282.         const praktikSIP = getMatchingPracticeList(practiceList);
  283.         const execute = susun(resepWithExpiredDates, praktikSIP);
  284.         return execute(resep);
  285.       })
  286.       .catch(err => console.error(err)),
  287.     );
  288.     Promise.all(obat)
  289.       .then((resep) => {
  290.         obat = [...resep, ...dataObat.Mrs.ScourPrescriptions];
  291.         const LabelObatTitled = props =>
  292.           <LabelObat {...props} title={title} />;
  293.         this.props.openPrintDialog({
  294.           body: (
  295.             <LabelObatTitled
  296.               obat={{
  297.                 ...dataObat,
  298.                 expiredDate: obat[jumlahPrint].expiredDate,
  299.                 SIP: obat[jumlahPrint].SIP,
  300.                 medicineName: obat[jumlahPrint].medicineName,
  301.                 unit: obat[jumlahPrint].unit,
  302.                 quantity: obat[jumlahPrint].quantity,
  303.                 mrNo: obat[jumlahPrint].mrNo,
  304.                 cignaLabel: obat[jumlahPrint].cignaLabel,
  305.               }}
  306.               key={obat[jumlahPrint].id}
  307.               ukuran={payload}
  308.               i={jumlahPrint}
  309.             />
  310.           ),
  311.           cb: () => {
  312.             if (jumlahPrint < obat.length) {
  313.               this.setState({ jumlahPrint: jumlahPrint + 1 }, () => {
  314.                 this.openPrintDialog(payload);
  315.               });
  316.             }
  317.           },
  318.         });
  319.       },
  320.       ).catch(err => console.error(err));
  321.   }
  322.  
  323.   render() {
  324.     const { openDialog, openPrint } = this.state;
  325.     const { dataObat, title } = this.props;
  326.     const isDisabled = dataObat.status !== 'succeed';
  327.     const LabelObatTitled = props =>
  328.       <LabelObat {...props} title={title} />;
  329.     return (
  330.       <React.Fragment>
  331.         {
  332.           this.state.isFromEmr &&
  333.           <Button
  334.             onClick={this.openDialog}
  335.             disabled={isDisabled}
  336.             style={{ display: isDisabled ? 'none' : 'initial' }}
  337.             variant="outlined"
  338.           >
  339.             Label
  340.           </Button>
  341.         }
  342.         <Dialog className="hideForPrint" open={openDialog} onClose={this.closeDialog}>
  343.           <DialogTitle>Pilih Ukuran Kertas Anda</DialogTitle>
  344.           <DialogActions>
  345.             <Button onClick={this.openPrint({ width: 70, height: 40 })} >70 x 40 mm</Button>
  346.             <Button onClick={this.openPrint({ width: 65, height: 40 })}>65 x 40 mm</Button>
  347.             <Button onClick={this.openPrint({ width: 50, height: 40 })}>50 x 40 mm</Button>
  348.             <Button onClick={this.exportExcel}>Export Excel</Button>
  349.           </DialogActions>
  350.         </Dialog>
  351.         {openPrint && (
  352.           <Modal isOpen={openPrint}>
  353.             <React.Fragment>
  354.               {this.state.obat.map((obat, index) => (
  355.                 <LabelObatTitled
  356.                   obat={{
  357.                     ...dataObat,
  358.                     expiredDate: obat.expiredDate,
  359.                     SIP: obat.SIP,
  360.                     medicineName: obat.medicineName,
  361.                     unit: obat.unit,
  362.                     quantity: obat.quantity,
  363.                     mrNo: obat.mrNo,
  364.                     cignaLabel: obat.cignaLabel,
  365.                   }}
  366.                   key={obat.id}
  367.                   ukuran={this.state.ukuran}
  368.                   i={index}
  369.                 />
  370.                   ))}
  371.             </React.Fragment>
  372.           </Modal>
  373.         )}
  374.       </React.Fragment>
  375.     );
  376.   }
  377. }
  378.  
  379. PrintLabel.propTypes = {
  380.   title: PropTypes.object.isRequired,
  381.   dataObat: PropTypes.object.isRequired,
  382.   hospitalId: PropTypes.string.isRequired,
  383.   openPrintDialog: PropTypes.func.isRequired,
  384. };
  385.  
  386. const mapState = state => ({
  387.   title: nameAddressSelector(state),
  388.   hospitalId: getHospitalId(state),
  389. });
  390.  
  391. const mapDispatch = dispatch => ({
  392.   openPrintDialog: dispatch.print.openPrintDialog,
  393. });
  394.  
  395. export default compose(connect(mapState, mapDispatch))(PrintLabel);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement