sql function to remove all traces of a PO. This is for testing/development purposes...
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Tue, 14 Apr 2009 16:35:35 +0000 (16:35 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Tue, 14 Apr 2009 16:35:35 +0000 (16:35 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@12869 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/support-scripts/test-scripts/purge_po.sql [new file with mode: 0644]

diff --git a/Open-ILS/src/support-scripts/test-scripts/purge_po.sql b/Open-ILS/src/support-scripts/test-scripts/purge_po.sql
new file mode 100644 (file)
index 0000000..6343b35
--- /dev/null
@@ -0,0 +1,25 @@
+-- Testing purposes only
+-- Removes all traces of a purchase order, including the PO, lineitems, 
+-- lineitem_details, bibs, copies, callnumbers, and debits
+
+CREATE OR REPLACE FUNCTION acq.purge_po (po_id INT) RETURNS VOID AS $$
+DECLARE
+    li RECORD;
+BEGIN
+    FOR li IN SELECT * FROM acq.lineitem WHERE purchase_order = po_id LOOP
+
+        DELETE FROM asset.copy WHERE call_number IN (
+            SELECT id FROM asset.call_number WHERE record = li.eg_bib_id);
+        DELETE FROM asset.call_number WHERE record = li.eg_bib_id;
+        DELETE FROM biblio.record_entry WHERE id = li.eg_bib_id;
+
+        DELETE FROM acq.fund_debit WHERE id in (
+            SELECT fund_debit FROM acq.lineitem_detail WHERE lineitem = li.id);
+        DELETE FROM acq.lineitem_detail WHERE lineitem = li.id;
+        DELETE FROM acq.lineitem_attr WHERE lineitem = li.id;
+        DELETE from acq.lineitem WHERE id = li.id;
+    END LOOP;
+
+    DELETE FROM acq.purchase_order WHERE id = po_id;
+END;
+$$ LANGUAGE plpgsql;