set search_path = bbrk;

create or replace function sp_payment_iscash(apaymentid bigint) returns boolean as
$BODY$
begin
  return exists(select * from loan_Payments lp
                left join loan_payment_detail_nupay lpdn on lpdn.loan_payment_id = lp.DKey
                left join loan_payment_detail_bank_deposit lpdbd on lpdbd.loan_payment_id = lp.DKey
                left join loan_payment_detail_mctdebit lpdmct on lpdmct.loan_payment_id = lp.DKey
                left join loan_payment_detail_cashbook lpdcb on lpdcb.loan_payment_id = lp.DKey
                where lp.DKey = apaymentid and (lp.Type = 0 or lpdn.loan_payment_id = apaymentid or lpdbd.loan_payment_id = apaymentid or lpdmct.loan_payment_id = apaymentid or lpdcb.loan_payment_id = apaymentid) and lp.Payment <> 0
               );
end;
$BODY$
immutable
language plpgsql;
