GPA
This commit is contained in:
@@ -355,28 +355,53 @@ router.post('/plans', validateRequest(applicationPlanSchema), async (req, res, n
|
||||
|
||||
const plan = planResult.rows[0];
|
||||
|
||||
// Calculate shared water amount and speed for liquid applications
|
||||
const sectionArea = areaSquareFeet || parseFloat(section.area);
|
||||
const firstProduct = products[0];
|
||||
const isLiquid = firstProduct.applicationType === 'liquid';
|
||||
|
||||
let sharedWaterAmount = 0;
|
||||
let sharedTargetSpeed = 3;
|
||||
|
||||
if (isLiquid) {
|
||||
// Prepare equipment and nozzle objects for water calculation
|
||||
const equipmentForCalc = {
|
||||
categoryName: equipmentData.category_name,
|
||||
tankSizeGallons: equipmentData.tank_size_gallons,
|
||||
sprayWidthFeet: equipmentData.spray_width_feet,
|
||||
capacityLbs: equipmentData.capacity_lbs,
|
||||
spreadWidth: equipmentData.spread_width
|
||||
};
|
||||
|
||||
const nozzleForCalc = nozzleData ? {
|
||||
flowRateGpm: nozzleData.flow_rate_gpm,
|
||||
sprayAngle: nozzleData.spray_angle
|
||||
} : null;
|
||||
|
||||
// Calculate water and speed once for the entire application
|
||||
const waterCalculation = calculateApplication({
|
||||
areaSquareFeet: sectionArea,
|
||||
rateAmount: 1, // Use dummy rate for water calculation
|
||||
rateUnit: 'oz/1000 sq ft',
|
||||
applicationType: 'liquid',
|
||||
equipment: equipmentForCalc,
|
||||
nozzle: nozzleForCalc
|
||||
});
|
||||
|
||||
sharedWaterAmount = waterCalculation.waterAmountGallons || 0;
|
||||
sharedTargetSpeed = waterCalculation.applicationSpeedMph || 3;
|
||||
|
||||
console.log('Shared liquid calculation:', {
|
||||
sectionArea,
|
||||
sharedWaterAmount,
|
||||
sharedTargetSpeed,
|
||||
productsCount: products.length
|
||||
});
|
||||
}
|
||||
|
||||
// Add products to plan with calculations
|
||||
for (const product of products) {
|
||||
const { productId, userProductId, rateAmount, rateUnit, applicationType } = product;
|
||||
|
||||
// Use passed area or get from database
|
||||
const sectionArea = areaSquareFeet || parseFloat(section.area);
|
||||
|
||||
console.log('Calculation inputs:', {
|
||||
areaSquareFeet: sectionArea,
|
||||
rateAmount: parseFloat(rateAmount),
|
||||
rateUnit,
|
||||
applicationType,
|
||||
equipmentData: {
|
||||
category_name: equipmentData.category_name,
|
||||
spray_width_feet: equipmentData.spray_width_feet,
|
||||
tank_size_gallons: equipmentData.tank_size_gallons
|
||||
},
|
||||
nozzleData: nozzleData ? {
|
||||
flow_rate_gpm: nozzleData.flow_rate_gpm,
|
||||
spray_angle: nozzleData.spray_angle
|
||||
} : null
|
||||
});
|
||||
|
||||
// Prepare equipment object for calculations
|
||||
const equipmentForCalc = {
|
||||
@@ -393,7 +418,7 @@ router.post('/plans', validateRequest(applicationPlanSchema), async (req, res, n
|
||||
sprayAngle: nozzleData.spray_angle
|
||||
} : null;
|
||||
|
||||
// Perform advanced calculations using the calculation engine
|
||||
// Calculate product amount
|
||||
const calculations = calculateApplication({
|
||||
areaSquareFeet: sectionArea,
|
||||
rateAmount: parseFloat(rateAmount),
|
||||
@@ -403,7 +428,12 @@ router.post('/plans', validateRequest(applicationPlanSchema), async (req, res, n
|
||||
nozzle: nozzleForCalc
|
||||
});
|
||||
|
||||
console.log('Plan creation calculations:', calculations);
|
||||
console.log('Individual product calculation:', {
|
||||
product: productId || userProductId,
|
||||
rateAmount,
|
||||
rateUnit,
|
||||
calculatedAmount: calculations.productAmountOunces || calculations.productAmountPounds
|
||||
});
|
||||
|
||||
// Extract calculated values based on application type
|
||||
let calculatedProductAmount = 0;
|
||||
@@ -412,7 +442,9 @@ router.post('/plans', validateRequest(applicationPlanSchema), async (req, res, n
|
||||
|
||||
if (calculations.type === 'liquid') {
|
||||
calculatedProductAmount = calculations.productAmountOunces || 0;
|
||||
calculatedWaterAmount = calculations.waterAmountGallons || 0;
|
||||
// Use shared water amount for liquid applications
|
||||
calculatedWaterAmount = sharedWaterAmount;
|
||||
targetSpeed = sharedTargetSpeed;
|
||||
} else if (calculations.type === 'granular') {
|
||||
calculatedProductAmount = calculations.productAmountPounds || 0;
|
||||
calculatedWaterAmount = 0; // No water for granular
|
||||
@@ -565,11 +597,46 @@ router.put('/plans/:id', validateParams(idParamSchema), validateRequest(applicat
|
||||
// Delete existing products
|
||||
await client.query('DELETE FROM application_plan_products WHERE plan_id = $1', [planId]);
|
||||
|
||||
// Calculate shared water amount and speed for liquid applications
|
||||
const sectionArea = areaSquareFeet || parseFloat(section.area);
|
||||
const firstProduct = products[0];
|
||||
const isLiquid = firstProduct.applicationType === 'liquid';
|
||||
|
||||
let sharedWaterAmount = 0;
|
||||
let sharedTargetSpeed = 3;
|
||||
|
||||
if (isLiquid) {
|
||||
// Prepare equipment and nozzle objects for water calculation
|
||||
const equipmentForCalc = {
|
||||
categoryName: equipmentData.category_name,
|
||||
tankSizeGallons: equipmentData.tank_size_gallons,
|
||||
sprayWidthFeet: equipmentData.spray_width_feet,
|
||||
capacityLbs: equipmentData.capacity_lbs,
|
||||
spreadWidth: equipmentData.spread_width
|
||||
};
|
||||
|
||||
const nozzleForCalc = nozzleData ? {
|
||||
flowRateGpm: nozzleData.flow_rate_gpm,
|
||||
sprayAngle: nozzleData.spray_angle
|
||||
} : null;
|
||||
|
||||
// Calculate water and speed once for the entire application
|
||||
const waterCalculation = calculateApplication({
|
||||
areaSquareFeet: sectionArea,
|
||||
rateAmount: 1, // Use dummy rate for water calculation
|
||||
rateUnit: 'oz/1000 sq ft',
|
||||
applicationType: 'liquid',
|
||||
equipment: equipmentForCalc,
|
||||
nozzle: nozzleForCalc
|
||||
});
|
||||
|
||||
sharedWaterAmount = waterCalculation.waterAmountGallons || 0;
|
||||
sharedTargetSpeed = waterCalculation.applicationSpeedMph || 3;
|
||||
}
|
||||
|
||||
// Add updated products with recalculation
|
||||
for (const product of products) {
|
||||
const { productId, userProductId, rateAmount, rateUnit, applicationType } = product;
|
||||
|
||||
const sectionArea = areaSquareFeet || parseFloat(section.area);
|
||||
|
||||
// Prepare equipment object for calculations
|
||||
const equipmentForCalc = {
|
||||
@@ -603,7 +670,9 @@ router.put('/plans/:id', validateParams(idParamSchema), validateRequest(applicat
|
||||
|
||||
if (calculations.type === 'liquid') {
|
||||
calculatedProductAmount = calculations.productAmountOunces || 0;
|
||||
calculatedWaterAmount = calculations.waterAmountGallons || 0;
|
||||
// Use shared water amount for liquid applications
|
||||
calculatedWaterAmount = sharedWaterAmount;
|
||||
targetSpeed = sharedTargetSpeed;
|
||||
} else if (calculations.type === 'granular') {
|
||||
calculatedProductAmount = calculations.productAmountPounds || 0;
|
||||
calculatedWaterAmount = 0;
|
||||
|
||||
Reference in New Issue
Block a user