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

bankdeposit-60-accrual.spp

bankdeposit/accrual/bankdeposit-60-accrual.spp

Download original file · Bank deposit source contents

1domain bankdeposit;23Role DepositAccrualPlan {4    int endDate;5    int normalDays;6    int overdraftDays;7    BigDecimal(25, 2) normalIncrement;8    BigDecimal(25, 2) agreementIncrement;9    int principalDays;10    int interestDays;11    BigDecimal(25, 2) overdraftIncrement;12    BigDecimal(25, 2) overdraftInterestIncrement;13}1415atomic check checkAccrualPolicy(DepositAccount, DepositInterestPolicy) {16    require(17        depositInterestPolicy.normalUnit > 0 && depositInterestPolicy.overdraftUnit > 0,18        RejectionCode.INVALID_ACCRUAL_UNIT19    );20    require(21        depositAccount.overdraftPrincipal <= 022            && depositAccount.overdraftInterest <= 023            && depositAccount.collectionInterest <= 0,24        RejectionCode.E115525    );26}2728atomic calculate prepareAccrualDates(DepositAccrualState, BusinessDay, BankDate, PeriodEnd) {29    start.encoded = depositAccrualState.lastProductDate;30    end.encoded = businessDay.date;31}3233atomic calculate prepareNormalAccrualPeriod(DepositAccount, DepositInterestPolicy, InterestPeriod) {34    normal.amount = 0;35    if (depositInterestPolicy.normalEnabled) {36        normal.amount = depositAccount.interestBase;37    }38    normal.unit = depositInterestPolicy.normalUnit;39}4041// the legacy product module B uses ACT days for overdraft independently of the normal interest calendar.4243atomic calculate calculateAccrual(44    DepositAccount,45    DepositInterestPolicy,46    InterestPeriod,47    InterestProduct,48    BankDate,49    PeriodEnd,50    DepositAccrualPlan51) {52    depositAccrualPlan.endDate = end.encoded;53    depositAccrualPlan.normalDays = normal.days;54    depositAccrualPlan.overdraftDays = end.epochDay - start.epochDay;55    depositAccrualPlan.normalIncrement = interestProduct.product;56    depositAccrualPlan.overdraftIncrement = roundDown(57        -depositAccount.overdraftPrincipal / depositInterestPolicy.overdraftUnit,58        059    ) * depositInterestPolicy.overdraftUnit * depositAccrualPlan.overdraftDays;60    depositAccrualPlan.overdraftInterestIncrement = roundDown(61        (-depositAccount.overdraftInterest62            - depositAccount.collectionInterest)63            / depositInterestPolicy.overdraftUnit,64        065    ) * depositInterestPolicy.overdraftUnit * depositAccrualPlan.overdraftDays;66}6768atomic calculate accumulateAccrual(DepositAccrualState, DepositAccrualPlan) {69    depositAccrualState.lastProductDate = depositAccrualPlan.endDate;70    depositAccrualState.normalProduct += depositAccrualPlan.normalIncrement;71    depositAccrualState.overdraftProduct += depositAccrualPlan.overdraftIncrement;72    depositAccrualState.overdraftInterestProduct += depositAccrualPlan.overdraftInterestIncrement;73    depositAccrualState.actualProduct = depositAccrualState.normalProduct74        - depositAccrualState.overdraftProduct;75    depositAccrualState.overdraftDays += depositAccrualPlan.principalDays;76    depositAccrualState.overdraftInterestDays += depositAccrualPlan.interestDays;77    depositAccrualState.agreementProduct += depositAccrualPlan.agreementIncrement;78}7980atomic check checkAccrualDates(81    DepositAccrualState,82    BusinessDay,83    InterestCalendar,84    AgreementTerm,85    AgreementBand[]86) {87    require(88        isGregorianDate(depositAccrualState.lastProductDate) && isGregorianDate(businessDay.date),89        RejectionCode.EE37490    );91    require(depositAccrualState.lastProductDate <= businessDay.date, RejectionCode.E1175);92    require(93        interestCalendar.convention == "ACT"94            || interestCalendar.convention == "360365"95            || interestCalendar.convention == "360360",96        RejectionCode.EEC2897    );98    require(99        agreementTerm.effectiveDate == 0 || isGregorianDate(agreementTerm.effectiveDate),100        RejectionCode.EE374101    );102    require(agreementTerm.cancelDate == 0 || isGregorianDate(agreementTerm.cancelDate), RejectionCode.EE374);103    int expected = 5;104    for (AgreementBand band : agreementBands) {105        require(band.slot == expected && band.threshold >= 0, RejectionCode.INVALID_AGREEMENT_BANDS);106        expected--;107    }108    require(expected == 0, RejectionCode.INVALID_AGREEMENT_BANDS);109}110111// All local mathematical intermediate participants are created in a calculation atom.112113atomic calculate calculateDepositAccrual(114    DepositAccount,115    BusinessDay,116    DepositAccrualState,117    DepositInterestPolicy,118    InterestCalendar,119    AgreementTerm,120    AgreementPolicy,121    AgreementBand[],122    DepositAccrualPlan123) {124    BankDate start = new BankDate();125    PeriodEnd end = new PeriodEnd();126    OverlapStart clippedStart = new OverlapStart();127    OverlapEnd clippedEnd = new OverlapEnd();128    InterestPeriod normal = new InterestPeriod();129    OverlapPeriod overlap = new OverlapPeriod();130    InterestProduct normalProduct = new InterestProduct();131    AgreementProduct products = new AgreementProduct();132    prepareAccrualDates(depositAccrualState, businessDay, start, end);133    decodeBankDate(start);134    decodeBankDate(end);135    prepareNormalAccrualPeriod(depositAccount, depositInterestPolicy, normal);136    calculateBankDays(start, end, interestCalendar, normal);137    agreementPolicy.enabled = depositInterestPolicy.normalEnabled && agreementPolicy.enabled;138    prepareAgreementWindow(139        start,140        end,141        agreementTerm,142        clippedStart,143        clippedEnd,144        normal,145        overlap,146        agreementPolicy147    );148    decodeBankDate(clippedStart);149    decodeBankDate(clippedEnd);150    calculateBankDays(clippedStart, clippedEnd, interestCalendar, overlap);151    calculateAgreementProduct(normal, overlap, agreementPolicy, agreementBands, products);152    calculateInterestProduct(normal, normalProduct);153    calculateAccrual(154        depositAccount,155        depositInterestPolicy,156        normal,157        normalProduct,158        start,159        end,160        depositAccrualPlan161    );162    depositAccrualPlan.normalIncrement = products.normalProduct;163    depositAccrualPlan.agreementIncrement = products.bandProduct;164    depositAccrualPlan.principalDays = 0;165    depositAccrualPlan.interestDays = 0;166    if (depositAccount.overdraftPrincipal < 0) {167        depositAccrualPlan.principalDays = depositAccrualPlan.overdraftDays;168    }169    if (depositAccount.overdraftInterest < 0) {170        depositAccrualPlan.interestDays = depositAccrualPlan.overdraftDays;171    }172}173174readonly Scenario serial previewDepositAccrual(175    DepositAccount,176    BusinessDay,177    DepositAccrualState,178    DepositInterestPolicy,179    InterestCalendar,180    AgreementTerm,181    AgreementPolicy,182    AgreementBand[],183    DepositAccrualPlan184) {185    checkAccrualDates;186    checkAccrualPolicy;187    calculateDepositAccrual;188    accumulateAccrual;189}