]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/CircCommon.pm
LP#980296: Void Lost Fines if copy claims returned.
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Circ / CircCommon.pm
1 package OpenILS::Application::Circ::CircCommon;
2 use strict; use warnings;
3 use DateTime;
4 use DateTime::Format::ISO8601;
5 use OpenILS::Application::AppUtils;
6 use OpenSRF::Utils qw/:datetime/;
7 use OpenILS::Event;
8 use OpenSRF::Utils::Logger qw(:logger);
9 use OpenILS::Utils::CStoreEditor q/:funcs/;
10 use OpenILS::Const qw/:const/;
11
12 my $U = "OpenILS::Application::AppUtils";
13
14 # -----------------------------------------------------------------
15 # Do not publish methods here.  This code is shared across apps.
16 # -----------------------------------------------------------------
17
18
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
23 # overdue fines.
24 # -----------------------------------------------------------------
25 sub void_overdues {
26     my($class, $e, $circ, $backdate, $note) = @_;
27
28     my $bill_search = { 
29         xact => $circ->id, 
30         btype => 1 
31     };
32
33     if( $backdate ) {
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         # ------------------------------------------------------------------
40
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);
46
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");
52         } else {
53             $backdate = $U->epoch2ISO8601($date->epoch + $interval);
54             $logger->info("applying backdate $backdate in overdue voiding");
55             $$bill_search{billing_ts} = {'>=' => $backdate};
56         }
57     }
58
59     my $bills = $e->search_money_billing($bill_search);
60     
61     for my $bill (@$bills) {
62         next if $U->is_true($bill->voided);
63         $logger->info("voiding overdue bill ".$bill->id);
64         $bill->voided('t');
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;
70     }
71
72     return undef;
73 }
74
75 # ------------------------------------------------------------------
76 # remove charge from patron's account if lost item is returned
77 # ------------------------------------------------------------------
78 sub void_lost {
79     my ($class, $e, $circ, $btype) = @_;
80
81     my $bills = $e->search_money_billing(
82         {
83             xact => $circ->id,
84             btype => $btype
85         }
86     );
87
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);
92             $bill->voided('t');
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");
97
98             return $e->die_event
99                 unless $e->update_money_billing($bill);
100         }
101     }
102     return undef;
103 }
104
105 sub reopen_xact {
106     my($class, $e, $xactid) = @_;
107
108     # -----------------------------------------------------------------
109     # make sure the transaction is not closed
110     my $xact = $e->retrieve_money_billable_transaction($xactid)
111         or return $e->die_event;
112
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;
120         } 
121     }
122
123     return undef;
124 }
125
126
127 sub create_bill {
128     my($class, $e, $amount, $btype, $type, $xactid, $note) = @_;
129
130     $logger->info("The system is charging $amount [$type] on xact $xactid");
131     $note ||= 'SYSTEM GENERATED';
132
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); 
140     $bill->note($note);
141     $e->create_money_billing($bill) or return $e->die_event;
142
143     return undef;
144 }
145
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;
152
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;
158
159             $logger->info( "Circ lib has an hours-of-operation entry and grace period extension is enabled." );
160
161             my $closed = 0;
162             my %h_closed = {};
163             for my $i (0 .. 6) {
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') {
167                     $closed++;
168                     $h_closed{$i} = 1;
169                 } else {
170                     $h_closed{$i} = 0;
171                 }
172             }
173
174             if($closed == 7) {
175                 $logger->info("Circ lib is closed all week according to hours-of-operation entry. Skipping grace period extension checks.");
176             } else {
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;
181
182                 my $grace_extend_all = $U->ou_ancestor_setting_value($circ_lib, 'circ.grace.extend.all');
183
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 );
189                 } else {
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 );
194                 }
195
196                 my $count = 0; # Infinite loop protection
197                 do {
198                     $closed = 0; # Starting assumption for day: We are not closed
199                     $count++; # We limit the number of loops below.
200
201                     # get the day of the week for the day we are looking at
202                     my $dow = $due_dt->day_of_week_0;
203
204                     # Check hours of operation first.
205                     if ($h_closed{$dow}) {
206                         $closed = 1;
207                         $new_grace_period += 86400;
208                         $due_dt->add( seconds => 86400 );
209                     } else {
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 }
216                         );
217                         if ($cl and @$cl) {
218                             $closed = 1;
219                             foreach (@$cl) {
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;
224                                 }
225                             }
226                         } else {
227                             $due_dt->add( seconds => 86400 );
228                         }
229                     }
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 ) . "]" );
234                 }
235             }
236         }
237     }
238     return $grace_period;
239 }
240
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.
244 sub can_close_circ {
245     my ($class, $e, $circ) = @_;
246     my $can_close = 0;
247
248     my $reason = $circ->stop_fines;
249
250     # We definitely want to close if this circulation was
251     # checked in or renewed.
252     if ($circ->checkin_time) {
253         $can_close = 1;
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);
258         if ($copy) {
259             $can_close = !$U->is_true(
260                 $U->ou_ancestor_setting_value(
261                     $copy->circ_lib,
262                     'circ.lost.xact_open_on_zero',
263                     $e
264                 )
265             );
266         }
267
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);
272         if ($copy) {
273             $can_close = !$U->is_true(
274                 $U->ou_ancestor_setting_value(
275                     $copy->circ_lib,
276                     'circ.longoverdue.xact_open_on_zero',
277                     $e
278                 )
279             );
280         }
281     }
282
283     return $can_close;
284 }
285
286 1;