diff --git a/Utils/sendPushNotification.js b/Utils/sendPushNotification.js index 6082800..d456fe8 100644 --- a/Utils/sendPushNotification.js +++ b/Utils/sendPushNotification.js @@ -1,5 +1,6 @@ import admin from "firebase-admin"; -import serviceAccount from "../googlefirebasePushnotification.json" with { type: "json" }; +// import serviceAccount from "../googlefirebasePushnotification.json" with { type: "json" }; +import serviceAccount from "../googlefirebasePushnotification.json" assert { type: "json" }; export const sendPushNotification = async (userToken,title, message) => { // const admin = require("firebase-admin"); // const serviceAccount = require("./path/to/your-firebase-adminsdk.json"); diff --git a/app.js b/app.js index 8eab3b4..2074ece 100644 --- a/app.js +++ b/app.js @@ -10,7 +10,8 @@ import cookieParser from "cookie-parser"; // firebase admin push notification import admin from "firebase-admin"; -import serviceAccount from "./googlefirebasePushnotification.json" with { type: "json" }; +// import serviceAccount from "./googlefirebasePushnotification.json" with { type: "json" }; +import serviceAccount from "./googlefirebasePushnotification.json" assert { type: "json" }; // Design Router import designRoute from "./resources/Design/designRouter.js"; app.use(express.json({ limit: "50mb" })); diff --git a/resources/PD_Orders/pdOrderController.js b/resources/PD_Orders/pdOrderController.js index 10446ea..40ce974 100644 --- a/resources/PD_Orders/pdOrderController.js +++ b/resources/PD_Orders/pdOrderController.js @@ -1125,11 +1125,13 @@ export const updateCourierStatusToDelivered = async (req, res) => { if (existingProduct) { // If the product exists, update the stock by adding the processquantity existingProduct.Stock += processquantity; + existingProduct.monthlyorderquantity+=processquantity; } else { // If the product doesn't exist, add a new entry for the product pdStock.products.push({ productid: productId, Stock: processquantity, + monthlyorderquantity: processquantity, }); } } diff --git a/resources/RD_Ordes/rdOrderController.js b/resources/RD_Ordes/rdOrderController.js index 379a43d..3460ab8 100644 --- a/resources/RD_Ordes/rdOrderController.js +++ b/resources/RD_Ordes/rdOrderController.js @@ -417,6 +417,7 @@ export const processOrder = async (req, res) => { if (item.processquantity <= productInStock.Stock) { // Deduct the quantity from the stock productInStock.Stock -= item.processquantity; + productInStock.liquidation += item.processquantity; // Add the item to updatedInvoiceItems (since stock is sufficient) return true; } diff --git a/resources/Stock/PdStockModel.js b/resources/Stock/PdStockModel.js index 39819cb..d911ff0 100644 --- a/resources/Stock/PdStockModel.js +++ b/resources/Stock/PdStockModel.js @@ -30,6 +30,18 @@ const ProductRecordSchema = new mongoose.Schema({ type: Number, default: 0, }, + monthstartstock: { + type: Number, + default: 0, + }, + liquidation: { + type: Number, + default: 0, + }, + monthlyorderquantity: { + type: Number, + default: 0, + }, }); // Define main Stock schema @@ -46,4 +58,21 @@ const StockSchema = new mongoose.Schema( { timestamps: true, versionKey: false } ); +// Static method to reset monthly fields +StockSchema.statics.resetMonthlyFields = async function () { + try { + const stocks = await this.find(); + for (const stock of stocks) { + for (const product of stock.products) { + product.liquidation = 0; + product.monthstartstock = product.Stock; + product.monthlyorderquantity = 0; + } + await stock.save(); + } + console.log("Monthly reset completed successfully!"); + } catch (error) { + console.error("Error during monthly reset:", error); + } +}; export const PDStock = mongoose.model("PDStock", StockSchema); diff --git a/resources/Stock/StockController.js b/resources/Stock/StockController.js index 7598240..851dc5f 100644 --- a/resources/Stock/StockController.js +++ b/resources/Stock/StockController.js @@ -67,7 +67,7 @@ export const uploadOpeningInventorypd = async (req, res) => { if (!stock) { stock = new PDStock({ userId: req.params.userId, products: [] }); } - +// console.log(stock); for (let i = 1; i < data.length; i++) { const row = data[i]; // Skip empty rows @@ -154,6 +154,7 @@ export const uploadOpeningInventorypd = async (req, res) => { if (Number(existingProduct.openingInventory) !== newOpeningInventory) { existingProduct.openingInventory = newOpeningInventory; existingProduct.Stock = newOpeningInventory; + existingProduct.monthstartstock = newOpeningInventory; updatedOpeningInventories.push({ SKU: existingProduct.SKU, updatedFields: "openingInventory", @@ -169,6 +170,7 @@ export const uploadOpeningInventorypd = async (req, res) => { productName: item.productName, openingInventory: item.openingInventory, Stock: item.openingInventory, + monthstartstock: item.openingInventory, }); newlyCreated.push({ SKU: item.SKU, @@ -191,6 +193,7 @@ export const uploadOpeningInventorypd = async (req, res) => { productName: product.name, openingInventory: 0, Stock: 0, + monthstartstock: 0, }); newlyCreated.push({ SKU: product.SKU, @@ -492,6 +495,9 @@ export const getProductsAndStockByPD = async (req, res) => { stockMap[product.productid.toString()] = { Stock: product.Stock, openingInventory: product.openingInventory, + monthstartstock: product.monthstartstock, + liquidation: product.liquidation, + monthlyorderquantity: product.monthlyorderquantity, }; }); } @@ -501,6 +507,10 @@ export const getProductsAndStockByPD = async (req, res) => { ...product, stock: stockMap[product._id.toString()]?.Stock || 0, openingInventory: stockMap[product._id.toString()]?.openingInventory || 0, + monthstartstock: stockMap[product._id.toString()]?.monthstartstock || 0, + liquidation: stockMap[product._id.toString()]?.liquidation || 0, + monthlyorderquantity: + stockMap[product._id.toString()]?.monthlyorderquantity || 0, })); // Get total count for pagination purposes @@ -748,6 +758,7 @@ export const createOrUpdateInventory = async (req, res) => { productName: productInSystem.name, openingInventory: openingInventory || 0, Stock: openingInventory || 0, + monthstartstock: openingInventory || 0, }); } @@ -1143,3 +1154,44 @@ export const getAllUsersWithStock = async (req, res) => { }); } }; + + +// Function to run the reset logic +export const runMonthlyReset = async () => { + try { + const now = new Date(); + if (now.getDate() === 1) { // Check if today is the 1st + console.log("Running monthly reset..."); + await PDStock.resetMonthlyFields(); + } else { + console.log("Not the 1st of the month, skipping reset."); + } + } catch (error) { + console.error("Error in monthly reset controller:", error); + } +}; + +// Function to schedule the task at 6:00 AM daily +const scheduleDailyTask = () => { + const now = new Date(); + const nextRun = new Date(); + + // Set the next run time to 6:00 AM + nextRun.setHours(6, 0, 0, 0); +// console.log(now, nextRun); + // If the current time is past 6:00 AM, schedule for the next day + if (now > nextRun) { + nextRun.setDate(nextRun.getDate() + 1); + } + + const delay = nextRun - now; // Calculate the delay in milliseconds + console.log(`Scheduled to run at: ${nextRun.toLocaleString()}`); +// console.log(`Delay: ${delay} ms`); + setTimeout(() => { + runMonthlyReset(); // Run the task + setInterval(runMonthlyReset, 24 * 60 * 60 * 1000); // Set a daily interval after the first run + }, delay); +}; + +// Call the scheduler +scheduleDailyTask();