asdfa
This commit is contained in:
@@ -398,58 +398,174 @@ const Applications = () => {
|
||||
onSubmit={async (planData) => {
|
||||
try {
|
||||
if (editingPlan) {
|
||||
// Edit existing plan
|
||||
const selectedArea = selectedPropertyDetails.sections.find(s => s.id === planData.selectedAreas[0]);
|
||||
const areaSquareFeet = selectedArea?.area || 0;
|
||||
const selectedEquipment = equipment.find(eq => eq.id === parseInt(planData.equipmentId));
|
||||
const selectedNozzle = planData.nozzleId ? nozzles.find(n => n.id === parseInt(planData.nozzleId)) : null;
|
||||
// Edit existing plan - handle multiple areas
|
||||
if (planData.selectedAreas.length === 1) {
|
||||
// Single area - update existing plan normally
|
||||
const selectedArea = selectedPropertyDetails.sections.find(s => s.id === planData.selectedAreas[0]);
|
||||
const areaSquareFeet = selectedArea?.area || 0;
|
||||
const selectedEquipment = equipment.find(eq => eq.id === parseInt(planData.equipmentId));
|
||||
const selectedNozzle = planData.nozzleId ? nozzles.find(n => n.id === parseInt(planData.nozzleId)) : null;
|
||||
|
||||
const planPayload = {
|
||||
lawnSectionId: parseInt(planData.selectedAreas[0]),
|
||||
equipmentId: parseInt(planData.equipmentId),
|
||||
...(planData.applicationType === 'liquid' && planData.nozzleId && { nozzleId: parseInt(planData.nozzleId) }),
|
||||
plannedDate: planData.plannedDate || new Date().toISOString().split('T')[0],
|
||||
notes: planData.notes || '',
|
||||
areaSquareFeet: areaSquareFeet,
|
||||
equipment: {
|
||||
id: selectedEquipment?.id,
|
||||
categoryName: selectedEquipment?.categoryName,
|
||||
tankSizeGallons: selectedEquipment?.tankSizeGallons,
|
||||
pumpGpm: selectedEquipment?.pumpGpm,
|
||||
sprayWidthFeet: selectedEquipment?.sprayWidthFeet,
|
||||
capacityLbs: selectedEquipment?.capacityLbs,
|
||||
spreadWidth: selectedEquipment?.spreadWidth
|
||||
},
|
||||
...(planData.applicationType === 'liquid' && selectedNozzle && {
|
||||
nozzle: {
|
||||
id: selectedNozzle.id,
|
||||
flowRateGpm: selectedNozzle.flowRateGpm,
|
||||
sprayAngle: selectedNozzle.sprayAngle
|
||||
}
|
||||
}),
|
||||
products: planData.applicationType === 'liquid'
|
||||
? planData.selectedProducts.map(item => ({
|
||||
...(item.product?.isShared
|
||||
? { productId: parseInt(item.product.id) }
|
||||
: { userProductId: parseInt(item.product.id) }
|
||||
),
|
||||
rateAmount: parseFloat(item.rateAmount || 1),
|
||||
rateUnit: item.rateUnit || 'oz/1000 sq ft',
|
||||
applicationType: planData.applicationType
|
||||
}))
|
||||
: [{
|
||||
...(planData.selectedProduct?.isShared
|
||||
? { productId: parseInt(planData.selectedProduct.id) }
|
||||
: { userProductId: parseInt(planData.selectedProduct.id) }
|
||||
),
|
||||
rateAmount: parseFloat(planData.selectedProduct?.customRateAmount || planData.selectedProduct?.rateAmount || 1),
|
||||
rateUnit: planData.selectedProduct?.customRateUnit || planData.selectedProduct?.rateUnit || 'per 1000sqft',
|
||||
applicationType: planData.applicationType
|
||||
}]
|
||||
};
|
||||
|
||||
await applicationsAPI.updatePlan(editingPlan.id, planPayload);
|
||||
toast.success('Application plan updated successfully');
|
||||
const planPayload = {
|
||||
lawnSectionId: parseInt(planData.selectedAreas[0]),
|
||||
equipmentId: parseInt(planData.equipmentId),
|
||||
...(planData.applicationType === 'liquid' && planData.nozzleId && { nozzleId: parseInt(planData.nozzleId) }),
|
||||
plannedDate: planData.plannedDate || new Date().toISOString().split('T')[0],
|
||||
notes: planData.notes || '',
|
||||
areaSquareFeet: areaSquareFeet,
|
||||
equipment: {
|
||||
id: selectedEquipment?.id,
|
||||
categoryName: selectedEquipment?.categoryName,
|
||||
tankSizeGallons: selectedEquipment?.tankSizeGallons,
|
||||
pumpGpm: selectedEquipment?.pumpGpm,
|
||||
sprayWidthFeet: selectedEquipment?.sprayWidthFeet,
|
||||
capacityLbs: selectedEquipment?.capacityLbs,
|
||||
spreadWidth: selectedEquipment?.spreadWidth
|
||||
},
|
||||
...(planData.applicationType === 'liquid' && selectedNozzle && {
|
||||
nozzle: {
|
||||
id: selectedNozzle.id,
|
||||
flowRateGpm: selectedNozzle.flowRateGpm,
|
||||
sprayAngle: selectedNozzle.sprayAngle
|
||||
}
|
||||
}),
|
||||
products: planData.applicationType === 'liquid'
|
||||
? planData.selectedProducts.map(item => ({
|
||||
...(item.product?.isShared
|
||||
? { productId: parseInt(item.product.id) }
|
||||
: { userProductId: parseInt(item.product.id) }
|
||||
),
|
||||
rateAmount: parseFloat(item.rateAmount || 1),
|
||||
rateUnit: item.rateUnit || 'oz/1000 sq ft',
|
||||
applicationType: planData.applicationType
|
||||
}))
|
||||
: [{
|
||||
...(planData.selectedProduct?.isShared
|
||||
? { productId: parseInt(planData.selectedProduct.id) }
|
||||
: { userProductId: parseInt(planData.selectedProduct.id) }
|
||||
),
|
||||
rateAmount: parseFloat(planData.selectedProduct?.customRateAmount || planData.selectedProduct?.rateAmount || 1),
|
||||
rateUnit: planData.selectedProduct?.customRateUnit || planData.selectedProduct?.rateUnit || 'per 1000sqft',
|
||||
applicationType: planData.applicationType
|
||||
}]
|
||||
};
|
||||
|
||||
await applicationsAPI.updatePlan(editingPlan.id, planPayload);
|
||||
toast.success('Application plan updated successfully');
|
||||
} else {
|
||||
// Multiple areas - update existing plan for first area, create new plans for additional areas
|
||||
const selectedEquipment = equipment.find(eq => eq.id === parseInt(planData.equipmentId));
|
||||
const selectedNozzle = planData.nozzleId ? nozzles.find(n => n.id === parseInt(planData.nozzleId)) : null;
|
||||
|
||||
// Calculate total area for all selected areas (for quantity calculations)
|
||||
const totalAreaSquareFeet = planData.selectedAreas.reduce((total, areaId) => {
|
||||
const area = selectedPropertyDetails.sections.find(s => s.id === areaId);
|
||||
return total + (area?.area || 0);
|
||||
}, 0);
|
||||
|
||||
// Update existing plan with first selected area but use total area for calculations
|
||||
const updatePayload = {
|
||||
lawnSectionId: parseInt(planData.selectedAreas[0]),
|
||||
equipmentId: parseInt(planData.equipmentId),
|
||||
...(planData.applicationType === 'liquid' && planData.nozzleId && { nozzleId: parseInt(planData.nozzleId) }),
|
||||
plannedDate: planData.plannedDate || new Date().toISOString().split('T')[0],
|
||||
notes: planData.notes || '',
|
||||
areaSquareFeet: totalAreaSquareFeet, // Use total area for proper quantity calculation
|
||||
equipment: {
|
||||
id: selectedEquipment?.id,
|
||||
categoryName: selectedEquipment?.categoryName,
|
||||
tankSizeGallons: selectedEquipment?.tankSizeGallons,
|
||||
pumpGpm: selectedEquipment?.pumpGpm,
|
||||
sprayWidthFeet: selectedEquipment?.sprayWidthFeet,
|
||||
capacityLbs: selectedEquipment?.capacityLbs,
|
||||
spreadWidth: selectedEquipment?.spreadWidth
|
||||
},
|
||||
...(planData.applicationType === 'liquid' && selectedNozzle && {
|
||||
nozzle: {
|
||||
id: selectedNozzle.id,
|
||||
flowRateGpm: selectedNozzle.flowRateGpm,
|
||||
sprayAngle: selectedNozzle.sprayAngle
|
||||
}
|
||||
}),
|
||||
products: planData.applicationType === 'liquid'
|
||||
? planData.selectedProducts.map(item => ({
|
||||
...(item.product?.isShared
|
||||
? { productId: parseInt(item.product.id) }
|
||||
: { userProductId: parseInt(item.product.id) }
|
||||
),
|
||||
rateAmount: parseFloat(item.rateAmount || 1),
|
||||
rateUnit: item.rateUnit || 'oz/1000 sq ft',
|
||||
applicationType: planData.applicationType
|
||||
}))
|
||||
: [{
|
||||
...(planData.selectedProduct?.isShared
|
||||
? { productId: parseInt(planData.selectedProduct.id) }
|
||||
: { userProductId: parseInt(planData.selectedProduct.id) }
|
||||
),
|
||||
rateAmount: parseFloat(planData.selectedProduct?.customRateAmount || planData.selectedProduct?.rateAmount || 1),
|
||||
rateUnit: planData.selectedProduct?.customRateUnit || planData.selectedProduct?.rateUnit || 'per 1000sqft',
|
||||
applicationType: planData.applicationType
|
||||
}]
|
||||
};
|
||||
|
||||
await applicationsAPI.updatePlan(editingPlan.id, updatePayload);
|
||||
|
||||
// Create new plans for additional areas
|
||||
const additionalAreas = planData.selectedAreas.slice(1);
|
||||
const additionalPlanPromises = additionalAreas.map(async (areaId) => {
|
||||
const selectedArea = selectedPropertyDetails.sections.find(s => s.id === areaId);
|
||||
const areaSquareFeet = selectedArea?.area || 0;
|
||||
|
||||
const planPayload = {
|
||||
lawnSectionId: parseInt(areaId),
|
||||
equipmentId: parseInt(planData.equipmentId),
|
||||
...(planData.applicationType === 'liquid' && planData.nozzleId && { nozzleId: parseInt(planData.nozzleId) }),
|
||||
plannedDate: planData.plannedDate || new Date().toISOString().split('T')[0],
|
||||
notes: planData.notes || '',
|
||||
areaSquareFeet: areaSquareFeet, // Individual area for this plan
|
||||
equipment: {
|
||||
id: selectedEquipment?.id,
|
||||
categoryName: selectedEquipment?.categoryName,
|
||||
tankSizeGallons: selectedEquipment?.tankSizeGallons,
|
||||
pumpGpm: selectedEquipment?.pumpGpm,
|
||||
sprayWidthFeet: selectedEquipment?.sprayWidthFeet,
|
||||
capacityLbs: selectedEquipment?.capacityLbs,
|
||||
spreadWidth: selectedEquipment?.spreadWidth
|
||||
},
|
||||
...(planData.applicationType === 'liquid' && selectedNozzle && {
|
||||
nozzle: {
|
||||
id: selectedNozzle.id,
|
||||
flowRateGpm: selectedNozzle.flowRateGpm,
|
||||
sprayAngle: selectedNozzle.sprayAngle
|
||||
}
|
||||
}),
|
||||
products: planData.applicationType === 'liquid'
|
||||
? planData.selectedProducts.map(item => ({
|
||||
...(item.product?.isShared
|
||||
? { productId: parseInt(item.product.id) }
|
||||
: { userProductId: parseInt(item.product.id) }
|
||||
),
|
||||
rateAmount: parseFloat(item.rateAmount || 1),
|
||||
rateUnit: item.rateUnit || 'oz/1000 sq ft',
|
||||
applicationType: planData.applicationType
|
||||
}))
|
||||
: [{
|
||||
...(planData.selectedProduct?.isShared
|
||||
? { productId: parseInt(planData.selectedProduct.id) }
|
||||
: { userProductId: parseInt(planData.selectedProduct.id) }
|
||||
),
|
||||
rateAmount: parseFloat(planData.selectedProduct?.customRateAmount || planData.selectedProduct?.rateAmount || 1),
|
||||
rateUnit: planData.selectedProduct?.customRateUnit || planData.selectedProduct?.rateUnit || 'per 1000sqft',
|
||||
applicationType: planData.applicationType
|
||||
}]
|
||||
};
|
||||
|
||||
return applicationsAPI.createPlan(planPayload);
|
||||
});
|
||||
|
||||
await Promise.all(additionalPlanPromises);
|
||||
toast.success(`Application plan updated and ${additionalAreas.length} additional plan(s) created for new areas`);
|
||||
}
|
||||
} else {
|
||||
// Create new plan(s)
|
||||
const planPromises = planData.selectedAreas.map(async (areaId) => {
|
||||
@@ -604,7 +720,7 @@ const ApplicationPlanModal = ({
|
||||
|
||||
setPlanData({
|
||||
propertyId: propertyId?.toString() || '',
|
||||
selectedAreas: [editingPlan.section?.id],
|
||||
selectedAreas: [editingPlan.section?.id].filter(Boolean), // Allow adding more areas
|
||||
productId: selectedProduct?.uniqueId || '',
|
||||
selectedProduct: selectedProduct,
|
||||
selectedProducts: [],
|
||||
@@ -633,7 +749,7 @@ const ApplicationPlanModal = ({
|
||||
|
||||
setPlanData({
|
||||
propertyId: propertyId?.toString() || '',
|
||||
selectedAreas: [editingPlan.section?.id],
|
||||
selectedAreas: [editingPlan.section?.id].filter(Boolean), // Allow adding more areas
|
||||
productId: '',
|
||||
selectedProduct: null,
|
||||
selectedProducts: selectedProducts,
|
||||
|
||||
Reference in New Issue
Block a user