create or replace FuncTion ft_RoundMoney( adc_money number , as_coopid char , as_applgroup char , as_function varchar2 ) return number is ls_satangtyp char(2); ls_roundtyp char(1); li_trunamt number(1,0); li_roundamt number(1,0); ldc_fac number(3,2); ldc_rdamt number(3,2); ldc_point1 number(3,2); ldc_point2 number(3,2); ldc_integer number(20,7); ldc_money number(20,7); begin ldc_money := adc_money; /* select satang_type , truncate_pos_amt , round_type , round_pos_amt into ls_satangtyp , li_trunamt , ls_roundtyp , li_roundamt from cmroundmoney where upper(branch_id) = upper(as_branchid) and upper(applgroup_code) = upper(as_applgroup) and upper(function_code) = upper(as_function) and use_flag = 1*/ select satang_type , truncate_pos_amt , round_type , round_pos_amt into ls_satangtyp , li_trunamt , ls_roundtyp , li_roundamt from ( select satang_type , truncate_pos_amt , round_type , round_pos_amt , 2 as sort from cmroundmoney where coop_id = as_coopid and applgroup_code = as_applgroup and function_code = 'ALL' and use_flag = 1 union select satang_type , truncate_pos_amt , round_type , round_pos_amt , 1 as sort from cmroundmoney where coop_id = as_coopid and applgroup_code = as_applgroup and function_code = as_function and use_flag = 1 ) cmrd where rownum = 1 order by sort; /*Exception When no_data_found then Begin dbms_output.put_line('no_data_found'); select satang_type , truncate_pos_amt , round_type , round_pos_amt into ls_satangtyp , li_trunamt , ls_roundtyp , li_roundamt from cmroundmoney where upper(branch_id) = upper(as_branchid) and upper(applgroup_code) = upper(as_applgroup) and upper(function_code) = 'ALL' and use_flag = 1; End; When others then Begin dbms_output.put_line('others'); End;*/ dbms_output.put_line('coop_id='||as_coopid); dbms_output.put_line('applgroup_code='||as_applgroup); dbms_output.put_line('function_code='||as_function); dbms_output.put_line('satang_type='||ls_satangtyp); dbms_output.put_line('truncate_pos_amt='||li_trunamt); dbms_output.put_line('round_type='||ls_roundtyp); dbms_output.put_line('round_pos_amt='||li_roundamt); ldc_money := trunc( ldc_money , li_trunamt ); dbms_output.put_line('Money_trunc(0)='||ldc_money); For li_trunct In REVERSE li_roundamt..li_trunamt Loop ldc_money := round( ldc_money , li_trunct ); dbms_output.put_line('Money_trunc('||li_trunct||')='||ldc_money); End Loop; ldc_integer := trunc( ldc_money , 0 ); ldc_fac := ldc_money - ldc_integer; dbms_output.put_line('Fac='||ldc_fac); ldc_point1 := trunc( ldc_fac , 1 ); dbms_output.put_line('Point1='||ldc_point1); ldc_point2 := ldc_fac - ldc_point1; dbms_output.put_line('Point2='||ldc_point2); Case ls_satangtyp When '00' Then /*ไม่ปัดเฟ้ย*/ return ldc_money; When '10' Then /*ปัดขึ้นเต็ม 5 สตางค์*/ If ldc_point2 = 0.00 Or ldc_point2 = 0.05 Then Return ldc_money; Elsif ldc_point2 >= 0.01 and ldc_point2 <= 0.04 Then ldc_point2 := 0.05; Elsif ldc_point2 >= 0.06 and ldc_point2 <= 0.09 Then ldc_point2 := 0.1; End if; dbms_output.put_line('10Point1='||ldc_point1); dbms_output.put_line('10Point2='||ldc_point2); ldc_rdamt := ldc_point1 + ldc_point2; When '11' Then /*ปัดขึ้นเต็ม 10 สตางค์*/ If ldc_point2 = 0.00 Then Return ldc_money; Else ldc_point2 := 0.1; End if; ldc_rdamt := ldc_point1 + ldc_point2; When '12' Then /*ปัดขึ้นเต็ม 25 สตางค์*/ If ldc_fac >= 0.01 and ldc_fac <= 0.25 Then ldc_rdamt := 0.25; Elsif ldc_fac >= 0.26 and ldc_fac <= 0.50 Then ldc_rdamt := 0.50; Elsif ldc_fac >= 0.51 and ldc_fac <= 0.75 Then ldc_rdamt := 0.75; Elsif ldc_fac >= 0.76 and ldc_fac <= 0.99 Then ldc_rdamt := 1; Else ldc_rdamt := 0; End if; When '13' Then /*ปัดขึ้นเต็ม 50 สตางค์*/ If ldc_fac >= 0.01 and ldc_fac <= 0.50 Then ldc_rdamt := 0.50; Elsif ldc_fac >= 0.51 and ldc_fac <= 0.99 Then ldc_rdamt := 1; Else ldc_rdamt := 0; End if; When '14' Then /*ปัดขึ้นเต็ม 1 บาท*/ If ldc_fac > 0.00 Then ldc_rdamt := 1; Else ldc_rdamt := 0; End if; When '48' then /* ปัดแบบ 5/4 เต็ม0.05บาท ต่ำกว่า 0.05 ปัดทิ้ง ตั้งแต่ 0.05 ขี้นไป ปัด 0.10 บาท */ if ldc_point2 < 0.05 Then ldc_point2 := 0; Elsif ldc_point2 = 0.05 then ldc_point2 := 0.05; Else ldc_point2 := 0.10; End if; ldc_rdamt := ldc_point1 + ldc_point2; When '49' then /* ปัดแบบ 5/4 เต็ม1บาท ต่ำกว่า 0.50 ปัดทิ้ง ตั้งแต่ 0.50 ขี้นไป ปัดเต็มบาท */ if ldc_fac < 0.50 Then ldc_rdamt := 0; Else ldc_rdamt := 1; End if; When '50' Then /*ปัดลงเต็ม 5 สตางค์*/ If ldc_point2 = 0.00 Or ldc_point2 = 0.05 Then Return ldc_money; Elsif ldc_point2 >= 0.01 and ldc_point2 <= 0.04 Then ldc_point2 := 0.00; Elsif ldc_point2 >= 0.06 and ldc_point2 <= 0.09 Then ldc_point2 := 0.05; End if; dbms_output.put_line('51Point1='||ldc_point1); dbms_output.put_line('51Point2='||ldc_point2); ldc_rdamt := ldc_point1 + ldc_point2; When '51' Then /*ปัดลงเต็ม 10 สตางค์*/ If ldc_point2 = 0.00 Then Return ldc_money; Else ldc_point2 := 0.00; End if; ldc_rdamt := ldc_point1 + ldc_point2; When '52' Then /*ปัดลงเต็ม 25 สตางค์*/ If ldc_fac >= 0.01 and ldc_fac <= 0.25 Then ldc_rdamt := 0.00; Elsif ldc_fac >= 0.26 and ldc_fac <= 0.50 Then ldc_rdamt := 0.25; Elsif ldc_fac >= 0.51 and ldc_fac <= 0.75 Then ldc_rdamt := 0.50; Elsif ldc_fac >= 0.76 and ldc_fac <= 0.99 Then ldc_rdamt := 0.75; Else ldc_rdamt := 0; End if; When '53' Then /*ปัดลงเต็ม 50 สตางค์*/ If ldc_fac >= 0.01 and ldc_fac <= 0.50 Then ldc_rdamt := 0.00; Elsif ldc_fac >= 0.51 and ldc_fac <= 0.99 Then ldc_rdamt := 0.50; Else ldc_rdamt := 0; End if; When '54' Then /*ปัดลงเต็ม 1 บาท*/ ldc_rdamt := 0; Else ldc_rdamt := ldc_fac; End Case; ldc_integer := ldc_integer + ldc_rdamt; return ldc_integer; end ; /