diff --git a/src/components/purchase/PurchaseActions.jsx b/src/components/purchase/PurchaseActions.jsx new file mode 100644 index 00000000..6cb82e66 --- /dev/null +++ b/src/components/purchase/PurchaseActions.jsx @@ -0,0 +1,102 @@ +export const getPurchaseActions = ({ + item, + isActive, + canDelete, + canAddChallan, + setViewPurchase, + setManagePurchase, + setDeletingId, + setIsDeleteModalOpen, + setChallan, + setAddPayment, +}) => { + const actions = []; + + // VIEW + actions.push({ + key: "view", + label: "View", + icon: "bx bx-show", + show: true, + onClick: () => + setViewPurchase({ + isOpen: true, + purchaseId: item.id, + }), + }); + + if (!isActive) { + // EDIT + actions.push({ + key: "edit", + label: "Edit", + icon: "bx bx-edit", + show: true, + onClick: () => + setManagePurchase({ + isOpen: true, + purchaseId: item.id, + }), + }); + + // DELETE + actions.push({ + key: "delete", + label: "Delete", + icon: "bx bx-trash", + show: canDelete, + onClick: () => { + setDeletingId(item.id); + setIsDeleteModalOpen(true); + }, + }); + + // ADD CHALLAN + actions.push({ + key: "challan", + label: "Add Delivery Challan", + icon: "bx bx-file bx-plus", + show: canAddChallan, + onClick: () => + setChallan({ + isOpen: true, + purchaseId: item.id, + }), + }); + + // ADD PAYMENT + actions.push({ + key: "payment", + label: "Add Payment", + icon: "bx bx-wallet", + show: true, + onClick: () => + setAddPayment({ + isOpen: true, + purchaseId: item.id, + }), + }); + } else { + // RESTORE + actions.push({ + key: "restore", + label: "Restore", + icon: "bx bx-undo", + show: true, + onClick: () => { + setDeletingId(item.id); + setIsDeleteModalOpen(true); + }, + }); + } + + return actions.filter((a) => a.show); +}; + +export const DropdownItem = ({ icon, label, onClick }) => ( +
  • + + {label} + +
  • +); diff --git a/src/components/purchase/PurchaseList.jsx b/src/components/purchase/PurchaseList.jsx index e37ce257..5d7715b1 100644 --- a/src/components/purchase/PurchaseList.jsx +++ b/src/components/purchase/PurchaseList.jsx @@ -1,21 +1,33 @@ import React, { useState } from "react"; -import { useDeletePurchaseInvoice, usePurchasesList } from "../../hooks/usePurchase"; -import { ITEMS_PER_PAGE } from "../../utils/constants"; +import { + useDeletePurchaseInvoice, + usePurchasesList, +} from "../../hooks/usePurchase"; +import { + ADD_DELIVERY_CHALLAN, + DELETEPURCHASE_INVOICE, + ITEMS_PER_PAGE, +} from "../../utils/constants"; import Pagination from "../common/Pagination"; import { PurchaseColumn } from "./Purchasetable"; import { SpinnerLoader } from "../common/Loader"; import { useDebounce } from "../../utils/appUtils"; import { usePurchaseContext } from "../../pages/purchase/PurchasePage"; import ConfirmModal from "../common/ConfirmModal"; +import { useHasUserPermission } from "../../hooks/useHasUserPermission"; +import { DropdownItem, getPurchaseActions } from "./PurchaseActions"; const PurchaseList = ({ searchString, isActive }) => { - const { setViewPurchase, setManagePurchase, setChallan } = + const { setViewPurchase, setManagePurchase, setChallan, setAddPayment } = usePurchaseContext(); const [currentPage, setCurrentPage] = useState(1); - const { mutate: DeletePurchaseInvoice, isPending } = useDeletePurchaseInvoice(); + const { mutate: DeletePurchaseInvoice, isPending } = + useDeletePurchaseInvoice(); const [IsDeleteModalOpen, setIsDeleteModalOpen] = useState(false); const [deletingId, setDeletingId] = useState(null); + const canAddChallan = useHasUserPermission(ADD_DELIVERY_CHALLAN); + const canDelete = useHasUserPermission(DELETEPURCHASE_INVOICE); const debounceSearch = useDebounce(searchString, 300); const { data, isLoading } = usePurchasesList( @@ -47,7 +59,6 @@ const PurchaseList = ({ searchString, isActive }) => { ); }; - return ( <> {IsDeleteModalOpen && ( @@ -55,7 +66,11 @@ const PurchaseList = ({ searchString, isActive }) => { isOpen={IsDeleteModalOpen} type={!isActive ? "delete" : "undo"} header={!isActive ? "Delete Invoice" : "Restore Invoice"} - message={!isActive ? "Are you sure you want to delete?" : "Are you sure you want to restore?"} + message={ + !isActive + ? "Are you sure you want to delete?" + : "Are you sure you want to restore?" + } onSubmit={handleDeleteRestore} onClose={() => setIsDeleteModalOpen(false)} loading={isPending} @@ -104,7 +119,6 @@ const PurchaseList = ({ searchString, isActive }) => { )} - {!isLoading && data?.data?.map((item, index) => ( @@ -113,7 +127,7 @@ const PurchaseList = ({ searchString, isActive }) => { {col.render ? col.render(item) : item[col.key] || "NA"} ))} - +
    diff --git a/src/components/purchase/PurchasePayment.jsx b/src/components/purchase/PurchasePayment.jsx index a1e440a4..530110a0 100644 --- a/src/components/purchase/PurchasePayment.jsx +++ b/src/components/purchase/PurchasePayment.jsx @@ -20,7 +20,7 @@ import { SpinnerLoader } from "../common/Loader"; import { formatUTCToLocalTime } from "../../utils/dateUtils"; import Avatar from "../common/Avatar"; -const PurchasePayment = ({ purchaseId }) => { +const PurchasePayment = ({onClose, purchaseId }) => { const { data: Purchase, isLoading: isPurchaseLoading, diff --git a/src/hooks/usePurchase.jsx b/src/hooks/usePurchase.jsx index b328f9f3..97d85953 100644 --- a/src/hooks/usePurchase.jsx +++ b/src/hooks/usePurchase.jsx @@ -131,6 +131,27 @@ export const useAddDeliverChallan = (onSuccessCallback) => { }, }); }; +export const useAddPurchasePayment =(onSuccessCallback)=>{ +const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: async (payload) => + PurchaseRepository.AddPayment(payload), + onSuccess: (data, variables) => { + queryClient.invalidateQueries({ queryKey: ["purchase_payment_history"] }); + showToast("Payment added successfully", "success"); + if (onSuccessCallback) onSuccessCallback(); + }, + onError: (error) => { + showToast( + error?.response?.data?.message || + error.message || + "Failed to Add payment", + "error" + ); + }, + }); +} export const useDeletePurchaseInvoice = () => { diff --git a/src/pages/purchase/PurchasePage.jsx b/src/pages/purchase/PurchasePage.jsx index d3991ed3..570a5945 100644 --- a/src/pages/purchase/PurchasePage.jsx +++ b/src/pages/purchase/PurchasePage.jsx @@ -89,7 +89,7 @@ const PurchasePage = () => { - +
    {canCreatePurchase && ( )} - +
    @@ -164,7 +164,10 @@ const PurchasePage = () => { setAddPayment({ isOpen: false, purchaseId: null }) } > - + setAddPayment({ isOpen: false, purchaseId: null })} + purchaseId={addPayment.purchaseId} + /> )} diff --git a/src/repositories/PurchaseRepository.jsx b/src/repositories/PurchaseRepository.jsx index 69e8087b..c949a661 100644 --- a/src/repositories/PurchaseRepository.jsx +++ b/src/repositories/PurchaseRepository.jsx @@ -15,6 +15,9 @@ export const PurchaseRepository = { addDelievryChallan: (data) => api.post(`/api/PurchaseInvoice/delivery-challan/create`, data), + AddPayment: (data) => api.post(`/api/PurchaseInvoice/add/payment`, data), + GetPaymentHistory: (purchaseInvoiceId) => + api.get(`/api/PurchaseInvoice/payment-history/list/${purchaseInvoiceId}`), deletePurchase: (id, isActive = false) => api.delete(`/api/PurchaseInvoice/delete/${id}?isActive=${isActive}`), };