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

bankdeposit-10-postings.spp

bankdeposit/reversal/bankdeposit-10-postings.spp

Download original file · Bank deposit source contents

1domain bankdeposit;23Role CashReversalPostingLine {4    CashReversalAgreement reversal;5    int slot;6    String subject;7    String side;8    BigDecimal(19, 2) amount;9}1011atomic query listDepositPostings(key CashDepositAgreement, DepositPostingLine[]);1213atomic check checkOriginalDepositPostings(CashDepositAgreement, DepositPostingLine[]) {14    int count = 0;15    for (DepositPostingLine line : depositPostingLines) {16        count++;17        require(18            line.deposit.depositNumber == cashDepositAgreement.depositNumber,19            RejectionCode.ORIGINAL_DEPOSIT_POSTING_OWNER_MISMATCH20        );21        require(22            line.slot >= 123                && line.slot <= 824                && line.subject != null25                && line.subject != ""26                && line.amount >= 0,27            RejectionCode.INVALID_ORIGINAL_DEPOSIT_POSTINGS28        );29        int matching = 0;30        for (DepositPostingLine candidate : depositPostingLines) {31            if (candidate.slot == line.slot) {32                matching++;33            }34        }35        require(matching == 1, RejectionCode.INVALID_ORIGINAL_DEPOSIT_POSTINGS);36        if (line.slot == 1 || line.slot == 2 || line.slot == 7) {37            require(line.side == "Debit", RejectionCode.INVALID_ORIGINAL_DEPOSIT_POSTINGS);38        } else {39            require(line.side == "Credit", RejectionCode.INVALID_ORIGINAL_DEPOSIT_POSTINGS);40        }41        int evidenceMatches = 0;42        for (DepositPostingLine evidence : cashDepositAgreement.postingEvidence) {43            if (evidence.slot == line.slot) {44                require(45                    evidence.subject == line.subject46                        && evidence.side == line.side47                        && evidence.amount == line.amount,48                    RejectionCode.ORIGINAL_DEPOSIT_LEDGER_EVIDENCE_MISMATCH49                );50                evidenceMatches++;51            }52        }53        require(evidenceMatches == 1, RejectionCode.ORIGINAL_DEPOSIT_LEDGER_EVIDENCE_MISMATCH);54        switch (line.slot) {55            case 1:56                require(57                    line.amount == cashDepositAgreement.amount,58                    RejectionCode.ORIGINAL_DEPOSIT_POSTING_AMOUNT_MISMATCH59                );60                break;61            case 2:62                require(63                    line.amount == max(0, -cashDepositAgreement.interestBaseDelta),64                    RejectionCode.ORIGINAL_DEPOSIT_POSTING_AMOUNT_MISMATCH65                );66                break;67            case 3:68                require(69                    line.amount == max(0, cashDepositAgreement.interestBaseDelta),70                    RejectionCode.ORIGINAL_DEPOSIT_POSTING_AMOUNT_MISMATCH71                );72                break;73            case 4:74                require(75                    line.amount == cashDepositAgreement.principalPaid,76                    RejectionCode.ORIGINAL_DEPOSIT_POSTING_AMOUNT_MISMATCH77                );78                break;79            case 5:80                require(81                    line.amount == cashDepositAgreement.interestPaid,82                    RejectionCode.ORIGINAL_DEPOSIT_POSTING_AMOUNT_MISMATCH83                );84                break;85            case 6:86                require(87                    line.amount == cashDepositAgreement.collectionPaid,88                    RejectionCode.ORIGINAL_DEPOSIT_POSTING_AMOUNT_MISMATCH89                );90                break;91            case 7:92                require(93                    line.amount == cashDepositAgreement.fee,94                    RejectionCode.ORIGINAL_DEPOSIT_POSTING_AMOUNT_MISMATCH95                );96                break;97            case 8:98                require(99                    line.amount == cashDepositAgreement.fee,100                    RejectionCode.ORIGINAL_DEPOSIT_POSTING_AMOUNT_MISMATCH101                );102                break;103        }104    }105    require(count == 8, RejectionCode.INVALID_ORIGINAL_DEPOSIT_POSTINGS);106}107108atomic calculate prepareCashReversalPostings(109    DepositPostingLine[],110    CashReversalAgreement,111    CashReversalPostingLine[],112    DepositPostingTotals113) {114    cashReversalPostingLines = new CashReversalPostingLine[] {115    };116    depositPostingTotals.debit = 0;117    depositPostingTotals.credit = 0;118    depositPostingTotals.fee = cashReversalAgreement.refundAmount;119    for (DepositPostingLine original : depositPostingLines) {120        CashReversalPostingLine line = new CashReversalPostingLine();121        line.slot = original.slot;122        line.subject = original.subject;123        if (original.side == "Debit") {124            line.side = "Credit";125        } else {126            line.side = "Debit";127        }128        line.amount = original.amount;129        if (original.slot == 7 || original.slot == 8) {130            line.amount = cashReversalAgreement.refundAmount;131        }132        if (line.side == "Debit") {133            depositPostingTotals.debit += line.amount;134        } else {135            depositPostingTotals.credit += line.amount;136        }137        cashReversalPostingLines = append(cashReversalPostingLines, line);138    }139}140141atomic calculate attachCashReversalPostings(CashReversalAgreement, CashReversalPostingLine[]) {142    for (CashReversalPostingLine line : cashReversalPostingLines) {143        line.reversal = cashReversalAgreement;144    }145}146147atomic query listCashReversalPostings(key CashReversalAgreement, CashReversalPostingLine[]);