CREATE procedure [dbo].[proc_web_cek_polis_permission] 
  @pi_no_polis varchar(11),
  @pi_id_user  varchar(4)
AS
BEGIN
  DECLARE @p_id_unit_pelayanan varchar(4),
  @hasil varchar(1)
  
	SELECT @p_id_unit_pelayanan = id_unit_pelayanan 
	FROM user_unit_pelayanan
	WHERE id_user = @pi_id_user
  
	IF (@p_id_unit_pelayanan = '0000')
	  BEGIN
	    -- semua
	    SET @hasil = '1'
	  END
	ELSE
	  BEGIN
	    -- tambah WHERE
	    if (exists(
	      SELECT id_polis 
	      FROM polis 
	      WHERE
	        (no_polis = @pi_no_polis) AND
	        (id_polis = dbo.func_get_id_polis_per_tanggal(no_polis, dbo.func_fixed_date(getdate()), 0)) AND
	        (
					  (coalesce(id_unit_pelayanan_produksi,'') IN (
					    SELECT id_unit_pelayanan FROM dbo.func_get_hirarki_unit_pelayanan(@p_id_unit_pelayanan)))
					  OR
					  (coalesce(id_unit_pelayanan_korespondensi,'') IN (
					    SELECT id_unit_pelayanan from dbo.func_get_hirarki_unit_pelayanan(@p_id_unit_pelayanan)))
	        )
			))
			  SET @hasil = '1'
	    ELSE
		    SET @hasil = '0'
	  END
	RETURN @hasil
END