diff --git a/src/controllers/supplierOrderController.js b/src/controllers/supplierOrderController.js index 9a9cf817..472c8152 100644 --- a/src/controllers/supplierOrderController.js +++ b/src/controllers/supplierOrderController.js @@ -784,4 +784,23 @@ exports.getBillingInfo = async (req, reply) => { } catch (err) { reply.status(400).send({ message: err.message }); } +}; + + +exports.getAlldistrubanceamountsdetails = async (req, reply) => { + const limit = parseInt(req.query.limit) || 100; + const page = parseInt(req.query.page) || 1; + const startindex = (page - 1) * limit; + const customerId = req.params.customerId + try { + await Tankerbooking.find({ customerId: customerId, payment_status: "due",distrubance_status:"1"}) + .limit(limit) + .skip(startindex) + .exec() + .then((docs) => { + reply.send({ status_code: 200, data: docs, count: docs.length }); + }) + } catch (err) { + reply.status(400).send({ message: err.message }); + } }; \ No newline at end of file diff --git a/src/controllers/tankersController.js b/src/controllers/tankersController.js index 759b1631..4159a4a8 100644 --- a/src/controllers/tankersController.js +++ b/src/controllers/tankersController.js @@ -684,15 +684,15 @@ exports.distrubanceStatus = async (req, reply) => { const distrubance_price = parseInt(booking.distrubance_price .replace(/,/g, ''), 10) const price = parseInt(booking.price.replace(/,/g, ''), 10) const amount_due = parseInt(booking.amount_due .replace(/,/g, ''), 10) - const price_variation = price-distrubance_price + const price_variation = price-distrubance_price if (action === "accept") { - await Tankerbooking.findOneAndUpdate({ bookingid:bookingid }, { $set: { amount_due: price,amount_difference:"0" } }); + await Tankerbooking.findOneAndUpdate({ bookingid:bookingid }, { $set: { amount_due: distrubance_price,amount_difference:price_variation},distrubance_status:"1" }); } if (action === "reject") { - await Tankerbooking.findOneAndUpdate({ bookingid:bookingid }, { $set: { amount_due: distrubance_price,amount_difference:"0" } }); + await Tankerbooking.findOneAndUpdate({ bookingid:bookingid }, { $set: { amount_due: price,amount_difference:"0",distrubance_status:"0" } }); } diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 5dff432e..b5713a7b 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -219,22 +219,25 @@ exports.getTanklevels = async (req, reply) => { } }; - - +const intervals = {}; +let sum_oh_count = 0 +let bore_sump_count = 0 exports.motorAction = async (req, reply) => { try { //let start_time,stop_time + const customerId = req.params.customerId; const action = req.body.action const receiver_tank = req.body.to const receiver_tank_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}); const receiver_capacity = parseInt((receiver_tank_info.capacity).replace(/,/g, ''), 10) const desired_water_percentage = parseInt((req.body.percentage).replace(/,/g, ''), 10) - const intervals = {}; + const supplier_tank = req.body.from const supplier_tank_type = (req.body.from_type).toLowerCase() const receiver_type = (req.body.to_type).toLowerCase() console.log(supplier_tank) + const interval_variable = supplier_tank+receiver_tank if(action === "start"){ const start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) @@ -259,26 +262,32 @@ exports.motorAction = async (req, reply) => { await overheadTank.save(); } - + sum_oh_count++; + console.log(sum_oh_count) // await changingfrom_tankwaterlevel(customerId,initial_update,supplier_tank_info); let supplier_waterlevel = parseInt(supplier_tank_info.waterlevel.replace(/,/g, ''), 10) // console.log(supplier_waterlevel) let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) - intervals[receiver_tank] = setInterval(async function () { + intervals[interval_variable] = setInterval(async function () { // Calculate new water levels const supplier_tank_info2 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type}); - const newWaterLevel = receiver_waterlevel + 250//Math.floor(supplier_waterlevel * 0.1); + const newWaterLevel = receiver_waterlevel + (250*sum_oh_count)//Math.floor(supplier_waterlevel * 0.1); const newSupplierWaterLevel = supplier_waterlevel//Math.floor(supplier_waterlevel * 0.1); const supplier_capacity = parseInt(supplier_tank_info.capacity.replace(/,/g, ''), 10) console.log((newSupplierWaterLevel/supplier_capacity)*100) // Check if updating should stop if ((newSupplierWaterLevel/supplier_capacity)*100 <= 5 || (newWaterLevel/receiver_capacity)*100 >= 95 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === "0") { - clearInterval(intervals[receiver_tank]); // Clear the interval for this tank - delete intervals[receiver_tank]; + clearInterval(intervals[interval_variable]); // Clear the interval for this tank + delete intervals[interval_variable]; + sum_oh_count--; + if (sum_oh_count===0){ await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "0" } }); + + + } const overheadTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type }); const connection = overheadTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank); @@ -359,6 +368,7 @@ exports.motorAction = async (req, reply) => { if(supplier_tank_type==="bore" && receiver_type === "sump"){ const receiver_capacity = parseInt(receiver_tank_info.capacity.replace(/,/g, ''), 10) // console.log(receiver_capacity,"0",receiver_tank_info.tankName) + bore_sump_count++; await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "1" } }); const sumpTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type }); const connection = sumpTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank); @@ -371,19 +381,23 @@ exports.motorAction = async (req, reply) => { let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) // console.log(receiver_waterlevel,"1") - intervals[receiver_tank] = setInterval(async function () { + intervals[interval_variable] = setInterval(async function () { // Calculate new water levels const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type}); //console.log(rcvr_info) //console.log(rcvr_info.motor_status) - const newWaterLevel = receiver_waterlevel+250; + const newWaterLevel = receiver_waterlevel+(250*bore_sump_count); //console.log(newWaterLevel,"2",receiver_tank_info.tankName) // Check if updating should stop if ((newWaterLevel/receiver_capacity)*100 >= 97 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === "0" ) { + clearInterval(intervals[interval_variable]); // Clear the interval for this tank + delete intervals[interval_variable]; + bore_sump_count--; + if (bore_sump_count===0){ await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "0" } }); - clearInterval(intervals[receiver_tank]); // Clear the interval for this tank - delete intervals[receiver_tank]; + + } const sumpTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type }); const connection = sumpTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank); @@ -435,6 +449,7 @@ exports.motorAction = async (req, reply) => { } const motor_data = await motorData.save(); + console.log(motor_data) // reply.send({ status_code: 200, data: motor_data }); @@ -449,6 +464,23 @@ exports.motorAction = async (req, reply) => { else if (action === "stop") { const stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + clearInterval(intervals[interval_variable]); // Clear the interval for this tank + delete intervals[interval_variable]; + if (supplier_tank_type === "sump"){ + sum_oh_count--; + if (sum_oh_count===0){ + await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}, { $set: { motor_status: "0" } }); + + } + } + if (supplier_tank_type === "bore"){ + bore_sump_count--; + if (bore_sump_count===0){ + await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}, { $set: { motor_status: "0" } }); + + } + } + const overheadTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type }); const connection = overheadTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank); @@ -458,7 +490,8 @@ exports.motorAction = async (req, reply) => { } // console.log(stop_time) // clearInterval(intervalId); - await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}, { $set: { motor_status: "0" } }); + + // await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}, { $set: { motor_status: "0" } }); reply.send({ status_code: 200, "stop time": stop_time}); } else { diff --git a/src/models/tankers.js b/src/models/tankers.js index 017002cc..7614c549 100644 --- a/src/models/tankers.js +++ b/src/models/tankers.js @@ -71,6 +71,7 @@ const tankersbookingSchema = new mongoose.Schema({ longitude: { type : Number,default: 0.0}, latitude: {type: Number,default: 0.0}, deliveredDate: { type: String, default: null }, + distrubance_status: { type: String, default: "0" }, }); const boreSchema = new mongoose.Schema({ diff --git a/src/routes/supplierOrdersRoutes.js b/src/routes/supplierOrdersRoutes.js index 6c2a9b20..669ec2a4 100644 --- a/src/routes/supplierOrdersRoutes.js +++ b/src/routes/supplierOrdersRoutes.js @@ -712,6 +712,37 @@ module.exports = function (fastify, opts, next) { }); + fastify.route({ + method: "GET", + url: "/api/getAlldistrubanceamountsdetails/:customerId", + schema: { + tags: ["Supplier-Order"], + description:"This is to get all distrubance amount accounts", + summary: "This is to get all distrubance amount accounts", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, + + security: [ + { + basicAuth: [], + }, + ], + + }, + handler:supplierOrderController.getAlldistrubanceamountsdetails, + + }); + + + next(); }