S++Core systems, human-reviewable
DEMO SOURCE · SPP / APP / PHYRead the book

bankdeposit-00-rules.spp

bankdeposit/reversal/bankdeposit-00-rules.spp

Download original file · Bank deposit source contents

1domain bankdeposit;23Role DepositReversalCapacity {4    boolean overdraftPermitted;5    BigDecimal(19, 2) overdraftLimit;6}78Role CashReversalMetadataPlan {9    int detailCount;10    int lastTransactionDate;11    BigDecimal(19, 2) cashDepositedToday;12    BigDecimal(25, 2) normalProduct;13    BigDecimal(25, 2) actualProduct;14    boolean reverseInstitutionLimit;15}1617atomic check checkOriginalCashMovement(CashDepositAgreement) {18    require(19        cashDepositAgreement.amount >= 020            && cashDepositAgreement.interestPaid >= 021            && cashDepositAgreement.collectionPaid >= 022            && cashDepositAgreement.principalPaid >= 0,23        RejectionCode.INVALID_ORIGINAL_DEPOSIT_MOVEMENT24    );25    require(26        cashDepositAgreement.amount == cashDepositAgreement.interestBaseDelta27            + cashDepositAgreement.interestPaid28            + cashDepositAgreement.collectionPaid29            + cashDepositAgreement.principalPaid,30        RejectionCode.ORIGINAL_DEPOSIT_MOVEMENT_NOT_BALANCED31    );32    require(!cashDepositAgreement.reversed, RejectionCode.DEPOSIT_ALREADY_REVERSED);33    require(cashDepositAgreement.fee >= 0, RejectionCode.INVALID_ORIGINAL_DEPOSIT_FEE);34}3536atomic check checkCashReversalCapacity(DepositAccount, CashDepositAgreement, DepositReversalCapacity) {37    BigDecimal available = max(38        0,39        depositAccount.interestBase - depositAccount.frozenAmount - depositAccount.authorizedAmount40    );41    require(42        cashDepositAgreement.amount <= available43            || (depositReversalCapacity.overdraftPermitted44            && depositReversalCapacity.overdraftLimit >= cashDepositAgreement.amount45            - available),46        RejectionCode.ES02047    );48    BigDecimal interestBase = depositAccount.interestBase - cashDepositAgreement.interestBaseDelta;49    require(interestBase >= 0, RejectionCode.EN249);50    require(51        !(interestBase > 052            && depositAccount.overdraftPrincipal53            - cashDepositAgreement.principalPaid < 054            && interestBase > depositAccount.frozenAmount),55        RejectionCode.EN17756    );57}5859atomic check checkCashReversalFacts(CashDepositAgreement, CashReversalAgreement, DepositAccrualState) {60    require(61        cashReversalAgreement.newDetailItems > 0 && cashDepositAgreement.addedBackdatedProduct >= 0,62        RejectionCode.INVALID_CASH_REVERSAL_FACTS63    );64    require(65        depositAccrualState.normalProduct >= cashDepositAgreement.addedBackdatedProduct,66        RejectionCode.EN09767    );68}6970atomic calculate calculateBankDepositCashReversalMetadata(71    CashDepositAgreement,72    CashReversalAgreement,73    AccountEligibility,74    DepositAccrualState,75    CashReversalMetadataPlan76) {77    cashReversalMetadataPlan.normalProduct = depositAccrualState.normalProduct78        - cashDepositAgreement.addedBackdatedProduct;79    cashReversalMetadataPlan.actualProduct = cashReversalMetadataPlan.normalProduct80        - depositAccrualState.overdraftProduct;81    cashReversalMetadataPlan.detailCount = accountEligibility.detailCount82        + cashReversalAgreement.newDetailItems;83    if (accountEligibility.detailCount == cashDepositAgreement.settledDetailCount) {84        cashReversalMetadataPlan.lastTransactionDate = cashDepositAgreement.previousTransactionDate;85    } else {86        cashReversalMetadataPlan.lastTransactionDate = cashReversalAgreement.businessDate;87    }88    if (cashReversalAgreement.overnightMode2) {89        cashReversalMetadataPlan.lastTransactionDate = cashReversalAgreement.businessDate;90    }91    cashReversalMetadataPlan.cashDepositedToday = max(92        0,93        accountEligibility.cashDepositedToday - cashDepositAgreement.amount94    );95    cashReversalMetadataPlan.reverseInstitutionLimit = cashDepositAgreement.transactionDate == cashReversalAgreement.businessDate96        && accountEligibility.detailCount == cashDepositAgreement.settledDetailCount;97}9899atomic calculate prepareCashReversalAgreement(100    CashDepositAgreement,101    CashReversalAgreement,102    CashReversalMetadataPlan103) {104    cashReversalAgreement.original = cashDepositAgreement;105    cashReversalAgreement.refundAmount = cashDepositAgreement.fee;106    cashReversalAgreement.reverseInstitutionLimit = cashReversalMetadataPlan.reverseInstitutionLimit;107}108109quantum update reverseCashDeposit(110    DepositAccount,111    CashSettlementAccount,112    InterestIncomeAccount,113    CollectionInterestIncomeAccount,114    CashDepositAgreement115) {116    if (cashDepositAgreement.interestBaseDelta >= 0) {117        depositAccount.interestBase -> cashSettlementAccount.balance : cashDepositAgreement.interestBaseDelta;118    } else {119        cashSettlementAccount.balance -> depositAccount.interestBase : -cashDepositAgreement.interestBaseDelta;120    }121    depositAccount.overdraftInterest -> cashSettlementAccount.balance : cashDepositAgreement.interestPaid;122    depositAccount.collectionInterest -> cashSettlementAccount.balance : cashDepositAgreement.collectionPaid;123    depositAccount.overdraftPrincipal -> cashSettlementAccount.balance : cashDepositAgreement.principalPaid;124}125126atomic calculate completeCashReversal(127    DepositAccount,128    CashDepositAgreement,129    CashReversalMetadataPlan,130    AccountEligibility,131    DepositAccrualState132) {133    accountEligibility.accountBalance = depositAccount.interestBase134        + depositAccount.overdraftInterest135        + depositAccount.collectionInterest136        + depositAccount.overdraftPrincipal;137    accountEligibility.cashDepositedToday = cashReversalMetadataPlan.cashDepositedToday;138    cashDepositAgreement.reversed = true;139    accountEligibility.detailCount = cashReversalMetadataPlan.detailCount;140    accountEligibility.lastTransactionDate = cashReversalMetadataPlan.lastTransactionDate;141    depositAccrualState.normalProduct = cashReversalMetadataPlan.normalProduct;142    depositAccrualState.actualProduct = cashReversalMetadataPlan.actualProduct;143}144145atomic check checkBankDepositCashReversalPath(DepositOperatorFacts, CashChannel) {146    require(147        cashChannel.teller148            && cashChannel.channel == "Teller"149            && cashChannel.transactionId == "DEP_CASH"150            && depositOperatorFacts.operationCode == "1102"151            && depositOperatorFacts.transactionPrefix == "SA01102",152        RejectionCode.UNSUPPORTED_CASH_REVERSAL_PATH153    );154}155156atomic check checkOriginalDepositAccount(DepositAccount, CashDepositAgreement) {157    require(158        cashDepositAgreement.account.accountNumber == depositAccount.accountNumber,159        RejectionCode.ORIGINAL_DEPOSIT_ACCOUNT_MISMATCH160    );161}162163atomic check checkOriginalSettlementAccounts(164    CashDepositAgreement,165    CashSettlementAccount,166    InterestIncomeAccount,167    CollectionInterestIncomeAccount,168    PrincipalRecoveryAccount169) {170    require(171        cashDepositAgreement.settlement.accountNumber == cashSettlementAccount.accountNumber172            && cashDepositAgreement.interestIncome.accountNumber == interestIncomeAccount.accountNumber173            && cashDepositAgreement.collectionIncome.accountNumber == collectionInterestIncomeAccount.accountNumber174            && cashDepositAgreement.principalRecovery.accountNumber == principalRecoveryAccount.accountNumber,175        RejectionCode.ORIGINAL_SETTLEMENT_ACCOUNTS_MISMATCH176    );177}178179readonly Scenario serial assessCashReversalFunding(180    DepositAccount,181    CashDepositAgreement,182    DepositReversalCapacity183) {184    checkOriginalCashMovement;185    checkCashReversalCapacity;186}187188readonly Scenario serial previewCashReversalMetadata(189    CashDepositAgreement,190    CashReversalAgreement,191    AccountEligibility,192    DepositAccrualState,193    CashReversalMetadataPlan194) {195    checkCashReversalFacts;196    calculateBankDepositCashReversalMetadata;197}198199readonly Scenario serial quoteCashReversal(200    CashDepositAgreement,201    CashReversalAgreement,202    CashReversalMetadataPlan203) {204    checkOriginalCashMovement;205    prepareCashReversalAgreement;206}