import React, { useCallback, useEffect, useRef, useState, useContext, } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { Divider, Form, Input, Segmented, Table } from 'antd'; import { Modal, Tooltip, DatePicker } from 'antd'; import { Messages } from '../../Components/Notifications/Messages'; import FormHeader from '../PageComponents/FormHeader'; import { InputField } from '../../Components/Forms/InputField'; import Buttons from '../../Components/Forms/Buttons'; import { ArrowRightOutlined } from '@ant-design/icons'; import { DropDowns } from '../../Components/Forms/DropDown'; import { RadioGrpButton } from '../../Components/Forms/RadioGroup.jsx'; import '../../Styles/CancelReschedule/ChangePaymentMode.scss'; import { ChangeSelectedCustDisable, GlobalSelCustId, GlobalpaymentOptionData, changeSelectedCustId, changeSelectedOption, getAllCustomer, getCancelReshRules, getCreditCustomer, getPaymentModeDetail, getPaymentOptions, putpaymentStatus, putpaymentModeChange, PostSalesCancellation, } from '../../Features/BookingScreen/BookingData/BookingData.js'; import { ExtractDateFormate, extractLastNumber, getSession, validateSafeInput, } from '../../Services/Others.js'; import PozoAddCustomerIcon from '../BookingScreen/Components/UtillComponents/Pozo retail icons/PozoAddCustomerIcon.jsx'; import BSCustomerSelect from '../BookingScreen/Components/UtillComponents/BSSelectCustomer.jsx'; import FeaturesFunctionalities from '../BookingScreen/Components/BookingFunctionality/FeaturesFunctionalities.jsx'; import { changeBreadCrumb, getEmpAccess, } from '../../Features/AppPage/CenterPage.js'; import { useAuth } from '../../AuthContext.jsx'; import dayjs from 'dayjs'; import { IoEye } from 'react-icons/io5'; import { ApplicationPreferences } from '../../Features/BrachLogin/BranchLogin.js'; const subDirectory = import.meta.env.BASE_URL; const containerStyle = { display: 'flex', flexDirection: 'column', overflow: 'scroll', }; const rowStyle = { display: 'flex', flexDirection: 'row', width: '90%', fontSize: '16px', }; const labelStyle = { display: 'flex', justifyContent: 'flex-start', width: '50%', }; const valueStyle = { display: 'flex', justifyContent: 'flex-end', width: '30%', }; const ChangePaymentmode = () => { const { SadminuserAccess } = useAuth(); let SAAccessCommonMaster = SadminuserAccess?.find( (e) => e?.MenuName === 'Payment Mode Change' ); const formRef = useRef(); const dispatch = useDispatch(); const CompId = getSession('CompId'); const BranchId = getSession('BranchId'); const AppId = getSession('AppId'); const UserId = getSession('UserId'); const UserType = getSession('UserType'); const EditableContext = React.createContext(null); const PaymentOptions = useSelector(GlobalpaymentOptionData); const SelectCustId = useSelector(GlobalSelCustId); const appPreferences = useSelector(ApplicationPreferences); const commonModulePreference = appPreferences?.find( (preference) => preference?.PreferredCatName === "Common Module" )?.PreferenceCatDetails; const sportsAppPreference = commonModulePreference?.find( (preference) => preference?.PreferredSubCatName === "SportsApp" && preference?.PreferredStatus == 'Y' ); const [messageType, setMessageType] = useState(null); const [messageData, setMessageData] = useState(null); const [selCancelPay, setselCancelPay] = useState('CR'); const [selActivityType, setselActivityType] = useState('Cancel'); const [paybtns, setpaybtns] = useState([]); const [CanReason, setCanReason] = useState([]); const [SelCanReason, setSelCanReason] = useState([]); const [RescheduleReason, setRescheduleReason] = useState([]); // const [SelRescheduleReason, setSelRescheduleReason] = useState([]) const [SalesDetail, setSalesDetail] = useState([]); const [FullSalesDetail, setFullSalesDetail] = useState([]); const [selectedRowKeys, setSelectedRowKeys] = useState([]); const [SelectedOrderId, setSelectedOrderId] = useState(null); // const [SelPayMode, setSelPayMode] = useState() const [ApiPaymentName, setApiPaymentName] = useState(null); const [DropSelPaymentName, setDropSelPaymentName] = useState(null); const [SelectSalesId, setSelectSalesId] = useState(); const [SelCanPaymode, setSelCanPaymode] = useState(); const [SelCancelType, setSelCancelType] = useState('OverAll'); const [CancelModalOpen, setCancelModalOpen] = useState(false); const [CancellationCharge, setCancellationCharge] = useState(null); const [addcustomer, setAddCustomer] = useState(false); const [SelectCust, setSelectCust] = useState(false); const [CreditCustDetails, setCreditCustDetails] = useState([]); const [CreditCustomer, setCreditCustomer] = useState(false); const [NewCustomer, setNewCustomer] = useState([]); const [empData, setEmpData] = useState(); const [addnewAccess, setaddnewAccess] = useState(true); const [isdisabled, setisdisabled] = useState(true); const [segmentValue, setSegmentValue] = useState('Bill No'); const [selectedRowKey, setSelectedRowKey] = useState(null); const [selectedPayments, setSelectedPayments] = useState([]); const [viewProductDetails, setViewProductDetails] = useState({ view: false, productDetails: [], }); const [viewPaymentDetails, setViewPaymentDetails] = useState({ view: false, paymentDetails: [], }); const [isPosting, setIsPosting] = useState(false); const items = [ { name: 'Home', link: `${subDirectory}app-page/home`, }, { name: ' Cancel/Payment Change', link: `${subDirectory}setting/cancel-paymentchange`, }, ]; useEffect(() => { dispatch(changeBreadCrumb({ items: items })); fetchPaymentOption(); GetCancellationReason(); }, []); useEffect(() => { if (UserType === 'Employee') { fetchApi(); } }, [UserType]); useEffect(() => { let hasAccess = false; if (UserType === 'Admin' || UserType === 'Super Admin') { hasAccess = true; } else if (UserType === 'Employee') { hasAccess = empData?.AddAccess === 'Y'; } else if (UserType === 'Super Admin User') { hasAccess = SAAccessCommonMaster?.AddAccess === 'Y'; } setaddnewAccess(!hasAccess); }, [empData, SAAccessCommonMaster, UserType]); useEffect(() => { getCreditcustDetail(); }, [SelectCustId, SelectCust]); useEffect(() => { if (UserType === 'Employee') { fetchApi(); } }, [UserType]); const fetchApi = async () => { let data = { CompId: CompId, BranchId: BranchId, AppId: AppId, EmpId: UserId, }; let response = await dispatch(getEmpAccess(data)).unwrap(); let datas = response?.data?.data?.[0]?.EmpAccessDetails?.filter( (item) => item.ConfigName === 'Payment Mode Change' ); setEmpData(datas?.[0]); }; useEffect(() => { const withoutCustomer = PaymentOptions?.filter?.( (item) => item?.ConfigName?.toLowerCase() !== 'credit'?.toLowerCase() ); setpaybtns(withoutCustomer); }, [PaymentOptions]); const OverAllOrderCancel = (second) => { // third; }; const disabledDate = (current) => current && current > dayjs().endOf('day'); const disabledFeatureDate = (current) => current && current < dayjs().endOf('day'); const getCreditcustDetail = async () => { let Credata = { CompId: CompId, BranchId: BranchId, AppId: AppId, CustId: SelectCustId, }; let res = await dispatch(getCreditCustomer(Credata)).unwrap(); if (res?.data?.statusCode === 1) { setCreditCustDetails(res?.data?.data); } else { let res = await dispatch( getAllCustomer({ CompId: CompId, AppId: AppId, branchId: BranchId }) ).unwrap(); if (res?.data?.statusCode == 1) { let allcustomer = res?.data?.data; let SelectedCustomer = allcustomer?.filter( (item) => item?.CustId === SelectCustId ); setNewCustomer(SelectedCustomer); setCreditCustDetails([]); } else { setCreditCustDetails([]); setNewCustomer([]); } } }; const getSalesOrderDetail = async () => { let fromDate; let toDate; let date; const billormobile = formRef.current?.getFieldsValue()?.[ selCancelPay === 'CR' ? 'OrderId' : 'PaymodeOrderId' ]; if (segmentValue === 'Mobile') { if (selCancelPay === 'CR') { if (selActivityType === 'Cancel') { fromDate = formRef.current ?.getFieldsValue() ?.CancelFromDate?.format('YYYY-MM-DD'); toDate = formRef.current ?.getFieldsValue() ?.CancelToDate?.format('YYYY-MM-DD'); } else if (selActivityType === 'Reschedule') { fromDate = formRef.current ?.getFieldsValue() ?.ReschedulFromDate?.format('YYYY-MM-DD'); toDate = formRef.current ?.getFieldsValue() ?.ReschedulToDate?.format('YYYY-MM-DD'); } } else if (selCancelPay === 'CP') { fromDate = formRef.current ?.getFieldsValue() ?.['Change-PaymentFromDate']?.format('YYYY-MM-DD'); toDate = formRef.current ?.getFieldsValue() ?.['Change-PaymentToDate']?.format('YYYY-MM-DD'); } } if (segmentValue === 'Date') { const fieldname = selCancelPay === 'CR' ? selActivityType === 'Cancel' ? 'Canceldate' : 'RescheduleApidate' : 'Change-Paymentdate'; date = formRef.current ?.getFieldsValue() ?.[fieldname]?.format('YYYY-MM-DD'); } const data = { segmentValue, mobileNumber: billormobile, fromDate, toDate, date, AppId, CompId, BranchId, OrderId: 'O' + AppId + '-' + CompId + '-' + BranchId + '-' + UserId + '-' + billormobile, }; const res = await dispatch(getPaymentModeDetail(data)).unwrap(); if (res?.data?.statusCode === 1) { const data = res?.data?.data; setFullSalesDetail(data); setSelCancelType('OverAll'); const tempListData = data?.[0]?.productDetails?.map((item) => ({ key: item?.InwardDtlId, ...item, })); setSalesDetail(tempListData); } else { setSalesDetail([]); setFullSalesDetail([]); if (segmentValue == "Mobile" && billormobile && fromDate && toDate) { setMessageType('error'); setMessageData(res?.data?.response); } else { setMessageType('error'); setMessageData(res?.data?.response); } } setisdisabled(true); }; const fetchPaymentOption = async () => { await dispatch(getPaymentOptions()).unwrap(); }; const GetCancellationReason = async () => { let data = { AppId: AppId, CompId: CompId, BranchId: BranchId, }; let res = await dispatch(getCancelReshRules(data)).unwrap(); if (res?.data?.statusCode === 1) { let getActivedatas = res?.data?.data?.filter( (item) => item?.ActiveStatus === 'A' && item?.ActivityType === 'C' ); let getReschDatas = res?.data?.data?.filter( (item) => item?.ActiveStatus === 'A' && item?.ActivityType === 'R' ); setCanReason(getActivedatas); setRescheduleReason(getReschDatas); } else { setCanReason([]); setRescheduleReason([]); } }; const selectCancelOrPaymentType = (e) => { setisdisabled(true); setselCancelPay(e); formRef.current?.resetFields(); setSelectedOrderId(); // setSelPayMode() setSelectedRowKey(null); setFullSalesDetail([]); }; const selectActivityType = (e) => { setisdisabled(true); setselActivityType(e); formRef.current?.resetFields(); setSelectedOrderId(); setSelectedRowKey(null); setFullSalesDetail([]); }; // Debounce utility const debounce = (func, delay) => { let timer; return (...args) => { if (timer) clearTimeout(timer); timer = setTimeout(() => { func(...args); }, delay); }; }; const getSalesDetailapi = debounce(async (e) => { setSelectedOrderId(e); if (segmentValue === 'Bill No') { selActivityType === 'Cancel' ? getSalesOrderDetail() : getPaymentMode(); return; } let fromDate; let toDate; if (selCancelPay === 'CR') { if (selActivityType === 'Cancel') { fromDate = formRef.current?.getFieldsValue()?.CancelFromDate; toDate = formRef.current?.getFieldsValue()?.CancelToDate; } else if (selActivityType === 'Reschedule') { fromDate = formRef.current?.getFieldsValue()?.ReschedulFromDate; toDate = formRef.current?.getFieldsValue()?.ReschedulToDate; } } else if (selCancelPay === 'CP') { fromDate = formRef.current?.getFieldsValue()?.['Change-PaymentFromDate']; toDate = formRef.current?.getFieldsValue()?.['Change-PaymentToDate']; } if ( segmentValue === 'Mobile' && fromDate != undefined && toDate !== undefined ) { selActivityType === 'Cancel' ? getSalesOrderDetail() : getPaymentMode(); } else { setMessageType('error'); setMessageData('Please select From Date and To Date'); } }, 300); const selectCancelType = (e) => { setSelCancelType(e); }; const onOrderSelectChange = (newselectedRowKeys) => { setSelectedRowKeys(newselectedRowKeys); }; const getPaymentMode = async (e) => { let fromDate; let toDate; let date; const billormobile = formRef.current?.getFieldsValue()?.[ selCancelPay === 'CR' ? 'OrderId' : 'PaymodeOrderId' ]; if (segmentValue === 'Mobile') { if (selCancelPay === 'CR') { if (selActivityType === 'Cancel') { fromDate = formRef.current ?.getFieldsValue() ?.CancelFromDate?.format('YYYY-MM-DD'); toDate = formRef.current ?.getFieldsValue() ?.CancelToDate?.format('YYYY-MM-DD'); } else if (selActivityType === 'Reschedule') { fromDate = formRef.current ?.getFieldsValue() ?.ReschedulFromDate?.format('YYYY-MM-DD'); toDate = formRef.current ?.getFieldsValue() ?.ReschedulToDate?.format('YYYY-MM-DD'); } } else if (selCancelPay === 'CP') { fromDate = formRef.current ?.getFieldsValue() ?.['Change-PaymentFromDate']?.format('YYYY-MM-DD'); toDate = formRef.current ?.getFieldsValue() ?.['Change-PaymentToDate']?.format('YYYY-MM-DD'); } } if (segmentValue === 'Date') { const fieldname = selCancelPay === 'CR' ? selActivityType === 'Cancel' ? 'Canceldate' : 'RescheduleApidate' : 'Change-Paymentdate'; date = formRef.current ?.getFieldsValue() ?.[fieldname]?.format('YYYY-MM-DD'); } formRef.current?.setFieldsValue({ PaymodeOrderId: e }); let data = { segmentValue, mobileNumber: billormobile, fromDate, toDate, date, AppId, CompId, BranchId, passType: false, OrderId: 'O' + AppId + '-' + CompId + '-' + BranchId + '-' + UserId + '-' + billormobile, }; const res = await dispatch(getPaymentModeDetail(data)).unwrap(); if (res?.data?.statusCode === 1) { const sales = res?.data?.data; setFullSalesDetail(sales); if (segmentValue === 'Bill No') { const { PaymentType, PaymentTypeName, OrderId, CustSuppId } = sales?.[0]; // setSelPayMode(PaymentType) setApiPaymentName(PaymentTypeName); // formRef.current?.setFieldsValue({ CpPaymentMode: PaymentType }) const configName = PaymentOptions?.find( (item) => item.ConfigId === PaymentType )?.ConfigName; setSelectCust(configName === 'Credit'); setSelectSalesId(OrderId); dispatch(changeSelectedCustId(CustSuppId)); handleCustomerSelection(CustSuppId); } else { } } else if (res?.data?.statusCode === 0) { // setSelPayMode() // formRef.current?.setFieldsValue({ CpPaymentMode: "" }) setSelectSalesId(); setSelectedOrderId(); setMessageType('error'); setMessageData(res?.data?.response); } setisdisabled(true); }; // const onChangePaymentMode = (e) => { // setSelPayMode(e) // formRef.current?.setFieldsValue({ CpPaymentMode: e }); // const selPayment = PaymentOptions?.find(item => item.ConfigId === e) // setDropSelPaymentName(selPayment?.ConfigName) // setSelectCust(selPayment?.ConfigName === "Credit") // } const SelectCanPaymode = (e) => { setSelCanPaymode(e); formRef.current?.setFieldsValue({ PaymentMode: e }); selectedRowKey !== undefined && setisdisabled(false); }; const defaultColumns = [ { title: 'Sl.No', key: 'sno', align: 'center', width: '100px', render: (text, object, index) => ( {index + 1} ), }, { title: 'Product Name', dataIndex: 'ProdNameQty', key: 'ProdNameQty', width: '100px', }, { title: 'Quantity ', dataIndex: 'SalesQty', key: 'SalesQty', width: '100px', align: 'right', editable: true, }, { title: 'Total Amount', dataIndex: 'TotalAmt', key: 'TotalAmt', width: '100px', align: 'right', }, ]; const columns = defaultColumns?.map((col) => { if (!col.editable) { return col; } return { ...col, onCell: (record) => ({ record, editable: col.editable, dataIndex: col.dataIndex, title: col.title, handleSave, }), }; }); const EditableRow = ({ index, ...props }) => { const [form] = Form.useForm(); return (
); }; const EditableCell = ({ title, editable, children, dataIndex, record, handleSave, ...restProps }) => { const [editing, setEditing] = useState(false); const inputRef = useRef(null); const form = useContext(EditableContext); useEffect(() => { if (editing) { inputRef?.current?.focus(); } }, [editing]); const toggleEdit = () => { setEditing(!editing); form.setFieldsValue({ [dataIndex]: record[dataIndex], }); }; const save = async () => { try { const values = await form.validateFields(); toggleEdit(); handleSave({ ...record, ...values, }); } catch (errInfo) { console.log('Save failed:', errInfo); } }; let childNode = children; if (editable) { childNode = editing ? (