fixed
// ============================================================================
// Section 6: SPIF vs Comm Deduction (Action Code: MAV_DIR_BB_ACTION_1012)
// Covers original Sections 10 & 11
// ============================================================================
if (BBActionManual_c == 'MAV_DIR_BB_ACTION_1012' && SalesRP_c == 'Y') {
println("[EPA Trigger] *** START Section 6: SPIF vs Comm Deduction ***")
println("[EPA Trigger] Action Code: ${BBActionManual_c}")
println("[EPA Trigger] IncentiveType: ${IncentiveType_c}")
println("[EPA Trigger] CED_c: ${CED_c}")
println("[EPA Trigger] Current ExpectedPaymentAmount_c: ${ExpectedPaymentAmount_c}")
// Verify the Incentive Type condition (BONUS only in original code)
if (IncentiveType_c == 'BONUS') {
println("[EPA Trigger] IncentiveType check passed (BONUS).")
// ** VALIDATION 1: Employee Consent check **
if (EmployeeConsent_c == 'N') {
BBActionManual_c = null // FIX: Reset the action field on validation failure
throw new oracle.jbo.ValidationException("EPA : Employee Consent is required for salesrep!");
}
def calculationExecuted = false
// --- START FIX: Update null check to use the correct variables ---
// Calculate the base paysheet amount for the later null check, using Groovy Elvis operator (?:) for safety
def basePaysheetAmount = SCDEPAValue_c ?: SCDCEDValue_c
// ** NULL-AWARE CHECK BEFORE VALIDATION/CALCULATION **
// Replaced TotalPaysheetAmountInLHC_c with the newly introduced fields SCDEPAValue_c/SCDCEDValue_c
if (basePaysheetAmount == null || ExpectedPaymentAmount_c == null) {
// WARNING MESSAGE ADDED/MODIFIED
println("[EPA Trigger] WARNING: MPAGenerated_c set to NULL because paysheet calculation input (SCDEPAValue_c or SCDCEDValue_c) or ExpectedPaymentAmount_c (${ExpectedPaymentAmount_c}) is null. Upstream data required for calculation.")
MPAGenerated_c = null // Set to null if inputs are missing
BBActionManual_c = null // FIX: Reset the action field on validation failure
throw new oracle.jbo.ValidationException("EPA : Commision EPA or CED is NULL. MPA cannot be compared!!");
} else {
// --- END FIX ---
// ** VALIDATION 2: Expected Payment Amount **
if (ExpectedPaymentAmount_c > 0) {
println("[EPA Trigger] ** VALIDATION FAILED ** ExpectedPaymentAmount_c is Positive: ${ExpectedPaymentAmount_c}")
BBActionManual_c = null // FIX: Reset the action field on validation failure
throw new oracle.jbo.ValidationException("EPA : Positive Value Not Allowed!");
}
println("[EPA Trigger] Validation Passed. ExpectedPaymentAmount_c is valid (<= 0).")
// Calculate MPAGenerated_c and CAP TO 2 DECIMAL PLACES
MPAGenerated_c = ((-1)*ExpectedPaymentAmount_c).setScale(2, BigDecimal.ROUND_HALF_UP)
println("[EPA Trigger] Calculated MPAGenerated_c: ${MPAGenerated_c} (Capped to 2 Decimals)")
calculationExecuted = true
}
//-Lookup validation
String lookupMeaning_c = null // Initialize
String lookupTag_c = null// Initialize
def lookupType = 'MTQ_ICM_BB_COUNTRY_LKP'
def lookupCode = EmploymentCountry_c
println("[EPA Trigger] Lookup Attempt: VO='CommonLookup', Type=${lookupType}, Code=${lookupCode}")
def voLookUp = newView("CommonLookup");
def str1SearchExpression = "LookupType = '${lookupType}' AND LookupCode = '${lookupCode}'"
println("[EPA Trigger] Applying View Criteria: ${str1SearchExpression}")
// 3. Apply the criteria and execute the query
voLookUp.appendViewCriteria(str1SearchExpression)
voLookUp.executeQuery()
if (voLookUp.getEstimatedRowCount() > 0) {
while(voLookUp.hasNext()) {
def rowLookup = voLookUp.next();
// Retrieve attributes using nvl(value, default) for null-safe access
lookupMeaning_c = nvl(rowLookup?.Description, "")
lookupTag_c = nvl(rowLookup?.Tag, "") // This is retrieved as a String
// Break after finding the first match
break
}
println("[EPA Trigger] Lookup Successful. Meaning: ${lookupMeaning_c}, Tag: ${lookupTag_c}")
} else {
println("[EPA Trigger] Lookup Failed: No matching row found for Type=${lookupType}, Code=${lookupCode}")
}
// Clean up the VO (optional but good practice)
voLookUp.reset()
// --- FIXED VALIDATION LOGIC START (2 Decimals) ---
def limitAmount = null
// Ensure all required variables are non-null and lookupTag_c is not empty
// Replaced TotalPaysheetAmountInLHC_c with basePaysheetAmount (which contains SCDEPAValue_c or SCDCEDValue_c)
if (MPAGenerated_c != null && basePaysheetAmount != null && lookupTag_c != null && lookupTag_c.trim() != '') {
try {
// Define explicit BigDecimal variables to avoid 'unpegged token *' error
// FIX: Use Groovy's Elvis operator (?:) for clearer, standard null-coalescing logic.
def paysheetAmount = (SCDEPAValue_c ?: SCDCEDValue_c) as BigDecimal // total paysheet amount is scdepaalue or scencedvalue
def tagLimitFactor = new BigDecimal(lookupTag_c.trim())
// Calculate the limit amount, and cap the result to 2 decimal places.
limitAmount = (paysheetAmount * tagLimitFactor).setScale(2, BigDecimal.ROUND_HALF_UP)
println("[EPA Trigger] Calculated Limit (2 Decimals): ${limitAmount}")
println("[EPA Trigger] Calculated MPAGenerated_c (Absolute): ${MPAGenerated_c.abs()}")
println("[EPA Trigger] Calculated Limit (Absolute): ${limitAmount.abs()}")
// Perform the validation comparison: Using absolute values for both sides for magnitude check.
// FIX: Using .abs() on limitAmount as requested.
if (MPAGenerated_c.abs() > limitAmount.abs()) {
// Throw the exception, using the 2-decimal capped limitAmount in the message
def formattedLimit = limitAmount.toString()
BBActionManual_c = null // FIX: Reset the action field on validation failure
MPAGenerated_c = null // ADDED: Reset the calculated field to prevent 'dirty' state
throw new oracle.jbo.ValidationException("EPA : MPA generated should not be greater than " + formattedLimit);
}
} catch (NumberFormatException e) {
// Handle case where lookupTag_c is not a valid number
println("[EPA Trigger] ERROR: Failed to convert lookupTag_c ('${lookupTag_c}') to a number. Skipping limit validation.")
}
} else {
println("[EPA Trigger] WARNING: Limit validation skipped due to missing values (MPAGenerated_c, basePaysheetAmount, or lookupTag_c).")
}
// --- FIXED VALIDATION LOGIC END ---
if (calculationExecuted) {
println("[EPA Trigger] Section 6 completed successfully")
}
} else {
println("[EPA Trigger] IncentiveType not BONUS - skipping Section 6 logic.")
}
println("[EPA Trigger] *** END Section 6 (Original 10 & 11) ***")
}
Comments
Post a Comment