Advertisement
chrissharp123

Untitled

Apr 24th, 2025
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH x AS ( -- begin WITH
  2.     SELECT au.*
  3.     --into actor.usrs_to_delete_2024_09_20
  4.     FROM actor.usr au
  5.     JOIN permission.grp_tree grp ON (au.profile = grp.id)
  6.     WHERE
  7.  
  8.     ( -- begin OR
  9.  
  10.         ( -- begin regular patrons
  11.         grp.name in (
  12.         'Digital Only',
  13.         'GLS',
  14.         'Homebound',
  15.         'NonResident',
  16.         'OutOfState',
  17.         'Patron',
  18.         'Quipu',
  19.         'Restricted',
  20.         'Temp',
  21.         'TempRes12',
  22.         'TempRes6',
  23.         'Trustee'
  24.         )
  25.  
  26.         AND date(au.expire_date) < date(now()) - '4 years'::interval
  27.         -- no owed money in either direction and no payment within the last 4 years
  28.         AND NOT EXISTS (
  29.             SELECT 1
  30.                 FROM money.materialized_billable_xact_summary mmbxs
  31.                 WHERE mmbxs.usr = au.id
  32.                 AND (
  33.                     balance_owed <> '0.00' OR (now() - last_payment_ts) < '4 years'::interval)
  34.             )
  35.         -- no activity entries within the last 4 years
  36.         AND NOT EXISTS (
  37.             SELECT 1
  38.                 FROM actor.usr_activity aua
  39.                 WHERE aua.usr = au.id
  40.                 AND (now() - event_time) < '4 years'::interval
  41.             )
  42.         -- no accounts created within the last 4 years
  43.         AND date(au.create_date) < date(now()) - '4 years'::interval
  44.         ) -- end regular patrons
  45.  
  46.     OR
  47.  
  48.         ( -- begin PLAY cards
  49.         -- PLAY Cards are deleted one year after expire date\
  50.         -- They can't owe any money.
  51.         grp.name = 'PLAY Card'
  52.         AND NOT EXISTS (
  53.             SELECT 1
  54.                 FROM money.materialized_billable_xact_summary mmbxs
  55.                 WHERE mmbxs.usr = au.id
  56.                 AND balance_owed <> '0.00'
  57.             )
  58.         AND date(au.expire_date) < date(now()) - '1 year'::interval
  59.         ) -- end PLAY cards
  60.  
  61.     ) -- end OR
  62.  
  63.     AND au.deleted = false
  64.     -- consider open circs
  65.     AND NOT EXISTS (
  66.         SELECT 1
  67.             FROM action.circulation circ
  68.             WHERE circ.usr = au.id
  69.             AND xact_finish is null
  70.     )
  71.     -- not in collections - PATRON_IN_COLLECTIONS standing penalty
  72.     AND NOT EXISTS (
  73.         SELECT 1
  74.             FROM actor.usr_standing_penalty usp
  75.             WHERE usp.usr = au.id
  76.             AND usp.standing_penalty = 30
  77.     )
  78. ) -- end WITH
  79. SELECT actor.usr_delete(u.id, NULL) FROM actor.usr u WHERE u.id in (SELECT id FROM x)
  80. ;
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement