diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index eb6896ef..361312e1 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -1485,21 +1485,47 @@ exports.createquotationforSensor = async (req, reply) => { text: item.text || "", })); + // Fetch pricing data from Iotprice database + const getPrice = async (name, type) => { + const priceData = await Iotprice.findOne({ name, type }); + return priceData ? priceData.cost : 0; // Default to 0 if not found + }; + + // Calculate price for masters, slaves, sensors, motor switches + const masterPrice = await getPrice("master", "master"); + const slavePrice = await getPrice("slave", "slave"); + const sensorPrice = await getPrice("sensor", "sensor"); + const motorSwitchPrice = await getPrice("motor_switch", "motor_switche"); + + // Calculate price for electricals + let electricalPrice = 0; + for (const item of formattedElectricals) { + if (item.type === "cable") { + electricalPrice += await getPrice("cable", item.wire); // wire field is type + } else if (item.type === "switch") { + electricalPrice += await getPrice("switch", item.switch); // switch field is type + } + } + + // Calculate total estimation price + const totalEstimatedPrice = masterPrice + slavePrice + sensorPrice + motorSwitchPrice + electricalPrice; + // Format current date and time in IST const formattedDateTime = dayjs().tz("Asia/Kolkata").format('DD-MMM-YYYY - HH:mm'); // Create a new SensorQuotation document const newQuotation = new SensorQuotation({ quatationId, - customerId: customerId, - installationId: installationId, + customerId, + installationId, quote_status: "sentfrominstaller", masters, slaves, sensors, motor_switches, electricals: formattedElectricals, - datetime: formattedDateTime, // Add the formatted IST date and time + datetime: formattedDateTime, + estimated_price: totalEstimatedPrice, // Store estimated price }); const savedQuotation = await newQuotation.save(); @@ -1520,6 +1546,7 @@ exports.createquotationforSensor = async (req, reply) => { }; + exports.editQuotationForSensor = async (req, reply) => { try { const { quatationId } = req.params; // Get the ID of the quotation to edit