cancellerd order screen done and api done

This commit is contained in:
ROSHAN GARG 2024-09-20 15:51:00 +05:30
parent 9941d699f5
commit 7737cf0dde
3 changed files with 243 additions and 60 deletions

View File

@ -15,9 +15,9 @@ import { cibGmail } from "@coreui/icons";
import { createRoot } from "react-dom/client"; import { createRoot } from "react-dom/client";
const setupAxios = () => { 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://cheminova-api-2.onrender.com";
axios.defaults.baseURL = "https://api.cnapp.co.in"; // axios.defaults.baseURL = "https://api.cnapp.co.in";
axios.defaults.headers = { axios.defaults.headers = {
"Cache-Control": "no-cache,no-store", "Cache-Control": "no-cache,no-store",

View File

@ -8,25 +8,23 @@ import {
Dialog, Dialog,
DialogActions, DialogActions,
DialogContent, DialogContent,
Table,
TableBody,
TableCell,
TableHead,
TableRow,
Paper,
DialogContentText, DialogContentText,
DialogTitle, DialogTitle,
TextField, TextField,
Card,
CardMedia,
CardContent,
Divider, Divider,
Paper,
Table,
TableHead,
TableRow,
TableCell,
TableBody,
} from "@mui/material"; } from "@mui/material";
import { useNavigate, useParams } from "react-router-dom"; import { useNavigate, useParams } from "react-router-dom";
import { TableContainer } from "@material-ui/core"; import { TableContainer } from "@material-ui/core";
import axios from "axios"; // Import axios for HTTP requests import axios from "axios"; // Import axios for HTTP requests
import { isAutheticated } from "src/auth"; import { isAutheticated } from "src/auth";
import Swal from "sweetalert2"; import Swal from "sweetalert2";
import OrderDetailsDialog from "./partialOrderModal";
const ViewOrders = () => { const ViewOrders = () => {
const [order, setOrder] = useState(null); // State to store order details const [order, setOrder] = useState(null); // State to store order details
@ -44,7 +42,9 @@ const ViewOrders = () => {
const [loading, setLoading] = useState(true); // Loading state const [loading, setLoading] = useState(true); // Loading state
const [error, setError] = useState(null); // Error state const [error, setError] = useState(null); // Error state
const navigate = useNavigate(); const navigate = useNavigate();
const { id } = useParams(); // Get order ID from URL params const { id } = useParams();
const [parialModal, setOpnePartialModal] = useState(false);
// Get order ID from URL params
useEffect(() => { useEffect(() => {
const fetchOrderDetails = async () => { const fetchOrderDetails = async () => {
@ -78,6 +78,8 @@ const ViewOrders = () => {
const handleUpdateClick = () => { const handleUpdateClick = () => {
if (orderStatus === "dispatched") { if (orderStatus === "dispatched") {
setOpenDispatchDialog(true); setOpenDispatchDialog(true);
} else if (orderStatus === "partial-processing") {
setOpnePartialModal(true);
} else if (orderStatus === "cancelled") { } else if (orderStatus === "cancelled") {
setOpenCancelDialog(true); setOpenCancelDialog(true);
} else if (orderStatus === "delivered") { } else if (orderStatus === "delivered") {
@ -94,10 +96,9 @@ const ViewOrders = () => {
if (!cancellationReason) { if (!cancellationReason) {
Swal.fire("Please give the order cancellation reason"); Swal.fire("Please give the order cancellation reason");
} }
const cancellationRes = await axios.patch( const cancellationRes = await axios.put(
`/api/change/status/${id}`, `/api/cancel-order/${id}`,
{ {
status: orderStatus,
ReasonforCancellation: cancellationReason, ReasonforCancellation: cancellationReason,
}, },
{ {
@ -159,10 +160,16 @@ const ViewOrders = () => {
navigate(`/orders/${orderStatus}`); navigate(`/orders/${orderStatus}`);
} }
} else if (orderStatus === "processing") { } else if (orderStatus === "processing") {
const cancellationRes = await axios.patch( const processingOrderInvoice = order?.orderItem.map((item) => ({
`/api/change/status/${id}`, ...item,
processquantity: item.quantity,
}));
console.log("");
const cancellationRes = await axios.post(
`/api/processing-order`,
{ {
status: orderStatus, invoiceItems: processingOrderInvoice,
orderId: order._id,
}, },
{ {
headers: { headers: {
@ -180,6 +187,7 @@ const ViewOrders = () => {
Swal.fire("Something went wrong ", error.message, "error"); Swal.fire("Something went wrong ", error.message, "error");
} }
// Perform update logic here // Perform update logic here
setOpnePartialModal(false);
setOpenConfirmDialog(false); setOpenConfirmDialog(false);
setOpenDispatchDialog(false); setOpenDispatchDialog(false);
setOpenCancelDialog(false); setOpenCancelDialog(false);
@ -192,6 +200,32 @@ const ViewOrders = () => {
setOpenCancelDialog(false); setOpenCancelDialog(false);
setOpenDeliveredDialog(false); // Close delivered dialog setOpenDeliveredDialog(false); // Close delivered dialog
}; };
const handlePartialOrderClose = () => {
setOpnePartialModal(false);
};
const handlePartialProcess = async (availability) => {
try {
const cancellationRes = await axios.post(
`/api/processing-order`,
{
invoiceItems: availability,
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");
}
};
const timelineData = [ const timelineData = [
{ event: "Order Placed On", date: order?.createdAt }, { event: "Order Placed On", date: order?.createdAt },
@ -238,48 +272,58 @@ const ViewOrders = () => {
</Typography> </Typography>
<Grid container spacing={2}> <Grid container spacing={2}>
{order?.orderItem.map((item, index) => ( <Grid item xs={12}>
<Grid item xs={12} sm={6} md={4} key={index}> <TableContainer component={Paper}>
<Card <Table>
sx={{ <TableHead>
display: "flex", <TableRow>
flexDirection: "column", <TableCell>Product</TableCell>
height: "100%", <TableCell align="right">Price ()</TableCell>
}} <TableCell align="right">Quantity</TableCell>
> <TableCell align="right">Subtotal ()</TableCell>
<CardMedia <TableCell align="right">GST (%)</TableCell>
component="img" <TableCell align="right">GST Amount ()</TableCell>
image={item.productId.image} <TableCell align="right">Total with GST ()</TableCell>
alt={item.productId.name} </TableRow>
sx={{ height: 140 }} </TableHead>
/> <TableBody>
<CardContent> {order?.orderItem.map((item, index) => {
<Typography variant="h6" gutterBottom> const subtotal = item.price * item.quantity;
{item.productId.name} const gstAmount =
</Typography> ((item.GST * item.price) / 100) * item.quantity;
<Typography color="textSecondary"> const totalWithGST = subtotal + gstAmount;
Price:{item.price}
</Typography> return (
<Typography color="textSecondary"> <TableRow key={index}>
Quantity: {item.quantity} <TableCell>
</Typography> <img
<Typography color="textSecondary"> src={item.productId.image}
Subtotal: {item.price * item.quantity} alt={item.productId.name}
</Typography> style={{
<Typography color="textSecondary"> width: 50,
GST ({item.GST}%): height: 50,
{((item.GST * item.price) / 100) * item.quantity} marginRight: 10,
</Typography> }}
<Typography color=""> />
Total with GST: {" "} <Typography variant="subtitle1">
{((item.GST * item.price) / 100) * item.quantity + {item.productId.name}
item.price * item.quantity} </Typography>
</Typography> </TableCell>
</CardContent> <TableCell align="right">{item.price}</TableCell>
</Card> <TableCell align="right">{item.quantity}</TableCell>
</Grid> <TableCell align="right">{subtotal}</TableCell>
))} <TableCell align="right">{item.GST}%</TableCell>
<TableCell align="right">{gstAmount}</TableCell>
<TableCell align="right">{totalWithGST}</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
</TableContainer>
</Grid>
</Grid> </Grid>
<Grid container spacing={2}> <Grid container spacing={2}>
<Grid item sm={6} md={6} xl={6}> <Grid item sm={6} md={6} xl={6}>
<Box <Box
@ -428,6 +472,9 @@ const ViewOrders = () => {
<> <>
<option value="">New</option> <option value="">New</option>
<option value="processing">Processing</option> <option value="processing">Processing</option>
<option value="partial-processing">
Partial Processing
</option>
<option value="cancelled">Cancelled</option> <option value="cancelled">Cancelled</option>
</> </>
)} )}
@ -593,6 +640,14 @@ const ViewOrders = () => {
</DialogActions> </DialogActions>
</form> </form>
</Dialog> </Dialog>
{/* partial processing */}
<OrderDetailsDialog
open={parialModal}
onClose={handlePartialOrderClose}
order={order}
onSubmit={handlePartialProcess}
/>
</Box> </Box>
); );
}; };

View File

@ -0,0 +1,128 @@
import React, { useState } from "react";
import {
Dialog,
DialogActions,
DialogContent,
DialogTitle,
Button,
TextField,
Typography,
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableRow,
Paper,
Grid,
} from "@mui/material";
const OrderDetailsDialog = ({ open, onClose, order, onSubmit }) => {
// Create a state to store availability input for each product
const [availability, setAvailability] = useState(
order?.orderItem.map((item) => ({
...item, // Keep all original properties from orderItem
processquantity: item.quantity, // Add availability field with default value equal to quantity
}))
);
// Handle input change for availability
const handleAvailabilityChange = (index, value) => {
const updatedAvailability = [...availability];
const newValue = Math.max(
0,
Math.min(value, updatedAvailability[index].quantity)
); // Ensure value is between 0 and available quantity
updatedAvailability[index].processquantity = newValue;
setAvailability(updatedAvailability);
};
// Handle form submission
const handleSubmit = () => {
onSubmit(availability); // Pass updated availability to parent component
onClose(); // Close modal
};
return (
<Dialog open={open} onClose={onClose} maxWidth="md" fullWidth>
<DialogTitle>Modify Product Availability</DialogTitle>
<DialogContent>
<Grid container spacing={2}>
<Grid item xs={12}>
<TableContainer component={Paper}>
<Table>
<TableHead>
<TableRow>
<TableCell>Product</TableCell>
<TableCell align="right">Price ()</TableCell>
<TableCell align="right">Quantity</TableCell>
<TableCell align="right">Availability</TableCell>
<TableCell align="right">Subtotal ()</TableCell>
<TableCell align="right">GST (%)</TableCell>
<TableCell align="right">GST Amount ()</TableCell>
<TableCell align="right">Total with GST ()</TableCell>
</TableRow>
</TableHead>
<TableBody>
{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 (
<TableRow key={index}>
<TableCell>
<img
src={item.productId.image}
alt={item.productId.name}
style={{ width: 50, height: 50, marginRight: 10 }}
/>
<Typography variant="subtitle1">
{item.productId.name}
</Typography>
</TableCell>
<TableCell align="right">{item.price}</TableCell>
<TableCell align="right">{item.quantity}</TableCell>
<TableCell align="right">
<TextField
sx={{ minWidth: "100px" }}
type="number"
value={availability[index].processquantity}
onChange={(e) =>
handleAvailabilityChange(
index,
Number(e.target.value)
)
}
inputProps={{
min: 0,
max: item.quantity,
}}
style={{ width: "60px", textAlign: "center" }}
/>
</TableCell>
<TableCell align="right">{subtotal}</TableCell>
<TableCell align="right">{item.GST}%</TableCell>
<TableCell align="right">{gstAmount}</TableCell>
<TableCell align="right">{totalWithGST}</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
</TableContainer>
</Grid>
</Grid>
</DialogContent>
<DialogActions>
<Button onClick={onClose}>Cancel</Button>
<Button onClick={handleSubmit} variant="contained" color="primary">
Submit
</Button>
</DialogActions>
</Dialog>
);
};
export default OrderDetailsDialog;