我正在编写一个PLSQL程序来计算分期付款系统的运行余额。当余额不等于零时,我面临剩余余额问题,我想将其调整为以前的分期付款金额
请参见以下代码,因为您可以在任何Plsql编辑器中对其进行测试
DECLARE
l_bal_flg VARCHAR2 (10) := 'Y';
l_install_amt NUMBER := 1362;
l_calcaulatedbalance NUMBER := 59024;
l_curr_bal NUMBER := 0;
l_residual_amt NUMBER := 14125;
l_installment_cnt NUMBER := 34;
l_install_seq NUMBER := 1;
BEGIN
FOR j IN 1 .. l_installment_cnt LOOP
IF l_residual_amt <> 0 THEN
IF l_installment_cnt = l_install_seq THEN
l_install_amt := l_residual_amt;
END IF;
END IF;
IF l_bal_flg = 'Y' THEN
l_curr_bal := l_calcaulatedbalance - l_install_amt;
l_bal_flg := 'N';
ELSE
l_curr_bal := l_curr_bal - l_install_amt;
END IF;
l_install_seq := l_install_seq + 1;
DBMS_OUTPUT.PUT_LINE (l_install_seq||' '||l_install_amt || ' ' || l_curr_bal);
END LOOP;
END;
输出
1 1362 57662
2 1362 56300
3 1362 54938
4 1362 53576
5 1362 52214
6 1362 50852
7 1362 49490
8 1362 48128
9 1362 46766
10 1362 45404
11 1362 44042
12 1362 42680
13 1362 41318
14 1362 39956
15 1362 38594
16 1362 37232
17 1362 35870
18 1362 34508
19 1362 33146
20 1362 31784
21 1362 30422
22 1362 29060
23 1362 27698
24 1362 26336
25 1362 24974
26 1362 23612
27 1362 22250
28 1362 20888
29 1362 19526
30 1362 18164
31 1362 16802
32 1362 15440
33 1362 14078
34 14125 -47
我要如下调整-47
到第33行
33 1362+(-47) 14078
34 14125 0
如果-47为-47或某个其他值大于1362,例如“剩余值”为1500,那么将会发生
32 1362+(-138) 15440
33 1362+(-1362) 14078
34 14125 0
答案 0 :(得分:0)
以下代码段应按照您要求的逻辑工作。
DECLARE
l_bal_flg VARCHAR2 (10) := 'Y';
l_install_amt NUMBER := 1362;
l_calcaulatedbalance NUMBER := 59024;
l_curr_bal NUMBER := 0;
l_tot_inst_amount NUMBER := 0;
l_last_bal_amount NUMBER := 0;
l_install_amount2 NUMBER := 0;
l_last_bal_amount2 NUMBER := 0;
l_residual_amt NUMBER := 14125;
l_installment_cnt NUMBER := 34;
l_install_seq NUMBER := 1;
l_bal_trans NUMBER :=0;
l_bal_trans_cnt NUMBER :=0;
BEGIN
-- amount to be balanced at last before transaction
l_tot_inst_amount := l_calcaulatedbalance-l_residual_amt;
l_last_bal_amount := round(((l_tot_inst_amount/(l_installment_cnt-1)) - l_install_amt) * (l_installment_cnt-1));
-- to get the transaction from which it has to be balanced
IF l_last_bal_amount <> 0
THEN
l_last_bal_amount2 := ABS(l_last_bal_amount);
l_install_amount2 := l_install_amt;
l_BAL_TRANS := ceil(l_last_bal_amount2/l_install_amt);
-- to get the amount to be added on first transaction to be balanced
loop
exit when l_last_bal_amount2 < l_install_amt;
l_last_bal_amount2 := l_last_bal_amount2 - l_install_amt;
end loop;
-- to get the exact transaction count on which first balance to be made
l_bal_trans_cnt :=l_installment_cnt-l_bal_trans;
END IF;
FOR j IN 1 .. l_installment_cnt LOOP
-- adjustment at the transactions to be balanced
IF l_bal_trans_cnt = l_install_seq THEN
l_install_amt := l_install_amount2+(l_last_bal_amount2*(l_last_bal_amount/ABS(l_last_bal_amount)));
elsif l_install_seq > l_bal_trans_cnt and l_last_bal_amount >0 then
l_install_amt := l_install_amount2+l_install_amount2;
END IF;
IF l_residual_amt <> 0 THEN
IF l_installment_cnt = l_install_seq THEN
l_install_amt := l_residual_amt;
END IF;
-- if balance to be paid is the residual amount then no installment to be paid for the current month
IF l_curr_bal = l_residual_amt and l_installment_cnt <> l_install_seq THEN
l_install_amt := 0;
END IF;
END IF;
IF l_bal_flg = 'Y' THEN
l_curr_bal := l_calcaulatedbalance - l_install_amt;
l_bal_flg := 'N';
ELSE
l_curr_bal := l_curr_bal - l_install_amt;
END IF;
l_install_seq := l_install_seq + 1;
DBMS_OUTPUT.PUT_LINE (l_install_seq||' '||l_install_amt || ' ' || l_curr_bal);
END LOOP;
END;
-- output:
for calculated balance 59024
34 1315 14125
35 14125 0
for calculated balance 63024
31 1362 22164
32 2591 19573
33 2724 16849
34 2724 14125
35 14125 0
for calculated balance 55024
31 1362 14164
32 39 14125
33 0 14125
34 0 14125
35 14125 0