1 package OpenILS::Application::Circ::CircCommon;
2 use strict; use warnings;
4 use DateTime::Format::ISO8601;
5 use OpenILS::Application::AppUtils;
6 use OpenSRF::Utils qw/:datetime/;
8 use OpenSRF::Utils::Logger qw(:logger);
9 use OpenILS::Utils::CStoreEditor q/:funcs/;
10 use OpenILS::Const qw/:const/;
12 my $U = "OpenILS::Application::AppUtils";
14 # -----------------------------------------------------------------
15 # Do not publish methods here. This code is shared across apps.
16 # -----------------------------------------------------------------
19 # -----------------------------------------------------------------
20 # Voids overdue fines on the given circ. if a backdate is
21 # provided, then we only void back to the backdate, unless the
22 # backdate is to within the grace period, in which case we void all
24 # -----------------------------------------------------------------
26 my($class, $e, $circ, $backdate, $note) = @_;
34 # ------------------------------------------------------------------
35 # Fines for overdue materials are assessed up to, but not including,
36 # one fine interval after the fines are applicable. Here, we add
37 # one fine interval to the backdate to ensure that we are not
38 # voiding fines that were applicable before the backdate.
39 # ------------------------------------------------------------------
41 # if there is a raw time component (e.g. from postgres),
42 # turn it into an interval that interval_to_seconds can parse
43 my $duration = $circ->fine_interval;
44 $duration =~ s/(\d{2}):(\d{2}):(\d{2})/$1 h $2 m $3 s/o;
45 my $interval = OpenSRF::Utils->interval_to_seconds($duration);
47 my $date = DateTime::Format::ISO8601->parse_datetime(cleanse_ISO8601($backdate));
48 my $due_date = DateTime::Format::ISO8601->parse_datetime(cleanse_ISO8601($circ->due_date))->epoch;
49 my $grace_period = extend_grace_period( $class, $circ->circ_lib, $circ->due_date, OpenSRF::Utils->interval_to_seconds($circ->grace_period), $e);
50 if($date->epoch <= $due_date + $grace_period) {
51 $logger->info("backdate $backdate is within grace period, voiding all");
53 $backdate = $U->epoch2ISO8601($date->epoch + $interval);
54 $logger->info("applying backdate $backdate in overdue voiding");
55 $$bill_search{billing_ts} = {'>=' => $backdate};
59 my $bills = $e->search_money_billing($bill_search);
61 for my $bill (@$bills) {
62 next if $U->is_true($bill->voided);
63 $logger->info("voiding overdue bill ".$bill->id);
65 $bill->void_time('now');
66 $bill->voider($e->requestor->id);
67 my $n = ($bill->note) ? sprintf("%s\n", $bill->note) : "";
68 $bill->note(sprintf("$n%s", ($note) ? $note : "System: VOIDED FOR BACKDATE"));
69 $e->update_money_billing($bill) or return $e->die_event;
75 # ------------------------------------------------------------------
76 # remove charge from patron's account if lost item is returned
77 # ------------------------------------------------------------------
79 my ($class, $e, $circ, $btype) = @_;
81 my $bills = $e->search_money_billing(
88 $logger->debug("voiding lost item charge of ".scalar(@$bills));
89 for my $bill (@$bills) {
90 if( !$U->is_true($bill->voided) ) {
91 $logger->info("lost item returned - voiding bill ".$bill->id);
93 $bill->void_time('now');
94 $bill->voider($e->requestor->id);
95 my $note = ($bill->note) ? $bill->note . "\n" : '';
96 $bill->note("${note}System: VOIDED FOR LOST ITEM RETURNED");
99 unless $e->update_money_billing($bill);
106 my($class, $e, $xactid) = @_;
108 # -----------------------------------------------------------------
109 # make sure the transaction is not closed
110 my $xact = $e->retrieve_money_billable_transaction($xactid)
111 or return $e->die_event;
113 if( $xact->xact_finish ) {
114 my ($mbts) = $U->fetch_mbts($xactid, $e);
115 if( $mbts->balance_owed != 0 ) {
116 $logger->info("* re-opening xact $xactid, orig xact_finish is ".$xact->xact_finish);
117 $xact->clear_xact_finish;
118 $e->update_money_billable_transaction($xact)
119 or return $e->die_event;
128 my($class, $e, $amount, $btype, $type, $xactid, $note) = @_;
130 $logger->info("The system is charging $amount [$type] on xact $xactid");
131 $note ||= 'SYSTEM GENERATED';
133 # -----------------------------------------------------------------
134 # now create the billing
135 my $bill = Fieldmapper::money::billing->new;
136 $bill->xact($xactid);
137 $bill->amount($amount);
138 $bill->billing_type($type);
139 $bill->btype($btype);
141 $e->create_money_billing($bill) or return $e->die_event;
146 sub extend_grace_period {
147 my($class, $circ_lib, $due_date, $grace_period, $e, $h) = @_;
148 if ($grace_period >= 86400) { # Only extend grace periods greater than or equal to a full day
149 my $parser = DateTime::Format::ISO8601->new;
150 my $due_dt = $parser->parse_datetime( cleanse_ISO8601( $due_date ) );
151 my $due = $due_dt->epoch;
153 my $grace_extend = $U->ou_ancestor_setting_value($circ_lib, 'circ.grace.extend');
154 $e = new_editor() if (!$e);
155 $h = $e->retrieve_actor_org_unit_hours_of_operation($circ_lib) if (!$h);
156 if ($grace_extend and $h) {
157 my $new_grace_period = $grace_period;
159 $logger->info( "Circ lib has an hours-of-operation entry and grace period extension is enabled." );
164 my $dow_open = "dow_${i}_open";
165 my $dow_close = "dow_${i}_close";
166 if($h->$dow_open() eq '00:00:00' and $h->$dow_close() eq '00:00:00') {
175 $logger->info("Circ lib is closed all week according to hours-of-operation entry. Skipping grace period extension checks.");
177 # Extra nice grace periods
178 # AKA, merge closed dates trailing the grace period into the grace period
179 my $grace_extend_into_closed = $U->ou_ancestor_setting_value($circ_lib, 'circ.grace.extend.into_closed');
180 $due += 86400 if $grace_extend_into_closed;
182 my $grace_extend_all = $U->ou_ancestor_setting_value($circ_lib, 'circ.grace.extend.all');
184 if ( $grace_extend_all ) {
185 # Start checking the day after the item was due
186 # This is "The grace period only counts open days"
187 # NOTE: Adding 86400 seconds is not the same as adding one day. This uses seconds intentionally.
188 $due_dt = $due_dt->add( seconds => 86400 );
190 # Jump to the end of the grace period
191 # This is "If the grace period ends on a closed day extend it"
192 # NOTE: This adds grace period as a number of seconds intentionally
193 $due_dt = $due_dt->add( seconds => $grace_period );
196 my $count = 0; # Infinite loop protection
198 $closed = 0; # Starting assumption for day: We are not closed
199 $count++; # We limit the number of loops below.
201 # get the day of the week for the day we are looking at
202 my $dow = $due_dt->day_of_week_0;
204 # Check hours of operation first.
205 if ($h_closed{$dow}) {
207 $new_grace_period += 86400;
208 $due_dt->add( seconds => 86400 );
210 # Check for closed dates for this period
211 my $timestamptz = $due_dt->strftime('%FT%T%z');
212 my $cl = $e->search_actor_org_unit_closed_date(
213 { close_start => { '<=' => $timestamptz },
214 close_end => { '>=' => $timestamptz },
215 org_unit => $circ_lib }
220 my $cl_dt = $parser->parse_datetime( cleanse_ISO8601( $_->close_end ) );
221 while ($due_dt <= $cl_dt) {
222 $due_dt->add( seconds => 86400 );
223 $new_grace_period += 86400;
227 $due_dt->add( seconds => 86400 );
230 } while ( $count <= 366 and ( $closed or $due_dt->epoch <= $due + $new_grace_period ) );
231 if ($new_grace_period > $grace_period) {
232 $grace_period = $new_grace_period;
233 $logger->info( "Grace period for circ extended to $grace_period [" . seconds_to_interval( $grace_period ) . "]" );
238 return $grace_period;
241 # check if a circulation transaction can be closed
242 # takes a CStoreEditor and a circ transaction.
243 # Returns 1 if the circ should be closed, 0 if not.
245 my ($class, $e, $circ) = @_;
248 my $reason = $circ->stop_fines;
250 # We definitely want to close if this circulation was
251 # checked in or renewed.
252 if ($circ->checkin_time) {
254 } elsif ($reason eq OILS_STOP_FINES_LOST) {
255 # Check the copy circ_lib to see if they close
256 # transactions when lost are paid.
257 my $copy = $e->retrieve_asset_copy($circ->target_copy);
259 $can_close = !$U->is_true(
260 $U->ou_ancestor_setting_value(
262 'circ.lost.xact_open_on_zero',
268 } elsif ($reason eq OILS_STOP_FINES_LONGOVERDUE) {
269 # Check the copy circ_lib to see if they close
270 # transactions when long-overdue are paid.
271 my $copy = $e->retrieve_asset_copy($circ->target_copy);
273 $can_close = !$U->is_true(
274 $U->ou_ancestor_setting_value(
276 'circ.longoverdue.xact_open_on_zero',