diff --git a/src/features/province/components/manage-guilds-requests/ManageGuildsRequests.js b/src/features/province/components/manage-guilds-requests/ManageGuildsRequests.js index fd78c65..1415d7b 100644 --- a/src/features/province/components/manage-guilds-requests/ManageGuildsRequests.js +++ b/src/features/province/components/manage-guilds-requests/ManageGuildsRequests.js @@ -80,7 +80,7 @@ export const ManageGuildsRequests = ({ userType }) => { item?.registerarMobile ? "(" + item?.registerarMobile + " )" : " " }`, item?.licenseNumber || "-", - item?.name || "-", + (IS_STEWARD ? item?.name : item?.guildsName) || "-", `${item?.user?.fullname || "-"} (${item?.user?.mobile || "-"})`, item?.user?.nationalId || "-", item?.typeActivity || "-", diff --git a/src/features/province/components/province-legal-guilds-in-province/ProvinceLegalGuildsForm.js b/src/features/province/components/province-legal-guilds-in-province/ProvinceLegalGuildsForm.js index 1e197ab..683fec6 100644 --- a/src/features/province/components/province-legal-guilds-in-province/ProvinceLegalGuildsForm.js +++ b/src/features/province/components/province-legal-guilds-in-province/ProvinceLegalGuildsForm.js @@ -116,56 +116,22 @@ const getValidationSchema = (isAdmin, userFound) => type_activity: yup.string(), }); -const LegalGuildForm = ({ formik, isAdmin, typeActivities, userType }) => { +const LegalGuildForm = ({ + formik, + isAdmin, + typeActivities, + userType, + provinces, + cities, +}) => { const IS_STEWARD = userType === "steward"; - const [provinceData, setProvinceData] = useState(); - const [cityData, setCityData] = useState(); - - const dispatch = useDispatch(); - - useEffect(() => { - dispatch(LOADING_START()); - dispatch(cityGetProvinces()) - ?.then((r) => { - if (r?.payload?.data) { - setProvinceData(r.payload.data); - } - }) - .catch((error) => { - console.error("Error fetching provinces:", error); - }) - .finally(() => { - dispatch(LOADING_END()); - }); - }, []); - - useEffect(() => { - if (formik?.values?.province) { - dispatch(LOADING_START()); - dispatch(cityGetCity(formik?.values?.province)) - .then((r) => { - if (r?.payload?.data) { - setCityData(r.payload.data); - } - }) - .catch((error) => { - console.error("Error fetching cities:", error); - }) - .finally(() => { - dispatch(LOADING_END()); - }); - } else { - setCityData([]); - } - }, [formik?.values?.province]); - return (
- + {isAdmin ? ( { - + { disablePortal id="province" options={ - provinceData - ? provinceData.map((i) => ({ + provinces + ? provinces.map((i) => ({ id: i.key, label: i.name, })) @@ -301,22 +267,15 @@ const LegalGuildForm = ({ formik, isAdmin, typeActivities, userType }) => { disablePortal id="city" options={ - cityData - ? cityData.map((i) => ({ id: i.name, label: i.name })) + cities + ? cities.map((i) => ({ id: i.key, label: i.name })) : [] } onChange={(e, value) => { formik.setFieldValue("city", value ? value.id : ""); }} renderInput={(params) => ( - + )} /> @@ -436,6 +395,9 @@ export const ProvinceLegalGuildsForm = ({ item, userType, }) => { + const [provinceData, setProvinceData] = useState(); + const [cityData, setCityData] = useState(); + const IS_STEWARD = userType === "steward"; const userInitialValue = { national_id: item?.user?.national_id || "", @@ -471,6 +433,22 @@ export const ProvinceLegalGuildsForm = ({ const currentRole = getRoleFromUrl(); const isAdmin = currentRole === "AdminX"; + useEffect(() => { + dispatch(LOADING_START()); + dispatch(cityGetProvinces()) + ?.then((r) => { + if (r?.payload?.data) { + setProvinceData(r.payload.data); + } + }) + .catch((error) => { + console.error("Error fetching provinces:", error); + }) + .finally(() => { + dispatch(LOADING_END()); + }); + }, []); + useEffect(() => { dispatch(provinceGetTypeActivity()).then((r) => { setTypeActivities(r.payload.data || []); @@ -529,6 +507,12 @@ export const ProvinceLegalGuildsForm = ({ if (r.payload.error) { setUserFound(false); if (isAdmin) { + openNotif({ + vertical: "top", + horizontal: "center", + msg: r.payload.error, + severity: "error", + }); // Admin can create even if user not found setUserData( IS_STEWARD @@ -612,10 +596,10 @@ export const ProvinceLegalGuildsForm = ({ first_name: values.first_name, last_name: values.last_name, national_id: values.national_id, - province: values.province, + province: provinceData?.find((p) => p?.key === values.province)?.name, address: values.address || "", mobile: values.mobile || "", - city: values.city, + city: cityData?.find((c) => c?.key === values.city)?.name, type_activity: typeActivityKey, // Send key instead of title ...(item?.key && { guilds_key: item.key }), // Include guild key for editing }; @@ -650,6 +634,26 @@ export const ProvinceLegalGuildsForm = ({ }, }); + useEffect(() => { + if (formik?.values?.province) { + dispatch(LOADING_START()); + dispatch(cityGetCity(formik?.values?.province)) + .then((r) => { + if (r?.payload?.data) { + setCityData(r.payload.data); + } + }) + .catch((error) => { + console.error("Error fetching cities:", error); + }) + .finally(() => { + dispatch(LOADING_END()); + }); + } else { + setCityData([]); + } + }, [formik?.values?.province]); + if (!userData && !item) { return ( ); }; diff --git a/src/features/province/components/province-legal-guilds-in-province/ProvinceLegalGuildsInProvince.js b/src/features/province/components/province-legal-guilds-in-province/ProvinceLegalGuildsInProvince.js index ad83c4f..8e6c19c 100644 --- a/src/features/province/components/province-legal-guilds-in-province/ProvinceLegalGuildsInProvince.js +++ b/src/features/province/components/province-legal-guilds-in-province/ProvinceLegalGuildsInProvince.js @@ -29,33 +29,28 @@ export const ProvinceLegalGuildsInProvince = ({ userType }) => { const handleTextChange = (e) => setTextValue(e.target.value); const fetchApiData = async (pageNum) => { - let response = null; - if (IS_STEWARD) { - response = await dispatch( - provinceGetTotalStewardsService({ - search: "filter", - value: textValue, - page: page, - page_size: perPage, - is_real_person: false, - role_key: checkPathStartsWith("province") - ? selectedSubUser?.key || "" - : "", - }) - ); - } else { - response = await dispatch( - provinceGetTotalGuildsService({ - search: "filter", - value: textValue, - page: pageNum, - page_size: perPage, - steward: false, - active_state: "all", - is_real_person: false, - }) - ); - } + const response = await dispatch( + IS_STEWARD + ? provinceGetTotalStewardsService({ + search: "filter", + value: textValue, + page: page, + page_size: perPage, + is_real_person: false, + role_key: checkPathStartsWith("province") + ? selectedSubUser?.key || "" + : "", + }) + : provinceGetTotalGuildsService({ + search: "filter", + value: textValue, + page: pageNum, + page_size: perPage, + steward: false, + active_state: "all", + is_real_person: false, + }) + ); if (response.payload.error) { console.error("Error fetching data:", response.payload.error); @@ -90,7 +85,7 @@ export const ProvinceLegalGuildsInProvince = ({ userType }) => { const d = data.map((item, i) => { return [ page === 1 ? i + 1 : i + perPage * (page - 1) + 1, - item?.nationalId || "-", + (IS_STEWARD ? item?.licenseNumber : item?.nationalId) || "-", (IS_STEWARD ? item?.name : item?.guildsName) || "-", `${item?.user?.fullname || "-"}`, item?.user?.mobile || "-", diff --git a/src/features/province/components/province-legal-guilds-out-province/ProvinceLegalGuildsOutProvince.js b/src/features/province/components/province-legal-guilds-out-province/ProvinceLegalGuildsOutProvince.js index d7731e6..0802f27 100644 --- a/src/features/province/components/province-legal-guilds-out-province/ProvinceLegalGuildsOutProvince.js +++ b/src/features/province/components/province-legal-guilds-out-province/ProvinceLegalGuildsOutProvince.js @@ -36,6 +36,7 @@ export const ProvinceLegalGuildsOutProvince = ({ userType }) => { page: page, pageSize: perPage, searchValue: textValue, + buyer_type: IS_STEWARD ? "Steward" : "Guilds", }); setData(response.data.results); setTotalRows(response.data.count); @@ -68,7 +69,9 @@ export const ProvinceLegalGuildsOutProvince = ({ userType }) => { return [ page === 1 ? i + 1 : i + perPage * (page - 1) + 1, item?.unitName || "-", - item?.buyer?.parentLegalPersonNationalCode || "-", + (IS_STEWARD + ? item?.buyer?.nationalId + : item?.buyer?.parentLegalPersonNationalCode) || "-", killHouseInfo, item?.province || "-", item?.city || "-", @@ -79,6 +82,7 @@ export const ProvinceLegalGuildsOutProvince = ({ userType }) => { key={item?.unitName} item={item} updateTable={() => fetchApiData(1)} + userType={userType} />, ]; }); diff --git a/src/features/province/components/province-true-guilds-out-province/ProvinceTrueGuildsOutProvince.js b/src/features/province/components/province-true-guilds-out-province/ProvinceTrueGuildsOutProvince.js index 3d10739..531de72 100644 --- a/src/features/province/components/province-true-guilds-out-province/ProvinceTrueGuildsOutProvince.js +++ b/src/features/province/components/province-true-guilds-out-province/ProvinceTrueGuildsOutProvince.js @@ -36,6 +36,7 @@ export const ProvinceTrueGuildsOutProvince = ({ userType }) => { page: page, pageSize: perPage, searchValue: textValue, + buyer_type: IS_STEWARD ? "Steward" : "Guilds", }); setData(response.data.results); setTotalRows(response.data.count); diff --git a/src/features/province/components/province-true-guilds-out-province/ProvinceTrueGuildsOutProvinceOperations.js b/src/features/province/components/province-true-guilds-out-province/ProvinceTrueGuildsOutProvinceOperations.js index ce826fb..dcd56fa 100644 --- a/src/features/province/components/province-true-guilds-out-province/ProvinceTrueGuildsOutProvinceOperations.js +++ b/src/features/province/components/province-true-guilds-out-province/ProvinceTrueGuildsOutProvinceOperations.js @@ -37,9 +37,15 @@ export const ProvinceTrueGuildsOutProvinceOperations = ({ const deleteGuild = () => { const buyerKey = item?.key; if (buyerKey) - dispatch(deleteOutProvinceTrueGuilds({ buyer_key: buyerKey })).then(() => - dispatch(CLOSE_MODAL()) - ); + dispatch( + deleteOutProvinceTrueGuilds({ + buyer_key: buyerKey, + buyer_type: IS_STEWARD ? "Steward" : "Guilds", + }) + ).then(() => { + updateTable(); + dispatch(CLOSE_MODAL()); + }); }; return ( @@ -93,7 +99,12 @@ export const ProvinceTrueGuildsOutProvinceOperations = ({ ? "legal" : "real" } - defaultNationalCode={item?.buyer?.nationalCode} + defaultNationalCode={ + IS_STEWARD + ? item?.buyer?.nationalId + : item?.buyer?.nationalCode + } + userType={userType} /> ), }) diff --git a/src/features/province/services/province-out-province-buyers.js b/src/features/province/services/province-out-province-buyers.js index 0a101c2..ede3033 100644 --- a/src/features/province/services/province-out-province-buyers.js +++ b/src/features/province/services/province-out-province-buyers.js @@ -7,6 +7,7 @@ import axios from "axios"; * @param {number} params.page - Page number * @param {number} params.pageSize - Items per page * @param {string} params.searchValue - Search value + * @param {string} params.buyerType - buyer type * @returns {Promise} Axios promise with buyers data */ export const fetchOutProvinceRealBuyers = async ({ @@ -14,9 +15,12 @@ export const fetchOutProvinceRealBuyers = async ({ page, pageSize, searchValue = "", + buyer_type = "", }) => { const response = await axios.get( - `out-province-carcasses-buyer/?search=filter&value=${searchValue}&role=${role}&page=${page}&page_size=${pageSize}&state=buyer-list&type=real` + `out-province-carcasses-buyer/?search=filter&value=${searchValue}&role=${role}&page=${page}&page_size=${pageSize}&state=buyer-list&type=real${ + buyer_type ? "&buyer_type=" + buyer_type : "" + }` ); return response; }; @@ -35,10 +39,12 @@ export const fetchOutProvinceLegalBuyers = async ({ page, pageSize, searchValue = "", + buyer_type = "", }) => { const response = await axios.get( - `out-province-carcasses-buyer/?search=filter&value=${searchValue}&role=${role}&page=${page}&page_size=${pageSize}&state=buyer-list&type=legal` + `out-province-carcasses-buyer/?search=filter&value=${searchValue}&role=${role}&page=${page}&page_size=${pageSize}&state=buyer-list&type=legal${ + buyer_type ? "&buyer_type=" + buyer_type : "" + }` ); return response; }; - diff --git a/src/features/slaughter-house/components/slaughter-sell-carcass-out-province-add-buyer/SlaughterSellCarcassOutProvinceAddBuyer.js b/src/features/slaughter-house/components/slaughter-sell-carcass-out-province-add-buyer/SlaughterSellCarcassOutProvinceAddBuyer.js index e2f998d..4ed931f 100644 --- a/src/features/slaughter-house/components/slaughter-sell-carcass-out-province-add-buyer/SlaughterSellCarcassOutProvinceAddBuyer.js +++ b/src/features/slaughter-house/components/slaughter-sell-carcass-out-province-add-buyer/SlaughterSellCarcassOutProvinceAddBuyer.js @@ -35,7 +35,9 @@ export const SlaughterSellCarcassOutProvinceAddBuyer = ({ data, defaultPersonType, defaultNationalCode, + userType, }) => { + const IS_STEWARD = userType === "steward"; const [openNotif] = useContext(AppContext); const [userData, setUserData] = useState(null); const [notFound, setNotFound] = useState(false); @@ -131,7 +133,9 @@ export const SlaughterSellCarcassOutProvinceAddBuyer = ({ } else if (data.buyer.parentLegalPersonNationalCode) { formik2.setFieldValue( "nationalCode", - data.buyer.parentLegalPersonNationalCode, + IS_STEWARD + ? data?.buyer?.nationalId + : data.buyer.parentLegalPersonNationalCode, false ); } @@ -140,15 +144,17 @@ export const SlaughterSellCarcassOutProvinceAddBuyer = ({ useEffect(() => { if (userData) { - formik.setValues({ + const formData = { mobile: userData.mobile || "", firstName: userData.firstName || "", lastName: userData.lastName || "", - unitName: userData.unitName || "", province: userData.province || "", city: userData.city || "", nationalId: userData.nationalId || "", - }); + unitName: data?.unitName || "", + }; + + formik.setValues(formData); const fieldKeys = [ "nationalId", @@ -174,15 +180,17 @@ export const SlaughterSellCarcassOutProvinceAddBuyer = ({ useEffect(() => { if (isEdit) { - formik.setValues({ + const formData = { mobile: data.mobile || "", firstName: data.firstName || "", lastName: data.lastName || "", - unitName: data.unitName || "", province: data.province || "", city: data.city || "", - nationalId: data.national_id || data.nationalId || "", - }); + nationalId: (IS_STEWARD ? defaultNationalCode : data.nationalId) || "", + unitName: data?.unitName || "", + }; + + formik.setValues(formData); setLockedFields({}); // formik2.setFieldValue( // "nationalCode", @@ -656,79 +664,85 @@ export const SlaughterSellCarcassOutProvinceAddBuyer = ({ disabled={!formik.isValid} onClick={() => { if (isEdit) { - dispatch( - slaughterEditBuyerDataService({ - buyer_key: data?.key, - mobile: formik.values.mobile, - first_name: formik.values.firstName, - last_name: formik.values.lastName, - unit_name: formik.values.unitName, - city: formik.values.city, - province: formik.values.province, - national_id: formik.values.nationalId, - }) - ).then((r) => { - updateTable(); - if (r.payload.error) { - openNotif({ - vertical: "top", - horizontal: "center", - msg: r.payload.error, - severity: "error", - }); - } else { - dispatch( - DRAWER({ right: false, bottom: false, content: null }) - ); + const submitData = { + buyer_type: IS_STEWARD ? "Steward" : "Guilds", + buyer_key: data?.key, + mobile: formik.values.mobile, + first_name: formik.values.firstName, + last_name: formik.values.lastName, + city: formik.values.city, + province: formik.values.province, + national_id: formik.values.nationalId, + unit_name: formik.values.unitName, + }; - openNotif({ - vertical: "top", - horizontal: "center", - msg: "عملیات با موفقیت انجام شد.", - severity: "success", - }); + dispatch(slaughterEditBuyerDataService(submitData)).then( + (r) => { + updateTable(); + if (r.payload.error) { + openNotif({ + vertical: "top", + horizontal: "center", + msg: r.payload.error, + severity: "error", + }); + } else { + dispatch( + DRAWER({ right: false, bottom: false, content: null }) + ); + + openNotif({ + vertical: "top", + horizontal: "center", + msg: "عملیات با موفقیت انجام شد.", + severity: "success", + }); + } } - }); + ); } else { - dispatch( - slaughterSubmitBuyerDataService({ - mobile: formik.values.mobile, - first_name: formik.values.firstName, - last_name: formik.values.lastName, + const submitData = { + mobile: formik.values.mobile, + buyer_type: IS_STEWARD ? "Steward" : "Guilds", + first_name: formik.values.firstName, + last_name: formik.values.lastName, + city: formik.values.city, + province: formik.values.province, + national_id: + formik2.values.personType === "legal" + ? null + : formik.values.nationalId, + ...(formik2.values.personType === "legal" && { + isRealPerson: false, + info_value: formik2.values.nationalCode, unit_name: formik.values.unitName, - city: formik.values.city, - province: formik.values.province, - national_id: - formik2.values.personType === "legal" - ? null - : formik.values.nationalId, - ...(formik2.values.personType === "legal" && { - isRealPerson: false, - info_value: formik2.values.nationalCode, - }), - }) - ).then((r) => { - updateTable(); - if (r.payload.error) { - openNotif({ - vertical: "top", - horizontal: "center", - msg: r.payload.error, - severity: "error", - }); - } else { - dispatch( - DRAWER({ right: false, bottom: false, content: null }) - ); + }), + }; - openNotif({ - vertical: "top", - horizontal: "center", - msg: "عملیات با موفقیت انجام شد.", - severity: "success", - }); + dispatch(slaughterSubmitBuyerDataService(submitData)).then( + (r) => { + updateTable(); + if (r.payload.error) { + openNotif({ + vertical: "top", + horizontal: "center", + msg: r.payload.error, + severity: "error", + }); + } else { + dispatch( + DRAWER({ right: false, bottom: false, content: null }) + ); + + openNotif({ + vertical: "top", + horizontal: "center", + msg: "عملیات با موفقیت انجام شد.", + severity: "success", + }); + } } - }); + ); } }} >