CREATE FUNCTION "DBA"."get_eak_totalvatsum"( in a_id_dk_earve integer, in a_id_dk_earve_sum integer )
RETURNS decimal(20,2)
BEGIN
  declare ldc_return decimal(20,2);
  select totalvatsum into ldc_return from "dk_earve_sum" where "dk_earve_sum"."id_dk_earve"=a_id_dk_earve and "dk_earve_sum"."id"=a_id_dk_earve_sum;
  if ldc_return is null then
    set ldc_return=0
  end if;
  if ldc_return = 0 then
    select sum(cast(trim("dk_earve_ext"."text3") as decimal(20,2))) into ldc_return from "dk_earve_ext" where "dk_earve_ext"."id_dk_earve"=a_id_dk_earve and "dk_earve_ext"."id_dk_earve_sum"=a_id_dk_earve_sum and "dk_earve_ext"."parent"='InvoiceSumGroup' and "dk_earve_ext"."name"='Extension' and "dk_earve_ext"."text1"='TotalVATSum';
  end if;
  if ldc_return is null then
    set ldc_return=0
  end if;
  return ldc_return;
END