|
|
|
|
@ -10,11 +10,39 @@ const fastify = require("fastify")({
|
|
|
|
|
return uuidv4();
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const { Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,WaterLeverSensor,MotorSwitchSenso,Insensors,generatequatationId, HardwareCart, ServiceCart, Sales} = require("../models/store");
|
|
|
|
|
const {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 populateIotPrices = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const pricingData = [
|
|
|
|
|
{ name: "master", type: "master", cost: 20000 },
|
|
|
|
|
{ name: "slave", type: "slave", cost: 10000 },
|
|
|
|
|
{ name: "sensor", type: "sensor", cost: 10000 },
|
|
|
|
|
{ name: "wire", type: "1core", cost: 100 },
|
|
|
|
|
{ name: "wire", type: "2core", cost: 150 },
|
|
|
|
|
{ name: "wire", type: "3core", cost: 200 },
|
|
|
|
|
{ name: "switch", type: "15A", cost: 20 },
|
|
|
|
|
{ name: "switch", type: "20A", cost: 30 },
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Insert or update pricing data
|
|
|
|
|
for (const item of pricingData) {
|
|
|
|
|
await Iotprice.findOneAndUpdate(
|
|
|
|
|
{ name: item.name, type: item.type }, // Match existing entries
|
|
|
|
|
{ $set: { cost: item.cost } }, // Update cost if exists
|
|
|
|
|
{ upsert: true, new: true } // Insert if not found
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log("Pricing data populated successfully");
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error populating pricing data:", error);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
populateIotPrices();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -1525,6 +1553,49 @@ exports.createquotationforSensor = async (req, reply) => {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.createQuotationForSensor = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const items = req.body;
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(items) || items.length === 0) {
|
|
|
|
|
return reply.code(400).send({ message: "Invalid request body. Expecting an array of items." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch prices from IotPrice based on name and type
|
|
|
|
|
const itemNamesAndTypes = items.map((item) => ({ name: item.name, type: item.type }));
|
|
|
|
|
const prices = await Iotprice.find({ $or: itemNamesAndTypes });
|
|
|
|
|
|
|
|
|
|
let totalAmount = 0;
|
|
|
|
|
let itemDetails = [];
|
|
|
|
|
|
|
|
|
|
for (const item of items) {
|
|
|
|
|
const priceEntry = prices.find((p) => p.name === item.name && p.type === item.type);
|
|
|
|
|
const unitPrice = priceEntry ? priceEntry.cost : 0;
|
|
|
|
|
const totalCost = unitPrice * item.quantity;
|
|
|
|
|
totalAmount += totalCost;
|
|
|
|
|
|
|
|
|
|
itemDetails.push({
|
|
|
|
|
name: item.name,
|
|
|
|
|
type: item.type,
|
|
|
|
|
quantity: item.quantity,
|
|
|
|
|
unitPrice,
|
|
|
|
|
totalCost
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return reply.code(200).send({
|
|
|
|
|
items: itemDetails,
|
|
|
|
|
totalEstimatedAmount: totalAmount
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error calculating quotation:", error);
|
|
|
|
|
return reply.code(500).send({ message: "Failed to calculate quotation" });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.editQuotationForSensor = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { quatationId } = req.params; // Get the ID of the quotation to edit
|
|
|
|
|
|