From 11392873c326ac95b2f48c7f95ef99a745007b28 Mon Sep 17 00:00:00 2001 From: Malini Date: Fri, 13 Feb 2026 12:12:34 +0530 Subject: [PATCH] CR Fixed for mand for varience --- .../survey/ProductData/ProductData.jsx | 178 +++++++++++++++++- .../ManufacturingIndex/ManufacturingIndex.jsx | 7 +- 2 files changed, 182 insertions(+), 3 deletions(-) diff --git a/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx b/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx index e42dcf5..d0f1673 100644 --- a/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx +++ b/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx @@ -1170,6 +1170,53 @@ useEffect(() => { }); }; + // Calculate percentage variation between previous and current quarter costs + const calculateVariationPercentage = (product) => { + // Previous quarter costs (Q4): October, November, December + const previousCosts = [ + parseFloat(product.octCost) || 0, + parseFloat(product.novCost) || 0, + parseFloat(product.decCost) || 0 + ]; + + // Current quarter costs (Q1): January, February, March + const currentCosts = [ + parseFloat(product.janCost) || 0, + parseFloat(product.febCost) || 0, + parseFloat(product.marCost) || 0 + ]; + + const totalPreviousCost = previousCosts.reduce((sum, cost) => sum + cost, 0); + const totalCurrentCost = currentCosts.reduce((sum, cost) => sum + cost, 0); + + // If previous cost is 0, return 0 to avoid division by zero + if (totalPreviousCost === 0) { + console.log('🔍 Variation Calculation: Previous cost is 0, returning 0% variation'); + return 0; + } + + // Calculate percentage variation + const variation = ((totalCurrentCost - totalPreviousCost) / totalPreviousCost) * 100; + + console.log('🔍 Variation Calculation Details:', { + productId: product.id || 'unknown', + previousCosts, + currentCosts, + totalPreviousCost, + totalCurrentCost, + variation: variation.toFixed(2) + '%', + isMandatory: Math.abs(variation) > 10 + }); + + return variation; + }; + + // Check if variation reason should be mandatory based on variation percentage + const isVariationReasonMandatory = (product) => { + const variation = calculateVariationPercentage(product); + return Math.abs(variation) > 10; // Greater than +10% or less than -10% + }; + // Simple validation function const validateField = (fieldName, value, productIndex) => { const errors = { ...formErrors }; @@ -1244,6 +1291,28 @@ useEffect(() => { isValid = false; } }); + + // Dynamic validation for variation reason based on cost variation + const mandatoryVariationReason = isVariationReasonMandatory(product); + if (mandatoryVariationReason) { + // Check if variation reason is selected + if (!product.variationReason || product.variationReason === '0' || product.variationReason === '') { + errors[`variationReason_${index}`] = 'Reason for variation is required when cost variation exceeds ±10%'; + isValid = false; + } + + // If "Other" is selected, validate the other variation reason field + const selectedReason = variationReasons.find(r => r.value === product.variationReason); + const isOtherSelected = selectedReason && ( + (selectedReason.name && selectedReason.name.toLowerCase().includes('other')) || + (selectedReason.label && selectedReason.label.toLowerCase().includes('other')) + ); + + if (isOtherSelected && (!product.otherVariationReason || product.otherVariationReason.trim() === '')) { + errors[`otherVariationReason_${index}`] = 'Please specify the reason for variation'; + isValid = false; + } + } }); setFormErrors(errors); @@ -1636,6 +1705,102 @@ useEffect(() => { delete newErrors[errorKey]; setFormErrors(newErrors); } + + // Real-time validation for variation reason when cost fields change + const costFields = ['octCost', 'novCost', 'decCost', 'janCost', 'febCost', 'marCost']; + if (costFields.includes(field)) { + const updatedProduct = updatedProducts.find(p => p.id === id); + if (updatedProduct) { + const mandatoryVariationReason = isVariationReasonMandatory(updatedProduct); + const variationErrorKey = `variationReason_${productIndex}`; + const otherVariationErrorKey = `otherVariationReason_${productIndex}`; + + const newErrors = { ...formErrors }; + + if (mandatoryVariationReason) { + // Check if variation reason is selected + if (!updatedProduct.variationReason || updatedProduct.variationReason === '0' || updatedProduct.variationReason === '') { + newErrors[variationErrorKey] = 'Reason for variation is required when cost variation exceeds ±10%'; + } else { + delete newErrors[variationErrorKey]; + + // If "Other" is selected, validate the other variation reason field + const selectedReason = variationReasons.find(r => r.value === updatedProduct.variationReason); + const isOtherSelected = selectedReason && ( + (selectedReason.name && selectedReason.name.toLowerCase().includes('other')) || + (selectedReason.label && selectedReason.label.toLowerCase().includes('other')) + ); + + if (isOtherSelected && (!updatedProduct.otherVariationReason || updatedProduct.otherVariationReason.trim() === '')) { + newErrors[otherVariationErrorKey] = 'Please specify the reason for variation'; + } else { + delete newErrors[otherVariationErrorKey]; + } + } + } else { + // Remove variation reason errors if variation is within ±10% + delete newErrors[variationErrorKey]; + delete newErrors[otherVariationErrorKey]; + } + + setFormErrors(newErrors); + } + } + + // Real-time validation when variation reason field changes + if (field === 'variationReason' || field === 'otherVariationReason') { + const updatedProduct = updatedProducts.find(p => p.id === id); + if (updatedProduct) { + const mandatoryVariationReason = isVariationReasonMandatory(updatedProduct); + const variationErrorKey = `variationReason_${productIndex}`; + const otherVariationErrorKey = `otherVariationReason_${productIndex}`; + + const newErrors = { ...formErrors }; + + if (mandatoryVariationReason) { + if (field === 'variationReason') { + // Check if variation reason is selected + if (!updatedProduct.variationReason || updatedProduct.variationReason === '0' || updatedProduct.variationReason === '') { + newErrors[variationErrorKey] = 'Reason for variation is required when cost variation exceeds ±10%'; + } else { + delete newErrors[variationErrorKey]; + + // If "Other" is selected, validate the other variation reason field + const selectedReason = variationReasons.find(r => r.value === updatedProduct.variationReason); + const isOtherSelected = selectedReason && ( + (selectedReason.name && selectedReason.name.toLowerCase().includes('other')) || + (selectedReason.label && selectedReason.label.toLowerCase().includes('other')) + ); + + if (isOtherSelected && (!updatedProduct.otherVariationReason || updatedProduct.otherVariationReason.trim() === '')) { + newErrors[otherVariationErrorKey] = 'Please specify the reason for variation'; + } else { + delete newErrors[otherVariationErrorKey]; + } + } + } else if (field === 'otherVariationReason') { + // Validate other variation reason if "Other" is selected + const selectedReason = variationReasons.find(r => r.value === updatedProduct.variationReason); + const isOtherSelected = selectedReason && ( + (selectedReason.name && selectedReason.name.toLowerCase().includes('other')) || + (selectedReason.label && selectedReason.label.toLowerCase().includes('other')) + ); + + if (isOtherSelected && (!updatedProduct.otherVariationReason || updatedProduct.otherVariationReason.trim() === '')) { + newErrors[otherVariationErrorKey] = 'Please specify the reason for variation'; + } else { + delete newErrors[otherVariationErrorKey]; + } + } + } else { + // Remove variation reason errors if variation is within ±10% + delete newErrors[variationErrorKey]; + delete newErrors[otherVariationErrorKey]; + } + + setFormErrors(newErrors); + } + } } onProductsChange(updatedProducts); @@ -2397,13 +2562,20 @@ useEffect(() => {
- +