Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

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