Skip to content

Instantly share code, notes, and snippets.

@faridrama123
Created January 13, 2024 07:51
Show Gist options
  • Save faridrama123/84116bab9973b40f8819e7bd78302a35 to your computer and use it in GitHub Desktop.
Save faridrama123/84116bab9973b40f8819e7bd78302a35 to your computer and use it in GitHub Desktop.
fun minMaxValidation(
isAlliancePatternCostStructure: Boolean,
worklist: AppFormWorklist,
potentialData: AppFormPotentialData,
chosenLoanPurposeId: Long?,
chosenMitraId: Long?
): String {
if (chosenLoanPurposeId == null) {
return Message.PotentialData.EMPTY_INTENDED_USED
}
if (isAlliancePatternCostStructure && chosenMitraId == null) {
return Message.PotentialData.EMPTY_PARTNER
}
val gradeSgp = getGradeSgp()
val loanPurpose = getLoanPurpose(chosenLoanPurposeId)
/**
* Alliance Pattern Logic
*/
if (isAlliancePatternCostStructure) {
val mitraLoanPurpose =
getMitraLoanPurpose(
chosenMitraId?.toInt(),
loanPurpose.lpCode
)
val costStructure =
mitraLoanPurpose.costStructure ?: return Message.General.SOMETHING_HAPPENED
val maxFinancingUnit =
mitraLoanPurpose.maxCostStructure ?: return Message.General.SOMETHING_HAPPENED
val maxLimitCostStructureCalc = maxFinancingUnit * costStructure
val remainingLimit = worklist.remainingLimit?.toInt()
/**
* Max Limit Logic
*/
val smallestMaxLimitTumTur = listOfNotNull(
gradeSgp.maxLimit,
maxLimitCostStructureCalc.toLong(),
remainingLimit?.toLong(),
Temporary.potentialData?.maxlimitTopup?.toLong()
).minOrNull() ?: return Message.General.SOMETHING_HAPPENED
val smallestMaxLimit = listOfNotNull(
gradeSgp.maxLimit,
maxLimitCostStructureCalc.toLong(),
remainingLimit?.toLong()
).minOrNull() ?: return Message.General.SOMETHING_HAPPENED
/**
* Min Limit Logic
*/
val originalAmount = potentialData.originalAmount?.toInt()
val minCostStructure =
mitraLoanPurpose.minCostStructure ?: return Message.General.SOMETHING_HAPPENED
val minLimitCalc = minCostStructure * costStructure
val biggestMinLimit =
listOfNotNull(gradeSgp.minLimit, minLimitCalc, originalAmount).maxOrNull()
?: return Message.General.SOMETHING_HAPPENED
/**
* Validity Logic
*/
val requestTypeCode = getRequestTypeCode(Temporary.paidOff?.requestTypeId)
val isTopUp = (requestTypeCode == TUM || requestTypeCode == TUR)
val maxLimitFinal = if (isTopUp) {
smallestMaxLimitTumTur
} else {
smallestMaxLimit
}
return if (biggestMinLimit < maxLimitFinal) {
Message.General.SUCCESS
} else {
Message.PotentialData.LOAN_PURPOSE_NOT_MATCH_WITH_GRADE + "\n" + "\n" +
"Minimum Limit: Rp " + FormatterUtils.numberToCurrency(biggestMinLimit) + "\n" +
"Maksimum Limit: Rp " + FormatterUtils.numberToCurrency(maxLimitFinal)
}
}
/**
* Non Alliance Pattern Logic
*/
val minLimit =
listOfNotNull(
gradeSgp.minLimit,
loanPurpose.minLimit,
potentialData.originalAmount?.toInt()
).maxOrNull() ?: return Message.General.SOMETHING_HAPPENED
val smallestMaxLimit = listOfNotNull(
gradeSgp.maxLimit,
loanPurpose.maxLimit,
worklist.remainingLimit?.toLong()
).minOrNull() ?: return Message.General.SOMETHING_HAPPENED
val smallestMaxLimitTumTur = listOfNotNull(
gradeSgp.maxLimit,
loanPurpose.maxLimit,
worklist.remainingLimit?.toLong(),
Temporary.potentialData?.maxlimitTopup?.toLong()
).minOrNull() ?: return Message.General.SOMETHING_HAPPENED
/**
* Validity Logic
*/
val requestTypeCode = getRequestTypeCode(Temporary.paidOff?.requestTypeId)
val isTopUp = (requestTypeCode == TUM || requestTypeCode == TUR)
val maxLimitFinal = if (isTopUp) {
smallestMaxLimitTumTur
} else {
smallestMaxLimit
}
return if (minLimit < maxLimitFinal) {
Message.General.SUCCESS
} else {
Message.PotentialData.LOAN_PURPOSE_NOT_MATCH_WITH_GRADE + "\n" + "\n" +
"Minimum Limit: Rp " + FormatterUtils.numberToCurrency(minLimit) + "\n" +
"Maksimum Limit: Rp " + FormatterUtils.numberToCurrency(maxLimitFinal)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment