3 SELECT evergreen.upgrade_deps_block_check('1208', :eg_version);
5 CREATE OR REPLACE FUNCTION action.emergency_closing_stage_2_circ ( circ_closing_entry INT )
8 circ action.circulation%ROWTYPE;
9 e_closing action.emergency_closing%ROWTYPE;
10 e_c_circ action.emergency_closing_circulation%ROWTYPE;
11 closing actor.org_unit_closed%ROWTYPE;
12 adjacent actor.org_unit_closed%ROWTYPE;
13 bill money.billing%ROWTYPE;
14 last_bill money.billing%ROWTYPE;
16 hoo_close TIME WITHOUT TIME ZONE;
22 -- Gather objects involved
23 SELECT * INTO e_c_circ
24 FROM action.emergency_closing_circulation
25 WHERE id = circ_closing_entry;
27 IF e_c_circ.process_time IS NOT NULL THEN
28 -- Already processed ... moving on
32 SELECT * INTO e_closing
33 FROM action.emergency_closing
34 WHERE id = e_c_circ.emergency_closing;
36 IF e_closing.process_start_time IS NULL THEN
37 -- Huh... that's odd. And wrong.
42 FROM actor.org_unit_closed
43 WHERE emergency_closing = e_closing.id;
46 FROM action.circulation
47 WHERE id = e_c_circ.circulation;
49 -- Record the processing
50 UPDATE action.emergency_closing_circulation
51 SET original_due_date = circ.due_date,
53 WHERE id = circ_closing_entry;
55 UPDATE action.emergency_closing
56 SET last_update_time = NOW()
57 WHERE id = e_closing.id;
59 SELECT value::BOOL INTO avoid_negative FROM actor.org_unit_ancestor_setting('bill.prohibit_negative_balance_on_overdues', circ.circ_lib);
60 SELECT value::BOOL INTO extend_grace FROM actor.org_unit_ancestor_setting('circ.grace.extend', circ.circ_lib);
62 new_due_date := evergreen.find_next_open_time( closing.org_unit, circ.due_date, EXTRACT(EPOCH FROM circ.duration)::INT % 86400 > 0 )::TEXT;
63 UPDATE action.circulation SET due_date = new_due_date::TIMESTAMPTZ WHERE id = circ.id;
65 -- Now, see if we need to get rid of some fines
66 SELECT * INTO last_bill
68 WHERE b.xact = circ.id
71 ORDER BY billing_ts DESC
77 WHERE b.xact = circ.id
81 b.billing_ts BETWEEN closing.close_start AND new_due_date::TIMESTAMPTZ
82 OR (extend_grace AND last_bill.billing_ts <= new_due_date::TIMESTAMPTZ + circ.grace_period)
84 AND NOT EXISTS (SELECT 1 FROM money.account_adjustment a WHERE a.billing = b.id)
87 IF avoid_negative THEN
88 PERFORM FROM money.materialized_billable_xact_summary WHERE id = circ.id AND balance_owed < bill.amount;
89 EXIT WHEN FOUND; -- We can't go negative, and voiding this bill would do that...
95 note = COALESCE(note,'') || ' :: Voided by emergency closing handler'
103 CREATE OR REPLACE FUNCTION action.emergency_closing_stage_2_reservation ( res_closing_entry INT )
106 res booking.reservation%ROWTYPE;
107 e_closing action.emergency_closing%ROWTYPE;
108 e_c_res action.emergency_closing_reservation%ROWTYPE;
109 closing actor.org_unit_closed%ROWTYPE;
110 adjacent actor.org_unit_closed%ROWTYPE;
111 bill money.billing%ROWTYPE;
113 hoo_close TIME WITHOUT TIME ZONE;
118 -- Gather objects involved
119 SELECT * INTO e_c_res
120 FROM action.emergency_closing_reservation
121 WHERE id = res_closing_entry;
123 IF e_c_res.process_time IS NOT NULL THEN
124 -- Already processed ... moving on
128 SELECT * INTO e_closing
129 FROM action.emergency_closing
130 WHERE id = e_c_res.emergency_closing;
132 IF e_closing.process_start_time IS NULL THEN
133 -- Huh... that's odd. And wrong.
137 SELECT * INTO closing
138 FROM actor.org_unit_closed
139 WHERE emergency_closing = e_closing.id;
142 FROM booking.reservation
143 WHERE id = e_c_res.reservation;
145 IF res.pickup_lib IS NULL THEN -- Need to be far enough along to have a pickup lib
149 -- Record the processing
150 UPDATE action.emergency_closing_reservation
151 SET original_end_time = res.end_time,
153 WHERE id = res_closing_entry;
155 UPDATE action.emergency_closing
156 SET last_update_time = NOW()
157 WHERE id = e_closing.id;
159 SELECT value::BOOL INTO avoid_negative FROM actor.org_unit_ancestor_setting('bill.prohibit_negative_balance_on_overdues', res.pickup_lib);
161 new_due_date := evergreen.find_next_open_time( closing.org_unit, res.end_time, EXTRACT(EPOCH FROM res.booking_interval)::INT % 86400 > 0 )::TEXT;
162 UPDATE booking.reservation SET end_time = new_due_date::TIMESTAMPTZ WHERE id = res.id;
164 -- Now, see if we need to get rid of some fines
168 WHERE b.xact = res.id
171 AND b.billing_ts BETWEEN closing.close_start AND new_due_date::TIMESTAMPTZ
172 AND NOT EXISTS (SELECT 1 FROM money.account_adjustment a WHERE a.billing = b.id)
174 IF avoid_negative THEN
175 PERFORM FROM money.materialized_billable_xact_summary WHERE id = res.id AND balance_owed < bill.amount;
176 EXIT WHEN FOUND; -- We can't go negative, and voiding this bill would do that...
182 note = COALESCE(note,'') || ' :: Voided by emergency closing handler'