diff --git a/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx b/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx index dc04eef..ebef52b 100644 --- a/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx +++ b/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx @@ -8,6 +8,7 @@ import { fetchZeroTargetReasons, fetchProducts, fetchUnits, + getVariationReasons, } from '@/services/masters/masterService.js'; // import { fetchEstablishmentDetail } from '@/services/establishments/establishmentService'; import { useLocation } from 'react-router-dom'; @@ -465,6 +466,7 @@ const ProductData = ({ const [maxProducts, setMaxProducts] = React.useState(0); const nextId = React.useRef(products.length + 1); const [variationReasons, setVariationReasons] = React.useState([]); + const [allVariationReasons, setAllVariationReasons] = React.useState([]); const [isLoadingReasons, setIsLoadingReasons] = React.useState(false); const [reasonsError, setReasonsError] = React.useState(''); const [zeroTargetReasons, setZeroTargetReasons] = React.useState([]); @@ -1330,8 +1332,10 @@ useEffect(() => { setIsLoadingReasons(true); setReasonsError(''); try { + const reason2 = await getVariationReasons(); const reasons = await fetchVariationReasons({ signal: variationController.signal }); setVariationReasons(reasons || []); + setAllVariationReasons(reason2.data || []); setReasonsError(''); } catch (error) { console.error('Failed to fetch variation reasons:', error); @@ -2039,7 +2043,7 @@ useEffect(() => { } }; - const updateProductField = (id, field, value, options = null) => { + const updateProductField = (id, field, value, options = null,period = null) => { // Create a copy of the current products array to work with let updatedProducts = [...products]; @@ -2409,6 +2413,37 @@ useEffect(() => { } onProductsChange(updatedProducts); + if (field.endsWith('Quantity') && value && value !== '') { + let previousTotal = 0; + let currentTotal = 0; + + updatedProducts.forEach(p => { + if (p.octQuantity) previousTotal += parseFloat(p.octQuantity) || 0; + if (p.novQuantity) previousTotal += parseFloat(p.novQuantity) || 0; + if (p.decQuantity) previousTotal += parseFloat(p.decQuantity) || 0; + if (p.janQuantity) currentTotal += parseFloat(p.janQuantity) || 0; + if (p.febQuantity) currentTotal += parseFloat(p.febQuantity) || 0; + if (p.marQuantity) currentTotal += parseFloat(p.marQuantity) || 0; + }); + + if (previousTotal > currentTotal) { + const lowVariationReasons = allVariationReasons + .filter(item => item.high_or_low === "Low") + .map(item => ({ + label: item.reason, + value: String(item.id) + })); + setVariationReasons(lowVariationReasons); + }else if (currentTotal > previousTotal) { + const highVariationReasons = allVariationReasons + .filter(item => item.high_or_low === "High") + .map(item => ({ + label: item.reason, + value: String(item.id) + })); + setVariationReasons(highVariationReasons); + } + } }; const handleProductSelect = async (productId, establishmentId, id) => { @@ -2951,7 +2986,7 @@ useEffect(() => { placeholder="Enter" value={p.octQuantity || ''} error={!!formErrors[`octQuantity_${idx}`]} - onChange={(e) => updateProductField(p.id, 'octQuantity', e.target.value)} + onChange={(e) => updateProductField(p.id, 'octQuantity', e.target.value,null,'previous') } /> {formErrors[`octQuantity_${idx}`] && (
{formErrors[`octQuantity_${idx}`]}
@@ -2966,7 +3001,7 @@ useEffect(() => { placeholder="Enter" value={p.novQuantity || ''} error={!!formErrors[`novQuantity_${idx}`]} - onChange={(e) => updateProductField(p.id, 'novQuantity', e.target.value)} + onChange={(e) => updateProductField(p.id, 'novQuantity', e.target.value,null,'previous') } /> {formErrors[`novQuantity_${idx}`] && ({formErrors[`novQuantity_${idx}`]}
@@ -2981,7 +3016,7 @@ useEffect(() => { placeholder="Enter" value={p.decQuantity || ''} error={!!formErrors[`decQuantity_${idx}`]} - onChange={(e) => updateProductField(p.id, 'decQuantity', e.target.value)} + onChange={(e) => updateProductField(p.id, 'decQuantity', e.target.value,null,'previous') } /> {formErrors[`decQuantity_${idx}`] && ({formErrors[`decQuantity_${idx}`]}
@@ -3060,7 +3095,7 @@ useEffect(() => { value={p.janQuantity || ''} error={!!formErrors[`janQuantity_${idx}`]} placeholder="Enter" - onChange={(e) => updateProductField(p.id, 'janQuantity', e.target.value)} + onChange={(e) => updateProductField(p.id, 'janQuantity', e.target.value,null,'current') } /> {formErrors[`janQuantity_${idx}`] && ({formErrors[`janQuantity_${idx}`]}
@@ -3076,7 +3111,7 @@ useEffect(() => { value={p.febQuantity || ''} error={!!formErrors[`febQuantity_${idx}`]} placeholder="Enter" - onChange={(e) => updateProductField(p.id, 'febQuantity', e.target.value)} + onChange={(e) => updateProductField(p.id, 'febQuantity', e.target.value,null,'current') } /> {formErrors[`febQuantity_${idx}`] && ({formErrors[`febQuantity_${idx}`]}
@@ -3092,7 +3127,7 @@ useEffect(() => { value={p.marQuantity || ''} error={!!formErrors[`marQuantity_${idx}`]} placeholder="Enter" - onChange={(e) => updateProductField(p.id, 'marQuantity', e.target.value)} + onChange={(e) => updateProductField(p.id, 'marQuantity', e.target.value,null,'current') } /> {formErrors[`marQuantity_${idx}`] && ({formErrors[`marQuantity_${idx}`]}
diff --git a/ipi-survey-platform/src/pages/Admin/configuration/EditCompanyProfile.jsx b/ipi-survey-platform/src/pages/Admin/configuration/EditCompanyProfile.jsx index fccb311..a3f0f16 100644 --- a/ipi-survey-platform/src/pages/Admin/configuration/EditCompanyProfile.jsx +++ b/ipi-survey-platform/src/pages/Admin/configuration/EditCompanyProfile.jsx @@ -29,6 +29,7 @@ const createEmptyProfile = () => ({ contactEmail: '', contactPostalCode: '', contactPoBox: '', + zone: '', contactWebsite: '', employmentEmiratiMale: 0, employmentEmiratiFemale: 0, @@ -58,6 +59,7 @@ const mapApiEstablishmentToProfile = (apiData) => { contactEmail: data.establishment_contact_email || '', contactPostalCode: data.establishment_postal_code || '', contactPoBox: data.establishment_po_box || '', + zone: data.zone || '', contactWebsite: data.establishment_website || '', employmentEmiratiMale: data.emirati_male || 0, employmentEmiratiFemale: data.emirati_female || 0, @@ -818,6 +820,7 @@ const EditCompanyProfile = () => { establishment_city_town_id: selectedCity ? Number(selectedCity.value) : null, establishment_emirate_id: selectedEmirate ? Number(selectedEmirate.value) : null, establishment_postal_code: form.contactPostalCode || "", + zone:form.zone|| "", establishment_po_box: form.contactPoBox || "", establishment_makani_number: form.contactMakaniNumber || "", establishment_contact_person_name: form.contactPersonName || "", @@ -1041,6 +1044,13 @@ const EditCompanyProfile = () => { placeholder="Enter Postal Code" width="100%" /> +