Advertisement
chrissharp123

Untitled

Apr 4th, 2024
1,361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE OR REPLACE FUNCTION csharp.migrate_call_numbers(source_ou INT, dest_ou INT) RETURNS INT AS $func$
  2. DECLARE
  3.     source_org  actor.org_unit%ROWTYPE;
  4.     dest_org    actor.org_unit%ROWTYPE;
  5.     source_cn   asset.call_number%ROWTYPE;
  6.     target_cn   asset.call_number%ROWTYPE;
  7.     hold        action.hold_request%ROWTYPE;
  8.  
  9. BEGIN
  10.  
  11.     SELECT INTO source_org *
  12.         FROM actor.org_unit
  13.         WHERE id = source_ou;
  14.  
  15.     SELECT INTO dest_org *
  16.         FROM actor.org_unit
  17.         WHERE id = dest_ou;
  18.  
  19.     -- Find call numbers attached to the source org ...
  20.     FOR source_cn IN
  21.         SELECT *
  22.             FROM asset.call_number
  23.             WHERE owning_lib = source_org.id
  24.             AND NOT deleted
  25.     LOOP
  26.  
  27.     SELECT INTO target_cn *
  28.         FROM  asset.call_number
  29.         WHERE label = source_cn.label
  30.         AND prefix = source_cn.prefix
  31.         AND suffix = source_cn.suffix
  32.         AND owning_lib = dest_org.id
  33.         AND record = source_cn.record
  34.         AND NOT deleted;
  35.  
  36.     -- ... and if there's a conflicting one on the target ...                                                  
  37.     IF FOUND THEN
  38.  
  39.             -- ... move the copies to that, and ...                                                            
  40.             UPDATE  asset.copy
  41.               SET   call_number = target_cn.id
  42.               WHERE call_number = source_cn.id;
  43.  
  44.             -- ... move V holds to the move-target call number                                                  
  45.             FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
  46.  
  47.                     UPDATE  action.hold_request
  48.                       SET   target = target_cn.id
  49.                       WHERE id = hold.id;
  50.  
  51.             END LOOP;
  52.  
  53.             UPDATE asset.call_number SET deleted = TRUE WHERE id = source_cn.id;
  54.  
  55.     -- ... if not ...                                                                                          
  56.     ELSE
  57.             -- ... just move the call number to the target org
  58.             UPDATE  asset.call_number
  59.               SET   owning_lib = dest_org.id
  60.               WHERE id = source_cn.id;
  61.     END IF;
  62.     END LOOP;
  63.     RETURN 1;
  64. END;
  65. $func$ LANGUAGE plpgsql;
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement