Files
tanami-admin-panel/src/Pages/IO_Management/ViewIO/HeaderModal/DistributionInvestor.jsx

462 lines
12 KiB
React
Raw Normal View History

import {
2024-08-21 13:03:50 +05:30
Alert,
AlertIcon,
Box,
Button,
2024-07-09 19:05:08 +05:30
FormControl,
2024-08-20 19:10:58 +05:30
FormErrorMessage,
2024-07-09 19:05:08 +05:30
FormLabel,
2024-07-15 12:27:23 +05:30
HStack,
Input,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
2024-07-15 12:27:23 +05:30
Switch,
Table,
Tbody,
Text,
Textarea,
2024-07-15 12:27:23 +05:30
Th,
Tr,
2024-08-21 13:03:50 +05:30
useToast,
} from "@chakra-ui/react";
2024-08-21 13:03:50 +05:30
import NormalData from "../../../../Components/DataTable/NormalTable";
2024-08-30 12:07:10 +05:30
import { useContext, useState } from "react";
2024-07-15 12:27:23 +05:30
import { AddIcon } from "@chakra-ui/icons";
2024-08-30 12:07:39 +05:30
import {
useGetDistributedToInvestorMutation,
useGetDistributionInvestorMutation,
useUpdateExitToInvestorMutation,
} from "../../../../Services/io.service";
2024-08-20 19:10:58 +05:30
import { useParams } from "react-router-dom";
import { useEffect } from "react";
import { Controller, useForm } from "react-hook-form";
import * as yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup";
2024-08-21 13:03:50 +05:30
import ToastBox from "../../../../Components/ToastBox";
import CurrencyInput from "../../../../Components/CurrencyInput";
import { IoCalculator } from "react-icons/io5";
2024-08-22 12:10:07 +05:30
import { debounce } from "../../../Master/Sponser/AddSponser";
2024-08-30 12:07:10 +05:30
import GlobalStateContext from "../../../../Contexts/GlobalStateContext";
2024-08-20 19:10:58 +05:30
2024-08-30 16:09:07 +05:30
const DistributionInvestor = ({ isOpen, onClose }) => {
2024-08-20 20:22:37 +05:30
const params = useParams();
2024-08-21 13:03:50 +05:30
const toast = useToast();
2024-08-20 20:22:37 +05:30
const id = params?.id;
2024-08-21 13:03:50 +05:30
const [ isCalculateLoading, setIsCalculateLoading ] = useState(false)
2024-08-22 12:10:07 +05:30
const [ isFinalCalculateLoading, setIsFinalCalculateLoading ] = useState(false)
2024-08-21 13:03:50 +05:30
const [ calcualtedData, setCalculatedDate ] = useState(null)
2024-08-22 12:10:07 +05:30
const [ isCalcualtedData, setIsCalcualtedData ] = useState(false)
2024-08-30 12:07:10 +05:30
const { IODetails } = useContext(GlobalStateContext);
2024-08-20 19:10:58 +05:30
2024-08-30 16:09:07 +05:30
2024-08-20 19:10:58 +05:30
// const {
// data:IObyID,
// error,
// isLoading,
// } = useGetDistributionInvestorMutation(id);
2024-08-20 20:22:37 +05:30
const [getDistributionInvestment] = useGetDistributionInvestorMutation();
2024-08-30 12:07:39 +05:30
const [getFinalDistributionInvestment] =
useGetDistributedToInvestorMutation();
const [updateExitToInvestor] = useUpdateExitToInvestorMutation();
2024-08-20 19:10:58 +05:30
2024-08-30 12:07:10 +05:30
const investorExit = yup.object().shape({
amount: yup
.string()
.required("Amount is required")
.test(
"max",
`Distribution amount should not be greater than IO cash amount ${IODetails?.ioCash}`,
function(value) {
const { ioCash } = IODetails || {}; // Safely get ioCash
if (value && ioCash) {
return parseFloat(value) <= parseFloat(ioCash); // Ensure both are compared as numbers
}
return true; // If ioCash is not available, skip validation
}
),
});
const investor = yup.object().shape({
amount: yup.string().required("Amount is required"),
});
2024-08-20 19:10:58 +05:30
const {
control,
handleSubmit,
formState: { errors },
reset,
} = useForm({
2024-08-30 17:42:27 +05:30
resolver: yupResolver(investorExit),
2024-08-20 19:10:58 +05:30
});
2024-08-30 16:09:07 +05:30
2024-07-15 12:27:23 +05:30
// ====================================================[Table Setup]================================================================
const tableHeadRow = [
"Sr No.",
"Client Id",
"First name",
"Last Name",
"Amount",
2024-08-30 12:07:39 +05:30
"Holding %",
2024-08-30 17:42:27 +05:30
"Distriution Amt($)",
2024-07-15 12:27:23 +05:30
];
2024-08-21 13:03:50 +05:30
const extractedArray = calcualtedData?.data?.map((item, index) => ({
2024-08-30 12:07:39 +05:30
id: item?.id,
"Sr No.": (
<Box
w={9}
display={"flex"}
alignItems={"center"}
isTruncated={true}
h={25}
>
<Text as={"span"} color={"teal.900"} fontWeight={"500"}>
{index + 1}
</Text>
</Box>
),
"Client Id": (
<Box w={100} isTruncated={true}>
<Text as={"span"} color={"teal.900"} fontWeight={"500"}>
{item?.clientId}
</Text>
</Box>
),
"First name": (
<Box minW={24} isTruncated={true}>
<Text as={"span"} color={"teal.900"} fontWeight={"500"}>
{item?.firstName}
</Text>
</Box>
),
"Last Name": (
<Box minW={24} isTruncated={true}>
<Text as={"span"} color={"teal.900"} fontWeight={"500"}>
{item?.lastName}
</Text>
</Box>
),
Amount: (
<Box minW={24} isTruncated={true}>
<Text as={"span"} color={"teal.900"} fontWeight={"500"}>
{item?.amount?.toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})}
</Text>
</Box>
),
"Holding %": (
<Box minW={19} isTruncated={true}>
<Text
textAlign={"right"}
as={"span"}
color={"teal.900"}
fontWeight={"500"}
2024-07-30 13:30:34 +05:30
>
2024-08-30 12:07:39 +05:30
{parseFloat(item?.distribution_per).toFixed(2)}%
</Text>
</Box>
),
2024-08-30 17:42:27 +05:30
"Distriution Amt($)": (
2024-08-30 12:07:39 +05:30
<Box minW={24} isTruncated={true}>
<Text as={"span"} color={"teal.900"} fontWeight={"500"}>
{item?.distribution_amt?.toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})}
</Text>
</Box>
),
}));
2024-07-15 12:27:23 +05:30
const Total = () => {
return (
<Table size="sm">
2024-08-21 13:03:50 +05:30
<Tbody>
2024-08-30 12:07:39 +05:30
<Tr backgroundColor="gray.50">
2024-07-15 12:27:23 +05:30
<Th
textAlign={"left"}
p={3}
width="90px"
color={"#004118"}
whiteSpace="normal"
wordBreak="normal"
overflowWrap="normal"
>
Total
</Th>
<Th
textAlign={"center"}
p={3}
width="110px"
color={"#004118"}
whiteSpace="normal"
wordBreak="normal"
overflowWrap="normal"
>
{" "}
</Th>
<Th
textAlign={"center"}
p={3}
width="110px"
color={"#004118"}
whiteSpace="normal"
wordBreak="normal"
overflowWrap="normal"
>
{" "}
</Th>
<Th
textAlign={"center"}
p={3}
width="110px"
color={"#004118"}
whiteSpace="normal"
wordBreak="normal"
overflowWrap="normal"
>
{" "}
</Th>
<Th
textAlign={"left"}
p={3}
width="110px"
color={"#004118"}
whiteSpace="normal"
wordBreak="normal"
overflowWrap="normal"
>
2024-08-30 12:07:39 +05:30
{calcualtedData?.totalInvestedAmt?.toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})}
2024-07-15 12:27:23 +05:30
</Th>
<Th
textAlign={"left"}
p={3}
width="90px"
color={"#004118"}
whiteSpace="normal"
wordBreak="normal"
overflowWrap="normal"
2024-08-23 18:03:18 +05:30
opacity={0}
2024-07-15 12:27:23 +05:30
>
2024-08-21 13:03:50 +05:30
{calcualtedData?.distributed_per?.toFixed(2)}%
2024-07-15 12:27:23 +05:30
</Th>
<Th
textAlign={"center"}
p={3}
width="100px"
color={"#004118"}
whiteSpace="normal"
wordBreak="normal"
overflowWrap="normal"
>
2024-08-30 12:07:39 +05:30
{calcualtedData?.distributed_amt?.toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})}
2024-07-15 12:27:23 +05:30
</Th>
</Tr>
</Tbody>
</Table>
);
};
2024-08-21 13:03:50 +05:30
const onSubmit = async (data) => {
2024-08-30 12:07:39 +05:30
setIsCalculateLoading(true);
2024-08-21 13:03:50 +05:30
try {
2024-08-30 12:07:39 +05:30
const res = await getDistributionInvestment({ id, data });
console.log(res?.data?.data);
2024-08-21 13:03:50 +05:30
2024-08-30 12:07:39 +05:30
if (res?.error?.status === 401) {
toast({
render: () => (
<ToastBox message={res?.error?.data?.message} status={"error"} />
),
});
setIsCalculateLoading(false);
setIsCalcualtedData(false);
} else if (res?.data?.statusCode === 200) {
setCalculatedDate(res?.data?.data);
toast({
render: () => <ToastBox message={res?.data?.message} />,
});
setIsCalculateLoading(false);
setIsCalcualtedData(true);
}
} catch (error) {}
2024-08-20 20:22:37 +05:30
};
2024-08-20 19:10:58 +05:30
2024-08-22 12:10:07 +05:30
const onFinalSubmit = async (data) => {
2024-08-30 12:07:39 +05:30
setIsFinalCalculateLoading(true);
2024-08-22 12:10:07 +05:30
if (!isCalcualtedData) {
2024-08-30 12:07:39 +05:30
setIsFinalCalculateLoading(false);
2024-08-22 12:10:07 +05:30
return toast({
render: () => (
2024-08-30 12:07:39 +05:30
<ToastBox
message={"Please calculate investment first."}
status="warn"
/>
2024-08-22 12:10:07 +05:30
),
2024-08-30 12:07:39 +05:30
});
2024-08-22 12:10:07 +05:30
}
2024-08-30 12:07:39 +05:30
2024-08-22 12:10:07 +05:30
const finalData = {
transactionAmount: data?.amount,
};
2024-08-30 12:07:39 +05:30
2024-08-22 12:10:07 +05:30
try {
2024-08-30 17:42:27 +05:30
const res = await getFinalDistributionInvestment({ id, data: finalData });
2024-08-22 12:10:07 +05:30
console.log(finalData);
2024-08-30 12:07:39 +05:30
2024-08-22 12:10:07 +05:30
if (res?.error?.status === 401) {
toast({
render: () => (
<ToastBox message={res?.error?.data?.message} status="error" />
),
});
} else if (res?.data?.statusCode === 200) {
toast({
render: () => <ToastBox message={res?.data?.message} />,
});
2024-08-30 12:07:39 +05:30
handleClose();
2024-08-22 12:10:07 +05:30
}
} catch (error) {
console.error("An error occurred:", error);
} finally {
2024-08-30 12:07:39 +05:30
handleClose();
2024-08-22 12:10:07 +05:30
}
};
const handleClose = () => {
2024-08-30 12:07:39 +05:30
onClose();
setIsFinalCalculateLoading(false);
2024-08-30 16:09:07 +05:30
reset({
amount:""
});
2024-08-30 12:07:39 +05:30
setCalculatedDate(null);
setIsCalcualtedData(false);
};
2024-08-20 19:10:58 +05:30
return (
2024-08-22 12:10:07 +05:30
<Modal size={"xl"} isOpen={isOpen} onClose={handleClose}>
<ModalOverlay />
2024-07-30 13:30:34 +05:30
<ModalContent maxW={1000}>
2024-08-20 19:10:58 +05:30
<ModalHeader fontSize={"md"}>
2024-08-30 16:09:07 +05:30
Distribution To Investor Transaction
2024-08-20 19:10:58 +05:30
</ModalHeader>
<ModalCloseButton />
2024-07-30 13:30:34 +05:30
<ModalBody>
2024-08-20 19:10:58 +05:30
{/* <Text as="label" mb="5px" fontSize="sm" fontWeight={500}>
Amount to Distribute
</Text> */}
<HStack onSubmit={handleSubmit(onSubmit)} as={"form"} mb={4}>
2024-07-30 13:30:34 +05:30
{/* <Input placeholder="$00.00" size={"sm"} className="col" /> */}
2024-08-22 18:48:45 +05:30
<FormControl isInvalid={errors.amount} isRequired>
2024-08-30 12:07:39 +05:30
<FormLabel textAlign={"right"} fontSize={"sm"}>
{" "}
2024-08-30 16:09:07 +05:30
Amount to Distribute
2024-08-30 12:07:39 +05:30
</FormLabel>
<Box
display={"flex"}
justifyContent={"end"}
alignItems={"end"}
gap={2}
>
2024-08-20 19:10:58 +05:30
<Controller
name="amount"
control={control}
render={({ field }) => (
2024-08-21 13:03:50 +05:30
<CurrencyInput
2024-08-30 12:07:39 +05:30
rounded={0}
w={"18%"}
2024-08-20 20:22:37 +05:30
{...field}
fontSize={"sm"}
type="number"
size={"sm"}
textAlign={"right"}
/>
2024-08-20 19:10:58 +05:30
)}
/>
2024-08-30 16:09:07 +05:30
<Button
2024-08-21 13:03:50 +05:30
leftIcon={<IoCalculator />}
2024-08-20 20:22:37 +05:30
size={"sm"}
2024-08-21 13:03:50 +05:30
rounded={0}
2024-08-20 20:22:37 +05:30
colorScheme="forestGreen"
type="submit"
2024-08-21 13:03:50 +05:30
isLoading={isCalculateLoading}
2024-08-20 20:22:37 +05:30
>
Calculate
2024-08-30 16:09:07 +05:30
</Button>
2024-08-20 20:22:37 +05:30
</Box>
2024-08-30 12:07:10 +05:30
<FormErrorMessage display={'flex'} justifyContent={'end'} pe={125} fontSize={"xs"} fontWeight={600}>
2024-08-20 20:22:37 +05:30
{errors.amount?.message}
</FormErrorMessage>
</FormControl>
2024-07-30 13:30:34 +05:30
</HStack>
2024-07-15 12:27:23 +05:30
2024-08-30 12:07:39 +05:30
{calcualtedData ? (
<NormalData
emptyMessage={`We don't have any Sponers `}
tableHeadRow={tableHeadRow}
data={extractedArray}
// total={<Total />}
// isLoading={isLoading}
/>
) : (
<Alert status="info" fontSize={"sm"} rounded={"sm"}>
<AlertIcon />
Please enter amount to calcutale Distribution!
</Alert>
)}
</ModalBody>
2024-07-30 13:30:34 +05:30
<ModalFooter pt={0}>
2024-08-30 12:07:39 +05:30
{isCalcualtedData ? (
<>
<Button
bg={"hsla(139, 100%, 14%, 1)"}
mr={3}
color={"#fff"}
_hover={{
bg: "hsl(139deg 98.99% 26.59%)",
}}
size={"sm"}
rounded={"sm"}
onClick={handleSubmit(onFinalSubmit)}
isLoading={isFinalCalculateLoading}
>
Save
</Button>
<Button size={"sm"} rounded={"sm"} mr={3} onClick={onClose}>
Close
</Button>
</>
) : (
""
)}
2024-07-30 13:30:34 +05:30
</ModalFooter>
</ModalContent>
</Modal>
);
};
export default DistributionInvestor;