fix
This commit is contained in:
parent
df35fdcc50
commit
5fb960abbd
@ -527,53 +527,63 @@ if (!establishment) {
|
||||
|
||||
// Prepare products data in the required format
|
||||
const formattedProducts = products.map(product => {
|
||||
// Get the selected product option to access product_id
|
||||
const selectedProduct = productOptions.find(p => p.value === product.product);
|
||||
const existingProduct = currentSubmission.products?.find(p =>
|
||||
p.product_id === (selectedProduct?.product_id || product.product_id) ||
|
||||
p.product?.id === (selectedProduct?.product_id || product.product_id)
|
||||
// Find the existing product in the current submission's products
|
||||
const selectedProduct = productOptions.find(p => p.value === product.product);
|
||||
console.log('Product matching debug:', {
|
||||
product,
|
||||
selectedProduct,
|
||||
productOptions,
|
||||
currentSubmissionProducts: currentSubmission.products
|
||||
});
|
||||
const existingProduct = currentSubmission.products?.find(p =>
|
||||
p.product_id == product.product_id || // Loose equality to handle string/number mismatch
|
||||
p.product?.id == product.product_id // Also check nested product.id
|
||||
);
|
||||
console.log('Product matching debug:', {
|
||||
productId: product.product_id,
|
||||
productIdType: typeof product.product_id,
|
||||
existingProduct,
|
||||
currentSubmissionProducts: currentSubmission.products
|
||||
});
|
||||
|
||||
return {
|
||||
id: existingProduct?.id || 0,
|
||||
product_id: selectedProduct?.product_id || product.product_id || null,
|
||||
unit_id: product.unit?.toString() || '', // Ensure unit_id is a string
|
||||
annual_installed_capacity: product.capacity || '0',
|
||||
previous_quantity_period_one: product.octQuantity || '0',
|
||||
previous_quantity_period_two: product.novQuantity || '0',
|
||||
previous_quantity_period_three: product.decQuantity || '0',
|
||||
previous_cost_period_one: product.octCost || '0',
|
||||
previous_cost_period_two: product.novCost || '0',
|
||||
previous_cost_period_three: product.decCost || '0',
|
||||
current_quantity_period_one: product.janQuantity || '0',
|
||||
current_quantity_period_two: product.febQuantity || '0',
|
||||
current_quantity_period_three: product.marQuantity || '0',
|
||||
current_cost_period_one: product.janCost || '0',
|
||||
current_cost_period_two: product.febCost || '0',
|
||||
current_cost_period_three: product.marCost || '0',
|
||||
forecast_quantity_period_one: product.aprQuantity || '0',
|
||||
forecast_quantity_period_two: product.mayQuantity || '0',
|
||||
forecast_quantity_period_three: product.junQuantity || '0',
|
||||
forecast_cost_period_one: product.aprCost || '0',
|
||||
forecast_cost_period_two: product.mayCost || '0',
|
||||
forecast_cost_period_three: product.junCost || '0',
|
||||
// Previous quarter (Q4) - October
|
||||
previous_quantity: product.octQuantity || '0',
|
||||
previous_cost: product.octCost || '0',
|
||||
// Current quarter (Q1) - Using November as current (as per previous implementation)
|
||||
// If you want to use a different field for current, replace novQuantity/novCost with the appropriate field
|
||||
current_quantity: product.novQuantity || '0',
|
||||
current_cost: product.novCost || '0',
|
||||
// Next quarter forecast (Q2) - December
|
||||
forecast_quantity: product.decQuantity || '0',
|
||||
forecast_cost: product.decCost || '0',
|
||||
variation_reason_master_id: product.variationReason || '',
|
||||
other_variation_reason: product.otherVariationReason || '',
|
||||
zero_target_reason_master_id: product.zeroTargetReason || '',
|
||||
other_zero_target_reason: product.otherZeroTargetReason || '',
|
||||
remarks: product.remarks || remarks || '' // Use product.remarks if available, otherwise use the component's remarks state
|
||||
};
|
||||
});
|
||||
return {
|
||||
// Use the existing product ID if found, otherwise use 0 for new products
|
||||
id: existingProduct?.id || 0,
|
||||
product_id: product.product_id || null,
|
||||
unit_id: product.unit?.toString() || '',
|
||||
annual_installed_capacity: product.capacity || '0',
|
||||
// ... rest of the product fields remain the same
|
||||
previous_quantity_period_one: product.octQuantity || '0',
|
||||
previous_quantity_period_two: product.novQuantity || '0',
|
||||
previous_quantity_period_three: product.decQuantity || '0',
|
||||
previous_cost_period_one: product.octCost || '0',
|
||||
previous_cost_period_two: product.novCost || '0',
|
||||
previous_cost_period_three: product.decCost || '0',
|
||||
current_quantity_period_one: product.janQuantity || '0',
|
||||
current_quantity_period_two: product.febQuantity || '0',
|
||||
current_quantity_period_three: product.marQuantity || '0',
|
||||
current_cost_period_one: product.janCost || '0',
|
||||
current_cost_period_two: product.febCost || '0',
|
||||
current_cost_period_three: product.marCost || '0',
|
||||
forecast_quantity_period_one: product.aprQuantity || '0',
|
||||
forecast_quantity_period_two: product.mayQuantity || '0',
|
||||
forecast_quantity_period_three: product.junQuantity || '0',
|
||||
forecast_cost_period_one: product.aprCost || '0',
|
||||
forecast_cost_period_two: product.mayCost || '0',
|
||||
forecast_cost_period_three: product.junCost || '0',
|
||||
previous_quantity: product.octQuantity || '0',
|
||||
previous_cost: product.octCost || '0',
|
||||
current_quantity: product.novQuantity || '0',
|
||||
current_cost: product.novCost || '0',
|
||||
forecast_quantity: product.decQuantity || '0',
|
||||
forecast_cost: product.decCost || '0',
|
||||
variation_reason_master_id: product.variationReason || '',
|
||||
other_variation_reason: product.otherVariationReason || '',
|
||||
zero_target_reason_master_id: product.zeroTargetReason || '',
|
||||
other_zero_target_reason: product.otherZeroTargetReason || '',
|
||||
remarks: product.remarks || remarks || ''
|
||||
};
|
||||
});
|
||||
|
||||
// Prepare the complete payload with values from currentSubmission
|
||||
const payload = {
|
||||
@ -597,10 +607,14 @@ const existingProduct = currentSubmission.products?.find(p =>
|
||||
// Update existing submission
|
||||
response = await resubmitSurvey(submissionId, payload);
|
||||
setToast({
|
||||
show: true,
|
||||
message: 'Draft updated successfully',
|
||||
type: 'success'
|
||||
});
|
||||
show: true,
|
||||
message: 'Draft Updated Successfully',
|
||||
type: 'success'
|
||||
});
|
||||
setTimeout(() => {
|
||||
window.location.href = '/dashboard'; // Update this path if your dashboard route is different
|
||||
}, 1000);
|
||||
// setShowDraftSaved(true);
|
||||
} else {
|
||||
// Create new submission
|
||||
response = await submitSurvey(payload);
|
||||
@ -609,38 +623,20 @@ const existingProduct = currentSubmission.products?.find(p =>
|
||||
if (response && response.submission_data) {
|
||||
localStorage.setItem('currentSubmission', JSON.stringify(response.submission_data));
|
||||
}
|
||||
|
||||
setToast({
|
||||
show: true,
|
||||
message: 'Draft saved successfully',
|
||||
type: 'success'
|
||||
});
|
||||
show: true,
|
||||
message: 'Draft Saved Successfully',
|
||||
type: 'success'
|
||||
});
|
||||
setTimeout(() => {
|
||||
window.location.href = '/dashboard'; // Update this path if your dashboard route is different
|
||||
}, 1000);
|
||||
// setShowDraftSaved(true);
|
||||
}
|
||||
|
||||
// Redirect to dashboard after a short delay to show the success message
|
||||
setTimeout(() => {
|
||||
window.location.href = '/dashboard';
|
||||
}, 1000);
|
||||
|
||||
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error details:', {
|
||||
message: error.message,
|
||||
response: error.response?.data,
|
||||
status: error.response?.status,
|
||||
config: {
|
||||
url: error.config?.url,
|
||||
method: error.config?.method,
|
||||
data: error.config?.data
|
||||
}
|
||||
});
|
||||
|
||||
setToast({
|
||||
show: true,
|
||||
message: error.message || 'Failed to save draft',
|
||||
type: 'error'
|
||||
});
|
||||
console.error('Error saving draft:', error);
|
||||
alert(error.message || 'Failed to save draft');
|
||||
} finally {
|
||||
setIsSavingDraft(false);
|
||||
}
|
||||
@ -681,14 +677,14 @@ const existingProduct = currentSubmission.products?.find(p =>
|
||||
|
||||
return (
|
||||
<div className="space-y-5">
|
||||
{toast.show && (
|
||||
{toast.show && toast.message && (
|
||||
<CustomToast
|
||||
message={toast.message}
|
||||
type={toast.type}
|
||||
onClose={() => setToast(prev => ({ ...prev, show: false }))}
|
||||
/>
|
||||
)}
|
||||
{toast && (
|
||||
{toast.show && toast.message && (
|
||||
<div
|
||||
className={`flex items-start justify-between gap-3 rounded-md border px-4 py-3 text-sm ${
|
||||
toast.type === 'success'
|
||||
|
||||
Loading…
Reference in New Issue
Block a user