3111
SQL Sprint
27/04/2025
create or replace function FTBH_MGIU_GLAI(
b_ma_dvi varchar2,b_so_id number,b_lh_nv varchar2,b_ma_dt varchar2,
b_nt_ta varchar2,b_do_tl number,b_ve_tl number,b_ngay number) return number
AS
b_i1 number; b_ma_nt varchar2(5); b_kieu varchar2(3):='C';
b_pt number; b_hs_gl number; b_tp number:=0; b_glai number:=0;
begin
select nvl(min(glai),0) into b_glai from tbh_mgiu_nv where so_id=b_so_id and lh_nv=b_lh_nv and ma_dt in(' ',b_ma_dt);
if b_glai=0 then return 0; end if;
b_ma_nt:=FTBH_MGIU_NT(b_so_id);
if b_ma_nt<>'VND' then b_tp:=2; end if;
if b_do_tl<>0 then
select nvl(min(pt),0) into b_i1 from tbh_mgiu_do where so_id=b_so_id and pt>=b_do_tl;
if b_i1<>0 then
select hs_gl into b_hs_gl from tbh_mgiu_do where so_id=b_so_id and pt=b_i1;
b_glai:=round(b_glai*b_hs_gl/100,b_tp);
end if;
end if;
if b_ve_tl<>0 then
select nvl(min(pt),0) into b_i1 from tbh_mgiu_ta where so_id=b_so_id and pt>=b_ve_tl;
if b_i1<>0 then
select hs_gl into b_hs_gl from tbh_mgiu_ta where so_id=b_so_id and pt=b_i1;
b_glai:=round(b_glai*b_hs_gl/100,b_tp);
end if;
end if;
if b_nt_ta=b_ma_nt then
b_glai:=FTT_TUNG_QD(b_ma_dvi,b_ngay,b_ma_nt,b_glai,b_nt_ta);
end if;
return b_glai;
end;