diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 7b02b3f1..ec736822 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1056,45 +1056,38 @@ exports.consumption = async (request, reply) => { const { customerId } = request.params; const { startDate, stopDate, block } = request.body; let { typeofwater } = request.body; - - + // Convert typeofwater to lowercase typeofwater = typeofwater.toLowerCase(); const start = moment(startDate, "DD-MMM-YYYY - HH:mm").toDate(); const end = moment(stopDate, "DD-MMM-YYYY - HH:mm").toDate(); - // Construct the query object based on block and typeofwater inputs + // Construct the query object const tankQuery = { customerId, tankLocation: "overhead" }; - - if (block !== "All") { - tankQuery.blockName = block; // Filter by specific block if not "all" - } - - if (typeofwater !== "all") { - tankQuery.typeOfWater = typeofwater; // Filter by specific type of water if not "all" - } + if (block !== "All") tankQuery.blockName = block; + if (typeofwater !== "all") tankQuery.typeOfWater = typeofwater; const tanks = await Tank.find(tankQuery); const tankData = []; const tankconsumptionData = []; - // Variable to track total consumption for the selected block and typeofwater - let totalConsumptionForSelectedBlockAndTypeOfWater = 0; - let totalCapacityForSelectedBlockAndTypeOfWater=0; + let totalBoreConsumptionForSelectedBlockAndTypeOfWater = 0; + let totalBoreCapacityForSelectedBlockAndTypeOfWater = 0; + let totalDrinkingConsumptionForSelectedBlockAndTypeOfWater = 0; + let totalDrinkingCapacityForSelectedBlockAndTypeOfWater = 0; for (const tank of tanks) { const waterlevel_at_midnight = parseInt(tank.waterlevel_at_midnight.replace(/,/g, ''), 10); const total_water_added_from_midnight = parseInt(tank.total_water_added_from_midnight.replace(/,/g, ''), 10); const waterlevel = parseInt(tank.waterlevel.replace(/,/g, ''), 10); const tankname = tank.tankName; - const capacity = parseInt(tank.capacity.replace(/,/g, ''), 10); + const capacity = parseInt(tank.capacity.replace(/,/g, ''), 10); const tankConsumptions = await TankConsumptionOriginalSchema.find({ customerId, tankName: tank.tankName, tankLocation: tank.tankLocation, - - ...(block !== "All" && { block: tank.blockName }), // Ensure correct field names - ...(typeofwater !== "all" && { typeofwater: tank.typeOfWater }) // Ensure correct field names + ...(block !== "All" && { block: tank.blockName }), + ...(typeofwater !== "all" && { typeofwater: tank.typeOfWater }), }); const filteredConsumptions = tankConsumptions.filter((record) => { @@ -1105,36 +1098,62 @@ exports.consumption = async (request, reply) => { const total_consumption_from_records = filteredConsumptions.reduce((acc, record) => { return acc + parseInt(record.consumption, 10); }, 0); - tankconsumptionData.push({ - tankname, - consumptionRecordsdatewise: filteredConsumptions - }) const consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records; - // Add to the total consumption for the selected block and typeofwater - totalConsumptionForSelectedBlockAndTypeOfWater += consumption; - totalCapacityForSelectedBlockAndTypeOfWater += capacity; + // Add to the total consumption and capacities based on water type + if (tank.typeOfWater === "bore") { + totalBoreConsumptionForSelectedBlockAndTypeOfWater += consumption; + totalBoreCapacityForSelectedBlockAndTypeOfWater += capacity; + } + if (tank.typeOfWater === "drinking") { + totalDrinkingConsumptionForSelectedBlockAndTypeOfWater += consumption; + totalDrinkingCapacityForSelectedBlockAndTypeOfWater += capacity; + } tankData.push({ tankname, - totalConsumption: consumption, block: tank.blockName, TypeofWater: tank.typeOfWater, location: tank.tankLocation, capacity: tank.capacity, - waterlevel: tank.waterlevel + waterlevel: tank.waterlevel, + }); + + tankconsumptionData.push({ + tankname, + consumptionRecordsdatewise: filteredConsumptions, }); } - const totalConsumptionPercentage = ((totalConsumptionForSelectedBlockAndTypeOfWater / totalCapacityForSelectedBlockAndTypeOfWater) * 100).toFixed(2) ; + + // Calculate total consumption percentage + const boreConsumptionPercentage = totalBoreCapacityForSelectedBlockAndTypeOfWater + ? ((totalBoreConsumptionForSelectedBlockAndTypeOfWater / totalBoreCapacityForSelectedBlockAndTypeOfWater) * 100).toFixed(2) + : 0; + + const drinkingConsumptionPercentage = totalDrinkingCapacityForSelectedBlockAndTypeOfWater + ? ((totalDrinkingConsumptionForSelectedBlockAndTypeOfWater / totalDrinkingCapacityForSelectedBlockAndTypeOfWater) * 100).toFixed(2) + : 0; + + const totalConsumptionPercentage = + typeofwater === "bore" + ? boreConsumptionPercentage + : typeofwater === "drinking" + ? drinkingConsumptionPercentage + : 0; // Include the total consumption in the response const response = { status_code: 200, - tankData,consumptiorecordsdatewise:tankconsumptionData, - totalConsumptionPercentage:totalConsumptionPercentage, - [`total consumption of ${typeofwater} and selected block`]: totalConsumptionForSelectedBlockAndTypeOfWater + tankData, + consumptiorecordsdatewise: tankconsumptionData, + totalConsumptionPercentage, + [`total consumption of ${typeofwater} and selected block`]: typeofwater === "bore" + ? totalBoreConsumptionForSelectedBlockAndTypeOfWater + : typeofwater === "drinking" + ? totalDrinkingConsumptionForSelectedBlockAndTypeOfWater + : totalBoreConsumptionForSelectedBlockAndTypeOfWater + totalDrinkingConsumptionForSelectedBlockAndTypeOfWater, }; reply.send(response); @@ -1143,6 +1162,7 @@ exports.consumption = async (request, reply) => { } }; + exports.consumptiondatewiseofalltanks = async (request, reply) => { try { const { customerId } = request.params;