|
|
@ -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')
|
|
|
|
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
|
|
|
|
// Update qccheckdate with the current date in "DD-MMM-YYYY - HH:MM" format
|
|
|
|
filteredUpdateData.qccheckdate = moment().format("DD-MMM-YYYY - HH:mm");
|
|
|
|
filteredUpdateData.qccheckdate = moment().format("DD-MMM-YYYY - HH:mm");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Find the sensor by ID
|
|
|
|
const updatedSensor = await Insensors.findByIdAndUpdate(
|
|
|
|
const updatedSensor = await Insensors.findByIdAndUpdate(
|
|
|
|
_id,
|
|
|
|
_id,
|
|
|
|
filteredUpdateData,
|
|
|
|
filteredUpdateData,
|
|
|
@ -1272,6 +1273,25 @@ exports.updateSensorQC = async (req, reply) => {
|
|
|
|
return reply.code(404).send({ message: "Sensor not found" });
|
|
|
|
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);
|
|
|
|
return reply.code(200).send(updatedSensor);
|
|
|
|
} catch (error) {
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error updating QC fields:", error);
|
|
|
|
console.error("Error updating QC fields:", error);
|
|
|
@ -1345,19 +1365,17 @@ const generateBatchNo = (type, hardwareIdCompany) => {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.createSensor = async (req, reply) => {
|
|
|
|
exports.createSensor = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const storeId = req.params.storeId;
|
|
|
|
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();
|
|
|
|
const sensorType = type.toLowerCase();
|
|
|
|
let finalBatchNo = batchno;
|
|
|
|
let finalBatchNo = batchno;
|
|
|
|
|
|
|
|
|
|
|
|
if (batchno === 'New') {
|
|
|
|
// Generate unique batch number if 'New' is received
|
|
|
|
|
|
|
|
if (batchno === "New") {
|
|
|
|
let isUnique = false;
|
|
|
|
let isUnique = false;
|
|
|
|
|
|
|
|
|
|
|
|
while (!isUnique) {
|
|
|
|
while (!isUnique) {
|
|
|
|
finalBatchNo = generateBatchNo(type, hardwareId_company);
|
|
|
|
finalBatchNo = generateBatchNo(type, hardwareId_company);
|
|
|
|
|
|
|
|
|
|
|
|
// Check for uniqueness
|
|
|
|
|
|
|
|
const existingBatchNo = await Insensors.findOne({ batchno: finalBatchNo });
|
|
|
|
const existingBatchNo = await Insensors.findOne({ batchno: finalBatchNo });
|
|
|
|
if (!existingBatchNo) {
|
|
|
|
if (!existingBatchNo) {
|
|
|
|
isUnique = true;
|
|
|
|
isUnique = true;
|
|
|
@ -1365,23 +1383,43 @@ exports.createSensor = async (req, reply) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const date = moment().format('MM-DD');
|
|
|
|
|
|
|
|
let entries = [];
|
|
|
|
let entries = [];
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < quantity; i++) {
|
|
|
|
for (let i = 0; i < quantity; i++) {
|
|
|
|
const newSensor = {
|
|
|
|
entries.push({
|
|
|
|
storeId,
|
|
|
|
storeId,
|
|
|
|
model,
|
|
|
|
model,
|
|
|
|
batchno: finalBatchNo,
|
|
|
|
batchno: finalBatchNo,
|
|
|
|
type: sensorType,
|
|
|
|
type: sensorType,
|
|
|
|
indate,
|
|
|
|
indate,
|
|
|
|
hardwareId_company
|
|
|
|
hardwareId_company,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
entries.push(newSensor);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Insert new sensors into Insensors collection
|
|
|
|
const savedSensors = await Insensors.insertMany(entries);
|
|
|
|
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);
|
|
|
|
reply.code(200).send(savedSensors);
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
reply.code(500).send(err);
|
|
|
|
reply.code(500).send(err);
|
|
|
@ -2050,22 +2088,49 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
message: "Quotation rejected successfully",
|
|
|
|
message: "Quotation rejected successfully",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else if (action === "accept") {
|
|
|
|
} else if (action === "accept") {
|
|
|
|
|
|
|
|
const { customerId, masters, slaves, sensors } = quotation;
|
|
|
|
|
|
|
|
|
|
|
|
// Convert quotation to an order object and include storeId
|
|
|
|
// Convert quotation to an order object and include storeId
|
|
|
|
const newOrder = new Order({
|
|
|
|
const newOrder = new Order({
|
|
|
|
...quotation.toObject(), // Copy all fields from quotation
|
|
|
|
...quotation.toObject(),
|
|
|
|
storeId: storeId, // Ensure storeId is included
|
|
|
|
storeId,
|
|
|
|
status: "pending", // Set status to "pending"
|
|
|
|
status: "pending",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Save to the Orders collection
|
|
|
|
// Save the order
|
|
|
|
await newOrder.save();
|
|
|
|
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
|
|
|
|
// Delete the record from SensorQuotation
|
|
|
|
await SensorQuotation.deleteOne({ quatationId: quotationId });
|
|
|
|
await SensorQuotation.deleteOne({ quatationId: quotationId });
|
|
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
return reply.send({
|
|
|
|
status_code: 200,
|
|
|
|
status_code: 200,
|
|
|
|
message: "Quotation accepted and moved to Orders",
|
|
|
|
message: "Quotation accepted, sensors blocked, and moved to Orders",
|
|
|
|
data: newOrder,
|
|
|
|
data: newOrder,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|