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
22 # -----------------------------------------------------------------
24 my($class, $e, $circ, $backdate, $note) = @_;
32 # ------------------------------------------------------------------
33 # Fines for overdue materials are assessed up to, but not including,
34 # one fine interval after the fines are applicable. Here, we add
35 # one fine interval to the backdate to ensure that we are not
36 # voiding fines that were applicable before the backdate.
37 # ------------------------------------------------------------------
39 # if there is a raw time component (e.g. from postgres),
40 # turn it into an interval that interval_to_seconds can parse
41 my $duration = $circ->fine_interval;
42 $duration =~ s/(\d{2}):(\d{2}):(\d{2})/$1 h $2 m $3 s/o;
43 my $interval = OpenSRF::Utils->interval_to_seconds($duration);
45 my $date = DateTime::Format::ISO8601->parse_datetime($backdate);
46 $backdate = $U->epoch2ISO8601($date->epoch + $interval);
47 $logger->info("applying backdate $backdate in overdue voiding");
48 $$bill_search{billing_ts} = {'>=' => $backdate};
51 my $bills = $e->search_money_billing($bill_search);
53 for my $bill (@$bills) {
54 next if $U->is_true($bill->voided);
55 $logger->info("voiding overdue bill ".$bill->id);
57 $bill->void_time('now');
58 $bill->voider($e->requestor->id);
59 my $n = ($bill->note) ? sprintf("%s\n", $bill->note) : "";
60 $bill->note(sprintf("$n%s", ($note) ? $note : "System: VOIDED FOR BACKDATE"));
61 $e->update_money_billing($bill) or return $e->die_event;
69 my($class, $e, $xactid) = @_;
71 # -----------------------------------------------------------------
72 # make sure the transaction is not closed
73 my $xact = $e->retrieve_money_billable_transaction($xactid)
74 or return $e->die_event;
76 if( $xact->xact_finish ) {
77 my ($mbts) = $U->fetch_mbts($xactid, $e);
78 if( $mbts->balance_owed != 0 ) {
79 $logger->info("* re-opening xact $xactid, orig xact_finish is ".$xact->xact_finish);
80 $xact->clear_xact_finish;
81 $e->update_money_billable_transaction($xact)
82 or return $e->die_event;
91 my($class, $e, $amount, $btype, $type, $xactid, $note) = @_;
93 $logger->info("The system is charging $amount [$type] on xact $xactid");
94 $note ||= 'SYSTEM GENERATED';
96 # -----------------------------------------------------------------
97 # now create the billing
98 my $bill = Fieldmapper::money::billing->new;
100 $bill->amount($amount);
101 $bill->billing_type($type);
102 $bill->btype($btype);
104 $e->create_money_billing($bill) or return $e->die_event;