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

416 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-07-15 12:27:23 +05:30
import { useState } from "react";
import { AddIcon } from "@chakra-ui/icons";
2024-08-22 12:10:07 +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-20 19:10:58 +05:30
export const investor = yup.object().shape({
amount: yup.string().required("Amount is required"),
});
2024-08-22 12:10:07 +05:30
const DistributionInvestor = ({ isOpen, onClose, exit }) => {
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-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-22 12:10:07 +05:30
const [getFinalDistributionInvestment] = useGetDistributedToInvestorMutation();
const [ updateExitToInvestor ] = useUpdateExitToInvestorMutation()
2024-08-20 19:10:58 +05:30
const {
control,
handleSubmit,
formState: { errors },
reset,
} = useForm({
resolver: yupResolver(investor),
});
2024-08-22 12:10:07 +05:30
2024-08-20 19:10:58 +05:30
// useEffect(()=>{
// try {
// const res = getDistributionInvestment({id,data})
// console.log(res);
2024-08-20 20:22:37 +05:30
2024-08-20 19:10:58 +05:30
// } catch (error) {
2024-08-20 20:22:37 +05:30
2024-08-20 19:10:58 +05:30
// }
// },[])
// console.log(IObyID);
2024-07-15 12:27:23 +05:30
// ====================================================[Table Setup]================================================================
const tableHeadRow = [
"Sr No.",
"Client Id",
"First name",
"Last Name",
"Amount",
"%",
"($)",
];
2024-08-21 13:03:50 +05:30
const extractedArray = calcualtedData?.data?.map((item, index) => ({
2024-07-15 12:27:23 +05:30
id: item?.id,
2024-07-30 13:30:34 +05:30
"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>
),
2024-07-15 12:27:23 +05:30
"Client Id": (
<Box w={100} isTruncated={true}>
<Text as={"span"} color={"teal.900"} fontWeight={"500"}>
2024-08-21 13:03:50 +05:30
{item?.clientId}
2024-07-15 12:27:23 +05:30
</Text>
</Box>
),
"First name": (
<Box minW={24} isTruncated={true}>
<Text as={"span"} color={"teal.900"} fontWeight={"500"}>
2024-08-21 13:03:50 +05:30
{item?.firstName}
2024-07-15 12:27:23 +05:30
</Text>
</Box>
),
"Last Name": (
<Box minW={24} isTruncated={true}>
2024-07-30 13:30:34 +05:30
<Text as={"span"} color={"teal.900"} fontWeight={"500"}>
2024-08-21 13:03:50 +05:30
{item?.lastName}
2024-07-30 13:30:34 +05:30
</Text>
2024-07-15 12:27:23 +05:30
</Box>
),
2024-07-30 13:30:34 +05:30
Amount: (
2024-07-15 12:27:23 +05:30
<Box minW={24} isTruncated={true}>
2024-07-30 13:30:34 +05:30
<Text as={"span"} color={"teal.900"} fontWeight={"500"}>
2024-08-22 12:10:07 +05:30
{item?.amount?.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}
2024-07-30 13:30:34 +05:30
</Text>
2024-07-15 12:27:23 +05:30
</Box>
),
"%": (
<Box minW={19} isTruncated={true}>
2024-07-30 13:30:34 +05:30
<Text
textAlign={"right"}
as={"span"}
color={"teal.900"}
fontWeight={"500"}
>
2024-08-21 13:03:50 +05:30
{item?.distribution_per} %
2024-07-30 13:30:34 +05:30
</Text>
2024-07-15 12:27:23 +05:30
</Box>
),
"($)": (
<Box minW={24} isTruncated={true}>
2024-07-30 13:30:34 +05:30
<Text as={"span"} color={"teal.900"} fontWeight={"500"}>
2024-08-21 13:03:50 +05:30
{item?.distribution_amt?.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}
2024-07-30 13:30:34 +05:30
</Text>
2024-07-15 12:27:23 +05:30
</Box>
),
}))
2024-08-21 13:03:50 +05:30
2024-07-15 12:27:23 +05:30
const Total = () => {
return (
<Table size="sm">
2024-08-21 13:03:50 +05:30
<Tbody>
<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-22 12:10:07 +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-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-22 12:10:07 +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) => {
setIsCalculateLoading(true)
try {
const res = await getDistributionInvestment({id,data})
console.log(res?.data?.data);
if (res?.error?.status === 401) {
toast({
render: () => (
<ToastBox message={res?.error?.data?.message} status={"error"} />
),
})
setIsCalculateLoading(false)
2024-08-22 12:10:07 +05:30
setIsCalcualtedData(false)
2024-08-21 13:03:50 +05:30
} else if(res?.data?.statusCode === 200){
setCalculatedDate(res?.data?.data)
toast({
render: () => (
<ToastBox message={res?.data?.message} />
),
})
setIsCalculateLoading(false)
2024-08-22 12:10:07 +05:30
setIsCalcualtedData(true)
2024-08-21 13:03:50 +05:30
}
} 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) => {
setIsFinalCalculateLoading(true)
if (!isCalcualtedData) {
setIsFinalCalculateLoading(false)
return toast({
render: () => (
<ToastBox message={"Please calculate investment first."} status="warn" />
),
})
}
const finalData = {
transactionAmount: data?.amount,
};
try {
const res = await exit ? updateExitToInvestor({id,data: finalData }) :getFinalDistributionInvestment({id,data: finalData });
console.log(finalData);
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} />,
});
handleClose()
}
} catch (error) {
console.error("An error occurred:", error);
} finally {
handleClose()
}
};
const handleClose = () => {
onClose()
setIsFinalCalculateLoading(false)
reset()
}
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"}>
Distribution To Investor Transaction
</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-20 19:10:58 +05:30
<FormControl isInvalid={errors.amount}>
2024-08-21 13:03:50 +05:30
<FormLabel textAlign={'right'} fontSize={"sm"} > Amount to Distribute</FormLabel>
2024-08-22 12:10:07 +05:30
<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
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-20 20:22:37 +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
</Button>
</Box>
2024-08-22 12:10:07 +05:30
<FormErrorMessage display={'flex'} justifyContent={'end'} pe={125} fontSize={"xs"} fontWeight={500}>
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-21 13:03:50 +05:30
{calcualtedData ? <NormalData
2024-07-30 13:30:34 +05:30
emptyMessage={`We don't have any Sponers `}
tableHeadRow={tableHeadRow}
data={extractedArray}
2024-08-21 13:03:50 +05:30
total={<Total />}
2024-07-30 13:30:34 +05:30
// isLoading={isLoading}
2024-08-21 13:03:50 +05:30
/> : <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}>
<Button
bg={"hsla(139, 100%, 14%, 1)"}
mr={3}
color={"#fff"}
_hover={{
bg: "hsl(139deg 98.99% 26.59%)",
}}
2024-08-20 19:10:58 +05:30
size={"sm"}
2024-07-09 19:05:08 +05:30
rounded={"sm"}
2024-08-22 12:10:07 +05:30
onClick={handleSubmit(onFinalSubmit)}
isLoading={isFinalCalculateLoading}
>
Save
</Button>
2024-08-20 19:10:58 +05:30
<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;