CREATE FUNCTION "DBA"."get_eak_invoicediscount"( in a_id_dk_earve integer, in a_id_dk_earve_sum integer )
RETURNS decimal(20,2)
BEGIN
  declare ldc_discount decimal(20,2);
  select sum(if "dk_earve_ext"."text1"='DSC' then -1*abs(cast(trim("dk_earve_ext"."text4") as decimal(20,2))) else abs(cast(trim("dk_earve_ext"."text4") as decimal(20,2))) endif) into ldc_discount 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"='Addition';
  if ldc_discount is null then
    set ldc_discount=0
  end if;
  return ldc_discount;
END