application planning
This commit is contained in:
@@ -3,6 +3,7 @@ const pool = require('../config/database');
|
||||
const { validateRequest, validateParams } = require('../utils/validation');
|
||||
const { applicationPlanSchema, applicationLogSchema, idParamSchema } = require('../utils/validation');
|
||||
const { AppError } = require('../middleware/errorHandler');
|
||||
const { calculateApplication } = require('../utils/applicationCalculations');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -173,7 +174,17 @@ router.get('/plans/:id', validateParams(idParamSchema), async (req, res, next) =
|
||||
// @access Private
|
||||
router.post('/plans', validateRequest(applicationPlanSchema), async (req, res, next) => {
|
||||
try {
|
||||
const { lawnSectionId, equipmentId, plannedDate, notes, products } = req.body;
|
||||
const {
|
||||
lawnSectionId,
|
||||
equipmentId,
|
||||
nozzleId,
|
||||
plannedDate,
|
||||
notes,
|
||||
products,
|
||||
areaSquareFeet,
|
||||
equipment,
|
||||
nozzle
|
||||
} = req.body;
|
||||
|
||||
// Start transaction
|
||||
const client = await pool.connect();
|
||||
@@ -210,39 +221,44 @@ router.post('/plans', validateRequest(applicationPlanSchema), async (req, res, n
|
||||
|
||||
// Create application plan
|
||||
const planResult = await client.query(
|
||||
`INSERT INTO application_plans (user_id, lawn_section_id, equipment_id, planned_date, notes)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
`INSERT INTO application_plans (user_id, lawn_section_id, equipment_id, nozzle_id, planned_date, notes)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING *`,
|
||||
[req.user.id, lawnSectionId, equipmentId, plannedDate, notes]
|
||||
[req.user.id, lawnSectionId, equipmentId, nozzleId, plannedDate, notes]
|
||||
);
|
||||
|
||||
const plan = planResult.rows[0];
|
||||
|
||||
// Add products to plan with calculations
|
||||
for (const product of products) {
|
||||
const { productId, userProductId, rateAmount, rateUnit } = product;
|
||||
const { productId, userProductId, rateAmount, rateUnit, applicationType } = product;
|
||||
|
||||
// Calculate application amounts based on area and rate
|
||||
const sectionArea = parseFloat(section.area);
|
||||
// Use passed area or get from database
|
||||
const sectionArea = areaSquareFeet || parseFloat(section.area);
|
||||
|
||||
// Perform advanced calculations using the calculation engine
|
||||
const calculations = calculateApplication({
|
||||
areaSquareFeet: sectionArea,
|
||||
rateAmount: parseFloat(rateAmount),
|
||||
rateUnit,
|
||||
applicationType,
|
||||
equipment,
|
||||
nozzle
|
||||
});
|
||||
|
||||
console.log('Plan creation calculations:', calculations);
|
||||
|
||||
// Extract calculated values based on application type
|
||||
let calculatedProductAmount = 0;
|
||||
let calculatedWaterAmount = 0;
|
||||
let targetSpeed = 3; // Default 3 MPH
|
||||
let targetSpeed = calculations.applicationSpeedMph || 3;
|
||||
|
||||
// Basic calculation logic (can be enhanced based on equipment type)
|
||||
if (rateUnit.includes('1000sqft')) {
|
||||
calculatedProductAmount = rateAmount * (sectionArea / 1000);
|
||||
} else if (rateUnit.includes('acre')) {
|
||||
calculatedProductAmount = rateAmount * (sectionArea / 43560);
|
||||
} else {
|
||||
calculatedProductAmount = rateAmount;
|
||||
}
|
||||
|
||||
// Water calculation for liquid applications
|
||||
if (rateUnit.includes('gal')) {
|
||||
calculatedWaterAmount = calculatedProductAmount;
|
||||
} else if (rateUnit.includes('oz/gal')) {
|
||||
calculatedWaterAmount = sectionArea / 1000; // 1 gal per 1000 sqft default
|
||||
calculatedProductAmount = rateAmount * calculatedWaterAmount;
|
||||
if (calculations.type === 'liquid') {
|
||||
calculatedProductAmount = calculations.productAmountOunces || 0;
|
||||
calculatedWaterAmount = calculations.waterAmountGallons || 0;
|
||||
} else if (calculations.type === 'granular') {
|
||||
calculatedProductAmount = calculations.productAmountPounds || 0;
|
||||
calculatedWaterAmount = 0; // No water for granular
|
||||
}
|
||||
|
||||
await client.query(
|
||||
|
||||
Reference in New Issue
Block a user