This commit is contained in:
Jake Kasper
2025-08-25 14:22:48 -04:00
parent 1bd5a76cd8
commit b4a8d1980a

View File

@@ -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 ( return (
<div className="p-6"> <div className="p-6">
<div className="flex justify-between items-center mb-6"> <div className="flex justify-between items-center mb-6">
@@ -374,104 +472,6 @@ const Applications = () => {
)} )}
</div> </div>
); );
// 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);
}
};
}; };
// Application Planning Modal Component // Application Planning Modal Component