asdfa
This commit is contained in:
@@ -398,58 +398,174 @@ const Applications = () => {
|
|||||||
onSubmit={async (planData) => {
|
onSubmit={async (planData) => {
|
||||||
try {
|
try {
|
||||||
if (editingPlan) {
|
if (editingPlan) {
|
||||||
// Edit existing plan
|
// Edit existing plan - handle multiple areas
|
||||||
const selectedArea = selectedPropertyDetails.sections.find(s => s.id === planData.selectedAreas[0]);
|
if (planData.selectedAreas.length === 1) {
|
||||||
const areaSquareFeet = selectedArea?.area || 0;
|
// Single area - update existing plan normally
|
||||||
const selectedEquipment = equipment.find(eq => eq.id === parseInt(planData.equipmentId));
|
const selectedArea = selectedPropertyDetails.sections.find(s => s.id === planData.selectedAreas[0]);
|
||||||
const selectedNozzle = planData.nozzleId ? nozzles.find(n => n.id === parseInt(planData.nozzleId)) : null;
|
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 = {
|
const planPayload = {
|
||||||
lawnSectionId: parseInt(planData.selectedAreas[0]),
|
lawnSectionId: parseInt(planData.selectedAreas[0]),
|
||||||
equipmentId: parseInt(planData.equipmentId),
|
equipmentId: parseInt(planData.equipmentId),
|
||||||
...(planData.applicationType === 'liquid' && planData.nozzleId && { nozzleId: parseInt(planData.nozzleId) }),
|
...(planData.applicationType === 'liquid' && planData.nozzleId && { nozzleId: parseInt(planData.nozzleId) }),
|
||||||
plannedDate: planData.plannedDate || new Date().toISOString().split('T')[0],
|
plannedDate: planData.plannedDate || new Date().toISOString().split('T')[0],
|
||||||
notes: planData.notes || '',
|
notes: planData.notes || '',
|
||||||
areaSquareFeet: areaSquareFeet,
|
areaSquareFeet: areaSquareFeet,
|
||||||
equipment: {
|
equipment: {
|
||||||
id: selectedEquipment?.id,
|
id: selectedEquipment?.id,
|
||||||
categoryName: selectedEquipment?.categoryName,
|
categoryName: selectedEquipment?.categoryName,
|
||||||
tankSizeGallons: selectedEquipment?.tankSizeGallons,
|
tankSizeGallons: selectedEquipment?.tankSizeGallons,
|
||||||
pumpGpm: selectedEquipment?.pumpGpm,
|
pumpGpm: selectedEquipment?.pumpGpm,
|
||||||
sprayWidthFeet: selectedEquipment?.sprayWidthFeet,
|
sprayWidthFeet: selectedEquipment?.sprayWidthFeet,
|
||||||
capacityLbs: selectedEquipment?.capacityLbs,
|
capacityLbs: selectedEquipment?.capacityLbs,
|
||||||
spreadWidth: selectedEquipment?.spreadWidth
|
spreadWidth: selectedEquipment?.spreadWidth
|
||||||
},
|
},
|
||||||
...(planData.applicationType === 'liquid' && selectedNozzle && {
|
...(planData.applicationType === 'liquid' && selectedNozzle && {
|
||||||
nozzle: {
|
nozzle: {
|
||||||
id: selectedNozzle.id,
|
id: selectedNozzle.id,
|
||||||
flowRateGpm: selectedNozzle.flowRateGpm,
|
flowRateGpm: selectedNozzle.flowRateGpm,
|
||||||
sprayAngle: selectedNozzle.sprayAngle
|
sprayAngle: selectedNozzle.sprayAngle
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
products: planData.applicationType === 'liquid'
|
products: planData.applicationType === 'liquid'
|
||||||
? planData.selectedProducts.map(item => ({
|
? planData.selectedProducts.map(item => ({
|
||||||
...(item.product?.isShared
|
...(item.product?.isShared
|
||||||
? { productId: parseInt(item.product.id) }
|
? { productId: parseInt(item.product.id) }
|
||||||
: { userProductId: parseInt(item.product.id) }
|
: { userProductId: parseInt(item.product.id) }
|
||||||
),
|
),
|
||||||
rateAmount: parseFloat(item.rateAmount || 1),
|
rateAmount: parseFloat(item.rateAmount || 1),
|
||||||
rateUnit: item.rateUnit || 'oz/1000 sq ft',
|
rateUnit: item.rateUnit || 'oz/1000 sq ft',
|
||||||
applicationType: planData.applicationType
|
applicationType: planData.applicationType
|
||||||
}))
|
}))
|
||||||
: [{
|
: [{
|
||||||
...(planData.selectedProduct?.isShared
|
...(planData.selectedProduct?.isShared
|
||||||
? { productId: parseInt(planData.selectedProduct.id) }
|
? { productId: parseInt(planData.selectedProduct.id) }
|
||||||
: { userProductId: parseInt(planData.selectedProduct.id) }
|
: { userProductId: parseInt(planData.selectedProduct.id) }
|
||||||
),
|
),
|
||||||
rateAmount: parseFloat(planData.selectedProduct?.customRateAmount || planData.selectedProduct?.rateAmount || 1),
|
rateAmount: parseFloat(planData.selectedProduct?.customRateAmount || planData.selectedProduct?.rateAmount || 1),
|
||||||
rateUnit: planData.selectedProduct?.customRateUnit || planData.selectedProduct?.rateUnit || 'per 1000sqft',
|
rateUnit: planData.selectedProduct?.customRateUnit || planData.selectedProduct?.rateUnit || 'per 1000sqft',
|
||||||
applicationType: planData.applicationType
|
applicationType: planData.applicationType
|
||||||
}]
|
}]
|
||||||
};
|
};
|
||||||
|
|
||||||
await applicationsAPI.updatePlan(editingPlan.id, planPayload);
|
await applicationsAPI.updatePlan(editingPlan.id, planPayload);
|
||||||
toast.success('Application plan updated successfully');
|
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 {
|
} else {
|
||||||
// Create new plan(s)
|
// Create new plan(s)
|
||||||
const planPromises = planData.selectedAreas.map(async (areaId) => {
|
const planPromises = planData.selectedAreas.map(async (areaId) => {
|
||||||
@@ -604,7 +720,7 @@ const ApplicationPlanModal = ({
|
|||||||
|
|
||||||
setPlanData({
|
setPlanData({
|
||||||
propertyId: propertyId?.toString() || '',
|
propertyId: propertyId?.toString() || '',
|
||||||
selectedAreas: [editingPlan.section?.id],
|
selectedAreas: [editingPlan.section?.id].filter(Boolean), // Allow adding more areas
|
||||||
productId: selectedProduct?.uniqueId || '',
|
productId: selectedProduct?.uniqueId || '',
|
||||||
selectedProduct: selectedProduct,
|
selectedProduct: selectedProduct,
|
||||||
selectedProducts: [],
|
selectedProducts: [],
|
||||||
@@ -633,7 +749,7 @@ const ApplicationPlanModal = ({
|
|||||||
|
|
||||||
setPlanData({
|
setPlanData({
|
||||||
propertyId: propertyId?.toString() || '',
|
propertyId: propertyId?.toString() || '',
|
||||||
selectedAreas: [editingPlan.section?.id],
|
selectedAreas: [editingPlan.section?.id].filter(Boolean), // Allow adding more areas
|
||||||
productId: '',
|
productId: '',
|
||||||
selectedProduct: null,
|
selectedProduct: null,
|
||||||
selectedProducts: selectedProducts,
|
selectedProducts: selectedProducts,
|
||||||
|
|||||||
Reference in New Issue
Block a user