
 
CREATE OR REPLACE FUNCTION apopen_isreversed(integer, text, text)
    RETURNS  BOOLEAN
    
AS $BODY$
DECLARE        
    i_apopen_id  ALIAS FOR $1;
    i_docnumber ALIAS FOR $2;
    i_doctype ALIAS FOR $3;
    v_posted BOOLEAN;
    
BEGIN
    --RAISE NOTICE 'test %', i_docnumber ;
    IF i_doctype != 'C' THEN
      --  RAISE NOTICE 'original not posted';
        RETURN FALSE;
    END IF;

    -- posted = true or null
    
    SELECT
        apopen_posted
    INTO
        v_posted
    FROM
        apopen
    WHERE
        apopen_id != i_apopen_id
        AND
        apopen_docnumber = i_docnumber
        AND
        apopen_doctype = 'V';
    
    IF NOT FOUND THEN
    --    RAISE NOTICE 'no match found';
        RETURN FALSE;
    END IF;
    
    --RAISE NOTICE 'match is not posted';
    RETURN TRUE;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
  
ALTER FUNCTION apopen_isreversed(integer, text, text)
  OWNER TO admin;

-- test..
--select apopen_isposted(10088,'NSVB-4668-92170', null);
