diff --git a/frontend/src/pages/Applications/Applications.js b/frontend/src/pages/Applications/Applications.js index aa365f9..0adba96 100644 --- a/frontend/src/pages/Applications/Applications.js +++ b/frontend/src/pages/Applications/Applications.js @@ -139,6 +139,104 @@ const Applications = () => { ); } + // Load spreader recommendations when granular product and spreader are selected + const loadSpreaderRecommendation = async (product, equipmentId, selectedAreas) => { + if (!product || !equipmentId || !selectedAreas.length || product.productType !== 'granular') { + setSpreaderRecommendation(null); + return; + } + + setLoadingRecommendation(true); + try { + // Find the selected equipment details + const selectedEquipment = equipment.find(eq => eq.id === parseInt(equipmentId)); + if (!selectedEquipment) { + setSpreaderRecommendation(null); + return; + } + + // Load spreader settings for this product + const productApiId = product.isShared ? product.id : product.id; // Use the actual product ID + const endpoint = product.isShared + ? `/api/product-spreader-settings/product/${productApiId}` + : `/api/product-spreader-settings/user-product/${productApiId}`; + + const response = await fetch(endpoint, { + headers: { + 'Authorization': `Bearer ${localStorage.getItem('authToken')}` + } + }); + + if (!response.ok) { + setSpreaderRecommendation(null); + return; + } + + const data = await response.json(); + const settings = data.data?.settings || []; + + // Find a matching setting for this equipment + let matchingSetting = null; + + // First try to find exact equipment match + matchingSetting = settings.find(setting => + setting.equipmentId === selectedEquipment.id + ); + + // If no exact match, try to find by equipment brand/manufacturer + if (!matchingSetting && selectedEquipment.manufacturer) { + matchingSetting = settings.find(setting => + setting.spreaderBrand && + setting.spreaderBrand.toLowerCase().includes(selectedEquipment.manufacturer.toLowerCase()) + ); + } + + // If still no match, use any available setting as fallback + if (!matchingSetting && settings.length > 0) { + matchingSetting = settings[0]; + } + + if (matchingSetting) { + // Calculate total area and product amount needed + const totalArea = selectedAreas.reduce((sum, areaId) => { + const area = selectedPropertyDetails?.sections?.find(s => s.id === areaId); + return sum + (area?.area || 0); + }, 0); + + // Calculate product amount based on rate + const rateAmount = product.customRateAmount || product.rateAmount || 1; + const rateUnit = product.customRateUnit || product.rateUnit || 'lbs/1000 sq ft'; + + let productAmountLbs = 0; + if (rateUnit.includes('1000')) { + // Rate per 1000 sq ft + productAmountLbs = (rateAmount * totalArea) / 1000; + } else if (rateUnit.includes('acre')) { + // Rate per acre (43,560 sq ft) + productAmountLbs = (rateAmount * totalArea) / 43560; + } else { + // Assume rate per sq ft + productAmountLbs = rateAmount * totalArea; + } + + setSpreaderRecommendation({ + setting: matchingSetting, + equipment: selectedEquipment, + totalArea, + productAmountLbs: Math.round(productAmountLbs * 100) / 100, // Round to 2 decimal places + isExactMatch: settings.some(s => s.equipmentId === selectedEquipment.id) + }); + } else { + setSpreaderRecommendation(null); + } + } catch (error) { + console.error('Failed to load spreader recommendation:', error); + setSpreaderRecommendation(null); + } finally { + setLoadingRecommendation(false); + } + }; + return (