Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

479 строки
23 KiB

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