CREATE OR REPLACE FuncTion ft_getlncontintrate( as_coopid char , as_contno varchar , adtm_operate date ) return number is ldc_intrate number; begin if as_coopid is null or as_contno is null or adtm_operate is null then return 0.00; end if; /* int_continttype 1 -> fix ตามสัญญาอ่านที่ int_contintrate 2 -> fix ตามตารางดูจาก int_continttabcode ไปที่ตาราง lccfloanintratedet 3 -> fix ตามตารางอัตราพิเศษ lccontintspc 4 -> คิดจากตารางเงินฝาก int_contintrate คืออัตราดอกเบี้ยที่คิดตามสัญญา int_contintincrease คืออัตราดอกเบี้ยที่เพิ่มลดจากตารางดอกเบี้ย int_intsteptype คือการคิดยอดจากอะไร 1 คิดจากยอดอนุมัติ 2 คิดจากยอดคงเหลือ(ส่วนใหญ่จะใช้กรณีที่อัตราดอกเบี้ยเป็นขั้นบันได) */ select ( case lm.int_continttype when 1 then nvl( lm.int_contintrate , 0 ) when 2 then ft_getlnintrate( lm.coop_id, lm.int_continttabcode, case lm.int_intsteptype when 1 then lm.loanapprove_amt else lm.principal_balance end, adtm_operate ) + nvl( lm.int_contintincrease, 0 ) when 3 then ( select case lsp.int_continttype when 1 then nvl( lsp.int_contintrate, 0 ) when 2 then ft_getlnintrate( lsp.coop_id, lsp.int_continttabcode, case lm.int_intsteptype when 1 then lm.loanapprove_amt else lm.principal_balance end, adtm_operate ) + nvl( lsp.int_contintincrease, 0 ) else 0 end from lncontintspc lsp where lsp.coop_id = lm.coop_id and lsp.loancontract_no = lm.loancontract_no and adtm_operate between lsp.effective_date and lsp.expire_date ) else 0 end ) as intrate into ldc_intrate from lncontmaster lm where lm.coop_id = as_coopid and lm.loancontract_no = as_contno; return ldc_intrate; end ; /