]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/support-scripts/generate-fines.pl
never used
[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 qw/:datetime/;
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                 my $due_dt = $parser->parse_datetime( clense_ISO8601( $c->due_date ) );
30
31                 my $due = $due_dt->epoch;
32                 my $now = time;
33                 my $fine_interval = interval_to_seconds( $c->fine_interval );
34
35                 if ( interval_to_seconds( $c->fine_interval ) >= interval_to_seconds('1d') ) {  
36                         my $tz_offset_s = 0;;
37                         if ($due_dt->strftime('%z') =~ /(-|\+)(\d{2}):?(\d{2})/) {
38                                 $tz_offset_s = $1 . interval_to_seconds( "${2}h ${3}m"); 
39                         }
40
41                         $due -= ($due % $fine_interval) + $tz_offset_s;
42                         $now -= ($now % $fine_interval) + $tz_offset_s;
43                 }
44
45                 print   "\nARG! Overdue circulation ".$c->id.
46                         " for item ".$c->target_copy.
47                         " (user ".$c->usr.").\n".
48                         "\tItem was due on or before: ".localtime($due)."\n";
49
50                 my $fine = $session->request(
51                         'open-ils.storage.direct.money.billing.search',
52                         { xact => $c->id, voided => 'f' },
53                         { order_by => 'billing_ts DESC', limit => '1' }
54                 )->gather(1);
55
56                 my $last_fine;
57                 if ($fine) {
58                         $last_fine = $parser->parse_datetime( clense_ISO8601( $fine->billing_ts ) )->epoch;
59                 } else {
60                         $last_fine = $due;
61                         $last_fine += $fine_interval * $grace;
62                 }
63
64                 my $pending_fine_count = int( ($now - $last_fine) / $fine_interval ); 
65                 unless($pending_fine_count) {
66                         print "\tNo fines to create.  ";
67                         if ($grace && $now < $due + $fine_interval * $grace) {
68                                 print "Still inside grace period of: ".
69                                         seconds_to_interval( $fine_interval * $grace)."\n";
70                         } else {
71                                 print "Last fine generated for: ".localtime($last_fine)."\n";
72                         }
73                         next;
74                 }
75
76                 print "\t$pending_fine_count pending fine(s)\n";
77
78                 for my $bill (1 .. $pending_fine_count) {
79
80                         my $total = $session->request(
81                                 'open-ils.storage.direct.money.billable_transaction_summary.retrieve',
82                                 $c->id
83                         )->gather(1);
84
85                         if ($total && $total->balance_owed > $c->max_fine) {
86                                 $c->stop_fines('MAXFINES');
87                                 $session->request( 'open-ils.storage.direct.action.circulation.update', $c )->gather(1);
88                                 print "\tMaximum fine level of ".$c->max_fine." reached for this circulation.\n\tNo more fines will be generated.\n";
89                                 last;
90                         }
91
92                         my $billing = new Fieldmapper::money::billing;
93                         $billing->xact( $c->id );
94                         $billing->note( "Overdue Fine" );
95                         $billing->amount( $c->recuring_fine );
96
97                         $billing->billing_ts(
98                                 DateTime->from_epoch( epoch => $last_fine + $fine_interval * $bill )->strftime('%FT%T%z')
99                         );
100
101                         print   "\t\tCreating fine of ".$billing->amount." for period starting ".
102                                 localtime(
103                                         $parser->parse_datetime(
104                                                 clense_ISO8601( $billing->billing_ts )
105                                         )->epoch
106                                 )."\n";
107
108                         $session->request( 'open-ils.storage.direct.money.billing.create', $billing )->gather(1);
109                 }
110         }
111 } catch Error with {
112         my $e = shift;
113         die "Error processing overdue circulations:\n\n$e\n";
114 };
115
116