




CREATE OR REPLACE FUNCTION  period_temp_close(i_id INTEGER)
    RETURNS  INTEGER
    
AS $BODY$
DECLARE        
    v_tmp INTEGER;
    v_btmp BOOLEAN;
    v_ret INTEGER;
    
    r_period RECORD;
    r_invlocdist RECORD;
    r_tmp RECORD;
    
    v_period_start DATE;
    v_period_end DATE;
    v_period_closed BOOLEAN;
    v_period_freeze BOOLEAN;
BEGIN

    SELECT period_start INTO v_period_end  FROM period where period_id = i_id;
    
    PERFORM closeAccountingPeriod( period_id ) FROM
        (SELECT
                period_id
            FROM
                period
            WHERE
                NOT period_closed
                AND
                period_start <= v_period_end
            ORDER BY
                period_start ASC
        ) x;
    
    
    
    
    PERFORM freezeAccountingPeriod(period_id) FROM
        (SELECT
                period_id
            FROM
                period
            WHERE
                NOT period_freeze
                AND
                period_start <= v_period_end
            ORDER BY
                period_start ASC
        ) x;
    
        --IF v_tmp < 0 THEN
        --    RAISE EXCEPTION 'open period returned %', v_tmp;
        --END IF;
    
    RETURN 0;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
  
ALTER FUNCTION  period_temp_close(  INTEGER)
  OWNER TO admin;    


