create or replace function f_get_rqa(plpcid number, deliverydate date)
  return number is
  res            number;
  v_plpcid       number;
  v_deliverydate date;
begin
  v_plpcid       := plpcid;
  v_deliverydate := deliverydate;
  select nvl(sum(RQA.QUANTITY), 0)
    into res
    from RENTALQTYADJUSTMENT RQA
   where (select count(1)
            from STANDARDDELIVERYLINE S
           where S.PRODUCTLISTPERDISTRIBPOINT_ID =
                 rqa.productlistperdistribpoint_id
             and S.PRODUCTLISTPERCUSTOMER_ID = v_plpcid) > 0
     and rqa.adjustmentdate <= v_deliverydate;
  return res;
end f_get_rqa;
/
