declare
     p_ijan afm02.ijan%type;
     p_cucd afm03.cucd%type;
     p_dat date;

     p_cutaxcls varchar(10);
     p_cutax afm03.cutax%type;

     w_gdbcst number := 0;
begin

     p_ijan := :ijan;
     p_cucd := :cucd;
     p_dat  := to_date(:dat,'yyyy/mm/dd');

     p_cutaxcls := :cutaxcls;
     p_cutax    := :cutax;

    /*Formsほぼそのまま*/
	if p_ijan is not null then

		if p_cucd is not null then


			if nvl(p_cutaxcls,'0') = '1' and nvl(p_cutax,'0') = '1' then			
				begin
					select
						case when p_dat is not null and gdspcst is not null
								and (gdspst is null or gdspst<=p_dat)
								and (gdspen is null or gdspen>=p_dat) then gdspcst
						else gdbcst1 end into w_gdbcst
						from afm0311
						where cucd=p_cucd and ijan=p_ijan;
				exception
				when NO_DATA_FOUND then
					w_gdbcst := null;
				end;
			else
				begin
					select
						case when p_dat is not null and gdspcst is not null
								and (gdspst is null or gdspst<=p_dat)
								and (gdspen is null or gdspen>=p_dat) then gdspcst
						else gdbcst end into w_gdbcst
						from afm0311
						where cucd=p_cucd and ijan=p_ijan;
				exception
				when NO_DATA_FOUND then
					w_gdbcst := null;
				end;
			end if;
		end if;

	end if;

     :gdbcst   := w_gdbcst;
end;

