25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

436 lines
20 KiB

  1. // material-ui
  2. import {
  3. Grid, Button, Checkbox, FormControlLabel, Typography,
  4. Dialog, DialogTitle, DialogContent, DialogActions,
  5. } from '@mui/material';
  6. // import { FormControlLabel } from '@material-ui/core';
  7. import MainCard from "components/MainCard";
  8. import * as React from "react";
  9. import { useFormik } from 'formik';
  10. import * as yup from 'yup';
  11. import { useEffect, useState } from "react";
  12. import * as HttpUtils from 'utils/HttpUtils';
  13. import * as UrlUtils from "utils/ApiPathConst";
  14. import * as FieldUtils from "utils/FieldUtils";
  15. import * as ComboData from "utils/ComboData";
  16. const LoadingComponent = Loadable(lazy(() => import('../../extra-pages/LoadingComponent')));
  17. import Loadable from 'components/Loadable';
  18. import { lazy } from 'react';
  19. import { notifySaveSuccess } from 'utils/CommonFunction';
  20. import { useIntl } from "react-intl";
  21. import {PNSPS_BUTTON_THEME} from "themes/buttonConst";
  22. import {ThemeProvider} from "@emotion/react";
  23. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  24. const OrganizationCard = ({ userData, loadDataFun, id, setEditModeFun }) => {
  25. const intl = useIntl();
  26. const [creditorConfirmPopUp, setCreditorConfirmPopUp] = React.useState(false);
  27. const [nonCreditorConfirmPopUp, setNonCreditorConfirmPopUp] = React.useState(false);
  28. const [currentUserData, setCurrentUserData] = useState({});
  29. const [editMode, setEditMode] = useState(false);
  30. const [createMode, setCreateMode] = useState(false);
  31. const [onReady, setOnReady] = useState(false);
  32. useEffect(() => {
  33. //if state data are ready and assign to different field
  34. // console.log(currentApplicationDetailData)
  35. if (Object.keys(currentUserData).length > 0) {
  36. setOnReady(true);
  37. }
  38. }, [currentUserData]);
  39. function displayErrorMsg(errorMsg) {
  40. return <Typography variant="errorMessage1">{errorMsg}</Typography>
  41. }
  42. const formik = useFormik({
  43. enableReinitialize: true,
  44. initialValues: currentUserData,
  45. validationSchema: yup.object().shape({
  46. enCompanyName: yup.string().max(255).required(displayErrorMsg(intl.formatMessage({ id: 'userRequireEnglishName' }))),
  47. chCompanyName: yup.string().max(255, displayErrorMsg(intl.formatMessage({ id: 'userRequireChineseName' }))).nullable(),
  48. addressLine1: yup.string().max(40).required(displayErrorMsg(intl.formatMessage({ id: 'validateAddressLine1' }))),
  49. addressLine2: yup.string().max(40, displayErrorMsg(intl.formatMessage({ id: 'noMoreThen40Words' }))),
  50. addressLine3: yup.string().max(40, displayErrorMsg(intl.formatMessage({ id: 'noMoreThen40Words' }))),
  51. fax_countryCode: yup.string().min(3, displayErrorMsg(intl.formatMessage({ id: 'requireDialingCode' }))).nullable(),
  52. tel_countryCode: yup.string().min(3, displayErrorMsg(intl.formatMessage({ id: 'requireDialingCode' }))),
  53. phoneNumber: yup.string().min(8, displayErrorMsg(intl.formatMessage({ id: 'requiredValidNumber' }))).required(displayErrorMsg(intl.formatMessage({ id: 'requireContactNumber' }))),
  54. faxNumber: yup.string().min(8, displayErrorMsg(intl.formatMessage({ id: 'require8Number' }))).nullable(),
  55. brExpiryDate: yup.string().min(8).required(displayErrorMsg(intl.formatMessage({ id: 'pleaseFillInBusinessRegCertValidityDate' }))),
  56. brNo: yup.string().required(displayErrorMsg(intl.formatMessage({ id: 'pleaseFillInBusinessRegCertNumber' }))).test('checkBrNoFormat', displayErrorMsg(displayErrorMsg(intl.formatMessage({ id: 'pleaseFillInValidBusinessRegCertNumber' }))), function (value) {
  57. var brNo_pattern = /[0-9]{8}/
  58. if (value !== undefined) {
  59. if (value.match(brNo_pattern)) {
  60. return true
  61. } else {
  62. return false
  63. }
  64. }
  65. }),
  66. }),
  67. onSubmit: vaule => {
  68. console.log(vaule)
  69. HttpUtils.post({
  70. url: UrlUtils.POST_ORG_SAVE_PATH,
  71. params: {
  72. id: id > 0 ? id : null,
  73. enCompanyName: vaule.enCompanyName,
  74. chCompanyName: vaule.chCompanyName,
  75. brNo: vaule.brNo,
  76. brExpiryDate: vaule.brExpiryDate,
  77. enCompanyNameTemp: vaule.enCompanyNameTemp,
  78. chCompanyNameTemp: vaule.chCompanyNameTemp,
  79. brExpiryDateTemp: vaule.brExpiryDateTemp,
  80. contactPerson: vaule.contactPerson,
  81. contactTel: {
  82. countryCode: vaule.tel_countryCode,
  83. phoneNumber: vaule.phoneNumber
  84. },
  85. faxNo: {
  86. countryCode: vaule.fax_countryCode,
  87. faxNumber: vaule.faxNumber
  88. },
  89. addressTemp: {
  90. country: vaule.country.id,
  91. district: vaule.district.id,
  92. addressLine1: vaule.addressLine1,
  93. addressLine2: vaule.addressLine2,
  94. addressLine3: vaule.addressLine3,
  95. },
  96. //creditor: vaule.creditor,
  97. },
  98. onSuccess: function () {
  99. notifySaveSuccess()
  100. loadDataFun();
  101. setEditMode(false);
  102. }
  103. });
  104. }
  105. });
  106. useEffect(() => {
  107. setEditModeFun(editMode);
  108. }, [editMode]);
  109. useEffect(() => {
  110. if (Object.keys(userData).length > 0) {
  111. setCreateMode(id <= 0);
  112. setEditMode(id <= 0);
  113. setCurrentUserData(userData);
  114. }
  115. }, [userData]);
  116. // useEffect(() => {
  117. // if (Object.keys(userData).length > 0) {
  118. // if(userData.creditor === undefined||userData.creditor === null){
  119. // userData.creditor = false
  120. // }
  121. // setCurrentUserData(userData);
  122. // }
  123. // }, []);
  124. const onEditClick = () => {
  125. setEditMode(true);
  126. };
  127. const markAsCreditor = () => {
  128. setCreditorConfirmPopUp(false);
  129. HttpUtils.get({
  130. url: UrlUtils.GET_ORG_MARK_AS_CREDITOR + "/" + id,
  131. onSuccess: () => {
  132. loadDataFun();
  133. }
  134. });
  135. }
  136. const markAsNonCreditor = () => {
  137. setNonCreditorConfirmPopUp(false);
  138. HttpUtils.get({
  139. url: UrlUtils.GET_ORG_MARK_AS_NON_CREDITOR + "/" + id,
  140. onSuccess: () => {
  141. loadDataFun();
  142. }
  143. });
  144. }
  145. return (
  146. <MainCard elevation={0}
  147. border={false}
  148. content={false}
  149. >
  150. <form onSubmit={formik.handleSubmit} style={{ padding: 24 }}>
  151. {/*top*/}
  152. <Grid item s={12} md={12} lg={12} sx={{ mb: 3 }} alignItems={"start"} justifyContent="center">
  153. <Grid container maxWidth justifyContent="flex-start">
  154. {editMode ?
  155. <>
  156. {createMode ?
  157. <>
  158. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  159. <Grid item sx={{ ml: 0, mr: 3 }}>
  160. <Button
  161. variant="contained"
  162. type="submit"
  163. color="success"
  164. >
  165. Create
  166. </Button>
  167. </Grid>
  168. </ThemeProvider>
  169. </> :
  170. <>
  171. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  172. <Grid item sx={{ ml: 0, mr: 3 }}>
  173. <Button
  174. variant="contained"
  175. onClick={loadDataFun}
  176. color="cancel"
  177. >
  178. Reset & Back
  179. </Button>
  180. </Grid>
  181. <Grid item sx={{ ml: 3, mr: 3 }}>
  182. <Button
  183. variant="contained"
  184. type="submit"
  185. color="success"
  186. >
  187. Save
  188. </Button>
  189. </Grid>
  190. </ThemeProvider>
  191. </>
  192. }
  193. </>
  194. :
  195. <>
  196. <Grid item sx={{ ml: 0, mr: 3 }}>
  197. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  198. <Button
  199. variant="contained"
  200. onClick={onEditClick}
  201. color="success"
  202. >
  203. Edit
  204. </Button>
  205. </ThemeProvider>
  206. </Grid>
  207. {
  208. currentUserData.creditor ?
  209. <Grid item sx={{ ml: 3, mr: 3 }}>
  210. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  211. <Button
  212. variant="contained"
  213. color="error"
  214. onClick={() => setNonCreditorConfirmPopUp(true)}
  215. >
  216. Mark as Non-Creditor
  217. </Button>
  218. </ThemeProvider>
  219. </Grid>
  220. :
  221. <Grid item sx={{ ml: 3, mr: 3 }}>
  222. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  223. <Button
  224. variant="contained"
  225. color="orange"
  226. onClick={() => setCreditorConfirmPopUp(true)}
  227. >
  228. Mark as Creditor
  229. </Button>
  230. </ThemeProvider>
  231. </Grid>
  232. }
  233. </>
  234. }
  235. </Grid>
  236. </Grid>
  237. {/*top*/}
  238. {!onReady ?
  239. <LoadingComponent />
  240. :
  241. <Grid container spacing={1}>
  242. <Grid item xs={12}>
  243. <Typography variant="h4" sx={{ mb: 2, mr: 3, borderBottom: "1px solid black" }}>
  244. Organization Details
  245. </Typography>
  246. </Grid>
  247. <Grid item lg={4} >
  248. {FieldUtils.getTextField({
  249. label: FieldUtils.notNullFieldLabel("BR No.:"),
  250. valueName: "brNo",
  251. disabled: (!editMode && !createMode),
  252. form: formik
  253. })}
  254. </Grid>
  255. <Grid item lg={4} >
  256. <FormControlLabel
  257. control={<Checkbox checked={formik.values.creditor} />}
  258. label="is Creditor"
  259. name="creditor"
  260. onChange={() => {
  261. formik.setFieldValue("creditor", !formik.values.creditor);
  262. }}
  263. disabled={true}
  264. //disabled={!editMode && !createMode}
  265. />
  266. </Grid>
  267. <Grid item lg={4} ></Grid>
  268. <Grid item lg={4} >
  269. {FieldUtils.getTextField({
  270. label: FieldUtils.notNullFieldLabel("Name (Eng):"),
  271. valueName: "enCompanyName",
  272. disabled: (!editMode && !createMode),
  273. form: formik
  274. })}
  275. </Grid>
  276. <Grid item lg={4} >
  277. {FieldUtils.getTextField({
  278. label: "Name (Ch):",
  279. valueName: "chCompanyName",
  280. disabled: (!editMode && !createMode),
  281. form: formik
  282. })}
  283. </Grid>
  284. <Grid item lg={4} >
  285. {FieldUtils.getDateField({
  286. label: FieldUtils.notNullFieldLabel("Expiry Date:"),
  287. valueName: "brExpiryDate",
  288. disabled: (!editMode && !createMode),
  289. form: formik
  290. })}
  291. </Grid>
  292. <Grid item lg={4} >
  293. {FieldUtils.getTextField({
  294. label: FieldUtils.notNullFieldLabel("Contact Person:"),
  295. valueName: "contactPerson",
  296. disabled: (!editMode && !createMode),
  297. form: formik
  298. })}
  299. </Grid>
  300. <Grid item lg={4} >
  301. {FieldUtils.getPhoneField({
  302. label: FieldUtils.notNullFieldLabel("Contact Tel:"),
  303. valueName: {
  304. code: "tel_countryCode",
  305. num: "phoneNumber"
  306. },
  307. disabled: (!editMode && !createMode),
  308. form: formik
  309. })}
  310. </Grid>
  311. <Grid item lg={4} >
  312. {FieldUtils.getPhoneField({
  313. label: "Fax No:",
  314. valueName: {
  315. code: "fax_countryCode",
  316. num: "faxNumber"
  317. },
  318. disabled: (!editMode && !createMode),
  319. form: formik
  320. })}
  321. </Grid>
  322. <Grid item lg={4} >
  323. {FieldUtils.getComboField({
  324. label: FieldUtils.notNullFieldLabel("Country:"),
  325. valueName: "country",
  326. disabled: (!editMode && !createMode),
  327. dataList: ComboData.country,
  328. getOptionLabel: (option) => option.type ? intl.formatMessage({ id: option.type }) : "",
  329. form: formik
  330. })}
  331. </Grid>
  332. <Grid item lg={4} >
  333. {FieldUtils.getComboField({
  334. label: FieldUtils.notNullFieldLabel("District:"),
  335. valueName: "district",
  336. disabled: (!editMode && !createMode),
  337. dataList: ComboData.district,
  338. getOptionLabel: (option) => option.type ? intl.formatMessage({ id: option.type }) : "",
  339. form: formik
  340. })}
  341. </Grid>
  342. <Grid item lg={4} >
  343. {FieldUtils.getAddressField({
  344. label: FieldUtils.notNullFieldLabel("Address:"),
  345. valueName: ["addressLine1", "addressLine2", "addressLine3"],
  346. disabled: (!editMode && !createMode),
  347. form: formik
  348. })}
  349. </Grid>
  350. <Grid item lg={12} ></Grid>
  351. </Grid>
  352. }
  353. </form>
  354. <div>
  355. <Dialog
  356. open={creditorConfirmPopUp}
  357. onClose={() => setCreditorConfirmPopUp(false)}
  358. PaperProps={{
  359. sx: {
  360. minWidth: '40vw',
  361. maxWidth: { xs: '90vw', s: '90vw', m: '70vw', lg: '70vw' },
  362. maxHeight: { xs: '90vh', s: '70vh', m: '70vh', lg: '60vh' }
  363. }
  364. }}
  365. >
  366. <DialogTitle><Typography variant="h3">Confirm</Typography></DialogTitle>
  367. <DialogContent style={{ display: 'flex', }}>
  368. <Typography variant="h4" style={{ padding: '16px' }}>Are you sure mark as Creditor?</Typography>
  369. </DialogContent>
  370. <DialogActions>
  371. <Button onClick={() => setCreditorConfirmPopUp(false)}><Typography variant="h5">Cancel</Typography></Button>
  372. <Button onClick={() => markAsCreditor()}><Typography variant="h5">Confirm</Typography></Button>
  373. </DialogActions>
  374. </Dialog>
  375. </div>
  376. <div>
  377. <Dialog
  378. open={nonCreditorConfirmPopUp}
  379. onClose={() => setNonCreditorConfirmPopUp(false)}
  380. PaperProps={{
  381. sx: {
  382. minWidth: '40vw',
  383. maxWidth: { xs: '90vw', s: '90vw', m: '70vw', lg: '70vw' },
  384. maxHeight: { xs: '90vh', s: '70vh', m: '70vh', lg: '60vh' }
  385. }
  386. }}
  387. >
  388. <DialogTitle><Typography variant="h3">Confirm</Typography></DialogTitle>
  389. <DialogContent style={{ display: 'flex', }}>
  390. <Typography variant="h4" style={{ padding: '16px' }}>Are you sure mark as Non-Creditor?</Typography>
  391. </DialogContent>
  392. <DialogActions>
  393. <Button onClick={() => setNonCreditorConfirmPopUp(false)}><Typography variant="h5">Cancel</Typography></Button>
  394. <Button onClick={() => markAsNonCreditor()}><Typography variant="h5">Confirm</Typography></Button>
  395. </DialogActions>
  396. </Dialog>
  397. </div>
  398. </MainCard>
  399. );
  400. };
  401. export default OrganizationCard;