changes in consumption

master^2
Varun 9 months ago
parent f29eafb1bd
commit 45c5917ae9

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

Loading…
Cancel
Save