--- /dev/null
+-- Revert kcls-evergreen:backstage-exports-continued from pg
+
+BEGIN;
+
+-- XXX Add DDLs here.
+
+CREATE FUNCTION public.export_ids_cat_date(record_to_check bigint, start_date date, end_date date) RETURNS boolean
+ LANGUAGE plpgsql STABLE
+ AS $$
+
+-- Returns true if a record has a Cat Date that falls between start_date and end_date inclusive.
+DECLARE
+ v_cat_date TIMESTAMP WITH TIME ZONE;
+BEGIN
+
+ IF start_date IS NULL OR end_date IS NULL THEN
+ RETURN TRUE;
+ ELSE
+ SELECT cataloging_date INTO v_cat_date
+ FROM biblio.record_entry
+ WHERE id = record_to_check;
+
+ IF v_cat_date >= start_date AND v_cat_date <= end_date THEN
+ RETURN TRUE;
+ ELSE
+ RETURN FALSE;
+ END IF;
+ END IF;
+END $$;
+
+CREATE FUNCTION public.export_ids_001(record_to_check bigint) RETURNS boolean
+ LANGUAGE plpgsql STABLE
+ AS $$
+
+-- Returns true if the record has at least one 001 field and at least one of those fields should contain “oc”
+-- OR has at least one 035 field with "WaOLN" in subfield "a"
+DECLARE
+ v_row_count BIGINT;
+BEGIN
+
+ SELECT count(*) INTO v_row_count
+ FROM metabib.real_full_rec
+ WHERE ((tag = '001' AND (value ILIKE 'oc%' OR value ILIKE 'on%' OR value ILIKE 'wln%'))
+ OR (tag = '035' AND subfield ILIKE 'a' AND value ILIKE '%WaOLN%'))
+ AND record = record_to_check;
+
+ IF v_row_count > 0 THEN
+ RETURN TRUE;
+ ELSE
+ RETURN FALSE;
+ END IF;
+
+END $$;
+
+CREATE FUNCTION public.export_ids_086_092_099(record_to_check bigint) RETURNS boolean
+ LANGUAGE plpgsql STABLE
+ AS $$
+
+-- Returns true if a record has at least one of the following fields 086 or 092 or 099 and every instance
+-- of each of these fields must not contain “ON ORDER”
+DECLARE
+ v_row_count BIGINT;
+BEGIN
+
+ SELECT count(*) INTO v_row_count
+ FROM metabib.real_full_rec
+ WHERE (tag = '086' OR tag = '092' OR tag = '099') AND value ILIKE '%on order%' AND record = record_to_check;
+
+ IF v_row_count > 0 THEN
+ RETURN FALSE;
+ END IF;
+
+ SELECT count(*) INTO v_row_count
+ FROM metabib.real_full_rec
+ WHERE (tag = '086' OR tag = '092' OR tag = '099') AND value NOT ILIKE '%on order%' AND record = record_to_check;
+
+ IF v_row_count > 0 THEN
+ RETURN TRUE;
+ ELSE
+ RETURN FALSE;
+ END IF;
+
+END $$;
+
+CREATE FUNCTION public.export_ids_998(record_to_check bigint) RETURNS boolean
+ LANGUAGE plpgsql STABLE
+ AS $$
+
+-- Returns true if a record has items attached OR the record must have at least one 998 field with subfield d that has
+-- one of the following values ‘d’ or ‘t’ or ‘v’ or ‘w’ or ‘x’ or ‘y’ or ‘1’
+DECLARE
+ v_row_count BIGINT;
+BEGIN
+
+ SELECT count(*) INTO v_row_count
+ FROM metabib.real_full_rec
+ WHERE tag = '998'
+ AND subfield = 'd'
+ AND value IN ('d','t','v','w','x','y','1')
+ AND record = record_to_check;
+
+ IF v_row_count > 0 OR public.export_ids_has_copy(record_to_check) THEN
+ RETURN TRUE;
+ ELSE
+ RETURN FALSE;
+ END IF;
+END $$;
+
+CREATE FUNCTION public.export_generate_ids(start_date date, end_date date) RETURNS SETOF bigint
+ LANGUAGE sql STABLE
+ AS $$
+-- Generates a list of ids for exporting based on the following criteria
+-- For a record to be exported it MUST MEET ALL of the following conditions
+-- 1.The record must have at least one LDR field and byte 05 of every instance of that field must not be ‘d’.
+-- AND
+-- 2. The record must have at least one 001 field and at least one of those fields should contain “oc”
+-- OR has at least one 035 field with "WaOLN" in subfield "a"
+-- AND
+-- 3. The record must have at least one of the following fields 086 or 092 or 099 and every instance of each of these fields must not contain “ON ORDER”
+-- AND
+-- 4. The record must have items attached OR the record must have at least one 998 field with subfield d that has one of the following values ‘d’ or ‘t’ or ‘v’ or ‘w’ or ‘x’ or ‘y’ or ‘1’
+-- AND
+-- 5. The records Cat Date must fall between two dates supplied by the user.
+
+
+ SELECT id
+ FROM biblio.record_entry
+ WHERE public.export_ids_cat_date(id, start_date, end_date) AND public.export_ids_LDR(id) AND public.export_ids_001(id)
+ AND public.export_ids_086_092_099(id) AND public.export_ids_998(id);
+$$;
+
+
+COMMIT;