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 use List::MoreUtils qw(uniq);
14 my $U = "OpenILS::Application::AppUtils";
15 my $parser = DateTime::Format::ISO8601->new;
17 # -----------------------------------------------------------------
18 # Do not publish methods here. This code is shared across apps.
19 # -----------------------------------------------------------------
22 # -----------------------------------------------------------------
23 # Voids (or zeros) overdue fines on the given circ. if a backdate is
24 # provided, then we only void back to the backdate, unless the
25 # backdate is to within the grace period, in which case we void all
27 # -----------------------------------------------------------------
29 #compatibility layer - TODO
31 sub void_or_zero_overdues {
32 my($class, $e, $circ, $opts) = @_;
39 if( $opts->{backdate} ) {
40 my $backdate = $opts->{backdate};
41 # ------------------------------------------------------------------
42 # Fines for overdue materials are assessed up to, but not including,
43 # one fine interval after the fines are applicable. Here, we add
44 # one fine interval to the backdate to ensure that we are not
45 # voiding fines that were applicable before the backdate.
46 # ------------------------------------------------------------------
48 # if there is a raw time component (e.g. from postgres),
49 # turn it into an interval that interval_to_seconds can parse
50 my $duration = $circ->fine_interval;
51 $duration =~ s/(\d{2}):(\d{2}):(\d{2})/$1 h $2 m $3 s/o;
52 my $interval = OpenSRF::Utils->interval_to_seconds($duration);
54 my $date = DateTime::Format::ISO8601->parse_datetime(cleanse_ISO8601($backdate));
55 my $due_date = DateTime::Format::ISO8601->parse_datetime(cleanse_ISO8601($circ->due_date))->epoch;
56 my $grace_period = extend_grace_period( $class, $circ->circ_lib, $circ->due_date, OpenSRF::Utils->interval_to_seconds($circ->grace_period), $e);
57 if($date->epoch <= $due_date + $grace_period) {
58 $logger->info("backdate $backdate is within grace period, voiding all");
60 $backdate = $U->epoch2ISO8601($date->epoch + $interval);
61 $logger->info("applying backdate $backdate in overdue voiding");
62 $$bill_search{billing_ts} = {'>=' => $backdate};
66 my $billids = $e->search_money_billing([$bill_search, {idlist=>1}]);
67 if ($billids && @$billids) {
68 # overdue settings come from transaction org unit
69 my $prohibit_neg_balance_overdues = (
70 $U->ou_ancestor_setting($circ->circ_lib(), 'bill.prohibit_negative_balance_on_overdues')
72 $U->ou_ancestor_setting($circ->circ_lib(), 'bill.prohibit_negative_balance_default')
74 my $neg_balance_interval_overdues = (
75 $U->ou_ancestor_setting($circ->circ_lib(), 'bill.negative_balance_interval_on_overdues')
77 $U->ou_ancestor_setting($circ->circ_lib(), 'bill.negative_balance_interval_default')
80 # if we prohibit negative overdue balances outright, OR we have an
81 # interval setting which determines what we do, let
82 # adjust_bills_to_zero() do the heavy lifting
83 if ($opts->{force_zero}
84 or (!$opts->{force_void} and ($U->is_true($prohibit_neg_balance_overdues) or $neg_balance_interval_overdues))
86 $result = $class->adjust_bills_to_zero($e, $billids, $opts->{note}, $neg_balance_interval_overdues);
88 # otherwise, just void the usual way
89 $result = $class->void_bills($e, $billids, $opts->{note});
99 # ------------------------------------------------------------------
100 # remove charge from patron's account if lost item is returned
101 # ------------------------------------------------------------------
103 my ($class, $e, $circ, $btype) = @_;
105 my $bills = $e->search_money_billing(
113 $logger->debug("voiding lost item charge of ".scalar(@$bills));
114 for my $bill (@$bills) {
115 if( !$U->is_true($bill->voided) ) {
116 $logger->info("lost item returned - voiding bill ".$bill->id);
118 $bill->void_time('now');
119 $bill->voider($e->requestor->id);
120 my $note = ($bill->note) ? $bill->note . "\n" : '';
121 $bill->note("${note}System: VOIDED FOR LOST ITEM RETURNED");
124 unless $e->update_money_billing($bill);
130 # ------------------------------------------------------------------
131 # Void (or zero) all bills of a given type on a circulation.
133 # Takes an editor, a circ object, the btype number for the bills you
134 # want to void, and an optional note.
136 # Returns undef on success or the result from void_bills.
137 # ------------------------------------------------------------------
138 sub void_or_zero_bills_of_type {
139 my ($class, $e, $circ, $copy, $btype, $note) = @_;
141 # Get a bill payment map.
142 my $bpmap = $class->bill_payment_map_for_xact($e, $circ);
143 if ($bpmap && @$bpmap) {
144 # Filter out the unvoided bills of the type we're looking for:
145 my @bills = map {$_->{bill}} grep { $_->{bill}->btype() == $btype && $_->{bill_amount} > $_->{void_amount} } @$bpmap;
147 # settings for lost come from copy circlib.
148 my $prohibit_neg_balance_lost = (
149 $U->ou_ancestor_setting($copy->circ_lib(), 'bill.prohibit_negative_balance_on_lost')
151 $U->ou_ancestor_setting($copy->circ_lib(), 'bill.prohibit_negative_balance_default')
153 my $neg_balance_interval_lost = (
154 $U->ou_ancestor_setting($copy->circ_lib(), 'bill.negative_balance_interval_on_lost')
156 $U->ou_ancestor_setting($copy->circ_lib(), 'bill.negative_balance_interval_default')
158 my $result = $class->void_bills($e, \@bills, $note);
169 my($class, $e, $xactid) = @_;
171 # -----------------------------------------------------------------
172 # make sure the transaction is not closed
173 my $xact = $e->retrieve_money_billable_transaction($xactid)
174 or return $e->die_event;
176 if( $xact->xact_finish ) {
177 my ($mbts) = $U->fetch_mbts($xactid, $e);
178 if( $mbts->balance_owed != 0 ) {
179 $logger->info("* re-opening xact $xactid, orig xact_finish is ".$xact->xact_finish);
180 $xact->clear_xact_finish;
181 $e->update_money_billable_transaction($xact)
182 or return $e->die_event;
191 my($class, $e, $amount, $btype, $type, $xactid, $note) = @_;
193 $logger->info("The system is charging $amount [$type] on xact $xactid");
194 $note ||= 'SYSTEM GENERATED';
196 # -----------------------------------------------------------------
197 # now create the billing
198 my $bill = Fieldmapper::money::billing->new;
199 $bill->xact($xactid);
200 $bill->amount($amount);
201 $bill->billing_type($type);
202 $bill->btype($btype);
204 $e->create_money_billing($bill) or return $e->die_event;
209 sub extend_grace_period {
210 my($class, $circ_lib, $due_date, $grace_period, $e, $h) = @_;
211 if ($grace_period >= 86400) { # Only extend grace periods greater than or equal to a full day
212 my $parser = DateTime::Format::ISO8601->new;
213 my $due_dt = $parser->parse_datetime( cleanse_ISO8601( $due_date ) );
214 my $due = $due_dt->epoch;
216 my $grace_extend = $U->ou_ancestor_setting_value($circ_lib, 'circ.grace.extend');
217 $e = new_editor() if (!$e);
218 $h = $e->retrieve_actor_org_unit_hours_of_operation($circ_lib) if (!$h);
219 if ($grace_extend and $h) {
220 my $new_grace_period = $grace_period;
222 $logger->info( "Circ lib has an hours-of-operation entry and grace period extension is enabled." );
227 my $dow_open = "dow_${i}_open";
228 my $dow_close = "dow_${i}_close";
229 if($h->$dow_open() eq '00:00:00' and $h->$dow_close() eq '00:00:00') {
238 $logger->info("Circ lib is closed all week according to hours-of-operation entry. Skipping grace period extension checks.");
240 # Extra nice grace periods
241 # AKA, merge closed dates trailing the grace period into the grace period
242 my $grace_extend_into_closed = $U->ou_ancestor_setting_value($circ_lib, 'circ.grace.extend.into_closed');
243 $due += 86400 if $grace_extend_into_closed;
245 my $grace_extend_all = $U->ou_ancestor_setting_value($circ_lib, 'circ.grace.extend.all');
247 if ( $grace_extend_all ) {
248 # Start checking the day after the item was due
249 # This is "The grace period only counts open days"
250 # NOTE: Adding 86400 seconds is not the same as adding one day. This uses seconds intentionally.
251 $due_dt = $due_dt->add( seconds => 86400 );
253 # Jump to the end of the grace period
254 # This is "If the grace period ends on a closed day extend it"
255 # NOTE: This adds grace period as a number of seconds intentionally
256 $due_dt = $due_dt->add( seconds => $grace_period );
259 my $count = 0; # Infinite loop protection
261 $closed = 0; # Starting assumption for day: We are not closed
262 $count++; # We limit the number of loops below.
264 # get the day of the week for the day we are looking at
265 my $dow = $due_dt->day_of_week_0;
267 # Check hours of operation first.
268 if ($h_closed{$dow}) {
270 $new_grace_period += 86400;
271 $due_dt->add( seconds => 86400 );
273 # Check for closed dates for this period
274 my $timestamptz = $due_dt->strftime('%FT%T%z');
275 my $cl = $e->search_actor_org_unit_closed_date(
276 { close_start => { '<=' => $timestamptz },
277 close_end => { '>=' => $timestamptz },
278 org_unit => $circ_lib }
283 my $cl_dt = $parser->parse_datetime( cleanse_ISO8601( $_->close_end ) );
284 while ($due_dt <= $cl_dt) {
285 $due_dt->add( seconds => 86400 );
286 $new_grace_period += 86400;
290 $due_dt->add( seconds => 86400 );
293 } while ( $count <= 366 and ( $closed or $due_dt->epoch <= $due + $new_grace_period ) );
294 if ($new_grace_period > $grace_period) {
295 $grace_period = $new_grace_period;
296 $logger->info( "Grace period for circ extended to $grace_period [" . seconds_to_interval( $grace_period ) . "]" );
301 return $grace_period;
304 # check if a circulation transaction can be closed
305 # takes a CStoreEditor and a circ transaction.
306 # Returns 1 if the circ should be closed, 0 if not.
308 my ($class, $e, $circ) = @_;
311 my $reason = $circ->stop_fines;
313 # We definitely want to close if this circulation was
314 # checked in or renewed.
315 if ($circ->checkin_time) {
317 } elsif ($reason eq OILS_STOP_FINES_LOST) {
318 # Check the copy circ_lib to see if they close
319 # transactions when lost are paid.
320 my $copy = $e->retrieve_asset_copy($circ->target_copy);
322 $can_close = !$U->is_true(
323 $U->ou_ancestor_setting_value(
325 'circ.lost.xact_open_on_zero',
331 } elsif ($reason eq OILS_STOP_FINES_LONGOVERDUE) {
332 # Check the copy circ_lib to see if they close
333 # transactions when long-overdue are paid.
334 my $copy = $e->retrieve_asset_copy($circ->target_copy);
336 $can_close = !$U->is_true(
337 $U->ou_ancestor_setting_value(
339 'circ.longoverdue.xact_open_on_zero',
349 sub seconds_to_interval_hash {
350 my $interval = shift;
351 my $limit = shift || 's';
352 $limit =~ s/^(.)/$1/o;
356 my ($y,$ym,$M,$Mm,$w,$wm,$d,$dm,$h,$hm,$m,$mm,$s);
357 my ($year, $month, $week, $day, $hour, $minute, $second) =
358 ('years','months','weeks','days', 'hours', 'minutes', 'seconds');
360 if ($y = int($interval / (60 * 60 * 24 * 365))) {
362 $ym = $interval % (60 * 60 * 24 * 365);
366 return %output if ($limit eq 'y');
368 if ($M = int($ym / ((60 * 60 * 24 * 365)/12))) {
369 $output{$month} = $M;
370 $Mm = $ym % ((60 * 60 * 24 * 365)/12);
374 return %output if ($limit eq 'M');
376 if ($w = int($Mm / 604800)) {
382 return %output if ($limit eq 'w');
384 if ($d = int($wm / 86400)) {
390 return %output if ($limit eq 'd');
392 if ($h = int($dm / 3600)) {
398 return %output if ($limit eq 'h');
400 if ($m = int($hm / 60)) {
401 $output{$minute} = $m;
406 return %output if ($limit eq 'm');
409 $output{$second} = $s;
411 $output{$second} = 0 unless (keys %output);
417 my ($class, $args) = @_;
418 my $circs = $args->{circs};
419 return unless $circs and @$circs;
420 my $e = $args->{editor};
421 # if a client connection is passed in, this will be chatty like
422 # the old storage version
423 my $conn = $args->{conn};
427 # Transactions are opened/closed with each circ, reservation, etc.
428 # The first $e->xact_begin (below) will cause a connect.
433 my %hoo = map { ( $_->id => $_ ) } @{ $e->retrieve_all_actor_org_unit_hours_of_operation };
435 my $handling_resvs = 0;
436 for my $c (@$circs) {
440 if (!$ctype) { # we received only an idlist, not objects
441 if ($handling_resvs) {
442 $c = $e->retrieve_booking_reservation($c);
443 } elsif (not defined $c) {
444 # an undef value is the indicator that we are moving
445 # from processing circulations to reservations.
449 $c = $e->retrieve_action_circulation($c);
454 $ctype =~ s/^.+::(\w+)$/$1/;
456 my $due_date_method = 'due_date';
457 my $target_copy_method = 'target_copy';
458 my $circ_lib_method = 'circ_lib';
459 my $recurring_fine_method = 'recurring_fine';
460 my $is_reservation = 0;
461 if ($ctype eq 'reservation') {
463 $due_date_method = 'end_time';
464 $target_copy_method = 'current_resource';
465 $circ_lib_method = 'pickup_lib';
466 $recurring_fine_method = 'fine_amount';
467 next unless ($c->fine_interval);
469 #TODO: reservation grace periods
470 my $grace_period = ($is_reservation ? 0 : interval_to_seconds($c->grace_period));
474 # Clean up after previous transaction.
475 # This is a no-op if there is no open transaction.
476 $e->xact_rollback if $commit;
478 $logger->info(sprintf("Processing $ctype %d...", $c->id));
480 # each (ils) transaction is processed in its own (db) transaction
481 $e->xact_begin if $commit;
483 my $due_dt = $parser->parse_datetime( cleanse_ISO8601( $c->$due_date_method ) );
485 my $due = $due_dt->epoch;
488 my $fine_interval = $c->fine_interval;
489 $fine_interval =~ s/(\d{2}):(\d{2}):(\d{2})/$1 h $2 m $3 s/o;
490 $fine_interval = interval_to_seconds( $fine_interval );
492 if ( $fine_interval == 0 || int($c->$recurring_fine_method * 100) == 0 || int($c->max_fine * 100) == 0 ) {
493 $conn->respond( "Fine Generator skipping circ due to 0 fine interval, 0 fine rate, or 0 max fine.\n" ) if $conn;
494 $logger->info( "Fine Generator skipping circ " . $c->id . " due to 0 fine interval, 0 fine rate, or 0 max fine." );
498 if ( $is_reservation and $fine_interval >= interval_to_seconds('1d') ) {
500 if ($due_dt->strftime('%z') =~ /(-|\+)(\d{2}):?(\d{2})/) {
501 $tz_offset_s = $1 . interval_to_seconds( "${2}h ${3}m");
504 $due -= ($due % $fine_interval) + $tz_offset_s;
505 $now -= ($now % $fine_interval) + $tz_offset_s;
509 "ARG! Overdue $ctype ".$c->id.
510 " for item ".$c->$target_copy_method.
511 " (user ".$c->usr.").\n".
512 "\tItem was due on or before: ".localtime($due)."\n") if $conn;
514 my @fines = @{$e->search_money_billing([
517 billing_ts => { '>' => $c->$due_date_method } },
518 { order_by => {mb => 'billing_ts DESC'}}
522 my $fine = $fines[$f_idx] if (@fines);
523 my $current_fine_total = 0;
524 $current_fine_total += int($_->amount * 100) for (grep { $_ and !$U->is_true($_->voided) } @fines);
528 $conn->respond( "Last billing time: ".$fine->billing_ts." (clensed format: ".cleanse_ISO8601( $fine->billing_ts ).")") if $conn;
529 $last_fine = $parser->parse_datetime( cleanse_ISO8601( $fine->billing_ts ) )->epoch;
531 $logger->info( "Potential first billing for circ ".$c->id );
534 $grace_period = extend_grace_period($class, $c->$circ_lib_method,$c->$due_date_method,$grace_period,undef,$hoo{$c->$circ_lib_method});
537 return if ($last_fine > $now);
538 # Generate fines for each past interval, including the one we are inside
539 my $pending_fine_count = ceil( ($now - $last_fine) / $fine_interval );
541 if ( $last_fine == $due # we have no fines yet
542 && $grace_period # and we have a grace period
543 && $now < $due + $grace_period # and some date math says were are within the grace period
545 $conn->respond( "Still inside grace period of: ". seconds_to_interval( $grace_period )."\n" ) if $conn;
546 $logger->info( "Circ ".$c->id." is still inside grace period of: $grace_period [". seconds_to_interval( $grace_period ).']' );
550 $conn->respond( "\t$pending_fine_count pending fine(s)\n" ) if $conn;
551 return unless ($pending_fine_count);
553 my $recurring_fine = int($c->$recurring_fine_method * 100);
554 my $max_fine = int($c->max_fine * 100);
556 my $skip_closed_check = $U->ou_ancestor_setting_value(
557 $c->$circ_lib_method, 'circ.fines.charge_when_closed');
558 $skip_closed_check = $U->is_true($skip_closed_check);
560 my $truncate_to_max_fine = $U->ou_ancestor_setting_value(
561 $c->$circ_lib_method, 'circ.fines.truncate_to_max_fine');
562 $truncate_to_max_fine = $U->is_true($truncate_to_max_fine);
564 my ($latest_billing_ts, $latest_amount) = ('',0);
565 for (my $bill = 1; $bill <= $pending_fine_count; $bill++) {
567 if ($current_fine_total >= $max_fine) {
568 if ($ctype eq 'circulation') {
569 $c->stop_fines('MAXFINES');
570 $c->stop_fines_time('now');
571 $e->update_action_circulation($c);
574 "\tMaximum fine level of ".$c->max_fine.
575 " reached for this $ctype.\n".
576 "\tNo more fines will be generated.\n" ) if $conn;
580 # XXX Use org time zone (or default to 'local') once we have the ou setting built for that
581 my $billing_ts = DateTime->from_epoch( epoch => $last_fine, time_zone => 'local' );
582 my $current_bill_count = $bill;
583 while ( $current_bill_count ) {
584 $billing_ts->add( seconds_to_interval_hash( $fine_interval ) );
585 $current_bill_count--;
588 my $timestamptz = $billing_ts->strftime('%FT%T%z');
589 if (!$skip_closed_check) {
590 my $dow = $billing_ts->day_of_week_0();
591 my $dow_open = "dow_${dow}_open";
592 my $dow_close = "dow_${dow}_close";
594 if (my $h = $hoo{$c->$circ_lib_method}) {
595 next if ( $h->$dow_open eq '00:00:00' and $h->$dow_close eq '00:00:00');
598 my @cl = @{$e->search_actor_org_unit_closed_date(
599 { close_start => { '<=' => $timestamptz },
600 close_end => { '>=' => $timestamptz },
601 org_unit => $c->$circ_lib_method }
606 # The billing amount for this billing normally ought to be the recurring fine amount.
607 # However, if the recurring fine amount would cause total fines to exceed the max fine amount,
608 # we may wish to reduce the amount for this billing (if circ.fines.truncate_to_max_fine is true).
609 my $this_billing_amount = $recurring_fine;
610 if ( $truncate_to_max_fine && ($current_fine_total + $this_billing_amount) > $max_fine ) {
611 $this_billing_amount = ($max_fine - $current_fine_total);
613 $current_fine_total += $this_billing_amount;
614 $latest_amount += $this_billing_amount;
615 $latest_billing_ts = $timestamptz;
617 my $bill = Fieldmapper::money::billing->new;
619 $bill->note("System Generated Overdue Fine");
620 $bill->billing_type("Overdue materials");
622 $bill->amount(sprintf('%0.2f', $this_billing_amount/100));
623 $bill->billing_ts($timestamptz);
624 $e->create_money_billing($bill);
628 $conn->respond( "\t\tAdding fines totaling $latest_amount for overdue up to $latest_billing_ts\n" )
629 if ($conn and $latest_billing_ts and $latest_amount);
632 # Calculate penalties inline
633 OpenILS::Utils::Penalty->calculate_penalties(
634 $e, $c->usr, $c->$circ_lib_method);
636 $e->xact_commit if $commit;
642 $conn->respond( "Error processing overdue $ctype [".$c->id."]:\n\n$e\n" ) if $conn;
643 $logger->error("Error processing overdue $ctype [".$c->id."]:\n$e\n");
644 last if ($e =~ /IS NOT CONNECTED TO THE NETWORK/o);
648 # roll back any (potentially) orphaned transaction and disconnect.
649 $e->rollback if $commit;
654 # -----------------------------------------------------------------
655 # Given an editor and a xact, return a reference to an array of
656 # hashrefs that map billing objects to payment objects. Returns undef
657 # if no bills are found for the given transaction.
659 # The bill amounts are adjusted to reflect the application of the
660 # payments to the bills. The original bill amounts are retained in
663 # The payment objects may or may not have their amounts adjusted
664 # depending on whether or not they apply to more than one bill. We
665 # could really use a better logic here, perhaps, but if it was
666 # consistent, it wouldn't be Evergreen.
668 # The data structure used in the array is a hashref that has the
671 # bill => the adjusted bill object
672 # voids => an arrayref of void payments that apply directly to the
674 # payments => an arrayref of payment objects applied to the bill
675 # bill_amount => original amount from the billing object
676 # void_amount => total of the void payments that apply directly to the
679 # Each bill is only mapped to payments one time. However, a single
680 # payment may be mapped to more than one bill if the payment amount is
681 # greater than the amount of each individual bill, such as a $3.00
682 # payment for 30 $0.10 overdue bills. There is an attempt made to
683 # first pay bills with payments that match the billing amount. This
684 # is intended to catch payments for lost and/or long overdue bills so
685 # that they will match up.
687 # This function is heavily adapted from code written by Jeff Godin of
688 # Traverse Area District Library and submitted on LaunchPad bug
690 # -----------------------------------------------------------------
691 sub bill_payment_map_for_xact {
692 my ($class, $e, $xact) = @_;
694 # Check for CStoreEditor and make a new one if we have to. This
695 # allows one-off calls to this subroutine to pass undef as the
696 # CStoreEditor and not have to create one of their own.
697 $e = OpenILS::Utils::CStoreEditor->new unless ($e);
699 # find all bills in order
701 {xact => $xact->id()},
702 { order_by => { mb => { billing_ts => { direction => 'asc' } } } },
705 # At some point, we should get rid of the voided column on
706 # money.payment and family. It is not exposed in the client at
707 # the moment, and should be replaced with a void_bill type. The
708 # descendants of money.payment don't expose the voided field in
709 # the fieldmapper, only the mp object, based on the money.payment
710 # view, does. However, I want to leave that complication for
711 # later. I wonder if I'm not slowing things down too much with
712 # the current adjustment_payment logic. It would probably be faster if
713 # we had direct Pg access at this layer. I can probably wrangle
714 # something via the drivers or store interfaces, but I haven't
715 # really figured those out, yet.
717 my $bills = $e->search_money_billing($bill_search);
719 # return undef if there are no bills.
720 return undef unless ($bills && @$bills);
722 # map the bills into our bill_payment_map entry format:
726 bill_amount => $_->amount(),
733 # Find all unvoided payments in order. Flesh voids so that we
734 # don't have to retrieve them later.
735 my $payments = $e->search_money_payment(
737 { xact => $xact->id, voided=>'f' },
739 order_by => { mp => { payment_ts => { direction => 'asc' } } },
741 flesh_fields => { mp => ['adjustment_payment'] }
746 # If there were no payments, then we just return the bills.
747 return \@entries unless ($payments && @$payments);
749 # Now, we go through the rigmarole of mapping payments to bills
750 # and adjusting the bill balances.
752 # Apply the voids before "paying" other bills.
753 foreach my $entry (@entries) {
754 my $bill = $entry->{bill};
755 # Find only the voids that apply to individual bills.
756 my @voids = map {$_->adjustment_payment()} grep {$_->payment_type() eq 'adjustment_payment' && $_->adjustment_payment()->billing() == $bill->id()} @$payments;
758 foreach my $void (@voids) {
759 my $new_amount = $U->fpdiff($bill->amount(),$void->amount());
760 if ($new_amount >= 0) {
761 push @{$entry->{voids}}, $void;
762 $entry->{void_amount} += $void->amount();
763 $bill->amount($new_amount);
764 # Remove the used up void from list of payments:
765 my @p = grep {$_->id() != $void->id()} @$payments;
768 # It should never happen that we have more void
769 # payments on a single bill than the amount of the
770 # bill. However, experience shows that the things
771 # that should never happen actually do happen with
772 # surprising regularity in a library setting.
774 # Clone the void to say how much of it actually
775 # applied to this bill.
776 my $new_void = $void->clone();
777 $new_void->amount($bill->amount());
778 $new_void->amount_collected($bill->amount());
779 push (@{$entry->{voids}}, $new_void);
780 $entry->{void_amount} += $new_void->amount();
782 $void->amount(-$new_amount);
783 # Could be a candidate for YAOUS about what to do
784 # with excess void amounts on a bill.
786 last if ($bill->amount() == 0);
791 # Try to map payments to bills by amounts starting with the
793 foreach my $payment (sort {$b->amount() <=> $a->amount()} @$payments) {
794 my @bills2pay = grep {$_->{bill}->amount() == $payment->amount()} @entries;
796 my $entry = $bills2pay[0];
797 $entry->{bill}->amount(0);
798 push @{$entry->{payments}}, $payment;
799 # Remove the payment from the master list.
800 my @p = grep {$_->id() != $payment->id()} @$payments;
805 # Map remaining bills to payments in whatever order.
806 foreach my $entry (grep {$_->{bill}->amount() > 0} @entries) {
807 my $bill = $entry->{bill};
808 # We could run out of payments before bills.
809 if ($payments && @$payments) {
810 while ($bill->amount() > 0) {
811 my $payment = shift @$payments;
812 last unless $payment;
813 my $new_amount = $U->fpdiff($bill->amount(),$payment->amount());
814 if ($new_amount < 0) {
815 # Clone the payment so we can say how much applied
817 my $new_payment = $payment->clone();
818 $new_payment->amount($bill->amount());
820 push @{$entry->{payments}}, $new_payment;
821 # Reset the payment amount and put it back on the
822 # list for later use.
823 $payment->amount(-$new_amount);
824 unshift @$payments, $payment;
826 $bill->amount($new_amount);
827 push @{$entry->{payments}}, $payment;
837 # This subroutine actually handles voiding of bills. It takes a
838 # CStoreEditor, an arrayref of bill ids or bills, and an optional note.
840 my ($class, $e, $billids, $note) = @_;
841 return $e->die_event unless $e->checkauth;
842 return $e->die_event unless $e->allowed('VOID_BILLING');
846 if (ref($billids->[0])) {
849 $bills = $e->search_money_billing([{id => $billids}])
850 or return $e->die_event;
852 for my $bill (@$bills) {
854 my $xact = $e->retrieve_money_billable_transaction($bill->xact)
855 or return $e->die_event;
857 if($U->is_true($bill->voided)) {
858 # For now, it is not an error to attempt to re-void a bill, but
859 # don't actually do anything
861 #return OpenILS::Event->new('BILL_ALREADY_VOIDED', payload => $bill)
865 my $org = $U->xact_org($bill->xact, $e);
866 $users{$xact->usr} = {} unless $users{$xact->usr};
867 $users{$xact->usr}->{$org} = 1;
870 $bill->voider($e->requestor->id);
871 $bill->void_time('now');
872 my $n = ($bill->note) ? sprintf("%s\n", $bill->note) : "";
873 $bill->note(sprintf("$n%s", ($note) ? $note : "System: VOIDED FOR BACKDATE"));
875 $e->update_money_billing($bill) or return $e->die_event;
876 my $evt = $U->check_open_xact($e, $bill->xact, $xact);
880 # calculate penalties for all user/org combinations
881 for my $user_id (keys %users) {
882 for my $org_id (keys %{$users{$user_id}}) {
883 OpenILS::Utils::Penalty->calculate_penalties($e, $user_id, $org_id)
891 # This subroutine actually handles "adjusting" bills to zero. It takes a
892 # CStoreEditor, an arrayref of bill ids or bills, an optional note, and an
894 sub adjust_bills_to_zero {
895 my ($class, $e, $billids, $note, $interval) = @_;
897 # Get with the editor to see if we have permission to void bills.
898 return $e->die_event unless $e->checkauth;
899 return $e->die_event unless $e->allowed('VOID_BILLING');
903 # Let's get all the billing objects and handle them by
906 if (ref($billids->[0])) {
909 $bills = $e->search_money_billing([{id => $billids}])
910 or return $e->die_event;
913 my @xactids = uniq map {$_->xact()} @$bills;
915 foreach my $xactid (@xactids) {
916 my $mbt = $e->retrieve_money_billable_transaction(
922 mbt=>['grocery','circulation'],
923 circ=>['target_copy']
927 ) or return $e->die_event;
928 # Flesh grocery bills and circulations so we don't have to
929 # retrieve them later.
930 my ($circ, $grocery, $copy);
931 $grocery = $mbt->grocery();
932 $circ = $mbt->circulation();
933 $copy = $circ->target_copy() if ($circ);
937 # Get the bill_payment_map for the transaction.
938 my $bpmap = $class->bill_payment_map_for_xact($e, $mbt);
940 # Get the bills for this transaction from the main list of bills.
941 my @xact_bills = grep {$_->xact() == $xactid} @$bills;
942 # Handle each bill in turn.
943 foreach my $bill (@xact_bills) {
944 # As the total open amount on the transaction will change
945 # as each bill is voided, we'll just recalculate it for
948 map {$xact_total += $_->{bill}->amount()} @$bpmap;
949 last if $xact_total == 0;
951 # Get the bill_payment_map entry for this bill:
952 my ($bpentry) = grep {$_->{bill}->id() == $bill->id()} @$bpmap;
953 if (_within_refund_interval($bpentry, $interval)) {
958 # From here on out, use the bill object from the bill
960 $bill = $bpentry->{bill};
962 # The amount to void is the non-voided balance on the
963 # bill. It should never be less than zero.
964 my $amount_to_void = $U->fpdiff($bpentry->{bill_amount},$bpentry->{void_amount});
966 # Check if this bill is already voided. We don't allow
967 # "double" voids regardless of settings.
968 if ($amount_to_void <= 0) {
969 #my $event = OpenILS::Event->new('BILL_ALREADY_VOIDED', payload => $bill);
975 if ($amount_to_void > $xact_total) {
976 $amount_to_void = $xact_total;
979 # Create the adjustment payment
980 my $payobj = Fieldmapper::money::adjustment_payment->new;
981 $payobj->amount($amount_to_void);
982 $payobj->amount_collected($amount_to_void);
983 $payobj->xact($xactid);
984 $payobj->accepting_usr($e->requestor->id);
985 $payobj->payment_ts('now');
986 $payobj->billing($bill->id());
987 $payobj->note($note) if ($note);
988 $e->create_money_adjustment_payment($payobj) or return $e->die_event;
989 # Adjust our bill_payment_map
990 $bpentry->{void_amount} += $amount_to_void;
991 push @{$bpentry->{voids}}, $payobj;
992 # Should come to zero:
993 my $new_bill_amount = $U->fpdiff($bill->amount(),$amount_to_void);
994 $bill->amount($new_bill_amount);
997 my $org = $U->xact_org($xactid, $e);
998 $users{$mbt->usr} = {} unless $users{$mbt->usr};
999 $users{$mbt->usr}->{$org} = 1;
1001 my $evt = $U->check_open_xact($e, $xactid, $mbt);
1002 return $evt if $evt;
1005 # calculate penalties for all user/org combinations
1006 for my $user_id (keys %users) {
1007 for my $org_id (keys %{$users{$user_id}}) {
1008 OpenILS::Utils::Penalty->calculate_penalties($e, $user_id, $org_id);
1015 # A helper function to check if the payments on a bill are within the
1016 # range of a given interval. The first argument is the entry hash
1017 # from the bill payment map for the bill to check and the second
1018 # argument is the interval. It returns true (1) if any of the bills
1019 # are within range of the interval, or false (0) otherwise. It also
1020 # returns true if the interval argument is undefined or empty, or if
1021 # the bill has no payments whatsoever. It will return false if the
1022 # entry has no payments other than voids.
1023 sub _within_refund_interval {
1024 my ($entry, $interval) = @_;
1025 my $result = ($interval ? 0 : 1);
1027 # A check to see if we were given the settings hash or the value:
1028 if (ref($interval) eq 'HASH') {
1029 $interval = $interval->{value};
1032 if ($interval && $entry && $entry->{payments} && @{$entry->{payments}}) {
1033 my $interval_secs = interval_to_seconds($interval);
1034 my @pay_dates = map {$_->payment_ts()} sort {$b->payment_ts() cmp $a->payment_ts()} grep {$_->payment_type() ne 'adjustment_payment'} @{$entry->{payments}};
1036 # Since we've sorted the payment dates from highest to
1037 # lowest, we really only need to check the 0th one.
1038 my $payment_date = DateTime::Format::ISO8601->parse_datetime(cleanse_ISO8601($pay_dates[0]))->epoch;
1040 $result = 1 if ($payment_date + $interval_secs >= $now);
1042 } elsif ($interval && (!$entry->{payments} || !@{$entry->{payments}})) {