From dc78466e8f8e0f3cb0d2bb8e2d3feef45afe405d Mon Sep 17 00:00:00 2001 From: ROSHAN GARG Date: Tue, 24 Sep 2024 10:23:55 +0530 Subject: [PATCH] updated the view invoices --- src/index.js | 4 +- src/routes.js | 7 + src/views/orders/viewInoices.js | 576 ++++++++++++++++++++++++++++++++ 3 files changed, 585 insertions(+), 2 deletions(-) create mode 100644 src/views/orders/viewInoices.js diff --git a/src/index.js b/src/index.js index 0ab534a..8a829f2 100644 --- a/src/index.js +++ b/src/index.js @@ -15,9 +15,9 @@ import { cibGmail } from "@coreui/icons"; import { createRoot } from "react-dom/client"; const setupAxios = () => { - // axios.defaults.baseURL = "http://localhost:5000"; + axios.defaults.baseURL = "http://localhost:5000"; // axios.defaults.baseURL = "https://cheminova-api-2.onrender.com"; - axios.defaults.baseURL = "https://api.cnapp.co.in"; + // axios.defaults.baseURL = "https://api.cnapp.co.in"; axios.defaults.headers = { "Cache-Control": "no-cache,no-store", diff --git a/src/routes.js b/src/routes.js index a0d441d..ceab322 100644 --- a/src/routes.js +++ b/src/routes.js @@ -153,6 +153,7 @@ import ViewRetailDistributorSC from "./views/SalesCoOrdinators/ViewRetailDistrib import ViewRetailDistributorPD from "./views/PrincipalDistributors/ViewRetailDistributorPD"; import MapRD from "./views/RetailDistributors/MapRD"; import PendingOrders from "./views/orders/pendingOrders"; +import ViewInvoices from "./views/orders/viewInoices"; const routes = [ //dashboard @@ -626,6 +627,12 @@ const routes = [ element: ViewOrders, navName: "Orders", }, + { + path: "/orders/invoice/:status/:id", + name: "View Invoice ", + element: ViewInvoices, + navName: "Orders", + }, { path: "/orders/processing", diff --git a/src/views/orders/viewInoices.js b/src/views/orders/viewInoices.js new file mode 100644 index 0000000..d4677ef --- /dev/null +++ b/src/views/orders/viewInoices.js @@ -0,0 +1,576 @@ +import React, { useState, useEffect } from "react"; +import { + Box, + Typography, + Grid, + Button, + FormControl, + Dialog, + DialogActions, + DialogContent, + Table, + TableBody, + TableCell, + TableHead, + TableRow, + Paper, + DialogContentText, + DialogTitle, + TextField, + Divider, +} from "@mui/material"; +import onvoicesData from "../../assets/incoicedata.json"; +import { useNavigate, useParams } from "react-router-dom"; +import { TableContainer } from "@material-ui/core"; +import axios from "axios"; // Import axios for HTTP requests +import { isAutheticated } from "src/auth"; +import Swal from "sweetalert2"; +import OrderDetailsDialog from "./partialOrderModal"; +import InvoiceTable from "./invoiceTable"; +import PendingOrderTable from "./pendingOrderTable"; + +const ViewInvoices = () => { + const [invoice, setInvoice] = useState(null); // State to store order details + const [status, setStatus] = useState(""); + const [openConfirmDialog, setOpenConfirmDialog] = useState(false); + const [openDispatchDialog, setOpenDispatchDialog] = useState(false); + + const [openDeliveredDialog, setOpenDeliveredDialog] = useState(false); // New dialog state for delivery confirmation + const [courierName, setCourierName] = useState(""); + const [couriertrackingId, setCourierId] = useState(""); + + const [deliveryDate, setDeliveryDate] = useState(""); + const token = isAutheticated(); // State for delivery date + const [invoiceStatus, setInvoiceStatus] = useState(""); + const [loading, setLoading] = useState(true); // Loading state + const [error, setError] = useState(null); // Error state + const navigate = useNavigate(); + const { id } = useParams(); + + // Get order ID from URL params + + useEffect(() => { + const fetchOrderDetails = async () => { + try { + const response = await axios.get( + `/api/get-single-placed-order-pd/${id}`, + { + headers: { + Authorization: `Bearer ${token}`, + }, + } + ); + console.log(response); + setOrder(response.data.singleOrder); + setStatus(response.data.singleOrder?.status); + + setLoading(false); + } catch (err) { + setError("Failed to fetch order details"); + setLoading(false); + } + }; + + fetchOrderDetails(); + }, [id]); + + const handleStatusChange = (event) => { + setInvoiceStatus(event.target.value); + }; + + const handleUpdateClick = () => { + if (orderStatus === "dispatched") { + setOpenDispatchDialog(true); + } else if (orderStatus === "partial-processing") { + setOpnePartialModal(true); + } else if (orderStatus === "cancelled") { + setOpenCancelDialog(true); + } else if (orderStatus === "delivered") { + setOpenDeliveredDialog(true); + } else { + } + }; + + const handleConfirmUpdate = async (e) => { + e.preventDefault(); + try { + if (orderStatus === "cancelled") { + if (!cancellationReason) { + Swal.fire("Please give the order cancellation reason"); + } + const cancellationRes = await axios.put( + `/api/cancel-order/${id}`, + { + cancellationReason: cancellationReason, + }, + { + headers: { + "Access-Control-Allow-Origin": "*", + Authorization: `Bearer ${token}`, + }, + } + ); + if (cancellationRes.status === 200) { + Swal.fire( + "Order Status updated", + `Order got cancelled due to${cancellationReason}`, + "success" + ); + navigate(`/orders/${orderStatus}`); + } + } else if (orderStatus === "dispatched") { + const cancellationRes = await axios.patch( + `/api/change/status/${id}`, + { + status: orderStatus, + courierName, + TrackingID: couriertrackingId, + }, + { + headers: { + "Access-Control-Allow-Origin": "*", + Authorization: `Bearer ${token}`, + }, + } + ); + if (cancellationRes.status === 200) { + Swal.fire("Order Status updated", `Order Dispatched`, "success"); + + navigate(`/orders/${orderStatus}`); + } + } else if (orderStatus === "delivered") { + if (!deliveryDate) { + Swal.fire("Please enter the Delivery Date"); + setOpenDeliveredDialog(false); + return; + } + const cancellationRes = await axios.patch( + `/api/change/status/${id}`, + { + status: orderStatus, + DDate: deliveryDate, + }, + { + headers: { + "Access-Control-Allow-Origin": "*", + Authorization: `Bearer ${token}`, + }, + } + ); + if (cancellationRes.status === 200) { + Swal.fire("Order Status updated", `Order in processing`, "success"); + navigate(`/orders/${orderStatus}`); + } + } else if (orderStatus === "processing") { + const processingOrderInvoice = order?.orderItem + .filter((item) => item.remainingQuantity > 0) // Only include items with remainingQuantity > 0 + .map((item) => ({ + ...item, + productId: item.productId._id, + processquantity: item.remainingQuantity, // Add processquantity only for items with remainingQuantity > 0 + })); + console.log(processingOrderInvoice); + const cancellationRes = await axios.post( + `/api/processing-order`, + { + invoiceItems: processingOrderInvoice, + orderId: order._id, + }, + { + headers: { + "Access-Control-Allow-Origin": "*", + Authorization: `Bearer ${token}`, + }, + } + ); + if (cancellationRes.status === 200) { + Swal.fire("Order Status updated", `Order in processing`, "success"); + navigate(`/orders/${orderStatus}`); + } + } + } catch (error) { + Swal.fire("Something went wrong ", error.message, "error"); + } + // Perform update logic here + + setOpenDispatchDialog(false); + + setOpenDeliveredDialog(false); // Close delivered dialog + }; + + const handleCancel = async () => { + setOpenDispatchDialog(false); + setOpenDeliveredDialog(false); // Close delivered dialog + }; + + if (loading) { + return Loading...; + } + + if (error) { + return {error}; + } + + return ( + + + + Invoice Id : {order?.uniqueId} + + + + + + + + Invoice + + + + + Order Summary + + + + + + + + + Product + Price (₹) + Quantity + Subtotal (₹) + GST (%) + GST Amount (₹) + Total with GST (₹) + + + + {order?.orderItem.map((item, index) => { + const subtotal = item.price * item.quantity; + const gstAmount = + ((item.GST * item.price) / 100) * item.quantity; + const totalWithGST = subtotal + gstAmount; + + return ( + + + {item.productId.name} + + {item.productId.name} + + + ₹{item.price} + {item.quantity} + ₹{subtotal} + {item.GST}% + ₹{gstAmount} + ₹{totalWithGST} + + ); + })} + +
+
+
+
+ + + + + + Order Summary + + + + Total Items: {order?.orderItem.length} + + + Total Subtotal: ₹{order?.subtotal} + Total GST: ₹{order?.gstTotal} + + Grand Total: ₹{order?.grandTotal} + + + + +
+
+ + + + + + Customer Details + + + SBU: {order?.addedBy.SBU} + + + Name: {order?.addedBy.name} + + + Email id: {order?.addedBy.email} + + + Number: {order?.addedBy.phone} + + + + + Bill Address + + {order?.billTo} + + + + Ship Address + + {order?.shipTo} + + + + Payment mode + + + {order?.paymentMode} + + + + + + Order Status + + {order?.status} + {order?.status === "cancelled" && ( + + {order?.order_Cancelled_Reason} + + )} + + + + {invoice?.status !== "delivered" && ( + + + + )} + + {orderStatus && ( + + )} + +
+ + {/* Dialogs for updating status */} + + Update Order Status + + + Are you sure you want to update the status to {orderStatus}? + + + + + + + + + {/* Dialog for dispatch details */} + + Dispatch Details +
+ + + Please provide courier name and ID for dispatch: + + + setCourierName(e.target.value)} + /> + setCourierId(e.target.value)} + /> + + + + + +
+
+ + {/* Dialog for cancellation reason */} + + Cancellation Reason +
+ + + Please provide a reason for cancelling the order: + + setCancellationReason(e.target.value)} + /> + + + + + +
+
+ + {/* Dialog for delivery confirmation */} + + Confirm Delivery +
+ + + Please confirm the delivery details: + + + setDeliveryDate(e.target.value)} + InputLabelProps={{ + shrink: true, + }} + /> + + + + + + +
+
+ + {/* partial processing */} + +
+ ); +}; +const formatAMPM = (date) => { + var hours = new Date(date).getHours(); + var minutes = new Date(date).getMinutes(); + var ampm = hours >= 12 ? "PM" : "AM"; + hours = hours % 12; + hours = hours ? hours : 12; + minutes = minutes < 10 ? "0" + minutes : minutes; + var strTime = hours + ":" + minutes + " " + ampm; + return strTime; +}; +export default ViewInvoices;