Advertisement
cmptrwz

Commcat Overdue/Lost Update

Aug 9th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Commcat Overdue/Lost Alerts
  2. -- I used ids 98/99 so that staff couldn't add the standing penalties manually.
  3. -- 98 is the overdue alert
  4. -- 99 is the lost block
  5. WITH comcat_stats AS (
  6.     SELECT
  7.         au.id AS usr,
  8.         count(DISTINCT CASE WHEN acp.status = 3 THEN acp.id ELSE NULL END) AS lost,
  9.         count(DISTINCT CASE WHEN acp.status <> 3 THEN acp.id ELSE NULL END) AS overdue,
  10.         string_agg(DISTINCT CASE WHEN acp.status = 3 THEN acp.barcode ELSE NULL END, ', ') AS lost_barcodes,
  11.         string_agg(DISTINCT CASE WHEN acp.status <> 3 THEN acp.barcode ELSE NULL END, ', ') AS overdue_barcodes
  12.     FROM action.circulation circ
  13.     JOIN asset.copy acp ON circ.target_copy = acp.id
  14.     JOIN actor.usr au ON circ.usr = au.id
  15.     WHERE
  16.         acp.circ_lib = 103
  17.         AND NOT acp.deleted
  18.         AND circ.xact_finish IS NULL
  19.         AND circ.checkin_time IS NULL
  20.         AND (acp.status = 3 OR circ.due_date < now() - '7 days'::INTERVAL)
  21.     GROUP BY au.id
  22. ), eb98 AS (
  23.     SELECT
  24.         id,
  25.         usr
  26.     FROM actor.usr_standing_penalty
  27.     WHERE standing_penalty = 98
  28. ), eb99 AS (
  29.     SELECT
  30.         id,
  31.         usr
  32.     FROM actor.usr_standing_penalty
  33.     WHERE standing_penalty = 99
  34. ), full_stats AS (
  35.     SELECT
  36.         COALESCE(cs.usr, eb98.usr, eb99.usr) AS usr,
  37.         cs.lost,
  38.         cs.overdue,
  39.         cs.lost_barcodes,
  40.         cs.overdue_barcodes,
  41.         eb98.id AS penalty_98,
  42.         eb99.id AS penalty_99
  43.     FROM comcat_stats cs
  44.     FULL OUTER JOIN eb98 ON ( cs.usr = eb98.usr )
  45.     FULL OUTER JOIN eb99 ON ( COALESCE(cs.usr, eb98.usr) = eb99.usr )
  46. ), update_98 AS (
  47.     UPDATE actor.usr_standing_penalty ausp
  48.     SET
  49.         org_unit = 1,
  50.         staff = NULL,
  51.         stop_date = NULL,
  52.         note = format(
  53.             'Patron has %1$s overdue Commonwealth Catalog item%2$s as of %3$s. Item Barcode%2$s: %4$s. If the issue has been resolved, this message can be removed from the messages tab.',
  54.             fs.overdue,
  55.             CASE WHEN fs.overdue > 1 THEN 's' ELSE '' END,
  56.             to_char(now(), 'DD Mon YYYY HH12:MI AM'),
  57.             fs.overdue_barcodes
  58.         )
  59.     FROM full_stats fs
  60.     WHERE fs.penalty_98 = ausp.id AND fs.overdue IS NOT NULL AND fs.overdue > 0
  61.     RETURNING id
  62. ), update_99 AS (
  63.     UPDATE actor.usr_standing_penalty ausp
  64.     SET
  65.         org_unit = 1,
  66.         staff = NULL,
  67.         stop_date = NULL,
  68.         note = format(
  69.             'Patron has %1$s LOST Commonwealth Catalog item%2$s as of %3$s. Item Barcode%2$s: %4$s. If the issue has been resolved, this message can be removed from the messages tab.',
  70.             fs.lost,
  71.             CASE WHEN fs.lost > 1 THEN 's' ELSE '' END,
  72.             to_char(now(), 'DD Mon YYYY HH12:MI AM'),
  73.             fs.lost_barcodes
  74.         )
  75.     FROM full_stats fs
  76.     WHERE fs.penalty_99 = ausp.id AND fs.lost IS NOT NULL AND fs.lost > 0
  77.     RETURNING id
  78. ), insert_98 AS (
  79.     INSERT INTO actor.usr_standing_penalty (org_unit, usr, standing_penalty, note)
  80.     SELECT
  81.         1,
  82.         usr,
  83.         98,
  84.         format(
  85.             'Patron has %1$s overdue Commonwealth Catalog item%2$s as of %3$s. Item Barcode%2$s: %4$s. If the issue has been resolved, this message can be removed from the messages tab.',
  86.             overdue,
  87.             CASE WHEN overdue > 1 THEN 's' ELSE '' END,
  88.             to_char(now(), 'DD Mon YYYY HH12:MI AM'),
  89.             overdue_barcodes
  90.         )
  91.     FROM full_stats
  92.     WHERE overdue > 0 AND penalty_98 IS NULL
  93.     RETURNING id
  94. ), insert_99 AS (
  95.     INSERT INTO actor.usr_standing_penalty (org_unit, usr, standing_penalty, note)
  96.     SELECT
  97.         1,
  98.         usr,
  99.         99,
  100.         format(
  101.             'Patron has %1$s LOST Commonwealth Catalog item%2$s as of %3$s. Item Barcode%2$s: %4$s. If the issue has been resolved, this message can be removed from the messages tab.',
  102.             lost,
  103.             CASE WHEN lost > 1 THEN 's' ELSE '' END,
  104.             to_char(now(), 'DD Mon YYYY HH12:MI AM'),
  105.             lost_barcodes
  106.         )
  107.     FROM full_stats
  108.     WHERE lost > 0 AND penalty_99 IS NULL
  109.     RETURNING id
  110. ), delete_old AS (
  111.     DELETE FROM actor.usr_standing_penalty ausp
  112.     USING full_stats fs
  113.     WHERE
  114.         (ausp.standing_penalty = 98 AND fs.penalty_98 = ausp.id AND (fs.overdue IS NULL OR fs.overdue = 0))
  115.         OR
  116.         (ausp.standing_penalty = 99 AND fs.penalty_99 = ausp.id AND (fs.lost IS NULL OR fs.lost = 0))
  117.     RETURNING id
  118. )
  119. select
  120.     count(DISTINCT u98) AS update_overdue,
  121.     count(DISTINCT u99) AS update_lost,
  122.     count(DISTINCT i98) AS insert_overdue,
  123.     count(DISTINCT i99) AS insert_lost,
  124.     count(DISTINCT del) AS deleted_old
  125. FROM
  126.     update_98 u98
  127.     FULL OUTER JOIN update_99 u99 ON true
  128.     FULL OUTER JOIN insert_98 i98 ON true
  129.     FULL OUTER JOIN insert_99 i99 ON true
  130.     FULL OUTER JOIN delete_old del ON true;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement