]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/support-scripts/test-scripts/purge_po.sql
Fix empty statuses filter
[working/Evergreen.git] / Open-ILS / src / support-scripts / test-scripts / purge_po.sql
1 -- Testing purposes only
2 -- Removes all traces of a purchase order, including the PO, lineitems, 
3 -- lineitem_details, bibs, copies, callnumbers, and debits
4
5 CREATE OR REPLACE FUNCTION acq.purge_po (po_id INT, purge_items BOOLEAN) RETURNS VOID AS $$
6 DECLARE
7     li RECORD;
8 BEGIN
9     FOR li IN SELECT * FROM acq.lineitem WHERE purchase_order = po_id LOOP
10
11         DELETE FROM asset.copy WHERE call_number IN (
12             SELECT id FROM asset.call_number WHERE record = li.eg_bib_id);
13         DELETE FROM asset.call_number WHERE record = li.eg_bib_id;
14         DELETE FROM biblio.record_entry WHERE id = li.eg_bib_id;
15
16         DELETE FROM acq.fund_debit WHERE id in (
17             SELECT fund_debit FROM acq.lineitem_detail WHERE lineitem = li.id);
18
19         IF li.picklist IS NOT NULL THEN
20             IF purge_items THEN
21                 DELETE FROM acq.lineitem_detail WHERE lineitem = li.id;
22             ELSE
23                 UPDATE acq.lineitem_detail SET eg_copy_id = NULL, fund_debit = NULL WHERE lineitem = li.id;
24             END IF;
25             UPDATE acq.lineitem SET purchase_order = NULL, eg_bib_id = NULL, state = 'new' WHERE id = li.id;
26         ELSE
27             DELETE FROM acq.lineitem_detail WHERE lineitem = li.id;
28             DELETE FROM acq.lineitem_attr WHERE lineitem = li.id;
29             DELETE from acq.lineitem_note WHERE lineitem = li.id;
30             DELETE from acq.lineitem WHERE id = li.id;
31         END IF;
32
33     END LOOP;
34
35     DELETE FROM acq.purchase_order WHERE id = po_id;
36 END;
37 $$ LANGUAGE plpgsql;