Liqudation

This commit is contained in:
Sibunnayak 2025-01-11 14:15:23 +05:30
parent 8e58b4b5f3
commit cc70e28ab6
6 changed files with 89 additions and 3 deletions

View File

@ -1,5 +1,6 @@
import admin from "firebase-admin"; 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) => { export const sendPushNotification = async (userToken,title, message) => {
// const admin = require("firebase-admin"); // const admin = require("firebase-admin");
// const serviceAccount = require("./path/to/your-firebase-adminsdk.json"); // const serviceAccount = require("./path/to/your-firebase-adminsdk.json");

3
app.js
View File

@ -10,7 +10,8 @@ import cookieParser from "cookie-parser";
// firebase admin push notification // firebase admin push notification
import admin from "firebase-admin"; 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 // Design Router
import designRoute from "./resources/Design/designRouter.js"; import designRoute from "./resources/Design/designRouter.js";
app.use(express.json({ limit: "50mb" })); app.use(express.json({ limit: "50mb" }));

View File

@ -1125,11 +1125,13 @@ export const updateCourierStatusToDelivered = async (req, res) => {
if (existingProduct) { if (existingProduct) {
// If the product exists, update the stock by adding the processquantity // If the product exists, update the stock by adding the processquantity
existingProduct.Stock += processquantity; existingProduct.Stock += processquantity;
existingProduct.monthlyorderquantity+=processquantity;
} else { } else {
// If the product doesn't exist, add a new entry for the product // If the product doesn't exist, add a new entry for the product
pdStock.products.push({ pdStock.products.push({
productid: productId, productid: productId,
Stock: processquantity, Stock: processquantity,
monthlyorderquantity: processquantity,
}); });
} }
} }

View File

@ -417,6 +417,7 @@ export const processOrder = async (req, res) => {
if (item.processquantity <= productInStock.Stock) { if (item.processquantity <= productInStock.Stock) {
// Deduct the quantity from the stock // Deduct the quantity from the stock
productInStock.Stock -= item.processquantity; productInStock.Stock -= item.processquantity;
productInStock.liquidation += item.processquantity;
// Add the item to updatedInvoiceItems (since stock is sufficient) // Add the item to updatedInvoiceItems (since stock is sufficient)
return true; return true;
} }

View File

@ -30,6 +30,18 @@ const ProductRecordSchema = new mongoose.Schema({
type: Number, type: Number,
default: 0, default: 0,
}, },
monthstartstock: {
type: Number,
default: 0,
},
liquidation: {
type: Number,
default: 0,
},
monthlyorderquantity: {
type: Number,
default: 0,
},
}); });
// Define main Stock schema // Define main Stock schema
@ -46,4 +58,21 @@ const StockSchema = new mongoose.Schema(
{ timestamps: true, versionKey: false } { 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); export const PDStock = mongoose.model("PDStock", StockSchema);

View File

@ -67,7 +67,7 @@ export const uploadOpeningInventorypd = async (req, res) => {
if (!stock) { if (!stock) {
stock = new PDStock({ userId: req.params.userId, products: [] }); stock = new PDStock({ userId: req.params.userId, products: [] });
} }
// console.log(stock);
for (let i = 1; i < data.length; i++) { for (let i = 1; i < data.length; i++) {
const row = data[i]; const row = data[i];
// Skip empty rows // Skip empty rows
@ -154,6 +154,7 @@ export const uploadOpeningInventorypd = async (req, res) => {
if (Number(existingProduct.openingInventory) !== newOpeningInventory) { if (Number(existingProduct.openingInventory) !== newOpeningInventory) {
existingProduct.openingInventory = newOpeningInventory; existingProduct.openingInventory = newOpeningInventory;
existingProduct.Stock = newOpeningInventory; existingProduct.Stock = newOpeningInventory;
existingProduct.monthstartstock = newOpeningInventory;
updatedOpeningInventories.push({ updatedOpeningInventories.push({
SKU: existingProduct.SKU, SKU: existingProduct.SKU,
updatedFields: "openingInventory", updatedFields: "openingInventory",
@ -169,6 +170,7 @@ export const uploadOpeningInventorypd = async (req, res) => {
productName: item.productName, productName: item.productName,
openingInventory: item.openingInventory, openingInventory: item.openingInventory,
Stock: item.openingInventory, Stock: item.openingInventory,
monthstartstock: item.openingInventory,
}); });
newlyCreated.push({ newlyCreated.push({
SKU: item.SKU, SKU: item.SKU,
@ -191,6 +193,7 @@ export const uploadOpeningInventorypd = async (req, res) => {
productName: product.name, productName: product.name,
openingInventory: 0, openingInventory: 0,
Stock: 0, Stock: 0,
monthstartstock: 0,
}); });
newlyCreated.push({ newlyCreated.push({
SKU: product.SKU, SKU: product.SKU,
@ -492,6 +495,9 @@ export const getProductsAndStockByPD = async (req, res) => {
stockMap[product.productid.toString()] = { stockMap[product.productid.toString()] = {
Stock: product.Stock, Stock: product.Stock,
openingInventory: product.openingInventory, openingInventory: product.openingInventory,
monthstartstock: product.monthstartstock,
liquidation: product.liquidation,
monthlyorderquantity: product.monthlyorderquantity,
}; };
}); });
} }
@ -501,6 +507,10 @@ export const getProductsAndStockByPD = async (req, res) => {
...product, ...product,
stock: stockMap[product._id.toString()]?.Stock || 0, stock: stockMap[product._id.toString()]?.Stock || 0,
openingInventory: stockMap[product._id.toString()]?.openingInventory || 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 // Get total count for pagination purposes
@ -748,6 +758,7 @@ export const createOrUpdateInventory = async (req, res) => {
productName: productInSystem.name, productName: productInSystem.name,
openingInventory: openingInventory || 0, openingInventory: openingInventory || 0,
Stock: 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();