CREATE or REPLACE FUNCTION  voicemail(varchar, varchar) RETURNS VARCHAR AS '
   DECLARE
       vm record;
   BEGIN
       select into vm phoneline_forwarding.when_busy, phoneline_forwarding.when_loggedoff, phoneline_forwarding.when_noanswer
       from   phoneline left join phoneline_forwarding on (phoneline_forwarding.profile = phoneline.profile and
                                                           phoneline_forwarding.phoneline = phoneline.id)
       where  phoneline.company = ( select id from company where shortname = $1 and status = ''active'') and extension = $2 and phoneline.status <> ''deleted'';
       IF vm.when_busy = ''voicemail'' OR vm.when_loggedoff = ''voicemail'' OR vm.when_noanswer = ''voicemail'' THEN
       RETURN ''yes'';
     END IF;
     RETURN ''no'';
   END;
' LANGUAGE plpgsql;

