Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,37 @@ const EditIndicatorModal: React.FC<EditIndicatorModalProps> = (props) => {
return DateUtil.toISO8601(date, globalSettings['default-date-format']);
};

const resolveDisaggregationOrientation = (
selectedDisaggregations: number[] = [],
disaggregationValues: any[] = []
) => {
const defaultOrientation = {
parentDisaggregationId: selectedDisaggregations[0],
childDisaggregationId: selectedDisaggregations[1],
};

if (selectedDisaggregations.length !== 2 || !Array.isArray(disaggregationValues) || disaggregationValues.length === 0) {
return defaultOrientation;
}

const orientationFromValues = disaggregationValues.find((value: any) => (
value?.parentDisaggregationId
&& value?.childDisaggregationId
&& selectedDisaggregations.includes(value.parentDisaggregationId)
&& selectedDisaggregations.includes(value.childDisaggregationId)
&& value.parentDisaggregationId !== value.childDisaggregationId
));

if (orientationFromValues) {
return {
parentDisaggregationId: orientationFromValues.parentDisaggregationId,
childDisaggregationId: orientationFromValues.childDisaggregationId,
};
}

return defaultOrientation;
};

const formikRef = useRef<FormikProps<IndicatorFormValues>>(null);

const getCategories = () => {
Expand Down Expand Up @@ -523,6 +554,11 @@ const EditIndicatorModal: React.FC<EditIndicatorModalProps> = (props) => {
}}
>
{(props) => {
const twoLevelOrientation = resolveDisaggregationOrientation(
props.values.disaggregation,
props.values.disaggregationValues || indicator?.disaggregationValues || []
);

// Fetch disaggregation children when disaggregation changes
useEffect(() => {
const selected = props.values.disaggregation;
Expand All @@ -546,20 +582,25 @@ const EditIndicatorModal: React.FC<EditIndicatorModalProps> = (props) => {
return existing || {
parentCategoryId: child.id,
childCategoryId: null,
parentDisaggregationId: selected[0],
base: { originalValue: '', originalValueDate: '', revisedValue: '', revisedValueDate: '' },
target: { originalValue: '', originalValueDate: '', revisedValue: '', revisedValueDate: '' }
};
});
} else if (selected.length === 2) {
// For double disaggregation, cross product of children
const parents = childrenMap[selected[0]] || [];
const children = childrenMap[selected[1]] || [];
const parentDisaggregationId = twoLevelOrientation.parentDisaggregationId;
const childDisaggregationId = twoLevelOrientation.childDisaggregationId;
const parents = childrenMap[parentDisaggregationId] || [];
const children = childrenMap[childDisaggregationId] || [];
parents.forEach((parent: any) => {
children.forEach((child: any) => {
const existing = (props.values.disaggregationValues || []).find((v: any) => v.parentCategoryId === parent.id && v.childCategoryId === child.id);
newDisaggValues.push(existing || {
parentCategoryId: parent.id,
childCategoryId: child.id,
parentDisaggregationId,
childDisaggregationId,
base: { originalValue: '', originalValueDate: '', revisedValue: '', revisedValueDate: '' },
target: { originalValue: '', originalValueDate: '', revisedValue: '', revisedValueDate: '' }
});
Expand Down Expand Up @@ -1026,7 +1067,7 @@ const EditIndicatorModal: React.FC<EditIndicatorModalProps> = (props) => {
<div style={{marginTop: '1rem'}}>
<h6>{t("amp.indicatormanager:disaggregation-values")}</h6>
<Accordion defaultActiveKey="0">
{disaggregationChildren[props.values.disaggregation[0]]?.map((parentChild: any, parentIdx: number) => (
{disaggregationChildren[twoLevelOrientation.parentDisaggregationId]?.map((parentChild: any, parentIdx: number) => (
<Card key={parentChild.id}>
<Accordion.Toggle
as={Card.Header}
Expand All @@ -1044,14 +1085,16 @@ const EditIndicatorModal: React.FC<EditIndicatorModalProps> = (props) => {
</Accordion.Toggle>
<Accordion.Collapse eventKey={String(parentIdx)}>
<Card.Body>
{disaggregationChildren[props.values.disaggregation[1]]?.length > 0 ? (
{disaggregationChildren[twoLevelOrientation.childDisaggregationId]?.length > 0 ? (
<div style={{maxHeight: '300px', overflowY: 'auto'}}>
{disaggregationChildren[props.values.disaggregation[1]].map((child: any) => {
{disaggregationChildren[twoLevelOrientation.childDisaggregationId].map((child: any) => {
const disaggArr = Array.isArray(props.values.disaggregationValues) ? props.values.disaggregationValues : [];
let entryIdx = disaggArr.findIndex((v: any) => v.parentCategoryId === parentChild.id && v.childCategoryId === child.id);
let entry = entryIdx !== -1 ? disaggArr[entryIdx] : {
parentCategoryId: parentChild.id,
childCategoryId: child.id,
parentDisaggregationId: twoLevelOrientation.parentDisaggregationId,
childDisaggregationId: twoLevelOrientation.childDisaggregationId,
base: { originalValue: '', originalValueDate: '', revisedValue: '', revisedValueDate: '' },
target: { originalValue: '', originalValueDate: '', revisedValue: '', revisedValueDate: '' }
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,6 @@ public static AmpActivityVersion saveActivityNewVersion(AmpActivityVersion a,
saveAnnualProjectBudgets(a, session);
saveProjectCosts(a, session);
saveStructures(a, session);
// Explicitly save indicator disaggregation values
saveIndicatorDisaggregationValues(a, session);
if (createNewVersion) {
if (a.getAmpActivityId() == null)
session.save(a);
Expand Down Expand Up @@ -1563,22 +1561,4 @@ public static Long calculateFundingDetailCheckSum(FundingInformationItem item) {
return checkSum;
}

private static void saveIndicatorDisaggregationValues(AmpActivityVersion a, Session session) {
Set<IndicatorActivity> indicators = a.getIndicators();
if (indicators == null) return;
for (IndicatorActivity indicatorActivity : indicators) {
AmpIndicator indicator = indicatorActivity.getIndicator();
if (indicator == null) continue;
Set<AmpIndicatorDisaggregationValue> disaggregationValues = indicator.getDisaggregationValues();
if (disaggregationValues == null) continue;
for (AmpIndicatorDisaggregationValue value : disaggregationValues) {
value.setIndicator(indicator);
if (value.getId() == null) {
session.saveOrUpdate(value);
} else {
session.merge(value);
}
}
}
}
}
Loading