OrderElement orderElement, BigDecimal percentage,
LocalDate latestWorkLogDate) {
AdvanceType advanceType = PredefinedAdvancedTypes.PERCENTAGE.getType();
DirectAdvanceAssignment directAdvanceAssignment = orderElement
.getDirectAdvanceAssignmentByType(advanceType);
if (directAdvanceAssignment == null) {
directAdvanceAssignment = DirectAdvanceAssignment.create(false,
new BigDecimal(100).setScale(2));
directAdvanceAssignment.setAdvanceType(advanceType);
try {
orderElement.addAdvanceAssignment(directAdvanceAssignment);
} catch (DuplicateValueTrueReportGlobalAdvanceException e) {
// This couldn't happen as it has just created the
// directAdvanceAssignment with false as reportGlobalAdvance
throw new RuntimeException(e);
} catch (DuplicateAdvanceAssignmentForOrderElementException e) {
// This could happen if a parent or child of the current
// OrderElement has an advance of type PERCENTAGE
synchronizationInfo
.addFailedReason(_(
"Duplicate value AdvanceAssignment for order element of \"{0}\"",
orderElement.getCode()));
return;
}
}
AdvanceMeasurement advanceMeasurement = directAdvanceAssignment
.getAdvanceMeasurementAtExactDate(latestWorkLogDate);
if (advanceMeasurement == null) {
advanceMeasurement = AdvanceMeasurement.create();
advanceMeasurement.setDate(latestWorkLogDate);
directAdvanceAssignment.addAdvanceMeasurements(advanceMeasurement);
}
advanceMeasurement.setValue(percentage
.setScale(2, RoundingMode.HALF_UP));
DirectAdvanceAssignment spreadAdvanceAssignment = orderElement
.getReportGlobalAdvanceAssignment();
if (spreadAdvanceAssignment != null) {
spreadAdvanceAssignment.setReportGlobalAdvance(false);
}
directAdvanceAssignment.setReportGlobalAdvance(true);
}