]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/CircCommon.pm
Make Evergreen Perl modules installable via Module::Build to match OpenSRF
[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
22 # -----------------------------------------------------------------
23 sub void_overdues {
24     my($class, $e, $circ, $backdate, $note) = @_;
25
26     my $bill_search = { 
27         xact => $circ->id, 
28         btype => 1 
29     };
30
31     if( $backdate ) {
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         # ------------------------------------------------------------------
38
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);
44
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};
49     }
50
51     my $bills = $e->search_money_billing($bill_search);
52     
53     for my $bill (@$bills) {
54         next if $U->is_true($bill->voided);
55         $logger->info("voiding overdue bill ".$bill->id);
56         $bill->voided('t');
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;
62     }
63
64         return undef;
65 }
66
67
68 sub reopen_xact {
69     my($class, $e, $xactid) = @_;
70
71     # -----------------------------------------------------------------
72     # make sure the transaction is not closed
73     my $xact = $e->retrieve_money_billable_transaction($xactid)
74         or return $e->die_event;
75
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;
83         } 
84     }
85
86     return undef;
87 }
88
89
90 sub create_bill {
91         my($class, $e, $amount, $btype, $type, $xactid, $note) = @_;
92
93         $logger->info("The system is charging $amount [$type] on xact $xactid");
94     $note ||= 'SYSTEM GENERATED';
95
96     # -----------------------------------------------------------------
97     # now create the billing
98         my $bill = Fieldmapper::money::billing->new;
99         $bill->xact($xactid);
100         $bill->amount($amount);
101         $bill->billing_type($type); 
102         $bill->btype($btype); 
103         $bill->note($note);
104     $e->create_money_billing($bill) or return $e->die_event;
105
106         return undef;
107 }
108
109 1;