]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/support-scripts/generate-fines.pl
78c6320cc2184bfa192a3c6223cfb4e86c659218
[Evergreen.git] / Open-ILS / src / support-scripts / generate-fines.pl
1 #!/usr/bin/perl -w
2 use strict;
3 use OpenSRF::EX qw/:try/;
4 use OpenSRF::System;
5 use OpenSRF::Application;
6 use OpenSRF::Utils::SettingsClient;
7 use OpenILS::Utils::Fieldmapper;
8 use OpenSRF::Utils;
9 use DateTime;
10 use DateTime::Format::ISO8601;
11 use Data::Dumper;
12
13 die "USAGE:\n\t$0 config_file [grace?]\n" unless @ARGV;
14
15 my $parser = DateTime::Format::ISO8601->new;
16
17 # hard coded for now, option later
18
19 OpenSRF::System->bootstrap_client( config_file => $ARGV[0] );
20 my $session = OpenSRF::AppSession->create('open-ils.storage');
21
22 my $grace = $ARGV[1];
23
24 try {
25         my $req = $session->request( 'open-ils.storage.action.circulation.overdue',$grace );
26         while (!$req->failed && (my $res = $req->recv)) {
27                 my $c = $res->content;
28
29                 print   "ARG! overdue circ ".$c->id.
30                         " for item ".$c->target_copy.
31                         " : was due at ".$c->due_date."\n";
32
33                 my $fine = $session->request(
34                         'open-ils.storage.direct.money.billing.search.xact',
35                         $c->id, { order_by => 'billing_ts DESC' }
36                 )->gather(1);
37
38                 my $now = time;
39                 my $fine_interval = OpenSRF::Utils->interval_to_seconds( $c->fine_interval );
40
41                 my $last_fine;
42                 if ($fine) {
43                         $last_fine = $parser->parse_datetime( OpenSRF::Utils->clense_ISO8601( $fine->billing_ts ))->epoch;
44                 } else {
45                         # Use Date::Manip here
46                         $last_fine = $parser->parse_datetime( OpenSRF::Utils->clense_ISO8601( $c->due_date ))->epoch;
47                         $last_fine += $fine_interval if ($grace);
48                 }
49
50                 my $pending_fine_count = int( ($now - $last_fine) / $fine_interval ); 
51                 next unless($pending_fine_count);
52
53                 print "Circ ".$c->id." has $pending_fine_count pending fine(s).\n";
54
55                 for my $bill (1 .. $pending_fine_count) {
56
57                         my $total = $session->request(
58                                 'open-ils.storage.money.billing.billable_transaction_summary',
59                                 $c->id
60                         )->gather(1);
61
62                         if ($total && $total->{balance_owed} > $c->max_fine) {
63                                 $c->stop_fines('MAXFINES');
64                                 
65                                 $session->request(
66                                         'open-ils.storage.direct.action.circulation.update',
67                                         $c
68                                 )->gather(1);
69
70                                 last;
71                         }
72
73                         my $billing = new Fieldmapper::money::billing;
74                         $billing->xact( $c->id );
75                         $billing->note( "Overdue Fine" );
76                         $billing->amount( $c->recuring_fine );
77                         $billing->billing_ts(
78                                 DateTime->from_epoch(
79                                         epoch => $last_fine + $fine_interval * $bill
80                                 )->strftime('%FT%T%z')
81                         );
82
83                         $session->request(
84                                 'open-ils.storage.direct.money.billing.create',
85                                 $billing
86                         )->gather(1);
87
88                 }
89         }
90
91 } catch Error with {
92         my $e = shift;
93         die "Error processing overdue circulations:\n\n$e\n";
94 };
95
96