From 12d7c0050ef6077abe6b8490d0012f69278148d8 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 5 Mar 2025 12:30:00 +0530 Subject: [PATCH] changes in accept quotation --- src/controllers/storeController.js | 103 +++++++++++++++++++++++------ src/models/store.js | 48 +++++++++++++- 2 files changed, 129 insertions(+), 22 deletions(-) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 57738a50..7d089e54 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -11,7 +11,7 @@ const fastify = require("fastify")({ }, }); -const {Order,EstimationOrder,Iotprice, Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,WaterLeverSensor,MotorSwitchSenso,Insensors,generatequatationId, HardwareCart, ServiceCart, Sales} = require("../models/store"); +const {SensorStock,Order,EstimationOrder,Iotprice, Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,WaterLeverSensor,MotorSwitchSenso,Insensors,generatequatationId, HardwareCart, ServiceCart, Sales} = require("../models/store"); const { User,Counter, generateBookingId,resetCounter,generateCustomerId,ProfilePicture} = require('../models/User') @@ -1262,6 +1262,7 @@ exports.updateSensorQC = async (req, reply) => { // Update qccheckdate with the current date in "DD-MMM-YYYY - HH:MM" format filteredUpdateData.qccheckdate = moment().format("DD-MMM-YYYY - HH:mm"); + // Find the sensor by ID const updatedSensor = await Insensors.findByIdAndUpdate( _id, filteredUpdateData, @@ -1272,6 +1273,25 @@ exports.updateSensorQC = async (req, reply) => { return reply.code(404).send({ message: "Sensor not found" }); } + // Update stock based on QC result + const stockRecord = await SensorStock.findOne({ storeId: updatedSensor.storeId, type: updatedSensor.type }); + + if (stockRecord) { + if (filteredUpdateData.qccheck && filteredUpdateData.qccheck.toLowerCase() === "ok") { + // If QC is "ok", move 1 from total_count_before_qc to total_available + await SensorStock.updateOne( + { storeId: updatedSensor.storeId, type: updatedSensor.type }, + { $inc: { total_count_before_qc: -1, total_available: 1 } } + ); + } else { + // If QC is failed, move 1 from total_count_before_qc to total_repair + await SensorStock.updateOne( + { storeId: updatedSensor.storeId, type: updatedSensor.type }, + { $inc: { total_count_before_qc: -1, total_repair: 1 } } + ); + } + } + return reply.code(200).send(updatedSensor); } catch (error) { console.error("Error updating QC fields:", error); @@ -1345,19 +1365,17 @@ const generateBatchNo = (type, hardwareIdCompany) => { }; exports.createSensor = async (req, reply) => { - try { + try { const storeId = req.params.storeId; - const { indate, batchno, hardwareId_company, quantity, model,type } = req.body; + const { indate, batchno, hardwareId_company, quantity, model, type } = req.body; const sensorType = type.toLowerCase(); let finalBatchNo = batchno; - if (batchno === 'New') { + // Generate unique batch number if 'New' is received + if (batchno === "New") { let isUnique = false; - while (!isUnique) { finalBatchNo = generateBatchNo(type, hardwareId_company); - - // Check for uniqueness const existingBatchNo = await Insensors.findOne({ batchno: finalBatchNo }); if (!existingBatchNo) { isUnique = true; @@ -1365,23 +1383,43 @@ exports.createSensor = async (req, reply) => { } } - const date = moment().format('MM-DD'); let entries = []; - for (let i = 0; i < quantity; i++) { - const newSensor = { + entries.push({ storeId, model, batchno: finalBatchNo, type: sensorType, indate, - hardwareId_company - }; - - entries.push(newSensor); + hardwareId_company, + }); } + // Insert new sensors into Insensors collection const savedSensors = await Insensors.insertMany(entries); + + // Update stock information in SensorStock + const stockRecord = await SensorStock.findOne({ storeId, type: sensorType }); + + if (stockRecord) { + // If stock record exists, update total_count + await SensorStock.updateOne( + { storeId, type: sensorType }, + { $inc: { total_count: quantity, total_count_before_qc: quantity } } + ); + } else { + // If no stock record exists, create a new one + await SensorStock.create({ + storeId, + type: sensorType, + total_count: quantity, + total_available: quantity, + total_blocked: 0, + total_repair: 0, + total_installed: 0, + }); + } + reply.code(200).send(savedSensors); } catch (err) { reply.code(500).send(err); @@ -2050,22 +2088,49 @@ exports.acceptQuotation = async (req, reply) => { message: "Quotation rejected successfully", }); } else if (action === "accept") { + const { customerId, masters, slaves, sensors } = quotation; + // Convert quotation to an order object and include storeId const newOrder = new Order({ - ...quotation.toObject(), // Copy all fields from quotation - storeId: storeId, // Ensure storeId is included - status: "pending", // Set status to "pending" + ...quotation.toObject(), + storeId, + status: "pending", }); - // Save to the Orders collection + // Save the order await newOrder.save(); + // Update Insensors: Block the sensors and assign customerId + const sensorTypes = [ + { type: "master", count: parseInt(masters || 0) }, + { type: "slave", count: parseInt(slaves || 0) }, + { type: "sensor", count: parseInt(sensors || 0) }, + ]; + + for (const sensor of sensorTypes) { + if (sensor.count > 0) { + await Insensors.updateMany( + { storeId, type: sensor.type, status: "available" }, + { $set: { status: "blocked", customerId } }, + { limit: sensor.count } + ); + + // Update SensorStock + await SensorStock.updateOne( + { storeId, type: sensor.type }, + { + $inc: { total_available: -sensor.count, total_blocked: sensor.count }, + } + ); + } + } + // Delete the record from SensorQuotation await SensorQuotation.deleteOne({ quatationId: quotationId }); return reply.send({ status_code: 200, - message: "Quotation accepted and moved to Orders", + message: "Quotation accepted, sensors blocked, and moved to Orders", data: newOrder, }); } else { diff --git a/src/models/store.js b/src/models/store.js index 68f52458..09dfe70b 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -468,7 +468,48 @@ const orderSchema = new mongoose.Schema({ status: { type: String, default: "pending" }, // Status field added }); - +const SensorStockSchema = new mongoose.Schema({ + storeId: { + type: String, + required: true, + ref: "Store", + }, + type: { + type: String, + required: true, + enum: ["master", "slave", "sensor"], // Ensures only valid types + }, + total_count: { + type: Number, + required: true, + default: 0, + }, + total_available: { + type: Number, + required: true, + default: 0, + }, + total_count_before_qc: { + type: Number, + required: true, + default: 0, + }, + total_blocked: { + type: Number, + required: true, + default: 0, + }, + total_repair: { + type: Number, + required: true, + default: 0, + }, + total_installed: { + type: Number, + required: true, + default: 0, + }, +}, { timestamps: true }); const hardwareCartSchema = new mongoose.Schema({ @@ -574,7 +615,8 @@ const Iotprice = mongoose.model('Iotprice', iotpriceSchema); const ProfilePictureInstall = mongoose.model('ProfilePictureInstall', profilePictureInstallSchema); const MotorSwitchSensor = mongoose.model('MotorSwitchSensor', motorSwitchSensorInSchema); const SensorQuotation = mongoose.model('SensorQuotationSchema', sensorquotationSchema); - + + const SensorStock = mongoose.model("SensorStock", SensorStockSchema); const Install = mongoose.model("Install", installationschema); const Survey = mongoose.model("Survey", surveyschema); @@ -584,4 +626,4 @@ const Iotprice = mongoose.model('Iotprice', iotpriceSchema); - module.exports = {Order,EstimationOrder,Iotprice,Sales, Install,Survey, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,ProfilePictureStore,WaterLeverSensor,MotorSwitchSensor,Insensors,generatequatationId, HardwareCart, ServiceCart}; + module.exports = {SensorStock,Order,EstimationOrder,Iotprice,Sales, Install,Survey, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,ProfilePictureStore,WaterLeverSensor,MotorSwitchSensor,Insensors,generatequatationId, HardwareCart, ServiceCart};