search functionality add in get invoice of process,dispatch and delivered
This commit is contained in:
parent
13349cc82e
commit
2968131835
@ -105,13 +105,14 @@ export const processOrder = async (req, res) => {
|
||||
// If there are any exceeded items, return an error with their names
|
||||
if (exceededItems.length > 0) {
|
||||
return res.status(400).json({
|
||||
error: `The following items have more quantity than remaining in the order: ${exceededItems.join(", ")}`,
|
||||
error: `The following items have more quantity than remaining in the order: ${exceededItems.join(
|
||||
", "
|
||||
)}`,
|
||||
});
|
||||
}
|
||||
|
||||
// Continue with the rest of the logic if no quantity limits are exceeded
|
||||
|
||||
|
||||
// Generate unique invoice number
|
||||
const existingInvoices = await Invoice.find({ orderId });
|
||||
const invoiceNumber = existingInvoices.length + 1;
|
||||
@ -144,55 +145,54 @@ export const processOrder = async (req, res) => {
|
||||
// Save the invoice
|
||||
const savedInvoice = await newInvoice.save();
|
||||
|
||||
// Update the order's order items with the remaining quantity
|
||||
let allItemsProcessed = true; // Flag to check if all items are processed
|
||||
// Update the order's order items with the remaining quantity
|
||||
let allItemsProcessed = true; // Flag to check if all items are processed
|
||||
|
||||
order.orderItem.forEach((item) => {
|
||||
const invoicedItem = invoiceItems.find(
|
||||
(i) => i.productId.toString() === item.productId.toString()
|
||||
);
|
||||
order.orderItem.forEach((item) => {
|
||||
const invoicedItem = invoiceItems.find(
|
||||
(i) => i.productId.toString() === item.productId.toString()
|
||||
);
|
||||
|
||||
// If the item was invoiced, update the remaining quantity
|
||||
if (invoicedItem) {
|
||||
// Deduct the processed quantity from remaining quantity
|
||||
item.remainingQuantity -= invoicedItem.processquantity;
|
||||
// If the item was invoiced, update the remaining quantity
|
||||
if (invoicedItem) {
|
||||
// Deduct the processed quantity from remaining quantity
|
||||
item.remainingQuantity -= invoicedItem.processquantity;
|
||||
|
||||
// Ensure remaining quantity does not go negative
|
||||
if (item.remainingQuantity < 0) {
|
||||
item.remainingQuantity = 0;
|
||||
// Ensure remaining quantity does not go negative
|
||||
if (item.remainingQuantity < 0) {
|
||||
item.remainingQuantity = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the remaining quantity is greater than 0, even for items not invoiced
|
||||
if (item.remainingQuantity > 0) {
|
||||
allItemsProcessed = false;
|
||||
}
|
||||
});
|
||||
|
||||
// Calculate total amount for pending items
|
||||
let pendingTotalAmount = 0;
|
||||
order.orderItem.forEach((item) => {
|
||||
if (item.remainingQuantity > 0) {
|
||||
const itemPendingSubtotal = item.price * item.remainingQuantity;
|
||||
const itemPendingGST =
|
||||
((item.price * item.GST) / 100) * item.remainingQuantity;
|
||||
pendingTotalAmount += itemPendingSubtotal + itemPendingGST;
|
||||
}
|
||||
});
|
||||
|
||||
// Only update order status if all items have been fully processed
|
||||
if (allItemsProcessed) {
|
||||
order.status = "processing"; // All items are fully processed
|
||||
} else {
|
||||
order.status = "pending"; // There are still remaining quantities
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the remaining quantity is greater than 0, even for items not invoiced
|
||||
if (item.remainingQuantity > 0) {
|
||||
allItemsProcessed = false;
|
||||
}
|
||||
});
|
||||
|
||||
// Calculate total amount for pending items
|
||||
let pendingTotalAmount = 0;
|
||||
order.orderItem.forEach((item) => {
|
||||
if (item.remainingQuantity > 0) {
|
||||
const itemPendingSubtotal = item.price * item.remainingQuantity;
|
||||
const itemPendingGST =
|
||||
((item.price * item.GST) / 100) * item.remainingQuantity;
|
||||
pendingTotalAmount += itemPendingSubtotal + itemPendingGST;
|
||||
}
|
||||
});
|
||||
|
||||
// Only update order status if all items have been fully processed
|
||||
if (allItemsProcessed) {
|
||||
order.status = "processing"; // All items are fully processed
|
||||
} else {
|
||||
order.status = "pending"; // There are still remaining quantities
|
||||
}
|
||||
|
||||
// Add the invoice to the order
|
||||
order.invoices.push(savedInvoice._id);
|
||||
|
||||
// Save the updated order
|
||||
await order.save();
|
||||
// Add the invoice to the order
|
||||
order.invoices.push(savedInvoice._id);
|
||||
|
||||
// Save the updated order
|
||||
await order.save();
|
||||
|
||||
// Prepare the email content
|
||||
const processedItems = invoiceItems
|
||||
@ -806,31 +806,47 @@ export const getProcessingInvoices = async (req, res) => {
|
||||
try {
|
||||
const page = parseInt(req.query.page, 10) || 1;
|
||||
const limit = parseInt(req.query.limit, 10) || 5;
|
||||
|
||||
// Calculate the number of documents to skip
|
||||
const { invoiceId, orderId } = req.query;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
// Get the total count of 'processing' invoices
|
||||
const totalInvoices = await Invoice.countDocuments({
|
||||
courierStatus: "processing",
|
||||
});
|
||||
let query = { courierStatus: "processing" };
|
||||
|
||||
// Fetch the invoices with pagination
|
||||
const invoices = await Invoice.find({ courierStatus: "processing" })
|
||||
if (invoiceId) {
|
||||
query.invoiceId = { $regex: invoiceId, $options: "i" };
|
||||
}
|
||||
const invoices = await Invoice.find(query)
|
||||
.sort({ createdAt: -1 })
|
||||
.skip(skip)
|
||||
.limit(limit);
|
||||
// Respond with the invoices and the total count
|
||||
res.status(200).json({
|
||||
totalCount: totalInvoices,
|
||||
currentPage: page,
|
||||
totalPages: Math.ceil(totalInvoices / limit),
|
||||
invoices,
|
||||
});
|
||||
.limit(limit)
|
||||
.populate({
|
||||
path: "orderId",
|
||||
select: "uniqueId",
|
||||
});
|
||||
|
||||
if (orderId) {
|
||||
const filteredInvoices = invoices.filter(invoice =>
|
||||
invoice.orderId && invoice.orderId.uniqueId &&
|
||||
invoice.orderId.uniqueId.toLowerCase().includes(orderId.toLowerCase())
|
||||
);
|
||||
res.status(200).json({
|
||||
totalCount: filteredInvoices.length,
|
||||
currentPage: page,
|
||||
totalPages: Math.ceil(filteredInvoices.length / limit),
|
||||
invoices: filteredInvoices,
|
||||
});
|
||||
} else {
|
||||
res.status(200).json({
|
||||
totalCount: invoices.length,
|
||||
currentPage: page,
|
||||
totalPages: Math.ceil(invoices.length / limit),
|
||||
invoices,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const getInvoiceDetailsById = async (req, res) => {
|
||||
const { invoiceId } = req.params;
|
||||
|
||||
@ -950,13 +966,14 @@ export const updateCourierStatusToDispatched = async (req, res) => {
|
||||
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||
product.price
|
||||
}</td>
|
||||
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||
((product.GST * product.price) / 100).toFixed(2)
|
||||
}</td>
|
||||
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||
((product.price + (product.GST * product.price) / 100) *
|
||||
product.processquantity).toFixed(2)
|
||||
}</td>
|
||||
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${(
|
||||
(product.GST * product.price) /
|
||||
100
|
||||
).toFixed(2)}</td>
|
||||
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${(
|
||||
(product.price + (product.GST * product.price) / 100) *
|
||||
product.processquantity
|
||||
).toFixed(2)}</td>
|
||||
</tr>
|
||||
`
|
||||
)
|
||||
@ -991,31 +1008,47 @@ export const getDispatchedInvoices = async (req, res) => {
|
||||
try {
|
||||
const page = parseInt(req.query.page, 10) || 1;
|
||||
const limit = parseInt(req.query.limit, 10) || 5;
|
||||
|
||||
// Calculate the number of documents to skip
|
||||
const { invoiceId, orderId } = req.query;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
// Get the total count of 'processing' invoices
|
||||
const totalInvoices = await Invoice.countDocuments({
|
||||
courierStatus: "dispatched",
|
||||
});
|
||||
let query = { courierStatus: "dispatched" };
|
||||
|
||||
// Fetch the invoices with pagination
|
||||
const invoices = await Invoice.find({ courierStatus: "dispatched" })
|
||||
if (invoiceId) {
|
||||
query.invoiceId = { $regex: invoiceId, $options: "i" };
|
||||
}
|
||||
const invoices = await Invoice.find(query)
|
||||
.sort({ createdAt: -1 })
|
||||
.skip(skip)
|
||||
.limit(limit);
|
||||
// Respond with the invoices and the total count
|
||||
res.status(200).json({
|
||||
totalCount: totalInvoices,
|
||||
currentPage: page,
|
||||
totalPages: Math.ceil(totalInvoices / limit),
|
||||
invoices,
|
||||
});
|
||||
.limit(limit)
|
||||
.populate({
|
||||
path: "orderId",
|
||||
select: "uniqueId",
|
||||
});
|
||||
|
||||
if (orderId) {
|
||||
const filteredInvoices = invoices.filter(invoice =>
|
||||
invoice.orderId && invoice.orderId.uniqueId &&
|
||||
invoice.orderId.uniqueId.toLowerCase().includes(orderId.toLowerCase())
|
||||
);
|
||||
res.status(200).json({
|
||||
totalCount: filteredInvoices.length,
|
||||
currentPage: page,
|
||||
totalPages: Math.ceil(filteredInvoices.length / limit),
|
||||
invoices: filteredInvoices,
|
||||
});
|
||||
} else {
|
||||
res.status(200).json({
|
||||
totalCount: invoices.length,
|
||||
currentPage: page,
|
||||
totalPages: Math.ceil(invoices.length / limit),
|
||||
invoices,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const updateCourierStatusToDelivered = async (req, res) => {
|
||||
const { invoiceId } = req.params;
|
||||
|
||||
@ -1107,22 +1140,23 @@ export const updateCourierStatusToDelivered = async (req, res) => {
|
||||
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||
product.price
|
||||
}</td>
|
||||
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||
((product.GST * product.price) / 100).toFixed(2)
|
||||
}</td>
|
||||
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||
((product.price + (product.GST * product.price) / 100) *
|
||||
product.processquantity).toFixed(2)
|
||||
}</td>
|
||||
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${(
|
||||
(product.GST * product.price) /
|
||||
100
|
||||
).toFixed(2)}</td>
|
||||
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${(
|
||||
(product.price + (product.GST * product.price) / 100) *
|
||||
product.processquantity
|
||||
).toFixed(2)}</td>
|
||||
</tr>
|
||||
`
|
||||
)
|
||||
.join("")}
|
||||
<tr>
|
||||
<th colspan="7" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount:</th>
|
||||
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||
invoice.invoiceAmount.toFixed(2)
|
||||
}</td>
|
||||
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${invoice.invoiceAmount.toFixed(
|
||||
2
|
||||
)}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
@ -1159,31 +1193,47 @@ export const getDeliveredInvoices = async (req, res) => {
|
||||
try {
|
||||
const page = parseInt(req.query.page, 10) || 1;
|
||||
const limit = parseInt(req.query.limit, 10) || 5;
|
||||
|
||||
// Calculate the number of documents to skip
|
||||
const { invoiceId, orderId } = req.query;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
// Get the total count of 'processing' invoices
|
||||
const totalInvoices = await Invoice.countDocuments({
|
||||
courierStatus: "delivered",
|
||||
});
|
||||
let query = { courierStatus: "delivered" };
|
||||
|
||||
// Fetch the invoices with pagination
|
||||
const invoices = await Invoice.find({ courierStatus: "delivered" })
|
||||
if (invoiceId) {
|
||||
query.invoiceId = { $regex: invoiceId, $options: "i" };
|
||||
}
|
||||
const invoices = await Invoice.find(query)
|
||||
.sort({ createdAt: -1 })
|
||||
.skip(skip)
|
||||
.limit(limit);
|
||||
// Respond with the invoices and the total count
|
||||
res.status(200).json({
|
||||
totalCount: totalInvoices,
|
||||
currentPage: page,
|
||||
totalPages: Math.ceil(totalInvoices / limit),
|
||||
invoices,
|
||||
});
|
||||
.limit(limit)
|
||||
.populate({
|
||||
path: "orderId",
|
||||
select: "uniqueId",
|
||||
});
|
||||
|
||||
if (orderId) {
|
||||
const filteredInvoices = invoices.filter(invoice =>
|
||||
invoice.orderId && invoice.orderId.uniqueId &&
|
||||
invoice.orderId.uniqueId.toLowerCase().includes(orderId.toLowerCase())
|
||||
);
|
||||
res.status(200).json({
|
||||
totalCount: filteredInvoices.length,
|
||||
currentPage: page,
|
||||
totalPages: Math.ceil(filteredInvoices.length / limit),
|
||||
invoices: filteredInvoices,
|
||||
});
|
||||
} else {
|
||||
res.status(200).json({
|
||||
totalCount: invoices.length,
|
||||
currentPage: page,
|
||||
totalPages: Math.ceil(invoices.length / limit),
|
||||
invoices,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const getDeliveredOrdersAdmin = async (req, res) => {
|
||||
try {
|
||||
// Extract page and limit from query parameters
|
||||
|
Loading…
Reference in New Issue
Block a user